*/
#include <stdio.h>
-#include <stdint.h>
+#include <inttypes.h>
+#include <limits>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>
#include "ministun.h"
+#include "netbase.h"
+
+extern int GetRandInt(int nMax);
+extern uint64_t GetRand(uint64_t nMax);
/*---------------------------------------------------------------------*/
}
/* helper function to generate a random request id */
-static uint64_t randfiller;
+static uint64_t randfiller = GetRand(std::numeric_limits<uint64_t>::max());
static void stun_req_id(struct stun_header *req)
{
const uint64_t *S_block = (const uint64_t *)StunSrvList;
+ req->id.id[0] = GetRandInt(std::numeric_limits<int32_t>::max());
+ req->id.id[1] = GetRandInt(std::numeric_limits<int32_t>::max());
+ req->id.id[2] = GetRandInt(std::numeric_limits<int32_t>::max());
+ req->id.id[3] = GetRandInt(std::numeric_limits<int32_t>::max());
+
req->id.id[0] |= 0x55555555;
req->id.id[1] &= 0x55555555;
req->id.id[2] |= 0x55555555;
req->id.id[3] &= 0x55555555;
- register char x = 20;
+ char x = 20;
do {
uint32_t s_elm = S_block[(uint8_t)randfiller];
randfiller ^= (randfiller << 5) | (randfiller >> (64 - 5));
req = (struct stun_header *)reqdata;
stun_req_id(req);
- int reqlen = 0;
+ unsigned short reqlen = 0;
req->msgtype = 0;
req->msglen = 0;
req->msglen = htons(reqlen);
if(hostinfo == NULL)
return -1;
+ SOCKET sock = INVALID_SOCKET;
struct sockaddr_in server, client;
memset(&server, 0, sizeof(server));
memset(&client, 0, sizeof(client));
server.sin_addr = *(struct in_addr*) hostinfo->h_addr;
server.sin_port = htons(port);
- int sock = socket(AF_INET, SOCK_DGRAM, 0);
- if(sock < 0)
+ sock = socket(AF_INET, SOCK_DGRAM, 0);
+ if(sock == INVALID_SOCKET)
return -2;
client.sin_addr.s_addr = htonl(INADDR_ANY);
int rc = -3;
if(bind(sock, (struct sockaddr*)&client, sizeof(client)) >= 0)
rc = StunRequest2(sock, &server, mapped);
-#ifndef WIN32
- close(sock);
-#else
- closesocket(sock);
-#endif
+ CloseSocket(sock);
return rc;
} // StunRequest