def set_data_type_buf_array(data_type, length, value_buf): buf = bytearray(length + 4) set_int_to_msg_buf(buf, 0, data_type, 2) set_int_to_msg_buf(buf, 2, length, 2) set_buf_to_msg_buf(buf, 4, value_buf, length) return buf
def method_eotu_ask_user_start(self, ask_user=None): logging.debug("###############EOTU ASK USER server[%s:%d]" % (self.coturn_server[0], self.coturn_server[1])) #初始化报头 msg_buf = bytearray(MESSAGE_MAX_LENGTH) msg_buf_header = stun_init_header(STUN_METHOD_EOTU_ASK_USER, self.my_user) msg_buf = set_buf_to_msg_buf(msg_buf, 0, msg_buf_header, STUN_HEADER_LENGTH) if ask_user == None: ask_user = self.ask_user if ask_user != None: msg = set_data_to_msg_buf(msg_buf, STUN_HEADER_LENGTH, STUN_ATTRIBUTE_ASK_USERID_INFO, ask_user) else: logging.error("ask user is None ,exiting ....") sys.exit() #设置msg_buf长度值 msg_to_send = set_msg_buf_size_final(msg) return msg_to_send
def set_addr_to_msg_buf(msg_buf, sttp, data_type, addr): length = 8 addr_buf = covert_addr_to_buf_array(addr) data_buf = set_data_type_buf_array(data_type, length, addr_buf) msg_buf = set_buf_to_msg_buf(msg_buf, sttp, data_buf, 12) return (msg_buf, sttp + 12)
def method_refresh_start(self): #初始化报头 msg_buf = bytearray(STUN_HEADER_LENGTH) msg_buf_header = stun_init_header(STUN_METHOD_REFRESH, self.my_user) msg_buf = set_buf_to_msg_buf(msg_buf, 0, msg_buf_header, STUN_HEADER_LENGTH) return msg_buf
def set_method_data_to_msg_buf(msg_buf, sttp, data_type, buf): buf_len = len(buf) # no more than 256 if buf_len > 255: print("error ! method send data length over 255 %d ,exiting ...." % buf_len) sys.exit(-1) msg_buf = set_int_to_msg_buf(msg_buf, sttp, data_type, 2) msg_buf = set_int_to_msg_buf(msg_buf, sttp + 2, buf_len, 2) msg_buf = set_buf_to_msg_buf(msg_buf, sttp + 4, buf, buf_len) return (msg_buf, sttp + 4 + buf_len)
def method_create_permission_start(self, peer_addr): #初始化报头 msg_buf = bytearray(MESSAGE_MAX_LENGTH) msg_buf_header = stun_init_header(STUN_METHOD_CREATE_PERMISSION, self.my_user) msg_buf = set_buf_to_msg_buf(msg_buf, 0, msg_buf_header, STUN_HEADER_LENGTH) #添加peer地址 msg = set_data_to_msg_buf(msg_buf, STUN_HEADER_LENGTH, STUN_ATTRIBUTE_XOR_PEER_ADDRESS, peer_addr) #设置msg_buf长度值 msg_to_send = set_msg_buf_size_final(msg) return msg_to_send
def method_send_start(self, send_data, ask_user): #初始化报头 msg_buf = bytearray(MESSAGE_MAX_LENGTH) msg_buf_header = stun_init_header(STUN_METHOD_SEND, self.my_user) msg_buf = set_buf_to_msg_buf(msg_buf, 0, msg_buf_header, STUN_HEADER_LENGTH) #添加peer地址 #msg = set_data_to_msg_buf(msg_buf,STUN_HEADER_LENGTH,STUN_ATTRIBUTE_XOR_PEER_ADDRESS,peer_addr) msg = set_data_to_msg_buf(msg_buf, STUN_HEADER_LENGTH, STUN_ATTRIBUTE_ASK_USERID_INFO, ask_user) #添加发送的数据 msg = set_data_to_msg_buf(msg_buf, msg[1], STUN_ATTRIBUTE_DATA, send_data) #设置msg_buf长度值 msg_to_send = set_msg_buf_size_final(msg) return msg_to_send
def allocation_msg(self, allocation_times=0): logging.debug("################ALLOCATION START ! server[%s:%d]" % (self.coturn_server[0], self.coturn_server[1])) #初始化报头 msg_buf = bytearray(MESSAGE_MAX_LENGTH) if allocation_times == 0: msg_buf_header = stun_init_header(STUN_METHOD_ALLOCATE, self.my_user, set_random=0xffffffff) else: msg_buf_header = stun_init_header(STUN_METHOD_ALLOCATE, self.my_user) msg_buf = set_buf_to_msg_buf(msg_buf, 0, msg_buf_header, STUN_HEADER_LENGTH) #添加本地地址 if self.local_addr != None: msg = set_data_to_msg_buf(msg_buf, STUN_HEADER_LENGTH, STUN_ATTRIBUTE_EOTU_LOCAL_ADDR, self.local_addr) #设置REQUESTED_TRANSPORT 默认UDP , 值为17 msg = set_data_to_msg_buf(msg_buf, msg[1], STUN_ATTRIBUTE_REQUESTED_TRANSPORT, 0x11000000) msg = set_data_to_msg_buf(msg_buf, msg[1], STUN_ATTRIBUTE_REQUESTED_ADDRESS_FAMILY, 0x01000000) #设置msg_buf长度值 msg_to_send = set_msg_buf_size_final(msg) logging.debug("ALLOCATION DATA [%s]" % msg_to_send.hex()) return msg_to_send