Altre correzioni alle funzioni, con riscrittura di ip_ntop per usare switch,
[gapil.git] / listati / sockbind.c
index ddf48d8962c7a4b4b286bb92e611f5b9a0c3570b..a131e80ff7f1aedd1f15248b09b88303f2f1c609 100644 (file)
@@ -1,32 +1,45 @@
 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 */
+    char buf[INET6_ADDRSTRLEN];
     memset(&hint, 0, sizeof(struct addrinfo)); 
-    hint.ai_flags = AI_PASSIVE;          /* address for binding */
-    hint.ai_family = PF_UNSPEC;          /* generic address (IPv4 or IPv6) */
-    hint.ai_protocol = prot;             /* protocol */
-    hint.ai_socktype = type;             /* socket type */
+    hint.ai_flags = AI_PASSIVE;            /* address for binding */
+    hint.ai_family = PF_UNSPEC;            /* generic address (IPv4 or IPv6) */
+    hint.ai_protocol = prot;               /* protocol */
+    hint.ai_socktype = type;               /* socket type */
     res = getaddrinfo(host, serv, &hint, &addr);   /* calling getaddrinfo */
     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));
+       fprintf(stderr, "sockbind: resolution failed:");
+       fprintf(stderr, " %s\n", 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;
+    save = addr;                           /* saving for freeaddrinfo */
+    while (addr != NULL) {                 /* loop on possible addresses */
+       sock = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
+       if (sock < 0) {                    /* on error */
+           if (addr->ai_next != NULL) {   /* if other addresses */
+               addr=addr->ai_next;        /* take next */
+               continue;                  /* restart cycle */
+           } else {                       /* else stop */
+               perror("sockbind: cannot create socket");
+               return sock;
+           }
+       }
+       if ( (res = bind(sock, addr->ai_addr, addr->ai_addrlen)) < 0) {
+           if (addr->ai_next != NULL) {   /* if other addresses */
+               addr=addr->ai_next;        /* take next */
+               close(sock);               /* close socket */
+               continue;                  /* restart cycle */
+           } else {                       /* else stop */
+               perror("sockbind: cannot connect");
+               close(sock);
+               return res;
+           }
+       } else break;                      /* ok, we are binded! */
     }
-    /* 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;
 }