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

Delta Between Two Patch Sets: Modules/socketmodule.c

Issue 22043: Use a monotonic clock to compute timeouts
Left Patch Set: Created 3 years, 3 months ago
Right Patch Set: Created 3 years, 2 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
« no previous file with change/comment | « Modules/gcmodule.c ('k') | Modules/timemodule.c » ('j') | 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 658 matching lines...) Expand 10 before | Expand all | Expand 10 after
669 Py_END_ALLOW_THREADS 669 Py_END_ALLOW_THREADS
670 if (timeout == 1) { 670 if (timeout == 1) {
671 PyErr_SetString(socket_timeout, "timed out"); 671 PyErr_SetString(socket_timeout, "timed out");
672 return -1; 672 return -1;
673 } 673 }
674 END_SELECT_LOOP(s) 674 END_SELECT_LOOP(s)
675 */ 675 */
676 676
677 #define BEGIN_SELECT_LOOP(s) \ 677 #define BEGIN_SELECT_LOOP(s) \
678 { \ 678 { \
679 _PyTimeSpec now, deadline = {0, 0}; \ 679 _PyTime_timeval now, deadline = {0, 0}; \
680 double interval = s->sock_timeout; \ 680 double interval = s->sock_timeout; \
681 int has_timeout = s->sock_timeout > 0.0; \ 681 int has_timeout = s->sock_timeout > 0.0; \
682 if (has_timeout) { \ 682 if (has_timeout) { \
683 _PyTimeSpec_get_time(&now); \ 683 _PyTime_monotonic(&now); \
684 deadline = now; \ 684 deadline = now; \
685 _PyTimeSpec_add_sec(&deadline, s->sock_timeout); \ 685 _PyTime_ADD_SECONDS(deadline, s->sock_timeout); \
686 } \ 686 } \
687 while (1) { \ 687 while (1) { \
688 errno = 0; \ 688 errno = 0; \
689 689
690 #define END_SELECT_LOOP(s) \ 690 #define END_SELECT_LOOP(s) \
691 if (!has_timeout || \ 691 if (!has_timeout || \
692 (!CHECK_ERRNO(EWOULDBLOCK) && !CHECK_ERRNO(EAGAIN))) \ 692 (!CHECK_ERRNO(EWOULDBLOCK) && !CHECK_ERRNO(EAGAIN))) \
693 break; \ 693 break; \
694 _PyTimeSpec_get_time(&now); \ 694 _PyTime_monotonic(&now); \
695 interval = _PyTimeSpec_interval_sec(&now, &deadline); \ 695 interval = _PyTime_INTERVAL(now, deadline); \
696 } \ 696 } \
697 } \ 697 } \
698 698
699 /* Initialize a new socket object. */ 699 /* Initialize a new socket object. */
700 700
701 static double defaulttimeout = -1.0; /* Default timeout for new sockets */ 701 static double defaulttimeout = -1.0; /* Default timeout for new sockets */
702 702
703 static void 703 static void
704 init_sockobject(PySocketSockObject *s, 704 init_sockobject(PySocketSockObject *s,
705 SOCKET_T fd, int family, int type, int proto) 705 SOCKET_T fd, int family, int type, int proto)
(...skipping 500 matching lines...) Expand 10 before | Expand all | Expand 10 after
1206 /* If we don't know the address family, don't raise an 1206 /* If we don't know the address family, don't raise an
1207 exception -- return it as an (int, bytes) tuple. */ 1207 exception -- return it as an (int, bytes) tuple. */
1208 return Py_BuildValue("iy#", 1208 return Py_BuildValue("iy#",
1209 addr->sa_family, 1209 addr->sa_family,
1210 addr->sa_data, 1210 addr->sa_data,
1211 sizeof(addr->sa_data)); 1211 sizeof(addr->sa_data));
1212 1212
1213 } 1213 }
1214 } 1214 }
1215 1215
1216 /* Helper for getsockaddrarg: bypass IDNA for ASCII-only host names
1217 (in particular, numeric IP addresses). */
1218 struct maybe_idna {
1219 PyObject *obj;
1220 char *buf;
1221 };
1222
1223 static void
1224 idna_cleanup(struct maybe_idna *data)
1225 {
1226 Py_CLEAR(data->obj);
1227 }
1228
1229 static int
1230 idna_converter(PyObject *obj, struct maybe_idna *data)
1231 {
1232 size_t len;
1233 PyObject *obj2, *obj3;
1234 if (obj == NULL) {
1235 idna_cleanup(data);
1236 return 1;
1237 }
1238 data->obj = NULL;
1239 len = -1;
1240 if (PyBytes_Check(obj)) {
1241 data->buf = PyBytes_AsString(obj);
1242 len = PyBytes_Size(obj);
1243 }
1244 else if (PyByteArray_Check(obj)) {
1245 data->buf = PyByteArray_AsString(obj);
1246 len = PyByteArray_Size(obj);
1247 }
1248 else if (PyUnicode_Check(obj) && PyUnicode_READY(obj) == 0 && PyUnicode_IS_C OMPACT_ASCII(obj)) {
1249 data->buf = PyUnicode_DATA(obj);
1250 len = PyUnicode_GET_LENGTH(obj);
1251 }
1252 else {
1253 obj2 = PyUnicode_FromObject(obj);
1254 if (!obj2) {
1255 PyErr_Format(PyExc_TypeError, "string or unicode text buffer expecte d, not %s",
1256 obj->ob_type->tp_name);
1257 return 0;
1258 }
1259 obj3 = PyUnicode_AsEncodedString(obj2, "idna", NULL);
1260 Py_DECREF(obj2);
1261 if (!obj3) {
1262 PyErr_SetString(PyExc_TypeError, "encoding of hostname failed");
1263 return 0;
1264 }
1265 if (!PyBytes_Check(obj3)) {
1266 Py_DECREF(obj3);
1267 PyErr_SetString(PyExc_TypeError, "encoding of hostname failed to ret urn bytes");
1268 return 0;
1269 }
1270 data->obj = obj3;
1271 data->buf = PyBytes_AS_STRING(obj3);
1272 len = PyBytes_GET_SIZE(obj3);
1273 }
1274 if (strlen(data->buf) != len) {
1275 Py_CLEAR(data->obj);
1276 PyErr_SetString(PyExc_TypeError, "host name must not contain NUL charact er");
1277 return 0;
1278 }
1279 return Py_CLEANUP_SUPPORTED;
1280 }
1216 1281
1217 /* Parse a socket address argument according to the socket object's 1282 /* Parse a socket address argument according to the socket object's
1218 address family. Return 1 if the address was in the proper format, 1283 address family. Return 1 if the address was in the proper format,
1219 0 of not. The address is returned through addr_ret, its length 1284 0 of not. The address is returned through addr_ret, its length
1220 through len_ret. */ 1285 through len_ret. */
1221 1286
1222 static int 1287 static int
1223 getsockaddrarg(PySocketSockObject *s, PyObject *args, 1288 getsockaddrarg(PySocketSockObject *s, PyObject *args,
1224 struct sockaddr *addr_ret, int *len_ret) 1289 struct sockaddr *addr_ret, int *len_ret)
1225 { 1290 {
(...skipping 10 matching lines...) Expand all
1236 /* PEP 383. Not using PyUnicode_FSConverter since we need to 1301 /* PEP 383. Not using PyUnicode_FSConverter since we need to
1237 allow embedded nulls on Linux. */ 1302 allow embedded nulls on Linux. */
1238 if (PyUnicode_Check(args)) { 1303 if (PyUnicode_Check(args)) {
1239 if ((args = PyUnicode_EncodeFSDefault(args)) == NULL) 1304 if ((args = PyUnicode_EncodeFSDefault(args)) == NULL)
1240 return 0; 1305 return 0;
1241 } 1306 }
1242 else 1307 else
1243 Py_INCREF(args); 1308 Py_INCREF(args);
1244 if (!PyArg_Parse(args, "y#", &path, &len)) 1309 if (!PyArg_Parse(args, "y#", &path, &len))
1245 goto unix_out; 1310 goto unix_out;
1311 assert(len >= 0);
1246 1312
1247 addr = (struct sockaddr_un*)addr_ret; 1313 addr = (struct sockaddr_un*)addr_ret;
1248 #ifdef linux 1314 #ifdef linux
1249 if (len > 0 && path[0] == 0) { 1315 if (len > 0 && path[0] == 0) {
1250 /* Linux abstract namespace extension */ 1316 /* Linux abstract namespace extension */
1251 if (len > sizeof addr->sun_path) { 1317 if ((size_t)len > sizeof addr->sun_path) {
1252 PyErr_SetString(PyExc_OSError, 1318 PyErr_SetString(PyExc_OSError,
1253 "AF_UNIX path too long"); 1319 "AF_UNIX path too long");
1254 goto unix_out; 1320 goto unix_out;
1255 } 1321 }
1256 } 1322 }
1257 else 1323 else
1258 #endif /* linux */ 1324 #endif /* linux */
1259 { 1325 {
1260 /* regular NULL-terminated string */ 1326 /* regular NULL-terminated string */
1261 if (len >= sizeof addr->sun_path) { 1327 if ((size_t)len >= sizeof addr->sun_path) {
1262 PyErr_SetString(PyExc_OSError, 1328 PyErr_SetString(PyExc_OSError,
1263 "AF_UNIX path too long"); 1329 "AF_UNIX path too long");
1264 goto unix_out; 1330 goto unix_out;
1265 } 1331 }
1266 addr->sun_path[len] = 0; 1332 addr->sun_path[len] = 0;
1267 } 1333 }
1268 addr->sun_family = s->sock_family; 1334 addr->sun_family = s->sock_family;
1269 memcpy(addr->sun_path, path, len); 1335 memcpy(addr->sun_path, path, len);
1270 *len_ret = len + offsetof(struct sockaddr_un, sun_path); 1336 *len_ret = len + offsetof(struct sockaddr_un, sun_path);
1271 retval = 1; 1337 retval = 1;
(...skipping 28 matching lines...) Expand all
1300 #endif 1366 #endif
1301 1367
1302 #ifdef AF_RDS 1368 #ifdef AF_RDS
1303 case AF_RDS: 1369 case AF_RDS:
1304 /* RDS sockets use sockaddr_in: fall-through */ 1370 /* RDS sockets use sockaddr_in: fall-through */
1305 #endif 1371 #endif
1306 1372
1307 case AF_INET: 1373 case AF_INET:
1308 { 1374 {
1309 struct sockaddr_in* addr; 1375 struct sockaddr_in* addr;
1310 char *host; 1376 struct maybe_idna host = {NULL, NULL};
1311 int port, result; 1377 int port, result;
1312 if (!PyTuple_Check(args)) { 1378 if (!PyTuple_Check(args)) {
1313 PyErr_Format( 1379 PyErr_Format(
1314 PyExc_TypeError, 1380 PyExc_TypeError,
1315 "getsockaddrarg: " 1381 "getsockaddrarg: "
1316 "AF_INET address must be tuple, not %.500s", 1382 "AF_INET address must be tuple, not %.500s",
1317 Py_TYPE(args)->tp_name); 1383 Py_TYPE(args)->tp_name);
1318 return 0; 1384 return 0;
1319 } 1385 }
1320 if (!PyArg_ParseTuple(args, "eti:getsockaddrarg", 1386 if (!PyArg_ParseTuple(args, "O&i:getsockaddrarg",
1321 "idna", &host, &port)) 1387 idna_converter, &host, &port))
1322 return 0; 1388 return 0;
1323 addr=(struct sockaddr_in*)addr_ret; 1389 addr=(struct sockaddr_in*)addr_ret;
1324 result = setipaddr(host, (struct sockaddr *)addr, 1390 result = setipaddr(host.buf, (struct sockaddr *)addr,
1325 sizeof(*addr), AF_INET); 1391 sizeof(*addr), AF_INET);
1326 PyMem_Free(host); 1392 idna_cleanup(&host);
1327 if (result < 0) 1393 if (result < 0)
1328 return 0; 1394 return 0;
1329 if (port < 0 || port > 0xffff) { 1395 if (port < 0 || port > 0xffff) {
1330 PyErr_SetString( 1396 PyErr_SetString(
1331 PyExc_OverflowError, 1397 PyExc_OverflowError,
1332 "getsockaddrarg: port must be 0-65535."); 1398 "getsockaddrarg: port must be 0-65535.");
1333 return 0; 1399 return 0;
1334 } 1400 }
1335 addr->sin_family = AF_INET; 1401 addr->sin_family = AF_INET;
1336 addr->sin_port = htons((short)port); 1402 addr->sin_port = htons((short)port);
1337 *len_ret = sizeof *addr; 1403 *len_ret = sizeof *addr;
1338 return 1; 1404 return 1;
1339 } 1405 }
1340 1406
1341 #ifdef ENABLE_IPV6 1407 #ifdef ENABLE_IPV6
1342 case AF_INET6: 1408 case AF_INET6:
1343 { 1409 {
1344 struct sockaddr_in6* addr; 1410 struct sockaddr_in6* addr;
1345 char *host; 1411 struct maybe_idna host = {NULL, NULL};
1346 int port, result; 1412 int port, result;
1347 unsigned int flowinfo, scope_id; 1413 unsigned int flowinfo, scope_id;
1348 flowinfo = scope_id = 0; 1414 flowinfo = scope_id = 0;
1349 if (!PyTuple_Check(args)) { 1415 if (!PyTuple_Check(args)) {
1350 PyErr_Format( 1416 PyErr_Format(
1351 PyExc_TypeError, 1417 PyExc_TypeError,
1352 "getsockaddrarg: " 1418 "getsockaddrarg: "
1353 "AF_INET6 address must be tuple, not %.500s", 1419 "AF_INET6 address must be tuple, not %.500s",
1354 Py_TYPE(args)->tp_name); 1420 Py_TYPE(args)->tp_name);
1355 return 0; 1421 return 0;
1356 } 1422 }
1357 if (!PyArg_ParseTuple(args, "eti|II", 1423 if (!PyArg_ParseTuple(args, "O&i|II",
1358 "idna", &host, &port, &flowinfo, 1424 idna_converter, &host, &port, &flowinfo,
1359 &scope_id)) { 1425 &scope_id)) {
1360 return 0; 1426 return 0;
1361 } 1427 }
1362 addr = (struct sockaddr_in6*)addr_ret; 1428 addr = (struct sockaddr_in6*)addr_ret;
1363 result = setipaddr(host, (struct sockaddr *)addr, 1429 result = setipaddr(host.buf, (struct sockaddr *)addr,
1364 sizeof(*addr), AF_INET6); 1430 sizeof(*addr), AF_INET6);
1365 PyMem_Free(host); 1431 idna_cleanup(&host);
1366 if (result < 0) 1432 if (result < 0)
1367 return 0; 1433 return 0;
1368 if (port < 0 || port > 0xffff) { 1434 if (port < 0 || port > 0xffff) {
1369 PyErr_SetString( 1435 PyErr_SetString(
1370 PyExc_OverflowError, 1436 PyExc_OverflowError,
1371 "getsockaddrarg: port must be 0-65535."); 1437 "getsockaddrarg: port must be 0-65535.");
1372 return 0; 1438 return 0;
1373 } 1439 }
1374 if (flowinfo > 0xfffff) { 1440 if (flowinfo > 0xfffff) {
1375 PyErr_SetString( 1441 PyErr_SetString(
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after
1603 addr = (struct sockaddr_can *)addr_ret; 1669 addr = (struct sockaddr_can *)addr_ret;
1604 1670
1605 if (!PyArg_ParseTuple(args, "O&", PyUnicode_FSConverter, 1671 if (!PyArg_ParseTuple(args, "O&", PyUnicode_FSConverter,
1606 &interfaceName)) 1672 &interfaceName))
1607 return 0; 1673 return 0;
1608 1674
1609 len = PyBytes_GET_SIZE(interfaceName); 1675 len = PyBytes_GET_SIZE(interfaceName);
1610 1676
1611 if (len == 0) { 1677 if (len == 0) {
1612 ifr.ifr_ifindex = 0; 1678 ifr.ifr_ifindex = 0;
1613 } else if (len < sizeof(ifr.ifr_name)) { 1679 } else if ((size_t)len < sizeof(ifr.ifr_name)) {
1614 strncpy(ifr.ifr_name, PyBytes_AS_STRING(interfaceName), sizeof(i fr.ifr_name)); 1680 strncpy(ifr.ifr_name, PyBytes_AS_STRING(interfaceName), sizeof(i fr.ifr_name));
1615 ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0'; 1681 ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0';
1616 if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) { 1682 if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) {
1617 s->errorhandler(); 1683 s->errorhandler();
1618 Py_DECREF(interfaceName); 1684 Py_DECREF(interfaceName);
1619 return 0; 1685 return 0;
1620 } 1686 }
1621 } else { 1687 } else {
1622 PyErr_SetString(PyExc_OSError, 1688 PyErr_SetString(PyExc_OSError,
1623 "AF_CAN interface name too long"); 1689 "AF_CAN interface name too long");
(...skipping 2594 matching lines...) Expand 10 before | Expand all | Expand 10 after
4218 4284
4219 PyDoc_STRVAR(gethostbyname_doc, 4285 PyDoc_STRVAR(gethostbyname_doc,
4220 "gethostbyname(host) -> address\n\ 4286 "gethostbyname(host) -> address\n\
4221 \n\ 4287 \n\
4222 Return the IP address (a string of the form '255.255.255.255') for a host."); 4288 Return the IP address (a string of the form '255.255.255.255') for a host.");
4223 4289
4224 4290
4225 /* Convenience function common to gethostbyname_ex and gethostbyaddr */ 4291 /* Convenience function common to gethostbyname_ex and gethostbyaddr */
4226 4292
4227 static PyObject * 4293 static PyObject *
4228 gethost_common(struct hostent *h, struct sockaddr *addr, int alen, int af) 4294 gethost_common(struct hostent *h, struct sockaddr *addr, size_t alen, int af)
4229 { 4295 {
4230 char **pch; 4296 char **pch;
4231 PyObject *rtn_tuple = (PyObject *)NULL; 4297 PyObject *rtn_tuple = (PyObject *)NULL;
4232 PyObject *name_list = (PyObject *)NULL; 4298 PyObject *name_list = (PyObject *)NULL;
4233 PyObject *addr_list = (PyObject *)NULL; 4299 PyObject *addr_list = (PyObject *)NULL;
4234 PyObject *tmp; 4300 PyObject *tmp;
4235 4301
4236 if (h == NULL) { 4302 if (h == NULL) {
4237 /* Let's get real error message to return */ 4303 /* Let's get real error message to return */
4238 set_herror(h_errno); 4304 set_herror(h_errno);
(...skipping 2699 matching lines...) Expand 10 before | Expand all | Expand 10 after
6938 return NULL; 7004 return NULL;
6939 memcpy(&packed_addr, src, sizeof(packed_addr)); 7005 memcpy(&packed_addr, src, sizeof(packed_addr));
6940 return strncpy(dst, inet_ntoa(packed_addr), size); 7006 return strncpy(dst, inet_ntoa(packed_addr), size);
6941 } 7007 }
6942 /* Should set errno to EAFNOSUPPORT */ 7008 /* Should set errno to EAFNOSUPPORT */
6943 return NULL; 7009 return NULL;
6944 } 7010 }
6945 7011
6946 #endif 7012 #endif
6947 #endif 7013 #endif
LEFTRIGHT

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