Esempio n. 1
0
 def __init__(self,
              addr=0x01000000,
              broadcast=0xffffffff,
              ioloop=None,
              control=None,
              secret=None):
     self.pid = os.getpid()
     self._stop_event = threading.Event()
     self._reload_event = threading.Event()
     self.shutdown_flag = threading.Event()
     self.addr = addr
     self.broadcast = broadcast
     self.marshal = MarshalEnv()
     self.ports = AddrPool(minaddr=0xff)
     self.nonces = AddrPool(minaddr=0xfff)
     self.active_sys = {}
     self.local = {}
     self.links = {}
     self.remote = {}
     self.discover = {}
     # fd lists for select()
     self._rlist = set()
     self._wlist = set()
     self._xlist = set()
     # routing
     self.masquerade = {}      # {int: MasqRecord()...}
     self.packet_ids = {}      # {int: CacheRecord()...}
     self.clients = set()      # set(socket, socket...)
     self.servers = set()      # set(socket, socket...)
     self.controls = set()     # set(socket, socket...)
     self.sockets = {}
     self.subscribe = {}
     self.providers = {}
     # modules = { IPRCMD_STOP: {'access': access.ADMIN,
     #                           'command': <function>},
     #             IPRCMD_...: ...
     self.modules = dict(((x.target, {'access': x.level,
                                      'command': x.command})
                          for x in modules))
     self._cid = list(range(1024))
     # secret; write non-zero byte as terminator
     if secret:
         self.secret = secret
     else:
         self.secret = os.urandom(15)
         self.secret += b'\xff'
     self.uuid = uuid.uuid4()
     # masquerade cache expiration
     self._expire_thread = Cache(self._stop_event)
     self._expire_thread.register_map(self.masquerade, self.nonces.free)
     self._expire_thread.register_map(self.packet_ids)
     self._expire_thread.setDaemon(True)
     if ioloop:
         self.ioloop = ioloop
         self.standalone = False
     else:
         self.ioloop = IOLoop()
         self.standalone = True
     if control:
         self.add_client(control)
         self.controls.add(control)
         self.ioloop.register(control, self.route, defer=True)
Esempio n. 2
0
    def __init__(self,
                 debug=False,
                 timeout=3,
                 do_connect=False,
                 host=None,
                 key=None,
                 cert=None,
                 ca=None,
                 addr=None,
                 fork=False,
                 secret=None):
        addr = addr or uuid32()
        self._timeout = timeout
        self.default_broker = addr
        self.default_dport = 0
        self.uids = set()
        self.listeners = {}  # {nonce: Queue(), ...}
        self.callbacks = []  # [(predicate, callback, args), ...]
        self.debug = debug
        self.cid = None
        self.cmd_nonce = AddrPool(minaddr=0xf, maxaddr=0xfffe)
        self.nonce = AddrPool(minaddr=0xffff, maxaddr=0xffffffff)
        self.emarshal = MarshalEnv()
        self.save = None
        if self.marshal is not None:
            self.marshal.debug = debug
            self.marshal = self.marshal()
        self.buffers = Queue.Queue()
        self._mirror = False
        self.host = host

        self.ioloop = IOLoop()

        self._brs, self.bridge = pairPipeSockets()
        # To fork or not to fork?
        #
        # It depends on how you gonna use RT netlink sockets
        # in your application. Performance can also differ,
        # and sometimes forked broker can even speedup your
        # application -- keep in mind Python's GIL
        if fork:
            # Start the I/O broker in a separate process,
            # so you can use multiple RT netlink sockets in
            # one application -- it does matter, if your
            # application already uses some RT netlink
            # library and you want to smoothly try and
            # migrate to the pyroute2
            self.forked_broker = Process(target=self._start_broker,
                                         args=(fork, secret))
            self.forked_broker.start()
        else:
            # Start I/O broker as an embedded object, so
            # the RT netlink socket will be opened in the
            # same process. Technically, you can open
            # multiple RT netlink sockets within one process,
            # but only the first one will receive all the
            # answers -- so effectively only one socket per
            # process can be used.
            self.forked_broker = None
            self._start_broker(fork, secret)
        self.ioloop.start()
        self.ioloop.register(self.bridge, self._route, defer=True)
        if do_connect:
            path = urlparse.urlparse(host).path
            (self.default_link,
             self.default_peer) = self.connect(self.host, key, cert, ca)
            self.default_dport = self.discover(self.default_target or path,
                                               self.default_peer)