UDP_IP = '127.0.0.1' UDP_PORT = 33201 m_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) m_sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2) u_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) u_sock.settimeout(5) u_sock.bind((UDP_IP, UDP_PORT)) head = bytes([0x01, # handle 0x00, # tx opts 0x1c, 0xaa, # sensor panid, little endian 0x00, 0x00]) # sensor short addr, little endian dh = ECDH() # test private key (RFC 5114 test vector) dh.private_key = int('814264145F2F56F2E96A8E337A1284993FAF432A5ABCE59E867B7291D507A3AF', 16) dh.gen_public_key() dap = b''.join([x.to_bytes(length=32, byteorder='big') for x in dh.public_key]) # msdu = mID + pubkey msdu = bytes([0x01]) + dap mpdu = bytes([len(msdu)]) + msdu REQ = bytes([0x17]) + head + mpdu REQ = bytes([len(REQ)]) + REQ try: m_sock.sendto(REQ, (MCAST_GRP, MCAST_PORT))
MCAST_PORT = 5007 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) sock.bind(('', MCAST_PORT)) mreq = struct.pack("4sl", socket.inet_aton(MCAST_GRP), socket.INADDR_ANY) sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq) while True: try: data, addr = sock.recvfrom(1024) other_key = tuple([int.from_bytes(x, byteorder='big') for x in [data[:32], data[32:]]]) dh = ECDH() if dh.check_public_key(other_key): dh.gen_private_key() dh.gen_public_key() ser_pub_key = b''.join([x.to_bytes(length=32, byteorder='big') for x in dh.public_key]) sock.sendto(ser_pub_key, addr) dh.gen_secret(other_key) print(hexlify(dh.gen_key())) else: sock.sendto(bytes('key error', 'UTF-8'), addr) print('key error')
def run(self): data = self.data # TODO: this "if" hasn't been tested if len(data) == 0 or len(data) != data[0] + 1: msg = wdc_error msg[2] = WRONG_CMD send_udp(msg, *(self.srv_udp_sock)) logging.error('bad command') return if data[1] == 0x10: # syn TDMA logging.debug('sync-ing WDC') elif data[1] == 0x11: # start TDMA logging.debug('starting TDMA') wdc_get_tdma_res = bytearray(24) wdc_get_tdma_res[0] = data[0] + 1 wdc_get_tdma_res[1] = 0x16 wdc_get_tdma_res[2] = 0x01 # running wdc_get_tdma_res[3:] = data[2:] write_serial(wdc_get_tdma_res, status_file) msg = ack msg[1] = 0x12 # START_TDMA_REQ_ACK send_udp(msg, *self.srv_udp_sock) elif data[1] == 0x13: # stop TDMA logging.debug('stopping TDMA') msg = ack msg[1] = 0x14 # STOP_TDMA_REQ_ACK send_udp(msg, *self.srv_udp_sock) elif data[1] == 0x15: # TDMA status logging.debug('sending TDMA status response') wdc_get_tdma_res = read_serial(status_file) msg = wdc_get_tdma_res send_udp(msg, *self.srv_udp_sock) elif data[1] == 0x17: # data request logging.debug('data request {}'.format(data)) if data[2] in range(1, 9): # key management messages ## TEST TEST TEST TEST TODO #params = data[3:196] dap = data[3:] if len(dap) != 64: logging.error('wrong public key length') return other_key = tuple([int.from_bytes(x, byteorder='big') for x in [dap[:32], dap[32:]]]) ecdh = ECDH() if ecdh.check_public_key(other_key): ecdh.gen_private_key() ecdh.gen_public_key() ser_pub_key = b''.join([x.to_bytes(length=32, byteorder='big') for x in ecdh.public_key]) ecdh.gen_secret(other_key) send_udp(ser_pub_key, *(self.srv_udp_sock)) logging.info('key: {}'.format(hexlify(ecdh.gen_key()))) elif data[2] in [0x09, 0x0A]: # application unicast, broadcast pass else: msg = wdc_error msg[2] = WRONG_CMD send_udp(msg, *(self.srv_udp_sock)) logging.error('wrong MAC_DATA_REQUEST mID') else: msg = wdc_error msg[2] = WRONG_CMD