- Test m_pkthdr.fw_flags against DUMMYNET_MBUF_TAGGED before trying to locate
[dragonfly/netmp.git] / contrib / ipfilter / ipsend / arp.c
blob8e5f7f4b8d429adcdb553a42f4f252343421fce4
1 /*
2 * arp.c (C) 1995-1998 Darren Reed
4 * See the IPFILTER.LICENCE file for details on licencing.
5 */
6 #if defined(__sgi) && (IRIX > 602)
7 # include <sys/ptimers.h>
8 #endif
9 #include <stdio.h>
10 #include <errno.h>
11 #include <sys/types.h>
12 #include <sys/socket.h>
13 #if !defined(ultrix) && !defined(hpux)
14 #include <sys/sockio.h>
15 #endif
16 #include <sys/ioctl.h>
17 #include <netdb.h>
18 #include <netinet/in.h>
19 #include <net/if.h>
20 #include <netinet/if_ether.h>
21 #ifndef ultrix
22 #include <net/if_arp.h>
23 #endif
24 #include <netinet/in.h>
25 #include <netinet/ip_var.h>
26 #include <netinet/tcp.h>
27 #include "ipsend.h"
28 #include "iplang/iplang.h"
30 #if !defined(lint)
31 static const char sccsid[] = "@(#)arp.c 1.4 1/11/96 (C)1995 Darren Reed";
32 static const char rcsid[] = "@(#)$Id: arp.c,v 2.1.4.4 2002/12/06 11:40:35 darrenr Exp $";
33 #endif
37 * lookup host and return
38 * its IP address in address
39 * (4 bytes)
41 int resolve(host, address)
42 char *host, *address;
44 struct hostent *hp;
45 u_long add;
47 add = inet_addr(host);
48 if (add == -1)
50 if (!(hp = gethostbyname(host)))
52 fprintf(stderr, "unknown host: %s\n", host);
53 return -1;
55 bcopy((char *)hp->h_addr, (char *)address, 4);
56 return 0;
58 bcopy((char*)&add, address, 4);
59 return 0;
63 * ARP for the MAC address corresponding
64 * to the IP address. This taken from
65 * some BSD program, I cant remember which.
67 int arp(ip, ether)
68 char *ip;
69 char *ether;
71 static int sfd = -1;
72 static char ethersave[6], ipsave[4];
73 struct arpreq ar;
74 struct sockaddr_in *sin, san;
75 struct hostent *hp;
76 int fd;
78 #ifdef IPSEND
79 if (arp_getipv4(ip, ether) == 0)
80 return 0;
81 #endif
82 if (!bcmp(ipsave, ip, 4)) {
83 bcopy(ethersave, ether, 6);
84 return 0;
86 fd = -1;
87 bzero((char *)&ar, sizeof(ar));
88 sin = (struct sockaddr_in *)&ar.arp_pa;
89 sin->sin_family = AF_INET;
90 bcopy(ip, (char *)&sin->sin_addr.s_addr, 4);
91 #ifndef hpux
92 if ((hp = gethostbyaddr(ip, 4, AF_INET)))
93 if (!(ether_hostton(hp->h_name, ether)))
94 goto savearp;
95 #endif
97 if (sfd == -1)
98 if ((sfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
100 perror("arp: socket");
101 return -1;
103 tryagain:
104 if (ioctl(sfd, SIOCGARP, (caddr_t)&ar) == -1)
106 if (fd == -1)
108 bzero((char *)&san, sizeof(san));
109 san.sin_family = AF_INET;
110 san.sin_port = htons(1);
111 bcopy(ip, &san.sin_addr.s_addr, 4);
112 fd = socket(AF_INET, SOCK_DGRAM, 0);
113 (void) sendto(fd, ip, 4, 0,
114 (struct sockaddr *)&san, sizeof(san));
115 sleep(1);
116 (void) close(fd);
117 goto tryagain;
119 fprintf(stderr, "(%s):", inet_ntoa(sin->sin_addr));
120 if (errno != ENXIO)
121 perror("SIOCGARP");
122 return -1;
125 bcopy(ar.arp_ha.sa_data, ether, 6);
126 savearp:
127 bcopy(ether, ethersave, 6);
128 bcopy(ip, ipsave, 4);
129 return 0;