[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
PPP port: iface_Create
From: |
Daniel E Baumann |
Subject: |
PPP port: iface_Create |
Date: |
Sat, 2 Dec 2000 12:31:27 -0600 |
I have writen the function to create an iface startuct in PPP using several
SIOC* ioctl glibc calls. This is the first time I've used this so I am
posting it for inspection, comments and all that. Also, is it ok to reuse the
same ifreq struct in the ioctl calls or should I declare one for each type of
call to pass in. Also, Marcus if your not implementing any of these calls
please let me know. I've got your list, but I haven't checked it yet.
struct iface *
iface_Create(const char *name)
{
#ifdef __GNU__
int s;
int flags;
struct iface_addr *addr;
struct ifreq *ifr;
struct iface *iface = NULL;
s = socket(AF_INET, SOCK_DGRAM, 0);
if (s < 0)
{
fprintf(stderr, "iface_Create: socket(): %s\n", strerror(errno));
return NULL;
}
if ((iface = (struct iface *)malloc(sizeof (struct iface))) == NULL)
{
fprintf(stderr, "iface_Create: malloc: %s\n", strerror(errno));
close (s);
return NULL;
}
if ((addr = (struct iface_addr *)malloc(sizeof (struct iface_addr))) ==
NULL)
{
fprintf(stderr, "iface_Create: malloc: %s\n", strerror(errno));
free (iface);
close (s);
return NULL;
}
if ((ifr = (struct ifreq *)malloc(sizeof (struct ifreq))) == NULL)
{
fprintf(stderr, "iface_Create: malloc: %s\n", strerror(errno));
free (iface);
free (addr);
close (s);
return NULL;
}
ioctl(s, SIOCGIFFLAGS, &ifr);
flags = ifr->ifr_flags;
iface->name = strdup(name);
iface->flags = flags;
iface->index = 0;
iface->in_addrs = 0;
iface->in_addr = addr;
if (ioctl (s, SIOCGIFADDR, &ifr) < 0)
{
fprintf(stderr, "iface_Create: ioctl: %s\n", strerror(errno));
free (iface);
free (addr);
free (ifr);
close (s);
return NULL;
}
else
addr->ifa = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
if (ioctl (s, SIOCGIFDSTADDR, &ifr) < 0)
addr->brd.s_addr = INADDR_ANY;
else
addr->brd = ((struct sockaddr_in *)&ifr->ifr_dstaddr)->sin_addr;
if (ioctl (s, SIOCGIFNETMASK, &ifr) < 0)
addr->mask.s_addr = INADDR_ANY;
else
addr->mask = ((struct sockaddr_in *)&ifr->ifr_netmask)->sin_addr;
addr->bits = bitsinmask(addr->mask);
free (ifr);
close (s);
return iface;
#else
...
Dan
---------------------------------------------------------
Daniel E. Baumann
E-mail:
baumannd@msoe.edu (preferred)
baumannd@users.sourceforge.net
baumannd@penguinpowered.com
baumannd@obfuscation.penguinpowered.com (caution: dynamic DNS)
Web location:
http://www.msoe.edu/~baumannd
http://www.linuxfreak.com/~baumannd
"Life would be so much easier if we could just look at the source code."
-- Dave Olson
"...that as we enjoy great advantages from the inventions of others, we should
be glad of an opportunity to serve others by an invention of ours, and this we
should do freely and generously..."
-- Benjamin Franklin
- PPP port: iface_Create,
Daniel E Baumann <=