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