summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPetr Vorel <pvorel@suse.cz>2022-11-11 16:50:39 +0100
committerPetr Vorel <pvorel@suse.cz>2022-11-11 19:12:29 +0100
commit8461d3cda867de6caa02a52e8116f7c942f0c255 (patch)
tree69e2bf774472a6e047f8cd1522d71093bfcaae0c
parentde70b597c8cc50961bc85aa4b585a1184dcb0809 (diff)
downloadiputils-8461d3cda867de6caa02a52e8116f7c942f0c255.tar.gz
ping: Refactor style in create_socket()
There will be more code in socket error handling, thus return on valid socket to reduce indent level. sock->socktype can be assigned, because all checks for validity are done via sock->fd. NOTE: check for 0 in str_socktype() is still needed for -4 or -6, because only socket for chosen IP version is being created, thus socktype of the other is 0. Signed-off-by: Petr Vorel <pvorel@suse.cz>
-rw-r--r--ping/ping.c20
1 files changed, 13 insertions, 7 deletions
diff --git a/ping/ping.c b/ping/ping.c
index c0c593e..40e9017 100644
--- a/ping/ping.c
+++ b/ping/ping.c
@@ -178,13 +178,19 @@ static void create_socket(struct ping_rts *rts, socket_st *sock, int family,
sock->fd = socket(family, SOCK_RAW, protocol);
}
- if (sock->fd == -1) {
- if (requisite || rts->opt_verbose)
- error(0, errno, "socket");
- if (requisite)
- exit(2);
- } else
- sock->socktype = socktype;
+ sock->socktype = socktype;
+
+ /* valid socket */
+ if (sock->fd != -1)
+ return;
+
+ /* failed to create socket */
+
+ if (requisite || rts->opt_verbose)
+ error(0, errno, "socket");
+
+ if (requisite)
+ exit(2);
}
static void set_socket_option(socket_st *sock, int level, int optname,