[Python-Dev] PATCH/RFC for AF_NETLINK support

Philippe Biondi phil at secdev.org
Mon Jan 10 17:17:49 CET 2005


Hi,

I've done a small patch to use linux AF_NETLINK sockets (see below).
Please comment!

Is there a reason for recvmsg() and sendmsg() not to be implemented
yet in socketmodule ?


The integration with autoconf has not been done, even if
this patch should be ok :

--- configure.in.ori    2005-01-10 17:09:32.000000000 +0100
+++ configure.in        2005-01-06 18:53:18.000000000 +0100
@@ -967,7 +967,7 @@
 sys/audioio.h sys/bsdtty.h sys/file.h sys/loadavg.h sys/lock.h sys/mkdev.h \
 sys/modem.h \
 sys/param.h sys/poll.h sys/select.h sys/socket.h sys/time.h sys/times.h \
-sys/un.h sys/utsname.h sys/wait.h pty.h libutil.h \
+sys/un.h linux/netlink.h sys/utsname.h sys/wait.h pty.h libutil.h \
 sys/resource.h netpacket/packet.h sysexits.h bluetooth.h \
 bluetooth/bluetooth.h)
 AC_HEADER_DIRENT
--- pyconfig.h.ori      2005-01-10 17:11:11.000000000 +0100
+++ pyconfig.h  2005-01-06 19:27:33.000000000 +0100
@@ -559,6 +559,9 @@
 /* Define to 1 if you have the <sys/un.h> header file. */
 #define HAVE_SYS_UN_H 1

+/* Define to 1 if you have the <linux/netlink.h> header file. */
+#define HAVE_LINUX_NETLINK_H 1
+
 /* Define to 1 if you have the <sys/utsname.h> header file. */
 #define HAVE_SYS_UTSNAME_H 1



--- socketmodule.h.ori  2005-01-07 19:25:18.000000000 +0100
+++ socketmodule.h      2005-01-06 18:20:54.000000000 +0100
@@ -32,6 +32,12 @@
 # undef AF_UNIX
 #endif

+#ifdef HAVE_LINUX_NETLINK_H
+# include <linux/netlink.h>
+#else
+#  undef AF_NETLINK
+#endif
+
 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
 #include <bluetooth/bluetooth.h>
 #include <bluetooth/rfcomm.h>
@@ -87,6 +93,9 @@ typedef struct {
 #ifdef AF_UNIX
                struct sockaddr_un un;
 #endif
+#ifdef AF_NETLINK
+               struct sockaddr_nl nl;
+#endif
 #ifdef ENABLE_IPV6
                struct sockaddr_in6 in6;
                struct sockaddr_storage storage;
--- socketmodule.c.ori  2005-01-07 19:25:19.000000000 +0100
+++ socketmodule.c      2005-01-10 17:04:38.000000000 +0100
@@ -948,6 +948,14 @@ makesockaddr(int sockfd, struct sockaddr
        }
 #endif /* AF_UNIX */

+#if defined(AF_NETLINK)
+       case AF_NETLINK:
+       {
+               struct sockaddr_nl *a = (struct sockaddr_nl *) addr;
+               return Py_BuildValue("ii", a->nl_pid, a->nl_groups);
+       }
+#endif /* AF_NETLINK */
+
 #ifdef ENABLE_IPV6
        case AF_INET6:
        {
@@ -1084,6 +1092,31 @@ getsockaddrarg(PySocketSockObject *s, Py
        }
 #endif /* AF_UNIX */

+#if defined(AF_NETLINK)
+       case AF_NETLINK:
+       {
+               struct sockaddr_nl* addr;
+               int pid, groups;
+               addr = (struct sockaddr_nl *)&(s->sock_addr).nl;
+               if (!PyTuple_Check(args)) {
+                       PyErr_Format(
+                               PyExc_TypeError,
+                               "getsockaddrarg: "
+                               "AF_NETLINK address must be tuple, not %.500s",
+                               args->ob_type->tp_name);
+                       return 0;
+               }
+               if (!PyArg_ParseTuple(args, "II", &pid, &groups))
+                       return 0;
+               addr->nl_family = AF_NETLINK;
+               addr->nl_pid = pid;
+               addr->nl_groups = groups;
+               *addr_ret = (struct sockaddr *) addr;
+               *len_ret = sizeof(*addr);
+               return 1;
+       }
+#endif
+
        case AF_INET:
        {
                struct sockaddr_in* addr;
@@ -1280,6 +1313,13 @@ getsockaddrlen(PySocketSockObject *s, so
                return 1;
        }
 #endif /* AF_UNIX */
+#if defined(AF_NETLINK)
+       case AF_NETLINK:
+       {
+               *len_ret = sizeof (struct sockaddr_nl);
+               return 1;
+       }
+#endif

        case AF_INET:
        {
@@ -3938,8 +3978,20 @@ init_socket(void)
        PyModule_AddIntConstant(m, "AF_KEY", AF_KEY);
 #endif
 #ifdef AF_NETLINK
-       /*  */
+       /* Netlink socket */
        PyModule_AddIntConstant(m, "AF_NETLINK", AF_NETLINK);
+       PyModule_AddIntConstant(m, "NETLINK_ROUTE", NETLINK_ROUTE);
+       PyModule_AddIntConstant(m, "NETLINK_SKIP", NETLINK_SKIP);
+       PyModule_AddIntConstant(m, "NETLINK_USERSOCK", NETLINK_USERSOCK);
+       PyModule_AddIntConstant(m, "NETLINK_FIREWALL", NETLINK_FIREWALL);
+       PyModule_AddIntConstant(m, "NETLINK_TCPDIAG", NETLINK_TCPDIAG);
+       PyModule_AddIntConstant(m, "NETLINK_NFLOG", NETLINK_NFLOG);
+       PyModule_AddIntConstant(m, "NETLINK_XFRM", NETLINK_XFRM);
+       PyModule_AddIntConstant(m, "NETLINK_ARPD", NETLINK_ARPD);
+       PyModule_AddIntConstant(m, "NETLINK_ROUTE6", NETLINK_ROUTE6);
+       PyModule_AddIntConstant(m, "NETLINK_IP6_FW", NETLINK_IP6_FW);
+       PyModule_AddIntConstant(m, "NETLINK_DNRTMSG", NETLINK_DNRTMSG);
+       PyModule_AddIntConstant(m, "NETLINK_TAPBASE", NETLINK_TAPBASE);
 #endif
 #ifdef AF_ROUTE
        /* Alias to emulate 4.4BSD */


-- 
Philippe Biondi <phil@ secdev.org>      SecDev.org
Security Consultant/R&D                 http://www.secdev.org
PGP KeyID:3D9A43E2  FingerPrint:C40A772533730E39330DC0985EE8FF5F3D9A43E2



More information about the Python-Dev mailing list