qmsk/net/transport/endpoint.py
changeset 38 f0fc793a3754
parent 37 14db3fe42b6c
child 44 01ac7755b15a
equal deleted inserted replaced
37:14db3fe42b6c 38:f0fc793a3754
     6         Represents a sockaddr along with related address-family etc. information
     6         Represents a sockaddr along with related address-family etc. information
     7     """
     7     """
     8 
     8 
     9 class Endpoint (object) :
     9 class Endpoint (object) :
    10     """
    10     """
    11         Abstract network address interface, 
    11         Abstract network address interface. This can map to multiple actual addresses, potentially of different
       
    12         address families.
    12     """
    13     """
    13 
    14 
    14     def resolve (self, socktype, protocol = 0, passive = True) :
    15     def resolve (self, socktype, protocol = 0, passive = True) :
    15         """
    16         """
    16             Translate this Endpoint into a sequence of AddrInfo objects for the given socktype.
    17             Translate this Endpoint into a sequence of AddrInfo objects for the given socktype.
    17         """
    18         """
    18         
    19         
    19         raise NotImplemented()
    20         raise NotImplemented()
    20 
    21 
    21 
    22 class InetEndpoint (Endpoint) :
    22 class InetAddr (Endpoint) :
       
    23     """
    23     """
    24         An internet address, either IPv4 or IPv6.
    24         An internet endpoint, supports IPv4, IPv6 addresses and DNS hostnames.
    25 
    25 
    26            hostname     - [str] literal address or DNS hostname
    26            hostname     - [str] literal address or DNS hostname
    27            service      - [str] port number or service name
    27            service      - [str] port number or service name
    28            family       - AF_* associated with this address
    28            family       - AF_* associated with this address
    29     """
    29     """
    54         if passive :
    54         if passive :
    55             flags |= constants.AI_PASSIVE
    55             flags |= constants.AI_PASSIVE
    56 
    56 
    57         return self.endpoint.getaddrinfo(self.family, socktype, protocol, flags)
    57         return self.endpoint.getaddrinfo(self.family, socktype, protocol, flags)
    58 
    58 
    59 class UnixAddr (Endpoint) :
    59 class Address (Endpoint) :
    60     """
    60     """
    61         A local AF_UNIX address, as a path.
    61         A specific socket-level address of some address family, also useable as an Endpoint.
    62     """
       
    63 
       
    64     def __init__ (self, path) :
       
    65         self.path = path
       
    66         self.addr = af_unix.sockaddr_un(path)
       
    67 
       
    68     def resolve (self, socktype, protocol = 0, passive = True) :
       
    69         if not socktype :
       
    70             raise ValueError("Unknown socktype: %s" % (socktype, ))
       
    71 
       
    72         return [AddrInfo(0, constants.AF_UNIX, socktype, protocol, self.addr, self.path)]
       
    73 
       
    74 class SockAddr (Endpoint) :
       
    75     """
       
    76         A specific address, suitable both as an endpoint.
       
    77 
    62 
    78             addr        - low-level sockaddr object
    63             addr        - low-level sockaddr object
    79             family      - AF_*
    64             family      - AF_*
    80             socktype    - SOCK_*
       
    81     """
    65     """
    82 
    66 
    83     def __init__ (self, addr, family = constants.AF_UNSPEC, socktype = 0) :
    67     def __init__ (self, sockaddr, family = constants.AF_UNSPEC, canonical = None) :
    84         if not family :
    68         if not family :
    85             family = addr.family
    69             family = sockaddr.family
    86 
    70 
    87         self.addr = addr
    71         if not canonical :
       
    72             # XXX: delay?
       
    73             canonical = str(sockaddr)
       
    74 
       
    75         self._sockaddr = sockaddr
    88         self.family = family
    76         self.family = family
    89         self.socktype = socktype
    77         self.canonical = canonical
       
    78     
       
    79     def sockaddr (self) :
       
    80         """
       
    81             Returns a qmsk.net.socket.address.sockaddr object for this address.
       
    82         """
       
    83 
       
    84         return self._sockaddr
    90 
    85 
    91     def resolve (self, socktype, protocol = 0, passive = True) :
    86     def resolve (self, socktype, protocol = 0, passive = True) :
    92         """
    87         """
    93             Returns a single AddrInfo object representing this address 
    88             Returns a single AddrInfo object representing this address 
    94         """
    89         """
    95     
    90     
    96         if socktype and self.socktype and socktype != self.socktype :
    91         if not socktype :
    97             raise ValueError("Socket type mismatch: %s should be %s" % (socktype, self.socktype))
    92             raise ValueError("Socket type unknown")
    98 
    93 
    99         if not socktype :
    94         return [AddrInfo(0, self.family, socktype, protocol, self._sockaddr, self.canonical)]
   100             if self.socktype :
       
   101                 socktype = self.socktype
       
   102 
    95 
   103             else :
    96 class InetAddr (Address) :
   104                 raise ValueError("Socket type unknown")
    97     """
       
    98         An AF_INET/AF_INET6 address, with addr and port.
       
    99     """
       
   100     
       
   101     @property
       
   102     def addr (self) :
       
   103         return self._sockaddr.addr
   105 
   104 
       
   105     @property
       
   106     def port (self) :
       
   107         return self._sockaddr.port
   106 
   108 
   107         return [AddrInfo(0, self.family, socktype, protocol, self.addr, None)]
   109 class IPv4Addr (InetAddr) :
       
   110     """
       
   111         A fixed AF_INET address and port
       
   112     """
   108 
   113 
       
   114     def __init__ (self, addr, port) :
       
   115         super(IPv4Addr, self).__init__(af_inet.sockaddr_in(addr, port))
       
   116 
       
   117 class IPv6Addr (InetAddr) :
       
   118     """
       
   119         A fixed AF_INET6 address and port
       
   120     """
       
   121 
       
   122     def __init__ (self, addr, port) :
       
   123         super(IPv6Addr, self).__init__(af_inet6.sockaddr_in6(addr, port))
       
   124 
       
   125 class UnixAddr (Address) :
       
   126     """
       
   127         A local AF_UNIX address, as a path.
       
   128     """
       
   129 
       
   130     def __init__ (self, path) :
       
   131         super(UnixAddr, self).__init__(af_unix.sockaddr_un(path))
       
   132