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