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