CTEST(stunserver, Encode_decode) { StunMessage stunMsg; StunMessage stunResponse; StunMsgId stunId; uint8_t stunBuff[STUN_MAX_PACKET_SIZE]; stunlib_createId(&stunId); sockaddr_initFromString( (struct sockaddr*)&mappedAddr, "193.200.93.152:3478" ); CreateConnectivityBindingResp(&stunMsg, stunId, (struct sockaddr *)&mappedAddr, 1, 1, STUN_MSG_BindResponseMsg, 200, NULL); int len = stunlib_encodeMessage(&stunMsg, (uint8_t*)stunBuff, STUN_MAX_PACKET_SIZE, (unsigned char *) passwd, strlen(passwd), NULL); ASSERT_TRUE(len == 72); ASSERT_TRUE( stunlib_DecodeMessage(stunBuff, len, &stunResponse, NULL, NULL /*stdout for debug*/)); }
/********* Server handling of STUN BIND RESP *************/ bool StunServer_SendConnectivityBindingResp(STUN_CLIENT_DATA* clientData, int32_t globalSocketId, StunMsgId transactionId, const char* password, const struct sockaddr* mappedAddr, const struct sockaddr* dstAddr, uint8_t reqTrnspCnt, uint8_t respTrnspCnt, void* userData, STUN_SENDFUNC sendFunc, int proto, bool useRelay, uint32_t responseCode, DiscussData* discussData) { StunMessage stunRespMsg; /* format */ if ( CreateConnectivityBindingResp(&stunRespMsg, transactionId, mappedAddr, reqTrnspCnt, respTrnspCnt, (responseCode == 200) ? STUN_MSG_BindResponseMsg : STUN_MSG_BindErrorResponseMsg, responseCode, discussData) ) { /* encode and send */ if ( SendConnectivityBindResponse(clientData, globalSocketId, &stunRespMsg, password, dstAddr, userData, sendFunc, proto, useRelay) ) { return true; } } return false; }
ICELIB_Result sendConnectivityResp(void* pUserData, uint32_t userValue1, uint32_t userValue2, uint32_t componentId, int sockfd, int proto, const struct sockaddr* source, const struct sockaddr* destination, const struct sockaddr* MappedAddress, uint16_t errorResponse, StunMsgId transactionId, bool useRelay, const char* pPasswd) { (void)userValue1; (void)userValue2; (void)componentId; (void)source; (void)destination; StunMessage stunMsg; CreateConnectivityBindingResp(&stunMsg, transactionId, MappedAddress, 0, 0, 0, 0, 0, 0, (errorResponse == 200) ? STUN_MSG_BindResponseMsg : STUN_MSG_BindErrorResponseMsg, errorResponse); /* encode */ uint8_t stunBuff[STUN_MAX_PACKET_SIZE]; int stunLen = stunlib_encodeMessage(&stunMsg, (uint8_t*)stunBuff, STUN_MAX_PACKET_SIZE, (unsigned char*)pPasswd, /* md5key **/ pPasswd ? strlen(pPasswd) : 0, /* * keyLen **/ NULL); if (!stunLen) { printf("Failed to encode STUN msg\n"); exit(1); } sendPacket(pUserData, sockfd, stunBuff, stunLen, destination, proto, useRelay, 0); return 0; }