Exemplo n.º 1
0
 def __init__(self, *argv, **kwarg):
     if 'family' in kwarg:
         kwarg.pop('family')
     super(IPRSocketMixin, self).__init__(NETLINK_ROUTE, *argv[1:], **kwarg)
     self.marshal = MarshalRtnl()
     self._s_channel = None
     if sys.platform.startswith('linux'):
         self._gate = self._gate_linux
         self.sendto_gate = self._gate_linux
         send_ns = Namespace(self, {'addr_pool': AddrPool(0x10000,
                                                          0x1ffff),
                                    'monitor': False})
         self._sproxy = NetlinkProxy(policy='return', nl=send_ns)
         self._sproxy.pmap = {rtnl.RTM_NEWLINK: proxy_newlink,
                              rtnl.RTM_SETLINK: proxy_setlink}
         if config.kernel < [3, 3, 0]:
             self._recv_ns = Namespace(self,
                                       {'addr_pool': AddrPool(0x20000,
                                                              0x2ffff),
                                        'monitor': False})
             self._sproxy.pmap[rtnl.RTM_DELLINK] = proxy_dellink
             # inject proxy hooks into recv() and...
             self.__recv = self._recv
             self._recv = self._p_recv
             # ... recv_into()
             self._recv_ft = self.recv_ft
             self.recv_ft = self._p_recv_ft
Exemplo n.º 2
0
 def __init__(self,
              fileno=None,
              sndbuf=1048576,
              rcvbuf=1048576,
              all_ns=False):
     super(IPRSocketMixin, self).__init__(NETLINK_ROUTE,
                                          fileno=fileno,
                                          sndbuf=sndbuf,
                                          rcvbuf=rcvbuf,
                                          all_ns=all_ns)
     self.marshal = MarshalRtnl()
     self._s_channel = None
     send_ns = Namespace(self, {
         'addr_pool': AddrPool(0x10000, 0x1ffff),
         'monitor': False
     })
     self._sproxy = NetlinkProxy(policy='return', nl=send_ns)
     self._sproxy.pmap = {
         rtnl.RTM_NEWLINK: proxy_newlink,
         rtnl.RTM_SETLINK: proxy_setlink
     }
     if config.kernel < [3, 3, 0]:
         self._recv_ns = Namespace(self, {
             'addr_pool': AddrPool(0x20000, 0x2ffff),
             'monitor': False
         })
         self._sproxy.pmap[rtnl.RTM_DELLINK] = proxy_dellink
         # inject proxy hooks into recv() and...
         self.__recv = self._recv
         self._recv = self._p_recv
         # ... recv_into()
         self._recv_ft = self.recv_ft
         self.recv_ft = self._p_recv_ft
Exemplo n.º 3
0
 def __init__(self, *argv, **kwarg):
     self.marshal = MarshalRtnl()
     send_ns = Namespace(self, {
         'addr_pool': AddrPool(0x10000, 0x1ffff),
         'monitor': False
     })
     self._sproxy = NetlinkProxy(policy='return', nl=send_ns)
Exemplo n.º 4
0
 def __init__(self, fileno=None):
     super(IPRSocketMixin, self).__init__(NETLINK_ROUTE, fileno=fileno)
     self.marshal = MarshalRtnl()
     self._s_channel = None
     send_ns = Namespace(self, {'addr_pool': AddrPool(0x10000, 0x1ffff),
                                'monitor': False})
     self._sproxy = NetlinkProxy(policy='return', nl=send_ns)
     self._sproxy.pmap = {rtnl.RTM_NEWLINK: proxy_newlink,
                          rtnl.RTM_SETLINK: proxy_setlink}
Exemplo n.º 5
0
 def __init__(self, *argv, **kwarg):
     self._ifc = Ifconfig()
     self.marshal = MarshalRtnl()
     send_ns = Namespace(self, {'addr_pool': AddrPool(0x10000, 0x1ffff),
                                'monitor': False})
     self._sproxy = NetlinkProxy(policy='return', nl=send_ns)
     self._mon_th = None
     self._rtm = None
     self._pfdr, self._pfdw = os.pipe()  # notify external poll/select
     self._ctlr, self._ctlw = os.pipe()  # notify monitoring thread
     self._outq = queue.Queue()
     self._system_lock = threading.Lock()
