我的STUN绑定请求出错了

时间:2016-12-02 10:56:53

标签: stun

我将udp请求发送到int stun_socket = socket(AF_INET, SOCK_DGRAM, 0); struct sockaddr_in stun_client; memset(&stun_client, 0, sizeof(stun_client)); stun_client.sin_family = AF_INET; stun_client.sin_port = htons(local_port); int rc = bind(stun_socket, (struct sockaddr *)&stun_client, sizeof(stun_client)); struct sockaddr_in stun_server; memset(&stun_server, 0, sizeof(stun_server)); stun_server.sin_family = AF_INET; stun_server.sin_port = htons(remote_port); inet_pton(AF_INET, server, &stun_server.sin_addr); typedef struct stun_header_tag { uint16_t message_type; uint16_t message_length; unsigned char transaction_id[16]; } stun_header_t; stun_header_t header; header.message_type = htons(0x0001); /* Binding Request */ header.message_length = htons(0); *(int *)(&header.transaction_id[8]) = 0xFFEEFFEE; /* transaction id in the response should keep consistent with this one */ rc = sendto(stun_socket, (void *)&header, sizeof(header), 0, (struct sockaddr *)&stun_server, sizeof(stun_server)); char response[64]; rc = recvfrom(stun_socket, response, 64, 0, NULL, 0); ,但我没有得到谷歌眩晕服务器的任何回复。我省略了这段代码中的所有错误检查。我的程序挂在assetic: assets: base: inputs: - '@AppBundle/Resources/public/js/jquery-2.2.4.min.js' - '@AppBundle/Resources/public/js/base.js'

{% javascripts
    '@base'
    '../app/Resources/js/page1_specific.js'
    '../app/Resources/js/page1_other.js'
    filter='uglifyjs2' output='page1.js'
%}

1 个答案:

答案 0 :(得分:0)

我猜你正在做与此类似的事情或发送数据的等价物:

sendto(sock, &header, sizeof(header), (sockaddr*)&addr, addrlen);

如果是这种情况,您可能忘记将message_type值转换为网络字节顺序(big-endian)。

试试这个:

header.message_type = htons(0x0001);

但是如果您想要更好的解决方案,并且可以使用C ++,请使用Stuntman中内置的客户端库。您可以使用在stuncore/stunbuilder.h文件中声明的C ++类CStunMessageBuilder生成如下的绑定请求。

CStunMessageBuilder builder;
StunTransactionId transId;

builder.AddBindingRequestHeader();
builder.AddRandomTransactionId(&transID);
unsigned char* msg = builder.GetStream().GetDataPointerUnsafe();
size_t len = builder.GetStream().GetSize();

sendto(sock, msg, len, (sockaddr*)&addr, addrlen);