author | Tero Marttila <terom@fixme.fi> |
Sun, 23 Aug 2009 22:39:19 +0300 | |
changeset 35 | 50dc1517f797 |
parent 32 | 2168a9ffaef7 |
child 37 | 14db3fe42b6c |
permissions | -rw-r--r-- |
32 | 1 |
|
2 |
from qmsk.net.socket import address, constants, af_unix |
|
3 |
||
4 |
class AddrInfo (address.addrinfo) : |
|
5 |
""" |
|
6 |
Represents a sockaddr along with related address-family etc. information |
|
7 |
""" |
|
8 |
||
9 |
class Endpoint (object) : |
|
10 |
""" |
|
11 |
Abstract network address interface, |
|
12 |
""" |
|
13 |
||
14 |
def resolve (self, family, socktype, protocol = 0, passive = True) : |
|
15 |
""" |
|
16 |
Translate this Endpoint into a sequence of AddrInfo objects for the given family/socktype. |
|
17 |
""" |
|
18 |
||
19 |
raise NotImplemented() |
|
20 |
||
21 |
||
22 |
class InetAddr (Endpoint, address.endpoint) : |
|
23 |
""" |
|
24 |
An internet address, either IPv4 or IPv6. |
|
25 |
||
26 |
hostname - [str] literal address or DNS hostname |
|
27 |
service - [str] port number or service name |
|
28 |
""" |
|
29 |
||
30 |
def resolve (self, family, socktype, protocol = 0, passive = True) : |
|
31 |
""" |
|
32 |
Resolve using getaddrinfo |
|
33 |
""" |
|
34 |
||
35 |
flags = 0 |
|
36 |
||
37 |
if passive : |
|
35
50dc1517f797
fix endpoint.InetAddr.resolve
Tero Marttila <terom@fixme.fi>
parents:
32
diff
changeset
|
38 |
flags |= constants.AI_PASSIVE |
32 | 39 |
|
35
50dc1517f797
fix endpoint.InetAddr.resolve
Tero Marttila <terom@fixme.fi>
parents:
32
diff
changeset
|
40 |
return self.getaddrinfo(family, socktype, protocol, flags) |
32 | 41 |
|
42 |
class UnixAddr (Endpoint) : |
|
43 |
""" |
|
44 |
A local AF_UNIX address, as a path. |
|
45 |
""" |
|
46 |
||
47 |
def __init__ (self, path) : |
|
48 |
self.path = path |
|
49 |
self.addr = af_unix.sockaddr_un(path) |
|
50 |
||
51 |
def resolve (self, family, socktype, protocol = 0, passive = True) : |
|
52 |
if family != constants.AF_UNIX : |
|
53 |
raise ValueError("Address family mismatch: %s" % (family, )) |
|
54 |
||
55 |
if not socktype : |
|
56 |
raise ValueError("Unknown socktype: %s" % (socktype, )) |
|
57 |
||
58 |
return [AddrInfo(0, constants.AF_UNIX, socktype, protocol, self.addr, self.path)] |
|
59 |
||
60 |
class SockAddr (Endpoint) : |
|
61 |
""" |
|
62 |
A specific address, suitable both as an endpoint. |
|
63 |
||
64 |
addr - low-level sockaddr object |
|
65 |
family - AF_* |
|
66 |
socktype - SOCK_* |
|
67 |
""" |
|
68 |
||
69 |
def __init__ (self, addr, family = constants.AF_UNSPEC, socktype = 0) : |
|
70 |
if not family : |
|
71 |
family = addr.family |
|
72 |
||
73 |
self.addr = addr |
|
74 |
self.family = family |
|
75 |
self.socktype = socktype |
|
76 |
||
77 |
def resolve (self, family, socktype, protocol = 0, passive = True) : |
|
78 |
""" |
|
79 |
Returns a single AddrInfo object representing this address |
|
80 |
""" |
|
81 |
||
82 |
if family and family != self.family : |
|
83 |
raise ValueError("Address family mismatch: %s should be %s" % (family, self.family)) |
|
84 |
||
85 |
elif not family : |
|
86 |
family = self.family |
|
87 |
||
88 |
||
89 |
if socktype and self.socktype and socktype != self.socktype : |
|
90 |
raise ValueError("Socket type mismatch: %s should be %s" % (socktype, self.socktype)) |
|
91 |
||
92 |
if not socktype : |
|
93 |
if self.socktype : |
|
94 |
socktype = self.socktype |
|
95 |
||
96 |
else : |
|
97 |
raise ValueError("Socket type unknown") |
|
98 |
||
99 |
||
100 |
return [AddrInfo(0, family, socktype, protocol, self.addr, None)] |
|
101 |