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