author | Tero Marttila <terom@fixme.fi> |
Tue, 18 Aug 2009 22:24:36 +0300 | |
changeset 23 | 15d8bb96b8d4 |
parent 22 | f6e8d5e37998 |
child 24 | f18b5787c46c |
permissions | -rw-r--r-- |
11
7ae92c2b433f
move libc+py back to qmsk.net.x
Tero Marttila <terom@fixme.fi>
parents:
10
diff
changeset
|
1 |
cimport qmsk.net.libc as libc |
22
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
2 |
cimport qmsk.net.py as py |
6 | 3 |
|
20
0e4933d5862e
rename qmsk.net.socket.addr -> qmsk.net.socket.address
Tero Marttila <terom@fixme.fi>
parents:
17
diff
changeset
|
4 |
from qmsk.net.socket.address cimport * |
10 | 5 |
|
6 |
cimport qmsk.net.socket.platform as platform |
|
6 | 7 |
|
8 |
cdef class sockaddr : |
|
10 | 9 |
cdef void _init_family (self, platform.sa_family_t family=platform.AF_UNSPEC) : |
6 | 10 |
self.family = family |
11 |
||
12 |
# XXX:use size_t |
|
10 | 13 |
cdef int _get_sockaddr (self, platform.sockaddr **sa_ptr, platform.socklen_t *sa_len) except -1 : |
6 | 14 |
""" |
15 |
Get the sockaddr pointer and sockaddr length for this address |
|
16 |
""" |
|
17 |
||
18 |
raise NotImplementedError() |
|
19 |
||
10 | 20 |
cdef platform.sockaddr* _get_sockaddr_ptr (self) except NULL : |
6 | 21 |
""" |
22 |
Get the sockaddr pointer |
|
23 |
""" |
|
24 |
||
10 | 25 |
cdef platform.sockaddr *sa |
26 |
cdef platform.socklen_t sa_len |
|
6 | 27 |
|
28 |
self._get_sockaddr(&sa, &sa_len) |
|
29 |
||
30 |
return sa |
|
31 |
||
10 | 32 |
cdef platform.socklen_t _get_sockaddr_len (self) except -1 : |
6 | 33 |
""" |
34 |
Get the sockaddr len |
|
35 |
""" |
|
36 |
||
10 | 37 |
cdef platform.sockaddr *sa |
38 |
cdef platform.socklen_t sa_len |
|
6 | 39 |
|
40 |
self._get_sockaddr(&sa, &sa_len) |
|
41 |
||
42 |
return sa_len |
|
43 |
||
10 | 44 |
cdef int _set_sockaddr (self, platform.sockaddr *sa, size_t sa_len) except -1 : |
6 | 45 |
""" |
46 |
Set the sockaddr value for this address; sa_len must match! |
|
47 |
""" |
|
48 |
||
49 |
raise NotImplementedError() |
|
50 |
||
51 |
def getnameinfo (self) : |
|
52 |
""" |
|
53 |
Returns a (host, serv) tuple for this address à la getnameinfo |
|
23
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
54 |
|
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
55 |
>>> addr = sockaddr_in() |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
56 |
>>> assert addr.getnameinfo() == (addr.addr, str(addr.port)) |
6 | 57 |
""" |
58 |
||
10 | 59 |
cdef platform.sockaddr *sa |
60 |
cdef platform.socklen_t sa_len |
|
6 | 61 |
|
62 |
# XXX: take as args? |
|
10 | 63 |
cdef int flags = platform.NI_NUMERICHOST | platform.NI_NUMERICSERV |
6 | 64 |
|
65 |
# get our abstract sockaddr |
|
66 |
self._get_sockaddr(&sa, &sa_len) |
|
67 |
||
68 |
# get nice text format |
|
10 | 69 |
return platform.getnameinfo(sa, sa_len, flags) |
6 | 70 |
|
71 |
property addr : |
|
72 |
""" |
|
73 |
The ASCII literal network address |
|
74 |
""" |
|
75 |
||
76 |
def __get__ (self) : |
|
77 |
""" |
|
78 |
Default implmentation using getnameinfo() |
|
79 |
""" |
|
80 |
||
81 |
addr, port = self.getnameinfo() |
|
82 |
||
83 |
return addr |
|
84 |
||
85 |
property port : |
|
86 |
""" |
|
87 |
The integer port number |
|
88 |
""" |
|
89 |
||
90 |
def __get__ (self) : |
|
91 |
""" |
|
92 |
Default implementation using getnameinfo() and int() |
|
93 |
""" |
|
94 |
||
95 |
addr, port = self.getnameinfo() |
|
96 |
||
97 |
return int(port) |
|
17
5f4077a530b0
add repr for sockaddr, and implment read for sockets, plus a recvmsg wrapper called readv...
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
98 |
|
5f4077a530b0
add repr for sockaddr, and implment read for sockets, plus a recvmsg wrapper called readv...
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
99 |
def __repr__ (self) : |
5f4077a530b0
add repr for sockaddr, and implment read for sockets, plus a recvmsg wrapper called readv...
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
100 |
return "sockaddr(%d, %s, %d)" % (self.family, self.addr, self.port) |
6 | 101 |
|
102 |
cdef class sockaddr_in (sockaddr) : |
|
103 |
""" |
|
104 |
AF_INET struct sockaddr_in |
|
105 |
||
106 |
>>> sa = sockaddr_in("127.0.0.1", 80) |
|
107 |
>>> sa.addr |
|
108 |
'127.0.0.1' |
|
109 |
>>> sa.port |
|
110 |
80 |
|
111 |
>>> str(sa) |
|
112 |
'127.0.0.1:80' |
|
22
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
113 |
|
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
114 |
>>> sockaddr_in('2001::5') |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
115 |
Traceback (most recent call last): |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
116 |
... |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
117 |
NameError: Invalid network address for specified address family: '2001::5' |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
118 |
|
6 | 119 |
""" |
120 |
||
121 |
# the struct sockaddr_in |
|
10 | 122 |
cdef platform.sockaddr_in sockaddr |
6 | 123 |
|
10 | 124 |
def __init__ (self, object addr=None, platform.in_port_t port=0) : |
6 | 125 |
""" |
126 |
Construct using given literal IPv4 address and TCP/UDP port |
|
127 |
||
128 |
addr - IPv4 address, defaults to INADDR_ANY (0.0.0.0) |
|
129 |
port - TCP/UDP port, defaults to 0 (ephemeral) |
|
130 |
""" |
|
131 |
||
22
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
132 |
# zero |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
133 |
libc.memset(&self.sockaddr, 0, sizeof(self.sockaddr)) |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
134 |
|
6 | 135 |
# store our family |
136 |
# XXX: this should be a class attribute... |
|
10 | 137 |
self._init_family(platform.AF_INET) |
6 | 138 |
|
139 |
# constant af |
|
140 |
self.sockaddr.sin_family = self.family |
|
141 |
||
142 |
# set the sin_port |
|
10 | 143 |
self.sockaddr.sin_port = platform.htons(port) |
6 | 144 |
|
145 |
if addr : |
|
146 |
# set the sin_addr |
|
147 |
# this automatically converts the addr from str -> char * |
|
10 | 148 |
platform.inet_pton(self.family, addr, &self.sockaddr.sin_addr) |
6 | 149 |
|
150 |
else : |
|
151 |
# set as INADDR_ANY |
|
10 | 152 |
self.sockaddr.sin_addr.s_addr = platform.INADDR_ANY |
6 | 153 |
|
10 | 154 |
cdef int _get_sockaddr (self, platform.sockaddr **sa_ptr, platform.socklen_t *sa_len) except -1 : |
6 | 155 |
if sa_ptr : |
10 | 156 |
sa_ptr[0] = <platform.sockaddr *> &self.sockaddr |
6 | 157 |
|
158 |
if sa_len : |
|
159 |
sa_len[0] = sizeof(self.sockaddr) |
|
160 |
||
161 |
return 0 |
|
162 |
||
10 | 163 |
cdef int _set_sockaddr (self, platform.sockaddr *sa, size_t sa_len) except -1 : |
6 | 164 |
assert sa_len == sizeof(self.sockaddr) |
165 |
||
166 |
libc.memcpy(&self.sockaddr, sa, sa_len) |
|
167 |
||
168 |
property port : |
|
169 |
""" |
|
170 |
The integer port number |
|
23
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
171 |
|
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
172 |
>>> sockaddr_in(port=1234).port |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
173 |
1234 |
6 | 174 |
""" |
175 |
||
176 |
def __get__ (self) : |
|
10 | 177 |
return platform.ntohs(self.sockaddr.sin_port) |
6 | 178 |
|
22
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
179 |
def __cmp__ (self, other_obj) : |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
180 |
""" |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
181 |
A sockaddr_in is equal to any other sockaddr_in which has the same addr and port |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
182 |
|
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
183 |
>>> assert sockaddr_in() == sockaddr_in() |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
184 |
>>> assert sockaddr_in('127.0.0.1', 80) == sockaddr_in('127.0.0.1', 80) |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
185 |
>>> addr = sockaddr_in(); assert addr == addr |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
186 |
""" |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
187 |
|
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
188 |
if not isinstance(other_obj, sockaddr_in) : |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
189 |
return <object> py.Py_NotImplemented |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
190 |
|
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
191 |
cdef sockaddr_in other = other_obj |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
192 |
cdef platform.sockaddr_in *sa1 = &self.sockaddr, *sa2 = &other.sockaddr |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
193 |
|
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
194 |
if other is self : |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
195 |
return 0 |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
196 |
|
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
197 |
return ( |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
198 |
libc.memcmp(<void *> &sa1.sin_port, <void *> &sa2.sin_port, sizeof(sa1.sin_port)) |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
199 |
or libc.memcmp(<void *> &sa1.sin_addr, <void *> &sa2.sin_addr, sizeof(sa1.sin_addr)) |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
200 |
) |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
201 |
|
6 | 202 |
def __str__ (self) : |
203 |
""" |
|
204 |
Return the literal ASCII representation for this sockaddr as an '<addr>:<port> string |
|
22
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
205 |
|
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
206 |
>>> str(sockaddr_in()) |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
207 |
'0.0.0.0:0' |
6 | 208 |
""" |
209 |
||
210 |
# format |
|
211 |
return "%s:%s" % self.getnameinfo() |
|
212 |
||
213 |
cdef class sockaddr_in6 (sockaddr) : |
|
214 |
""" |
|
215 |
AF_INET6 struct sockaddr_in6 |
|
216 |
||
217 |
>>> sa6 = sockaddr_in6("::1", 80) |
|
218 |
>>> sa6.addr |
|
219 |
'::1' |
|
220 |
>>> sa6.port |
|
221 |
80 |
|
222 |
>>> str(sa6) |
|
223 |
'[::1]:80' |
|
22
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
224 |
|
6 | 225 |
""" |
226 |
||
10 | 227 |
cdef platform.sockaddr_in6 sockaddr |
6 | 228 |
|
22
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
229 |
def __init__ (self, object addr=None, platform.in_port_t port=0, unsigned int scope_id = 0) : |
6 | 230 |
""" |
231 |
Construct using given literal IPv6 address and TCP/UDP port |
|
232 |
||
10 | 233 |
addr - IPv6 address, defaults to platform.in6addr_any (::) |
6 | 234 |
port - TCP/UDP port, defaults to 0 (ephemeral) |
22
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
235 |
scope_id - (optional) scope ID representing interface index for link-local addresses |
6 | 236 |
""" |
237 |
||
22
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
238 |
# zero |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
239 |
libc.memset(&self.sockaddr, 0, sizeof(self.sockaddr)) |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
240 |
|
6 | 241 |
# store our family |
242 |
# XXX: this should be a class attribute... |
|
10 | 243 |
self._init_family(platform.AF_INET6) |
6 | 244 |
|
245 |
# constant af |
|
246 |
self.sockaddr.sin6_family = self.family |
|
247 |
||
248 |
# set the sin_port |
|
10 | 249 |
self.sockaddr.sin6_port = platform.htons(port) |
6 | 250 |
|
251 |
if addr : |
|
252 |
# set the sin_addr |
|
253 |
# this automatically converts the addr from str -> char * |
|
10 | 254 |
platform.inet_pton(self.family, addr, &self.sockaddr.sin6_addr) |
6 | 255 |
|
256 |
else : |
|
257 |
# set as INADDR_ANY |
|
10 | 258 |
self.sockaddr.sin6_addr = platform.in6addr_any |
6 | 259 |
|
22
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
260 |
# scope ID |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
261 |
self.sockaddr.sin6_scope_id = scope_id |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
262 |
|
10 | 263 |
cdef int _get_sockaddr (self, platform.sockaddr **sa_ptr, platform.socklen_t *sa_len) except -1 : |
6 | 264 |
if sa_ptr : |
10 | 265 |
sa_ptr[0] = <platform.sockaddr *> &self.sockaddr |
6 | 266 |
|
267 |
if sa_len : |
|
268 |
sa_len[0] = sizeof(self.sockaddr) |
|
269 |
||
270 |
return 0 |
|
271 |
||
10 | 272 |
cdef int _set_sockaddr (self, platform.sockaddr *sa, size_t sa_len) except -1 : |
6 | 273 |
assert sa_len == sizeof(self.sockaddr) |
274 |
||
275 |
libc.memcpy(&self.sockaddr, sa, sa_len) |
|
276 |
||
277 |
property port : |
|
278 |
""" |
|
22
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
279 |
The integer port number. |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
280 |
|
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
281 |
This will represent it correctly in host byte order. |
23
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
282 |
|
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
283 |
>>> sockaddr_in6(port=1234).port |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
284 |
1234 |
6 | 285 |
""" |
286 |
||
287 |
def __get__ (self) : |
|
10 | 288 |
return platform.ntohs(self.sockaddr.sin6_port) |
6 | 289 |
|
290 |
||
22
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
291 |
property flowinfo : |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
292 |
""" |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
293 |
The integer flowinfo |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
294 |
|
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
295 |
XXX: byteorder? |
23
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
296 |
|
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
297 |
>>> sockaddr_in6().flowinfo |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
298 |
0 |
22
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
299 |
""" |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
300 |
|
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
301 |
def __get__ (self) : |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
302 |
return self.sockaddr.sin6_flowinfo |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
303 |
|
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
304 |
|
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
305 |
property scope_id : |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
306 |
""" |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
307 |
The scope ID - corresponds to an interface index for link-scope addresses. |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
308 |
|
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
309 |
This should be in host byte order... |
23
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
310 |
|
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
311 |
>>> sockaddr_in6(scope_id=1337).scope_id |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
312 |
1337 |
22
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
313 |
""" |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
314 |
|
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
315 |
def __get__ (self) : |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
316 |
return self.sockaddr.sin6_scope_id |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
317 |
|
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
318 |
def __cmp__ (self, other_obj) : |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
319 |
""" |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
320 |
A sockaddr_in6 is equal to any other sockaddr_in6 which has the same addr, port and scope ID. |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
321 |
|
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
322 |
XXX: flowinfo? |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
323 |
|
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
324 |
XXX: A sockaddr_in6 is also equal to a sockaddr_in if the sockaddr_in6 represents the given v4-mapped address. |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
325 |
|
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
326 |
>>> assert sockaddr_in6() == sockaddr_in6() |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
327 |
>>> assert sockaddr_in6('0:0:0::1', 80) == sockaddr_in6('::1', 80) |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
328 |
>>> assert sockaddr_in6('::127.0.0.1') == sockaddr_in('127.0.0.1') |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
329 |
""" |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
330 |
|
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
331 |
if not isinstance(other_obj, sockaddr_in6) : |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
332 |
return <object> py.Py_NotImplemented |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
333 |
|
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
334 |
cdef sockaddr_in6 other = other_obj |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
335 |
cdef platform.sockaddr_in6 *sa1 = &self.sockaddr, *sa2 = &other.sockaddr |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
336 |
|
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
337 |
if other is self : |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
338 |
return 0 |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
339 |
|
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
340 |
return ( |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
341 |
libc.memcmp(<void *> &sa1.sin6_port, <void *> &sa2.sin6_port, sizeof(sa1.sin6_port)) |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
342 |
or libc.memcmp(<void *> &sa1.sin6_addr, <void *> &sa2.sin6_addr, sizeof(sa1.sin6_addr)) |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
343 |
or libc.memcmp(<void *> &sa1.sin6_scope_id, <void *> &sa2.sin6_scope_id, sizeof(sa1.sin6_scope_id)) |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
344 |
) |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
345 |
|
6 | 346 |
def __str__ (self) : |
347 |
""" |
|
348 |
Return the literal ASCII representation for this sockaddr as a '[<addr>]:<port> string |
|
22
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
349 |
|
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
350 |
>>> str(sockaddr_in6()) |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
351 |
'[::]:0' |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
352 |
|
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
353 |
>>> str(sockaddr_in6('2001:0::05:1')) |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
354 |
'[2001::5:1]' |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
355 |
|
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
356 |
>>> str(sockaddr_in6('fe80::abcd', scope_id=5)) |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
357 |
'[fe80::abcd%5]' |
6 | 358 |
""" |
22
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
359 |
|
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
360 |
addr, port = self.getnameinfo() |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
361 |
scope_id = self.scope_id |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
362 |
|
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
363 |
# format with scope_id |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
364 |
return "[%s%s]:%s" % ( |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
365 |
addr, |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
366 |
"%%%d" % scope_id if scope_id else "", |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
367 |
port |
f6e8d5e37998
some __cmp__ and doctest love for socket.address
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
368 |
) |
6 | 369 |
|
370 |
# mapping of AF -> sockaddr, user-modifyable |
|
371 |
SOCKADDR_BY_FAMILY = { |
|
10 | 372 |
platform.AF_INET: sockaddr_in, |
373 |
platform.AF_INET6: sockaddr_in6, |
|
6 | 374 |
} |
375 |
||
376 |
# build a sockaddr from the given sockaddr struct, based on sa_family |
|
10 | 377 |
cdef sockaddr build_sockaddr (platform.sockaddr *sa, size_t sa_len) : |
6 | 378 |
# lookup correct class to use |
379 |
addr_type = SOCKADDR_BY_FAMILY[sa.sa_family] |
|
380 |
||
381 |
# construct with defaults |
|
382 |
cdef sockaddr addr = addr_type() |
|
383 |
||
384 |
# store |
|
385 |
addr._set_sockaddr(sa, sa_len) |
|
386 |
||
387 |
return addr |
|
388 |
||
389 |
cdef class addrinfo : |
|
23
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
390 |
|
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
391 |
cdef _init_ai_members (self) : |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
392 |
""" |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
393 |
Update self.ai.ai_* to reflect self.ai_* |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
394 |
""" |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
395 |
|
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
396 |
cdef platform.socklen_t addr_len |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
397 |
|
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
398 |
if self.ai_addr is not None : |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
399 |
self.ai_addr._get_sockaddr(&self.ai.ai_addr, &addr_len) |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
400 |
self.ai.ai_addrlen = addr_len |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
401 |
|
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
402 |
else : |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
403 |
self.ai.ai_addr = NULL |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
404 |
self.ai.ai_addrlen = 0 |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
405 |
|
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
406 |
if self.ai_canonname is not None : |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
407 |
self.ai.ai_canonname = self.ai_canonname |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
408 |
|
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
409 |
else : |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
410 |
self.ai.ai_canonname = NULL |
6 | 411 |
|
23
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
412 |
def __init__ (self, int flags = 0, int family = 0, int socktype = 0, int protocol = 0, sockaddr addr = None, object canonname = None) : |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
413 |
""" |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
414 |
Construct a new addrinfo with the given parameters |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
415 |
""" |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
416 |
|
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
417 |
|
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
418 |
self.ai.ai_flags = flags |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
419 |
self.ai.ai_family = family |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
420 |
self.ai.ai_socktype = socktype |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
421 |
self.ai.ai_protocol = protocol |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
422 |
|
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
423 |
self.ai_addr = addr |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
424 |
|
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
425 |
if canonname is not None : |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
426 |
self.ai_canonname = str(canonname) |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
427 |
|
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
428 |
else : |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
429 |
self.ai_canonname = None |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
430 |
|
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
431 |
# update self.ai |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
432 |
self._init_ai_members() |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
433 |
|
10 | 434 |
cdef _init_addrinfo (self, platform.addrinfo *ai) : |
23
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
435 |
""" |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
436 |
Re-initialize this addrinfo's parameters from the given addrinfo |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
437 |
""" |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
438 |
|
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
439 |
# copy raw |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
440 |
self.ai = ai[0] |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
441 |
|
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
442 |
# store copies of external objects |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
443 |
if ai.ai_addr : |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
444 |
# copy addr as object |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
445 |
self.ai_addr = build_sockaddr(ai.ai_addr, ai.ai_addrlen) |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
446 |
|
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
447 |
else : |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
448 |
self.ai_addr = None |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
449 |
|
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
450 |
if ai.ai_canonname : |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
451 |
# copy as object |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
452 |
self.ai_canonname = ai.ai_canonname |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
453 |
|
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
454 |
else : |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
455 |
self.ai_canonname = None |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
456 |
|
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
457 |
# update self.ai |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
458 |
self._init_ai_members() |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
459 |
|
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
460 |
property flags : |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
461 |
def __get__ (self) : return self.ai.ai_flags |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
462 |
|
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
463 |
property family : |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
464 |
def __get__ (self) : return self.ai.ai_family |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
465 |
|
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
466 |
property socktype : |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
467 |
def __get__ (self) : return self.ai.ai_socktype |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
468 |
|
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
469 |
property protocol : |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
470 |
def __get__ (self) : return self.ai.ai_protocol |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
471 |
|
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
472 |
property addr : |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
473 |
# XXX: None? |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
474 |
def __get__ (self) : return self.ai_addr |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
475 |
|
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
476 |
property canonname : |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
477 |
# XXX: None? |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
478 |
def __get__ (self) : return self.ai_canonname |
6 | 479 |
|
480 |
def __str__ (self) : |
|
481 |
return "family=%d, socktype=%d, protocol=%d, addr=%s, canonname=%s" % (self.family, self.socktype, self.protocol, self.addr, self.canonname) |
|
482 |
||
10 | 483 |
cdef addrinfo build_addrinfo (platform.addrinfo *c_ai) : |
6 | 484 |
cdef addrinfo ai = addrinfo() |
485 |
||
486 |
ai._init_addrinfo(c_ai) |
|
487 |
||
488 |
return ai |
|
489 |
||
490 |
cdef class endpoint : |
|
491 |
||
492 |
def __init__ (self, hostname=None, service=None) : |
|
493 |
""" |
|
494 |
Construct with the given hostname/service, either of which may be None. |
|
495 |
||
496 |
A hostname of None implies all valid local addresses (with AI_PASSIVE), and a service of None implies an |
|
497 |
ephemeral port. |
|
498 |
||
499 |
hostname - the literal address or DNS hostname or anything else that GAI supports |
|
500 |
service - the numeric port or service name |
|
501 |
""" |
|
502 |
||
503 |
self.hostname = str(hostname) |
|
504 |
self.service = str(service) |
|
505 |
||
10 | 506 |
cpdef getaddrinfo (self, int family, int socktype, int protocol = 0, int flags = platform.AI_PASSIVE) : |
6 | 507 |
""" |
508 |
Look up our hostname/service using the given socket parameters, and return a sequence of addrinfo objects. |
|
509 |
""" |
|
510 |
||
511 |
# XXX: Cython doesn't support proper compound value literals... |
|
10 | 512 |
cdef platform.addrinfo hints |
6 | 513 |
|
514 |
libc.memset(&hints, 0, sizeof(hints)) |
|
515 |
hints.ai_flags = flags |
|
516 |
hints.ai_family = family |
|
517 |
hints.ai_socktype = socktype |
|
518 |
hints.ai_protocol = protocol |
|
519 |
||
10 | 520 |
cdef platform.addrinfo *res, *r |
6 | 521 |
cdef int err |
522 |
cdef object ret = [] |
|
523 |
||
524 |
cdef char *hostname = NULL |
|
525 |
cdef char *service = NULL |
|
526 |
||
527 |
if self.hostname is not None : |
|
528 |
hostname = self.hostname |
|
529 |
||
530 |
if self.service is not None : |
|
531 |
service = self.service |
|
532 |
||
533 |
# operate! |
|
10 | 534 |
err = platform.c_getaddrinfo(hostname, service, &hints, &res) |
6 | 535 |
|
536 |
try : |
|
537 |
if err : |
|
538 |
# XXX: raise a GAIError |
|
10 | 539 |
raise Exception(platform.gai_strerror(err)) |
6 | 540 |
|
23
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
541 |
# gather results from linked list to PyList |
6 | 542 |
r = res |
543 |
||
544 |
while r : |
|
545 |
ret.append(build_addrinfo(r)) |
|
546 |
||
547 |
r = r.ai_next |
|
548 |
||
549 |
# ok |
|
550 |
return ret |
|
551 |
||
552 |
finally : |
|
10 | 553 |
platform.c_freeaddrinfo(res) |
6 | 554 |
|
555 |
def __str__ (self) : |
|
556 |
return "hostname=%s, service=%s" % (self.hostname, self.service) |
|
557 |