hwaddr.c (3229) | hwaddr.c (13572) |
---|---|
1/* 2 * hwaddr.c - routines that deal with hardware addresses. 3 * (i.e. Ethernet) 4 */ 5 6#include <sys/types.h> 7#include <sys/param.h> 8#include <sys/socket.h> 9#include <sys/ioctl.h> 10 11#if defined(SUNOS) || defined(SVR4) 12#include <sys/sockio.h> 13#endif 14#ifdef SVR4 15#include <sys/stream.h> 16#include <stropts.h> 17#include <fcntl.h> 18#endif 19 | 1/* 2 * hwaddr.c - routines that deal with hardware addresses. 3 * (i.e. Ethernet) 4 */ 5 6#include <sys/types.h> 7#include <sys/param.h> 8#include <sys/socket.h> 9#include <sys/ioctl.h> 10 11#if defined(SUNOS) || defined(SVR4) 12#include <sys/sockio.h> 13#endif 14#ifdef SVR4 15#include <sys/stream.h> 16#include <stropts.h> 17#include <fcntl.h> 18#endif 19 |
20#ifdef _AIX32 21#include <sys/time.h> /* for struct timeval in net/if.h */ 22#include <net/if.h> /* for struct ifnet in net/if_arp.h */ 23#endif 24 |
|
20#include <net/if_arp.h> 21#include <netinet/in.h> | 25#include <net/if_arp.h> 26#include <netinet/in.h> |
27 28#ifdef WIN_TCP 29#include <netinet/if_ether.h> 30#include <sys/dlpi.h> 31#endif 32 |
|
22#include <stdio.h> 23#ifndef NO_UNISTD 24#include <unistd.h> 25#endif 26#include <syslog.h> 27 28#ifndef USE_BFUNCS 29/* Yes, memcpy is OK here (no overlapped copies). */ 30#include <memory.h> 31#define bcopy(a,b,c) memcpy(b,a,c) 32#define bzero(p,l) memset(p,0,l) 33#define bcmp(a,b,c) memcmp(a,b,c) 34#endif 35 | 33#include <stdio.h> 34#ifndef NO_UNISTD 35#include <unistd.h> 36#endif 37#include <syslog.h> 38 39#ifndef USE_BFUNCS 40/* Yes, memcpy is OK here (no overlapped copies). */ 41#include <memory.h> 42#define bcopy(a,b,c) memcpy(b,a,c) 43#define bzero(p,l) memset(p,0,l) 44#define bcmp(a,b,c) memcmp(a,b,c) 45#endif 46 |
36/* For BSD 4.4, set arp entry by writing to routing socket */ 37#if defined(BSD) 38#if BSD >= 199306 39extern int bsd_arp_set __P((struct in_addr *, char *, int)); | 47#ifndef ATF_INUSE /* Not defined on some systems (i.e. Linux) */ 48#define ATF_INUSE 0 |
40#endif | 49#endif |
41#endif | |
42 43#include "bptypes.h" 44#include "hwaddr.h" 45#include "report.h" 46 47extern int debug; 48 49/* --- 17 unchanged lines hidden (view full) --- 67int hwinfocnt = sizeof(hwinfolist) / sizeof(hwinfolist[0]); 68 69 70/* 71 * Setup the arp cache so that IP address 'ia' will be temporarily 72 * bound to hardware address 'ha' of length 'len'. 73 */ 74void | 50 51#include "bptypes.h" 52#include "hwaddr.h" 53#include "report.h" 54 55extern int debug; 56 57/* --- 17 unchanged lines hidden (view full) --- 75int hwinfocnt = sizeof(hwinfolist) / sizeof(hwinfolist[0]); 76 77 78/* 79 * Setup the arp cache so that IP address 'ia' will be temporarily 80 * bound to hardware address 'ha' of length 'len'. 81 */ 82void |
75setarp(s, ia, ha, len) | 83setarp(s, ia, hafamily, haddr, halen) |
76 int s; /* socket fd */ | 84 int s; /* socket fd */ |
77 struct in_addr *ia; 78 u_char *ha; 79 int len; | 85 struct in_addr *ia; /* protocol address */ 86 int hafamily; /* HW address family */ 87 u_char *haddr; /* HW address data */ 88 int halen; |
80{ 81#ifdef SIOCSARP | 89{ 90#ifdef SIOCSARP |
91#ifdef WIN_TCP 92 /* This is an SVR4 with different networking code from 93 * Wollongong WIN-TCP. Not quite like the Lachman code. 94 * Code from: drew@drewsun.FEITH.COM (Andrew B. Sudell) 95 */ 96#undef SIOCSARP 97#define SIOCSARP ARP_ADD 98 struct arptab arpreq; /* Arp table entry */ 99 100 bzero((caddr_t) &arpreq, sizeof(arpreq)); 101 arpreq.at_flags = ATF_COM; 102 103 /* Set up IP address */ 104 arpreq.at_in = ia->s_addr; 105 106 /* Set up Hardware Address */ 107 bcopy(haddr, arpreq.at_enaddr, halen); 108 109 /* Set the Date Link type. */ 110 /* XXX - Translate (hafamily) to dltype somehow? */ 111 arpreq.at_dltype = DL_ETHER; 112 113#else /* WIN_TCP */ 114 /* Good old Berkeley way. */ |
|
82 struct arpreq arpreq; /* Arp request ioctl block */ 83 struct sockaddr_in *si; | 115 struct arpreq arpreq; /* Arp request ioctl block */ 116 struct sockaddr_in *si; |
84#ifdef SVR4 85 int fd; 86 struct strioctl iocb; 87#endif /* SVR4 */ | 117 char *p; |
88 | 118 |
89 bzero((caddr_t) & arpreq, sizeof(arpreq)); | 119 bzero((caddr_t) &arpreq, sizeof(arpreq)); |
90 arpreq.arp_flags = ATF_INUSE | ATF_COM; 91 92 /* Set up the protocol address. */ 93 arpreq.arp_pa.sa_family = AF_INET; 94 si = (struct sockaddr_in *) &arpreq.arp_pa; 95 si->sin_addr = *ia; 96 97 /* Set up the hardware address. */ | 120 arpreq.arp_flags = ATF_INUSE | ATF_COM; 121 122 /* Set up the protocol address. */ 123 arpreq.arp_pa.sa_family = AF_INET; 124 si = (struct sockaddr_in *) &arpreq.arp_pa; 125 si->sin_addr = *ia; 126 127 /* Set up the hardware address. */ |
98 bcopy(ha, arpreq.arp_ha.sa_data, len); | 128#ifdef __linux__ /* XXX - Do others need this? -gwr */ 129 /* 130 * Linux requires the sa_family field set. 131 * longyear@netcom.com (Al Longyear) 132 */ 133 arpreq.arp_ha.sa_family = hafamily; 134#endif /* linux */ |
99 | 135 |
136 /* This variable is just to help catch type mismatches. */ 137 p = arpreq.arp_ha.sa_data; 138 bcopy(haddr, p, halen); 139#endif /* WIN_TCP */ 140 |
|
100#ifdef SVR4 101 /* 102 * And now the stuff for System V Rel 4.x which does not 103 * appear to allow SIOCxxx ioctls on a socket descriptor. 104 * Thanks to several people: (all sent the same fix) 105 * Barney Wolff <barney@databus.com>, 106 * bear@upsys.se (Bj|rn Sj|holm), 107 * Michael Kuschke <Michael.Kuschke@Materna.DE>, 108 */ | 141#ifdef SVR4 142 /* 143 * And now the stuff for System V Rel 4.x which does not 144 * appear to allow SIOCxxx ioctls on a socket descriptor. 145 * Thanks to several people: (all sent the same fix) 146 * Barney Wolff <barney@databus.com>, 147 * bear@upsys.se (Bj|rn Sj|holm), 148 * Michael Kuschke <Michael.Kuschke@Materna.DE>, 149 */ |
109 if ((fd=open("/dev/arp", O_RDWR)) < 0) { 110 report(LOG_ERR, "open /dev/arp: %s\n", get_errmsg()); 111 } 112 iocb.ic_cmd = SIOCSARP; 113 iocb.ic_timout = 0; 114 iocb.ic_dp = (char *)&arpreq; 115 iocb.ic_len = sizeof(arpreq); 116 if (ioctl(fd, I_STR, (caddr_t)&iocb) < 0) { 117 report(LOG_ERR, "ioctl I_STR: %s\n", get_errmsg()); 118 } 119 close (fd); | 150 { 151 int fd; 152 struct strioctl iocb; |
120 | 153 |
154 if ((fd=open("/dev/arp", O_RDWR)) < 0) { 155 report(LOG_ERR, "open /dev/arp: %s\n", get_errmsg()); 156 } 157 iocb.ic_cmd = SIOCSARP; 158 iocb.ic_timout = 0; 159 iocb.ic_dp = (char *)&arpreq; 160 iocb.ic_len = sizeof(arpreq); 161 if (ioctl(fd, I_STR, (caddr_t)&iocb) < 0) { 162 report(LOG_ERR, "ioctl I_STR: %s\n", get_errmsg()); 163 } 164 close (fd); 165 } |
|
121#else /* SVR4 */ 122 /* 123 * On SunOS, the ioctl sometimes returns ENXIO, and it 124 * appears to happen when the ARP cache entry you tried 125 * to add is already in the cache. (Sigh...) 126 * XXX - Should this error simply be ignored? -gwr 127 */ | 166#else /* SVR4 */ 167 /* 168 * On SunOS, the ioctl sometimes returns ENXIO, and it 169 * appears to happen when the ARP cache entry you tried 170 * to add is already in the cache. (Sigh...) 171 * XXX - Should this error simply be ignored? -gwr 172 */ |
128 if (ioctl(s, SIOCSARP, (caddr_t) & arpreq) < 0) { | 173 if (ioctl(s, SIOCSARP, (caddr_t) &arpreq) < 0) { |
129 report(LOG_ERR, "ioctl SIOCSARP: %s", get_errmsg()); 130 } 131#endif /* SVR4 */ 132#else /* SIOCSARP */ | 174 report(LOG_ERR, "ioctl SIOCSARP: %s", get_errmsg()); 175 } 176#endif /* SVR4 */ 177#else /* SIOCSARP */ |
133#if defined(BSD) && (BSD >= 199306) 134 bsd_arp_set(ia, ha, len); 135#else /* Not BSD 4.4, and SIOCSARP not defined */ | |
136 /* 137 * Oh well, SIOCSARP is not defined. Just run arp(8). | 178 /* 179 * Oh well, SIOCSARP is not defined. Just run arp(8). |
180 * Need to delete partial entry first on some systems. |
|
138 * XXX - Gag! 139 */ | 181 * XXX - Gag! 182 */ |
140 char buf[256]; | |
141 int status; | 183 int status; |
184 char buf[256]; 185 char *a; 186 extern char *inet_ntoa(); |
|
142 | 187 |
143 sprintf(buf, "arp -s %s %s temp", 144 inet_ntoa(*ia), haddrtoa(ha, len)); | 188 a = inet_ntoa(*ia); 189 sprintf(buf, "arp -d %s; arp -s %s %s temp", 190 a, a, haddrtoa(haddr, halen)); |
145 if (debug > 2) 146 report(LOG_INFO, buf); 147 status = system(buf); 148 if (status) 149 report(LOG_ERR, "arp failed, exit code=0x%x", status); 150 return; | 191 if (debug > 2) 192 report(LOG_INFO, buf); 193 status = system(buf); 194 if (status) 195 report(LOG_ERR, "arp failed, exit code=0x%x", status); 196 return; |
151#endif /* ! 4.4 BSD */ | |
152#endif /* SIOCSARP */ 153} 154 155 156/* 157 * Convert a hardware address to an ASCII string. 158 */ 159char * --- 135 unchanged lines hidden --- | 197#endif /* SIOCSARP */ 198} 199 200 201/* 202 * Convert a hardware address to an ASCII string. 203 */ 204char * --- 135 unchanged lines hidden --- |