chiark / gitweb /
assoc.pyx, sym.pyx: Mark arguments as `not None'.
[mLib-python] / bres.pyx
1 ### -*-pyrex-*-
2 ###
3 ### Background name resolution
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 cdef class SelResolve:
27   cdef bres_client r
28   cdef int _activep
29   cdef _resolved
30   cdef _failed
31   def __init__(me, *hunoz, **hukairz):
32     raise TypeError, 'abstract class'
33   def __dealloc__(me):
34     if me._activep:
35       me._dead()
36       bres_abort(&me.r)
37   property activep:
38     def __get__(me):
39       return _tobool(me._activep)
40   def kill(me):
41     if not me._activep:
42       raise ValueError, 'already dead'
43     me._dead()
44     bres_abort(&me.r)
45     return me
46   cdef _dead(me):
47     me._activep = 0
48     me.dead()
49   def dead(me):
50     pass
51   property resolvedproc:
52     def __get__(me):
53       return me._resolved
54     def __set__(me, proc):
55       me._resolved = _checkcallable(proc, 'resolved proc')
56     def __del__(me):
57       me._resolved = None
58   property failedproc:
59     def __get__(me):
60       return me._failed
61     def __set__(me, proc):
62       me._failed = _checkcallable(proc, 'failed proc')
63     def __del__(me):
64       me._failed = None
65   def resolved(me, name, aliases, addrs):
66     return _maybecall(me._resolved, (name, aliases, addrs))
67   def failed(me):
68     return _maybecall(me._failed, ())
69
70 cdef class SelResolveByName (SelResolve):
71   def __cinit__(me, char *name, resolvedproc = None, failedproc = None,
72               *hunoz, **hukairz):
73     me._resolved = _checkcallable(resolvedproc, 'resolved proc')
74     me._failed = _checkcallable(failedproc, 'failed proc')
75     me._activep = 1
76     bres_byname(&me.r, name, _resfunc, <void *>me)
77   def __init__(me, name, resolvedproc = None, failedproc = None):
78     pass
79
80 cdef class SelResolveByAddr (SelResolve):
81   def __cinit__(me, char *addr, resolvedproc = None, failedproc = None,
82               *hunoz, **hukairz):
83     cdef in_addr ia
84     if not inet_aton(addr, &ia):
85       raise TypeError, 'bad IP address'
86     me._resolved = _checkcallable(resolvedproc, 'resolved proc')
87     me._failed = _checkcallable(failedproc, 'failed proc')
88     me._activep = 1
89     bres_byaddr(&me.r, ia, _resfunc, <void *>me)
90   def __init__(me, addr, resolvedproc = None, failedproc = None):
91     pass
92
93 cdef void _resfunc(hostent *h, void *arg):
94   cdef SelResolve r
95   cdef int i
96   r = <SelResolve>arg
97   r._dead()
98   if h is NULL:
99     r.failed()
100   else:
101     alias = []
102     addr = []
103     i = 0
104     while h.h_aliases[i]:
105       alias.append(h.h_aliases[i])
106       i = i + 1
107     i = 0
108     while h.h_addr_list[i]:
109       addr.append(inet_ntoa((<in_addr *>h.h_addr_list[i])[0]))
110       i = i + 1
111     r.resolved(h.h_name, alias, addr)
112
113 bres_exec(NULL)
114 bres_init(&_sel)
115
116 ###----- That's all, folks --------------------------------------------------