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_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
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
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
91 int
92 _ping_send (obj)
93 pingobj_t *obj
94 CODE:
95 RETVAL=ping_send (obj);
96 OUTPUT:
97 RETVAL
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
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
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;
123 RETVAL = -1.0;
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
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;
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
153 buffer = (char *) malloc (buffer_size);
154 if (buffer == NULL)
155 break;
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 }
165 XPUSHs (sv_2mortal (newSVpvn(buffer,buffer_size)));
166 free(buffer);
167 } while (0);
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;
178 RETVAL = -1;
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
191 int
192 _ping_iterator_get_recv_ttl (iter)
193 pingobj_iter_t *iter
194 CODE:
195 #if defined(PING_INFO_RECV_TTL)
196 int tmp;
197 size_t tmp_size;
198 int status;
200 RETVAL = -1;
202 tmp_size = sizeof (tmp);
203 status = ping_iterator_get_info (iter, PING_INFO_RECV_TTL,
204 (void *) &tmp, &tmp_size);
205 if (status == 0)
206 RETVAL = tmp;
207 #else
208 RETVAL = -1;
209 #endif
210 OUTPUT:
211 RETVAL
213 const char *
214 _ping_get_error (obj)
215 pingobj_t *obj
216 CODE:
217 RETVAL = ping_get_error(obj);
218 OUTPUT:
219 RETVAL