host: initialize sockaddr->sa_len if it is available
authorMartin Willi <martin@revosec.ch>
Wed, 20 Mar 2013 17:15:26 +0000 (18:15 +0100)
committerMartin Willi <martin@revosec.ch>
Mon, 6 May 2013 14:10:11 +0000 (16:10 +0200)
src/libstrongswan/networking/host.c

index bffa960..268d9b1 100644 (file)
@@ -54,6 +54,15 @@ struct private_host_t {
        socklen_t socklen;
 };
 
+/**
+ * Update the sockaddr internal sa_len option, if available
+ */
+static inline void update_sa_len(private_host_t *this)
+{
+#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
+       this->address.sa_len = this->socklen;
+#endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
+}
 
 METHOD(host_t, get_sockaddr, sockaddr_t*,
        private_host_t *this)
@@ -440,6 +449,7 @@ host_t *host_create_from_sockaddr(sockaddr_t *sockaddr)
                        memcpy(&this->address4, (struct sockaddr_in*)sockaddr,
                                   sizeof(struct sockaddr_in));
                        this->socklen = sizeof(struct sockaddr_in);
+                       update_sa_len(this);
                        return &this->public;
                }
                case AF_INET6:
@@ -447,6 +457,7 @@ host_t *host_create_from_sockaddr(sockaddr_t *sockaddr)
                        memcpy(&this->address6, (struct sockaddr_in6*)sockaddr,
                                   sizeof(struct sockaddr_in6));
                        this->socklen = sizeof(struct sockaddr_in6);
+                       update_sa_len(this);
                        return &this->public;
                }
                default:
@@ -529,6 +540,7 @@ host_t *host_create_from_chunk(int family, chunk_t address, u_int16_t port)
                        this->socklen = sizeof(struct sockaddr_in6);
                        break;
        }
+       update_sa_len(this);
        return &this->public;
 }
 
@@ -582,11 +594,13 @@ host_t *host_create_any(int family)
                case AF_INET:
                {
                        this->socklen = sizeof(struct sockaddr_in);
+                       update_sa_len(this);
                        return &(this->public);
                }
                case AF_INET6:
                {
                        this->socklen = sizeof(struct sockaddr_in6);
+                       update_sa_len(this);
                        return &this->public;
                }
                default: