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