nl_recvmsgs(ev->sock, ev->cb);
}
+static struct nl_sock *
+create_socket(int protocol)
+{
+ struct nl_sock *sock;
+
+ sock = nl_socket_alloc();
+ if (!sock)
+ return NULL;
+
+ if (nl_connect(sock, protocol))
+ return NULL;
+
+ return sock;
+}
+
static bool
create_event_socket(struct event_socket *ev, int protocol,
int (*cb)(struct nl_msg *msg, void *arg))
nl_cb_set(ev->cb, NL_CB_VALID, NL_CB_CUSTOM, cb, NULL);
- ev->sock = nl_socket_alloc();
+ ev->sock = create_socket(protocol);
if (!ev->sock)
return false;
- if (nl_connect(ev->sock, protocol))
- return false;
-
ev->uloop.fd = nl_socket_get_fd(ev->sock);
ev->uloop.cb = handler_nl_event;
uloop_fd_add(&ev->uloop, ULOOP_READ | ULOOP_EDGE_TRIGGER);
fcntl(sock_ioctl, F_SETFD, fcntl(sock_ioctl, F_GETFD) | FD_CLOEXEC);
// Prepare socket for routing / address control
- sock_rtnl = nl_socket_alloc();
+ sock_rtnl = create_socket(NETLINK_ROUTE);
if (!sock_rtnl)
return -1;
- if (nl_connect(sock_rtnl, NETLINK_ROUTE))
- return -1;
-
if (!create_event_socket(&rtnl_event, NETLINK_ROUTE, cb_rtnl_event))
return -1;
// Receive network link events form kernel
nl_socket_add_membership(rtnl_event.sock, RTNLGRP_LINK);
-
return 0;
}