diff options
author | Tom Gundersen <teg@jklm.no> | 2015-06-13 21:25:05 +0200 |
---|---|---|
committer | Tom Gundersen <teg@jklm.no> | 2015-06-13 21:25:05 +0200 |
commit | b95cc756de7a27f7546e42dd9acf6da0669da402 (patch) | |
tree | 5a192546c8c53815c29986c9ec3cd9dc5331c776 /src/libsystemd/sd-netlink/netlink-socket.c | |
parent | 89489ef7d451d61e176764deb608a84e29b1fd38 (diff) |
sd-netlink: socket - move some functions from main source file
Diffstat (limited to 'src/libsystemd/sd-netlink/netlink-socket.c')
-rw-r--r-- | src/libsystemd/sd-netlink/netlink-socket.c | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/libsystemd/sd-netlink/netlink-socket.c b/src/libsystemd/sd-netlink/netlink-socket.c index 2e2826ac63..8136cf36ae 100644 --- a/src/libsystemd/sd-netlink/netlink-socket.c +++ b/src/libsystemd/sd-netlink/netlink-socket.c @@ -34,6 +34,53 @@ #include "netlink-internal.h" #include "netlink-types.h" +int socket_open(int family) { + int fd; + + fd = socket(PF_NETLINK, SOCK_RAW|SOCK_CLOEXEC|SOCK_NONBLOCK, family); + if (fd < 0) + return -errno; + + return fd; +} + +int socket_bind(sd_netlink *nl) { + socklen_t addrlen; + int r, one = 1; + + r = setsockopt(nl->fd, SOL_NETLINK, NETLINK_PKTINFO, &one, sizeof(one)); + if (r < 0) + return -errno; + + addrlen = sizeof(nl->sockaddr); + + r = bind(nl->fd, &nl->sockaddr.sa, addrlen); + /* ignore EINVAL to allow opening an already bound socket */ + if (r < 0 && errno != EINVAL) + return -errno; + + r = getsockname(nl->fd, &nl->sockaddr.sa, &addrlen); + if (r < 0) + return -errno; + + return 0; +} + + +int socket_join_broadcast_group(sd_netlink *nl, unsigned group) { + int r; + + assert(nl); + assert(nl->fd >= 0); + assert(group > 0); + + r = setsockopt(nl->fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, &group, sizeof(group)); + if (r < 0) + return -errno; + + return 0; +} + /* returns the number of bytes sent, or a negative error code */ int socket_write_message(sd_netlink *nl, sd_netlink_message *m) { union { |