From f56df9c083fb4cfcfc410513304be39602001ae7 Mon Sep 17 00:00:00 2001 From: Simone Piccardi Date: Tue, 7 Dec 2004 00:38:00 +0000 Subject: [PATCH] Correzione per gestire IPv4 e IPv6, fase 1 --- sources/TCP_echod.c | 2 +- sources/sockbind.c | 32 +++++++++++++++++++------------- sources/sockconn.c | 42 +++++++++++++++++++++++++++++------------- 3 files changed, 49 insertions(+), 27 deletions(-) diff --git a/sources/TCP_echod.c b/sources/TCP_echod.c index 7062c3b..f0eab95 100644 --- a/sources/TCP_echod.c +++ b/sources/TCP_echod.c @@ -116,7 +116,7 @@ int main(int argc, char *argv[]) SignalRestart(SIGCHLD, HandSigCHLD); /* restarting handler */ } /* create and bind socket */ - if ( (list_fd = sockbind(argv[optind], "echo", 6, SOCK_STREAM)) < 0) { + if ( (list_fd = sockbind(NULL, "echo", 6, SOCK_STREAM)) < 0) { if (errno) perror("Socket creation error"); return 1; } diff --git a/sources/sockbind.c b/sources/sockbind.c index 3e31fe7..4ecbe97 100644 --- a/sources/sockbind.c +++ b/sources/sockbind.c @@ -37,7 +37,7 @@ int sockbind(char *host, char *serv, int prot, int type) { - struct addrinfo hint, *addr; + struct addrinfo hint, *addr, *save; int res; int sock; /* initialize hint structure */ @@ -50,20 +50,26 @@ int sockbind(char *host, char *serv, int prot, int type) if (res != 0) { /* on error exit */ printf("sockbind cannot resolve host %s, service %s, ", host, serv); printf("protocol %d: %s\n", prot, gai_strerror(res)); + errno = 0; /* clear errno */ return -1; } - /* get a socket */ - sock = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); - if (sock < 0) { - printf("sockconn cannot create socket\n"); - return sock; + /* loop on possible addresses */ + save = addr; + while (addr != NULL) { + /* get a socket */ + sock = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); + if ( (sock < 0) && (addr->ai_next == NULL) ) { + printf("sockbind cannot create socket\n"); + return sock; + } else continue; + /* connect the socket */ + res = bind(sock, addr->ai_addr, addr->ai_addrlen); + if ( (res < 0) && (addr->ai_next == NULL) ) { + printf("sockbind cannot bind socket\n"); + return res; + } + addr=addr->ai_next; } - /* connect the socket */ - res = bind(sock, addr->ai_addr, addr->ai_addrlen); - if (res < 0) { - printf("sockconn cannot bind socket\n"); - return res; - } - freeaddrinfo(addr); /* done, release memory */ + freeaddrinfo(save); /* done, release memory */ return sock; } diff --git a/sources/sockconn.c b/sources/sockconn.c index 81c982d..952910a 100644 --- a/sources/sockconn.c +++ b/sources/sockconn.c @@ -37,7 +37,7 @@ int sockconn(char *host, char *serv, int prot, int type) { - struct addrinfo hint, *addr; + struct addrinfo hint, *addr, *save; int res; int sock; /* initialize hint structure */ @@ -49,20 +49,36 @@ int sockconn(char *host, char *serv, int prot, int type) if (res != 0) { /* on error exit */ printf("sockconn cannot resolve host %s, service %s, ", host, serv); printf("protocol %d: %s\n", prot, gai_strerror(res)); + errno = 0; /* clear errno */ return -1; } - /* get a socket */ - sock = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); - if (sock < 0) { - printf("sockconn cannot create socket\n"); - return sock; + /* loop on possible addresses */ + save = addr; + while (addr != NULL) { + /* get a socket */ + sock = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); + if (sock < 0) { + if (addr->ai_next == NULL) { + addr=addr->ai_next; + continue; + } else { + printf("sockconn cannot create socket\n"); + return sock; + } + } + /* connect the socket */ + res = connect(sock, addr->ai_addr, addr->ai_addrlen); + if (res < 0) { + if (addr->ai_next == NULL) { + close(sock); + addr=addr->ai_next; + continue; + } else { + printf("sockconn cannot connect socket\n"); + return res; + } + } } - /* connect the socket */ - res = connect(sock, addr->ai_addr, addr->ai_addrlen); - if (res < 0) { - printf("sockconn cannot connect socket\n"); - return res; - } - freeaddrinfo(addr); /* done, release memory */ + freeaddrinfo(save); /* done, release memory */ return sock; } -- 2.30.2