Commit e610fb4f authored by Youness Alaoui's avatar Youness Alaoui

fix stund against strict aliasing issues

parent 764065e9
...@@ -78,10 +78,16 @@ static const uint16_t known_attributes[] = { ...@@ -78,10 +78,16 @@ static const uint16_t known_attributes[] = {
/** /**
* Creates a listening socket * Creates a listening socket
*/ */
int listen_socket (int fam, int type, int proto, uint16_t port) int listen_socket (int fam, int type, int proto, unsigned int port)
{ {
int yes = 1; int yes = 1;
int fd = socket (fam, type, proto); int fd = socket (fam, type, proto);
union {
struct sockaddr addr;
struct sockaddr_in in;
struct sockaddr_in6 in6;
struct sockaddr_storage storage;
} addr;
if (fd == -1) if (fd == -1)
{ {
perror ("Error opening IP port"); perror ("Error opening IP port");
...@@ -90,24 +96,23 @@ int listen_socket (int fam, int type, int proto, uint16_t port) ...@@ -90,24 +96,23 @@ int listen_socket (int fam, int type, int proto, uint16_t port)
if (fd < 3) if (fd < 3)
goto error; goto error;
struct sockaddr_storage addr;
memset (&addr, 0, sizeof (addr)); memset (&addr, 0, sizeof (addr));
addr.ss_family = fam; addr.storage.ss_family = fam;
#ifdef HAVE_SA_LEN #ifdef HAVE_SA_LEN
addr.ss_len = sizeof (addr); addr.storage.ss_len = sizeof (addr);
#endif #endif
switch (fam) switch (fam)
{ {
case AF_INET: case AF_INET:
((struct sockaddr_in *)&addr)->sin_port = port; addr.in.sin_port = htons (port);
break; break;
case AF_INET6: case AF_INET6:
#ifdef IPV6_V6ONLY #ifdef IPV6_V6ONLY
setsockopt (fd, SOL_IPV6, IPV6_V6ONLY, &yes, sizeof (yes)); setsockopt (fd, SOL_IPV6, IPV6_V6ONLY, &yes, sizeof (yes));
#endif #endif
((struct sockaddr_in6 *)&addr)->sin6_port = port; addr.in6.sin6_port = htons (port);
break; break;
} }
...@@ -271,7 +276,7 @@ static int run (int family, int protocol, unsigned port) ...@@ -271,7 +276,7 @@ static int run (int family, int protocol, unsigned port)
{ {
StunAgent oldagent; StunAgent oldagent;
StunAgent newagent; StunAgent newagent;
int sock = listen_socket (family, SOCK_DGRAM, protocol, htons (port)); int sock = listen_socket (family, SOCK_DGRAM, protocol, port);
if (sock == -1) if (sock == -1)
return -1; return -1;
......
...@@ -36,7 +36,7 @@ ...@@ -36,7 +36,7 @@
#ifndef NICE_STUN_STUND_H #ifndef NICE_STUN_STUND_H
# define NICE_STUN_STUND_H 1 # define NICE_STUN_STUND_H 1
int listen_socket (int fam, int type, int proto, uint16_t port); int listen_socket (int fam, int type, int proto, unsigned port);
ssize_t send_safe (int fd, const struct msghdr *msg); ssize_t send_safe (int fd, const struct msghdr *msg);
ssize_t recv_safe (int fd, struct msghdr *msg); ssize_t recv_safe (int fd, struct msghdr *msg);
......
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