larp.c revision 31183
1/*
2 * larp.c (C) 1995-1997 Darren Reed
3 *
4 * Redistribution and use in source and binary forms are permitted
5 * provided that this notice is preserved and due credit is given
6 * to the original author and the contributors.
7 */
8#if !defined(lint)
9static const char sccsid[] = "@(#)larp.c	1.1 8/19/95 (C)1995 Darren Reed";
10static const char rcsid[] = "@(#)$Id: larp.c,v 2.0.2.3 1997/09/28 07:13:31 darrenr Exp $";
11#endif
12#include <stdio.h>
13#include <errno.h>
14#include <sys/types.h>
15#include <sys/socket.h>
16#include <sys/ioctl.h>
17#include <netdb.h>
18#include <netinet/in.h>
19#include <net/if.h>
20#include <net/if_arp.h>
21
22/*
23 * lookup host and return
24 * its IP address in address
25 * (4 bytes)
26 */
27int	resolve(host, address)
28char	*host, *address;
29{
30        struct	hostent	*hp;
31        u_long	add;
32
33	add = inet_addr(host);
34	if (add == -1)
35	    {
36		if (!(hp = gethostbyname(host)))
37		    {
38			fprintf(stderr, "unknown host: %s\n", host);
39			return -1;
40		    }
41		bcopy((char *)hp->h_addr, (char *)address, 4);
42		return 0;
43	}
44	bcopy((char*)&add, address, 4);
45	return 0;
46}
47
48/*
49 * ARP for the MAC address corresponding
50 * to the IP address.  This taken from
51 * some BSD program, I cant remember which.
52 */
53int	arp(ip, ether)
54char	*ip;
55char	*ether;
56{
57	static	int	s = -1;
58	struct	arpreq	ar;
59	struct	sockaddr_in	*sin;
60	char	*inet_ntoa();
61
62	bzero((char *)&ar, sizeof(ar));
63	sin = (struct sockaddr_in *)&ar.arp_pa;
64	sin->sin_family = AF_INET;
65	bcopy(ip, (char *)&sin->sin_addr.s_addr, 4);
66
67	if (s == -1)
68		if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
69		    {
70			perror("arp: socket");
71			return -1;
72		    }
73
74	if (ioctl(s, SIOCGARP, (caddr_t)&ar) == -1)
75	    {
76		fprintf(stderr, "(%s):", inet_ntoa(sin->sin_addr));
77		if (errno != ENXIO)
78			perror("SIOCGARP");
79		return -1;
80	    }
81
82	bcopy(ar.arp_ha.sa_data, ether, 6);
83	return 0;
84}
85