wmbiff: tidy up socket connexions.
Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
This commit is contained in:
parent
1d9d06f152
commit
5d9eb1eb11
2 changed files with 19 additions and 15 deletions
|
@ -50,21 +50,25 @@ static int sanity_check_hostname(const char *hostname)
|
||||||
|
|
||||||
static int ipv4_sock_connect(struct in_addr *address, uint16_t port)
|
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;
|
int fd, i;
|
||||||
|
|
||||||
fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
|
fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||||
if (fd == -1) {
|
if (fd == -1) {
|
||||||
perror("Error opening socket");
|
perror("Error opening socket");
|
||||||
printf("socket() failed.\n");
|
printf("socket() failed.\n");
|
||||||
return (-1);
|
return (-1);
|
||||||
};
|
};
|
||||||
if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
|
if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
|
||||||
perror("fcntl(FD_CLOEXEC)");
|
perror("fcntl(FD_CLOEXEC)");
|
||||||
|
|
||||||
addr.sin_family = AF_INET;
|
i = connect(fd, addrp, addrlen);
|
||||||
addr.sin_addr.s_addr = *(u_long *) address;
|
|
||||||
addr.sin_port = htons(port);
|
|
||||||
i = connect(fd, (struct sockaddr *) &addr, sizeof addr);
|
|
||||||
if (i == -1) {
|
if (i == -1) {
|
||||||
int saved_errno = errno;
|
int saved_errno = errno;
|
||||||
perror("Error connecting");
|
perror("Error connecting");
|
||||||
|
|
|
@ -364,21 +364,21 @@ int test_charutil(void)
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
int test_sock_connect(void)
|
int test_sock_connect(void)
|
||||||
{
|
{
|
||||||
struct sockaddr_in addr;
|
struct sockaddr_in addr = { .sin_family = AF_INET };
|
||||||
int s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
struct sockaddr *addrp = (struct sockaddr *) &addr;
|
||||||
socklen_t addrlen = sizeof(struct sockaddr_in);
|
socklen_t addrlen = sizeof addr;
|
||||||
|
int s;
|
||||||
|
|
||||||
|
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||||
if (s < 0) {
|
if (s < 0) {
|
||||||
perror("socket");
|
perror("socket");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
addr.sin_family = AF_INET;
|
if (bind(s, addrp, addrlen) < 0) {
|
||||||
addr.sin_addr.s_addr = 0;
|
|
||||||
addr.sin_port = 0;
|
|
||||||
if (bind(s, (const struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0) {
|
|
||||||
perror("bind");
|
perror("bind");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
getsockname(s, (struct sockaddr *)&addr, &addrlen);
|
getsockname(s, addrp, &addrlen);
|
||||||
if (listen(s, 5) < 0) {
|
if (listen(s, 5) < 0) {
|
||||||
perror("listen");
|
perror("listen");
|
||||||
return 1;
|
return 1;
|
||||||
|
|
Loading…
Reference in a new issue