def register(self, eid, na): icn_packet = ICNPacket() icn_packet.setHeader("c11e70000000000000000000000c11e7", "92500000000000000000000000000925", "00") cmd_type = binascii.a2b_hex("01") eid_hex = binascii.a2b_hex(eid) na_hex = binascii.a2b_hex(na) icn_packet.setPayload(cmd_type + eid_hex + na_hex) icn_packet.fill_packet() icn_packet.print_packet() reply = self.send(icn_packet) cmd_type = int(binascii.b2a_hex(reply.payload[:1]), 16) ack = int(binascii.b2a_hex(reply.payload[17:18]), 16) if cmd_type == 2 and ack == 1: return True else: return False
def register(self, hrn): icn_packet = ICNPacket() icn_packet.setHeader("226cf119b78825f1720cf2ca485c2d85", "00000000000000000000000000000000", "00") cmd_type = binascii.a2b_hex("01") hrn_hex = hrn.encode("utf-8") print(hrn_hex) hrn_len = len(hrn_hex) icn_packet.setPayload(cmd_type + binascii.a2b_hex(int2byte(hrn_len, 8)) + hrn_hex) icn_packet.fill_packet() icn_packet.print_packet() reply = self.send(icn_packet) cmd_type = binascii.b2a_hex(reply.payload[:1]) ack = binascii.b2a_hex(reply.payload[1:2]) cmd_type = int(binascii.b2a_hex(reply.payload[:1]), 16) ack = int(binascii.b2a_hex(reply.payload[1:2]), 16) if cmd_type == 2 and ack == 1: eid = binascii.b2a_hex(reply.payload[2:18]) return eid.decode("utf-8") else: return None
def query_na(self, eid): icn_packet = ICNPacket() icn_packet.setHeader("c11e70000000000000000000000c11e7", "92500000000000000000000000000925", "00") cmd_type = binascii.a2b_hex("03") eid_hex = binascii.a2b_hex(eid) icn_packet.setPayload(cmd_type + eid_hex) icn_packet.fill_packet() reply = self.send(icn_packet) cmd_type = int(binascii.b2a_hex(reply.payload[:1]), 16) na = binascii.b2a_hex(reply.payload[17:21]).decode("utf-8") if cmd_type == 4: return na else: return None
def query_hrn(self, eid): icn_packet = ICNPacket() icn_packet.setHeader("226cf119b78825f1720cf2ca485c2d85", "00000000000000000000000000000000", "00") cmd_type = binascii.a2b_hex("05") hrn_hex = binascii.a2b_hex(eid) icn_packet.setPayload(cmd_type + hrn_hex) icn_packet.fill_packet() reply = self.send(icn_packet) cmd_type = int(binascii.b2a_hex(reply.payload[:1]), 16) ack = int(binascii.b2a_hex(reply.payload[1:2]), 16) if cmd_type == 6 and ack == 1: hrn_len = int(binascii.b2a_hex(reply.payload[2:3]), 16) hrn = reply.payload[3:3 + hrn_len].decode("utf-8") return hrn else: return None
def send(self, packet): data = packet.icn2byte() if len(data) > self.UDP_MTU: return None sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.sendto(data, (self.ncs_address, self.ncs_port)) sock.settimeout(1) reply = None try: ack, remote_address = sock.recvfrom(self.UDP_MTU) print(ack) reply = ICNPacket() reply.byte2icn(ack) reply.print_packet() except socket.timeout: logger.error("Timeout") finally: sock.close() return reply
def request_handler(self, data, address): packet = ICNPacket() packet.byte2icn(data) packet.print_packet() payload = packet.payload cmd_type = int(binascii.b2a_hex(payload[:1]), 16) reply = ICNPacket() reply.setHeader("92500000000000000000000000000925", "c11e70000000000000000000000c11e7", "00") if cmd_type == 1: reply.setPayload(self.register(payload)) elif cmd_type == 3: reply.setPayload(self.query_na(payload)) reply.fill_packet() reply.print_packet() self.send(reply, address)