Fixed VERY dumb typo in `liboping'
[collectd.git] / src / liboping / liboping.c
1 /**
2  * Object oriented C module to send ICMP and ICMPv6 `echo's.
3  * Copyright (C) 2006  Florian octo Forster <octo at verplant.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  */
19
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <errno.h>
24 #include <string.h>
25
26 #include <assert.h>
27
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31
32 #include <sys/socket.h>
33 #include <netdb.h>
34 #include <netinet/ip_icmp.h>
35 #include <netinet/icmp6.h>
36
37 #include <sys/time.h>
38 #include <time.h>
39
40 #include "liboping.h"
41
42 #if DEBUG
43 # define dprintf(...) printf ("%s[%4i]: %-20s: ", __FILE__, __LINE__, __FUNCTION__); printf (__VA_ARGS__)
44 #else
45 # define dprintf(format, ...) /**/
46 #endif
47
48 #define PING_DATA "Florian Forster <octo@verplant.org> http://verplant.org/"
49
50 static int ping_timeval_sub (struct timeval *tv1, struct timeval *tv2,
51                 struct timeval *res)
52 {
53
54         if ((tv1->tv_sec < tv2->tv_sec)
55                         || ((tv1->tv_sec == tv2->tv_sec)
56                                 && (tv1->tv_usec < tv2->tv_usec)))
57                 return (-1);
58
59         res->tv_sec  = tv1->tv_sec  - tv2->tv_sec;
60         res->tv_usec = tv1->tv_usec - tv2->tv_usec;
61
62         assert ((res->tv_sec > 0) || ((res->tv_sec == 0) && (res->tv_usec > 0)));
63
64         while (res->tv_usec < 0)
65         {
66                 res->tv_usec += 1000000;
67                 res->tv_sec--;
68         }
69
70         return (0);
71 }
72
73 static uint16_t ping_icmp4_checksum (char *buf, size_t len)
74 {
75         uint32_t sum = 0;
76         uint16_t ret = 0;
77
78         uint16_t *ptr;
79
80         for (ptr = (uint16_t *) buf; len > 1; ptr++, len -= 2)
81                 sum += *ptr;
82
83         if (len == 1)
84         {
85                 *(char *) &ret = *(char *) ptr;
86                 sum += ret;
87         }
88
89         /* Do this twice to get all possible carries.. */
90         sum = (sum >> 16) + (sum & 0xFFFF);
91         sum = (sum >> 16) + (sum & 0xFFFF);
92
93         ret = ~sum;
94
95         return (ret);
96 }
97
98 static pinghost_t *ping_receive_ipv4 (pinghost_t *ph, char *buffer, size_t buffer_len)
99 {
100         struct ip *ip_hdr;
101         struct icmphdr *icmp_hdr;
102
103         size_t ip_hdr_len;
104
105         uint16_t recv_checksum;
106         uint16_t calc_checksum;
107
108         uint16_t ident;
109         uint16_t seq;
110
111         pinghost_t *ptr;
112
113         if (buffer_len < sizeof (struct ip))
114                 return (NULL);
115
116         ip_hdr     = (struct ip *) buffer;
117         ip_hdr_len = ip_hdr->ip_hl << 2;
118
119         if (buffer_len < ip_hdr_len)
120                 return (NULL);
121
122         buffer     += ip_hdr_len;
123         buffer_len -= ip_hdr_len;
124
125         if (buffer_len < sizeof (struct icmphdr))
126                 return (NULL);
127
128         icmp_hdr = (struct icmphdr *) buffer;
129         buffer     += sizeof (struct icmphdr);
130         buffer_len -= sizeof (struct icmphdr);
131
132         if (icmp_hdr->type != ICMP_ECHOREPLY)
133         {
134                 dprintf ("Unexpected ICMP type: %i\n", icmp_hdr->type);
135                 return (NULL);
136         }
137
138         recv_checksum = icmp_hdr->checksum;
139         icmp_hdr->checksum = 0;
140         calc_checksum = ping_icmp4_checksum ((char *) icmp_hdr,
141                         sizeof (struct icmphdr) + buffer_len);
142
143         if (recv_checksum != calc_checksum)
144         {
145                 dprintf ("Checksum missmatch: Got 0x%04x, calculated 0x%04x\n",
146                                 recv_checksum, calc_checksum);
147                 return (NULL);
148         }
149
150         ident = ntohs (icmp_hdr->un.echo.id);
151         seq   = ntohs (icmp_hdr->un.echo.sequence);
152
153         for (ptr = ph; ptr != NULL; ptr = ptr->next)
154         {
155                 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
156                                 ptr->hostname, ptr->ident, ptr->sequence - 1);
157
158                 if (ptr->addrfamily != AF_INET)
159                         continue;
160
161                 if (!timerisset (ptr->timer))
162                         continue;
163
164                 if (ptr->ident != ident)
165                         continue;
166
167                 if ((ptr->sequence - 1) != seq)
168                         continue;
169
170                 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
171                                 ptr->hostname, ident, seq);
172
173                 break;
174         }
175
176         if (ptr == NULL)
177         {
178                 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
179                                 ident, seq);
180         }
181
182         return (ptr);
183 }
184
185 static pinghost_t *ping_receive_ipv6 (pinghost_t *ph, char *buffer, size_t buffer_len)
186 {
187         struct icmp6_hdr *icmp_hdr;
188
189         uint16_t ident;
190         uint16_t seq;
191
192         pinghost_t *ptr;
193
194         if (buffer_len < sizeof (struct icmp6_hdr))
195                 return (NULL);
196
197         icmp_hdr = (struct icmp6_hdr *) buffer;
198         buffer     += sizeof (struct icmphdr);
199         buffer_len -= sizeof (struct icmphdr);
200
201         if (icmp_hdr->icmp6_type != ICMP6_ECHO_REPLY)
202         {
203                 dprintf ("Unexpected ICMP type: %02x\n", icmp_hdr->icmp6_type);
204                 return (NULL);
205         }
206
207         if (icmp_hdr->icmp6_code != 0)
208         {
209                 dprintf ("Unexpected ICMP code: %02x\n", icmp_hdr->icmp6_code);
210                 return (NULL);
211         }
212
213         ident = ntohs (icmp_hdr->icmp6_id);
214         seq   = ntohs (icmp_hdr->icmp6_seq);
215
216         for (ptr = ph; ptr != NULL; ptr = ptr->next)
217         {
218                 dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
219                                 ptr->hostname, ptr->ident, ptr->sequence - 1);
220
221                 if (ptr->addrfamily != AF_INET6)
222                         continue;
223
224                 if (!timerisset (ptr->timer))
225                         continue;
226
227                 if (ptr->ident != ident)
228                         continue;
229
230                 if ((ptr->sequence - 1) != seq)
231                         continue;
232
233                 dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
234                                 ptr->hostname, ident, seq);
235
236                 break;
237         }
238
239         if (ptr == NULL)
240         {
241                 dprintf ("No match found for ident = 0x%04x, seq = %i\n",
242                                 ident, seq);
243         }
244
245         return (ptr);
246 }
247
248 static void ping_receive_one (int fd, pinghost_t *ph)
249 {
250         char   buffer[4096];
251         size_t buffer_len;
252
253         struct timeval now;
254         struct timeval diff;
255
256         pinghost_t *host = NULL;
257
258         struct sockaddr_storage sa;
259         socklen_t               sa_len;
260
261         sa_len = sizeof (sa);
262
263         buffer_len = recvfrom (fd, buffer, sizeof (buffer), 0,
264                         (struct sockaddr *) &sa, &sa_len);
265         if (buffer_len == -1)
266         {
267                 dprintf ("recvfrom: %s\n", strerror (errno));
268                 return;
269         }
270
271         dprintf ("Read %i bytes from fd = %i\n", buffer_len, fd);
272
273         if (sa.ss_family == AF_INET)
274         {
275                 if ((host = ping_receive_ipv4 (ph, buffer, buffer_len)) == NULL)
276                         return;
277         }
278         else if (sa.ss_family == AF_INET6)
279         {
280                 if ((host = ping_receive_ipv6 (ph, buffer, buffer_len)) == NULL)
281                         return;
282         }
283
284         if (gettimeofday (&now, NULL) == -1)
285         {
286                 dprintf ("gettimeofday: %s\n", strerror (errno));
287                 timerclear (host->timer);
288                 return;
289         }
290
291         dprintf ("sent: %12i.%06i\n",
292                         (int) host->timer->tv_sec,
293                         (int) host->timer->tv_usec);
294         dprintf ("rcvd: %12i.%06i\n",
295                         (int) now.tv_sec,
296                         (int) now.tv_usec);
297
298         if (ping_timeval_sub (&now, host->timer, &diff) < 0)
299         {
300                 timerclear (host->timer);
301                 return;
302         }
303
304         dprintf ("diff: %12i.%06i\n",
305                         (int) diff.tv_sec,
306                         (int) diff.tv_usec);
307
308         host->latency  = ((double) diff.tv_usec) / 1000.0;
309         host->latency += ((double) diff.tv_sec)  * 1000.0;
310
311         timerclear (host->timer);
312 }
313
314 static int ping_receive_all (pinghost_t *ph)
315 {
316         fd_set readfds;
317         int num_readfds;
318         int max_readfds;
319
320         pinghost_t *ptr;
321
322         struct timeval endtime;
323         struct timeval nowtime;
324         struct timeval timeout;
325         int status;
326
327         if (gettimeofday (&endtime, NULL) == -1)
328                 return (-1);
329         endtime.tv_sec += 1;
330
331         for (ptr = ph; ptr != NULL; ptr = ptr->next)
332                 ptr->latency = -1.0;
333
334         while (1)
335         {
336                 FD_ZERO (&readfds);
337                 num_readfds =  0;
338                 max_readfds = -1;
339
340                 if (gettimeofday (&nowtime, NULL) == -1)
341                         return (-1);
342
343                 if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
344                         return (0);
345
346                 for (ptr = ph; ptr != NULL; ptr = ptr->next)
347                 {
348                         if (!timerisset (ptr->timer))
349                                 continue;
350
351                         FD_SET (ptr->fd, &readfds);
352                         num_readfds++;
353
354                         if (max_readfds < ptr->fd)
355                                 max_readfds = ptr->fd;
356                 }
357
358                 if (num_readfds == 0)
359                         break;
360
361                 dprintf ("Waiting on %i sockets for %i.%06i seconds\n", num_readfds,
362                                 (int) timeout.tv_sec,
363                                 (int) timeout.tv_usec);
364
365                 status = select (max_readfds + 1, &readfds, NULL, NULL, &timeout);
366                 
367                 if ((status == -1) && (errno == EINTR))
368                 {
369                         dprintf ("select was interrupted by signal..\n");
370                         continue;
371                 }
372                 else if (status < 0)
373                 {
374                         dprintf ("select: %s\n", strerror (errno));
375                         break;
376                 }
377                 else if (status == 0)
378                 {
379                         dprintf ("select timed out\n");
380                         break;
381                 }
382
383                 for (ptr = ph; ptr != NULL; ptr = ptr->next)
384                 {
385                         if (FD_ISSET (ptr->fd, &readfds))
386                                 ping_receive_one (ptr->fd, ph);
387                 }
388         }
389         
390         /* FIXME - return correct status */
391         return (0);
392 }
393
394 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
395  * Sending functions:                                                        *
396  *                                                                           *
397  * ping_send_all                                                             *
398  * +-> ping_send_one_ipv4                                                    *
399  * `-> ping_send_one_ipv6                                                    *
400  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
401 static int ping_send_one_ipv4 (pinghost_t *ph)
402 {
403         struct icmphdr *icmp4;
404         int status;
405
406         char buf[4096];
407         int  buflen;
408
409         char *data;
410         int   datalen;
411
412         dprintf ("ph->hostname = %s\n", ph->hostname);
413
414         memset (buf, '\0', sizeof (buf));
415         icmp4 = (struct icmphdr *) buf;
416         data  = (char *) (icmp4 + 1);
417
418         icmp4->type             = ICMP_ECHO;
419         icmp4->code             = 0;
420         /* The checksum will be calculated by the TCP/IP stack.  */
421         icmp4->checksum         = 0;
422         icmp4->un.echo.id       = htons (ph->ident);
423         icmp4->un.echo.sequence = htons (ph->sequence);
424
425         strcpy (data, PING_DATA);
426         datalen = strlen (data);
427
428         buflen = datalen + sizeof (struct icmphdr);
429
430         icmp4->checksum = ping_icmp4_checksum (buf, buflen);
431
432         dprintf ("Sending ICMPv4 package with ID 0x%04x\n", ph->ident);
433
434         status = sendto (ph->fd, buf, buflen, 0,
435                         (struct sockaddr *) ph->addr, ph->addrlen);
436         if (status < 0)
437         {
438                 perror ("sendto");
439                 return (-1);
440         }
441
442         dprintf ("sendto: status = %i\n", status);
443
444         return (0);
445 }
446
447 static int ping_send_one_ipv6 (pinghost_t *ph)
448 {
449         struct icmp6_hdr *icmp6;
450         int status;
451
452         char buf[4096];
453         int  buflen;
454
455         char *data;
456         int   datalen;
457
458         dprintf ("ph->hostname = %s\n", ph->hostname);
459
460         memset (buf, '\0', sizeof (buf));
461         icmp6 = (struct icmp6_hdr *) buf;
462         data  = (char *) (icmp6 + 1);
463
464         icmp6->icmp6_type  = ICMP6_ECHO_REQUEST;
465         icmp6->icmp6_code  = 0;
466         /* The checksum will be calculated by the TCP/IP stack.  */
467         icmp6->icmp6_cksum = 0;
468         icmp6->icmp6_id    = htons (ph->ident);
469         icmp6->icmp6_seq   = htons (ph->sequence);
470
471         strcpy (data, PING_DATA);
472         datalen = strlen (data);
473
474         buflen = datalen + sizeof (struct icmp6_hdr);
475
476         dprintf ("Sending ICMPv6 package with ID 0x%04x\n", ph->ident);
477
478         status = sendto (ph->fd, buf, buflen, 0,
479                         (struct sockaddr *) ph->addr, ph->addrlen);
480         if (status < 0)
481         {
482                 perror ("sendto");
483                 return (-1);
484         }
485
486         dprintf ("sendto: status = %i\n", status);
487
488         return (0);
489 }
490
491 static int ping_send_all (pinghost_t *ph)
492 {
493         pinghost_t *ptr;
494
495         for (ptr = ph; ptr != NULL; ptr = ptr->next)
496         {
497                 /* start timer.. The GNU `ping6' starts the timer before
498                  * sending the packet, so I will do that too */
499                 if (gettimeofday (ptr->timer, NULL) == -1)
500                 {
501                         dprintf ("gettimeofday: %s\n", strerror (errno));
502                         timerclear (ptr->timer);
503                         continue;
504                 }
505                 else
506                 {
507                         dprintf ("timer set for hostname = %s\n", ptr->hostname);
508                 }
509
510                 if (ptr->addrfamily == AF_INET6)
511                 {       
512                         dprintf ("Sending ICMPv6 echo request to `%s'\n", ptr->hostname);
513                         if (ping_send_one_ipv6 (ptr) != 0)
514                         {
515                                 timerclear (ptr->timer);
516                                 continue;
517                         }
518                 }
519                 else if (ptr->addrfamily == AF_INET)
520                 {
521                         dprintf ("Sending ICMPv4 echo request to `%s'\n", ptr->hostname);
522                         if (ping_send_one_ipv4 (ptr) != 0)
523                         {
524                                 timerclear (ptr->timer);
525                                 continue;
526                         }
527                 }
528                 else /* this should not happen */
529                 {
530                         dprintf ("Unknown address family: %i\n", ptr->addrfamily);
531                         timerclear (ptr->timer);
532                         continue;
533                 }
534
535                 ptr->sequence++;
536         }
537
538         /* FIXME */
539         return (0);
540 }
541
542 static int ping_get_ident (void)
543 {
544         int fd;
545         static int did_seed = 0;
546
547         int retval;
548
549         if (did_seed == 0)
550         {
551                 if ((fd = open ("/dev/urandom", O_RDONLY)) != -1)
552                 {
553                         unsigned int seed;
554
555                         if (read (fd, &seed, sizeof (seed)) != -1)
556                         {
557                                 did_seed = 1;
558                                 dprintf ("Random seed: %i\n", seed);
559                                 srandom (seed);
560                         }
561
562                         close (fd);
563                 }
564                 else
565                 {
566                         dprintf ("open (/dev/urandom): %s\n", strerror (errno));
567                 }
568         }
569
570         retval = (int) random ();
571
572         dprintf ("Random number: %i\n", retval);
573         
574         return (retval);
575 }
576
577 static pinghost_t *ping_alloc (void)
578 {
579         pinghost_t *ph;
580         size_t      ph_size;
581
582         ph_size = sizeof (pinghost_t)
583                 + sizeof (struct sockaddr_storage)
584                 + sizeof (struct timeval);
585
586         ph = (pinghost_t *) malloc (ph_size);
587         if (ph == NULL)
588                 return (NULL);
589
590         memset (ph, '\0', ph_size);
591
592         ph->timer   = (struct timeval *) (ph + 1);
593         ph->addr    = (struct sockaddr_storage *) (ph->timer + 1);
594
595         ph->addrlen = sizeof (struct sockaddr_storage);
596         ph->latency = -1.0;
597         ph->ident   = ping_get_ident () & 0xFFFF;
598
599         return (ph);
600 }
601
602 static void ping_free (pinghost_t *ph)
603 {
604         if (ph->hostname != NULL)
605                 free (ph->hostname);
606
607         free (ph);
608 }
609
610 /*
611  * public methods
612  */
613 pingobj_t *ping_construct (int flags)
614 {
615         pingobj_t *obj;
616
617         if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
618                 return (NULL);
619
620         obj->flags = flags;
621         obj->head = NULL;
622
623         return (obj);
624 }
625
626 void ping_destroy (pingobj_t *obj)
627 {
628         pinghost_t *current;
629         pinghost_t *next;
630
631         current = obj->head;
632         next = NULL;
633
634         while (current != NULL)
635         {
636                 next = current->next;
637                 ping_free (current);
638                 current = next;
639         }
640
641         free (obj);
642
643         return;
644 }
645
646 int ping_send (pingobj_t *obj)
647 {
648         if (ping_send_all (obj->head) < 0)
649                 return (-1);
650
651         if (ping_receive_all (obj->head) < 0)
652                 return (-2);
653
654         return (0);
655 }
656
657 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
658 {
659         while (ph != NULL)
660         {
661                 if (strcasecmp (ph->hostname, host) == 0)
662                         break;
663
664                 ph = ph->next;
665         }
666
667         return (ph);
668 }
669
670 int ping_host_add (pingobj_t *obj, const char *host)
671 {
672         pinghost_t *ph;
673
674         struct sockaddr_storage sockaddr;
675         socklen_t               sockaddr_len;
676
677         struct addrinfo  ai_hints;
678         struct addrinfo *ai_list, *ai_ptr;
679         int              ai_return;
680
681         dprintf ("host = %s\n", host);
682
683         if (ping_host_search (obj->head, host) != NULL)
684                 return (0);
685
686         memset (&ai_hints, '\0', sizeof (ai_hints));
687         ai_hints.ai_flags    = AI_ADDRCONFIG;
688         ai_hints.ai_family   = PF_UNSPEC;
689         ai_hints.ai_socktype = SOCK_RAW;
690
691         if ((ph = ping_alloc ()) == NULL)
692         {
693                 dprintf ("Out of memory!\n");
694                 return (-1);
695         }
696
697         if ((ph->hostname = strdup (host)) == NULL)
698         {
699                 dprintf ("Out of memory!\n");
700                 ping_free (ph);
701                 return (-1);
702         }
703
704         if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
705         {
706                 dprintf ("getaddrinfo failed\n");
707                 ping_free (ph);
708                 return (-1);
709         }
710
711         for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
712         {
713                 ph->fd = -1;
714
715                 sockaddr_len = sizeof (sockaddr);
716                 memset (&sockaddr, '\0', sockaddr_len);
717
718                 if (ai_ptr->ai_family == AF_INET)
719                 {
720                         struct sockaddr_in *si;
721
722                         si = (struct sockaddr_in *) &sockaddr;
723                         si->sin_family = AF_INET;
724                         si->sin_port   = htons (ph->ident);
725                         si->sin_addr.s_addr = htonl (INADDR_ANY);
726
727                         ai_ptr->ai_protocol = IPPROTO_ICMP;
728                 }
729                 else if (ai_ptr->ai_family == AF_INET6)
730                 {
731                         struct sockaddr_in6 *si;
732
733                         si = (struct sockaddr_in6 *) &sockaddr;
734                         si->sin6_family = AF_INET6;
735                         si->sin6_port   = htons (ph->ident);
736                         si->sin6_addr   = in6addr_any;
737
738                         ai_ptr->ai_protocol = IPPROTO_ICMPV6;
739                 }
740                 else
741                 {
742                         dprintf ("Unknown `ai_family': %i\n", ai_ptr->ai_family);
743                         continue;
744                 }
745
746                 ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
747                 if (ph->fd == -1)
748                 {
749                         dprintf ("socket: %s\n", strerror (errno));
750                         continue;
751                 }
752
753                 if (bind (ph->fd, (struct sockaddr *) &sockaddr, sockaddr_len) == -1)
754                 {
755                         dprintf ("bind: %s\n", strerror (errno));
756                         close (ph->fd);
757                         ph->fd = -1;
758                         continue;
759                 }
760
761                 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
762                 memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
763                 memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
764                 ph->addrlen = ai_ptr->ai_addrlen;
765                 ph->addrfamily = ai_ptr->ai_family;
766
767                 break;
768         }
769
770         freeaddrinfo (ai_list);
771
772         if (ph->fd < 0)
773         {
774                 free (ph->hostname);
775                 free (ph);
776                 return (-1);
777         }
778
779         ph->next  = obj->head;
780         obj->head = ph;
781
782         return (0);
783 }
784
785 int ping_host_remove (pingobj_t *obj, const char *host)
786 {
787         pinghost_t *pre, *cur;
788
789         pre = NULL;
790         cur = obj->head;
791
792         while (cur != NULL)
793         {
794                 if (strcasecmp (host, cur->hostname))
795                         break;
796
797                 pre = cur;
798                 cur = cur->next;
799         }
800
801         if (cur == NULL)
802                 return (-1);
803
804         if (pre == NULL)
805                 obj->head = cur->next;
806         else
807                 pre->next = cur->next;
808         
809         if (cur->fd >= 0)
810                 close (cur->fd);
811
812         ping_free (cur);
813
814         return (0);
815 }
816
817 pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
818 {
819         return ((pingobj_iter_t *) obj->head);
820 }
821
822 pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
823 {
824         return ((pingobj_iter_t *) iter->next);
825 }
826
827 const char *ping_iterator_get_host (pingobj_iter_t *iter)
828 {
829         return (iter->hostname);
830 }
831
832 double ping_iterator_get_latency (pingobj_iter_t *iter)
833 {
834         return (iter->latency);
835 }