コード例 #1
0
ファイル: iprsocket.py プロジェクト: liske/pyroute2
 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
コード例 #2
0
ファイル: windows.py プロジェクト: liske/pyroute2
 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)
コード例 #3
0
 def __init__(self, *argv, **kwarg):
     if 'ssh' in kwarg:
         self._ssh = ['ssh', kwarg.pop('ssh')]
     else:
         self._ssh = []
     async_qsize = kwarg.get('async_qsize')
     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._brd_socket = None
     self._pfdr, self._pfdw = os.pipe()  # notify external poll/select
     self._ctlr, self._ctlw = os.pipe()  # notify monitoring thread
     self._outq = queue.Queue(maxsize=async_qsize or config.async_qsize)
     self._system_lock = threading.Lock()
     self.closed = threading.Event()
コード例 #4
0
ファイル: iprsocket.py プロジェクト: sandcake/pyroute2
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