QString StunClient::getStunAddress(QByteArray resp, uint16_t attr_type) { stun_buffer buf; u08bits addr_buff[STUN_BUFFER_SIZE] = {0}; buf.len = resp.length(); memcpy(buf.buf, resp.data(), resp.length()); uint16_t t_attr_type; const u08bits *attr_value; int attr_len; ioa_addr stun_addr; stun_attr_ref raw_attr = stun_attr_get_first_str(buf.buf, buf.len); for ( ; raw_attr ; raw_attr = stun_attr_get_next_str(buf.buf, buf.len, raw_attr)) { t_attr_type = stun_attr_get_type(raw_attr); attr_value = stun_attr_get_value(raw_attr); attr_len = stun_attr_get_len(raw_attr); if (t_attr_type == attr_type) { stun_attr_get_addr_str(buf.buf, buf.len, raw_attr, &stun_addr, NULL); addr_to_string(&stun_addr, (u08bits*)addr_buff); break; } } return QString((char*)addr_buff); }
stun_attr_ref stun_attr_get_first(const stun_buffer* buf) { return stun_attr_get_first_str(buf->buf, (size_t)(buf->len)); }
void StunClient::debugStunResponse(QByteArray resp) { u08bits rbuf[STUN_BUFFER_SIZE]; size_t rlen = 0; stun_buffer buf; u08bits addr_buff[STUN_BUFFER_SIZE] = {0}; rlen = resp.length(); memcpy(rbuf, resp.data(), resp.length()); buf.len = resp.length(); memcpy(buf.buf, resp.data(), resp.length()); if (!stun_is_command_message(&buf)) { qDebug()<<resp.length()<<("The response is not a STUN message\n"); return; } if (!stun_is_response(&buf)) { qDebug()<<resp.length()<<("The response is not a reponse message\n"); // return; } u16bits stun_method; u16bits stun_msg_type; stun_method = stun_get_method_str(buf.buf, buf.len); stun_msg_type = stun_get_msg_type_str(buf.buf, buf.len); qDebug()<<"method:"<<stun_method<<getMethodName(stun_method)<<",msg type:"<<stun_msg_type; int attr_type; const u08bits *attr_value; int attr_len; char relayed_addr_str[32] = {0}; char mapped_addr_str[32] = {0}; char addr_str[32] = {0}; ioa_addr relayed_addr; ioa_addr mapped_addr; ioa_addr stun_addr; uint32_t lifetime = 0; uint32_t bandwidth = 0; stun_attr_ref raw_attr = stun_attr_get_first_str(buf.buf, buf.len); for ( ; raw_attr ; raw_attr = stun_attr_get_next_str(buf.buf, buf.len, raw_attr)) { attr_type = stun_attr_get_type(raw_attr); attr_value = stun_attr_get_value(raw_attr); attr_len = stun_attr_get_len(raw_attr); switch (attr_type) { case STUN_ATTRIBUTE_SOFTWARE: qDebug()<<"attr software:"<<QByteArray((char*)attr_value, attr_len); break; case STUN_ATTRIBUTE_XOR_MAPPED_ADDRESS: stun_attr_get_addr_str(buf.buf, buf.len, raw_attr, &mapped_addr, NULL); addr_to_string(&mapped_addr, (u08bits*)mapped_addr_str); qDebug()<<"mapped addr:"<<mapped_addr_str; break; case STUN_ATTRIBUTE_XOR_RELAYED_ADDRESS: stun_attr_get_addr_str(buf.buf, buf.len, raw_attr, &relayed_addr, NULL); addr_to_string(&relayed_addr, (u08bits*)relayed_addr_str); qDebug()<<"relayed addr:"<<relayed_addr_str; break; case STUN_ATTRIBUTE_MESSAGE_INTEGRITY: qDebug()<<"message integrity:"<<attr_len<<QByteArray((char*)attr_value, attr_len).toHex(); break; case STUN_ATTRIBUTE_LIFETIME: memcpy(&lifetime, attr_value, attr_len); lifetime = nswap32(lifetime); qDebug()<<"lifetime:"<<lifetime; break; case STUN_ATTRIBUTE_BANDWIDTH: memcpy(&bandwidth, attr_value, attr_len); bandwidth = nswap32(bandwidth); qDebug()<<"bandwidth:"<<bandwidth; break; case STUN_ATTRIBUTE_XOR_PEER_ADDRESS: stun_attr_get_addr_str(buf.buf, buf.len, raw_attr, &stun_addr, NULL); addr_to_string(&stun_addr, (u08bits*)addr_str); qDebug()<<"xor peer addr:"<<addr_str; break; case STUN_ATTRIBUTE_DATA: qDebug()<<"attr data len:"<<attr_len<<QString(QByteArray((char*)attr_value + 4, attr_len - 4)).left(50)<<"..."; break; default: qDebug()<<"unkown attr:"<<attr_type<<attr_len; break; } } }