This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: socket.setsockopt() is broken for multicast TTL and Loop options
Type: behavior Stage:
Components: Library (Lib) Versions: Python 2.4, Python 3.4, Python 2.7, Python 2.5
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: gpolo, niallo, tamentis
Priority: normal Keywords:

Created on 2008-07-16 01:38 by niallo, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (4)
msg69741 - (view) Author: Niall O'Higgins (niallo) Date: 2008-07-16 01:38
socket.setsockopt() sets an optlen of '4' in the setsockopt system call
for options IP_MULTICAST_TTL and IP_MULTICAST_LOOP.  On OpenBSD, this
causes the kernel to hit an error condition and set errno 22 when these
options are set:

socket.error: (22, 'Invalid argument')

Yielded by e.g. socket.setsockopt(socket.IPPROTO_IP,
socket.IP_MULTICAST_TTL, 1)

According to both FreeBSD and OpenBSD manual pages (see e.g.
http://www.freebsd.org/cgi/man.cgi?query=ip&apropos=0&sektion=0&manpath=FreeBSD+7.0-RELEASE&format=html),
these fields are in fact both 8 bit unsigned, and the manuals suggest
the following:

u_char ttl;     /* range: 0 to 255, default = 1 */                     
                    
setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl));

While '4' (sizeof int) is correct for many options, special casing is
needed in Modules/socketmodule.c.  The following diff fixes the issue
for me on OpenBSD:

 @@ -1716,12 +1719,8 @@ sock_setsockopt(PySocketSockObject *s, PyObject
*args)
 
        if (PyArg_ParseTuple(args, "iii:setsockopt",
                             &level, &optname, &flag)) {
-               buflen = sizeof flag;
-               /* Multi cast options take shorter arguments */
-               if (optname == IP_MULTICAST_TTL
-                   || optname == IP_MULTICAST_LOOP)
-                       buflen = sizeof(u_char);
                buf = (char *) &flag;
+               buflen = sizeof flag;
        }
        else {
                PyErr_Clear();
msg69743 - (view) Author: Niall O'Higgins (niallo) Date: 2008-07-16 01:39
This also appears to be a bug in Python 2.4.
msg69953 - (view) Author: Guilherme Polo (gpolo) * (Python committer) Date: 2008-07-18 13:07
This is already fixed in release24-maint, release25-maint, trunk and py3k.
msg233158 - (view) Author: Bertrand Janin (tamentis) * Date: 2014-12-29 02:29
This is still an issue as of OpenBSD 5.6. Here is an updated patch for the latest 2.7 branch:

diff -r 88de50c1696b Modules/socketmodule.c
--- a/Modules/socketmodule.c    Sun Dec 28 18:51:25 2014 +0200
+++ b/Modules/socketmodule.c    Sun Dec 28 21:24:41 2014 -0500
@@ -1881,24 +1881,29 @@
 {
     int level;
     int optname;
     int res;
     char *buf;
     int buflen;
     int flag;
 
     if (PyArg_ParseTuple(args, "iii:setsockopt",
                          &level, &optname, &flag)) {
         buf = (char *) &flag;
         buflen = sizeof flag;
+        /* Multi cast options take shorter arguments */
+        if (optname == IP_MULTICAST_TTL
+            || optname == IP_MULTICAST_LOOP)
+                buflen = sizeof(u_char);
+         buf = (char *) &flag;
     }
     else {
         PyErr_Clear();
         if (!PyArg_ParseTuple(args, "iis#:setsockopt",
                               &level, &optname, &buf, &buflen))
             return NULL;
     }
     res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen);
     if (res < 0)
         return s->errorhandler();
     Py_INCREF(Py_None);
     return Py_None;
History
Date User Action Args
2022-04-11 14:56:36adminsetgithub: 47622
2014-12-29 02:29:19tamentissetnosy: + tamentis

messages: + msg233158
versions: + Python 2.7, Python 3.4
2008-07-18 13:07:15gpolosetstatus: open -> closed
resolution: out of date
messages: + msg69953
nosy: + gpolo
2008-07-16 01:39:15niallosetmessages: + msg69743
versions: + Python 2.4
2008-07-16 01:38:52niallocreate