Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 11017b1

Browse files
committed
Patch #1103116: AF_NETLINK sockets basic support.
1 parent 015f72b commit 11017b1

8 files changed

Lines changed: 160 additions & 6 deletions

File tree

Doc/lib/libsocket.tex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ \section{\module{socket} ---
6868
configuration. For deterministic behavior use a numeric address in
6969
\var{host} portion.
7070

71+
\versionadded[2.5]{AF_NETLINK sockets are represented as
72+
pairs \code{\var{pid}, \var{groups}}.}
73+
7174
All errors raise exceptions. The normal exceptions for invalid
7275
argument types and out-of-memory conditions can be raised; errors
7376
related to socket or address semantics raise the error

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Steven Bethard
5858
Stephen Bevan
5959
Ron Bickers
6060
Dominic Binks
61+
Philippe Biondi
6162
Stuart Bishop
6263
Roy Bixler
6364
Martin Bless

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ Core and builtins
216216
Extension Modules
217217
-----------------
218218

219+
- Patch #1103116: Basic AF_NETLINK support.
220+
219221
- Bug #1402308, (possible) segfault when using mmap.mmap(-1, ...)
220222

221223
- Bug #1400822, _curses over{lay,write} doesn't work when passing 6 ints.

Modules/socketmodule.c

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ This module provides an interface to Berkeley socket IPC.
77
Limitations:
88
99
- Only AF_INET, AF_INET6 and AF_UNIX address families are supported in a
10-
portable manner, though AF_PACKET is supported under Linux.
10+
portable manner, though AF_PACKET and AF_NETLINK are supported under Linux.
1111
- No read/write operations (use sendall/recv or makefile instead).
1212
- Additional restrictions apply on some non-Unix platforms (compensated
1313
for by socket.py).
@@ -954,6 +954,14 @@ makesockaddr(int sockfd, struct sockaddr *addr, int addrlen, int proto)
954954
}
955955
#endif /* AF_UNIX */
956956

957+
#if defined(AF_NETLINK)
958+
case AF_NETLINK:
959+
{
960+
struct sockaddr_nl *a = (struct sockaddr_nl *) addr;
961+
return Py_BuildValue("ii", a->nl_pid, a->nl_groups);
962+
}
963+
#endif /* AF_NETLINK */
964+
957965
#ifdef ENABLE_IPV6
958966
case AF_INET6:
959967
{
@@ -1090,6 +1098,31 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args,
10901098
}
10911099
#endif /* AF_UNIX */
10921100

1101+
#if defined(AF_NETLINK)
1102+
case AF_NETLINK:
1103+
{
1104+
struct sockaddr_nl* addr;
1105+
int pid, groups;
1106+
addr = (struct sockaddr_nl *)&(s->sock_addr).nl;
1107+
if (!PyTuple_Check(args)) {
1108+
PyErr_Format(
1109+
PyExc_TypeError,
1110+
"getsockaddrarg: "
1111+
"AF_NETLINK address must be tuple, not %.500s",
1112+
args->ob_type->tp_name);
1113+
return 0;
1114+
}
1115+
if (!PyArg_ParseTuple(args, "II:getsockaddrarg", &pid, &groups))
1116+
return 0;
1117+
addr->nl_family = AF_NETLINK;
1118+
addr->nl_pid = pid;
1119+
addr->nl_groups = groups;
1120+
*addr_ret = (struct sockaddr *) addr;
1121+
*len_ret = sizeof(*addr);
1122+
return 1;
1123+
}
1124+
#endif
1125+
10931126
case AF_INET:
10941127
{
10951128
struct sockaddr_in* addr;
@@ -1286,6 +1319,13 @@ getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret)
12861319
return 1;
12871320
}
12881321
#endif /* AF_UNIX */
1322+
#if defined(AF_NETLINK)
1323+
case AF_NETLINK:
1324+
{
1325+
*len_ret = sizeof (struct sockaddr_nl);
1326+
return 1;
1327+
}
1328+
#endif
12891329

