Commit af6a515b authored by Filippo Della Betta's avatar Filippo Della Betta Committed by Youness Alaoui

Fixed compiling on Visual Studio and removed getsockname before bind. Added...

Fixed compiling on Visual Studio and removed getsockname before bind. Added WSAStartup/WSACleanup on win32
parent 7d17f097
...@@ -43,7 +43,6 @@ ...@@ -43,7 +43,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h>
#include <string.h> #include <string.h>
#ifdef _WIN32 #ifdef _WIN32
...@@ -59,6 +58,7 @@ ...@@ -59,6 +58,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h> #include <netdb.h>
#endif #endif
...@@ -69,6 +69,7 @@ ...@@ -69,6 +69,7 @@
static int listen_dgram (void) static int listen_dgram (void)
{ {
struct addrinfo hints, *res; struct addrinfo hints, *res;
const struct addrinfo *ptr;
int val = -1; int val = -1;
memset (&hints, 0, sizeof (hints)); memset (&hints, 0, sizeof (hints));
...@@ -77,7 +78,7 @@ static int listen_dgram (void) ...@@ -77,7 +78,7 @@ static int listen_dgram (void)
if (getaddrinfo (NULL, "0", &hints, &res)) if (getaddrinfo (NULL, "0", &hints, &res))
return -1; return -1;
for (const struct addrinfo *ptr = res; ptr != NULL; ptr = ptr->ai_next) for (ptr = res; ptr != NULL; ptr = ptr->ai_next)
{ {
int fd = socket (ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol); int fd = socket (ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
if (fd == -1) if (fd == -1)
...@@ -103,6 +104,7 @@ static void bad_family (void) ...@@ -103,6 +104,7 @@ static void bad_family (void)
{ {
struct sockaddr addr, dummy; struct sockaddr addr, dummy;
int val; int val;
socklen_t dummylen = sizeof(dummy);
memset (&addr, 0, sizeof (addr)); memset (&addr, 0, sizeof (addr));
addr.sa_family = AF_UNSPEC; addr.sa_family = AF_UNSPEC;
...@@ -111,7 +113,7 @@ static void bad_family (void) ...@@ -111,7 +113,7 @@ static void bad_family (void)
#endif #endif
val = stun_usage_bind_run (&addr, sizeof (addr), val = stun_usage_bind_run (&addr, sizeof (addr),
&dummy, &(socklen_t){ sizeof (dummy) }); &dummy, &dummylen);
assert (val != 0); assert (val != 0);
} }
...@@ -121,6 +123,7 @@ static void small_srv_addr (void) ...@@ -121,6 +123,7 @@ static void small_srv_addr (void)
{ {
struct sockaddr addr, dummy; struct sockaddr addr, dummy;
int val; int val;
socklen_t dummylen = sizeof(dummy);
memset (&addr, 0, sizeof (addr)); memset (&addr, 0, sizeof (addr));
addr.sa_family = AF_INET; addr.sa_family = AF_INET;
...@@ -129,7 +132,7 @@ static void small_srv_addr (void) ...@@ -129,7 +132,7 @@ static void small_srv_addr (void)
#endif #endif
val = stun_usage_bind_run (&addr, 1, val = stun_usage_bind_run (&addr, 1,
&dummy, &(socklen_t){ sizeof (dummy) }); &dummy, &dummylen);
assert (val == STUN_USAGE_BIND_RETURN_ERROR); assert (val == STUN_USAGE_BIND_RETURN_ERROR);
} }
...@@ -139,12 +142,13 @@ static void big_srv_addr (void) ...@@ -139,12 +142,13 @@ static void big_srv_addr (void)
{ {
uint8_t buf[sizeof (struct sockaddr_storage) + 16]; uint8_t buf[sizeof (struct sockaddr_storage) + 16];
struct sockaddr dummy; struct sockaddr dummy;
int fd, val; int val;
socklen_t dummylen = sizeof(dummy);
memset (buf, 0, sizeof (buf)); memset (buf, 0, sizeof (buf));
val = stun_usage_bind_run ((struct sockaddr *)buf, sizeof (buf), val = stun_usage_bind_run ((struct sockaddr *)buf, sizeof (buf),
&dummy, &(socklen_t){ sizeof (dummy) }); &dummy, &dummylen);
assert (val == STUN_USAGE_BIND_RETURN_ERROR); assert (val == STUN_USAGE_BIND_RETURN_ERROR);
} }
...@@ -155,6 +159,7 @@ static void timeout (void) ...@@ -155,6 +159,7 @@ static void timeout (void)
struct sockaddr_storage srv; struct sockaddr_storage srv;
struct sockaddr dummy; struct sockaddr dummy;
socklen_t srvlen = sizeof (srv); socklen_t srvlen = sizeof (srv);
socklen_t dummylen = sizeof(dummy);
int val; int val;
/* Allocate a local UDP port, so we are 100% sure nobody responds there */ /* Allocate a local UDP port, so we are 100% sure nobody responds there */
...@@ -165,7 +170,7 @@ static void timeout (void) ...@@ -165,7 +170,7 @@ static void timeout (void)
assert (val == 0); assert (val == 0);
val = stun_usage_bind_run ((struct sockaddr *)&srv, srvlen, val = stun_usage_bind_run ((struct sockaddr *)&srv, srvlen,
&dummy, &(socklen_t){ sizeof (dummy) }); &dummy, &dummylen);
assert (val == STUN_USAGE_BIND_RETURN_TIMEOUT); assert (val == STUN_USAGE_BIND_RETURN_TIMEOUT);
close (servfd); close (servfd);
...@@ -183,6 +188,7 @@ static void bad_responses (void) ...@@ -183,6 +188,7 @@ static void bad_responses (void)
StunAgent agent; StunAgent agent;
StunMessage msg; StunMessage msg;
StunMessage req_msg; StunMessage req_msg;
int servfd, fd;
uint16_t known_attributes[] = { uint16_t known_attributes[] = {
STUN_ATTRIBUTE_MAPPED_ADDRESS, STUN_ATTRIBUTE_MAPPED_ADDRESS,
...@@ -196,7 +202,7 @@ static void bad_responses (void) ...@@ -196,7 +202,7 @@ static void bad_responses (void)
STUN_COMPATIBILITY_RFC5389, 0); STUN_COMPATIBILITY_RFC5389, 0);
/* Allocate a local UDP port */ /* Allocate a local UDP port */
int servfd = listen_dgram (), fd; servfd = listen_dgram ();
assert (servfd != -1); assert (servfd != -1);
val = getsockname (servfd, (struct sockaddr *)&addr, &addrlen); val = getsockname (servfd, (struct sockaddr *)&addr, &addrlen);
...@@ -280,10 +286,6 @@ static void responses (void) ...@@ -280,10 +286,6 @@ static void responses (void)
fd = socket (addr.ss_family, SOCK_DGRAM, 0); fd = socket (addr.ss_family, SOCK_DGRAM, 0);
assert (fd != -1); assert (fd != -1);
/* Send to/receive from our client instance only */
val = getsockname (fd, (struct sockaddr *)&addr, &addrlen);
assert (val == 0);
/* Send error response */ /* Send error response */
req_len = stun_usage_bind_create (&agent, &req_msg, req, sizeof(req)); req_len = stun_usage_bind_create (&agent, &req_msg, req, sizeof(req));
assert (req_len > 0); assert (req_len > 0);
...@@ -445,12 +447,21 @@ static void test (void (*func) (void), const char *name) ...@@ -445,12 +447,21 @@ static void test (void (*func) (void), const char *name)
int main (void) int main (void)
{ {
#ifdef _WIN32
WSADATA w;
WSAStartup(0x0202, &w);
#endif
test (bad_family, "Bad socket family"); test (bad_family, "Bad socket family");
test (small_srv_addr, "Too small server address"); test (small_srv_addr, "Too small server address");
test (big_srv_addr, "Too big server address"); test (big_srv_addr, "Too big server address");
test (bad_responses, "Bad responses"); test (bad_responses, "Bad responses");
test (responses, "Error responses"); test (responses, "Error responses");
test (keepalive, "Keep alives"); test (keepalive, "Keep alives");
#ifdef HAVE_POLL
test (timeout, "Binding discovery timeout"); test (timeout, "Binding discovery timeout");
#endif
#ifdef _WIN32
WSACleanup();
#endif
return 0; return 0;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment