[New-bugs-announce] [issue3372] socket.setsockopt() is broken for multicast TTL and Loop options

Niall O'Higgins report at bugs.python.org
Wed Jul 16 03:38:52 CEST 2008


New submission from Niall O'Higgins <niallo at unworkable.org>:

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();

----------
components: Library (Lib)
messages: 69741
nosy: niallo
severity: normal
status: open
title: socket.setsockopt() is broken for multicast TTL and Loop options
type: behavior
versions: Python 2.5

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue3372>
_______________________________________


More information about the New-bugs-announce mailing list