X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=src%2Fstun.cpp;h=42d02c522484fb62b8334264082ced133aa48eb2;hb=0a18ce8f4cd1a723f50333945d94c84b45c8d56b;hp=1ab2bc7b4df3fb2afa5cd9873027d1cba31fe8c3;hpb=3328b10641c7a6c6e6b7cf935ec709e1ca80f97b;p=novacoin.git diff --git a/src/stun.cpp b/src/stun.cpp index 1ab2bc7..42d02c5 100644 --- a/src/stun.cpp +++ b/src/stun.cpp @@ -28,10 +28,9 @@ * Of course all fields are in network format. */ -#define __STDC_LIMIT_MACROS - #include -#include +#include +#include #include #include #include @@ -50,6 +49,7 @@ #include #include "ministun.h" +#include "netbase.h" extern int GetRandInt(int nMax); extern uint64_t GetRand(uint64_t nMax); @@ -339,20 +339,20 @@ static int stun_send(int s, struct sockaddr_in *dst, struct stun_header *resp) } /* helper function to generate a random request id */ -static uint64_t randfiller = GetRand(UINT64_MAX); +static uint64_t randfiller = GetRand(std::numeric_limits::max()); static void stun_req_id(struct stun_header *req) { const uint64_t *S_block = (const uint64_t *)StunSrvList; - req->id.id[0] = GetRandInt(INT_MAX); - req->id.id[1] = GetRandInt(INT_MAX); - req->id.id[2] = GetRandInt(INT_MAX); - req->id.id[3] = GetRandInt(INT_MAX); + req->id.id[0] = GetRandInt(std::numeric_limits::max()); + req->id.id[1] = GetRandInt(std::numeric_limits::max()); + req->id.id[2] = GetRandInt(std::numeric_limits::max()); + req->id.id[3] = GetRandInt(std::numeric_limits::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)); @@ -457,7 +457,7 @@ static int StunRequest2(int sock, struct sockaddr_in *server, struct sockaddr_in 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); @@ -500,6 +500,7 @@ static int StunRequest(const char *host, uint16_t port, struct sockaddr_in *mapp if(hostinfo == NULL) return -1; + SOCKET sock = INVALID_SOCKET; struct sockaddr_in server, client; memset(&server, 0, sizeof(server)); memset(&client, 0, sizeof(client)); @@ -508,8 +509,8 @@ static int StunRequest(const char *host, uint16_t port, struct sockaddr_in *mapp 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); @@ -517,11 +518,7 @@ static int StunRequest(const char *host, uint16_t port, struct sockaddr_in *mapp 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