From 06b336cca325b03155a8afa79e99531e75d0e7f2 Mon Sep 17 00:00:00 2001 From: Daniel Hokka Zakrisson Date: Fri, 12 Dec 2008 21:19:38 +0000 Subject: [PATCH] Commit 11328 for trunk. --- pyplnet.spec | 9 +-- setup.py | 4 +- sioc.c | 160 --------------------------------------------------- sioc.py | 66 +++++++++++++++++++++ 4 files changed, 72 insertions(+), 167 deletions(-) delete mode 100644 sioc.c create mode 100644 sioc.py diff --git a/pyplnet.spec b/pyplnet.spec index 218321f..df5a919 100644 --- a/pyplnet.spec +++ b/pyplnet.spec @@ -9,7 +9,7 @@ %define release %{taglevel}%{?pldistro:.%{pldistro}}%{?date:.%{date}} -%{!?python_sitearch: %define python_sitearch %(%{__python} -c "from distutils.sysconfig import get_python_lib; print get_python_lib(1)")} +%{!?python_sitelib: %define python_sitelib %(%{__python} -c "from distutils.sysconfig import get_python_lib; print get_python_lib()")} Summary: PlanetLab Network Configuration library Name: %{name} @@ -27,6 +27,7 @@ URL: %(echo %{url} | cut -d ' ' -f 2) Requires: python >= 2.4 BuildRequires: python, python-devel +BuildArch: noarch %description pyplnet is used to write the network configuration files based on the @@ -41,9 +42,9 @@ python setup.py build %install rm -rf $RPM_BUILD_ROOT python setup.py install --skip-build --root "$RPM_BUILD_ROOT" -chmod +x $RPM_BUILD_ROOT/%{python_sitearch}/plnet.py +chmod +x $RPM_BUILD_ROOT/%{python_sitelib}/plnet.py mkdir -p $RPM_BUILD_ROOT/%{_bindir} -ln -s %{python_sitearch}/plnet.py $RPM_BUILD_ROOT/%{_bindir}/plnet +ln -s %{python_sitelib}/plnet.py $RPM_BUILD_ROOT/%{_bindir}/plnet %clean rm -rf $RPM_BUILD_ROOT @@ -51,7 +52,7 @@ rm -rf $RPM_BUILD_ROOT %files %defattr(-,root,root,-) %{_bindir}/plnet -%{python_sitearch}/* +%{python_sitelib}/* %changelog * Tue Dec 2 2008 Daniel Hokka Zakrisson - pyplnet-4.3-1 diff --git a/setup.py b/setup.py index 4a9d1f3..5993ca4 100644 --- a/setup.py +++ b/setup.py @@ -46,12 +46,10 @@ class bdist_rpmspec(Command): setup( name='pyplnet', version='4.3', - ext_modules=[ - Extension('sioc', ['sioc.c']), - ], py_modules=[ 'plnet', 'modprobe', + 'sioc', ], cmdclass={'sdist': my_sdist, 'bdist_rpmspec': bdist_rpmspec}, ) diff --git a/sioc.c b/sioc.c deleted file mode 100644 index 0ddeb31..0000000 --- a/sioc.c +++ /dev/null @@ -1,160 +0,0 @@ -/* - * Extension to gather information about network interfaces - * - * Mark Huang - * Copyright (C) 2006 The Trustees of Princeton University - * - * $Id$ - */ - -#include - -/* struct ifreq */ -#include - -/* socket() */ -#include -#include - -/* ioctl() */ -#include - -/* inet_ntoa() */ -#include -#include - -/* ARPHRD_ETHER */ -#include - -/* ETH_ALEN */ -#include - -static PyObject * -gifconf(PyObject *self, PyObject *args) -{ - struct ifconf ifc; - int len; - int s; - PyObject *addrs; - void *buf; - struct ifreq *ifr; - struct sockaddr_in *sin; - - if ((s = socket(PF_INET, SOCK_DGRAM, 0)) < 0) - return PyErr_SetFromErrno(PyExc_OSError); - - len = sizeof(struct ifreq); - ifc.ifc_len = 0; - ifc.ifc_req = NULL; - - do { - len *= 2; - buf = realloc(ifc.ifc_req, len); - if (!buf) - break; - ifc.ifc_len = len; - ifc.ifc_req = buf; - if (ioctl(s, SIOCGIFCONF, &ifc) < 0) - break; - } while (ifc.ifc_len >= len); - - close(s); - - addrs = Py_BuildValue("{}"); - - for (ifr = ifc.ifc_req, len = ifc.ifc_len; len > 0; ifr++, len -= sizeof(struct ifreq)) { - sin = (struct sockaddr_in *) &ifr->ifr_addr; - PyDict_SetItem(addrs, - Py_BuildValue("s", ifr->ifr_name), - Py_BuildValue("s", inet_ntoa(sin->sin_addr))); - } - - if (ifc.ifc_req) - free(ifc.ifc_req); - - return addrs; -} - -static PyObject * -gifaddr(PyObject *self, PyObject *args) -{ - const char *name; - struct ifreq ifr; - int s; - struct sockaddr_in *sin; - - if (!PyArg_ParseTuple(args, "s", &name)) - return NULL; - - memset(&ifr, 0, sizeof(ifr)); - strncpy(ifr.ifr_name, name, IFNAMSIZ); - - if ((s = socket(PF_INET, SOCK_DGRAM, 0)) < 0) - return PyErr_SetFromErrno(PyExc_OSError); - - if (ioctl(s, SIOCGIFADDR, &ifr) < 0) { - close(s); - return PyErr_SetFromErrno(PyExc_OSError); - } - - close(s); - - sin = (struct sockaddr_in *) &ifr.ifr_addr; - return Py_BuildValue("s", inet_ntoa(sin->sin_addr)); -} - -static PyObject * -gifhwaddr(PyObject *self, PyObject *args) -{ - const char *name; - struct ifreq ifr; - int s; - char mac[sizeof(ifr.ifr_hwaddr.sa_data) * 3], *c; - int len, i; - - if (!PyArg_ParseTuple(args, "s", &name)) - return NULL; - - memset(&ifr, 0, sizeof(ifr)); - strncpy(ifr.ifr_name, name, IFNAMSIZ); - - if ((s = socket(PF_INET, SOCK_DGRAM, 0)) < 0) - return PyErr_SetFromErrno(PyExc_OSError); - - if (ioctl(s, SIOCGIFHWADDR, &ifr) < 0) { - close(s); - return PyErr_SetFromErrno(PyExc_OSError); - } - - close(s); - - switch (ifr.ifr_hwaddr.sa_family) { - case ARPHRD_ETHER: - len = ETH_ALEN; - break; - default: - len = sizeof(ifr.ifr_hwaddr.sa_data); - break; - } - - for (i = 0, c = mac; i < len; i++) { - if (i) - c += sprintf(c, ":"); - c += sprintf(c, "%02X", (unsigned char)(ifr.ifr_hwaddr.sa_data[i] & 0xFF)); - } - - return Py_BuildValue("s", mac); -} - -static PyMethodDef methods[] = { - { "gifconf", gifconf, METH_VARARGS, "Get all interface addresses" }, - { "gifaddr", gifaddr, METH_VARARGS, "Get interface address" }, - { "gifhwaddr", gifhwaddr, METH_VARARGS, "Get interface hardware address" }, - { NULL, NULL, 0, NULL } -}; - -PyMODINIT_FUNC -initsioc(void) -{ - Py_InitModule("sioc", methods); -} diff --git a/sioc.py b/sioc.py new file mode 100644 index 0000000..1bb0095 --- /dev/null +++ b/sioc.py @@ -0,0 +1,66 @@ +# $Id$ +# vim:set ts=4 sw=4 expandtab: +# (c) Copyright 2008 The Trustees of Princeton University + +import os +import socket +import fcntl +import struct + +SIOCGIFADDR = 0x8915 +SIOCGIFADDR_struct = "16xH2xI8x" +SIOCGIFHWADDR = 0x8927 +SIOCGIFHWADDR_struct = "16x2x6B8x" + +def _format_ip(nip): + ip = socket.ntohl(nip) + return "%d.%d.%d.%d" % ((ip & 0xff000000) >> 24, + (ip & 0x00ff0000) >> 16, + (ip & 0x0000ff00) >> 8, + (ip & 0x000000ff)) + +def gifaddr(interface): + s = None + try: + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0) + ifreq = fcntl.ioctl(s.fileno(), SIOCGIFADDR, struct.pack("16s16x", interface)) + (family, ip) = struct.unpack(SIOCGIFADDR_struct, ifreq) + if family == socket.AF_INET: + return _format_ip(ip) + finally: + if s is not None: + s.close() + return None + +def gifconf(): + interfaces = os.listdir("/sys/class/net") + s = None + ret = {} + try: + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0) + for interface in interfaces: + try: + ifreq = fcntl.ioctl(s.fileno(), SIOCGIFADDR, + struct.pack("16sH14x", interface, socket.AF_INET)) + (family, ip) = struct.unpack(SIOCGIFADDR_struct, ifreq) + if family == socket.AF_INET: + ret[interface] = _format_ip(ip) + else: + raise Exception + except: + ret[interface] = "0.0.0.0" + finally: + if s is not None: + s.close() + return ret + +def gifhwaddr(interface): + s = None + try: + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0) + ifreq = fcntl.ioctl(s.fileno(), SIOCGIFHWADDR, struct.pack("16s16x", interface)) + mac = struct.unpack(SIOCGIFHWADDR_struct, ifreq) + return "%02x:%02x:%02x:%02x:%02x:%02x" % mac + finally: + s.close() + return None -- 2.43.0