wmbiff: tidy up socket connexions.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
This commit is contained in:
Jeremy Sowden 2019-06-28 16:11:18 +01:00 committed by Carlos R. Mafra
parent 1d9d06f152
commit 5d9eb1eb11
2 changed files with 19 additions and 15 deletions

View file

@ -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");

View file

@ -364,21 +364,21 @@ int test_charutil(void)
#include <sys/socket.h>
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;