qmsk/net/socket/_address.pyx
author Tero Marttila <terom@fixme.fi>
Fri, 25 Sep 2009 21:34:04 +0300
changeset 49 e2f79e68418a
permissions -rw-r--r--
fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
49
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
cdef class sockaddr :
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
    cdef void _init_family (self, platform.sa_family_t family=platform.AF_UNSPEC) :
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
        self.family = family
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     6
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
    # XXX:use size_t
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
    cdef int _get_sockaddr (self, platform.sockaddr **sa_ptr, platform.socklen_t *sa_len) except -1 :
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
        """
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
            Get the sockaddr pointer and sockaddr length for this address
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
        """ 
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    13
        raise NotImplementedError()
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    14
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
    cdef platform.sockaddr* _get_sockaddr_ptr (self) except NULL :
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
        """
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    17
            Get the sockaddr pointer
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
        """
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
        cdef platform.sockaddr *sa
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    21
        cdef platform.socklen_t sa_len
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    22
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    23
        self._get_sockaddr(&sa, &sa_len)
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    24
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    25
        return sa
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    26
    
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    27
    cdef platform.socklen_t _get_sockaddr_len (self) except -1 :
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    28
        """
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    29
            Get the sockaddr len
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    30
        """
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    31
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    32
        cdef platform.sockaddr *sa
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    33
        cdef platform.socklen_t sa_len
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    34
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    35
        self._get_sockaddr(&sa, &sa_len)
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    36
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    37
        return sa_len
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    38
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    39
    cdef int _set_sockaddr (self, platform.sockaddr *sa, size_t sa_len) except -1 :
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    40
        """
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    41
            Set the sockaddr value for this address; sa_len must match!
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    42
        """
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    43
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    44
        raise NotImplementedError()
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    45
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    46
    def getnameinfo (self) :
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    47
        """
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    48
            Returns a (host, serv) tuple for this address à la getnameinfo
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    49
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    50
            >>> addr = sockaddr_in()
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    51
            >>> assert addr.getnameinfo() == (addr.addr, str(addr.port))
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    52
        """
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    53
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    54
        cdef platform.sockaddr *sa
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    55
        cdef platform.socklen_t sa_len
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    56
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    57
        # XXX: take as args?
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    58
        cdef int flags = platform.NI_NUMERICHOST | platform.NI_NUMERICSERV
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    59
        
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    60
        # get our abstract sockaddr
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    61
        self._get_sockaddr(&sa, &sa_len)
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    62
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    63
        # get nice text format
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    64
        return platform.getnameinfo(sa, sa_len, flags)
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    65
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    66
    property addr :
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    67
        """
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    68
            The ASCII literal network address
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    69
        """
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    70
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    71
        def __get__ (self) :
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    72
            """
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    73
                Default implmentation using getnameinfo()
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    74
            """
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    75
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    76
            addr, port = self.getnameinfo()
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    77
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    78
            return addr
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    79
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    80
    property port :
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    81
        """
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    82
            The integer port number
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    83
        """
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    84
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    85
        def __get__ (self) :
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    86
            """
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    87
                Default implementation using getnameinfo() and int()
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    88
            """
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    89
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    90
            addr, port = self.getnameinfo()
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    91
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    92
            return int(port)
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    93
    
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    94
    def __repr__ (self) :
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    95
        return "sockaddr(%d, %s, %d)" % (self.family, self.addr, self.port)
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    96
e2f79e68418a fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    97