xref: /freebsd/sbin/ipf/ipsend/44arp.c (revision 2a63c3be158216222d89a073dcbd6a72ee4aab5a)
141edb306SCy Schubert 
241edb306SCy Schubert /*
341edb306SCy Schubert  * Based upon 4.4BSD's /usr/sbin/arp
441edb306SCy Schubert  */
541edb306SCy Schubert #include <sys/param.h>
641edb306SCy Schubert #include <sys/file.h>
741edb306SCy Schubert #include <sys/socket.h>
841edb306SCy Schubert #include <sys/sysctl.h>
941edb306SCy Schubert #include <net/if.h>
1041edb306SCy Schubert #include <net/if_dl.h>
1141edb306SCy Schubert #include <net/if_types.h>
1241edb306SCy Schubert # include <net/route.h>
1341edb306SCy Schubert #include <netinet/in.h>
1441edb306SCy Schubert #include <netinet/if_ether.h>
1541edb306SCy Schubert #include <arpa/inet.h>
1641edb306SCy Schubert #include <netinet/in.h>
1741edb306SCy Schubert #include <netinet/in_systm.h>
1841edb306SCy Schubert #include <netinet/ip.h>
1941edb306SCy Schubert #include <netinet/ip_var.h>
2041edb306SCy Schubert #include <netinet/tcp.h>
2141edb306SCy Schubert #include <unistd.h>
2241edb306SCy Schubert #include <string.h>
2341edb306SCy Schubert #include <stdlib.h>
2441edb306SCy Schubert #include <netdb.h>
2541edb306SCy Schubert #include <errno.h>
2641edb306SCy Schubert #include <nlist.h>
2741edb306SCy Schubert #include <stdio.h>
2841edb306SCy Schubert #include "ipsend.h"
2941edb306SCy Schubert #include "iplang/iplang.h"
3041edb306SCy Schubert 
3141edb306SCy Schubert 
3241edb306SCy Schubert /*
3341edb306SCy Schubert  * lookup host and return
3441edb306SCy Schubert  * its IP address in address
3541edb306SCy Schubert  * (4 bytes)
3641edb306SCy Schubert  */
resolve(char * host,char * address)37efeb8bffSCy Schubert int	resolve(char *host, char *address)
3841edb306SCy Schubert {
3941edb306SCy Schubert 	struct	hostent	*hp;
4041edb306SCy Schubert 	u_long	add;
4141edb306SCy Schubert 
4241edb306SCy Schubert 	add = inet_addr(host);
4341edb306SCy Schubert 	if (add == -1)
4441edb306SCy Schubert 	    {
4541edb306SCy Schubert 		if (!(hp = gethostbyname(host)))
4641edb306SCy Schubert 		    {
4741edb306SCy Schubert 			fprintf(stderr, "unknown host: %s\n", host);
48*2582ae57SCy Schubert 			return (-1);
4941edb306SCy Schubert 		    }
5041edb306SCy Schubert 		bcopy((char *)hp->h_addr, (char *)address, 4);
51*2582ae57SCy Schubert 		return (0);
5241edb306SCy Schubert 	}
5341edb306SCy Schubert 	bcopy((char*)&add, address, 4);
54*2582ae57SCy Schubert 	return (0);
5541edb306SCy Schubert }
5641edb306SCy Schubert 
5741edb306SCy Schubert 
arp(char * addr,char * eaddr)58efeb8bffSCy Schubert int	arp(char *addr, char *eaddr)
5941edb306SCy Schubert {
6041edb306SCy Schubert 	int	mib[6];
6141edb306SCy Schubert 	size_t	needed;
6241edb306SCy Schubert 	char	*lim, *buf, *next;
6341edb306SCy Schubert 	struct	rt_msghdr	*rtm;
6441edb306SCy Schubert 	struct	sockaddr_in	*sin;
6541edb306SCy Schubert 	struct	sockaddr_dl	*sdl;
6641edb306SCy Schubert 
6741edb306SCy Schubert #ifdef	IPSEND
6841edb306SCy Schubert 	if (arp_getipv4(addr, ether) == 0)
69*2582ae57SCy Schubert 		return (0);
7041edb306SCy Schubert #endif
7141edb306SCy Schubert 
7241edb306SCy Schubert 	if (!addr)
73*2582ae57SCy Schubert 		return (-1);
7441edb306SCy Schubert 
7541edb306SCy Schubert 	mib[0] = CTL_NET;
7641edb306SCy Schubert 	mib[1] = PF_ROUTE;
7741edb306SCy Schubert 	mib[2] = 0;
7841edb306SCy Schubert 	mib[3] = AF_INET;
7941edb306SCy Schubert 	mib[4] = NET_RT_FLAGS;
8041edb306SCy Schubert #ifdef RTF_LLINFO
8141edb306SCy Schubert 	mib[5] = RTF_LLINFO;
8241edb306SCy Schubert #else
8341edb306SCy Schubert 	mib[5] = 0;
8441edb306SCy Schubert #endif
8541edb306SCy Schubert 
8641edb306SCy Schubert 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) == -1)
8741edb306SCy Schubert 	    {
8841edb306SCy Schubert 		perror("route-sysctl-estimate");
8941edb306SCy Schubert 		exit(-1);
9041edb306SCy Schubert 	    }
9141edb306SCy Schubert 	if ((buf = malloc(needed)) == NULL)
9241edb306SCy Schubert 	    {
9341edb306SCy Schubert 		perror("malloc");
9441edb306SCy Schubert 		exit(-1);
9541edb306SCy Schubert 	    }
9641edb306SCy Schubert 	if (sysctl(mib, 6, buf, &needed, NULL, 0) == -1)
9741edb306SCy Schubert 	    {
9841edb306SCy Schubert 		perror("actual retrieval of routing table");
9941edb306SCy Schubert 		exit(-1);
10041edb306SCy Schubert 	    }
10141edb306SCy Schubert 	lim = buf + needed;
10241edb306SCy Schubert 	for (next = buf; next < lim; next += rtm->rtm_msglen)
10341edb306SCy Schubert 	    {
10441edb306SCy Schubert 		rtm = (struct rt_msghdr *)next;
10541edb306SCy Schubert 		sin = (struct sockaddr_in *)(rtm + 1);
10641edb306SCy Schubert 		sdl = (struct sockaddr_dl *)(sin + 1);
10741edb306SCy Schubert 		if (!bcmp(addr, (char *)&sin->sin_addr,
10841edb306SCy Schubert 			  sizeof(struct in_addr)))
10941edb306SCy Schubert 		    {
11041edb306SCy Schubert 			bcopy(LLADDR(sdl), eaddr, sdl->sdl_alen);
111*2582ae57SCy Schubert 			return (0);
11241edb306SCy Schubert 		    }
11341edb306SCy Schubert 	    }
114*2582ae57SCy Schubert 	return (-1);
11541edb306SCy Schubert }
116