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