From e19e9800b93dbaec566c43d752b9cf1a3c7c913b Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Sun, 23 Oct 2011 16:02:18 +0200 Subject: [PATCH] remove some more code duplication --- system-linux.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/system-linux.c b/system-linux.c index 11126fa..0308e6f 100644 --- a/system-linux.c +++ b/system-linux.c @@ -42,6 +42,21 @@ handler_nl_event(struct uloop_fd *u, unsigned int events) 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)) @@ -53,13 +68,10 @@ create_event_socket(struct event_socket *ev, int protocol, 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); @@ -74,20 +86,16 @@ int system_init(void) 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; } -- 2.25.1