Exemplo n.º 6
0
 def __init__(self, *argv, **kwarg):
     if 'ssh' in kwarg:
         self._ssh = ['ssh', kwarg.pop('ssh')]
     else:
         self._ssh = []
     self._ifc = Ifconfig(cmd=self._ssh + ['ifconfig', '-a'])
     self._arp = ARP(cmd=self._ssh + ['arp', '-an'])
     self._route = Route(cmd=self._ssh + ['netstat', '-rn'])
     self.marshal = MarshalRtnl()
     send_ns = Namespace(self, {'addr_pool': AddrPool(0x10000, 0x1ffff),
                                'monitor': False})
     self._sproxy = NetlinkProxy(policy='return', nl=send_ns)
     self._mon_th = None
     self._rtm = None
     self._pfdr, self._pfdw = os.pipe()  # notify external poll/select
     self._ctlr, self._ctlw = os.pipe()  # notify monitoring thread
     self._outq = queue.Queue()
     self._system_lock = threading.Lock()
Exemplo n.º 7
0
 def __init__(self, fileno=None, sndbuf=1048576, rcvbuf=1048576,
              all_ns=False):
     super(IPRSocketMixin, self).__init__(NETLINK_ROUTE, fileno=fileno,
                                          sndbuf=sndbuf, rcvbuf=rcvbuf,
                                          all_ns=all_ns)
     self.marshal = MarshalRtnl()
     self._s_channel = None
     send_ns = Namespace(self, {'addr_pool': AddrPool(0x10000, 0x1ffff),
                                'monitor': False})
     self._sproxy = NetlinkProxy(policy='return', nl=send_ns)
     self._sproxy.pmap = {rtnl.RTM_NEWLINK: proxy_newlink,
                          rtnl.RTM_SETLINK: proxy_setlink}
     if config.kernel < [3, 3, 0]:
         self._recv_ns = Namespace(self,
                                   {'addr_pool': AddrPool(0x20000, 0x2ffff),
                                    'monitor': False})
         self._sproxy.pmap[rtnl.RTM_DELLINK] = proxy_dellink
         # inject proxy hooks into recv() and...
         self.__recv = self._recv
         self._recv = self._p_recv
         # ... recv_into()
         self._recv_ft = self.recv_ft
         self.recv_ft = self._p_recv_ft
Exemplo n.º 8
0
class IPRSocketMixin(object):

    def __init__(self, *argv, **kwarg):
        if 'family' in kwarg:
            kwarg.pop('family')
        super(IPRSocketMixin, self).__init__(NETLINK_ROUTE, *argv[1:], **kwarg)
        self.marshal = MarshalRtnl()
        self._s_channel = None
        if sys.platform.startswith('linux'):
            self._gate = self._gate_linux
            self.sendto_gate = self._gate_linux
            send_ns = Namespace(self, {'addr_pool': AddrPool(0x10000,
                                                             0x1ffff),
                                       'monitor': False})
            self._sproxy = NetlinkProxy(policy='return', nl=send_ns)
            self._sproxy.pmap = {rtnl.RTM_NEWLINK: proxy_newlink,
                                 rtnl.RTM_SETLINK: proxy_setlink}
            if config.kernel < [3, 3, 0]:
                self._recv_ns = Namespace(self,
                                          {'addr_pool': AddrPool(0x20000,
                                                                 0x2ffff),
                                           'monitor': False})
                self._sproxy.pmap[rtnl.RTM_DELLINK] = proxy_dellink
                # inject proxy hooks into recv() and...
                self.__recv = self._recv
                self._recv = self._p_recv
                # ... recv_into()
                self._recv_ft = self.recv_ft
                self.recv_ft = self._p_recv_ft

    def bind(self, groups=rtnl.RTMGRP_DEFAULTS, **kwarg):
        super(IPRSocketMixin, self).bind(groups, **kwarg)

    def _gate_linux(self, msg, addr):
        msg.reset()
        msg.encode()
        ret = self._sproxy.handle(msg)
        if ret is not None:
            if ret['verdict'] == 'forward':
                return self._sendto(ret['data'], addr)
            elif ret['verdict'] in ('return', 'error'):
                if self._s_channel is not None:
                    return self._s_channel.send(ret['data'])
                else:
                    msgs = self.marshal.parse(ret['data'])
                    for msg in msgs:
                        seq = msg['header']['sequence_number']
                        if seq in self.backlog:
                            self.backlog[seq].append(msg)
                        else:
                            self.backlog[seq] = [msg]
                    return len(ret['data'])
            else:
                ValueError('Incorrect verdict')

        return self._sendto(msg.data, addr)

    def _p_recv_ft(self, bufsize, flags=0):
        data = self._recv_ft(bufsize, flags)
        ret = proxy_linkinfo(data, self._recv_ns)
        if ret is not None:
            if ret['verdict'] in ('forward', 'error'):
                return ret['data']
            else:
                ValueError('Incorrect verdict')

        return data

    def _p_recv(self, bufsize, flags=0):
        data = self.__recv(bufsize, flags)
        ret = proxy_linkinfo(data, self._recv_ns)
        if ret is not None:
            if ret['verdict'] in ('forward', 'error'):
                return ret['data']
            else:
                ValueError('Incorrect verdict')

        return data