Mailing List Archive

ifconfig C module
I got permission to publish the results of my 'ifconfig' module.

Since it's rather small, I thought I would post the whole thing here.

I realize it's not perfect, but it does what I need. I'll accept
patches to make it better. I'm sure the license could be changed to
the Python one if there is interest in including it in the main
distribution.

-------------------------------

/* $Id: ifmodule.c,v 1.2 1999/08/12 19:43:42 dwelton Exp $ */

/*
Copyright (C) 1999 Linuxcare Inc.

By David N. Welton <davidw@linuxcare.com>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA
*/

/*
To build under Debian GNU/Linux:

gcc -shared -Wl,-soname,pyifconfig.so -o pyifconfig.so ifmodule.c
*/


#include <python1.5/Python.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/in.h>
#include <arpa/inet.h>

static PyObject *pyifconfig(PyObject *self, PyObject *args)
{
int fd;

struct ifreq ifreq;
unsigned char *hw;
struct sockaddr_in *sin;
char *itf;

char pyhwaddr[40];
char pyaddr[20];
char pybrdaddr[20];
char pynetmask[20];

if (!PyArg_ParseTuple(args, "s", &itf))
return NULL;

fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);

strcpy(ifreq.ifr_name, itf);

/* hardware address */
ioctl(fd, SIOCGIFHWADDR, &ifreq);
hw = ifreq.ifr_hwaddr.sa_data;
sprintf(pyhwaddr, "%02x:%02x:%02x:%02x:%02x:%02x",
*hw, *(hw + 1), *(hw + 2), *(hw + 3), *(hw + 4), *(hw + 5));

/* address */
ioctl(fd, SIOCGIFADDR, &ifreq);
sin = (struct sockaddr_in *)&ifreq.ifr_broadaddr;
sprintf(pyaddr, "%s", inet_ntoa(sin->sin_addr));

/* broadcast */
ioctl(fd, SIOCGIFBRDADDR, &ifreq);
sin = (struct sockaddr_in *)&ifreq.ifr_broadaddr;
sprintf(pybrdaddr, "%s", inet_ntoa(sin->sin_addr));

/* netmask */
ioctl(fd, SIOCGIFNETMASK, &ifreq);
sin = (struct sockaddr_in *)&ifreq.ifr_broadaddr;
sprintf(pynetmask, "%s", inet_ntoa(sin->sin_addr));

close(fd);
return Py_BuildValue("{s:s,s:s,s:s,s:s}",
"hwaddr", pyhwaddr,
"addr", pyaddr,
"brdaddr", pybrdaddr,
"netmask", pynetmask);
}

/* The command pyifconfig.pyifconfig(interface_name), where
interface_name is the name of the interface, such as 'eth0' or 'lo'
returns a dictionary with the following keys:

hwaddr: hardware address
addr: ip address
brdaddr: broadcast address
netmask: netmask
*/

static struct PyMethodDef pyifmethods[] = {
{"pyifconfig", pyifconfig, METH_VARARGS},
{NULL, NULL}
};

void initpyifconfig()
{
Py_InitModule("pyifconfig", pyifmethods);
}


------------------------------

Have fun,
--
David N. Welton, Web Engineer, Linuxcare, Inc.
415.354.4878 x241 tel, 415.701.7457 fax
dwelton@linuxcare.com, http://www.linuxcare.com/
Linuxcare. At the center of Linux.
ifconfig C module [ In reply to ]
> I got permission to publish the results of my 'ifconfig' module.
>
> Since it's rather small, I thought I would post the whole thing here.
>
> I realize it's not perfect, but it does what I need. I'll accept
> patches to make it better. I'm sure the license could be changed to
> the Python one if there is interest in including it in the main
> distribution.

I'd suggest adding this to either the socketmodule.c or the
fcntlmodule.c in the standard Python distribution.

FYI, I've spiced up the file a bit and turned it into a package
which is available here for easier install:

http://starship.skyport.net/~lemburg/ifconfig-0.1.zip

I changed the names a little in the above archive:

Package name: ifconfig
API: ifconfig.ifconfig

From David's code:

The command ifconfig.ifconfig(interface_name), where
interface_name is the name of the interface, such as 'eth0' or 'lo'
returns a dictionary with the following keys:

hwaddr: hardware address
addr: ip address
brdaddr: broadcast address
netmask: netmask

Great work, David ! I never thought it would be that easy to get
at the hardware MAC address. Now the only question remaining is:
how portable is this code ? E.g. does it compile on Windows ?

Cheers,
--
Marc-Andre Lemburg
______________________________________________________________________
Y2000: 140 days left
Business: http://www.lemburg.com/
Python Pages: http://www.lemburg.com/python/