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

Skip to content

Commit c6a164b

Browse files
committed
Port inet_ntoa and inet_aton to Windows:
- fix unescaped newline in string literal - removed unused err variable - Windows doesn't have inet_aton; use inet_addr instead
1 parent e61e98d commit c6a164b

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

Modules/socketmodule.c

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1801,12 +1801,13 @@ Convert a 32-bit integer from host to network byte order.";
18011801
static char inet_aton_doc[] =
18021802
"inet_aton(string) -> packed 32-bit IP representation\n\
18031803
\n\
1804-
Convert an IP address in string format (123.45.67.89) to the 32-bit packed
1804+
Convert an IP address in string format (123.45.67.89) to the 32-bit packed\n\
18051805
binary format used in low-level network functions.";
18061806

18071807
static PyObject*
18081808
BUILD_FUNC_DEF_2(PySocket_inet_aton, PyObject *, self, PyObject *, args)
18091809
{
1810+
#ifndef MS_WINDOWS
18101811
char *ip_addr;
18111812
struct in_addr packed_addr;
18121813
int err;
@@ -1825,6 +1826,26 @@ BUILD_FUNC_DEF_2(PySocket_inet_aton, PyObject *, self, PyObject *, args)
18251826

18261827
return PyString_FromStringAndSize((char *) &packed_addr,
18271828
sizeof(packed_addr));
1829+
#else /* MS_WINDOWS */
1830+
/* Have to use inet_addr() instead */
1831+
char *ip_addr;
1832+
long packed_addr;
1833+
1834+
if (!PyArg_Parse(args, "s", &ip_addr)) {
1835+
return NULL;
1836+
}
1837+
1838+
packed_addr = inet_addr(ip_addr);
1839+
1840+
if (packed_addr == INADDR_NONE) { /* invalid address */
1841+
PyErr_SetString(PySocket_Error,
1842+
"illegal IP address string passed to inet_aton");
1843+
return NULL;
1844+
}
1845+
1846+
return PyString_FromStringAndSize((char *) &packed_addr,
1847+
sizeof(packed_addr));
1848+
#endif /* MS_WINDOWS */
18281849
}
18291850

18301851
static char inet_ntoa_doc[] =
@@ -1836,7 +1857,7 @@ static PyObject*
18361857
BUILD_FUNC_DEF_2(PySocket_inet_ntoa, PyObject *, self, PyObject *, args)
18371858
{
18381859
char *packed_str;
1839-
int err, addr_len;
1860+
int addr_len;
18401861
struct in_addr packed_addr;
18411862

18421863
if (!PyArg_Parse(args, "s#", &packed_str, &addr_len)) {

0 commit comments

Comments
 (0)