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"
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>
38 MODULE = Net::Oping PACKAGE = Net::Oping
40 PROTOTYPES: DISABLE
42 pingobj_t *
43 _ping_construct ()
44 CODE:
45 RETVAL = ping_construct ();
46 OUTPUT:
47 RETVAL
49 void
50 _ping_destroy (obj);
51 pingobj_t *obj
52 CODE:
53 ping_destroy(obj);
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
64 int
65 _ping_setopt_ttl (obj, ttl)
66 pingobj_t *obj
67 int ttl
68 CODE:
69 RETVAL = ping_setopt (obj, PING_OPT_TTL, &ttl);
70 OUTPUT:
71 RETVAL
73 int
74 _ping_setopt_source (obj, addr)
75 pingobj_t *obj
76 char *addr
77 CODE:
78 RETVAL = ping_setopt (obj, PING_OPT_SOURCE, addr);
79 OUTPUT:
80 RETVAL
82 int
83 _ping_setopt_device (obj, dev)
84 pingobj_t *obj
85 char *dev
86 CODE:
87 #if OPING_VERSION >= 1003000
88 RETVAL = ping_setopt (obj, PING_OPT_DEVICE, dev);
89 #else
90 RETVAL = -95;
91 #endif
92 OUTPUT:
93 RETVAL
95 int
96 _ping_host_add (obj, host);
97 pingobj_t *obj
98 const char *host
99 CODE:
100 RETVAL = ping_host_add (obj, host);
101 OUTPUT:
102 RETVAL
104 int
105 _ping_host_remove (obj, host)
106 pingobj_t *obj
107 const char *host
108 CODE:
109 RETVAL = ping_host_remove (obj, host);
110 OUTPUT:
111 RETVAL
113 int
114 _ping_send (obj)
115 pingobj_t *obj
116 CODE:
117 RETVAL=ping_send (obj);
118 OUTPUT:
119 RETVAL
121 pingobj_iter_t *
122 _ping_iterator_get (obj)
123 pingobj_t *obj
124 CODE:
125 RETVAL = ping_iterator_get (obj);
126 OUTPUT:
127 RETVAL
129 pingobj_iter_t *
130 _ping_iterator_next (iter)
131 pingobj_iter_t *iter
132 CODE:
133 RETVAL = ping_iterator_next (iter);
134 OUTPUT:
135 RETVAL
137 double
138 _ping_iterator_get_latency (iter)
139 pingobj_iter_t *iter
140 CODE:
141 double tmp;
142 size_t tmp_size;
143 int status;
145 RETVAL = -1.0;
147 tmp_size = sizeof (tmp);
148 status = ping_iterator_get_info (iter, PING_INFO_LATENCY,
149 (void *) &tmp, &tmp_size);
150 if (status == 0)
151 RETVAL = tmp;
152 OUTPUT:
153 RETVAL
155 void
156 _ping_iterator_get_hostname (iter)
157 pingobj_iter_t *iter
158 PPCODE:
159 char *buffer;
160 size_t buffer_size;
161 int status;
163 do {
164 buffer = NULL;
165 buffer_size = 0;
166 status = ping_iterator_get_info (iter, PING_INFO_HOSTNAME,
167 (void *) buffer, &buffer_size);
168 if (status != ENOMEM)
169 break;
170 #if !defined(OPING_VERSION) || (OPING_VERSION <= 3005)
171 /* This is a workaround for a bug in 0.3.5. */
172 buffer_size++;
173 #endif
175 buffer = (char *) malloc (buffer_size);
176 if (buffer == NULL)
177 break;
179 status = ping_iterator_get_info (iter, PING_INFO_HOSTNAME,
180 (void *) buffer, &buffer_size);
181 if (status != 0)
182 {
183 free (buffer);
184 break;
185 }
186 buffer[buffer_size - 1] = 0;
188 XPUSHs (sv_2mortal (newSVpvn(buffer, strlen (buffer))));
189 free(buffer);
190 } while (0);
192 int
193 _ping_iterator_get_dropped (iter)
194 pingobj_iter_t *iter
195 CODE:
196 #if defined(PING_INFO_DROPPED)
197 uint32_t tmp;
198 size_t tmp_size;
199 int status;
201 RETVAL = -1;
203 tmp_size = sizeof (tmp);
204 status = ping_iterator_get_info (iter, PING_INFO_DROPPED,
205 (void *) &tmp, &tmp_size);
206 if (status == 0)
207 RETVAL = (int) tmp;
208 #else
209 RETVAL = -1;
210 #endif
211 OUTPUT:
212 RETVAL
214 int
215 _ping_iterator_get_recv_ttl (iter)
216 pingobj_iter_t *iter
217 CODE:
218 #if defined(PING_INFO_RECV_TTL)
219 int tmp;
220 size_t tmp_size;
221 int status;
223 RETVAL = -1;
225 tmp_size = sizeof (tmp);
226 status = ping_iterator_get_info (iter, PING_INFO_RECV_TTL,
227 (void *) &tmp, &tmp_size);
228 if (status == 0)
229 RETVAL = tmp;
230 #else
231 RETVAL = -1;
232 #endif
233 OUTPUT:
234 RETVAL
236 const char *
237 _ping_get_error (obj)
238 pingobj_t *obj
239 CODE:
240 RETVAL = ping_get_error(obj);
241 OUTPUT:
242 RETVAL