X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fliboping.c;h=6950103363e052a944a247ea51359f9cb67fa057;hb=38bd1cff27396660917ba42cafa79ea4577b7fa3;hp=0dc956ea06310d337ead1168d96581c152216c3f;hpb=33a84dc3dca4be5fe92061153277eb5df8a54e7f;p=liboping.git diff --git a/src/liboping.c b/src/liboping.c index 0dc956e..6950103 100644 --- a/src/liboping.c +++ b/src/liboping.c @@ -1,6 +1,6 @@ /** * Object oriented C module to send ICMP and ICMPv6 `echo's. - * Copyright (C) 2006 Florian octo Forster + * Copyright (C) 2006-2009 Florian octo Forster * * 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 @@ -25,12 +25,17 @@ # include # include # include +# include # include # include #else # error "You don't have the standard C99 header files installed" #endif /* STDC_HEADERS */ +#ifdef HAVE_STDINT_H +# include +#endif + #if HAVE_UNISTD_H # include #endif @@ -59,6 +64,7 @@ #if HAVE_SYS_SOCKET_H # include #endif + #if HAVE_NETDB_H # include #endif @@ -97,6 +103,9 @@ struct pinghost { + /* username: name passed in by the user */ + char *username; + /* hostname: name returned by the reverse lookup */ char *hostname; struct sockaddr_storage *addr; socklen_t addrlen; @@ -106,6 +115,8 @@ struct pinghost int sequence; struct timeval *timer; double latency; + uint32_t dropped; + int recv_ttl; char *data; void *context; @@ -120,9 +131,11 @@ struct pingobj int addrfamily; char *data; - struct sockaddr_storage *srcaddr; + struct sockaddr *srcaddr; socklen_t srcaddrlen; + char *device; + char errmsg[PING_ERRMSG_LEN]; pinghost_t *head; @@ -131,11 +144,59 @@ struct pingobj /* * private (static) functions */ +/* Even though Posix requires "strerror_r" to return an "int", + * some systems (e.g. the GNU libc) return a "char *" _and_ + * ignore the second argument ... -tokkee */ +static char *sstrerror (int errnum, char *buf, size_t buflen) +{ + buf[0] = 0; + +#if !HAVE_STRERROR_R + { + snprintf (buf, buflen, "Error %i (%#x)", errnum, errnum); + } +/* #endif !HAVE_STRERROR_R */ + +#elif STRERROR_R_CHAR_P + { + char *temp; + temp = strerror_r (errnum, buf, buflen); + if (buf[0] == 0) + { + if ((temp != NULL) && (temp != buf) && (temp[0] != 0)) + strncpy (buf, temp, buflen); + else + strncpy (buf, "strerror_r did not return " + "an error message", buflen); + } + } +/* #endif STRERROR_R_CHAR_P */ + +#else + if (strerror_r (errnum, buf, buflen) != 0) + { + snprintf (buf, buflen, "Error %i (%#x); " + "Additionally, strerror_r failed.", + errnum, errnum); + } +#endif /* STRERROR_R_CHAR_P */ + + buf[buflen - 1] = 0; + + return (buf); +} /* char *sstrerror */ + static void ping_set_error (pingobj_t *obj, const char *function, const char *message) { - snprintf (obj->errmsg, PING_ERRMSG_LEN, "%s: %s", function, message); - obj->errmsg[PING_ERRMSG_LEN - 1] = '\0'; + snprintf (obj->errmsg, sizeof (obj->errmsg), + "%s: %s", function, message); + obj->errmsg[sizeof (obj->errmsg) - 1] = 0; +} + +static void ping_set_errno (pingobj_t *obj, int error_number) +{ + sstrerror (error_number, obj->errmsg, sizeof (obj->errmsg)); } static int ping_timeval_add (struct timeval *tv1, struct timeval *tv2, @@ -156,7 +217,6 @@ static int ping_timeval_add (struct timeval *tv1, struct timeval *tv2, static int ping_timeval_sub (struct timeval *tv1, struct timeval *tv2, struct timeval *res) { - if ((tv1->tv_sec < tv2->tv_sec) || ((tv1->tv_sec == tv2->tv_sec) && (tv1->tv_usec < tv2->tv_usec))) @@ -165,7 +225,7 @@ static int ping_timeval_sub (struct timeval *tv1, struct timeval *tv2, res->tv_sec = tv1->tv_sec - tv2->tv_sec; res->tv_usec = tv1->tv_usec - tv2->tv_usec; - assert ((res->tv_sec > 0) || ((res->tv_sec == 0) && (res->tv_usec > 0))); + assert ((res->tv_sec > 0) || ((res->tv_sec == 0) && (res->tv_usec >= 0))); while (res->tv_usec < 0) { @@ -201,7 +261,8 @@ static uint16_t ping_icmp4_checksum (char *buf, size_t len) return (ret); } -static pinghost_t *ping_receive_ipv4 (pinghost_t *ph, char *buffer, size_t buffer_len) +static pinghost_t *ping_receive_ipv4 (pingobj_t *obj, char *buffer, + size_t buffer_len) { struct ip *ip_hdr; struct icmp *icmp_hdr; @@ -248,7 +309,8 @@ static pinghost_t *ping_receive_ipv4 (pinghost_t *ph, char *buffer, size_t buffe if (recv_checksum != calc_checksum) { - dprintf ("Checksum missmatch: Got 0x%04x, calculated 0x%04x\n", + dprintf ("Checksum missmatch: Got 0x%04"PRIx16", " + "calculated 0x%04"PRIx16"\n", recv_checksum, calc_checksum); return (NULL); } @@ -256,7 +318,9 @@ static pinghost_t *ping_receive_ipv4 (pinghost_t *ph, char *buffer, size_t buffe ident = ntohs (icmp_hdr->icmp_id); seq = ntohs (icmp_hdr->icmp_seq); - for (ptr = ph; ptr != NULL; ptr = ptr->next) + /* We have to iterate over all hosts, since ICMPv4 packets may + * be received on any raw v4 socket. */ + for (ptr = obj->head; ptr != NULL; ptr = ptr->next) { dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n", ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF)); @@ -273,7 +337,8 @@ static pinghost_t *ping_receive_ipv4 (pinghost_t *ph, char *buffer, size_t buffe if (((ptr->sequence - 1) & 0xFFFF) != seq) continue; - dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n", + dprintf ("Match found: hostname = %s, ident = 0x%04"PRIx16", " + "seq = %"PRIu16"\n", ptr->hostname, ident, seq); break; @@ -281,14 +346,34 @@ static pinghost_t *ping_receive_ipv4 (pinghost_t *ph, char *buffer, size_t buffe if (ptr == NULL) { - dprintf ("No match found for ident = 0x%04x, seq = %i\n", + dprintf ("No match found for ident = 0x%04"PRIx16", seq = %"PRIu16"\n", ident, seq); } + if (ptr != NULL) + ptr->recv_ttl = ip_hdr->ip_ttl; + return (ptr); } -static pinghost_t *ping_receive_ipv6 (pinghost_t *ph, char *buffer, size_t buffer_len) +#ifndef ICMP6_ECHO_REQUEST +# ifdef ICMP6_ECHO /* AIX netinet/ip6_icmp.h */ +# define ICMP6_ECHO_REQUEST ICMP6_ECHO +# else +# define ICMP6_ECHO_REQUEST 128 +# endif +#endif + +#ifndef ICMP6_ECHO_REPLY +# ifdef ICMP6_ECHOREPLY /* AIX netinet/ip6_icmp.h */ +# define ICMP6_ECHO_REPLY ICMP6_ECHOREPLY +# else +# define ICMP6_ECHO_REPLY 129 +# endif +#endif + +static pinghost_t *ping_receive_ipv6 (pingobj_t *obj, char *buffer, + size_t buffer_len) { struct icmp6_hdr *icmp_hdr; @@ -319,7 +404,9 @@ static pinghost_t *ping_receive_ipv6 (pinghost_t *ph, char *buffer, size_t buffe ident = ntohs (icmp_hdr->icmp6_id); seq = ntohs (icmp_hdr->icmp6_seq); - for (ptr = ph; ptr != NULL; ptr = ptr->next) + /* We have to iterate over all hosts, since ICMPv6 packets may + * be received on any raw v6 socket. */ + for (ptr = obj->head; ptr != NULL; ptr = ptr->next) { dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n", ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF)); @@ -336,7 +423,8 @@ static pinghost_t *ping_receive_ipv6 (pinghost_t *ph, char *buffer, size_t buffe if (((ptr->sequence - 1) & 0xFFFF) != seq) continue; - dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n", + dprintf ("Match found: hostname = %s, ident = 0x%04"PRIx16", " + "seq = %"PRIu16"\n", ptr->hostname, ident, seq); break; @@ -344,47 +432,133 @@ static pinghost_t *ping_receive_ipv6 (pinghost_t *ph, char *buffer, size_t buffe if (ptr == NULL) { - dprintf ("No match found for ident = 0x%04x, seq = %i\n", + dprintf ("No match found for ident = 0x%04"PRIx16", " + "seq = %"PRIu16"\n", ident, seq); } return (ptr); } -static int ping_receive_one (int fd, pinghost_t *ph, struct timeval *now) +static int ping_receive_one (pingobj_t *obj, const pinghost_t *ph, + struct timeval *now) { - char buffer[4096]; - size_t buffer_len; + /* Note: 'ph' is not necessarily the host object for which we receive a + * reply. The right object will be returned by ping_receive_ipv*(). For + * now, we can only rely on ph->fd and ph->addrfamily. */ struct timeval diff; - pinghost_t *host = NULL; + int recv_ttl; + + /* + * Set up the receive buffer.. + */ + struct msghdr msghdr; + struct cmsghdr *cmsg; + char payload_buffer[4096]; + ssize_t payload_buffer_len; + char control_buffer[4096]; + struct iovec payload_iovec; + + memset (&payload_iovec, 0, sizeof (payload_iovec)); + payload_iovec.iov_base = payload_buffer; + payload_iovec.iov_len = sizeof (payload_buffer); + + memset (&msghdr, 0, sizeof (msghdr)); + /* unspecified source address */ + msghdr.msg_name = NULL; + msghdr.msg_namelen = 0; + /* output buffer vector, see readv(2) */ + msghdr.msg_iov = &payload_iovec; + msghdr.msg_iovlen = 1; + /* output buffer for control messages */ + msghdr.msg_control = control_buffer; + msghdr.msg_controllen = sizeof (control_buffer); + /* flags; this is an output only field.. */ + msghdr.msg_flags = 0; +#ifdef MSG_XPG4_2 + msghdr.msg_flags |= MSG_XPG4_2; +#endif - struct sockaddr_storage sa; - socklen_t sa_len; - - sa_len = sizeof (sa); - - buffer_len = recvfrom (fd, buffer, sizeof (buffer), 0, - (struct sockaddr *) &sa, &sa_len); - if (buffer_len == -1) + payload_buffer_len = recvmsg (ph->fd, &msghdr, /* flags = */ 0); + if (payload_buffer_len < 0) { - dprintf ("recvfrom: %s\n", strerror (errno)); +#if WITH_DEBUG + char errbuf[PING_ERRMSG_LEN]; + dprintf ("recvfrom: %s\n", + sstrerror (errno, errbuf, sizeof (errbuf))); +#endif return (-1); } + dprintf ("Read %zi bytes from fd = %i\n", payload_buffer_len, ph->fd); - dprintf ("Read %u bytes from fd = %i\n", (unsigned int) buffer_len, fd); + /* Iterate over all auxiliary data in msghdr */ + recv_ttl = -1; + for (cmsg = CMSG_FIRSTHDR (&msghdr); /* {{{ */ + cmsg != NULL; + cmsg = CMSG_NXTHDR (&msghdr, cmsg)) + { + if (ph->addrfamily == AF_INET) /* {{{ */ + { + if (cmsg->cmsg_level != IPPROTO_IP) + continue; + + if (cmsg->cmsg_type == IP_TTL) + { + memcpy (&recv_ttl, CMSG_DATA (cmsg), + sizeof (recv_ttl)); + dprintf ("TTLv4 = %i;\n", recv_ttl); + } + else + { + dprintf ("Not handling option %i.\n", + cmsg->cmsg_type); + } + } /* }}} */ + else if (ph->addrfamily == AF_INET6) /* {{{ */ + { + if (cmsg->cmsg_level != IPPROTO_IPV6) + continue; + + if (cmsg->cmsg_type == IPV6_HOPLIMIT) + { + memcpy (&recv_ttl, CMSG_DATA (cmsg), + sizeof (recv_ttl)); + dprintf ("TTLv6 = %i;\n", recv_ttl); + } + else + { + dprintf ("Not handling option %i.\n", + cmsg->cmsg_type); + } + } /* }}} */ + else + { + dprintf ("Don't know how to handle " + "unknown protocol %i.\n", + cmsg->cmsg_level); + } + } /* }}} for (cmsg) */ - if (sa.ss_family == AF_INET) + if (ph->addrfamily == AF_INET) { - if ((host = ping_receive_ipv4 (ph, buffer, buffer_len)) == NULL) + host = ping_receive_ipv4 (obj, payload_buffer, payload_buffer_len); + if (host == NULL) return (-1); } - else if (sa.ss_family == AF_INET6) + else if (ph->addrfamily == AF_INET6) { - if ((host = ping_receive_ipv6 (ph, buffer, buffer_len)) == NULL) + host = ping_receive_ipv6 (obj, payload_buffer, payload_buffer_len); + if (host == NULL) return (-1); } + else + { + dprintf ("ping_receive_one: Unknown address family %i.\n", + ph->addrfamily); + return (-1); + } dprintf ("rcvd: %12i.%06i\n", (int) now->tv_sec, @@ -403,6 +577,9 @@ static int ping_receive_one (int fd, pinghost_t *ph, struct timeval *now) (int) diff.tv_sec, (int) diff.tv_usec); + if (recv_ttl >= 0) + host->recv_ttl = recv_ttl; + host->latency = ((double) diff.tv_usec) / 1000.0; host->latency += ((double) diff.tv_sec) * 1000.0; @@ -431,11 +608,14 @@ static int ping_receive_all (pingobj_t *obj) ret = 0; for (ptr = ph; ptr != NULL; ptr = ptr->next) - ptr->latency = -1.0; + { + ptr->latency = -1.0; + ptr->recv_ttl = -1; + } if (gettimeofday (&nowtime, NULL) == -1) { - ping_set_error (obj, "gettimeofday", strerror (errno)); + ping_set_errno (obj, errno); return (-1); } @@ -472,7 +652,7 @@ static int ping_receive_all (pingobj_t *obj) if (gettimeofday (&nowtime, NULL) == -1) { - ping_set_error (obj, "gettimeofday", strerror (errno)); + ping_set_errno (obj, errno); return (-1); } @@ -487,7 +667,7 @@ static int ping_receive_all (pingobj_t *obj) if (gettimeofday (&nowtime, NULL) == -1) { - ping_set_error (obj, "gettimeofday", strerror (errno)); + ping_set_errno (obj, errno); return (-1); } @@ -498,19 +678,26 @@ static int ping_receive_all (pingobj_t *obj) } else if (status < 0) { - dprintf ("select: %s\n", strerror (errno)); +#if WITH_DEBUG + char errbuf[PING_ERRMSG_LEN]; + dprintf ("select: %s\n", + sstrerror (errno, errbuf, sizeof (errbuf))); +#endif break; } else if (status == 0) { dprintf ("select timed out\n"); + for (ptr = ph; ptr != NULL; ptr = ptr->next) + if (ptr->latency < 0.0) + ptr->dropped++; break; } for (ptr = ph; ptr != NULL; ptr = ptr->next) { if (FD_ISSET (ptr->fd, &readfds)) - if (ping_receive_one (ptr->fd, ph, &nowtime) == 0) + if (ping_receive_one (obj, ptr, &nowtime) == 0) ret++; } } /* while (1) */ @@ -540,7 +727,17 @@ static ssize_t ping_sendto (pingobj_t *obj, pinghost_t *ph, (struct sockaddr *) ph->addr, ph->addrlen); if (ret < 0) - ping_set_error (obj, "sendto", strerror (errno)); + { +#if defined(EHOSTUNREACH) + if (errno == EHOSTUNREACH) + return (0); +#endif +#if defined(ENETUNREACH) + if (errno == ENETUNREACH) + return (0); +#endif + ping_set_errno (obj, errno); + } return (ret); } @@ -651,7 +848,11 @@ static int ping_send_all (pingobj_t *obj) * sending the packet, so I will do that too */ if (gettimeofday (ptr->timer, NULL) == -1) { - dprintf ("gettimeofday: %s\n", strerror (errno)); +#if WITH_DEBUG + char errbuf[PING_ERRMSG_LEN]; + dprintf ("gettimeofday: %s\n", + sstrerror (errno, errbuf, sizeof (errbuf))); +#endif timerclear (ptr->timer); ret--; continue; @@ -704,11 +905,15 @@ static int ping_set_ttl (pinghost_t *ph, int ttl) if (ph->addrfamily == AF_INET) { - ret = setsockopt (ph->fd, IPPROTO_IP, IP_TTL, &ttl, sizeof (ttl)); + dprintf ("Setting TTLv4 to %i\n", ttl); + ret = setsockopt (ph->fd, IPPROTO_IP, IP_TTL, + &ttl, sizeof (ttl)); } else if (ph->addrfamily == AF_INET6) { - ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof (ttl)); + dprintf ("Setting TTLv6 to %i\n", ttl); + ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, + &ttl, sizeof (ttl)); } return (ret); @@ -730,21 +935,25 @@ static int ping_get_ident (void) if (read (fd, &seed, sizeof (seed)) != -1) { did_seed = 1; - dprintf ("Random seed: %i\n", seed); + dprintf ("Random seed: %#x\n", seed); srandom (seed); } close (fd); } +#if WITH_DEBUG else { - dprintf ("open (/dev/urandom): %s\n", strerror (errno)); + char errbuf[PING_ERRMSG_LEN]; + dprintf ("open (/dev/urandom): %s\n", + sstrerror (errno, errbuf, sizeof (errbuf))); } +#endif } retval = (int) random (); - dprintf ("Random number: %i\n", retval); + dprintf ("Random number: %#x\n", retval); return (retval); } @@ -768,7 +977,9 @@ static pinghost_t *ping_alloc (void) ph->addr = (struct sockaddr_storage *) (ph->timer + 1); ph->addrlen = sizeof (struct sockaddr_storage); + ph->fd = -1; ph->latency = -1.0; + ph->dropped = 0; ph->ident = ping_get_ident () & 0xFFFF; return (ph); @@ -776,6 +987,12 @@ static pinghost_t *ping_alloc (void) static void ping_free (pinghost_t *ph) { + if (ph->fd >= 0) + close (ph->fd); + + if (ph->username != NULL) + free (ph->username); + if (ph->hostname != NULL) free (ph->hostname); @@ -790,6 +1007,8 @@ static void ping_free (pinghost_t *ph) */ const char *ping_get_error (pingobj_t *obj) { + if (obj == NULL) + return (NULL); return (obj->errmsg); } @@ -814,6 +1033,9 @@ void ping_destroy (pingobj_t *obj) pinghost_t *current; pinghost_t *next; + if (obj == NULL) + return; + current = obj->head; next = NULL; @@ -830,6 +1052,9 @@ void ping_destroy (pingobj_t *obj) if (obj->srcaddr != NULL) free (obj->srcaddr); + if (obj->device != NULL) + free (obj->device); + free (obj); return; @@ -839,6 +1064,9 @@ int ping_setopt (pingobj_t *obj, int option, void *value) { int ret = 0; + if ((obj == NULL) || (value == NULL)) + return (-1); + switch (option) { case PING_OPT_TIMEOUT: @@ -857,6 +1085,13 @@ int ping_setopt (pingobj_t *obj, int option, void *value) obj->ttl = PING_DEF_TTL; ret = -1; } + else + { + pinghost_t *ph; + + for (ph = obj->head; ph != NULL; ph = ph->next) + ping_set_ttl (ph, obj->ttl); + } break; case PING_OPT_AF: @@ -904,10 +1139,15 @@ int ping_setopt (pingobj_t *obj, int option, void *value) status = getaddrinfo (hostname, NULL, &ai_hints, &ai_list); if (status != 0) { +#if defined(EAI_SYSTEM) + char errbuf[PING_ERRMSG_LEN]; +#endif ping_set_error (obj, "getaddrinfo", - status == EAI_SYSTEM - ? strerror (errno) - : gai_strerror (status)); +#if defined(EAI_SYSTEM) + (status == EAI_SYSTEM) + ? sstrerror (errno, errbuf, sizeof (errbuf)) : +#endif + gai_strerror (status)); ret = -1; break; } @@ -920,17 +1160,16 @@ int ping_setopt (pingobj_t *obj, int option, void *value) if (obj->srcaddr == NULL) { obj->srcaddrlen = 0; - obj->srcaddr = (struct sockaddr_storage *) malloc (sizeof (struct sockaddr_storage)); + obj->srcaddr = malloc (sizeof (struct sockaddr_storage)); if (obj->srcaddr == NULL) { - ping_set_error (obj, "malloc", - strerror (errno)); + ping_set_errno (obj, errno); ret = -1; freeaddrinfo (ai_list); break; } } - memset ((void *) obj->srcaddr, '\0', sizeof (struct sockaddr_storage)); + memset ((void *) obj->srcaddr, 0, sizeof (struct sockaddr_storage)); assert (ai_list->ai_addrlen <= sizeof (struct sockaddr_storage)); memcpy ((void *) obj->srcaddr, (const void *) ai_list->ai_addr, ai_list->ai_addrlen); @@ -941,6 +1180,28 @@ int ping_setopt (pingobj_t *obj, int option, void *value) } /* case PING_OPT_SOURCE */ break; + case PING_OPT_DEVICE: + { +#ifdef SO_BINDTODEVICE + char *device = strdup ((char *) value); + + if (device == NULL) + { + ping_set_errno (obj, errno); + ret = -1; + break; + } + + if (obj->device != NULL) + free (obj->device); + obj->device = device; +#else /* ! SO_BINDTODEVICE */ + ping_set_errno (obj, ENOTSUP); + ret = -1; +#endif /* ! SO_BINDTODEVICE */ + } /* case PING_OPT_DEVICE */ + break; + default: ret = -2; } /* switch (option) */ @@ -953,6 +1214,9 @@ int ping_send (pingobj_t *obj) { int ret; + if (obj == NULL) + return (-1); + if (ping_send_all (obj) < 0) return (-1); @@ -966,7 +1230,7 @@ static pinghost_t *ping_host_search (pinghost_t *ph, const char *host) { while (ph != NULL) { - if (strcasecmp (ph->hostname, host) == 0) + if (strcasecmp (ph->username, host) == 0) break; ph = ph->next; @@ -979,13 +1243,13 @@ int ping_host_add (pingobj_t *obj, const char *host) { pinghost_t *ph; - struct sockaddr_storage sockaddr; - socklen_t sockaddr_len; - struct addrinfo ai_hints; struct addrinfo *ai_list, *ai_ptr; int ai_return; + if ((obj == NULL) || (host == NULL)) + return (-1); + dprintf ("host = %s\n", host); if (ping_host_search (obj->head, host) != NULL) @@ -1008,10 +1272,18 @@ int ping_host_add (pingobj_t *obj, const char *host) return (-1); } + if ((ph->username = strdup (host)) == NULL) + { + dprintf ("Out of memory!\n"); + ping_set_errno (obj, errno); + ping_free (ph); + return (-1); + } + if ((ph->hostname = strdup (host)) == NULL) { dprintf ("Out of memory!\n"); - ping_set_error (obj, "strdup", strerror (errno)); + ping_set_errno (obj, errno); ping_free (ph); return (-1); } @@ -1020,18 +1292,23 @@ int ping_host_add (pingobj_t *obj, const char *host) if ((ph->data = strdup (obj->data == NULL ? PING_DEF_DATA : obj->data)) == NULL) { dprintf ("Out of memory!\n"); - ping_set_error (obj, "strdup", strerror (errno)); + ping_set_errno (obj, errno); ping_free (ph); return (-1); } if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0) { +#if defined(EAI_SYSTEM) + char errbuf[PING_ERRMSG_LEN]; +#endif dprintf ("getaddrinfo failed\n"); ping_set_error (obj, "getaddrinfo", - (ai_return == EAI_SYSTEM) - ? strerror (errno) - : gai_strerror (ai_return)); +#if defined(EAI_SYSTEM) + (ai_return == EAI_SYSTEM) + ? sstrerror (errno, errbuf, sizeof (errbuf)) : +#endif + gai_strerror (ai_return)); ping_free (ph); return (-1); } @@ -1043,30 +1320,13 @@ int ping_host_add (pingobj_t *obj, const char *host) { ph->fd = -1; - sockaddr_len = sizeof (sockaddr); - memset (&sockaddr, '\0', sockaddr_len); - if (ai_ptr->ai_family == AF_INET) { - struct sockaddr_in *si; - - si = (struct sockaddr_in *) &sockaddr; - si->sin_family = AF_INET; - si->sin_port = htons (ph->ident); - si->sin_addr.s_addr = htonl (INADDR_ANY); - ai_ptr->ai_socktype = SOCK_RAW; ai_ptr->ai_protocol = IPPROTO_ICMP; } else if (ai_ptr->ai_family == AF_INET6) { - struct sockaddr_in6 *si; - - si = (struct sockaddr_in6 *) &sockaddr; - si->sin6_family = AF_INET6; - si->sin6_port = htons (ph->ident); - si->sin6_addr = in6addr_any; - ai_ptr->ai_socktype = SOCK_RAW; ai_ptr->ai_protocol = IPPROTO_ICMPV6; } @@ -1087,8 +1347,12 @@ int ping_host_add (pingobj_t *obj, const char *host) ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol); if (ph->fd == -1) { - dprintf ("socket: %s\n", strerror (errno)); - ping_set_error (obj, "socket", strerror (errno)); +#if WITH_DEBUG + char errbuf[PING_ERRMSG_LEN]; + dprintf ("socket: %s\n", + sstrerror (errno, errbuf, sizeof (errbuf))); +#endif + ping_set_errno (obj, errno); continue; } @@ -1097,15 +1361,38 @@ int ping_host_add (pingobj_t *obj, const char *host) assert (obj->srcaddrlen > 0); assert (obj->srcaddrlen <= sizeof (struct sockaddr_storage)); - if (bind (ph->fd, (struct sockaddr *) obj->srcaddr, obj->srcaddrlen) == -1) + if (bind (ph->fd, obj->srcaddr, obj->srcaddrlen) == -1) + { +#if WITH_DEBUG + char errbuf[PING_ERRMSG_LEN]; + dprintf ("bind: %s\n", + sstrerror (errno, errbuf, sizeof (errbuf))); +#endif + ping_set_errno (obj, errno); + close (ph->fd); + ph->fd = -1; + continue; + } + } + +#ifdef SO_BINDTODEVICE + if (obj->device != NULL) + { + if (setsockopt (ph->fd, SOL_SOCKET, SO_BINDTODEVICE, + obj->device, strlen (obj->device) + 1) != 0) { - dprintf ("bind: %s\n", strerror (errno)); - ping_set_error (obj, "bind", strerror (errno)); +#if WITH_DEBUG + char errbuf[PING_ERRMSG_LEN]; + dprintf ("setsockopt: %s\n", + sstrerror (errno, errbuf, sizeof (errbuf))); +#endif + ping_set_errno (obj, errno); close (ph->fd); ph->fd = -1; continue; } } +#endif /* SO_BINDTODEVICE */ assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen); memset (ph->addr, '\0', sizeof (struct sockaddr_storage)); @@ -1135,6 +1422,23 @@ int ping_host_add (pingobj_t *obj, const char *host) } #endif /* AI_CANONNAME */ + if (ph->addrfamily == AF_INET) + { + int opt = 1; + + setsockopt (ph->fd, IPPROTO_IP, IP_RECVTTL, + &opt, sizeof (opt)); + } +#if defined(IPPROTO_IPV6) && defined(IPV6_RECVHOPLIMIT) + else if (ph->addrfamily == AF_INET6) + { + int opt = 1; + + setsockopt (ph->fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, + &opt, sizeof (opt)); + } +#endif + break; } @@ -1142,8 +1446,7 @@ int ping_host_add (pingobj_t *obj, const char *host) if (ph->fd < 0) { - free (ph->hostname); - free (ph); + ping_free (ph); return (-1); } @@ -1171,18 +1474,21 @@ int ping_host_add (pingobj_t *obj, const char *host) ping_set_ttl (ph, obj->ttl); return (0); -} +} /* int ping_host_add */ int ping_host_remove (pingobj_t *obj, const char *host) { pinghost_t *pre, *cur; + if ((obj == NULL) || (host == NULL)) + return (-1); + pre = NULL; cur = obj->head; while (cur != NULL) { - if (strcasecmp (host, cur->hostname)) + if (strcasecmp (host, cur->username) == 0) break; pre = cur; @@ -1200,9 +1506,6 @@ int ping_host_remove (pingobj_t *obj, const char *host) else pre->next = cur->next; - if (cur->fd >= 0) - close (cur->fd); - ping_free (cur); return (0); @@ -1210,11 +1513,15 @@ int ping_host_remove (pingobj_t *obj, const char *host) pingobj_iter_t *ping_iterator_get (pingobj_t *obj) { + if (obj == NULL) + return (NULL); return ((pingobj_iter_t *) obj->head); } pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter) { + if (iter == NULL) + return (NULL); return ((pingobj_iter_t *) iter->next); } @@ -1225,16 +1532,31 @@ int ping_iterator_get_info (pingobj_iter_t *iter, int info, size_t orig_buffer_len = *buffer_len; + if ((iter == NULL) || (buffer == NULL) || (buffer_len == NULL)) + return (-1); + switch (info) { - case PING_INFO_HOSTNAME: + case PING_INFO_USERNAME: ret = ENOMEM; - *buffer_len = strlen (iter->hostname); + *buffer_len = strlen (iter->username) + 1; if (orig_buffer_len <= *buffer_len) break; /* Since (orig_buffer_len > *buffer_len) `strncpy' * will copy `*buffer_len' and pad the rest of * `buffer' with null-bytes */ + strncpy (buffer, iter->username, orig_buffer_len); + ret = 0; + break; + + case PING_INFO_HOSTNAME: + ret = ENOMEM; + *buffer_len = strlen (iter->hostname) + 1; + if (orig_buffer_len < *buffer_len) + break; + /* Since (orig_buffer_len > *buffer_len) `strncpy' + * will copy `*buffer_len' and pad the rest of + * `buffer' with null-bytes */ strncpy (buffer, iter->hostname, orig_buffer_len); ret = 0; break; @@ -1254,9 +1576,10 @@ int ping_iterator_get_info (pingobj_iter_t *iter, int info, #endif ) ret = ENOMEM; +#if defined(EAI_SYSTEM) else if (ret == EAI_SYSTEM) - /* XXX: Not thread-safe! */ ret = errno; +#endif else ret = EINVAL; } @@ -1280,6 +1603,15 @@ int ping_iterator_get_info (pingobj_iter_t *iter, int info, ret = 0; break; + case PING_INFO_DROPPED: + ret = ENOMEM; + *buffer_len = sizeof (uint32_t); + if (orig_buffer_len < sizeof (uint32_t)) + break; + *((uint32_t *) buffer) = iter->dropped; + ret = 0; + break; + case PING_INFO_SEQUENCE: ret = ENOMEM; *buffer_len = sizeof (unsigned int); @@ -1306,17 +1638,30 @@ int ping_iterator_get_info (pingobj_iter_t *iter, int info, strncpy ((char *) buffer, iter->data, orig_buffer_len); ret = 0; break; + + case PING_INFO_RECV_TTL: + ret = ENOMEM; + *buffer_len = sizeof (int); + if (orig_buffer_len < sizeof (int)) + break; + *((int *) buffer) = iter->recv_ttl; + ret = 0; + break; } return (ret); -} +} /* ping_iterator_get_info */ void *ping_iterator_get_context (pingobj_iter_t *iter) { + if (iter == NULL) + return (NULL); return (iter->context); } void ping_iterator_set_context (pingobj_iter_t *iter, void *context) { + if (iter == NULL) + return; iter->context = context; }