diff options
author | Neels Hofmeyr <nhofmeyr@sysmocom.de> | 2016-08-22 13:34:23 +0200 |
---|---|---|
committer | Harald Welte <laforge@gnumonks.org> | 2016-08-27 02:01:20 +0000 |
commit | f0f07d9c9b3227b5233352418d658aba3584310b (patch) | |
tree | ece686988cd3f83875c7e28d0d7bb4d1cff5389c /src | |
parent | 898e1d878e47298f97289e50f9b0cca7ecdf2ed7 (diff) |
osmo_sock_init(): include host and port in error messages
For programs like osmo-hnbgw with numerous sockets, the message that some
unspecified connection was refused is not very helpful. Also output the host
and port where an error occured.
Instead of perror, use fprintf(stderr, ..., strerror()) to be able to include a
format string and print host and port as passed to osmo_sock_init().
Change-Id: I8d0343f51310699b78fcb83fd76fd93764acf3dc
Diffstat (limited to 'src')
-rw-r--r-- | src/socket.c | 21 |
1 files changed, 16 insertions, 5 deletions
diff --git a/src/socket.c b/src/socket.c index 7e610bf3..a36aee79 100644 --- a/src/socket.c +++ b/src/socket.c @@ -71,8 +71,11 @@ int osmo_sock_init(uint16_t family, uint16_t type, uint8_t proto, char portbuf[16]; if ((flags & (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) == - (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) + (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) { + fprintf(stderr, "invalid: both bind and connect flags set:" + " %s:%u: %s\n", host, port); return -EINVAL; + } sprintf(portbuf, "%u", port); memset(&hints, 0, sizeof(struct addrinfo)); @@ -93,7 +96,8 @@ int osmo_sock_init(uint16_t family, uint16_t type, uint8_t proto, rc = getaddrinfo(host, portbuf, &hints, &result); if (rc != 0) { - perror("getaddrinfo returned NULL"); + fprintf(stderr, "getaddrinfo returned NULL: %s:%u: %s\n", + host, port, strerror(errno)); return -EINVAL; } @@ -109,7 +113,10 @@ int osmo_sock_init(uint16_t family, uint16_t type, uint8_t proto, continue; if (flags & OSMO_SOCK_F_NONBLOCK) { if (ioctl(sfd, FIONBIO, (unsigned char *)&on) < 0) { - perror("cannot set this socket unblocking"); + fprintf(stderr, + "cannot set this socket unblocking:" + " %s:%u: %s\n", + host, port, strerror(errno)); close(sfd); return -EINVAL; } @@ -122,7 +129,10 @@ int osmo_sock_init(uint16_t family, uint16_t type, uint8_t proto, rc = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); if (rc < 0) { - perror("cannot setsockopt socket"); + fprintf(stderr, + "cannot setsockopt socket:" + " %s:%u: %s\n", + host, port, strerror(errno)); break; } if (bind(sfd, rp->ai_addr, rp->ai_addrlen) != -1) @@ -133,7 +143,8 @@ int osmo_sock_init(uint16_t family, uint16_t type, uint8_t proto, freeaddrinfo(result); if (rp == NULL) { - perror("unable to connect/bind socket"); + fprintf(stderr, "unable to connect/bind socket: %s:%u: %s\n", + host, port, strerror(errno)); return -ENODEV; } |