]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/ipfilter/ipsend/arp.c
Import IPFilter 4.1.23 to vendor branch.
[FreeBSD/FreeBSD.git] / contrib / ipfilter / ipsend / arp.c
1 /*
2  * arp.c (C) 1995-1998 Darren Reed
3  *
4  * See the IPFILTER.LICENCE file for details on licencing.
5  */
6 #if !defined(lint)
7 static const char sccsid[] = "@(#)arp.c 1.4 1/11/96 (C)1995 Darren Reed";
8 static const char rcsid[] = "@(#)$Id: arp.c,v 2.8.2.2 2007/02/17 12:41:50 darrenr Exp $";
9 #endif
10 #include <sys/types.h>
11 #include <sys/socket.h>
12 #if !defined(ultrix) && !defined(hpux) && !defined(__hpux) && !defined(__osf__) && !defined(_AIX51)
13 # include <sys/sockio.h>
14 #endif
15 #include <sys/ioctl.h>
16 #include <netinet/in_systm.h>
17 #include <netinet/in.h>
18 #ifdef __osf__
19 # include "radix_ipf_local.h"
20 #endif
21 #include <net/if.h>
22 #include <netinet/if_ether.h>
23 #ifndef ultrix
24 # include <net/if_arp.h>
25 #endif
26 #include <netinet/in.h>
27 #include <netinet/ip.h>
28 #include <netinet/ip_var.h>
29 #include <netinet/tcp.h>
30 #include <stdio.h>
31 #include <errno.h>
32 #include <netdb.h>
33 #include "ipsend.h"
34 #include "iplang/iplang.h"
35
36
37 /*
38  * lookup host and return
39  * its IP address in address
40  * (4 bytes)
41  */
42 int     resolve(host, address)
43 char    *host, *address;
44 {
45         struct  hostent *hp;
46         u_long  add;
47
48         add = inet_addr(host);
49         if (add == -1)
50             {
51                 if (!(hp = gethostbyname(host)))
52                     {
53                         fprintf(stderr, "unknown host: %s\n", host);
54                         return -1;
55                     }
56                 bcopy((char *)hp->h_addr, (char *)address, 4);
57                 return 0;
58         }
59         bcopy((char*)&add, address, 4);
60         return 0;
61 }
62
63 /*
64  * ARP for the MAC address corresponding
65  * to the IP address.  This taken from
66  * some BSD program, I cant remember which.
67  */
68 int     arp(ip, ether)
69 char    *ip;
70 char    *ether;
71 {
72         static  int     sfd = -1;
73         static  char    ethersave[6], ipsave[4];
74         struct  arpreq  ar;
75         struct  sockaddr_in     *sin, san;
76         struct  hostent *hp;
77         int     fd;
78
79 #ifdef  IPSEND
80         if (arp_getipv4(ip, ether) == 0)
81                 return 0;
82 #endif
83         if (!bcmp(ipsave, ip, 4)) {
84                 bcopy(ethersave, ether, 6);
85                 return 0;
86         }
87         fd = -1;
88         bzero((char *)&ar, sizeof(ar));
89         sin = (struct sockaddr_in *)&ar.arp_pa;
90         sin->sin_family = AF_INET;
91         bcopy(ip, (char *)&sin->sin_addr.s_addr, 4);
92 #ifndef hpux
93         if ((hp = gethostbyaddr(ip, 4, AF_INET)))
94 # if SOLARIS && (SOLARIS2 >= 10)
95                 if (!(ether_hostton(hp->h_name, (struct ether_addr *)ether)))
96 # else
97                 if (!(ether_hostton(hp->h_name, ether)))
98 # endif
99                         goto savearp;
100 #endif
101
102         if (sfd == -1)
103                 if ((sfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
104                     {
105                         perror("arp: socket");
106                         return -1;
107                     }
108 tryagain:
109         if (ioctl(sfd, SIOCGARP, (caddr_t)&ar) == -1)
110             {
111                 if (fd == -1)
112                     {
113                         bzero((char *)&san, sizeof(san));
114                         san.sin_family = AF_INET;
115                         san.sin_port = htons(1);
116                         bcopy(ip, &san.sin_addr.s_addr, 4);
117                         fd = socket(AF_INET, SOCK_DGRAM, 0);
118                         (void) sendto(fd, ip, 4, 0,
119                                       (struct sockaddr *)&san, sizeof(san));
120                         sleep(1);
121                         (void) close(fd);
122                         goto tryagain;
123                     }
124                 fprintf(stderr, "(%s):", inet_ntoa(sin->sin_addr));
125                 if (errno != ENXIO)
126                         perror("SIOCGARP");
127                 return -1;
128             }
129
130         if ((ar.arp_ha.sa_data[0] == 0) && (ar.arp_ha.sa_data[1] == 0) &&
131             (ar.arp_ha.sa_data[2] == 0) && (ar.arp_ha.sa_data[3] == 0) &&
132             (ar.arp_ha.sa_data[4] == 0) && (ar.arp_ha.sa_data[5] == 0)) {
133                 fprintf(stderr, "(%s):", inet_ntoa(sin->sin_addr));
134                 return -1;
135         }
136
137         bcopy(ar.arp_ha.sa_data, ether, 6);
138 savearp:
139         bcopy(ether, ethersave, 6);
140         bcopy(ip, ipsave, 4);
141         return 0;
142 }