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

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, 6 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 275 matching lines...) Expand 10 before | Expand all | Expand 10 after
286 #include <net/if.h> 286 #include <net/if.h>
287 #endif 287 #endif
288 288
289 #ifdef HAVE_SOCKADDR_ALG 289 #ifdef HAVE_SOCKADDR_ALG
290 #include <linux/if_alg.h> 290 #include <linux/if_alg.h>
291 #ifndef AF_ALG 291 #ifndef AF_ALG
292 #define AF_ALG 38 292 #define AF_ALG 38
293 #endif 293 #endif
294 #ifndef SOL_ALG 294 #ifndef SOL_ALG
295 #define SOL_ALG 279 295 #define SOL_ALG 279
296 #endif 296 #endif
haypo 2016/08/30 16:56:07 I don't understand why you need to define these co
christian.heimes 2016/08/30 17:21:51 It's fine to define missi g constants. For example
297 297
298 /* Linux 3.19 */ 298 /* Linux 3.19 */
299 #ifndef ALG_SET_AEAD_ASSOCLEN 299 #ifndef ALG_SET_AEAD_ASSOCLEN
300 #define ALG_SET_AEAD_ASSOCLEN 4 300 #define ALG_SET_AEAD_ASSOCLEN 4
301 #endif 301 #endif
302 #ifndef ALG_SET_AEAD_AUTHSIZE 302 #ifndef ALG_SET_AEAD_AUTHSIZE
303 #define ALG_SET_AEAD_AUTHSIZE 5 303 #define ALG_SET_AEAD_AUTHSIZE 5
304 #endif 304 #endif
305 /* Linux 4.8 */ 305 /* Linux 4.8 */
306 #ifndef ALG_SET_PUBKEY 306 #ifndef ALG_SET_PUBKEY
307 #define ALG_SET_PUBKEY 6 307 #define ALG_SET_PUBKEY 6
308 #endif 308 #endif
haypo 2016/08/30 16:56:07 Ditto. I would prefer to only use constants provid
christian.heimes 2016/08/30 17:21:51 AF_ALG is Linux-only. The values are already fixed
309 309
310 #ifndef ALG_OP_SIGN 310 #ifndef ALG_OP_SIGN
311 #define ALG_OP_SIGN 2 311 #define ALG_OP_SIGN 2
312 #endif 312 #endif
313 #ifndef ALG_OP_VERIFY 313 #ifndef ALG_OP_VERIFY
314 #define ALG_OP_VERIFY 3 314 #define ALG_OP_VERIFY 3
315 #endif 315 #endif
316 316
317 #endif /* HAVE_SOCKADDR_ALG */ 317 #endif /* HAVE_SOCKADDR_ALG */
318 318
(...skipping 1086 matching lines...) Expand 10 before | Expand all | Expand 10 after
1405 } 1405 }
1406 #endif 1406 #endif
1407 1407
1408 #ifdef HAVE_SOCKADDR_ALG 1408 #ifdef HAVE_SOCKADDR_ALG
1409 case AF_ALG: 1409 case AF_ALG:
1410 { 1410 {
1411 struct sockaddr_alg *a = (struct sockaddr_alg *)addr; 1411 struct sockaddr_alg *a = (struct sockaddr_alg *)addr;
1412 return Py_BuildValue("s#s#HH", 1412 return Py_BuildValue("s#s#HH",
1413 a->salg_type, 1413 a->salg_type,
1414 strnlen((const char*)a->salg_type, 1414 strnlen((const char*)a->salg_type,
1415 sizeof(a->salg_type)), 1415 sizeof(a->salg_type)),
haypo 2016/08/30 16:56:07 Oh, too bad, socketmodule.c still doesn't support
1416 a->salg_name, 1416 a->salg_name,
1417 strnlen((const char*)a->salg_name, 1417 strnlen((const char*)a->salg_name,
1418 sizeof(a->salg_name)), 1418 sizeof(a->salg_name)),
1419 a->salg_feat, 1419 a->salg_feat,
1420 a->salg_mask); 1420 a->salg_mask);
1421 } 1421 }
1422 #endif 1422 #endif
1423 1423
1424 /* More cases here... */ 1424 /* More cases here... */
1425 1425
(...skipping 567 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)) {
haypo 2016/08/30 16:56:07 Is it ok to not put a NUL byte at the end?
christian.heimes 2016/08/30 17:21:51 Yes, it's fine. The sockaddr_alg has fixed-size ch
haypo 2016/08/30 17:49:09 Can you please add a comment just saying that? It
christian.heimes 2016/09/04 16:58:44 That makes sense. I've added a comment.
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));
2013 2014
(...skipping 282 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;
haypo 2016/08/30 16:56:07 Hum, would it make sense to rename the variable to
christian.heimes 2016/08/30 17:21:51 Good idea, makes sense!
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/08/30 16:56:07 I suggest the name "optlen" to stay consistent wit
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/08/30 16:56:07 The comment is outdated, no? It's now: /* setsoc
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)) {
haypo 2016/08/30 16:56:07 Hum, I'm not confortable with passing a socklen_t*
christian.heimes 2016/08/30 17:21:51 socklen_t is an unsigned int. Let's keep it simple
haypo 2016/08/30 17:49:09 Please add at least the following assertion before
christian.heimes 2016/09/04 16:58:44 I have added the assertion assert(sizeof(socklen_t
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 1552 matching lines...) Expand 10 before | Expand all | Expand 10 after
4125 protocol level and protocol-specific type respectively, and cmsg_data\n\ 4127 protocol level and protocol-specific type respectively, and cmsg_data\n\
4126 is a bytes-like object holding the associated data. The flags\n\ 4128 is a bytes-like object holding the associated data. The flags\n\
4127 argument defaults to 0 and has the same meaning as for send(). If\n\ 4129 argument defaults to 0 and has the same meaning as for send(). If\n\
4128 address is supplied and not None, it sets a destination address for\n\ 4130 address is supplied and not None, it sets a destination address for\n\
4129 the message. The return value is the number of bytes of non-ancillary\n\ 4131 the message. The return value is the number of bytes of non-ancillary\n\
4130 data sent."); 4132 data sent.");
4131 #endif /* CMSG_LEN */ 4133 #endif /* CMSG_LEN */
4132 4134
4133 #ifdef HAVE_SOCKADDR_ALG 4135 #ifdef HAVE_SOCKADDR_ALG
4134 static PyObject* 4136 static PyObject*
4135 sock_sendmsg_afalg(PySocketSockObject *self, PyObject *args, PyObject *kwds) 4137 sock_sendmsg_afalg(PySocketSockObject *self, PyObject *args, PyObject *kwds)
haypo 2016/08/30 16:56:07 I suggest to rename the method: socket.sendmsg_alg
christian.heimes 2016/08/30 17:21:51 I prefer sendmsg_afalg(). It makes it clear that i
4136 { 4138 {
4137 PyObject *retval = NULL; 4139 PyObject *retval = NULL;
4138 4140
4139 Py_ssize_t i, ndatabufs = 0; 4141 Py_ssize_t i, ndatabufs = 0;
4140 Py_buffer *databufs = NULL; 4142 Py_buffer *databufs = NULL;
4141 PyObject *data_arg = NULL; 4143 PyObject *data_arg = NULL;
4142 4144
4143 Py_buffer iv = {NULL, NULL}; 4145 Py_buffer iv = {NULL, NULL};
4144 4146
4145 PyObject *opobj = NULL; 4147 PyObject *opobj = NULL;
(...skipping 21 matching lines...) Expand all
4167 4169
4168 if (!PyArg_ParseTupleAndKeywords(args, kwds, 4170 if (!PyArg_ParseTupleAndKeywords(args, kwds,
4169 "|O$O!y*O!i:sendmsg_afalg", keywords, 4171 "|O$O!y*O!i:sendmsg_afalg", keywords,
4170 &data_arg, 4172 &data_arg,
4171 &PyLong_Type, &opobj, &iv, 4173 &PyLong_Type, &opobj, &iv,
4172 &PyLong_Type, &assoclenobj, &flags)) 4174 &PyLong_Type, &assoclenobj, &flags))
4173 return NULL; 4175 return NULL;
4174 4176
4175 /* op is a required, keyword-only argument >= 0 */ 4177 /* op is a required, keyword-only argument >= 0 */
4176 if (opobj != NULL) { 4178 if (opobj != NULL) {
4177 op = _PyLong_AsInt(opobj); 4179 op = _PyLong_AsInt(opobj);
haypo 2016/08/30 16:56:07 You may check for integer overflow here as you did
christian.heimes 2016/08/30 17:21:51 The check on 4179 checks for overflow error. The e
4178 } 4180 }
4179 if (op < 0) { 4181 if (op < 0) {
4180 /* override exception from _PyLong_AsInt() */ 4182 /* override exception from _PyLong_AsInt() */
4181 PyErr_SetString(PyExc_TypeError, 4183 PyErr_SetString(PyExc_TypeError,
4182 "Invalid or missing argument 'op'"); 4184 "Invalid or missing argument 'op'");
4183 goto finally; 4185 goto finally;
4184 } 4186 }
4185 /* assoclen is optional but must be >= 0 */ 4187 /* assoclen is optional but must be >= 0 */
4186 if (assoclenobj != NULL) { 4188 if (assoclenobj != NULL) {
4187 assoclen = _PyLong_AsInt(assoclenobj); 4189 assoclen = _PyLong_AsInt(assoclenobj);
(...skipping 2736 matching lines...) Expand 10 before | Expand all | Expand 10 after
6924 PyModule_AddIntMacro(m, SO_PROTOCOL); 6926 PyModule_AddIntMacro(m, SO_PROTOCOL);
6925 #endif 6927 #endif
6926 6928
6927 /* Maximum number of connections for "listen" */ 6929 /* Maximum number of connections for "listen" */
6928 #ifdef SOMAXCONN 6930 #ifdef SOMAXCONN
6929 PyModule_AddIntMacro(m, SOMAXCONN); 6931 PyModule_AddIntMacro(m, SOMAXCONN);
6930 #else 6932 #else
6931 PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */ 6933 PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */
6932 #endif 6934 #endif
6933 6935
6934 /* Ancilliary message types */ 6936 /* Ancillary message types */
6935 #ifdef SCM_RIGHTS 6937 #ifdef SCM_RIGHTS
6936 PyModule_AddIntMacro(m, SCM_RIGHTS); 6938 PyModule_AddIntMacro(m, SCM_RIGHTS);
6937 #endif 6939 #endif
6938 #ifdef SCM_CREDENTIALS 6940 #ifdef SCM_CREDENTIALS
6939 PyModule_AddIntMacro(m, SCM_CREDENTIALS); 6941 PyModule_AddIntMacro(m, SCM_CREDENTIALS);
6940 #endif 6942 #endif
6941 #ifdef SCM_CREDS 6943 #ifdef SCM_CREDS
6942 PyModule_AddIntMacro(m, SCM_CREDS); 6944 PyModule_AddIntMacro(m, SCM_CREDS);
6943 #endif 6945 #endif
6944 6946
(...skipping 748 matching lines...) Expand 10 before | Expand all | Expand 10 after
7693 return NULL; 7695 return NULL;
7694 memcpy(&packed_addr, src, sizeof(packed_addr)); 7696 memcpy(&packed_addr, src, sizeof(packed_addr));
7695 return strncpy(dst, inet_ntoa(packed_addr), size); 7697 return strncpy(dst, inet_ntoa(packed_addr), size);
7696 } 7698 }
7697 /* Should set errno to EAFNOSUPPORT */ 7699 /* Should set errno to EAFNOSUPPORT */
7698 return NULL; 7700 return NULL;
7699 } 7701 }
7700 7702
7701 #endif 7703 #endif
7702 #endif 7704 #endif
LEFTRIGHT

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