1995-01-31 06:29:58 +00:00
|
|
|
/*
|
|
|
|
* PPP Routing related Module
|
|
|
|
*
|
|
|
|
* Written by Toshiharu OHNO (tony-o@iij.ad.jp)
|
|
|
|
*
|
|
|
|
* Copyright (C) 1994, Internet Initiative Japan, Inc. All rights reserverd.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms are permitted
|
|
|
|
* provided that the above copyright notice and this paragraph are
|
|
|
|
* duplicated in all such forms and that any documentation,
|
|
|
|
* advertising materials, and other materials related to such
|
|
|
|
* distribution and use acknowledge that the software was developed
|
|
|
|
* by the Internet Initiative Japan, Inc. The name of the
|
|
|
|
* IIJ may not be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
*
|
1997-11-09 03:22:49 +00:00
|
|
|
* $Id: route.c,v 1.21 1997/11/08 00:28:11 brian Exp $
|
1995-05-30 03:57:47 +00:00
|
|
|
*
|
1995-01-31 06:29:58 +00:00
|
|
|
*/
|
1997-10-26 01:04:02 +00:00
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
#include <sys/param.h>
|
1996-12-10 17:11:53 +00:00
|
|
|
#include <sys/time.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
#include <sys/socket.h>
|
1996-12-10 17:11:53 +00:00
|
|
|
#include <net/route.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
1997-10-26 01:04:02 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <machine/endian.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "mbuf.h"
|
1995-07-08 06:08:52 +00:00
|
|
|
#include "log.h"
|
1997-06-09 03:27:43 +00:00
|
|
|
#include "loadalias.h"
|
1997-10-26 01:04:02 +00:00
|
|
|
#include "command.h"
|
1997-06-09 03:27:43 +00:00
|
|
|
#include "vars.h"
|
1997-10-26 01:04:02 +00:00
|
|
|
#include "route.h"
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
static int IfIndex;
|
|
|
|
|
|
|
|
struct rtmsg {
|
|
|
|
struct rt_msghdr m_rtm;
|
|
|
|
char m_space[64];
|
|
|
|
};
|
|
|
|
|
|
|
|
static int seqno;
|
|
|
|
|
|
|
|
void
|
1997-08-25 00:29:32 +00:00
|
|
|
OsSetRoute(int cmd,
|
|
|
|
struct in_addr dst,
|
|
|
|
struct in_addr gateway,
|
|
|
|
struct in_addr mask)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
struct rtmsg rtmes;
|
|
|
|
int s, nb, wb;
|
|
|
|
char *cp;
|
|
|
|
u_long *lp;
|
|
|
|
struct sockaddr_in rtdata;
|
|
|
|
|
|
|
|
s = socket(PF_ROUTE, SOCK_RAW, 0);
|
1997-07-28 01:02:27 +00:00
|
|
|
if (s < 0) {
|
1997-08-31 22:59:49 +00:00
|
|
|
LogPrintf(LogERROR, "OsSetRoute: socket(): %s\n", strerror(errno));
|
1997-07-28 01:02:27 +00:00
|
|
|
return;
|
|
|
|
}
|
1997-10-26 01:04:02 +00:00
|
|
|
memset(&rtmes, '\0', sizeof(rtmes));
|
1995-01-31 06:29:58 +00:00
|
|
|
rtmes.m_rtm.rtm_version = RTM_VERSION;
|
|
|
|
rtmes.m_rtm.rtm_type = cmd;
|
1997-11-09 03:22:49 +00:00
|
|
|
rtmes.m_rtm.rtm_addrs = RTA_DST | RTA_NETMASK;
|
1995-01-31 06:29:58 +00:00
|
|
|
rtmes.m_rtm.rtm_seq = ++seqno;
|
|
|
|
rtmes.m_rtm.rtm_pid = getpid();
|
1996-08-13 09:19:45 +00:00
|
|
|
rtmes.m_rtm.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1997-10-26 01:04:02 +00:00
|
|
|
memset(&rtdata, '\0', sizeof(rtdata));
|
1995-01-31 06:29:58 +00:00
|
|
|
rtdata.sin_len = 16;
|
|
|
|
rtdata.sin_family = AF_INET;
|
|
|
|
rtdata.sin_port = 0;
|
|
|
|
rtdata.sin_addr = dst;
|
|
|
|
|
|
|
|
cp = rtmes.m_space;
|
1997-10-26 01:04:02 +00:00
|
|
|
memcpy(cp, &rtdata, 16);
|
1995-01-31 06:29:58 +00:00
|
|
|
cp += 16;
|
|
|
|
if (gateway.s_addr) {
|
|
|
|
rtdata.sin_addr = gateway;
|
1997-10-26 01:04:02 +00:00
|
|
|
memcpy(cp, &rtdata, 16);
|
1995-01-31 06:29:58 +00:00
|
|
|
cp += 16;
|
1997-11-09 03:22:49 +00:00
|
|
|
rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
if (dst.s_addr == INADDR_ANY)
|
|
|
|
mask.s_addr = INADDR_ANY;
|
|
|
|
|
1997-08-25 00:29:32 +00:00
|
|
|
lp = (u_long *) cp;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
if (mask.s_addr) {
|
|
|
|
*lp++ = 8;
|
|
|
|
cp += sizeof(int);
|
|
|
|
*lp = mask.s_addr;
|
|
|
|
} else
|
|
|
|
*lp = 0;
|
|
|
|
cp += sizeof(u_long);
|
|
|
|
|
1997-08-25 00:29:32 +00:00
|
|
|
nb = cp - (char *) &rtmes;
|
1995-01-31 06:29:58 +00:00
|
|
|
rtmes.m_rtm.rtm_msglen = nb;
|
|
|
|
wb = write(s, &rtmes, nb);
|
|
|
|
if (wb < 0) {
|
1997-06-13 03:59:36 +00:00
|
|
|
LogPrintf(LogTCPIP, "OsSetRoute: Dst = %s\n", inet_ntoa(dst));
|
|
|
|
LogPrintf(LogTCPIP, "OsSetRoute: Gateway = %s\n", inet_ntoa(gateway));
|
|
|
|
LogPrintf(LogTCPIP, "OsSetRoute: Mask = %s\n", inet_ntoa(mask));
|
1997-08-25 00:29:32 +00:00
|
|
|
switch (rtmes.m_rtm.rtm_errno) {
|
|
|
|
case EEXIST:
|
|
|
|
LogPrintf(LogTCPIP, "Add route failed: Already exists\n");
|
|
|
|
break;
|
|
|
|
case ESRCH:
|
|
|
|
LogPrintf(LogTCPIP, "Del route failed: Non-existent\n");
|
|
|
|
break;
|
|
|
|
case ENOBUFS:
|
|
|
|
default:
|
|
|
|
LogPrintf(LogTCPIP, "Add/Del route failed: %s\n",
|
|
|
|
strerror(rtmes.m_rtm.rtm_errno));
|
|
|
|
break;
|
1997-06-13 03:59:36 +00:00
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1997-06-09 03:27:43 +00:00
|
|
|
LogPrintf(LogDEBUG, "wrote %d: dst = %x, gateway = %x\n", nb,
|
1997-08-25 00:29:32 +00:00
|
|
|
dst.s_addr, gateway.s_addr);
|
1995-01-31 06:29:58 +00:00
|
|
|
close(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1997-08-25 00:29:32 +00:00
|
|
|
p_sockaddr(struct sockaddr * sa, int width)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1997-06-09 03:27:43 +00:00
|
|
|
if (VarTerm) {
|
|
|
|
register char *cp;
|
1997-11-08 00:28:11 +00:00
|
|
|
register struct sockaddr_in *sock_in = (struct sockaddr_in *) sa;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1997-11-08 00:28:11 +00:00
|
|
|
cp = (sock_in->sin_addr.s_addr == 0) ? "default" :
|
|
|
|
inet_ntoa(sock_in->sin_addr);
|
1997-06-09 03:27:43 +00:00
|
|
|
fprintf(VarTerm, "%-*.*s ", width, width, cp);
|
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct bits {
|
|
|
|
short b_mask;
|
1997-08-25 00:29:32 +00:00
|
|
|
char b_val;
|
|
|
|
} bits[] = {
|
|
|
|
|
|
|
|
{
|
|
|
|
RTF_UP, 'U'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
RTF_GATEWAY, 'G'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
RTF_HOST, 'H'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
RTF_DYNAMIC, 'D'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
RTF_MODIFIED, 'M'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
RTF_CLONING, 'C'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
RTF_XRESOLVE, 'X'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
RTF_LLINFO, 'L'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
RTF_REJECT, 'R'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
0
|
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
1997-08-25 00:29:32 +00:00
|
|
|
p_flags(int f, char *format)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1997-06-09 03:27:43 +00:00
|
|
|
if (VarTerm) {
|
|
|
|
char name[33], *flags;
|
|
|
|
register struct bits *p = bits;
|
|
|
|
|
|
|
|
for (flags = name; p->b_mask; p++)
|
|
|
|
if (p->b_mask & f)
|
1997-08-25 00:29:32 +00:00
|
|
|
*flags++ = p->b_val;
|
1997-06-09 03:27:43 +00:00
|
|
|
*flags = '\0';
|
|
|
|
fprintf(VarTerm, format, name);
|
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ShowRoute()
|
|
|
|
{
|
|
|
|
struct rt_msghdr *rtm;
|
|
|
|
struct sockaddr *sa;
|
|
|
|
char *sp, *ep, *cp;
|
|
|
|
u_char *wp;
|
|
|
|
int *lp;
|
|
|
|
int needed, nb;
|
|
|
|
u_long mask;
|
1995-02-26 12:18:08 +00:00
|
|
|
int mib[6];
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1997-06-09 03:27:43 +00:00
|
|
|
if (!VarTerm)
|
|
|
|
return 1;
|
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
mib[0] = CTL_NET;
|
|
|
|
mib[1] = PF_ROUTE;
|
1995-02-26 12:18:08 +00:00
|
|
|
mib[2] = 0;
|
|
|
|
mib[3] = 0;
|
1995-01-31 06:29:58 +00:00
|
|
|
mib[4] = NET_RT_DUMP;
|
1995-02-26 12:18:08 +00:00
|
|
|
mib[5] = 0;
|
|
|
|
if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
|
1997-08-31 22:59:49 +00:00
|
|
|
LogPrintf(LogERROR, "ShowRoute: sysctl: estimate: %s\n", strerror(errno));
|
1997-08-25 00:29:32 +00:00
|
|
|
return (1);
|
1995-02-26 12:18:08 +00:00
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
if (needed < 0)
|
1997-08-25 00:29:32 +00:00
|
|
|
return (1);
|
1995-01-31 06:29:58 +00:00
|
|
|
sp = malloc(needed);
|
|
|
|
if (sp == NULL)
|
1997-08-25 00:29:32 +00:00
|
|
|
return (1);
|
1995-02-26 12:18:08 +00:00
|
|
|
if (sysctl(mib, 6, sp, &needed, NULL, 0) < 0) {
|
1997-08-31 22:59:49 +00:00
|
|
|
LogPrintf(LogERROR, "ShowRoute: sysctl: getroute: %s\n", strerror(errno));
|
1996-10-12 16:20:34 +00:00
|
|
|
free(sp);
|
1997-08-25 00:29:32 +00:00
|
|
|
return (1);
|
1995-02-26 12:18:08 +00:00
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
ep = sp + needed;
|
|
|
|
|
|
|
|
for (cp = sp; cp < ep; cp += rtm->rtm_msglen) {
|
1997-08-25 00:29:32 +00:00
|
|
|
rtm = (struct rt_msghdr *) cp;
|
|
|
|
sa = (struct sockaddr *) (rtm + 1);
|
1995-01-31 06:29:58 +00:00
|
|
|
mask = 0xffffffff;
|
|
|
|
if (rtm->rtm_addrs == RTA_DST)
|
|
|
|
p_sockaddr(sa, 36);
|
|
|
|
else {
|
1997-08-25 00:29:32 +00:00
|
|
|
wp = (u_char *) cp + rtm->rtm_msglen;
|
1995-01-31 06:29:58 +00:00
|
|
|
p_sockaddr(sa, 16);
|
|
|
|
if (sa->sa_len == 0)
|
|
|
|
sa->sa_len = sizeof(long);
|
1997-08-25 00:29:32 +00:00
|
|
|
sa = (struct sockaddr *) (sa->sa_len + (char *) sa);
|
1995-01-31 06:29:58 +00:00
|
|
|
p_sockaddr(sa, 18);
|
1997-08-25 00:29:32 +00:00
|
|
|
lp = (int *) (sa->sa_len + (char *) sa);
|
|
|
|
if ((char *) lp < (char *) wp && *lp) {
|
1997-08-31 22:59:49 +00:00
|
|
|
LogPrintf(LogDEBUG, " flag = %x, rest = %d\n", rtm->rtm_flags, *lp);
|
1997-08-25 00:29:32 +00:00
|
|
|
wp = (u_char *) (lp + 1);
|
1995-01-31 06:29:58 +00:00
|
|
|
mask = 0;
|
1997-08-25 00:29:32 +00:00
|
|
|
for (nb = *(char *) lp; nb > 4; nb--) {
|
1995-01-31 06:29:58 +00:00
|
|
|
mask <<= 8;
|
|
|
|
mask |= *wp++;
|
|
|
|
}
|
1997-08-25 00:29:32 +00:00
|
|
|
for (nb = 8 - *(char *) lp; nb > 0; nb--)
|
1995-01-31 06:29:58 +00:00
|
|
|
mask <<= 8;
|
|
|
|
}
|
|
|
|
}
|
1997-06-09 03:27:43 +00:00
|
|
|
fprintf(VarTerm, "%08lx ", mask);
|
1997-08-25 00:29:32 +00:00
|
|
|
p_flags(rtm->rtm_flags & (RTF_UP | RTF_GATEWAY | RTF_HOST), "%-6.6s ");
|
1997-06-09 03:27:43 +00:00
|
|
|
fprintf(VarTerm, "(%d)\n", rtm->rtm_index);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1996-10-12 16:20:34 +00:00
|
|
|
free(sp);
|
1997-06-09 03:27:43 +00:00
|
|
|
return 0;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Delete routes associated with our interface
|
|
|
|
*/
|
|
|
|
void
|
1997-08-25 00:29:32 +00:00
|
|
|
DeleteIfRoutes(int all)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
struct rt_msghdr *rtm;
|
|
|
|
struct sockaddr *sa;
|
1996-05-11 20:48:42 +00:00
|
|
|
struct in_addr dstnet, gateway, maddr;
|
1995-01-31 06:29:58 +00:00
|
|
|
int needed;
|
|
|
|
char *sp, *cp, *ep;
|
|
|
|
u_long mask;
|
|
|
|
int *lp, nb;
|
|
|
|
u_char *wp;
|
1995-02-26 12:18:08 +00:00
|
|
|
int mib[6];
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1997-06-09 03:27:43 +00:00
|
|
|
LogPrintf(LogDEBUG, "DeleteIfRoutes (%d)\n", IfIndex);
|
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
mib[0] = CTL_NET;
|
|
|
|
mib[1] = PF_ROUTE;
|
1995-02-26 12:18:08 +00:00
|
|
|
mib[2] = 0;
|
|
|
|
mib[3] = 0;
|
1995-01-31 06:29:58 +00:00
|
|
|
mib[4] = NET_RT_DUMP;
|
1995-02-26 12:18:08 +00:00
|
|
|
mib[5] = 0;
|
|
|
|
if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
|
1997-08-31 22:59:49 +00:00
|
|
|
LogPrintf(LogERROR, "DeleteIfRoutes: sysctl: estimate: %s\n",
|
|
|
|
strerror(errno));
|
1995-02-26 12:18:08 +00:00
|
|
|
return;
|
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
if (needed < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
sp = malloc(needed);
|
|
|
|
if (sp == NULL)
|
|
|
|
return;
|
|
|
|
|
1995-02-26 12:18:08 +00:00
|
|
|
if (sysctl(mib, 6, sp, &needed, NULL, 0) < 0) {
|
1997-08-31 22:59:49 +00:00
|
|
|
LogPrintf(LogERROR, "DeleteIfRoutes: sysctl: getroute: %s\n",
|
|
|
|
strerror(errno));
|
1995-01-31 06:29:58 +00:00
|
|
|
free(sp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ep = sp + needed;
|
|
|
|
|
|
|
|
for (cp = sp; cp < ep; cp += rtm->rtm_msglen) {
|
1997-08-25 00:29:32 +00:00
|
|
|
rtm = (struct rt_msghdr *) cp;
|
|
|
|
sa = (struct sockaddr *) (rtm + 1);
|
1997-06-09 03:27:43 +00:00
|
|
|
LogPrintf(LogDEBUG, "DeleteIfRoutes: addrs: %x, index: %d, flags: %x,"
|
1997-08-25 00:29:32 +00:00
|
|
|
" dstnet: %s\n",
|
|
|
|
rtm->rtm_addrs, rtm->rtm_index, rtm->rtm_flags,
|
|
|
|
inet_ntoa(((struct sockaddr_in *) sa)->sin_addr));
|
1995-01-31 06:29:58 +00:00
|
|
|
if (rtm->rtm_addrs != RTA_DST &&
|
1997-08-25 00:29:32 +00:00
|
|
|
(rtm->rtm_index == IfIndex) &&
|
|
|
|
(all || (rtm->rtm_flags & RTF_GATEWAY))) {
|
1997-06-20 23:43:35 +00:00
|
|
|
LogPrintf(LogDEBUG, "DeleteIfRoutes: Remove it\n");
|
1997-08-25 00:29:32 +00:00
|
|
|
dstnet = ((struct sockaddr_in *) sa)->sin_addr;
|
|
|
|
wp = (u_char *) cp + rtm->rtm_msglen;
|
1995-01-31 06:29:58 +00:00
|
|
|
if (sa->sa_len == 0)
|
|
|
|
sa->sa_len = sizeof(long);
|
1997-08-25 00:29:32 +00:00
|
|
|
sa = (struct sockaddr *) (sa->sa_len + (char *) sa);
|
|
|
|
gateway = ((struct sockaddr_in *) sa)->sin_addr;
|
|
|
|
lp = (int *) (sa->sa_len + (char *) sa);
|
1995-01-31 06:29:58 +00:00
|
|
|
mask = 0;
|
1997-08-25 00:29:32 +00:00
|
|
|
if ((char *) lp < (char *) wp && *lp) {
|
1997-08-31 22:59:49 +00:00
|
|
|
LogPrintf(LogDEBUG, "DeleteIfRoutes: flag = %x, rest = %d\n",
|
1997-06-09 03:27:43 +00:00
|
|
|
rtm->rtm_flags, *lp);
|
1997-08-25 00:29:32 +00:00
|
|
|
wp = (u_char *) (lp + 1);
|
1995-01-31 06:29:58 +00:00
|
|
|
for (nb = *lp; nb > 4; nb--) {
|
|
|
|
mask <<= 8;
|
|
|
|
mask |= *wp++;
|
|
|
|
}
|
|
|
|
for (nb = 8 - *lp; nb > 0; nb--)
|
|
|
|
mask <<= 8;
|
|
|
|
}
|
1997-06-20 23:43:35 +00:00
|
|
|
LogPrintf(LogDEBUG, "DeleteIfRoutes: Dst: %s\n", inet_ntoa(dstnet));
|
1997-06-09 03:27:43 +00:00
|
|
|
LogPrintf(LogDEBUG, "DeleteIfRoutes: Gw: %s\n", inet_ntoa(gateway));
|
|
|
|
LogPrintf(LogDEBUG, "DeleteIfRoutes: Index: %d\n", rtm->rtm_index);
|
1997-06-20 23:43:35 +00:00
|
|
|
if (dstnet.s_addr == INADDR_ANY)
|
1997-08-25 00:29:32 +00:00
|
|
|
mask = INADDR_ANY;
|
1996-05-11 20:48:42 +00:00
|
|
|
maddr.s_addr = htonl(mask);
|
|
|
|
OsSetRoute(RTM_DELETE, dstnet, gateway, maddr);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
free(sp);
|
|
|
|
}
|
|
|
|
|
1996-10-06 13:32:37 +00:00
|
|
|
/*
|
|
|
|
* 960603 - Modified to use dynamic buffer allocator as in ifconfig
|
|
|
|
*/
|
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
int
|
1997-08-25 00:29:32 +00:00
|
|
|
GetIfIndex(char *name)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1996-10-06 13:32:37 +00:00
|
|
|
char *buffer;
|
1995-01-31 06:29:58 +00:00
|
|
|
struct ifreq *ifrp;
|
1997-11-08 00:28:11 +00:00
|
|
|
int s, len, elen, newIfIndex;
|
1995-01-31 06:29:58 +00:00
|
|
|
struct ifconf ifconfs;
|
1997-08-25 00:29:32 +00:00
|
|
|
|
1996-10-06 13:32:37 +00:00
|
|
|
/* struct ifreq reqbuf[256]; -- obsoleted :) */
|
|
|
|
int oldbufsize, bufsize = sizeof(struct ifreq);
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
s = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (s < 0) {
|
1997-08-31 22:59:49 +00:00
|
|
|
LogPrintf(LogERROR, "GetIfIndex: socket(): %s\n", strerror(errno));
|
1997-08-25 00:29:32 +00:00
|
|
|
return (-1);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1997-08-25 00:29:32 +00:00
|
|
|
buffer = malloc(bufsize); /* allocate first buffer */
|
|
|
|
ifconfs.ifc_len = bufsize; /* Initial setting */
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1996-10-06 13:32:37 +00:00
|
|
|
/*
|
1997-08-25 00:29:32 +00:00
|
|
|
* Iterate through here until we don't get many more data
|
1996-10-06 13:32:37 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
do {
|
1997-08-25 00:29:32 +00:00
|
|
|
oldbufsize = ifconfs.ifc_len;
|
|
|
|
bufsize += 1 + sizeof(struct ifreq);
|
|
|
|
buffer = realloc((void *) buffer, bufsize); /* Make it bigger */
|
|
|
|
LogPrintf(LogDEBUG, "GetIfIndex: Growing buffer to %d\n", bufsize);
|
|
|
|
ifconfs.ifc_len = bufsize;
|
|
|
|
ifconfs.ifc_buf = buffer;
|
|
|
|
if (ioctl(s, SIOCGIFCONF, &ifconfs) < 0) {
|
1997-08-31 22:59:49 +00:00
|
|
|
LogPrintf(LogERROR, "GetIfIndex: ioctl(SIOCGIFCONF): %s\n",
|
|
|
|
strerror(errno));
|
1997-08-25 00:29:32 +00:00
|
|
|
close(s);
|
|
|
|
free(buffer);
|
|
|
|
return (-1);
|
|
|
|
}
|
1996-10-06 13:32:37 +00:00
|
|
|
} while (ifconfs.ifc_len > oldbufsize);
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
ifrp = ifconfs.ifc_req;
|
|
|
|
|
1997-11-08 00:28:11 +00:00
|
|
|
newIfIndex = 1;
|
1995-01-31 06:29:58 +00:00
|
|
|
for (len = ifconfs.ifc_len; len > 0; len -= sizeof(struct ifreq)) {
|
|
|
|
elen = ifrp->ifr_addr.sa_len - sizeof(struct sockaddr);
|
|
|
|
if (ifrp->ifr_addr.sa_family == AF_LINK) {
|
1997-06-09 03:27:43 +00:00
|
|
|
LogPrintf(LogDEBUG, "GetIfIndex: %d: %-*.*s, %d, %d\n",
|
1997-11-08 00:28:11 +00:00
|
|
|
newIfIndex, IFNAMSIZ, IFNAMSIZ, ifrp->ifr_name,
|
1997-06-09 03:27:43 +00:00
|
|
|
ifrp->ifr_addr.sa_family, elen);
|
1995-01-31 06:29:58 +00:00
|
|
|
if (strcmp(ifrp->ifr_name, name) == 0) {
|
1997-11-08 00:28:11 +00:00
|
|
|
IfIndex = newIfIndex;
|
1997-07-28 01:02:27 +00:00
|
|
|
close(s);
|
1997-05-10 01:22:19 +00:00
|
|
|
free(buffer);
|
1997-11-08 00:28:11 +00:00
|
|
|
return (newIfIndex);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1997-11-08 00:28:11 +00:00
|
|
|
newIfIndex++;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
len -= elen;
|
1997-08-25 00:29:32 +00:00
|
|
|
ifrp = (struct ifreq *) ((char *) ifrp + elen);
|
1995-01-31 06:29:58 +00:00
|
|
|
ifrp++;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(s);
|
1996-10-06 13:32:37 +00:00
|
|
|
free(buffer);
|
1997-08-25 00:29:32 +00:00
|
|
|
return (-1);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|