bindings/perl: Update license information in the XS file.
[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 <errno.h>
32 #include <assert.h>
33 #include <netdb.h> /* NI_MAXHOST */
34 #include <oping.h>
35
36 MODULE = Net::Oping             PACKAGE = Net::Oping            
37
38 PROTOTYPES: DISABLE
39
40 pingobj_t *
41 _ping_construct ()
42         CODE:
43                 RETVAL = ping_construct ();
44         OUTPUT:
45                 RETVAL
46
47 void 
48 _ping_destroy (obj);
49         pingobj_t *obj
50         CODE:
51                 ping_destroy(obj);
52
53 int
54 _ping_setopt_timeout (obj, timeout)
55         pingobj_t *obj
56         double timeout
57         CODE:
58                 RETVAL = ping_setopt (obj, PING_OPT_TIMEOUT, &timeout);
59         OUTPUT:
60                 RETVAL
61
62 int
63 _ping_setopt_source (obj, addr)
64         pingobj_t *obj
65         char *addr
66         CODE:
67                 RETVAL = ping_setopt (obj, PING_OPT_SOURCE, addr);
68         OUTPUT:
69                 RETVAL
70
71 int 
72 _ping_host_add (obj, host);
73         pingobj_t *obj
74         const char *host
75         CODE:
76                 RETVAL = ping_host_add (obj, host);
77         OUTPUT:
78                 RETVAL
79
80 int 
81 _ping_host_remove (obj, host)
82         pingobj_t *obj
83         const char *host
84         CODE:
85                 RETVAL = ping_host_remove (obj, host);
86         OUTPUT:
87                 RETVAL
88
89 int 
90 _ping_send (obj)
91         pingobj_t *obj
92         CODE:
93                 RETVAL=ping_send (obj);
94         OUTPUT:
95                 RETVAL
96
97 pingobj_iter_t *
98 _ping_iterator_get (obj)
99         pingobj_t *obj
100         CODE:
101                 RETVAL = ping_iterator_get (obj);
102         OUTPUT:
103                 RETVAL
104
105 pingobj_iter_t *
106 _ping_iterator_next (iter)
107         pingobj_iter_t *iter
108         CODE:
109                 RETVAL = ping_iterator_next (iter);
110         OUTPUT:
111                 RETVAL
112
113 double
114 _ping_iterator_get_latency (iter)
115         pingobj_iter_t *iter
116         CODE:
117                 double tmp;
118                 size_t tmp_size;
119                 int status;
120
121                 RETVAL = -1.0;
122
123                 tmp_size = sizeof (tmp);
124                 status = ping_iterator_get_info (iter, PING_INFO_LATENCY,
125                         (void *) &tmp, &tmp_size);
126                 if (status == 0)
127                         RETVAL = tmp;
128         OUTPUT:
129                 RETVAL
130
131 void
132 _ping_iterator_get_hostname (iter)
133         pingobj_iter_t *iter
134         PPCODE:
135                 char *buffer;
136                 size_t buffer_size;
137                 int status;
138
139         do {
140                 buffer = NULL;
141                 buffer_size = 0;
142                 status = ping_iterator_get_info (iter, PING_INFO_HOSTNAME,
143                                 (void *) buffer, &buffer_size);
144                 if (status != ENOMEM)
145                         break;
146 #if !defined(OPING_VERSION) || (OPING_VERSION <= 3005)
147                 /* This is a workaround for a bug in 0.3.5. */
148                 buffer_size++;
149 #endif
150
151                 buffer = (char *) malloc (buffer_size);
152                 if (buffer == NULL)
153                         break;
154
155                 status = ping_iterator_get_info (iter, PING_INFO_HOSTNAME,
156                                 (void *) buffer, &buffer_size);
157                 if (status != 0)
158                 {
159                         free (buffer);
160                         break;
161                 }
162
163                 XPUSHs (sv_2mortal (newSVpvn(buffer,buffer_size)));
164                 free(buffer);
165         } while (0);
166
167 const char *
168 _ping_get_error (obj)
169         pingobj_t *obj
170         CODE:
171                 RETVAL = ping_get_error(obj);
172         OUTPUT:
173                 RETVAL