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;
}
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 */
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;
}
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 */
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;
}