debian/control: Add Build-Depends for `dh-python'.
[mLib-python] / ident.pyx
1 ### -*-pyrex-*-
2 ###
3 ### Ident client
4 ###
5 ### (c) 2005 Straylight/Edgeware
6 ###
7
8 ###----- Licensing notice ---------------------------------------------------
9 ###
10 ### This file is part of the Python interface to mLib.
11 ###
12 ### mLib/Python is free software; you can redistribute it and/or modify
13 ### it under the terms of the GNU General Public License as published by
14 ### the Free Software Foundation; either version 2 of the License, or
15 ### (at your option) any later version.
16 ###
17 ### mLib/Python is distributed in the hope that it will be useful,
18 ### but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ### GNU General Public License for more details.
21 ###
22 ### You should have received a copy of the GNU General Public License
23 ### along with mLib/Python; if not, write to the Free Software Foundation,
24 ### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25
26 import socket
27
28 cdef _inaddr_frompy(sockaddr_in *sin, addr):
29 cdef int port
30 if len(addr) != 2:
31 raise TypeError, 'want address/port pair'
32 a = addr[0]
33 if not inet_aton(a, &sin.sin_addr):
34 raise TypeError, 'bad IP address'
35 port = addr[1]
36 if not (0 <= port < 65536):
37 raise TypeError, 'port number out of range'
38 sin.sin_port = htons(port)
39
40 cdef _inaddr_topy(sockaddr_in *sin):
41 return inet_ntoa(sin.sin_addr), ntohs(sin.sin_port)
42
43 cdef class SelIdentify:
44 """
45 SelIdentify(SK, [userproc = None], [bogusproc = None],
46 [badproc = None], [errorproc = None],)
47
48 Asynchronously enquire about remote user of socket SK.
49 """
50 cdef ident_request irq
51 cdef int _activep
52 cdef readonly localaddr
53 cdef readonly remoteaddr
54 cdef _user
55 cdef _bad
56 cdef _error
57 cdef _bogus
58 def __cinit__(me, sk, userproc = None, bogusproc = None,
59 badproc = None, errorproc = None, *hunoz, **hukairz):
60 cdef sockaddr_in s_in, s_out
61 cdef socklen_t sz_in, sz_out
62 cdef int fd
63 if typecheck(sk, socket.SocketType):
64 fd = sk.fileno()
65 sz_in = PSIZEOF(&s_in)
66 sz_out = PSIZEOF(&s_out)
67 if getsockname(fd, <sockaddr *>&s_in, &sz_in) or \
68 getpeername(fd, <sockaddr *>&s_out, &sz_out):
69 _oserror()
70 if s_in.sin_family != AF_INET or s_out.sin_family != AF_INET:
71 raise TypeError, 'must be internet socket'
72 elif len(sk) != 2:
73 raise TypeError, 'want pair of addresses'
74 else:
75 _inaddr_frompy(&s_in, sk[0])
76 _inaddr_frompy(&s_out, sk[1])
77 ident(&me.irq, &_sel, &s_in, &s_out, _identfunc, <void *>me)
78 me.localaddr = _inaddr_topy(&s_in)
79 me.remoteaddr = _inaddr_topy(&s_out)
80 me._activep = 1
81 me._user = _checkcallable(userproc, 'user proc')
82 me._bad = _checkcallable(badproc, 'bad proc')
83 me._error = _checkcallable(errorproc, 'error proc')
84 me._bogus = _checkcallable(bogusproc, 'bogus proc')
85 def __dealloc__(me):
86 if me._activep:
87 ident_abort(&me.irq)
88 property activep:
89 """I.activep -> BOOL: query still in progress?"""
90 def __get__(me):
91 return _tobool(me._activep)
92 property userproc:
93 """I.userproc -> FUNC: call FUNC(OS, USER) if server replied"""
94 def __get__(me):
95 return me._user
96 def __set__(me, proc):
97 me._user = _checkcallable(proc, 'user proc')
98 def __del__(me):
99 me._user = None
100 property badproc:
101 """I.badproc -> FUNC: call FUNC() if server's reply was broken"""
102 def __get__(me):
103 return me._bad
104 def __set__(me, proc):
105 me._bad = _checkcallable(proc, 'bad proc')
106 def __del__(me):
107 me._bad = None
108 property errorproc:
109 """I.errorproc -> FUNC: call FUNC(ERR) if server reported error"""
110 def __get__(me):
111 return me._error
112 def __set__(me, proc):
113 me._error = _checkcallable(proc, 'error proc')
114 def __del__(me):
115 me._error = None
116 property bogusproc:
117 """I.bogusproc -> FUNC: call FUNC() on failure if no specific handler"""
118 def __get__(me):
119 return me._bogus
120 def __set__(me, proc):
121 me._bogus = _checkcallable(proc, 'bogus proc')
122 def __del__(me):
123 me._bogus = None
124 def kill(me):
125 """I.kill(): cancel ident query"""
126 if not me._activep:
127 raise ValueError, 'already disabled'
128 ident_abort(&me.irq)
129 me._dead()
130 def _dead(me):
131 me._activep = 0
132 me.dead()
133 def dead(me):
134 """I.dead(): called when operation completes or fails"""
135 pass
136 def user(me, os, user):
137 """I.user(OS, USER): called if server returns user name"""
138 return _maybecall(me._user, (os, user))
139 def bad(me):
140 """I.bad(): called if server's reply is invalid"""
141 if me._bad is not None:
142 return me._bad()
143 return me.bogus()
144 def error(me, error):
145 """I.error(ERR): called if server returns an error"""
146 if me._error is not None:
147 return me._error(error)
148 return me.bogus()
149 def bogus(me):
150 """I.bogus(): called on failure if there's no more specific handler"""
151 return _maybecall(me._bogus, ())
152
153 cdef void _identfunc(ident_reply *i, void *arg):
154 cdef SelIdentify id
155 id = <SelIdentify>arg
156 id._dead()
157 if i.type == IDENT_BAD:
158 id.bad()
159 elif i.type == IDENT_ERROR:
160 id.error(i.u.error)
161 elif i.type == IDENT_USERID:
162 id.user(i.u.userid.os, i.u.userid.user)
163
164 ###----- That's all, folks --------------------------------------------------