[Nice] [nice/master] fix stund against strict aliasing issues

Youness Alaoui youness.alaoui at collabora.co.uk
Wed Nov 12 16:58:14 PST 2008


---
 stun/tools/stund.c |   19 ++++++++++++-------
 stun/tools/stund.h |    2 +-
 2 files changed, 13 insertions(+), 8 deletions(-)

diff --git a/stun/tools/stund.c b/stun/tools/stund.c
index 4f479d0..e36b64c 100644
--- a/stun/tools/stund.c
+++ b/stun/tools/stund.c
@@ -78,10 +78,16 @@ static const uint16_t known_attributes[] =  {
 /**
  * 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 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)
   {
     perror ("Error opening IP port");
@@ -90,24 +96,23 @@ int listen_socket (int fam, int type, int proto, uint16_t port)
   if (fd < 3)
     goto error;
 
-  struct sockaddr_storage addr;
   memset (&addr, 0, sizeof (addr));
-  addr.ss_family = fam;
+  addr.storage.ss_family = fam;
 #ifdef HAVE_SA_LEN
-  addr.ss_len = sizeof (addr);
+  addr.storage.ss_len = sizeof (addr);
 #endif
 
   switch (fam)
   {
     case AF_INET:
-      ((struct sockaddr_in *)&addr)->sin_port = port;
+      addr.in.sin_port = htons (port);
       break;
 
     case AF_INET6:
 #ifdef IPV6_V6ONLY
       setsockopt (fd, SOL_IPV6, IPV6_V6ONLY, &yes, sizeof (yes));
 #endif
-      ((struct sockaddr_in6 *)&addr)->sin6_port = port;
+      addr.in6.sin6_port = htons (port);
       break;
   }
 
@@ -271,7 +276,7 @@ static int run (int family, int protocol, unsigned port)
 {
   StunAgent oldagent;
   StunAgent newagent;
-  int sock = listen_socket (family, SOCK_DGRAM, protocol, htons (port));
+  int sock = listen_socket (family, SOCK_DGRAM, protocol, port);
   if (sock == -1)
     return -1;
 
diff --git a/stun/tools/stund.h b/stun/tools/stund.h
index e89b275..9b20141 100644
--- a/stun/tools/stund.h
+++ b/stun/tools/stund.h
@@ -36,7 +36,7 @@
 #ifndef NICE_STUN_STUND_H
 # 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 recv_safe (int fd, struct msghdr *msg);
 
-- 
1.5.6.5




More information about the Nice mailing list