2 * arp.c (C) 1995-1998 Darren Reed
4 * Redistribution and use in source and binary forms are permitted
5 * provided that this notice is preserved and due credit is given
6 * to the original author and the contributors.
9 static const char sccsid[] = "@(#)arp.c 1.4 1/11/96 (C)1995 Darren Reed";
10 static const char rcsid[] = "@(#)$Id: arp.c,v 2.1 1999/08/04 17:31:03 darrenr Exp $";
14 #include <sys/types.h>
15 #include <sys/socket.h>
16 #if !defined(ultrix) && !defined(hpux)
17 #include <sys/sockio.h>
19 #include <sys/ioctl.h>
21 #include <netinet/in.h>
23 #include <netinet/if_ether.h>
25 #include <net/if_arp.h>
27 #include <netinet/in.h>
28 #include <netinet/ip_var.h>
29 #include <netinet/tcp.h>
31 #include "iplang/iplang.h"
35 * lookup host and return
36 * its IP address in address
39 int resolve(host, address)
45 add = inet_addr(host);
48 if (!(hp = gethostbyname(host)))
50 fprintf(stderr, "unknown host: %s\n", host);
53 bcopy((char *)hp->h_addr, (char *)address, 4);
56 bcopy((char*)&add, address, 4);
61 * ARP for the MAC address corresponding
62 * to the IP address. This taken from
63 * some BSD program, I cant remember which.
70 static char ethersave[6], ipsave[4];
72 struct sockaddr_in *sin, san;
77 if (arp_getipv4(ip, ether) == 0)
80 if (!bcmp(ipsave, ip, 4)) {
81 bcopy(ethersave, ether, 6);
85 bzero((char *)&ar, sizeof(ar));
86 sin = (struct sockaddr_in *)&ar.arp_pa;
87 sin->sin_family = AF_INET;
88 bcopy(ip, (char *)&sin->sin_addr.s_addr, 4);
90 if ((hp = gethostbyaddr(ip, 4, AF_INET)))
91 if (!(ether_hostton(hp->h_name, ether)))
96 if ((sfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
98 perror("arp: socket");
102 if (ioctl(sfd, SIOCGARP, (caddr_t)&ar) == -1)
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));
117 fprintf(stderr, "(%s):", inet_ntoa(sin->sin_addr));
123 bcopy(ar.arp_ha.sa_data, ether, 6);
125 bcopy(ether, ethersave, 6);
126 bcopy(ip, ipsave, 4);