3 ### Background name resolution
5 ### (c) 2005 Straylight/Edgeware
8 ###----- Licensing notice ---------------------------------------------------
10 ### This file is part of the Python interface to mLib.
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.
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.
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.
26 cdef class SelResolve:
27 """Abstract superclass for background name resolution."""
32 def __init__(me, *hunoz, **hukairz):
33 raise TypeError, 'abstract class'
39 """BR.activep: is lookup still waiting?"""
41 return _tobool(me._activep)
43 """BR.kill(): cancel in-progress lookup"""
45 raise ValueError, 'already dead'
53 """BR.dead(): called when lookup completes or is cancelled"""
55 property resolvedproc:
56 """BR.resolvedproc -> FUNC: call FUNC(NAME, ALIASES, ADDRS) when ok"""
59 def __set__(me, proc):
60 me._resolved = _checkcallable(proc, 'resolved proc')
64 """BR.failedproc -> FUNC: call FUNC() when lookup fails"""
67 def __set__(me, proc):
68 me._failed = _checkcallable(proc, 'failed proc')
71 def resolved(me, name, aliases, addrs):
72 """BR.resolved(NAME, ALIASES, ADDRS): called when lookup completes"""
73 return _maybecall(me._resolved, (name, aliases, addrs))
75 """BR.failed(): called when lookup fails"""
76 return _maybecall(me._failed, ())
78 cdef class SelResolveByName (SelResolve):
80 Resolve a hostname to an IP address, asynchronously.
82 SelResolveByName(NAME, [resolvedproc = None], [failedproc = None])
84 Calls RESOLVEDPROC(NAME, ALIASES, ADDRS) on success, or FAILEDPROC() on
87 def __cinit__(me, char *name, resolvedproc = None, failedproc = None,
89 me._resolved = _checkcallable(resolvedproc, 'resolved proc')
90 me._failed = _checkcallable(failedproc, 'failed proc')
92 bres_byname(&me.r, name, _resfunc, <void *>me)
93 def __init__(me, name, resolvedproc = None, failedproc = None):
96 cdef class SelResolveByAddr (SelResolve):
98 Resolve an IPv4 address to a hostname, asynchronously.
100 SelResolveByAddr(ADDR, [resolvedproc = None], [failedproc = None])
102 Calls RESOLVEDPROC(NAME, ALIASES, ADDRS) on success, or FAILEDPROC() on
105 def __cinit__(me, char *addr, resolvedproc = None, failedproc = None,
108 if not inet_aton(addr, &ia):
109 raise TypeError, 'bad IP address'
110 me._resolved = _checkcallable(resolvedproc, 'resolved proc')
111 me._failed = _checkcallable(failedproc, 'failed proc')
113 bres_byaddr(&me.r, ia, _resfunc, <void *>me)
114 def __init__(me, addr, resolvedproc = None, failedproc = None):
117 cdef void _resfunc(hostent *h, void *arg):
128 while h.h_aliases[i]:
129 alias.append(h.h_aliases[i])
132 while h.h_addr_list[i]:
133 addr.append(inet_ntoa((<in_addr *>h.h_addr_list[i])[0]))
135 r.resolved(h.h_name, alias, addr)
140 ###----- That's all, folks --------------------------------------------------