/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

mandosclient
        changed to argp

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
 
 
437
        line = self.request.makefile().readline()
 
438
        logger.debug(u"Protocol version: %r", line)
 
439
        try:
 
440
            if int(line.strip().split()[0]) > 1:
 
441
                raise RuntimeError
 
442
        except (ValueError, IndexError, RuntimeError), error:
 
443
            logger.error(u"Unknown protocol version: %s", error)
 
444
            return
 
445
        
436
446
        session = gnutls.connection.ClientSession\
437
447
                  (self.request, gnutls.connection.X509Credentials())
438
448
        # Note: gnutls.connection.X509Credentials is really a generic
509
519
        """This overrides the normal server_bind() function
510
520
        to bind to an interface if one was specified, and also NOT to
511
521
        bind to an address or port if they were not specified."""
512
 
        if self.settings["interface"] != avahi.IF_UNSPEC:
 
522
        if self.settings["interface"]:
513
523
            # 25 is from /usr/include/asm-i486/socket.h
514
524
            SO_BINDTODEVICE = getattr(socket, "SO_BINDTODEVICE", 25)
515
525
            try:
592
602
                        unicode(error))
593
603
        raise AvahiGroupError("State changed: %s", str(error))
594
604
 
595
 
def if_nametoindex(interface, _func=[None]):
 
605
def if_nametoindex(interface):
596
606
    """Call the C function if_nametoindex(), or equivalent"""
597
 
    if _func[0] is not None:
598
 
        return _func[0](interface)
 
607
    global if_nametoindex
599
608
    try:
600
609
        if "ctypes.util" not in sys.modules:
601
610
            import ctypes.util
602
 
        while True:
603
 
            try:
604
 
                libc = ctypes.cdll.LoadLibrary\
605
 
                       (ctypes.util.find_library("c"))
606
 
                func[0] = libc.if_nametoindex
607
 
                return _func[0](interface)
608
 
            except IOError, e:
609
 
                if e != errno.EINTR:
610
 
                    raise
 
611
        if_nametoindex = ctypes.cdll.LoadLibrary\
 
612
            (ctypes.util.find_library("c")).if_nametoindex
611
613
    except (OSError, AttributeError):
612
614
        if "struct" not in sys.modules:
613
615
            import struct
614
616
        if "fcntl" not in sys.modules:
615
617
            import fcntl
616
 
        def the_hard_way(interface):
 
618
        def if_nametoindex(interface):
617
619
            "Get an interface index the hard way, i.e. using fcntl()"
618
620
            SIOCGIFINDEX = 0x8933  # From /usr/include/linux/sockios.h
619
621
            s = socket.socket()
622
624
            s.close()
623
625
            interface_index = struct.unpack("I", ifreq[16:20])[0]
624
626
            return interface_index
625
 
        _func[0] = the_hard_way
626
 
        return _func[0](interface)
 
627
    return if_nametoindex(interface)
627
628
 
628
629
 
629
630
def daemon(nochdir, noclose):
699
700
    server_settings["debug"] = server_config.getboolean\
700
701
                               (server_section, "debug")
701
702
    del server_config
702
 
    if not server_settings["interface"]:
703
 
        server_settings["interface"] = avahi.IF_UNSPEC
704
703
    
705
704
    # Override the settings from the config file with command line
706
705
    # options, if set.
724
723
    global service
725
724
    service = AvahiService(name = server_settings["servicename"],
726
725
                           type = "_mandos._tcp", );
 
726
    if server_settings["interface"]:
 
727
        service.interface = if_nametoindex(server_settings["interface"])
727
728
    
728
729
    global main_loop
729
730
    global bus
794
795
                                clients=clients)
795
796
    # Find out what port we got
796
797
    service.port = tcp_server.socket.getsockname()[1]
797
 
    logger.debug(u"Now listening on port %d", service.port)
 
798
    logger.debug(u"Now listening on address %r, port %d, flowinfo %d,"
 
799
                 u" scope_id %d" % tcp_server.socket.getsockname())
798
800
    
799
 
    if not server_settings["interface"]:
800
 
        service.interface = if_nametoindex\
801
 
                            (server_settings["interface"])
 
801
    #service.interface = tcp_server.socket.getsockname()[3]
802
802
    
803
803
    try:
804
804
        # From the Avahi example code