X-Git-Url: https://git.distorted.org.uk/~mdw/mLib-python/blobdiff_plain/20bce5e92b01cd928f26b61be78215117039c561..579d01693c86259110fe7a2c2a6f005f1bdbad5b:/bres.pyx diff --git a/bres.pyx b/bres.pyx new file mode 100644 index 0000000..162ff02 --- /dev/null +++ b/bres.pyx @@ -0,0 +1,114 @@ +# -*-pyrex-*- +# +# $Id$ +# +# Background name resolution +# +# (c) 2005 Straylight/Edgeware +# + +#----- Licensing notice ----------------------------------------------------- +# +# This file is part of the Python interface to mLib. +# +# mLib/Python is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# mLib/Python is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with mLib/Python; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +cdef class SelResolve: + cdef bres_client r + cdef int _activep + cdef _resolved + cdef _failed + def __init__(me, *hunoz, **hukairz): + raise TypeError, 'abstract class' + property activep: + def __get__(me): + return _tobool(me._activep) + def kill(me): + if not me._activep: + raise ValueError, 'already dead' + me._dead() + bres_abort(&me.r) + return me + cdef _dead(me): + me._activep = 0 + me.dead() + def dead(me): + pass + property resolvedproc: + def __get__(me): + return me._resolved + def __set__(me, proc): + me._resolved = _checkcallable(proc, 'resolved proc') + def __del__(me): + me._resolved = None + property failedproc: + def __get__(me): + return me._failed + def __set__(me, proc): + me._failed = _checkcallable(proc, 'failed proc') + def __del__(me): + me._failed = None + def resolved(me, name, aliases, addrs): + return _maybecall(me._resolved, (name, aliases, addrs)) + def failed(me): + return _maybecall(me._failed, ()) + +cdef class SelResolveByName (SelResolve): + def __new__(me, char *name, resolvedproc = None, failedproc = None, + *hunoz, **hukairz): + bres_byname(&me.r, name, _resfunc, me) + me._resolved = _checkcallable(resolvedproc, 'resolved proc') + me._failed = _checkcallable(failedproc, 'failed proc') + me._activep = 1 + def __init__(me, name, resolvedproc = None, failedproc = None): + pass + +cdef class SelResolveByAddr (SelResolve): + def __new__(me, char *addr, resolvedproc = None, failedproc = None, + *hunoz, **hukairz): + cdef in_addr ia + if not inet_aton(addr, &ia): + raise TypeError, 'bad IP address' + bres_byaddr(&me.r, ia, _resfunc, me) + me._resolved = _checkcallable(resolvedproc, 'resolved proc') + me._failed = _checkcallable(failedproc, 'failed proc') + me._activep = 1 + def __init__(me, addr, resolvedproc = None, failedproc = None): + pass + +cdef void _resfunc(hostent *h, void *arg): + cdef SelResolve r + cdef int i + r = arg + r._dead() + if h is NULL: + r.failed(r) + else: + alias = [] + addr = [] + i = 0 + while h.h_aliases[i]: + alias.append(h.h_aliases[i]) + i = i + 1 + i = 0 + while h.h_addr_list[i]: + addr.append(inet_ntoa((h.h_addr_list[i])[0])) + i = i + 1 + r.resolved(h.h_name, alias, addr) + +bres_exec(NULL) +bres_init(&_sel) + +#----- That's all, folks ----------------------------------------------------