]> The Tcpdump Group git mirrors - libpcap/blob - inet.c
From Paolo Abeni:
[libpcap] / inet.c
1 /* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */
2 /*
3 * Copyright (c) 1994, 1995, 1996, 1997, 1998
4 * The Regents of the University of California. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the Computer Systems
17 * Engineering Group at Lawrence Berkeley Laboratory.
18 * 4. Neither the name of the University nor of the Laboratory may be used
19 * to endorse or promote products derived from this software without
20 * specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #ifndef lint
36 static const char rcsid[] _U_ =
37 "@(#) $Header: /tcpdump/master/libpcap/inet.c,v 1.71 2006-10-13 09:06:05 guy Exp $ (LBL)";
38 #endif
39
40 #ifdef HAVE_CONFIG_H
41 #include "config.h"
42 #endif
43
44 #ifdef WIN32
45 #include <pcap-stdinc.h>
46 #else /* WIN32 */
47
48 #include <sys/param.h>
49 #ifndef MSDOS
50 #include <sys/file.h>
51 #endif
52 #include <sys/ioctl.h>
53 #include <sys/socket.h>
54 #ifdef HAVE_SYS_SOCKIO_H
55 #include <sys/sockio.h>
56 #endif
57
58 struct mbuf; /* Squelch compiler warnings on some platforms for */
59 struct rtentry; /* declarations in <net/if.h> */
60 #include <net/if.h>
61 #include <netinet/in.h>
62 #endif /* WIN32 */
63
64 #include <ctype.h>
65 #include <errno.h>
66 #include <memory.h>
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <string.h>
70 #if !defined(WIN32) && !defined(__BORLANDC__)
71 #include <unistd.h>
72 #endif /* !WIN32 && !__BORLANDC__ */
73 #ifdef HAVE_LIMITS_H
74 #include <limits.h>
75 #else
76 #define INT_MAX 2147483647
77 #endif
78
79 #include "pcap-int.h"
80
81 #ifdef HAVE_OS_PROTO_H
82 #include "os-proto.h"
83 #endif
84
85 /* Not all systems have IFF_LOOPBACK */
86 #ifdef IFF_LOOPBACK
87 #define ISLOOPBACK(name, flags) ((flags) & IFF_LOOPBACK)
88 #else
89 #define ISLOOPBACK(name, flags) ((name)[0] == 'l' && (name)[1] == 'o' && \
90 (isdigit((unsigned char)((name)[2])) || (name)[2] == '\0'))
91 #endif
92
93 struct sockaddr *
94 dup_sockaddr(struct sockaddr *sa, size_t sa_length)
95 {
96 struct sockaddr *newsa;
97
98 if ((newsa = malloc(sa_length)) == NULL)
99 return (NULL);
100 return (memcpy(newsa, sa, sa_length));
101 }
102
103 static int
104 get_instance(const char *name)
105 {
106 const char *cp, *endcp;
107 int n;
108
109 if (strcmp(name, "any") == 0) {
110 /*
111 * Give the "any" device an artificially high instance
112 * number, so it shows up after all other non-loopback
113 * interfaces.
114 */
115 return INT_MAX;
116 }
117
118 endcp = name + strlen(name);
119 for (cp = name; cp < endcp && !isdigit((unsigned char)*cp); ++cp)
120 continue;
121
122 if (isdigit((unsigned char)*cp))
123 n = atoi(cp);
124 else
125 n = 0;
126 return (n);
127 }
128
129 int
130 add_or_find_if(pcap_if_t **curdev_ret, pcap_if_t **alldevs, const char *name,
131 u_int flags, const char *description, char *errbuf)
132 {
133 pcap_t *p;
134 pcap_if_t *curdev, *prevdev, *nextdev;
135 int this_instance;
136
137 /*
138 * Is there already an entry in the list for this interface?
139 */
140 for (curdev = *alldevs; curdev != NULL; curdev = curdev->next) {
141 if (strcmp(name, curdev->name) == 0)
142 break; /* yes, we found it */
143 }
144
145 if (curdev == NULL) {
146 /*
147 * No, we didn't find it.
148 *
149 * Can we open this interface for live capture?
150 *
151 * We do this check so that interfaces that are
152 * supplied by the interface enumeration mechanism
153 * we're using but that don't support packet capture
154 * aren't included in the list. Loopback interfaces
155 * on Solaris are an example of this; we don't just
156 * omit loopback interfaces on all platforms because
157 * you *can* capture on loopback interfaces on some
158 * OSes.
159 */
160 p = pcap_open_live(name, 68, 0, 0, errbuf);
161 if (p == NULL) {
162 /*
163 * No. Don't bother including it.
164 * Don't treat this as an error, though.
165 */
166 *curdev_ret = NULL;
167 return (0);
168 }
169 pcap_close(p);
170
171 /*
172 * Yes, we can open it.
173 * Allocate a new entry.
174 */
175 curdev = malloc(sizeof(pcap_if_t));
176 if (curdev == NULL) {
177 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
178 "malloc: %s", pcap_strerror(errno));
179 return (-1);
180 }
181
182 /*
183 * Fill in the entry.
184 */
185 curdev->next = NULL;
186 curdev->name = strdup(name);
187 if (curdev->name == NULL) {
188 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
189 "malloc: %s", pcap_strerror(errno));
190 free(curdev);
191 return (-1);
192 }
193 if (description != NULL) {
194 /*
195 * We have a description for this interface.
196 */
197 curdev->description = strdup(description);
198 if (curdev->description == NULL) {
199 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
200 "malloc: %s", pcap_strerror(errno));
201 free(curdev->name);
202 free(curdev);
203 return (-1);
204 }
205 } else {
206 /*
207 * We don't.
208 */
209 curdev->description = NULL;
210 }
211 curdev->addresses = NULL; /* list starts out as empty */
212 curdev->flags = 0;
213 if (ISLOOPBACK(name, flags))
214 curdev->flags |= PCAP_IF_LOOPBACK;
215
216 /*
217 * Add it to the list, in the appropriate location.
218 * First, get the instance number of this interface.
219 */
220 this_instance = get_instance(name);
221
222 /*
223 * Now look for the last interface with an instance number
224 * less than or equal to the new interface's instance
225 * number - except that non-loopback interfaces are
226 * arbitrarily treated as having interface numbers less
227 * than those of loopback interfaces, so the loopback
228 * interfaces are put at the end of the list.
229 *
230 * We start with "prevdev" being NULL, meaning we're before
231 * the first element in the list.
232 */
233 prevdev = NULL;
234 for (;;) {
235 /*
236 * Get the interface after this one.
237 */
238 if (prevdev == NULL) {
239 /*
240 * The next element is the first element.
241 */
242 nextdev = *alldevs;
243 } else
244 nextdev = prevdev->next;
245
246 /*
247 * Are we at the end of the list?
248 */
249 if (nextdev == NULL) {
250 /*
251 * Yes - we have to put the new entry
252 * after "prevdev".
253 */
254 break;
255 }
256
257 /*
258 * Is the new interface a non-loopback interface
259 * and the next interface a loopback interface?
260 */
261 if (!(curdev->flags & PCAP_IF_LOOPBACK) &&
262 (nextdev->flags & PCAP_IF_LOOPBACK)) {
263 /*
264 * Yes, we should put the new entry
265 * before "nextdev", i.e. after "prevdev".
266 */
267 break;
268 }
269
270 /*
271 * Is the new interface's instance number less
272 * than the next interface's instance number,
273 * and is it the case that the new interface is a
274 * non-loopback interface or the next interface is
275 * a loopback interface?
276 *
277 * (The goal of both loopback tests is to make
278 * sure that we never put a loopback interface
279 * before any non-loopback interface and that we
280 * always put a non-loopback interface before all
281 * loopback interfaces.)
282 */
283 if (this_instance < get_instance(nextdev->name) &&
284 (!(curdev->flags & PCAP_IF_LOOPBACK) ||
285 (nextdev->flags & PCAP_IF_LOOPBACK))) {
286 /*
287 * Yes - we should put the new entry
288 * before "nextdev", i.e. after "prevdev".
289 */
290 break;
291 }
292
293 prevdev = nextdev;
294 }
295
296 /*
297 * Insert before "nextdev".
298 */
299 curdev->next = nextdev;
300
301 /*
302 * Insert after "prevdev" - unless "prevdev" is null,
303 * in which case this is the first interface.
304 */
305 if (prevdev == NULL) {
306 /*
307 * This is the first interface. Pass back a
308 * pointer to it, and put "curdev" before
309 * "nextdev".
310 */
311 *alldevs = curdev;
312 } else
313 prevdev->next = curdev;
314 }
315
316 *curdev_ret = curdev;
317 return (0);
318 }
319
320 /*
321 * XXX - on FreeBSDs that support it, should it get the sysctl named
322 * "dev.{adapter family name}.{adapter unit}.%desc" to get a description
323 * of the adapter? Note that "dev.an.0.%desc" is "Aironet PC4500/PC4800"
324 * with my Cisco 350 card, so the name isn't entirely descriptive. The
325 * "dev.an.0.%pnpinfo" has a better description, although one might argue
326 * that the problem is really a driver bug - if it can find out that it's
327 * a Cisco 340 or 350, rather than an old Aironet card, it should use
328 * that in the description.
329 *
330 * Do NetBSD, DragonflyBSD, or OpenBSD support this as well?
331 * Do any other UN*Xes support getting a description?
332 */
333 int
334 add_addr_to_iflist(pcap_if_t **alldevs, const char *name, u_int flags,
335 struct sockaddr *addr, size_t addr_size,
336 struct sockaddr *netmask, size_t netmask_size,
337 struct sockaddr *broadaddr, size_t broadaddr_size,
338 struct sockaddr *dstaddr, size_t dstaddr_size,
339 char *errbuf)
340 {
341 pcap_if_t *curdev;
342 pcap_addr_t *curaddr, *prevaddr, *nextaddr;
343
344 if (add_or_find_if(&curdev, alldevs, name, flags, NULL, errbuf) == -1) {
345 /*
346 * Error - give up.
347 */
348 return (-1);
349 }
350 if (curdev == NULL) {
351 /*
352 * Device wasn't added because it can't be opened.
353 * Not a fatal error.
354 */
355 return (0);
356 }
357
358 /*
359 * "curdev" is an entry for this interface; add an entry for this
360 * address to its list of addresses.
361 *
362 * Allocate the new entry and fill it in.
363 */
364 curaddr = malloc(sizeof(pcap_addr_t));
365 if (curaddr == NULL) {
366 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
367 "malloc: %s", pcap_strerror(errno));
368 return (-1);
369 }
370
371 curaddr->next = NULL;
372 if (addr != NULL) {
373 curaddr->addr = dup_sockaddr(addr, addr_size);
374 if (curaddr->addr == NULL) {
375 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
376 "malloc: %s", pcap_strerror(errno));
377 free(curaddr);
378 return (-1);
379 }
380 } else
381 curaddr->addr = NULL;
382
383 if (netmask != NULL) {
384 curaddr->netmask = dup_sockaddr(netmask, netmask_size);
385 if (curaddr->netmask == NULL) {
386 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
387 "malloc: %s", pcap_strerror(errno));
388 if (curaddr->addr != NULL)
389 free(curaddr->addr);
390 free(curaddr);
391 return (-1);
392 }
393 } else
394 curaddr->netmask = NULL;
395
396 if (broadaddr != NULL) {
397 curaddr->broadaddr = dup_sockaddr(broadaddr, broadaddr_size);
398 if (curaddr->broadaddr == NULL) {
399 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
400 "malloc: %s", pcap_strerror(errno));
401 if (curaddr->netmask != NULL)
402 free(curaddr->netmask);
403 if (curaddr->addr != NULL)
404 free(curaddr->addr);
405 free(curaddr);
406 return (-1);
407 }
408 } else
409 curaddr->broadaddr = NULL;
410
411 if (dstaddr != NULL) {
412 curaddr->dstaddr = dup_sockaddr(dstaddr, dstaddr_size);
413 if (curaddr->dstaddr == NULL) {
414 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
415 "malloc: %s", pcap_strerror(errno));
416 if (curaddr->broadaddr != NULL)
417 free(curaddr->broadaddr);
418 if (curaddr->netmask != NULL)
419 free(curaddr->netmask);
420 if (curaddr->addr != NULL)
421 free(curaddr->addr);
422 free(curaddr);
423 return (-1);
424 }
425 } else
426 curaddr->dstaddr = NULL;
427
428 /*
429 * Find the end of the list of addresses.
430 */
431 for (prevaddr = curdev->addresses; prevaddr != NULL; prevaddr = nextaddr) {
432 nextaddr = prevaddr->next;
433 if (nextaddr == NULL) {
434 /*
435 * This is the end of the list.
436 */
437 break;
438 }
439 }
440
441 if (prevaddr == NULL) {
442 /*
443 * The list was empty; this is the first member.
444 */
445 curdev->addresses = curaddr;
446 } else {
447 /*
448 * "prevaddr" is the last member of the list; append
449 * this member to it.
450 */
451 prevaddr->next = curaddr;
452 }
453
454 return (0);
455 }
456
457 int
458 pcap_add_if(pcap_if_t **devlist, const char *name, u_int flags,
459 const char *description, char *errbuf)
460 {
461 pcap_if_t *curdev;
462
463 return (add_or_find_if(&curdev, devlist, name, flags, description,
464 errbuf));
465 }
466
467
468 /*
469 * Free a list of interfaces.
470 */
471 void
472 pcap_freealldevs(pcap_if_t *alldevs)
473 {
474 pcap_if_t *curdev, *nextdev;
475 pcap_addr_t *curaddr, *nextaddr;
476
477 for (curdev = alldevs; curdev != NULL; curdev = nextdev) {
478 nextdev = curdev->next;
479
480 /*
481 * Free all addresses.
482 */
483 for (curaddr = curdev->addresses; curaddr != NULL; curaddr = nextaddr) {
484 nextaddr = curaddr->next;
485 if (curaddr->addr)
486 free(curaddr->addr);
487 if (curaddr->netmask)
488 free(curaddr->netmask);
489 if (curaddr->broadaddr)
490 free(curaddr->broadaddr);
491 if (curaddr->dstaddr)
492 free(curaddr->dstaddr);
493 free(curaddr);
494 }
495
496 /*
497 * Free the name string.
498 */
499 free(curdev->name);
500
501 /*
502 * Free the description string, if any.
503 */
504 if (curdev->description != NULL)
505 free(curdev->description);
506
507 /*
508 * Free the interface.
509 */
510 free(curdev);
511 }
512 }
513
514 #if !defined(WIN32) && !defined(MSDOS)
515
516 /*
517 * Return the name of a network interface attached to the system, or NULL
518 * if none can be found. The interface must be configured up; the
519 * lowest unit number is preferred; loopback is ignored.
520 */
521 char *
522 pcap_lookupdev(errbuf)
523 register char *errbuf;
524 {
525 pcap_if_t *alldevs;
526 /* for old BSD systems, including bsdi3 */
527 #ifndef IF_NAMESIZE
528 #define IF_NAMESIZE IFNAMSIZ
529 #endif
530 static char device[IF_NAMESIZE + 1];
531 char *ret;
532
533 if (pcap_findalldevs(&alldevs, errbuf) == -1)
534 return (NULL);
535
536 if (alldevs == NULL || (alldevs->flags & PCAP_IF_LOOPBACK)) {
537 /*
538 * There are no devices on the list, or the first device
539 * on the list is a loopback device, which means there
540 * are no non-loopback devices on the list. This means
541 * we can't return any device.
542 *
543 * XXX - why not return a loopback device? If we can't
544 * capture on it, it won't be on the list, and if it's
545 * on the list, there aren't any non-loopback devices,
546 * so why not just supply it as the default device?
547 */
548 (void)strlcpy(errbuf, "no suitable device found",
549 PCAP_ERRBUF_SIZE);
550 ret = NULL;
551 } else {
552 /*
553 * Return the name of the first device on the list.
554 */
555 (void)strlcpy(device, alldevs->name, sizeof(device));
556 ret = device;
557 }
558
559 pcap_freealldevs(alldevs);
560 return (ret);
561 }
562
563 int
564 pcap_lookupnet(device, netp, maskp, errbuf)
565 register const char *device;
566 register bpf_u_int32 *netp, *maskp;
567 register char *errbuf;
568 {
569 register int fd;
570 register struct sockaddr_in *sin;
571 struct ifreq ifr;
572
573 /*
574 * The pseudo-device "any" listens on all interfaces and therefore
575 * has the network address and -mask "0.0.0.0" therefore catching
576 * all traffic. Using NULL for the interface is the same as "any".
577 */
578 if (!device || strcmp(device, "any") == 0
579 #ifdef HAVE_DAG_API
580 || strstr(device, "dag") != NULL
581 #endif
582 #ifdef HAVE_SEPTEL_API
583 || strstr(device, "septel") != NULL
584 #endif
585 #ifdef PCAP_SUPPORT_USB
586 || strstr(device, "usb") != NULL
587 #endif
588 ) {
589 *netp = *maskp = 0;
590 return 0;
591 }
592
593 fd = socket(AF_INET, SOCK_DGRAM, 0);
594 if (fd < 0) {
595 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, "socket: %s",
596 pcap_strerror(errno));
597 return (-1);
598 }
599 memset(&ifr, 0, sizeof(ifr));
600 #ifdef linux
601 /* XXX Work around Linux kernel bug */
602 ifr.ifr_addr.sa_family = AF_INET;
603 #endif
604 (void)strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
605 if (ioctl(fd, SIOCGIFADDR, (char *)&ifr) < 0) {
606 if (errno == EADDRNOTAVAIL) {
607 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
608 "%s: no IPv4 address assigned", device);
609 } else {
610 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
611 "SIOCGIFADDR: %s: %s",
612 device, pcap_strerror(errno));
613 }
614 (void)close(fd);
615 return (-1);
616 }
617 sin = (struct sockaddr_in *)&ifr.ifr_addr;
618 *netp = sin->sin_addr.s_addr;
619 if (ioctl(fd, SIOCGIFNETMASK, (char *)&ifr) < 0) {
620 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
621 "SIOCGIFNETMASK: %s: %s", device, pcap_strerror(errno));
622 (void)close(fd);
623 return (-1);
624 }
625 (void)close(fd);
626 *maskp = sin->sin_addr.s_addr;
627 if (*maskp == 0) {
628 if (IN_CLASSA(*netp))
629 *maskp = IN_CLASSA_NET;
630 else if (IN_CLASSB(*netp))
631 *maskp = IN_CLASSB_NET;
632 else if (IN_CLASSC(*netp))
633 *maskp = IN_CLASSC_NET;
634 else {
635 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
636 "inet class for 0x%x unknown", *netp);
637 return (-1);
638 }
639 }
640 *netp &= *maskp;
641 return (0);
642 }
643
644 #elif defined(WIN32)
645
646 /*
647 * Return the name of a network interface attached to the system, or NULL
648 * if none can be found. The interface must be configured up; the
649 * lowest unit number is preferred; loopback is ignored.
650 */
651 char *
652 pcap_lookupdev(errbuf)
653 register char *errbuf;
654 {
655 DWORD dwVersion;
656 DWORD dwWindowsMajorVersion;
657 dwVersion = GetVersion(); /* get the OS version */
658 dwWindowsMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
659
660 if (dwVersion >= 0x80000000 && dwWindowsMajorVersion >= 4) {
661 /*
662 * Windows 95, 98, ME.
663 */
664 ULONG NameLength = 8192;
665 static char AdaptersName[8192];
666
667 if (PacketGetAdapterNames(AdaptersName,&NameLength) )
668 return (AdaptersName);
669 else
670 return NULL;
671 } else {
672 /*
673 * Windows NT (NT 4.0, W2K, WXP). Convert the names to UNICODE for backward compatibility
674 */
675 ULONG NameLength = 8192;
676 static WCHAR AdaptersName[8192];
677 char *tAstr;
678 WCHAR *tUstr;
679 WCHAR *TAdaptersName = (WCHAR*)malloc(8192 * sizeof(WCHAR));
680 int NAdapts = 0;
681
682 if(TAdaptersName == NULL)
683 {
684 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, "memory allocation failure");
685 return NULL;
686 }
687
688 if ( !PacketGetAdapterNames((PTSTR)TAdaptersName,&NameLength) )
689 {
690 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
691 "PacketGetAdapterNames: %s",
692 pcap_win32strerror());
693 free(TAdaptersName);
694 return NULL;
695 }
696
697
698 tAstr = (char*)TAdaptersName;
699 tUstr = (WCHAR*)AdaptersName;
700
701 /*
702 * Convert and copy the device names
703 */
704 while(sscanf(tAstr, "%S", tUstr) > 0)
705 {
706 tAstr += strlen(tAstr) + 1;
707 tUstr += wcslen(tUstr) + 1;
708 NAdapts ++;
709 }
710
711 tAstr++;
712 *tUstr = 0;
713 tUstr++;
714
715 /*
716 * Copy the descriptions
717 */
718 while(NAdapts--)
719 {
720 strcpy((char*)tUstr, tAstr);
721 (char*)tUstr += strlen(tAstr) + 1;;
722 tAstr += strlen(tAstr) + 1;
723 }
724
725 free(TAdaptersName);
726 return (char *)(AdaptersName);
727 }
728 }
729
730
731 int
732 pcap_lookupnet(device, netp, maskp, errbuf)
733 register const char *device;
734 register bpf_u_int32 *netp, *maskp;
735 register char *errbuf;
736 {
737 /*
738 * We need only the first IPv4 address, so we must scan the array returned by PacketGetNetInfo()
739 * in order to skip non IPv4 (i.e. IPv6 addresses)
740 */
741 npf_if_addr if_addrs[MAX_NETWORK_ADDRESSES];
742 LONG if_addr_size = 1;
743 struct sockaddr_in *t_addr;
744 unsigned int i;
745
746 if (!PacketGetNetInfoEx((void *)device, if_addrs, &if_addr_size)) {
747 *netp = *maskp = 0;
748 return (0);
749 }
750
751 for(i=0; i<MAX_NETWORK_ADDRESSES; i++)
752 {
753 if(if_addrs[i].IPAddress.ss_family == AF_INET)
754 {
755 t_addr = (struct sockaddr_in *) &(if_addrs[i].IPAddress);
756 *netp = t_addr->sin_addr.S_un.S_addr;
757 t_addr = (struct sockaddr_in *) &(if_addrs[i].SubnetMask);
758 *maskp = t_addr->sin_addr.S_un.S_addr;
759
760 *netp &= *maskp;
761 return (0);
762 }
763
764 }
765
766 *netp = *maskp = 0;
767 return (0);
768 }
769
770 #endif /* !WIN32 && !MSDOS */