bindings/perl: Add support for the number of dropped packets.
[liboping.git] / bindings / perl / Oping.xs
1 /**
2  * Net-Oping - Oping.xs
3  * Copyright (C) 2007       Olivier Fredj
4  * Copyright (C) 2008,2009  Florian octo Forster
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; only version 2 of the License is
9  * applicable.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  *
20  * Authors:
21  *   Olivier Fredj <ofredj at proxad.net>
22  *   Florian octo Forster <octo at verplant.org>
23  */
24 #include "EXTERN.h"
25 #include "perl.h"
26 #include "XSUB.h"
27
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdint.h>
32 #include <inttypes.h>
33 #include <errno.h>
34 #include <assert.h>
35 #include <netdb.h> /* NI_MAXHOST */
36 #include <oping.h>
37
38 MODULE = Net::Oping             PACKAGE = Net::Oping            
39
40 PROTOTYPES: DISABLE
41
42 pingobj_t *
43 _ping_construct ()
44         CODE:
45                 RETVAL = ping_construct ();
46         OUTPUT:
47                 RETVAL
48
49 void 
50 _ping_destroy (obj);
51         pingobj_t *obj
52         CODE:
53                 ping_destroy(obj);
54
55 int
56 _ping_setopt_timeout (obj, timeout)
57         pingobj_t *obj
58         double timeout
59         CODE:
60                 RETVAL = ping_setopt (obj, PING_OPT_TIMEOUT, &timeout);
61         OUTPUT:
62                 RETVAL
63
64 int
65 _ping_setopt_source (obj, addr)
66         pingobj_t *obj
67         char *addr
68         CODE:
69                 RETVAL = ping_setopt (obj, PING_OPT_SOURCE, addr);
70         OUTPUT:
71                 RETVAL
72
73 int 
74 _ping_host_add (obj, host);
75         pingobj_t *obj
76         const char *host
77         CODE:
78                 RETVAL = ping_host_add (obj, host);
79         OUTPUT:
80                 RETVAL
81
82 int 
83 _ping_host_remove (obj, host)
84         pingobj_t *obj
85         const char *host
86         CODE:
87                 RETVAL = ping_host_remove (obj, host);
88         OUTPUT:
89                 RETVAL
90
91 int 
92 _ping_send (obj)
93         pingobj_t *obj
94         CODE:
95                 RETVAL=ping_send (obj);
96         OUTPUT:
97                 RETVAL
98
99 pingobj_iter_t *
100 _ping_iterator_get (obj)
101         pingobj_t *obj
102         CODE:
103                 RETVAL = ping_iterator_get (obj);
104         OUTPUT:
105                 RETVAL
106
107 pingobj_iter_t *
108 _ping_iterator_next (iter)
109         pingobj_iter_t *iter
110         CODE:
111                 RETVAL = ping_iterator_next (iter);
112         OUTPUT:
113                 RETVAL
114
115 double
116 _ping_iterator_get_latency (iter)
117         pingobj_iter_t *iter
118         CODE:
119                 double tmp;
120                 size_t tmp_size;
121                 int status;
122
123                 RETVAL = -1.0;
124
125                 tmp_size = sizeof (tmp);
126                 status = ping_iterator_get_info (iter, PING_INFO_LATENCY,
127                         (void *) &tmp, &tmp_size);
128                 if (status == 0)
129                         RETVAL = tmp;
130         OUTPUT:
131                 RETVAL
132
133 void
134 _ping_iterator_get_hostname (iter)
135         pingobj_iter_t *iter
136         PPCODE:
137                 char *buffer;
138                 size_t buffer_size;
139                 int status;
140
141         do {
142                 buffer = NULL;
143                 buffer_size = 0;
144                 status = ping_iterator_get_info (iter, PING_INFO_HOSTNAME,
145                                 (void *) buffer, &buffer_size);
146                 if (status != ENOMEM)
147                         break;
148 #if !defined(OPING_VERSION) || (OPING_VERSION <= 3005)
149                 /* This is a workaround for a bug in 0.3.5. */
150                 buffer_size++;
151 #endif
152
153                 buffer = (char *) malloc (buffer_size);
154                 if (buffer == NULL)
155                         break;
156
157                 status = ping_iterator_get_info (iter, PING_INFO_HOSTNAME,
158                                 (void *) buffer, &buffer_size);
159                 if (status != 0)
160                 {
161                         free (buffer);
162                         break;
163                 }
164
165                 XPUSHs (sv_2mortal (newSVpvn(buffer,buffer_size)));
166                 free(buffer);
167         } while (0);
168
169 int
170 _ping_iterator_get_dropped (iter)
171         pingobj_iter_t *iter
172         CODE:
173 #if defined(PING_INFO_DROPPED)
174                 uint32_t tmp;
175                 size_t tmp_size;
176                 int status;
177
178                 RETVAL = -1;
179
180                 tmp_size = sizeof (tmp);
181                 status = ping_iterator_get_info (iter, PING_INFO_DROPPED,
182                         (void *) &tmp, &tmp_size);
183                 if (status == 0)
184                         RETVAL = (int) tmp;
185 #else
186                 RETVAL = -1;
187 #endif
188         OUTPUT:
189                 RETVAL
190
191 const char *
192 _ping_get_error (obj)
193         pingobj_t *obj
194         CODE:
195                 RETVAL = ping_get_error(obj);
196         OUTPUT:
197                 RETVAL