Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0e85f9d

Browse files
committed
Patch 731209: Restore socketmodule's behaviour with dotted quad addresses
to that of Python2.1. Such nnn.nnn.nnn.nnn addresses are just used directly, not passed to the resolver for a pointless lookup.
1 parent 6dc4a8e commit 0e85f9d

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

Modules/socketmodule.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,8 @@ setipaddr(char *name, struct sockaddr *addr_ret, size_t addr_ret_size, int af)
626626
{
627627
struct addrinfo hints, *res;
628628
int error;
629+
int d1, d2, d3, d4;
630+
char ch;
629631

630632
memset((void *) addr_ret, '\0', sizeof(*addr_ret));
631633
if (name[0] == '\0') {
@@ -682,6 +684,20 @@ setipaddr(char *name, struct sockaddr *addr_ret, size_t addr_ret_size, int af)
682684
sin->sin_addr.s_addr = INADDR_BROADCAST;
683685
return sizeof(sin->sin_addr);
684686
}
687+
if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 &&
688+
0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 &&
689+
0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) {
690+
struct sockaddr_in *sin;
691+
sin = (struct sockaddr_in *)addr_ret;
692+
sin->sin_addr.s_addr = htonl(
693+
((long) d1 << 24) | ((long) d2 << 16) |
694+
((long) d3 << 8) | ((long) d4 << 0));
695+
sin->sin_family = AF_INET;
696+
#ifdef HAVE_SOCKADDR_SA_LEN
697+
sin->sin_len = sizeof(*sin);
698+
#endif
699+
return 4;
700+
}
685701
memset(&hints, 0, sizeof(hints));
686702
hints.ai_family = af;
687703
error = getaddrinfo(name, NULL, &hints, &res);

0 commit comments

Comments
 (0)