[Python-Dev] More socket questions

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


Hello,

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:

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

Implementation:

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

        /* 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;
#else
        packed_addr = inet_addr(ip_addr);
#endif

        if (packed_addr == INADDR_NONE) {       /* invalid address */
                PyErr_SetString(PySocket_Error,
                        "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 (255.255.255.255),
       and inet_aton provides a cleaner  way  to  indicate  error
       return.


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?

Thanks,
Misa