From 5d9eb1eb111801f530d7d631376e17cdc31a8089 Mon Sep 17 00:00:00 2001 From: Jeremy Sowden Date: Fri, 28 Jun 2019 16:11:18 +0100 Subject: [PATCH] wmbiff: tidy up socket connexions. Signed-off-by: Jeremy Sowden --- wmbiff/wmbiff/socket.c | 18 +++++++++++------- wmbiff/wmbiff/test_wmbiff.c | 16 ++++++++-------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/wmbiff/wmbiff/socket.c b/wmbiff/wmbiff/socket.c index d3cadea..eca1d36 100644 --- a/wmbiff/wmbiff/socket.c +++ b/wmbiff/wmbiff/socket.c @@ -50,21 +50,25 @@ static int sanity_check_hostname(const char *hostname) static int ipv4_sock_connect(struct in_addr *address, uint16_t port) { - struct sockaddr_in addr; + struct sockaddr_in addr = { + .sin_family = AF_INET, + .sin_addr.s_addr = *(u_long *) address, + .sin_port = htons(port) + }; + struct sockaddr *addrp = (struct sockaddr *) &addr; + socklen_t addrlen = sizeof addr; int fd, i; + fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); if (fd == -1) { perror("Error opening socket"); printf("socket() failed.\n"); return (-1); }; - if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) - perror("fcntl(FD_CLOEXEC)"); + if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) + perror("fcntl(FD_CLOEXEC)"); - addr.sin_family = AF_INET; - addr.sin_addr.s_addr = *(u_long *) address; - addr.sin_port = htons(port); - i = connect(fd, (struct sockaddr *) &addr, sizeof addr); + i = connect(fd, addrp, addrlen); if (i == -1) { int saved_errno = errno; perror("Error connecting"); diff --git a/wmbiff/wmbiff/test_wmbiff.c b/wmbiff/wmbiff/test_wmbiff.c index 8e090cc..e4bdabb 100644 --- a/wmbiff/wmbiff/test_wmbiff.c +++ b/wmbiff/wmbiff/test_wmbiff.c @@ -364,21 +364,21 @@ int test_charutil(void) #include int test_sock_connect(void) { - struct sockaddr_in addr; - int s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); - socklen_t addrlen = sizeof(struct sockaddr_in); + struct sockaddr_in addr = { .sin_family = AF_INET }; + struct sockaddr *addrp = (struct sockaddr *) &addr; + socklen_t addrlen = sizeof addr; + int s; + + s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (s < 0) { perror("socket"); return 1; } - addr.sin_family = AF_INET; - addr.sin_addr.s_addr = 0; - addr.sin_port = 0; - if (bind(s, (const struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0) { + if (bind(s, addrp, addrlen) < 0) { perror("bind"); return 1; } - getsockname(s, (struct sockaddr *)&addr, &addrlen); + getsockname(s, addrp, &addrlen); if (listen(s, 5) < 0) { perror("listen"); return 1;