kernel-netlink: Clean up socket initialization, handle 0 as valid socket fd
authorMartin Willi <martin@revosec.ch>
Wed, 9 Jul 2014 14:47:06 +0000 (16:47 +0200)
committerMartin Willi <martin@revosec.ch>
Wed, 24 Sep 2014 09:19:59 +0000 (11:19 +0200)
src/libhydra/plugins/kernel_netlink/kernel_netlink_shared.c

index 12df7c5..74466e2 100644 (file)
@@ -219,7 +219,7 @@ METHOD(netlink_socket_t, netlink_send_ack, status_t,
 METHOD(netlink_socket_t, destroy, void,
        private_netlink_socket_t *this)
 {
-       if (this->socket > 0)
+       if (this->socket != -1)
        {
                close(this->socket);
        }
@@ -233,7 +233,9 @@ METHOD(netlink_socket_t, destroy, void,
 netlink_socket_t *netlink_socket_create(int protocol)
 {
        private_netlink_socket_t *this;
-       struct sockaddr_nl addr;
+       struct sockaddr_nl addr = {
+               .nl_family = AF_NETLINK,
+       };
 
        INIT(this,
                .public = {
@@ -243,21 +245,16 @@ netlink_socket_t *netlink_socket_create(int protocol)
                },
                .seq = 200,
                .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
+               .socket = socket(AF_NETLINK, SOCK_RAW, protocol),
                .protocol = protocol,
        );
 
-       memset(&addr, 0, sizeof(addr));
-       addr.nl_family = AF_NETLINK;
-
-       this->socket = socket(AF_NETLINK, SOCK_RAW, protocol);
-       if (this->socket < 0)
+       if (this->socket == -1)
        {
                DBG1(DBG_KNL, "unable to create netlink socket");
                destroy(this);
                return NULL;
        }
-
-       addr.nl_groups = 0;
        if (bind(this->socket, (struct sockaddr*)&addr, sizeof(addr)))
        {
                DBG1(DBG_KNL, "unable to bind netlink socket");