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_host_add (obj, host);
84 pingobj_t *obj
85 const char *host
86 CODE:
87 RETVAL = ping_host_add (obj, host);
88 OUTPUT:
89 RETVAL
91 int
92 _ping_host_remove (obj, host)
93 pingobj_t *obj
94 const char *host
95 CODE:
96 RETVAL = ping_host_remove (obj, host);
97 OUTPUT:
98 RETVAL
100 int
101 _ping_send (obj)
102 pingobj_t *obj
103 CODE:
104 RETVAL=ping_send (obj);
105 OUTPUT:
106 RETVAL
108 pingobj_iter_t *
109 _ping_iterator_get (obj)
110 pingobj_t *obj
111 CODE:
112 RETVAL = ping_iterator_get (obj);
113 OUTPUT:
114 RETVAL
116 pingobj_iter_t *
117 _ping_iterator_next (iter)
118 pingobj_iter_t *iter
119 CODE:
120 RETVAL = ping_iterator_next (iter);
121 OUTPUT:
122 RETVAL
124 double
125 _ping_iterator_get_latency (iter)
126 pingobj_iter_t *iter
127 CODE:
128 double tmp;
129 size_t tmp_size;
130 int status;
132 RETVAL = -1.0;
134 tmp_size = sizeof (tmp);
135 status = ping_iterator_get_info (iter, PING_INFO_LATENCY,
136 (void *) &tmp, &tmp_size);
137 if (status == 0)
138 RETVAL = tmp;
139 OUTPUT:
140 RETVAL
142 void
143 _ping_iterator_get_hostname (iter)
144 pingobj_iter_t *iter
145 PPCODE:
146 char *buffer;
147 size_t buffer_size;
148 int status;
150 do {
151 buffer = NULL;
152 buffer_size = 0;
153 status = ping_iterator_get_info (iter, PING_INFO_HOSTNAME,
154 (void *) buffer, &buffer_size);
155 if (status != ENOMEM)
156 break;
157 #if !defined(OPING_VERSION) || (OPING_VERSION <= 3005)
158 /* This is a workaround for a bug in 0.3.5. */
159 buffer_size++;
160 #endif
162 buffer = (char *) malloc (buffer_size);
163 if (buffer == NULL)
164 break;
166 status = ping_iterator_get_info (iter, PING_INFO_HOSTNAME,
167 (void *) buffer, &buffer_size);
168 if (status != 0)
169 {
170 free (buffer);
171 break;
172 }
174 XPUSHs (sv_2mortal (newSVpvn(buffer,buffer_size)));
175 free(buffer);
176 } while (0);
178 int
179 _ping_iterator_get_dropped (iter)
180 pingobj_iter_t *iter
181 CODE:
182 #if defined(PING_INFO_DROPPED)
183 uint32_t tmp;
184 size_t tmp_size;
185 int status;
187 RETVAL = -1;
189 tmp_size = sizeof (tmp);
190 status = ping_iterator_get_info (iter, PING_INFO_DROPPED,
191 (void *) &tmp, &tmp_size);
192 if (status == 0)
193 RETVAL = (int) tmp;
194 #else
195 RETVAL = -1;
196 #endif
197 OUTPUT:
198 RETVAL
200 int
201 _ping_iterator_get_recv_ttl (iter)
202 pingobj_iter_t *iter
203 CODE:
204 #if defined(PING_INFO_RECV_TTL)
205 int tmp;
206 size_t tmp_size;
207 int status;
209 RETVAL = -1;
211 tmp_size = sizeof (tmp);
212 status = ping_iterator_get_info (iter, PING_INFO_RECV_TTL,
213 (void *) &tmp, &tmp_size);
214 if (status == 0)
215 RETVAL = tmp;
216 #else
217 RETVAL = -1;
218 #endif
219 OUTPUT:
220 RETVAL
222 const char *
223 _ping_get_error (obj)
224 pingobj_t *obj
225 CODE:
226 RETVAL = ping_get_error(obj);
227 OUTPUT:
228 RETVAL