qmsk/net/libc.pyx
author Tero Marttila <terom@fixme.fi>
Sun, 16 Aug 2009 19:20:55 +0300
changeset 8 b3880dafbab1
parent 7 74fde84264b1
child 10 94b0d5a208c1
permissions -rw-r--r--
phew, things are working
7
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
from libc cimport *
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
from py cimport raise_errno
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
cdef object inet_ntop (int af, void *sockaddr) :
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
    """
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     6
        Wrapper around inet_ntop, returning a PyString
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
    """
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
    # XXX: longest possible address...
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
    cdef char buf[INET6_ADDRSTRLEN]
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
    if c_inet_ntop(af, sockaddr, buf, sizeof(buf)) == NULL :
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    13
        raise_errno('inet_ntop')
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    14
    
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
    # autoconvert -> str
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
    return buf
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    17
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
cdef object inet_pton (int af, char *addr, void *sockaddr_out) :
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
    cdef int ret
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    21
    ret = c_inet_pton(af, addr, sockaddr_out)
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    22
    
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    23
    if ret < 0 :
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    24
        raise_errno('inet_pton')
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    25
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    26
    elif ret == 0 :
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    27
        raise NameError("Invalid network address for specified address family: %r" % (addr, ))
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    28
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    29
cdef object getnameinfo (sockaddr *sa, socklen_t salen, int flags) :
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    30
    cdef char hostbuf[NI_MAXHOST]
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    31
    cdef char servbuf[NI_MAXSERV]
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    32
    cdef int err
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    33
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    34
    err = c_getnameinfo(sa, salen, hostbuf, sizeof(hostbuf), servbuf, sizeof(servbuf), flags)
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    35
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    36
    if err :
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    37
        # XXX: raise a GAIError
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    38
        raise Exception(gai_strerror(err))
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    39
#        raise GAIError('getnameinfo', err)
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    40
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    41
    else :
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    42
        return hostbuf, servbuf
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    43
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    44
#cdef class Errno (py.OSError) :
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    45
#    def __init__ (self, func) :
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    46
#        self.func = func
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    47
#        self.err = errno
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    48
#        self.strerror = strerror(errno)
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    49
#
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    50
#    def __str__ (self) :
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    51
#        return "%s: %s" % (self.func, self.strerror)
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    52
#
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    53
#cdef class GAIError (py.OSError) :
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    54
#    def __init__ (self, func, err) :
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    55
#        self.func = func
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    56
#        self.err = err
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    57
#        self.strerror = gai_strerror(err)
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    58
#
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    59
#    def __str__ (self) :
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    60
#        return "%s: %s" % (self.func, self.strerror)
74fde84264b1 broke Cython with this package magic
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    61
#