1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
|
/*
* $Id: netaddr.c,v 1.6 2009/04/15 16:21:40 c4chris Exp $
*/
#include "os.h"
/*
* netaddr.c - routines for working with network addresses.
*
* David A. Curry Jeffrey C. Mogul
* Purdue University Digital Equipment Corporation
* Engineering Computer Network Western Research Laboratory
* 1285 Electrical Engineering Building 250 University Avenue
* West Lafayette, IN 47907-1285 Palo Alto, CA 94301
* davy@ecn.purdue.edu mogul@decwrl.dec.com
*
*/
/*
* TODO:
* - replace the gethostbyname() functions with getaddrinfo()
* - look at this broken strict aliasing warning:
* In function 'setup_ouraddrs':
* netaddr.c:338: warning:
* dereferencing pointer 'sin' does break strict-aliasing rules
* netaddr.c:322: note: initialized from here
*/
#include <sys/param.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#ifdef SUNOS5
#include <sys/sockio.h>
#endif /* SUNOS5 */
#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in.h>
#include "nfswatch.h"
#include "externs.h"
#define MAXOURADDRS 32
ipaddrt ouraddrs[MAXOURADDRS];
static void setup_ouraddrs(void);
/*
* get_net_addrs - get network addresses of source and destination
* hosts, along with official host names.
*/
void
get_net_addrs(void)
{
register int n, i;
register char **cp;
struct hostent *hp;
/*
* Look up the local host.
*/
if ((hp = gethostbyname(myhost)) == NULL) {
(void) fprintf(stderr, "%s: %s: unknown host.\n", pname,
myhost);
finish(-1);
}
setup_ouraddrs();
/*
* Save the official host name.
*/
(void) strcpy(myhost, hp->h_name);
/*
* If one was specified, look up the destination host.
* Otherwise, we can use what we have.
*/
if (allflag) {
(void) sprintf(dsthost, "all hosts");
}
else if (dstflag) {
if ((hp = gethostbyname(dsthost)) == NULL) {
(void) fprintf(stderr, "%s: %s: unknown host.\n", pname,
dsthost);
finish(-1);
}
/*
* Save the official host name.
*/
(void) strcpy(dsthost, hp->h_name);
}
else {
/*
* Host name is the same as the local
* host.
*/
(void) strcpy(dsthost, myhost);
}
/* Setup the dst addrs we are interested in. */
n = 0;
memset(dstaddrs, 0, MAXHOSTADDR * sizeof(ipaddrt));
/* Add ouraddrs. */
for (i=0; (ouraddrs[i] != 0) && (i < MAXOURADDRS); i++) {
dstaddrs[n++] = ouraddrs[i];
}
/*
* Copy destination host's network addresses.
*/
for (cp = hp->h_addr_list; *cp != NULL; cp++) {
if (n >= MAXHOSTADDR)
break;
memcpy(&dstaddrs[n], *cp, hp->h_length);
n++;
}
/*
* If they specified a server host, get its addresses.
*/
if (serverflag) {
if ((hp = gethostbyname(serverhost)) == NULL) {
fprintf(stderr, "%s: %s: unknown host.\n", pname,
serverhost);
finish(-1);
}
/*
* Save the official host name.
*/
(void) strcpy(serverhost, hp->h_name);
/*
* Copy the server's network addresses.
*/
n = 0;
memset(serveraddrs, 0, MAXHOSTADDR * sizeof(ipaddrt));
for (cp = hp->h_addr_list; *cp != NULL; cp++) {
if (n >= MAXHOSTADDR)
break;
memcpy(&serveraddrs[n], *cp, hp->h_length);
n++;
}
}
/*
* If they didn't specify a source host,
* we're done.
*/
if (!srcflag)
return;
/*
* Look up the source host.
*/
if ((hp = gethostbyname(srchost)) == NULL) {
(void) fprintf(stderr, "%s: %s: unknown host.\n", pname,
srchost);
finish(-1);
}
/*
* Save the official host name.
*/
(void) strcpy(srchost, hp->h_name);
/*
* Copy source host's network addresses.
*/
n = 0;
memset(srcaddrs, 0, MAXHOSTADDR * sizeof(ipaddrt));
for (cp = hp->h_addr_list; *cp != NULL; cp++) {
if (n >= MAXHOSTADDR)
break;
memcpy(&srcaddrs[n], *cp, hp->h_length);
n++;
}
}
/*
* want_packet - determine if we're interested in a packet by examining
* its source and destination addresses.
*/
int
want_packet(ipaddrt src, ipaddrt dst)
{
register int i, want;
want = FALSE;
thisdst = dst;
/*
* Check that the source or destination is the server.
*/
if (serverflag) {
for (i=0; (serveraddrs[i] != 0) && (i < MAXHOSTADDR); i++) {
if (!bcmp((char *) &src, (char *) &serveraddrs[i],
sizeof(ipaddrt)) ||
!bcmp((char *) &dst, (char *) &serveraddrs[i],
sizeof(ipaddrt))) {
want = TRUE;
break;
}
}
return(want);
}
/*
* Any source or destination is okay.
*/
if (allflag) {
return(TRUE);
}
/*
* Check source address first.
*/
if (srcflag) {
for (i = 0; (srcaddrs[i] != 0) && (i < MAXHOSTADDR); i++) {
if (!bcmp((char *) &src, (char *) &srcaddrs[i],
sizeof(ipaddrt))) {
want = TRUE;
break;
}
}
/*
* If it's not from our source, we
* don't even need to check the destination.
*/
if (!want)
return(FALSE);
}
want = FALSE;
/*
* Check destination address.
*/
for (i = 0; (dstaddrs[i] != 0) && (i < MAXHOSTADDR); i++) {
if (!bcmp((char *) &dst, (char *) &dstaddrs[i],
sizeof(ipaddrt))) {
want = TRUE;
break;
}
}
return(want);
}
/*
* to_self - determine if packet destination is the local host
*/
int
to_self(ipaddrt dst)
{
register int i;
/*
* Check if the destination is one of our addresses
*/
for (i=0; (ouraddrs[i] != 0) && (i < MAXOURADDRS); i++) {
if (ouraddrs[i] == dst) {
return(1);
}
}
return(0);
}
/* Not all systems have IFF_LOOPBACK */
#ifdef IFF_LOOPBACK
#define ISLOOPBACK(p) ((p)->ifr_flags & IFF_LOOPBACK)
#else
#define ISLOOPBACK(p) (strcmp((p)->ifr_name, "lo0") == 0)
#endif
/*
* Make a list of our possible IP addresses
* A lot of the code in setup_ouraddrs() was borrowed from tcpdump/
*/
static void
setup_ouraddrs(void)
{
struct ifreq ibuf[MAXOURADDRS], *ifrp, *ifend;
struct ifconf ifc;
int n;
int fd;
n = 0;
memset(ouraddrs, 0, MAXOURADDRS * sizeof(ipaddrt));
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
perror("socket");
finish(-1);
}
ifc.ifc_len = sizeof ibuf;
ifc.ifc_buf = (caddr_t)ibuf;
if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0 ||
(unsigned int) ifc.ifc_len < sizeof(struct ifreq)) {
perror("SIOCGIFCONF");
finish(-1);
}
ifrp = ibuf;
ifend = (struct ifreq *)((char *)ibuf + ifc.ifc_len);
while (ifrp < ifend) {
struct ifreq ifr;
struct sockaddr_in *sin =
(struct sockaddr_in *)&ifrp->ifr_addr;
/*
* Need a temporary to preserve address info that is
* used below to locate the next entry. (Otherwise,
* SIOCGIFFLAGS stomps over it because the requests
* are returned in a union.)
*/
memcpy(ifr.ifr_name, ifrp->ifr_name, sizeof(ifr.ifr_name));
if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifr) < 0) {
(void)fprintf(stderr, "SIOCGIFFLAGS: ");
perror(ifrp->ifr_name);
finish(-1);
}
if ((ifr.ifr_flags & IFF_UP) && !ISLOOPBACK(&ifr)) {
if (n >= MAXOURADDRS)
break;
ouraddrs[n] = sin->sin_addr.s_addr;
n++;
}
#if BSD >= 199006
n = ifrp->ifr_addr.sa_len + sizeof(ifrp->ifr_name);
if (n < sizeof(*ifrp))
++ifrp;
else
ifrp = (struct ifreq *)((char *)ifrp + n);
#else
++ifrp;
#endif
}
close(fd);
}
|