[New-bugs-announce] [issue18806] socketmodule: fix/improve setipaddr() numeric addresses handling

Charles-François Natali report at bugs.python.org
Thu Aug 22 07:44:15 CEST 2013


New submission from Charles-François Natali:

Currently, setipaddr() has this code to special-case numeric IPv4 addresses and avoid a name resolution:

"""
    if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 &&
        0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 &&
        0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) {
        struct sockaddr_in *sin;
        sin = (struct sockaddr_in *)addr_ret;
        sin->sin_addr.s_addr = htonl(
            ((long) d1 << 24) | ((long) d2 << 16) |
            ((long) d3 << 8) | ((long) d4 << 0));
        sin->sin_family = AF_INET;
#ifdef HAVE_SOCKADDR_SA_LEN
        sin->sin_len = sizeof(*sin);
#endif
        return 4;
    }
"""

- it's sub-optimal to hand-parse an IP address while we have inet_pton() and getaddrinfo()
- it doesn't work for IPv6 addresses
- it's also subject to integer overflow due to the scanf formatter

Wouldn't it be better getaddrinfo() with AI_NUMERICHOST instead?

----------
components: Library (Lib)
messages: 195859
nosy: loewis, neologix
priority: normal
severity: normal
status: open
title: socketmodule: fix/improve setipaddr() numeric addresses handling
type: enhancement
versions: Python 3.4

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue18806>
_______________________________________


More information about the New-bugs-announce mailing list