def tunnel_listp(devname): s = NetlinkSocket() s.bind() msg = ifinfmsg() nonce = 123 # fill the protocol-specific fields msg['index'] = 85 # index of the interface msg['family'] = 18 # address family # attach NLA -- it MUST be a list / mutable msg['attrs'] = [['CTRL_ATTR_FAMILY_NAME', "gtp0"]] # fill generic netlink fields msg['header']['sequence_number'] = nonce # an unique seq number msg['header']['pid'] = os.getpid() msg['header']['type'] = GENL_ID_CTRL msg['header']['flags'] = NLM_F_REQUEST |\ NLM_F_ACK # encode the packet msg.encode() # send the buffer s.sendto(msg.data, (0, 0)) s.get() s.close() l = 1 print("tthis is a list; {}".format(l)) return list
def bind(self, proto, msg_class, groups=0, pid=0): ''' Bind the socket and performs generic netlink proto lookup. The `proto` parameter is a string, like "TASKSTATS", `msg_class` is a class to parse messages with. ''' NetlinkSocket.bind(self, groups, pid) self.marshal.msg_map[GENL_ID_CTRL] = ctrlmsg self.prid = self.get_protocol_id(proto) self.marshal.msg_map[self.prid] = msg_class
def test_ports_auto(self): # create two sockets s1 = NetlinkSocket() s2 = NetlinkSocket() # both bind() should succeed s1.bind() s2.bind() # check that ports are different assert s1.port != s2.port s1.close() s2.close()
def bind(self, mode=IPQ_COPY_PACKET): ''' Bind the socket and performs IPQ mode configuration. The only parameter is mode, the default value is IPQ_COPY_PACKET (copy all the packet data). ''' NetlinkSocket.bind(self, groups=0, pid=0) self.register_policy(MarshalIPQ.msg_map) msg = ipq_mode_msg() msg['value'] = mode msg['range'] = IPQ_MAX_PAYLOAD msg['header']['type'] = IPQM_MODE msg['header']['flags'] = NLM_F_REQUEST msg.encode() self.sendto(msg.data, (0, 0))
def bind(self, proto, msg_class, groups=0, pid=None, **kwarg): ''' Bind the socket and performs generic netlink proto lookup. The `proto` parameter is a string, like "TASKSTATS", `msg_class` is a class to parse messages with. ''' NetlinkSocket.bind(self, groups, pid, **kwarg) self.marshal.msg_map[GENL_ID_CTRL] = ctrlmsg msg = self.discovery(proto) self.prid = msg.get_attr('CTRL_ATTR_FAMILY_ID') self.mcast_groups = \ dict([(x.get_attr('CTRL_ATTR_MCAST_GRP_NAME'), x.get_attr('CTRL_ATTR_MCAST_GRP_ID')) for x in msg.get_attr('CTRL_ATTR_MCAST_GROUPS', [])]) self.marshal.msg_map[self.prid] = msg_class
def test_no_free_ports(self): require_user('root') # create and bind 1024 sockets ports = [NetlinkSocket() for x in range(1024)] for port in ports: port.bind() # create an extra socket fail = NetlinkSocket() try: # bind must fail with KeyError: no free ports available fail.bind() except KeyError: pass # cleanup for port in ports: port.close() try: # failed socket shouldn't permit close() fail.close() except AssertionError: pass
def test_ports_fail(self): s1 = NetlinkSocket(port=0x10) s2 = NetlinkSocket(port=0x10) # check if ports are set assert s1.port == s2.port # bind the first socket, must succeed s1.bind() # bind the second, must fail try: s2.bind() except socket.error as e: # but it must fail only with errno == 98 if e.errno == 98: pass # check the first socket is bound assert s1.getsockname()[0] != 0 # check the second socket is not bound assert s2.getsockname()[0] == 0 s1.close()
def test_no_free_ports(self): require_user("root") # create and bind 1024 sockets ports = [NetlinkSocket() for x in range(1024)] for port in ports: port.bind() # create an extra socket fail = NetlinkSocket() try: # bind must fail with KeyError: no free ports available fail.bind() except KeyError: pass # cleanup for port in ports: port.close() try: # failed socket shouldn't permit close() fail.close() except AssertionError: pass