[Python-Dev] More socket questions

Mihai Ibanescu misa@redhat.com
Thu, 10 Apr 2003 15:29:20 -0400 (EDT)


Since somebody mention inet_addr, here's something else that I can attempt 
to fix if we agree on it.

In python 2.2.2:

Traceback (most recent call last):
  File "<stdin>", line 1, in ?
socket.error: illegal IP address string passed to inet_aton


static PyObject*
PySocket_inet_aton(PyObject *self, PyObject *args)
#define INADDR_NONE (-1)

        /* Have to use inet_addr() instead */
        char *ip_addr;
        unsigned long packed_addr;

        if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr)) {
                return NULL;
#ifdef USE_GUSI1
        packed_addr = inet_addr(ip_addr).s_addr;
        packed_addr = inet_addr(ip_addr);

        if (packed_addr == INADDR_NONE) {       /* invalid address */
                        "illegal IP address string passed to inet_aton");

Reason for this behaviour can be found in the man page for inet_addr:

       The  inet_addr()  function  converts  the  Internet   host
       address cp from numbers-and-dots notation into binary data
       in  network  byte  order.   If  the  input   is   invalid,
       INADDR_NONE (usually -1) is returned.  This is an obsolete
       interface to inet_aton, described immediately above; it is
       obsolete  because -1 is a valid address (,
       and inet_aton provides a cleaner  way  to  indicate  error

I propose that we use inet_aton to implement PySocket_inet_aton (am I 
clever or what). The part that I don't know, how portable is this 
function? Does it exist on Mac and Windows?