1*09973b35Sozaki-r /* $NetBSD: 44arp.c,v 1.2 2016/04/04 07:37:07 ozaki-r Exp $ */
2bc4097aaSchristos
3bc4097aaSchristos /*
4bc4097aaSchristos * Based upon 4.4BSD's /usr/sbin/arp
5bc4097aaSchristos */
6bc4097aaSchristos #include <sys/param.h>
7bc4097aaSchristos #include <sys/file.h>
8bc4097aaSchristos #include <sys/socket.h>
9bc4097aaSchristos #include <sys/sysctl.h>
10bc4097aaSchristos #include <net/if.h>
11bc4097aaSchristos #if __FreeBSD_version >= 300000
12bc4097aaSchristos # include <net/if_var.h>
13bc4097aaSchristos #endif
14bc4097aaSchristos #include <net/if_dl.h>
15bc4097aaSchristos #include <net/if_types.h>
16bc4097aaSchristos #ifndef __osf__
17bc4097aaSchristos # include <net/route.h>
18bc4097aaSchristos #endif
19bc4097aaSchristos #include <netinet/in.h>
20bc4097aaSchristos #include <netinet/if_ether.h>
21bc4097aaSchristos #include <arpa/inet.h>
22bc4097aaSchristos #include <netinet/in.h>
23bc4097aaSchristos #include <netinet/in_systm.h>
24bc4097aaSchristos #include <netinet/ip.h>
25bc4097aaSchristos #include <netinet/ip_var.h>
26bc4097aaSchristos #include <netinet/tcp.h>
27bc4097aaSchristos #include <unistd.h>
28bc4097aaSchristos #include <string.h>
29bc4097aaSchristos #include <stdlib.h>
30bc4097aaSchristos #include <netdb.h>
31bc4097aaSchristos #include <errno.h>
32bc4097aaSchristos #include <nlist.h>
33bc4097aaSchristos #include <stdio.h>
34bc4097aaSchristos #include "ipsend.h"
35bc4097aaSchristos #include "iplang/iplang.h"
36bc4097aaSchristos
37bc4097aaSchristos
38bc4097aaSchristos /*
39bc4097aaSchristos * lookup host and return
40bc4097aaSchristos * its IP address in address
41bc4097aaSchristos * (4 bytes)
42bc4097aaSchristos */
resolve(host,address)43bc4097aaSchristos int resolve(host, address)
44bc4097aaSchristos char *host, *address;
45bc4097aaSchristos {
46bc4097aaSchristos struct hostent *hp;
47bc4097aaSchristos u_long add;
48bc4097aaSchristos
49bc4097aaSchristos add = inet_addr(host);
50bc4097aaSchristos if (add == -1)
51bc4097aaSchristos {
52bc4097aaSchristos if (!(hp = gethostbyname(host)))
53bc4097aaSchristos {
54bc4097aaSchristos fprintf(stderr, "unknown host: %s\n", host);
55bc4097aaSchristos return -1;
56bc4097aaSchristos }
57bc4097aaSchristos bcopy((char *)hp->h_addr, (char *)address, 4);
58bc4097aaSchristos return 0;
59bc4097aaSchristos }
60bc4097aaSchristos bcopy((char*)&add, address, 4);
61bc4097aaSchristos return 0;
62bc4097aaSchristos }
63bc4097aaSchristos
64bc4097aaSchristos
arp(addr,eaddr)65bc4097aaSchristos int arp(addr, eaddr)
66bc4097aaSchristos char *addr, *eaddr;
67bc4097aaSchristos {
68bc4097aaSchristos int mib[6];
69bc4097aaSchristos size_t needed;
70bc4097aaSchristos char *lim, *buf, *next;
71bc4097aaSchristos struct rt_msghdr *rtm;
72bc4097aaSchristos struct sockaddr_inarp *sin;
73bc4097aaSchristos struct sockaddr_dl *sdl;
74bc4097aaSchristos
75bc4097aaSchristos #ifdef IPSEND
76bc4097aaSchristos if (arp_getipv4(addr, ether) == 0)
77bc4097aaSchristos return 0;
78bc4097aaSchristos #endif
79bc4097aaSchristos
80bc4097aaSchristos if (!addr)
81bc4097aaSchristos return -1;
82bc4097aaSchristos
83bc4097aaSchristos mib[0] = CTL_NET;
84bc4097aaSchristos mib[1] = PF_ROUTE;
85bc4097aaSchristos mib[2] = 0;
86bc4097aaSchristos mib[3] = AF_INET;
87bc4097aaSchristos mib[4] = NET_RT_FLAGS;
88*09973b35Sozaki-r #ifdef RTF_LLINFO
89bc4097aaSchristos mib[5] = RTF_LLINFO;
90*09973b35Sozaki-r #else
91*09973b35Sozaki-r mib[5] = 0;
92*09973b35Sozaki-r #endif
93bc4097aaSchristos if (sysctl(mib, 6, NULL, &needed, NULL, 0) == -1)
94bc4097aaSchristos {
95bc4097aaSchristos perror("route-sysctl-estimate");
96bc4097aaSchristos exit(-1);
97bc4097aaSchristos }
98bc4097aaSchristos if ((buf = malloc(needed)) == NULL)
99bc4097aaSchristos {
100bc4097aaSchristos perror("malloc");
101bc4097aaSchristos exit(-1);
102bc4097aaSchristos }
103bc4097aaSchristos if (sysctl(mib, 6, buf, &needed, NULL, 0) == -1)
104bc4097aaSchristos {
105bc4097aaSchristos perror("actual retrieval of routing table");
106bc4097aaSchristos exit(-1);
107bc4097aaSchristos }
108bc4097aaSchristos lim = buf + needed;
109bc4097aaSchristos for (next = buf; next < lim; next += rtm->rtm_msglen)
110bc4097aaSchristos {
111bc4097aaSchristos rtm = (struct rt_msghdr *)next;
112bc4097aaSchristos sin = (struct sockaddr_inarp *)(rtm + 1);
113bc4097aaSchristos sdl = (struct sockaddr_dl *)(sin + 1);
114bc4097aaSchristos if (!bcmp(addr, (char *)&sin->sin_addr,
115bc4097aaSchristos sizeof(struct in_addr)))
116bc4097aaSchristos {
117bc4097aaSchristos bcopy(LLADDR(sdl), eaddr, sdl->sdl_alen);
118bc4097aaSchristos return 0;
119bc4097aaSchristos }
120bc4097aaSchristos }
121bc4097aaSchristos return -1;
122bc4097aaSchristos }
123