]> granicus.if.org Git - python/commitdiff
Issue #9975: socket: Fix incorrect use of flowinfo and scope_id. Patch by
authorCharles-François Natali <neologix@free.fr>
Mon, 2 Jan 2012 14:47:29 +0000 (15:47 +0100)
committerCharles-François Natali <neologix@free.fr>
Mon, 2 Jan 2012 14:47:29 +0000 (15:47 +0100)
Vilmos Nebehaj.

Lib/test/test_socket.py
Misc/ACKS
Misc/NEWS
Modules/socketmodule.c

index d41930163a36eb407defd123b27f4d659f0318c8..d77b7dc9ed648f8fd0134b520458c1a8745d0c13 100644 (file)
@@ -846,6 +846,13 @@ class GeneralModuleTests(unittest.TestCase):
         srv.listen(0)
         srv.close()
 
+    @unittest.skipUnless(SUPPORTS_IPV6, 'IPv6 required for this test.')
+    def test_flowinfo(self):
+        self.assertRaises(OverflowError, socket.getnameinfo,
+                          ('::1',0, 0xffffffff), 0)
+        with socket.socket(socket.AF_INET6, socket.SOCK_STREAM) as s:
+            self.assertRaises(OverflowError, s.bind, ('::1', 0, -10))
+
 
 @unittest.skipUnless(thread, 'Threading required for this test.')
 class BasicTCPTest(SocketConnectedTest):
index ef21b7f457911473a44718a5cea53a35b7791641..6ad3f8cbb0091fd259b14dbd63e9858214d83eb4 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -643,6 +643,7 @@ John Nagle
 Takahiro Nakayama
 Travers Naran
 Charles-François Natali
+Vilmos Nebehaj
 Fredrik Nehr
 Trent Nelson
 Tony Nelson
index 7e6ce40574da87527048ec395a72cec6b39475bf..da9e9de1b6da7496ab052311cf26e05c614eae97 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -345,6 +345,9 @@ Tests
 Extension Modules
 -----------------
 
+- Issue #9975: socket: Fix incorrect use of flowinfo and scope_id. Patch by
+  Vilmos Nebehaj.
+
 - Issue #13159: FileIO and BZ2File now use a linear-time buffer growth
   strategy instead of a quadratic-time one.
 
index d3e5c753155f503117858859575477817d43b08c..320227fa860939ca7c1e6d6720cf0a1dccc18802 100644 (file)
@@ -1065,10 +1065,10 @@ makesockaddr(SOCKET_T sockfd, struct sockaddr *addr, size_t addrlen, int proto)
         PyObject *ret = NULL;
         if (addrobj) {
             a = (struct sockaddr_in6 *)addr;
-            ret = Py_BuildValue("Oiii",
+            ret = Py_BuildValue("OiII",
                                 addrobj,
                                 ntohs(a->sin6_port),
-                                a->sin6_flowinfo,
+                                ntohl(a->sin6_flowinfo),
                                 a->sin6_scope_id);
             Py_DECREF(addrobj);
         }
@@ -1319,7 +1319,8 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args,
     {
         struct sockaddr_in6* addr;
         char *host;
-        int port, flowinfo, scope_id, result;
+        int port, result;
+        unsigned int flowinfo, scope_id;
         flowinfo = scope_id = 0;
         if (!PyTuple_Check(args)) {
             PyErr_Format(
@@ -1329,7 +1330,7 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args,
                 Py_TYPE(args)->tp_name);
             return 0;
         }
-        if (!PyArg_ParseTuple(args, "eti|ii",
+        if (!PyArg_ParseTuple(args, "eti|II",
                               "idna", &host, &port, &flowinfo,
                               &scope_id)) {
             return 0;
@@ -1346,9 +1347,15 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args,
                 "getsockaddrarg: port must be 0-65535.");
             return 0;
         }
+        if (flowinfo < 0 || flowinfo > 0xfffff) {
+            PyErr_SetString(
+                PyExc_OverflowError,
+                "getsockaddrarg: flowinfo must be 0-1048575.");
+            return 0;
+        }
         addr->sin6_family = s->sock_family;
         addr->sin6_port = htons((short)port);
-        addr->sin6_flowinfo = flowinfo;
+        addr->sin6_flowinfo = htonl(flowinfo);
         addr->sin6_scope_id = scope_id;
         *len_ret = sizeof *addr;
         return 1;
@@ -4104,7 +4111,8 @@ socket_getnameinfo(PyObject *self, PyObject *args)
     PyObject *sa = (PyObject *)NULL;
     int flags;
     char *hostp;
-    int port, flowinfo, scope_id;
+    int port;
+    unsigned int flowinfo, scope_id;
     char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
     struct addrinfo hints, *res = NULL;
     int error;
@@ -4118,9 +4126,14 @@ socket_getnameinfo(PyObject *self, PyObject *args)
                         "getnameinfo() argument 1 must be a tuple");
         return NULL;
     }
-    if (!PyArg_ParseTuple(sa, "si|ii",
+    if (!PyArg_ParseTuple(sa, "si|II",
                           &hostp, &port, &flowinfo, &scope_id))
         return NULL;
+    if (flowinfo < 0 || flowinfo > 0xfffff) {
+        PyErr_SetString(PyExc_OverflowError,
+                        "getsockaddrarg: flowinfo must be 0-1048575.");
+        return NULL;
+    }
     PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port);
     memset(&hints, 0, sizeof(hints));
     hints.ai_family = AF_UNSPEC;
@@ -4155,7 +4168,7 @@ socket_getnameinfo(PyObject *self, PyObject *args)
         {
         struct sockaddr_in6 *sin6;
         sin6 = (struct sockaddr_in6 *)res->ai_addr;
-        sin6->sin6_flowinfo = flowinfo;
+        sin6->sin6_flowinfo = htonl(flowinfo);
         sin6->sin6_scope_id = scope_id;
         break;
         }