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