12901330
case AF_INET:
12911331
{
@@ -3947,6 +3987,18 @@ init_socket(void)
39473987
#ifdef AF_NETLINK
39483988
/* */
39493989
PyModule_AddIntConstant(m, "AF_NETLINK", AF_NETLINK);
3990+
PyModule_AddIntConstant(m, "NETLINK_ROUTE", NETLINK_ROUTE);
3991+
PyModule_AddIntConstant(m, "NETLINK_SKIP", NETLINK_SKIP);
3992+
PyModule_AddIntConstant(m, "NETLINK_USERSOCK", NETLINK_USERSOCK);
3993+
PyModule_AddIntConstant(m, "NETLINK_FIREWALL", NETLINK_FIREWALL);
3994+
PyModule_AddIntConstant(m, "NETLINK_TCPDIAG", NETLINK_TCPDIAG);
3995+
PyModule_AddIntConstant(m, "NETLINK_NFLOG", NETLINK_NFLOG);
3996+
PyModule_AddIntConstant(m, "NETLINK_XFRM", NETLINK_XFRM);
3997+
PyModule_AddIntConstant(m, "NETLINK_ARPD", NETLINK_ARPD);
3998+
PyModule_AddIntConstant(m, "NETLINK_ROUTE6", NETLINK_ROUTE6);
3999+
PyModule_AddIntConstant(m, "NETLINK_IP6_FW", NETLINK_IP6_FW);
4000+
PyModule_AddIntConstant(m, "NETLINK_DNRTMSG", NETLINK_DNRTMSG);
4001+
PyModule_AddIntConstant(m, "NETLINK_TAPBASE", NETLINK_TAPBASE);
39504002
#endif
39514003
#ifdef AF_ROUTE
39524004
/* Alias to emulate 4.4BSD */

Modules/socketmodule.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@
3232
# undef AF_UNIX
3333
#endif
3434

35+
#ifdef HAVE_LINUX_NETLINK_H
36+
# include <linux/netlink.h>
37+
#else
38+
# undef AF_NETLINK
39+
#endif
40+
3541
#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
3642
#include <bluetooth/bluetooth.h>
3743
#include <bluetooth/rfcomm.h>
@@ -78,6 +84,9 @@ typedef union sock_addr {
7884
#ifdef AF_UNIX
7985
struct sockaddr_un un;
8086
#endif
87+
#ifdef AF_NETLINK
88+
struct sockaddr_nl nl;
89+
#endif
8190
#ifdef ENABLE_IPV6
8291
struct sockaddr_in6 in6;
8392
struct sockaddr_storage storage;

configure

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#! /bin/sh
2-
# From configure.in Revision: 41975 .
2+
# From configure.in Revision: 41984 .
33
# Guess values for system-dependent variables and create Makefiles.
44
# Generated by GNU Autoconf 2.59 for python 2.5.
55
#
@@ -4609,8 +4609,9 @@ done
46094609

46104610

46114611

4612-
for ac_header in curses.h dlfcn.h fcntl.h grp.h shadow.h langinfo.h \
4613-
libintl.h ncurses.h poll.h pthread.h \
4612+
4613+
for ac_header in asm/types.h curses.h dlfcn.h fcntl.h grp.h \
4614+
shadow.h langinfo.h libintl.h ncurses.h poll.h pthread.h \
46144615
stropts.h termios.h thread.h \
46154616
unistd.h utime.h \
46164617
sys/audioio.h sys/bsdtty.h sys/file.h sys/loadavg.h sys/lock.h sys/mkdev.h \
@@ -5519,6 +5520,76 @@ fi
55195520
done
55205521

55215522

5523+
# On Linux, netlink.h requires asm/types.h
5524+
5525+
for ac_header in linux/netlink.h
5526+
do
5527+
as_ac_Header=`echo "ac_cv_header_$ac_header" | $as_tr_sh`
5528+
echo "$as_me:$LINENO: checking for $ac_header" >&5
5529+
echo $ECHO_N "checking for $ac_header... $ECHO_C" >&6
5530+
if eval "test \"\${$as_ac_Header+set}\" = set"; then
5531+
echo $ECHO_N "(cached) $ECHO_C" >&6
5532+
else
5533+
cat >conftest.$ac_ext <<_ACEOF
5534+
/* confdefs.h. */
5535+
_ACEOF
5536+
cat confdefs.h >>conftest.$ac_ext
5537+
cat >>conftest.$ac_ext <<_ACEOF
5538+
/* end confdefs.h. */
5539+
5540+
#ifdef HAVE_ASM_TYPES_H
5541+
#include <asm/types.h>
5542+
#endif
5543+
#ifdef HAVE_SYS_SOCKET_H
5544+
#include <sys/socket.h>
5545+
#endif
5546+
5547+
5548+
#include <$ac_header>
5549+
_ACEOF
5550+
rm -f conftest.$ac_objext
5551+
if { (eval echo "$as_me:$LINENO: \"$ac_compile\"") >&5
5552+
(eval $ac_compile) 2>conftest.er1
5553+
ac_status=$?
5554+
grep -v '^ *+' conftest.er1 >conftest.err
5555+
rm -f conftest.er1
5556+
cat conftest.err >&5
5557+
echo "$as_me:$LINENO: \$? = $ac_status" >&5
5558+
(exit $ac_status); } &&
5559+
{ ac_try='test -z "$ac_c_werror_flag"
5560+
|| test ! -s conftest.err'
5561+
{ (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
5562+
(eval $ac_try) 2>&5
5563+
ac_status=$?
5564+
echo "$as_me:$LINENO: \$? = $ac_status" >&5
5565+
(exit $ac_status); }; } &&
5566+
{ ac_try='test -s conftest.$ac_objext'
5567+
{ (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
5568+
(eval $ac_try) 2>&5
5569+
ac_status=$?
5570+
echo "$as_me:$LINENO: \$? = $ac_status" >&5
5571+
(exit $ac_status); }; }; then
5572+
eval "$as_ac_Header=yes"
5573+
else
5574+
echo "$as_me: failed program was:" >&5
5575+
sed 's/^/| /' conftest.$ac_ext >&5
5576+
5577+
eval "$as_ac_Header=no"
5578+
fi
5579+
rm -f conftest.err conftest.$ac_objext conftest.$ac_ext
5580+
fi
5581+
echo "$as_me:$LINENO: result: `eval echo '${'$as_ac_Header'}'`" >&5
5582+
echo "${ECHO_T}`eval echo '${'$as_ac_Header'}'`" >&6
5583+
if test `eval echo '${'$as_ac_Header'}'` = yes; then
5584+
cat >>confdefs.h <<_ACEOF
5585+
#define `echo "HAVE_$ac_header" | $as_tr_cpp` 1
5586+
_ACEOF
5587+
5588+
fi
5589+
5590+
done
5591+
5592+
55225593
# checks for typedefs
55235594
was_it_defined=no
55245595
echo "$as_me:$LINENO: checking for clock_t in time.h" >&5

configure.in

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -994,8 +994,8 @@ dnl AC_MSG_RESULT($cpp_type)
994994

995995
# checks for header files
996996
AC_HEADER_STDC
997-
AC_CHECK_HEADERS(curses.h dlfcn.h fcntl.h grp.h shadow.h langinfo.h \
998-
libintl.h ncurses.h poll.h pthread.h \
997+
AC_CHECK_HEADERS(asm/types.h curses.h dlfcn.h fcntl.h grp.h \
998+
shadow.h langinfo.h libintl.h ncurses.h poll.h pthread.h \
999999
stropts.h termios.h thread.h \
10001000
unistd.h utime.h \
10011001
sys/audioio.h sys/bsdtty.h sys/file.h sys/loadavg.h sys/lock.h sys/mkdev.h \
@@ -1014,6 +1014,16 @@ AC_CHECK_HEADERS(term.h,,,[
10141014
#endif
10151015
])
10161016

1017+
# On Linux, netlink.h requires asm/types.h
1018+
AC_CHECK_HEADERS(linux/netlink.h,,,[
1019+
#ifdef HAVE_ASM_TYPES_H
1020+
#include <asm/types.h>
1021+
#endif
1022+
#ifdef HAVE_SYS_SOCKET_H
1023+
#include <sys/socket.h>
1024+
#endif
1025+
])
1026+
10171027
# checks for typedefs
10181028
was_it_defined=no
10191029
AC_MSG_CHECKING(for clock_t in time.h)

pyconfig.h.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
/* Define this if your time.h defines altzone. */
3838
#undef HAVE_ALTZONE
3939

40+
/* Define to 1 if you have the <asm/types.h> header file. */
41+
#undef HAVE_ASM_TYPES_H
42+
4043
/* Define to 1 if you have the `bind_textdomain_codeset' function. */
4144
#undef HAVE_BIND_TEXTDOMAIN_CODESET
4245

@@ -290,6 +293,9 @@
290293
/* Define if you have the 'link' function. */
291294
#undef HAVE_LINK
292295

296+
/* Define to 1 if you have the <linux/netlink.h> header file. */
297+
#undef HAVE_LINUX_NETLINK_H
298+
293299
/* Define this if you have the type long long. */
294300
#undef HAVE_LONG_LONG
295301

0 commit comments

Comments
 (0)