diff options
Diffstat (limited to 'src/shared')
-rw-r--r-- | src/shared/socket-util.c | 39 | ||||
-rw-r--r-- | src/shared/socket-util.h | 2 |
2 files changed, 41 insertions, 0 deletions
diff --git a/src/shared/socket-util.c b/src/shared/socket-util.c index 6c94d69486..f6ddea3183 100644 --- a/src/shared/socket-util.c +++ b/src/shared/socket-util.c @@ -565,6 +565,45 @@ bool socket_address_matches_fd(const SocketAddress *a, int fd) { return false; } +int make_socket_fd(const char* address, int flags) { + SocketAddress a; + int fd, r; + char _cleanup_free_ *p = NULL; + + r = socket_address_parse(&a, address); + if (r < 0) { + log_error("failed to parse socket: %s", strerror(-r)); + return r; + } + + fd = socket(socket_address_family(&a), flags, 0); + if (fd < 0) { + log_error("socket(): %m"); + return -errno; + } + + r = socket_address_print(&a, &p); + if (r < 0) { + log_error("socket_address_print(): %s", strerror(-r)); + return r; + } + log_info("Listening on %s", p); + + r = bind(fd, &a.sockaddr.sa, a.size); + if (r < 0) { + log_error("bind to %s: %m", address); + return -errno; + } + + r = listen(fd, SOMAXCONN); + if (r < 0) { + log_error("listen on %s: %m", address); + return -errno; + } + + return fd; +} + static const char* const netlink_family_table[] = { [NETLINK_ROUTE] = "route", [NETLINK_FIREWALL] = "firewall", diff --git a/src/shared/socket-util.h b/src/shared/socket-util.h index 771765d323..33838345ed 100644 --- a/src/shared/socket-util.h +++ b/src/shared/socket-util.h @@ -88,6 +88,8 @@ bool socket_address_is_netlink(const SocketAddress *a, const char *s); bool socket_address_matches_fd(const SocketAddress *a, int fd); +int make_socket_fd(const char* address, int flags); + bool socket_address_equal(const SocketAddress *a, const SocketAddress *b); bool socket_address_needs_mount(const SocketAddress *a, const char *prefix); |