route.c (17571) | route.c (18752) |
---|---|
1/* 2 * PPP Routing related Module 3 * 4 * Written by Toshiharu OHNO (tony-o@iij.ad.jp) 5 * 6 * Copyright (C) 1994, Internet Initiative Japan, Inc. All rights reserverd. 7 * 8 * Redistribution and use in source and binary forms are permitted 9 * provided that the above copyright notice and this paragraph are 10 * duplicated in all such forms and that any documentation, 11 * advertising materials, and other materials related to such 12 * distribution and use acknowledge that the software was developed 13 * by the Internet Initiative Japan, Inc. The name of the 14 * IIJ may not be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 19 * | 1/* 2 * PPP Routing related Module 3 * 4 * Written by Toshiharu OHNO (tony-o@iij.ad.jp) 5 * 6 * Copyright (C) 1994, Internet Initiative Japan, Inc. All rights reserverd. 7 * 8 * Redistribution and use in source and binary forms are permitted 9 * provided that the above copyright notice and this paragraph are 10 * duplicated in all such forms and that any documentation, 11 * advertising materials, and other materials related to such 12 * distribution and use acknowledge that the software was developed 13 * by the Internet Initiative Japan, Inc. The name of the 14 * IIJ may not be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 19 * |
20 * $Id: route.c,v 1.6 1996/05/11 20:48:42 phk Exp $ | 20 * $Id: route.c,v 1.7 1996/08/13 09:19:45 peter Exp $ |
21 * 22 */ 23#include <sys/types.h> 24#include <machine/endian.h> 25#include <sys/param.h> 26#include <sys/socket.h> 27#include <net/route.h> 28#include <sys/ioctl.h> --- 317 unchanged lines hidden (view full) --- 346 else if (rtm->rtm_index == IfIndex) { 347 logprintf("??? addrs: %x, flags = %x\n", rtm->rtm_addrs, rtm->rtm_flags); 348 } 349#endif 350 } 351 free(sp); 352} 353 | 21 * 22 */ 23#include <sys/types.h> 24#include <machine/endian.h> 25#include <sys/param.h> 26#include <sys/socket.h> 27#include <net/route.h> 28#include <sys/ioctl.h> --- 317 unchanged lines hidden (view full) --- 346 else if (rtm->rtm_index == IfIndex) { 347 logprintf("??? addrs: %x, flags = %x\n", rtm->rtm_addrs, rtm->rtm_flags); 348 } 349#endif 350 } 351 free(sp); 352} 353 |
354 /* 355 * 960603 - Modified to use dynamic buffer allocator as in ifconfig 356 */ 357 |
|
354int 355GetIfIndex(name) 356char *name; 357{ | 358int 359GetIfIndex(name) 360char *name; 361{ |
362 char *buffer; |
|
358 struct ifreq *ifrp; 359 int s, len, elen, index; 360 struct ifconf ifconfs; | 363 struct ifreq *ifrp; 364 int s, len, elen, index; 365 struct ifconf ifconfs; |
361 struct ifreq reqbuf[32]; | 366 /* struct ifreq reqbuf[256]; -- obsoleted :) */ 367 int oldbufsize, bufsize = sizeof(struct ifreq); |
362 363 s = socket(AF_INET, SOCK_DGRAM, 0); 364 if (s < 0) { 365 perror("socket"); 366 return(-1); 367 } 368 | 368 369 s = socket(AF_INET, SOCK_DGRAM, 0); 370 if (s < 0) { 371 perror("socket"); 372 return(-1); 373 } 374 |
369 ifconfs.ifc_len = sizeof(reqbuf); 370 ifconfs.ifc_buf = (caddr_t)reqbuf; 371 if (ioctl(s, SIOCGIFCONF, &ifconfs) < 0) { 372 perror("IFCONF"); 373 return(-1); 374 } | 375 buffer = malloc(bufsize); /* allocate first buffer */ 376 ifconfs.ifc_len = bufsize; /* Initial setting */ 377 /* 378 * Iterate through here until we don't get many more data 379 */ |
375 | 380 |
381 do { 382 oldbufsize = ifconfs.ifc_len; 383 bufsize += 1+sizeof(struct ifreq); 384 buffer = realloc((void *)buffer, bufsize); /* Make it bigger */ 385#ifdef DEBUG 386 logprintf ("Growing buffer to %d\n", bufsize); 387#endif 388 ifconfs.ifc_len = bufsize; 389 ifconfs.ifc_buf = buffer; 390 if (ioctl(s, SIOCGIFCONF, &ifconfs) < 0) { 391 perror("IFCONF"); 392 free(buffer); 393 return(-1); 394 } 395 } while (ifconfs.ifc_len > oldbufsize); 396 |
|
376 ifrp = ifconfs.ifc_req; 377 378 index = 1; 379 for (len = ifconfs.ifc_len; len > 0; len -= sizeof(struct ifreq)) { 380 elen = ifrp->ifr_addr.sa_len - sizeof(struct sockaddr); 381 if (ifrp->ifr_addr.sa_family == AF_LINK) { 382#ifdef DEBUG 383 logprintf("%d: %-*.*s, %d, %d\n", index, IFNAMSIZ, IFNAMSIZ, ifrp->ifr_name, 384 ifrp->ifr_addr.sa_family, elen); 385#endif 386 if (strcmp(ifrp->ifr_name, name) == 0) { 387 IfIndex = index; | 397 ifrp = ifconfs.ifc_req; 398 399 index = 1; 400 for (len = ifconfs.ifc_len; len > 0; len -= sizeof(struct ifreq)) { 401 elen = ifrp->ifr_addr.sa_len - sizeof(struct sockaddr); 402 if (ifrp->ifr_addr.sa_family == AF_LINK) { 403#ifdef DEBUG 404 logprintf("%d: %-*.*s, %d, %d\n", index, IFNAMSIZ, IFNAMSIZ, ifrp->ifr_name, 405 ifrp->ifr_addr.sa_family, elen); 406#endif 407 if (strcmp(ifrp->ifr_name, name) == 0) { 408 IfIndex = index; |
409 free(buffer); |
|
388 return(index); 389 } 390 index++; 391 } 392 393 len -= elen; 394 ifrp = (struct ifreq *)((char *)ifrp + elen); 395 ifrp++; 396 } 397 398 close(s); | 410 return(index); 411 } 412 index++; 413 } 414 415 len -= elen; 416 ifrp = (struct ifreq *)((char *)ifrp + elen); 417 ifrp++; 418 } 419 420 close(s); |
421 free(buffer); |
|
399 return(-1); 400} | 422 return(-1); 423} |