]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - ipsend/larp.c
As per the developers handbook (5.3.1 step 1), prepare the vendor trees for
[FreeBSD/FreeBSD.git] / ipsend / larp.c
1 /*
2  * larp.c (C) 1995-1998 Darren Reed
3  *
4  * See the IPFILTER.LICENCE file for details on licencing.
5  *
6  */
7 #if !defined(lint)
8 static const char sccsid[] = "@(#)larp.c        1.1 8/19/95 (C)1995 Darren Reed";
9 static const char rcsid[] = "@(#)$Id: larp.c,v 2.4 2003/12/01 02:01:16 darrenr Exp $";
10 #endif
11 #include <sys/param.h>
12 #include <sys/types.h>
13 #include <sys/socket.h>
14 #include <sys/ioctl.h>
15 #include <netinet/in.h>
16 #include <net/if.h>
17 #include <net/if_arp.h>
18 #include <stdio.h>
19 #include <netdb.h>
20 #include <errno.h>
21
22 #include "ip_compat.h"
23 #include "iplang/iplang.h"
24
25 /*
26  * lookup host and return
27  * its IP address in address
28  * (4 bytes)
29  */
30 int     resolve(host, address)
31 char    *host, *address;
32 {
33         struct  hostent *hp;
34         u_long  add;
35
36         add = inet_addr(host);
37         if (add == -1)
38             {
39                 if (!(hp = gethostbyname(host)))
40                     {
41                         fprintf(stderr, "unknown host: %s\n", host);
42                         return -1;
43                     }
44                 bcopy((char *)hp->h_addr, (char *)address, 4);
45                 return 0;
46         }
47         bcopy((char*)&add, address, 4);
48         return 0;
49 }
50
51 /*
52  * ARP for the MAC address corresponding
53  * to the IP address.  This taken from
54  * some BSD program, I cant remember which.
55  */
56 int     arp(ip, ether)
57 char    *ip;
58 char    *ether;
59 {
60         static  int     s = -1;
61         struct  arpreq  ar;
62         struct  sockaddr_in     *sin;
63         char    *inet_ntoa();
64
65 #ifdef  IP_SEND
66         if (arp_getipv4(ip, ether) == 0)
67                 return 0;
68 #endif
69         bzero((char *)&ar, sizeof(ar));
70         sin = (struct sockaddr_in *)&ar.arp_pa;
71         sin->sin_family = AF_INET;
72         bcopy(ip, (char *)&sin->sin_addr.s_addr, 4);
73
74         if (s == -1)
75                 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
76                     {
77                         perror("arp: socket");
78                         return -1;
79                     }
80
81         if (ioctl(s, SIOCGARP, (caddr_t)&ar) == -1)
82             {
83                 fprintf(stderr, "(%s):", inet_ntoa(sin->sin_addr));
84                 if (errno != ENXIO)
85                         perror("SIOCGARP");
86                 return -1;
87             }
88
89         bcopy(ar.arp_ha.sa_data, ether, 6);
90         return 0;
91 }