Fix usage of `strcasecmp' in `ping_host_remove'.
[liboping.git] / src / liboping.c
index d74659f..f749301 100644 (file)
@@ -4,8 +4,8 @@
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
+ * the Free Software Foundation; only version 2 of the License is
+ * applicable.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -540,7 +540,17 @@ static ssize_t ping_sendto (pingobj_t *obj, pinghost_t *ph,
                        (struct sockaddr *) ph->addr, ph->addrlen);
 
        if (ret < 0)
+       {
+#if defined(EHOSTUNREACH)
+               if (errno == EHOSTUNREACH)
+                       return (0);
+#endif
+#if defined(ENETUNREACH)
+               if (errno == ENETUNREACH)
+                       return (0);
+#endif
                ping_set_error (obj, "sendto", strerror (errno));
+       }
 
        return (ret);
 }
@@ -890,9 +900,14 @@ int ping_setopt (pingobj_t *obj, int option, void *value)
                        struct addrinfo  ai_hints;
                        struct addrinfo *ai_list;
                        int              status;
-
+#if WITH_DEBUG
+                       if (obj->addrfamily != AF_UNSPEC)
+                       {
+                               dprintf ("Resetting obj->addrfamily to AF_UNSPEC.\n");
+                       }
+#endif
                        memset ((void *) &ai_hints, '\0', sizeof (ai_hints));
-                       ai_hints.ai_family = obj->addrfamily;
+                       ai_hints.ai_family = obj->addrfamily = AF_UNSPEC;
 #if defined(AI_ADDRCONFIG)
                        ai_hints.ai_flags = AI_ADDRCONFIG;
 #endif
@@ -930,6 +945,7 @@ int ping_setopt (pingobj_t *obj, int option, void *value)
                        memcpy ((void *) obj->srcaddr, (const void *) ai_list->ai_addr,
                                        ai_list->ai_addrlen);
                        obj->srcaddrlen = ai_list->ai_addrlen;
+                       obj->addrfamily = ai_list->ai_family;
 
                        freeaddrinfo (ai_list);
                } /* case PING_OPT_SOURCE */
@@ -1086,21 +1102,20 @@ int ping_host_add (pingobj_t *obj, const char *host)
                        continue;
                }
 
-/*
- * The majority vote of operating systems has decided that you don't need to
- * bind here. This code should be reactivated to bind to a specific address,
- * though. See the `-I' option of `ping(1)' (GNU).  -octo
- */
-#if 0
-               if (bind (ph->fd, (struct sockaddr *) &sockaddr, sockaddr_len) == -1)
+               if (obj->srcaddr != NULL)
                {
-                       dprintf ("bind: %s\n", strerror (errno));
-                       ping_set_error (obj, "bind", strerror (errno));
-                       close (ph->fd);
-                       ph->fd = -1;
-                       continue;
+                       assert (obj->srcaddrlen > 0);
+                       assert (obj->srcaddrlen <= sizeof (struct sockaddr_storage));
+
+                       if (bind (ph->fd, (struct sockaddr *) obj->srcaddr, obj->srcaddrlen) == -1)
+                       {
+                               dprintf ("bind: %s\n", strerror (errno));
+                               ping_set_error (obj, "bind", strerror (errno));
+                               close (ph->fd);
+                               ph->fd = -1;
+                               continue;
+                       }
                }
-#endif
 
                assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
                memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
@@ -1177,7 +1192,7 @@ int ping_host_remove (pingobj_t *obj, const char *host)
 
        while (cur != NULL)
        {
-               if (strcasecmp (host, cur->hostname))
+               if (strcasecmp (host, cur->hostname) == 0)
                        break;
 
                pre = cur;