chiark / gitweb /
Release 1.0.5.
[mLib-python] / bres.pyx
CommitLineData
5b1830f3
MW
1### -*-pyrex-*-
2###
3### Background name resolution
4###
5### (c) 2005 Straylight/Edgeware
6###
579d0169 7
5b1830f3
MW
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.
579d0169 25
26cdef 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'
b170d65a
MW
33 def __dealloc__(me):
34 if me._activep:
35 me._dead()
36 bres_abort(&me.r)
579d0169 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
70cdef class SelResolveByName (SelResolve):
376ad06d 71 def __cinit__(me, char *name, resolvedproc = None, failedproc = None,
579d0169 72 *hunoz, **hukairz):
579d0169 73 me._resolved = _checkcallable(resolvedproc, 'resolved proc')
74 me._failed = _checkcallable(failedproc, 'failed proc')
75 me._activep = 1
b170d65a 76 bres_byname(&me.r, name, _resfunc, <void *>me)
579d0169 77 def __init__(me, name, resolvedproc = None, failedproc = None):
78 pass
79
80cdef class SelResolveByAddr (SelResolve):
376ad06d 81 def __cinit__(me, char *addr, resolvedproc = None, failedproc = None,
579d0169 82 *hunoz, **hukairz):
83 cdef in_addr ia
84 if not inet_aton(addr, &ia):
85 raise TypeError, 'bad IP address'
579d0169 86 me._resolved = _checkcallable(resolvedproc, 'resolved proc')
87 me._failed = _checkcallable(failedproc, 'failed proc')
88 me._activep = 1
b170d65a 89 bres_byaddr(&me.r, ia, _resfunc, <void *>me)
579d0169 90 def __init__(me, addr, resolvedproc = None, failedproc = None):
91 pass
92
b51b6cf0 93cdef void _resfunc(hostent *h, void *arg):
579d0169 94 cdef SelResolve r
95 cdef int i
96 r = <SelResolve>arg
97 r._dead()
98 if h is NULL:
b170d65a 99 r.failed()
579d0169 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
113bres_exec(NULL)
114bres_init(&_sel)
115
5b1830f3 116###----- That's all, folks --------------------------------------------------