author | Tero Marttila <terom@fixme.fi> |
Fri, 25 Sep 2009 21:34:04 +0300 | |
changeset 49 | e2f79e68418a |
parent 46 | 64b4ffb44754 |
permissions | -rw-r--r-- |
6 | 1 |
""" |
2 |
Socket addresses at various levels: |
|
3 |
||
4 |
sockaddr - specific network-level address for socket operations |
|
5 |
addrinfo - information on a specific sockaddr including its socket parameters |
|
6 |
endpoint - human-readable network address, corresponding to multiple sockaddr's |
|
7 |
""" |
|
8 |
||
10 | 9 |
cimport qmsk.net.socket.platform as platform |
10 |
||
49
e2f79e68418a
fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
11 |
from qmsk.net.socket._address cimport sockaddr |
6 | 12 |
|
13 |
# build a sockaddr from the given sockaddr struct, based on sa_family |
|
10 | 14 |
cdef sockaddr build_sockaddr (platform.sockaddr *sa, size_t sa_len) |
6 | 15 |
|
16 |
cdef class addrinfo : |
|
17 |
""" |
|
23
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
18 |
A socket-level endpoint address, which contains the full socket parameters and an bind/connect address. |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
19 |
|
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
20 |
A full addrinfo struct is stored, but ai_canonname and ai_addr are stored as (optional) objects outside of the |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
21 |
addrinfo struct. |
30 | 22 |
|
23 |
>>> from qmsk.net.socket import af_inet |
|
23
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
24 |
>>> ai = addrinfo() |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
25 |
>>> 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:
20
diff
changeset
|
26 |
>>> ai.canonname |
30 | 27 |
>>> print addrinfo(addr=af_inet.sockaddr_in()) |
23
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
28 |
family=0, socktype=0, protocol=0, addr=0.0.0.0:0, 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:
20
diff
changeset
|
29 |
""" |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
30 |
|
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
31 |
# canonname is not stored |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
32 |
# may be NULL |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
33 |
cdef platform.addrinfo ai |
6 | 34 |
|
23
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
35 |
# may be NULL |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
36 |
cdef sockaddr 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:
20
diff
changeset
|
37 |
cdef object 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:
20
diff
changeset
|
38 |
|
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
39 |
# 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:
20
diff
changeset
|
40 |
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:
20
diff
changeset
|
41 |
|
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
42 |
# set the contents of self.ai from the given real addrinfo |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
43 |
# this ignores the ai_canonname attribute |
15d8bb96b8d4
fix up addrinfo to hold a real platform.addrinfo, storing ai_addr/ai_canonname as objects
Tero Marttila <terom@fixme.fi>
parents:
20
diff
changeset
|
44 |
cdef _init_addrinfo (self, platform.addrinfo *ai) |
6 | 45 |
|
46 |
# build and return a new addrinfo instance |
|
10 | 47 |
cdef addrinfo build_addrinfo (platform.addrinfo *c_ai) |
6 | 48 |
|
49 |
cdef class endpoint : |
|
50 |
""" |
|
49
e2f79e68418a
fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
51 |
XXX: silly abstraction, just use the getaddrinfo function |
6 | 52 |
|
49
e2f79e68418a
fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
53 |
>>> from qmsk.net.socket.constants import * |
6 | 54 |
>>> e = endpoint('127.0.0.1', 80) |
55 |
>>> str(e) |
|
56 |
'hostname=127.0.0.1, service=80' |
|
49
e2f79e68418a
fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
57 |
>>> res = e.getaddrinfo(AF_UNSPEC, SOCK_STREAM) |
6 | 58 |
>>> len(res) |
59 |
1 |
|
60 |
>>> str(res[0]) |
|
61 |
'family=2, socktype=1, protocol=6, addr=127.0.0.1:80, canonname=None' |
|
62 |
>>> e = endpoint('2001::5', 80) |
|
63 |
>>> str(e) |
|
64 |
'hostname=2001::5, service=80' |
|
49
e2f79e68418a
fix up circular cimports related to the sockaddr type, and touch up endpoint/getaddrinfo a bit
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
65 |
>>> res = e.getaddrinfo(AF_UNSPEC, SOCK_STREAM) |
6 | 66 |
>>> len(res) |
67 |
1 |
|
68 |
>>> str(res[0]) |
|
69 |
'family=10, socktype=1, protocol=6, addr=[2001::5]:80, canonname=None' |
|
70 |
||
71 |
""" |
|
72 |
||
73 |
# our defining attributes, set via __init__ |
|
30 | 74 |
cdef public object hostname, service |
6 | 75 |