From db144ce571748e691ea88b04f07436542cdfca99 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Thu, 6 Sep 2018 14:55:52 +0200 Subject: osmocon: Use osmo_sock_unix_init_ofd() from libosmocore We don't need to hand-code unix domain socket initialization but can simply use our library function for it. As an added benefit, the library code already contains corner case handling for non-NUL terminated unix domain socket path. Change-Id: I3ab69a971be555c9f9b5b7a7e5da53008a119504 --- src/host/osmocon/osmocon.c | 45 ++++----------------------------------------- 1 file changed, 4 insertions(+), 41 deletions(-) (limited to 'src/host/osmocon/osmocon.c') diff --git a/src/host/osmocon/osmocon.c b/src/host/osmocon/osmocon.c index 6075e610..26416f76 100644 --- a/src/host/osmocon/osmocon.c +++ b/src/host/osmocon/osmocon.c @@ -1,6 +1,6 @@ /* osmocon */ -/* (C) 2010 by Harald Welte +/* (C) 2010,2018 by Harald Welte * (C) 2010 by Holger Hans Peter Freyther * (C) 2010 by Steve Markgraf * @@ -45,6 +45,7 @@ #include #include #include +#include #include @@ -1333,47 +1334,14 @@ static int register_tool_server(struct tool_server *ts, uint8_t dlci) { struct osmo_fd *bfd = &ts->bfd; - struct sockaddr_un local; - unsigned int namelen; int rc; - bfd->fd = socket(AF_UNIX, SOCK_STREAM, 0); - - if (bfd->fd < 0) { + rc = osmo_sock_unix_init_ofd(bfd, SOCK_STREAM, 0, path, OSMO_SOCK_F_BIND); + if (rc < 0) { fprintf(stderr, "Failed to create Unix Domain Socket.\n"); return -1; } - local.sun_family = AF_UNIX; - strncpy(local.sun_path, path, sizeof(local.sun_path)); - local.sun_path[sizeof(local.sun_path) - 1] = '\0'; - unlink(local.sun_path); - - /* we use the same magic that X11 uses in Xtranssock.c for - * calculating the proper length of the sockaddr */ -#if defined(BSD44SOCKETS) || defined(__UNIXWARE__) - local.sun_len = strlen(local.sun_path); -#endif -#if defined(BSD44SOCKETS) || defined(SUN_LEN) - namelen = SUN_LEN(&local); -#else - namelen = strlen(local.sun_path) + - offsetof(struct sockaddr_un, sun_path); -#endif - - rc = bind(bfd->fd, (struct sockaddr *) &local, namelen); - if (rc != 0) { - fprintf(stderr, "Failed to bind the unix domain socket. '%s'\n", - local.sun_path); - return -1; - } - - if (listen(bfd->fd, 0) != 0) { - fprintf(stderr, "Failed to listen.\n"); - return -1; - } - - bfd->when = BSC_FD_READ; bfd->cb = tool_accept; bfd->data = ts; @@ -1384,11 +1352,6 @@ static int register_tool_server(struct tool_server *ts, sercomm_register_rx_cb(dlci, hdlc_tool_cb); - if (osmo_fd_register(bfd) != 0) { - fprintf(stderr, "Failed to register the bfd.\n"); - return -1; - } - return 0; } -- cgit v1.2.3