Revision fa04d422

b/nfdhcp.py
22 22
import os
23 23
import re
24 24
import glob
25
import time
25 26
import logging
26 27
import logging.handlers
27 28
import subprocess
......
52 53
DHCP_DUMMY_SERVER_IP = "1.2.3.4"
53 54

  
54 55
LOG_FORMAT = "%(asctime)-15s %(levelname)-6s %(message)s"
56
PERIODIC_RA_TIMEOUT = 30 # seconds
55 57

  
56 58
DHCPDISCOVER = 1
57 59
DHCPOFFER = 2
......
485 487
        sendp(resp, iface=iface, verbose=False)
486 488
        return 1
487 489

  
490
    def send_periodic_ra(self):
491
        logging.debug("Sending out periodic RAs")
492
        for client in self.clients.values():
493
            iface = client.iface
494
            subnet = self.v6nets[iface]
495
            ifmac = self.get_iface_hw_addr(iface)
496
            ifll = subnet.make_ll64(ifmac)
497
            resp = Ether(src=ifmac)/\
498
                   IPv6(src=str(ifll))/ICMPv6ND_RA(routerlifetime=14400)/\
499
                   ICMPv6NDOptPrefixInfo(prefix=str(subnet.prefix),
500
                                         prefixlen=subnet.prefixlen)
501
            try:
502
                sendp(resp, iface=iface, verbose=False)
503
            except:
504
                logging.debug("Periodic RA on %s failed" % iface)
505

  
488 506
    def serve(self):
489 507
        """ Loop forever, serving DHCP requests
490 508

  
......
493 511

  
494 512
        iwfd = self.notifier._fd
495 513

  
514
        self.send_periodic_ra()
515
        timeout = PERIODIC_RA_TIMEOUT
516

  
496 517
        while True:
497
            rlist, _, xlist = select(self.nfq.keys() + [iwfd], [], [], 1.0)
518
            start = time.time()
519
            rlist, _, xlist = select(self.nfq.keys() + [iwfd], [], [], timeout)
498 520
            # First check if there are any inotify (= configuration change)
499 521
            # events
522
            if not (rlist or xlist):
523
                # We were woken up by a timeout
524
                self.send_periodic_ra()
525
                timeout = PERIODIC_RA_TIMEOUT
526
                continue
527

  
500 528
            if iwfd in rlist:
501 529
                self.notifier.read_events()
502 530
                self.notifier.process_events()
......
505 533
            for fd in rlist:
506 534
                self.nfq[fd].process_pending()
507 535

  
536
            # Calculate the new timeout
537
            timeout = PERIODIC_RA_TIMEOUT - (time.time() - start)
538

  
539
            # Just to be safe we won't miss anything
540
            if timeout <= 0:
541
                self.send_periodic_ra()
542
                timeout = PERIODIC_RA_TIMEOUT
543

  
508 544

  
509 545
if __name__ == "__main__":
510 546
    import optparse

Also available in: Unified diff