Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(2)

Delta Between Two Patch Sets: Modules/socketmodule.c

Issue 27744: Add AF_ALG (Linux Kernel crypto) to socket module
Left Patch Set: Created 3 years, 5 months ago
Right Patch Set: Created 3 years, 5 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« Lib/test/test_socket.py ('K') | « Lib/test/test_socket.py ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* Socket module */ 1 /* Socket module */
2 2
3 /* 3 /*
4 4
5 This module provides an interface to Berkeley socket IPC. 5 This module provides an interface to Berkeley socket IPC.
6 6
7 Limitations: 7 Limitations:
8 8
9 - Only AF_INET, AF_INET6 and AF_UNIX address families are supported in a 9 - Only AF_INET, AF_INET6 and AF_UNIX address families are supported in a
10 portable manner, though AF_PACKET, AF_NETLINK and AF_TIPC are supported 10 portable manner, though AF_PACKET, AF_NETLINK and AF_TIPC are supported
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 listen([n]) -- start listening for incoming connections\n\ 129 listen([n]) -- start listening for incoming connections\n\
130 recv(buflen[, flags]) -- receive data\n\ 130 recv(buflen[, flags]) -- receive data\n\
131 recv_into(buffer[, nbytes[, flags]]) -- receive data (into a buffer)\n\ 131 recv_into(buffer[, nbytes[, flags]]) -- receive data (into a buffer)\n\
132 recvfrom(buflen[, flags]) -- receive data and sender\'s address\n\ 132 recvfrom(buflen[, flags]) -- receive data and sender\'s address\n\
133 recvfrom_into(buffer[, nbytes, [, flags])\n\ 133 recvfrom_into(buffer[, nbytes, [, flags])\n\
134 -- receive data and sender\'s address (into a buffer)\n\ 134 -- receive data and sender\'s address (into a buffer)\n\
135 sendall(data[, flags]) -- send all data\n\ 135 sendall(data[, flags]) -- send all data\n\
136 send(data[, flags]) -- send data, may not send all of it\n\ 136 send(data[, flags]) -- send data, may not send all of it\n\
137 sendto(data[, flags], addr) -- send data to a given address\n\ 137 sendto(data[, flags], addr) -- send data to a given address\n\
138 setblocking(0 | 1) -- set or clear the blocking I/O flag\n\ 138 setblocking(0 | 1) -- set or clear the blocking I/O flag\n\
139 setsockopt(level, optname, value) -- set socket options\n\ 139 setsockopt(level, optname, value[, optlen]) -- set socket options\n\
140 settimeout(None | float) -- set or clear the timeout\n\ 140 settimeout(None | float) -- set or clear the timeout\n\
141 shutdown(how) -- shut down traffic in one or both directions\n\ 141 shutdown(how) -- shut down traffic in one or both directions\n\
142 if_nameindex() -- return all network interface indices and names\n\ 142 if_nameindex() -- return all network interface indices and names\n\
143 if_nametoindex(name) -- return the corresponding interface index\n\ 143 if_nametoindex(name) -- return the corresponding interface index\n\
144 if_indextoname(index) -- return the corresponding interface name\n\ 144 if_indextoname(index) -- return the corresponding interface name\n\
145 \n\ 145 \n\
146 [*] not available on all platforms!"); 146 [*] not available on all platforms!");
147 147
148 /* XXX This is a terrible mess of platform-dependent preprocessor hacks. 148 /* XXX This is a terrible mess of platform-dependent preprocessor hacks.
149 I hope some day someone can clean this up please... */ 149 I hope some day someone can clean this up please... */
(...skipping 1843 matching lines...) Expand 10 before | Expand all | Expand 10 after
1993 char *type; 1993 char *type;
1994 char *name; 1994 char *name;
1995 sa = (struct sockaddr_alg *)addr_ret; 1995 sa = (struct sockaddr_alg *)addr_ret;
1996 1996
1997 memset(sa, 0, sizeof(*sa)); 1997 memset(sa, 0, sizeof(*sa));
1998 sa->salg_family = AF_ALG; 1998 sa->salg_family = AF_ALG;
1999 1999
2000 if (!PyArg_ParseTuple(args, "ss|HH:getsockaddrarg", 2000 if (!PyArg_ParseTuple(args, "ss|HH:getsockaddrarg",
2001 &type, &name, &sa->salg_feat, &sa->salg_mask)) 2001 &type, &name, &sa->salg_feat, &sa->salg_mask))
2002 return 0; 2002 return 0;
2003 /* sockaddr_alg has fixed-sized char arrays for type and name */
2003 if (strlen(type) > sizeof(sa->salg_type)) { 2004 if (strlen(type) > sizeof(sa->salg_type)) {
2004 PyErr_SetString(PyExc_ValueError, "AF_ALG type too long."); 2005 PyErr_SetString(PyExc_ValueError, "AF_ALG type too long.");
2005 return 0; 2006 return 0;
2006 } 2007 }
2007 strncpy((char *)sa->salg_type, type, sizeof(sa->salg_type)); 2008 strncpy((char *)sa->salg_type, type, sizeof(sa->salg_type));
2008 if (strlen(name) > sizeof(sa->salg_name)) { 2009 if (strlen(name) > sizeof(sa->salg_name)) {
2009 PyErr_SetString(PyExc_ValueError, "AF_ALG name too long."); 2010 PyErr_SetString(PyExc_ValueError, "AF_ALG name too long.");
2010 return 0; 2011 return 0;
2011 } 2012 }
2012 strncpy((char *)sa->salg_name, name, sizeof(sa->salg_name)); 2013 strncpy((char *)sa->salg_name, name, sizeof(sa->salg_name));
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after
2296 #if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC) 2297 #if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC)
2297 /* accept4() is available on Linux 2.6.28+ and glibc 2.10 */ 2298 /* accept4() is available on Linux 2.6.28+ and glibc 2.10 */
2298 static int accept4_works = -1; 2299 static int accept4_works = -1;
2299 #endif 2300 #endif
2300 2301
2301 static int 2302 static int
2302 sock_accept_impl(PySocketSockObject *s, void *data) 2303 sock_accept_impl(PySocketSockObject *s, void *data)
2303 { 2304 {
2304 struct sock_accept *ctx = data; 2305 struct sock_accept *ctx = data;
2305 struct sockaddr *addr = SAS2SA(ctx->addrbuf); 2306 struct sockaddr *addr = SAS2SA(ctx->addrbuf);
2306 socklen_t *addrlen = ctx->addrlen; 2307 socklen_t *paddrlen = ctx->addrlen;
2307 #ifdef HAVE_SOCKADDR_ALG 2308 #ifdef HAVE_SOCKADDR_ALG
2308 /* AF_ALG does not support accept() with addr and raises 2309 /* AF_ALG does not support accept() with addr and raises
2309 * ECONNABORTED instead. */ 2310 * ECONNABORTED instead. */
2310 if (s->sock_family == AF_ALG) { 2311 if (s->sock_family == AF_ALG) {
2311 addr = NULL; 2312 addr = NULL;
2312 addrlen = NULL; 2313 paddrlen = NULL;
2313 *ctx->addrlen = 0; 2314 *ctx->addrlen = 0;
2314 } 2315 }
2315 #endif 2316 #endif
2316 2317
2317 #if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC) 2318 #if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC)
2318 if (accept4_works != 0) { 2319 if (accept4_works != 0) {
2319 ctx->result = accept4(s->sock_fd, addr, addrlen, 2320 ctx->result = accept4(s->sock_fd, addr, paddrlen,
2320 SOCK_CLOEXEC); 2321 SOCK_CLOEXEC);
2321 if (ctx->result == INVALID_SOCKET && accept4_works == -1) { 2322 if (ctx->result == INVALID_SOCKET && accept4_works == -1) {
2322 /* On Linux older than 2.6.28, accept4() fails with ENOSYS */ 2323 /* On Linux older than 2.6.28, accept4() fails with ENOSYS */
2323 accept4_works = (errno != ENOSYS); 2324 accept4_works = (errno != ENOSYS);
2324 } 2325 }
2325 } 2326 }
2326 if (accept4_works == 0) 2327 if (accept4_works == 0)
2327 ctx->result = accept(s->sock_fd, addr, addrlen); 2328 ctx->result = accept(s->sock_fd, addr, paddrlen);
2328 #else 2329 #else
2329 ctx->result = accept(s->sock_fd, addr, addrlen); 2330 ctx->result = accept(s->sock_fd, addr, paddrlen);
2330 #endif 2331 #endif
2331 2332
2332 #ifdef MS_WINDOWS 2333 #ifdef MS_WINDOWS
2333 return (ctx->result != INVALID_SOCKET); 2334 return (ctx->result != INVALID_SOCKET);
2334 #else 2335 #else
2335 return (ctx->result >= 0); 2336 return (ctx->result >= 0);
2336 #endif 2337 #endif
2337 } 2338 }
2338 2339
2339 /* s._accept() -> (fd, address) */ 2340 /* s._accept() -> (fd, address) */
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
2536 */ 2537 */
2537 2538
2538 static PyObject * 2539 static PyObject *
2539 sock_setsockopt(PySocketSockObject *s, PyObject *args) 2540 sock_setsockopt(PySocketSockObject *s, PyObject *args)
2540 { 2541 {
2541 int level; 2542 int level;
2542 int optname; 2543 int optname;
2543 int res; 2544 int res;
2544 Py_buffer optval; 2545 Py_buffer optval;
2545 int flag; 2546 int flag;
2546 socklen_t socklen; 2547 unsigned int optlen;
haypo 2016/09/01 23:10:30 Oh, thanks for fixing optlen type ;-)
2547 PyObject *none; 2548 PyObject *none;
2548 2549
2549 /* setsockopt(level, opt, flag) */ 2550 /* setsockopt(level, opt, flag) */
2550 if (PyArg_ParseTuple(args, "iii:setsockopt", 2551 if (PyArg_ParseTuple(args, "iii:setsockopt",
2551 &level, &optname, &flag)) { 2552 &level, &optname, &flag)) {
2552 res = setsockopt(s->sock_fd, level, optname, 2553 res = setsockopt(s->sock_fd, level, optname,
2553 (char*)&flag, sizeof flag); 2554 (char*)&flag, sizeof flag);
2554 goto done; 2555 goto done;
2555 } 2556 }
2556 2557
2557 PyErr_Clear(); 2558 PyErr_Clear();
2558 /* setsockopt(level, opt, (None, flag)) */ 2559 /* setsockopt(level, opt, (None, flag)) */
haypo 2016/09/01 23:10:30 The comment is still outdated, no?
2559 if (PyArg_ParseTuple(args, "iiO!I:setsockopt", 2560 if (PyArg_ParseTuple(args, "iiO!I:setsockopt",
2560 &level, &optname, Py_TYPE(Py_None), &none, &socklen)) { 2561 &level, &optname, Py_TYPE(Py_None), &none, &optlen)) {
2562 assert(sizeof(socklen_t) >= sizeof(unsigned int));
2561 res = setsockopt(s->sock_fd, level, optname, 2563 res = setsockopt(s->sock_fd, level, optname,
2562 NULL, socklen); 2564 NULL, (socklen_t)optlen);
2563 goto done; 2565 goto done;
2564 } 2566 }
2565 2567
2566 PyErr_Clear(); 2568 PyErr_Clear();
2567 /* setsockopt(level, opt, buffer) */ 2569 /* setsockopt(level, opt, buffer) */
2568 if (!PyArg_ParseTuple(args, "iiy*:setsockopt", 2570 if (!PyArg_ParseTuple(args, "iiy*:setsockopt",
2569 &level, &optname, &optval)) 2571 &level, &optname, &optval))
2570 return NULL; 2572 return NULL;
2571 2573
2572 #ifdef MS_WINDOWS 2574 #ifdef MS_WINDOWS
(...skipping 13 matching lines...) Expand all
2586 2588
2587 done: 2589 done:
2588 if (res < 0) { 2590 if (res < 0) {
2589 return s->errorhandler(); 2591 return s->errorhandler();
2590 } 2592 }
2591 2593
2592 Py_RETURN_NONE; 2594 Py_RETURN_NONE;
2593 } 2595 }
2594 2596
2595 PyDoc_STRVAR(setsockopt_doc, 2597 PyDoc_STRVAR(setsockopt_doc,
2596 "setsockopt(level, option, value[, intvalue])\n\ 2598 "setsockopt(level, option, value: int)\n\
haypo 2016/08/28 23:53:21 Hum, the documentation is unclear. I suggest to do
christian.heimes 2016/08/29 08:10:37 I don't know which platforms support this kind of
2599 setsockopt(level, option, value: buffer)\n\
2600 setsockopt(level, option, None, optlen: int)\n\
2597 \n\ 2601 \n\
2598 Set a socket option. See the Unix manual for level and option.\n\ 2602 Set a socket option. See the Unix manual for level and option.\n\
2599 The value argument can either be an integer, a string buffer or None\n\ 2603 The value argument can either be an integer, a string buffer, or \n\
2600 with intvalue."); 2604 None, optlen.");
2601 2605
2602 2606
2603 /* s.getsockopt() method. 2607 /* s.getsockopt() method.
2604 With two arguments, retrieves an integer option. 2608 With two arguments, retrieves an integer option.
2605 With a third integer argument, retrieves a string buffer of that size; 2609 With a third integer argument, retrieves a string buffer of that size;
2606 use optional built-in module 'struct' to decode the string. */ 2610 use optional built-in module 'struct' to decode the string. */
2607 2611
2608 static PyObject * 2612 static PyObject *
2609 sock_getsockopt(PySocketSockObject *s, PyObject *args) 2613 sock_getsockopt(PySocketSockObject *s, PyObject *args)
2610 { 2614 {
(...skipping 4311 matching lines...) Expand 10 before | Expand all | Expand 10 after
6922 PyModule_AddIntMacro(m, SO_PROTOCOL); 6926 PyModule_AddIntMacro(m, SO_PROTOCOL);
6923 #endif 6927 #endif
6924 6928
6925 /* Maximum number of connections for "listen" */ 6929 /* Maximum number of connections for "listen" */
6926 #ifdef SOMAXCONN 6930 #ifdef SOMAXCONN
6927 PyModule_AddIntMacro(m, SOMAXCONN); 6931 PyModule_AddIntMacro(m, SOMAXCONN);
6928 #else 6932 #else
6929 PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */ 6933 PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */
6930 #endif 6934 #endif
6931 6935
6932 /* Ancilliary message types */ 6936 /* Ancillary message types */
6933 #ifdef SCM_RIGHTS 6937 #ifdef SCM_RIGHTS
6934 PyModule_AddIntMacro(m, SCM_RIGHTS); 6938 PyModule_AddIntMacro(m, SCM_RIGHTS);
6935 #endif 6939 #endif
6936 #ifdef SCM_CREDENTIALS 6940 #ifdef SCM_CREDENTIALS
6937 PyModule_AddIntMacro(m, SCM_CREDENTIALS); 6941 PyModule_AddIntMacro(m, SCM_CREDENTIALS);
6938 #endif 6942 #endif
6939 #ifdef SCM_CREDS 6943 #ifdef SCM_CREDS
6940 PyModule_AddIntMacro(m, SCM_CREDS); 6944 PyModule_AddIntMacro(m, SCM_CREDS);
6941 #endif 6945 #endif
6942 6946
(...skipping 748 matching lines...) Expand 10 before | Expand all | Expand 10 after
7691 return NULL; 7695 return NULL;
7692 memcpy(&packed_addr, src, sizeof(packed_addr)); 7696 memcpy(&packed_addr, src, sizeof(packed_addr));
7693 return strncpy(dst, inet_ntoa(packed_addr), size); 7697 return strncpy(dst, inet_ntoa(packed_addr), size);
7694 } 7698 }
7695 /* Should set errno to EAFNOSUPPORT */ 7699 /* Should set errno to EAFNOSUPPORT */
7696 return NULL; 7700 return NULL;
7697 } 7701 }
7698 7702
7699 #endif 7703 #endif
7700 #endif 7704 #endif
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+