/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to server.py

Added support for protocol version handling

Show diffs side-by-side

added added

removed removed

Lines of Context:
433
433
    def handle(self):
434
434
        logger.debug(u"TCP connection from: %s",
435
435
                     unicode(self.client_address))
436
 
        session = gnutls.connection.ClientSession\
437
 
                  (self.request, gnutls.connection.X509Credentials())
438
 
        
439
 
        line = self.request.makefile().readline()
440
 
        logger.debug(u"Protocol version: %r", line)
 
436
 
 
437
        line = self.socket.makefile().readline()
441
438
        try:
442
439
            if int(line.strip().split()[0]) > 1:
443
440
                raise RuntimeError
445
442
            logger.error(u"Unknown protocol version: %s", error)
446
443
            return
447
444
        
 
445
        session = gnutls.connection.ClientSession\
 
446
                  (self.request, gnutls.connection.X509Credentials())
448
447
        # Note: gnutls.connection.X509Credentials is really a generic
449
448
        # GnuTLS certificate credentials object so long as no X.509
450
449
        # keys are added to it.  Therefore, we can use it here despite
602
601
                        unicode(error))
603
602
        raise AvahiGroupError("State changed: %s", str(error))
604
603
 
605
 
def if_nametoindex(interface):
 
604
def if_nametoindex(interface, _func=[None]):
606
605
    """Call the C function if_nametoindex(), or equivalent"""
607
 
    global if_nametoindex
 
606
    if _func[0] is not None:
 
607
        return _func[0](interface)
608
608
    try:
609
609
        if "ctypes.util" not in sys.modules:
610
610
            import ctypes.util
611
 
        if_nametoindex = ctypes.cdll.LoadLibrary\
612
 
            (ctypes.util.find_library("c")).if_nametoindex
 
611
        while True:
 
612
            try:
 
613
                libc = ctypes.cdll.LoadLibrary\
 
614
                       (ctypes.util.find_library("c"))
 
615
                _func[0] = libc.if_nametoindex
 
616
                return _func[0](interface)
 
617
            except IOError, e:
 
618
                if e != errno.EINTR:
 
619
                    raise
613
620
    except (OSError, AttributeError):
614
621
        if "struct" not in sys.modules:
615
622
            import struct
616
623
        if "fcntl" not in sys.modules:
617
624
            import fcntl
618
 
        def if_nametoindex(interface):
 
625
        def the_hard_way(interface):
619
626
            "Get an interface index the hard way, i.e. using fcntl()"
620
627
            SIOCGIFINDEX = 0x8933  # From /usr/include/linux/sockios.h
621
628
            s = socket.socket()
624
631
            s.close()
625
632
            interface_index = struct.unpack("I", ifreq[16:20])[0]
626
633
            return interface_index
627
 
    return if_nametoindex(interface)
 
634
        _func[0] = the_hard_way
 
635
        return _func[0](interface)
628
636
 
629
637
 
630
638
def daemon(nochdir, noclose):