Added the first version of an own ping library..
[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 == EINTR)
368                 {
369                         dprintf ("select was interrupted by signal..\n");
370                         break; /* XXX */
371                         continue;
372                 }
373                 else if (status < 0)
374                 {
375                         dprintf ("select: %s\n", strerror (errno));
376                         break;
377                 }
378                 else if (status == 0)
379                 {
380                         dprintf ("select timed out\n");
381                         break;
382                 }
383
384                 for (ptr = ph; ptr != NULL; ptr = ptr->next)
385                 {
386                         if (FD_ISSET (ptr->fd, &readfds))
387                                 ping_receive_one (ptr->fd, ph);
388                 }
389         }
390         
391         /* FIXME - return correct status */
392         return (0);
393 }
394
395 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
396  * Sending functions:                                                        *
397  *                                                                           *
398  * ping_send_all                                                             *
399  * +-> ping_send_one_ipv4                                                    *
400  * `-> ping_send_one_ipv6                                                    *
401  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
402 static int ping_send_one_ipv4 (pinghost_t *ph)
403 {
404         struct icmphdr *icmp4;
405         int status;
406
407         char buf[4096];
408         int  buflen;
409
410         char *data;
411         int   datalen;
412
413         dprintf ("ph->hostname = %s\n", ph->hostname);
414
415         memset (buf, '\0', sizeof (buf));
416         icmp4 = (struct icmphdr *) buf;
417         data  = (char *) (icmp4 + 1);
418
419         icmp4->type             = ICMP_ECHO;
420         icmp4->code             = 0;
421         /* The checksum will be calculated by the TCP/IP stack.  */
422         icmp4->checksum         = 0;
423         icmp4->un.echo.id       = htons (ph->ident);
424         icmp4->un.echo.sequence = htons (ph->sequence);
425
426         strcpy (data, PING_DATA);
427         datalen = strlen (data);
428
429         buflen = datalen + sizeof (struct icmphdr);
430
431         icmp4->checksum = ping_icmp4_checksum (buf, buflen);
432
433         dprintf ("Sending ICMPv4 package with ID 0x%04x\n", ph->ident);
434
435         status = sendto (ph->fd, buf, buflen, 0,
436                         (struct sockaddr *) ph->addr, ph->addrlen);
437         if (status < 0)
438         {
439                 perror ("sendto");
440                 return (-1);
441         }
442
443         dprintf ("sendto: status = %i\n", status);
444
445         return (0);
446 }
447
448 static int ping_send_one_ipv6 (pinghost_t *ph)
449 {
450         struct icmp6_hdr *icmp6;
451         int status;
452
453         char buf[4096];
454         int  buflen;
455
456         char *data;
457         int   datalen;
458
459         dprintf ("ph->hostname = %s\n", ph->hostname);
460
461         memset (buf, '\0', sizeof (buf));
462         icmp6 = (struct icmp6_hdr *) buf;
463         data  = (char *) (icmp6 + 1);
464
465         icmp6->icmp6_type  = ICMP6_ECHO_REQUEST;
466         icmp6->icmp6_code  = 0;
467         /* The checksum will be calculated by the TCP/IP stack.  */
468         icmp6->icmp6_cksum = 0;
469         icmp6->icmp6_id    = htons (ph->ident);
470         icmp6->icmp6_seq   = htons (ph->sequence);
471
472         strcpy (data, PING_DATA);
473         datalen = strlen (data);
474
475         buflen = datalen + sizeof (struct icmp6_hdr);
476
477         dprintf ("Sending ICMPv6 package with ID 0x%04x\n", ph->ident);
478
479         status = sendto (ph->fd, buf, buflen, 0,
480                         (struct sockaddr *) ph->addr, ph->addrlen);
481         if (status < 0)
482         {
483                 perror ("sendto");
484                 return (-1);
485         }
486
487         dprintf ("sendto: status = %i\n", status);
488
489         return (0);
490 }
491
492 static int ping_send_all (pinghost_t *ph)
493 {
494         pinghost_t *ptr;
495
496         for (ptr = ph; ptr != NULL; ptr = ptr->next)
497         {
498                 /* start timer.. The GNU `ping6' starts the timer before
499                  * sending the packet, so I will do that too */
500                 if (gettimeofday (ptr->timer, NULL) == -1)
501                 {
502                         dprintf ("gettimeofday: %s\n", strerror (errno));
503                         timerclear (ptr->timer);
504                         continue;
505                 }
506                 else
507                 {
508                         dprintf ("timer set for hostname = %s\n", ptr->hostname);
509                 }
510
511                 if (ptr->addrfamily == AF_INET6)
512                 {       
513                         dprintf ("Sending ICMPv6 echo request to `%s'\n", ptr->hostname);
514                         if (ping_send_one_ipv6 (ptr) != 0)
515                         {
516                                 timerclear (ptr->timer);
517                                 continue;
518                         }
519                 }
520                 else if (ptr->addrfamily == AF_INET)
521                 {
522                         dprintf ("Sending ICMPv4 echo request to `%s'\n", ptr->hostname);
523                         if (ping_send_one_ipv4 (ptr) != 0)
524                         {
525                                 timerclear (ptr->timer);
526                                 continue;
527                         }
528                 }
529                 else /* this should not happen */
530                 {
531                         dprintf ("Unknown address family: %i\n", ptr->addrfamily);
532                         timerclear (ptr->timer);
533                         continue;
534                 }
535
536                 ptr->sequence++;
537         }
538
539         /* FIXME */
540         return (0);
541 }
542
543 static int ping_get_ident (void)
544 {
545         int fd;
546         static int did_seed = 0;
547
548         int retval;
549
550         if (did_seed == 0)
551         {
552                 if ((fd = open ("/dev/urandom", O_RDONLY)) != -1)
553                 {
554                         unsigned int seed;
555
556                         if (read (fd, &seed, sizeof (seed)) != -1)
557                         {
558                                 did_seed = 1;
559                                 dprintf ("Random seed: %i\n", seed);
560                                 srandom (seed);
561                         }
562
563                         close (fd);
564                 }
565                 else
566                 {
567                         dprintf ("open (/dev/urandom): %s\n", strerror (errno));
568                 }
569         }
570
571         retval = (int) random ();
572
573         dprintf ("Random number: %i\n", retval);
574         
575         return (retval);
576 }
577
578 static pinghost_t *ping_alloc (void)
579 {
580         pinghost_t *ph;
581         size_t      ph_size;
582
583         ph_size = sizeof (pinghost_t)
584                 + sizeof (struct sockaddr_storage)
585                 + sizeof (struct timeval);
586
587         ph = (pinghost_t *) malloc (ph_size);
588         if (ph == NULL)
589                 return (NULL);
590
591         memset (ph, '\0', ph_size);
592
593         ph->timer   = (struct timeval *) (ph + 1);
594         ph->addr    = (struct sockaddr_storage *) (ph->timer + 1);
595
596         ph->addrlen = sizeof (struct sockaddr_storage);
597         ph->latency = -1.0;
598         ph->ident   = ping_get_ident () & 0xFFFF;
599
600         return (ph);
601 }
602
603 static void ping_free (pinghost_t *ph)
604 {
605         if (ph->hostname != NULL)
606                 free (ph->hostname);
607
608         free (ph);
609 }
610
611 /*
612  * public methods
613  */
614 pingobj_t *ping_construct (int flags)
615 {
616         pingobj_t *obj;
617
618         if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
619                 return (NULL);
620
621         obj->flags = flags;
622         obj->head = NULL;
623
624         return (obj);
625 }
626
627 void ping_destroy (pingobj_t *obj)
628 {
629         pinghost_t *current;
630         pinghost_t *next;
631
632         current = obj->head;
633         next = NULL;
634
635         while (current != NULL)
636         {
637                 next = current->next;
638                 ping_free (current);
639                 current = next;
640         }
641
642         free (obj);
643
644         return;
645 }
646
647 int ping_send (pingobj_t *obj)
648 {
649         if (ping_send_all (obj->head) < 0)
650                 return (-1);
651
652         if (ping_receive_all (obj->head) < 0)
653                 return (-2);
654
655         return (0);
656 }
657
658 static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
659 {
660         while (ph != NULL)
661         {
662                 if (strcasecmp (ph->hostname, host) == 0)
663                         break;
664
665                 ph = ph->next;
666         }
667
668         return (ph);
669 }
670
671 int ping_host_add (pingobj_t *obj, const char *host)
672 {
673         pinghost_t *ph;
674
675         struct sockaddr_storage sockaddr;
676         socklen_t               sockaddr_len;
677
678         struct addrinfo  ai_hints;
679         struct addrinfo *ai_list, *ai_ptr;
680         int              ai_return;
681
682         dprintf ("host = %s\n", host);
683
684         if (ping_host_search (obj->head, host) != NULL)
685                 return (0);
686
687         memset (&ai_hints, '\0', sizeof (ai_hints));
688         ai_hints.ai_flags    = AI_ADDRCONFIG;
689         ai_hints.ai_family   = PF_UNSPEC;
690         ai_hints.ai_socktype = SOCK_RAW;
691
692         if ((ph = ping_alloc ()) == NULL)
693         {
694                 dprintf ("Out of memory!\n");
695                 return (-1);
696         }
697
698         if ((ph->hostname = strdup (host)) == NULL)
699         {
700                 dprintf ("Out of memory!\n");
701                 ping_free (ph);
702                 return (-1);
703         }
704
705         if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
706         {
707                 dprintf ("getaddrinfo failed\n");
708                 ping_free (ph);
709                 return (-1);
710         }
711
712         for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
713         {
714                 ph->fd = -1;
715
716                 sockaddr_len = sizeof (sockaddr);
717                 memset (&sockaddr, '\0', sockaddr_len);
718
719                 if (ai_ptr->ai_family == AF_INET)
720                 {
721                         struct sockaddr_in *si;
722
723                         si = (struct sockaddr_in *) &sockaddr;
724                         si->sin_family = AF_INET;
725                         si->sin_port   = htons (ph->ident);
726                         si->sin_addr.s_addr = htonl (INADDR_ANY);
727
728                         ai_ptr->ai_protocol = IPPROTO_ICMP;
729                 }
730                 else if (ai_ptr->ai_family == AF_INET6)
731                 {
732                         struct sockaddr_in6 *si;
733
734                         si = (struct sockaddr_in6 *) &sockaddr;
735                         si->sin6_family = AF_INET6;
736                         si->sin6_port   = htons (ph->ident);
737                         si->sin6_addr   = in6addr_any;
738
739                         ai_ptr->ai_protocol = IPPROTO_ICMPV6;
740                 }
741                 else
742                 {
743                         dprintf ("Unknown `ai_family': %i\n", ai_ptr->ai_family);
744                         continue;
745                 }
746
747                 ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
748                 if (ph->fd == -1)
749                 {
750                         dprintf ("socket: %s\n", strerror (errno));
751                         continue;
752                 }
753
754                 if (bind (ph->fd, (struct sockaddr *) &sockaddr, sockaddr_len) == -1)
755                 {
756                         dprintf ("bind: %s\n", strerror (errno));
757                         close (ph->fd);
758                         ph->fd = -1;
759                         continue;
760                 }
761
762                 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
763                 memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
764                 memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
765                 ph->addrlen = ai_ptr->ai_addrlen;
766                 ph->addrfamily = ai_ptr->ai_family;
767
768                 break;
769         }
770
771         freeaddrinfo (ai_list);
772
773         if (ph->fd < 0)
774         {
775                 free (ph->hostname);
776                 free (ph);
777                 return (-1);
778         }
779
780         ph->next  = obj->head;
781         obj->head = ph;
782
783         return (0);
784 }
785
786 int ping_host_remove (pingobj_t *obj, const char *host)
787 {
788         pinghost_t *pre, *cur;
789
790         pre = NULL;
791         cur = obj->head;
792
793         while (cur != NULL)
794         {
795                 if (strcasecmp (host, cur->hostname))
796                         break;
797
798                 pre = cur;
799                 cur = cur->next;
800         }
801
802         if (cur == NULL)
803                 return (-1);
804
805         if (pre == NULL)
806                 obj->head = cur->next;
807         else
808                 pre->next = cur->next;
809         
810         if (cur->fd >= 0)
811                 close (cur->fd);
812
813         ping_free (cur);
814
815         return (0);
816 }
817
818 pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
819 {
820         return ((pingobj_iter_t *) obj->head);
821 }
822
823 pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
824 {
825         return ((pingobj_iter_t *) iter->next);
826 }
827
828 const char *ping_iterator_get_host (pingobj_iter_t *iter)
829 {
830         return (iter->hostname);
831 }
832
833 double ping_iterator_get_latency (pingobj_iter_t *iter)
834 {
835         return (iter->latency);
836 }