chiark / gitweb /
1c2789e48ceeb9b841da5df3c6cb37205058ac6f
[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   cdef ident_request irq
45   cdef int _activep
46   cdef readonly localaddr
47   cdef readonly remoteaddr
48   cdef _user
49   cdef _bad
50   cdef _error
51   cdef _bogus
52   def __cinit__(me, sk, userproc = None, bogusproc = None,
53                 badproc = None, errorproc = None, *hunoz, **hukairz):
54     cdef sockaddr_in s_in, s_out
55     cdef socklen_t sz_in, sz_out
56     cdef int fd
57     if typecheck(sk, socket.SocketType):
58       fd = sk.fileno()
59       sz_in = PSIZEOF(&s_in)
60       sz_out = PSIZEOF(&s_out)
61       if getsockname(fd, <sockaddr *>&s_in, &sz_in) or \
62          getpeername(fd, <sockaddr *>&s_out, &sz_out):
63         _oserror()
64       if s_in.sin_family != AF_INET or s_out.sin_family != AF_INET:
65         raise TypeError, 'must be internet socket'
66     elif len(sk) != 2:
67       raise TypeError, 'want pair of addresses'
68     else:
69       _inaddr_frompy(&s_in, sk[0])
70       _inaddr_frompy(&s_out, sk[1])
71     ident(&me.irq, &_sel, &s_in, &s_out, _identfunc, <void *>me)
72     me.localaddr = _inaddr_topy(&s_in)
73     me.remoteaddr = _inaddr_topy(&s_out)
74     me._activep = 1
75     me._user = _checkcallable(userproc, 'user proc')
76     me._bad = _checkcallable(badproc, 'bad proc')
77     me._error = _checkcallable(errorproc, 'error proc')
78     me._bogus = _checkcallable(bogusproc, 'bogus proc')
79   def __dealloc__(me):
80     if me._activep:
81       ident_abort(&me.irq)
82   property activep:
83     def __get__(me):
84       return _tobool(me._activep)
85   property userproc:
86     def __get__(me):
87       return me._user
88     def __set__(me, proc):
89       me._user = _checkcallable(proc, 'user proc')
90     def __del__(me):
91       me._user = None
92   property badproc:
93     def __get__(me):
94       return me._bad
95     def __set__(me, proc):
96       me._bad = _checkcallable(proc, 'bad proc')
97     def __del__(me):
98       me._bad = None
99   property errorproc:
100     def __get__(me):
101       return me._error
102     def __set__(me, proc):
103       me._error = _checkcallable(proc, 'error proc')
104     def __del__(me):
105       me._error = None
106   property bogusproc:
107     def __get__(me):
108       return me._bogus
109     def __set__(me, proc):
110       me._bogus = _checkcallable(proc, 'bogus proc')
111     def __del__(me):
112       me._bogus = None
113   def kill(me):
114     if not me._activep:
115       raise ValueError, 'already disabled'
116     ident_abort(&me.irq)
117     me._dead()
118   def _dead(me):
119     me._activep = 0
120     me.dead()
121   def dead(me):
122     pass
123   def user(me, os, user):
124     return _maybecall(me._user, (os, user))
125   def bad(me):
126     if me._bad is not None:
127       return me._bad()
128     return me.bogus()
129   def error(me, error):
130     if me._error is not None:
131       return me._error(error)
132     return me.bogus()
133   def bogus(me):
134     return _maybecall(me._bogus, ())
135
136 cdef void _identfunc(ident_reply *i, void *arg):
137   cdef SelIdentify id
138   id = <SelIdentify>arg
139   id._dead()
140   if i.type == IDENT_BAD:
141     id.bad()
142   elif i.type == IDENT_ERROR:
143     id.error(i.u.error)
144   elif i.type == IDENT_USERID:
145     id.user(i.u.userid.os, i.u.userid.user)
146
147 ###----- That's all, folks --------------------------------------------------