Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / contrib / ipfilter / ipsend / 44arp.c
1 /*
2  * Based upon 4.4BSD's /usr/sbin/arp
3  */
4 #if defined(__sgi) && (IRIX > 602)
5 # include <sys/ptimers.h>
6 #endif
7 #include <unistd.h>
8 #include <string.h>
9 #include <stdlib.h>
10 #include <sys/param.h>
11 #include <sys/file.h>
12 #include <sys/socket.h>
13 #include <sys/sysctl.h>
14 #include <net/if.h>
15 #include <net/if_dl.h>
16 #include <net/if_types.h>
17 #include <net/route.h>
18 #include <netinet/in.h>
19 #include <netinet/if_ether.h>
20 #include <arpa/inet.h>
21 #include <netdb.h>
22 #include <errno.h>
23 #include <nlist.h>
24 #include <stdio.h>
25 #include <netinet/in.h>
26 #include <netinet/ip_var.h>
27 #include <netinet/tcp.h>
28 #if __FreeBSD_version >= 300000
29 # include <net/if_var.h>
30 #endif
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_inarp  *sin;
70         struct  sockaddr_dl     *sdl;
71
72 #ifdef  IPSEND
73         if (arp_getipv4(addr, ether) == 0)
74                 return 0;
75 #endif
76
77         mib[0] = CTL_NET;
78         mib[1] = PF_ROUTE;
79         mib[2] = 0;
80         mib[3] = AF_INET;
81         mib[4] = NET_RT_FLAGS;
82         mib[5] = RTF_LLINFO;
83         if (sysctl(mib, 6, NULL, &needed, NULL, 0) == -1)
84             {
85                 perror("route-sysctl-estimate");
86                 exit(-1);
87             }
88         if ((buf = malloc(needed)) == NULL)
89             {
90                 perror("malloc");
91                 exit(-1);
92             }
93         if (sysctl(mib, 6, buf, &needed, NULL, 0) == -1)
94             {
95                 perror("actual retrieval of routing table");
96                 exit(-1);
97             }
98         lim = buf + needed;
99         for (next = buf; next < lim; next += rtm->rtm_msglen)
100             {
101                 rtm = (struct rt_msghdr *)next;
102                 sin = (struct sockaddr_inarp *)(rtm + 1);
103                 sdl = (struct sockaddr_dl *)(sin + 1);
104                 if (addr && !bcmp(addr, (char *)&sin->sin_addr,
105                                   sizeof(struct in_addr)))
106                     {
107                         bcopy(LLADDR(sdl), eaddr, sdl->sdl_alen);
108                         return 0;
109                     }
110             }
111         return -1;
112 }