7 # (c) 2005 Straylight/Edgeware
10 #----- Licensing notice -----------------------------------------------------
12 # This file is part of the Python interface to mLib.
14 # mLib/Python is free software; you can redistribute it and/or modify
15 # it under the terms of the GNU General Public License as published by
16 # the Free Software Foundation; either version 2 of the License, or
17 # (at your option) any later version.
19 # mLib/Python is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details.
24 # You should have received a copy of the GNU General Public License
25 # along with mLib/Python; if not, write to the Free Software Foundation,
26 # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 cdef _inaddr_frompy(sockaddr_in *sin, addr):
33 raise TypeError, 'want address/port pair'
35 if not inet_aton(a, &sin.sin_addr):
36 raise TypeError, 'bad IP address'
38 if not (0 <= port < 65536):
39 raise TypeError, 'port number out of range'
40 sin.sin_port = htons(port)
42 cdef _inaddr_topy(sockaddr_in *sin):
43 return inet_ntoa(sin.sin_addr), ntohs(sin.sin_port)
45 cdef class SelIdentify:
46 cdef ident_request irq
48 cdef readonly localaddr
49 cdef readonly remoteaddr
56 userproc = None, bogusproc = None,
57 badproc = None, errorproc = None, failedproc = None,
59 cdef sockaddr_in s_in, s_out
60 cdef size_t sz_in, sz_out
62 if PyObject_TypeCheck(sk, socket.SocketType):
64 sz_in = PSIZEOF(&s_in)
65 sz_out = PSIZEOF(&s_out)
66 if getsockname(fd, <sockaddr *>&s_in, &sz_in) or \
67 getpeername(fd, <sockaddr *>&s_out, &sz_out):
69 if s_in.sin_family != AF_INET or s_out.sin_family != AF_INET:
70 raise TypeError, 'must be internet socket'
72 raise TypeError, 'want pair of addresses'
74 _inaddr_frompy(&s_in, sk[0])
75 _inaddr_frompy(&s_out, sk[1])
76 ident(&me.irq, &_sel, &s_in, &s_out, _identfunc, <void *>me)
77 me.localaddr = _inaddr_topy(&s_in)
78 me.remoteaddr = _inaddr_topy(&s_out)
80 me._user = _checkcallable(userproc, 'user proc')
81 me._bad = _checkcallable(badproc, 'bad proc')
82 me._error = _checkcallable(errorproc, 'error proc')
83 me._failed = _checkcallable(failedproc, 'failed proc')
84 me._bogus = _checkcallable(bogusproc, 'bogus proc')
90 return _tobool(me._activep)
94 def __set__(me, proc):
95 me._user = _checkcallable(proc, 'user proc')
101 def __set__(me, proc):
102 me._eof = _checkcallable(proc, 'eof proc')
108 def __set__(me, proc):
109 me._bad = _checkcallable(proc, 'bad proc')
115 def __set__(me, proc):
116 me._error = _checkcallable(proc, 'error proc')
122 def __set__(me, proc):
123 me._failed = _checkcallable(proc, 'failed proc')
129 def __set__(me, proc):
130 me._bogus = _checkcallable(proc, 'bogus proc')
135 raise ValueError, 'already disabled'
143 def user(me, os, user):
144 return _maybecall(me._user, (os, user))
146 if me._bad is not None:
149 def error(me, error):
150 if me._error is not None:
151 return me._error(error)
153 def failed(me, errno, strerror):
154 if me._failed is not None:
155 return me._failed(errno, strerror)
158 return _maybecall(me._bogus, ())
160 cdef void _identfunc(ident_reply *i, void *arg):
162 id = <SelIdentify>arg
164 if i.type == IDENT_BAD:
166 elif i.type == IDENT_ERROR:
168 elif i.type == IDENT_USERID:
169 id.user(i.u.userid.os, i.u.userid.user)
171 #----- That's all, folks ----------------------------------------------------