MFC: An off-by-one malloc size was corrupting the installer's memory,
[dragonfly.git] / contrib / ipfilter / ipsend / 44arp.c
blobde9f4d9ce703d3005e6361b5fa148929a6d9350a
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"
36 * lookup host and return
37 * its IP address in address
38 * (4 bytes)
40 int resolve(host, address)
41 char *host, *address;
43 struct hostent *hp;
44 u_long add;
46 add = inet_addr(host);
47 if (add == -1)
49 if (!(hp = gethostbyname(host)))
51 fprintf(stderr, "unknown host: %s\n", host);
52 return -1;
54 bcopy((char *)hp->h_addr, (char *)address, 4);
55 return 0;
57 bcopy((char*)&add, address, 4);
58 return 0;
62 int arp(addr, eaddr)
63 char *addr, *eaddr;
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;
72 #ifdef IPSEND
73 if (arp_getipv4(addr, ether) == 0)
74 return 0;
75 #endif
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)
85 perror("route-sysctl-estimate");
86 exit(-1);
88 if ((buf = malloc(needed)) == NULL)
90 perror("malloc");
91 exit(-1);
93 if (sysctl(mib, 6, buf, &needed, NULL, 0) == -1)
95 perror("actual retrieval of routing table");
96 exit(-1);
98 lim = buf + needed;
99 for (next = buf; next < lim; next += rtm->rtm_msglen)
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)))
107 bcopy(LLADDR(sdl), eaddr, sdl->sdl_alen);
108 return 0;
111 return -1;