Interface.xs

filter: Statementstmt 26.8% 62 / 231 Branchbran 22.3% 29 / 130 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 25.2% 91 / 361 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/IO/Interface/Simple.pm ↑ Summary
1 /* Interface.xs: part of LibIO-Interface-Perl */
2 /* Copyright 2014 Lincoln D. Stein */
3 /* Licensed under Perl Artistic License 2.0 */
4 /* Please see LICENSE and README.md for more information. */
5  
6 #include "EXTERN.h"
7 #include "perl.h"
8 #include "XSUB.h"
9  
10 #include <stdio.h>
11 #include <string.h>
12  
13 /* socket definitions */
14 #include <sys/types.h>
15 #include <sys/socket.h>
16 #include <sys/ioctl.h>
17  
18 /* location of IFF_* constants */
19 #include <net/if.h>
20  
21 /* location of getifaddrs() definition */
22 #ifdef USE_GETIFADDRS
23 #include <ifaddrs.h>
24  
25 #ifdef HAVE_SOCKADDR_DL_STRUCT
26 #include <net/if_dl.h>
27 #endif
28  
29 #endif
30  
31 #ifndef SIOCGIFCONF
32 #include <sys/sockio.h>
33 #endif
34  
35 #ifdef OSIOCGIFCONF
36 #define MY_SIOCGIFCONF OSIOCGIFCONF
37 #else
38 #define MY_SIOCGIFCONF SIOCGIFCONF
39 #endif
40  
41 #ifdef PerlIO
42 typedef PerlIO * InputStream;
43 #else
44 #define PERLIO_IS_STDIO 1
45 typedef FILE * InputStream;
46 #define PerlIO_fileno(f) fileno(f)
47 #endif
48  
49 #if !defined(__USE_BSD)
50 #if defined(__linux__)
51 typedef int IOCTL_CMD_T;
52 #define __USE_BSD
53 #elif defined(__APPLE__)
54 typedef unsigned long IOCTL_CMD_T;
55 #define __USE_BSD
56 #else
57 typedef int IOCTL_CMD_T;
58 #endif
59 #else
60 typedef unsigned long IOCTL_CMD_T;
61 #endif
62  
63 /* HP-UX, Solaris */
64 #if !defined(ifr_mtu) && defined(ifr_metric)
65 #define ifr_mtu ifr_metric
66 #endif
67  
68 static double
69 0 constant_IFF_N(char *name, int len, int arg)
70 {
71 0 errno = 0;
72 0 ▶ if (5 + 1 >= len ) {
Branch: 1 branch
BranchTrueFalseTotal
if (5 + 1 >= len ) { 0 0 0
73 0 errno = EINVAL;
74 0 return 0;
75 }
76 0 switch (name[5 + 1]) {
77 0 case 'A':
78 0 ▶ if (strEQ(name + 5, "OARP")) { /* IFF_N removed */
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name + 5, "OARP")) { /* IFF_N removed */ 0 0 0
79 #ifdef IFF_NOARP
80 0 return IFF_NOARP;
81 #else
82 goto not_there;
83 #endif
84 }
85 case 'T':
86 0 ▶ if (strEQ(name + 5, "OTRAILERS")) { /* IFF_N removed */
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name + 5, "OTRAILERS")) { /* IFF_N removed */ 0 0 0
87 #ifdef IFF_NOTRAILERS
88 0 return IFF_NOTRAILERS;
89 #else
90 goto not_there;
91 #endif
92 }
93 }
94 0 errno = EINVAL;
95 0 return 0;
96  
97 not_there:
98 errno = ENOENT;
99 return 0;
100 }
101  
102 static double
103 0 constant_IFF_PO(char *name, int len, int arg)
104 {
105 0 errno = 0;
106 0 switch (name[6 + 0]) {
107 0 case 'I':
108 0 ▶ if (strEQ(name + 6, "INTOPOINT")) { /* IFF_PO removed */
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name + 6, "INTOPOINT")) { /* IFF_PO removed */ 0 0 0
109 #ifdef IFF_POINTOPOINT
110 0 return IFF_POINTOPOINT;
111 #else
112 goto not_there;
113 #endif
114 }
115 case 'R':
116 0 ▶ if (strEQ(name + 6, "RTSEL")) { /* IFF_PO removed */
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name + 6, "RTSEL")) { /* IFF_PO removed */ 0 0 0
117 #ifdef IFF_PORTSEL
118 0 return IFF_PORTSEL;
119 #else
120 goto not_there;
121 #endif
122 }
123 }
124 0 errno = EINVAL;
125 0 return 0;
126  
127 not_there:
128 errno = ENOENT;
129 return 0;
130 }
131  
132 static double
133 0 constant_IFF_P(char *name, int len, int arg)
134 {
135 0 errno = 0;
136 0 switch (name[5 + 0]) {
137 0 case 'O':
138 0 return constant_IFF_PO(name, len, arg);
139 0 case 'R':
140 0 ▶ if (strEQ(name + 5, "ROMISC")) { /* IFF_P removed */
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name + 5, "ROMISC")) { /* IFF_P removed */ 0 0 0
141 #ifdef IFF_PROMISC
142 0 return IFF_PROMISC;
143 #else
144 goto not_there;
145 #endif
146 }
147 }
148 0 errno = EINVAL;
149 0 return 0;
150  
151 not_there:
152 errno = ENOENT;
153 return 0;
154 }
155  
156 static double
157 0 constant_IFF_A(char *name, int len, int arg)
158 {
159 0 errno = 0;
160 0 switch (name[5 + 0]) {
161 0 case 'L':
162 0 ▶ if (strEQ(name + 5, "LLMULTI")) { /* IFF_A removed */
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name + 5, "LLMULTI")) { /* IFF_A removed */ 0 0 0
163 #ifdef IFF_ALLMULTI
164 0 return IFF_ALLMULTI;
165 #else
166 goto not_there;
167 #endif
168 }
169 case 'U':
170 0 ▶ if (strEQ(name + 5, "UTOMEDIA")) { /* IFF_A removed */
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name + 5, "UTOMEDIA")) { /* IFF_A removed */ 0 0 0
171 #ifdef IFF_AUTOMEDIA
172 0 return IFF_AUTOMEDIA;
173 #else
174 goto not_there;
175 #endif
176 }
177 }
178 0 errno = EINVAL;
179 0 return 0;
180  
181 not_there:
182 errno = ENOENT;
183 return 0;
184 }
185  
186 static double
187 0 constant_IFF_M(char *name, int len, int arg)
188 {
189 0 errno = 0;
190 0 switch (name[5 + 0]) {
191 0 case 'A':
192 0 ▶ if (strEQ(name + 5, "ASTER")) { /* IFF_M removed */
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name + 5, "ASTER")) { /* IFF_M removed */ 0 0 0
193 #ifdef IFF_MASTER
194 0 return IFF_MASTER;
195 #else
196 goto not_there;
197 #endif
198 }
199 case 'U':
200 0 ▶ if (strEQ(name + 5, "ULTICAST")) { /* IFF_M removed */
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name + 5, "ULTICAST")) { /* IFF_M removed */ 0 0 0
201 #ifdef IFF_MULTICAST
202 0 return IFF_MULTICAST;
203 #else
204 goto not_there;
205 #endif
206 }
207 }
208 0 errno = EINVAL;
209 0 return 0;
210  
211 not_there:
212 errno = ENOENT;
213 return 0;
214 }
215  
216 static double
217 4 constant_IFF(char *name, int len, int arg)
218 {
219 4 errno = 0;
220 4 ▶ if (3 + 1 >= len ) {
Branch: 1 branch
BranchTrueFalseTotal
if (3 + 1 >= len ) { 0 4 4
221 0 errno = EINVAL;
222 0 return 0;
223 }
224 4 switch (name[3 + 1]) {
225 0 case 'A':
226 0 ▶ if (!strnEQ(name + 3,"_", 1))
Branch: 1 branch
BranchTrueFalseTotal
if (!strnEQ(name + 3,"_", 1)) 0 0 0
227 0 break;
228 0 return constant_IFF_A(name, len, arg);
229 0 case 'B':
230 0 ▶ if (strEQ(name + 3, "_BROADCAST")) { /* IFF removed */
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name + 3, "_BROADCAST")) { /* IFF removed */ 0 0 0
231 #ifdef IFF_BROADCAST
232 0 return IFF_BROADCAST;
233 #else
234 goto not_there;
235 #endif
236 }
237 case 'D':
238 0 ▶ if (strEQ(name + 3, "_DEBUG")) { /* IFF removed */
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name + 3, "_DEBUG")) { /* IFF removed */ 0 0 0
239 #ifdef IFF_DEBUG
240 0 return IFF_DEBUG;
241 #else
242 goto not_there;
243 #endif
244 }
245 case 'L':
246 2 ▶ if (strEQ(name + 3, "_LOOPBACK")) { /* IFF removed */
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name + 3, "_LOOPBACK")) { /* IFF removed */ 2 0 2
247 #ifdef IFF_LOOPBACK
248 2 return IFF_LOOPBACK;
249 #else
250 goto not_there;
251 #endif
252 }
253 case 'M':
254 0 ▶ if (!strnEQ(name + 3,"_", 1))
Branch: 1 branch
BranchTrueFalseTotal
if (!strnEQ(name + 3,"_", 1)) 0 0 0
255 0 break;
256 0 return constant_IFF_M(name, len, arg);
257 0 case 'N':
258 0 ▶ if (!strnEQ(name + 3,"_", 1))
Branch: 1 branch
BranchTrueFalseTotal
if (!strnEQ(name + 3,"_", 1)) 0 0 0
259 0 break;
260 0 return constant_IFF_N(name, len, arg);
261 0 case 'P':
262 0 ▶ if (!strnEQ(name + 3,"_", 1))
Branch: 1 branch
BranchTrueFalseTotal
if (!strnEQ(name + 3,"_", 1)) 0 0 0
263 0 break;
264 0 return constant_IFF_P(name, len, arg);
265 1 case 'R':
266 1 ▶ if (strEQ(name + 3, "_RUNNING")) { /* IFF removed */
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name + 3, "_RUNNING")) { /* IFF removed */ 1 0 1
267 #ifdef IFF_RUNNING
268 1 return IFF_RUNNING;
269 #else
270 goto not_there;
271 #endif
272 }
273 case 'S':
274 0 ▶ if (strEQ(name + 3, "_SLAVE")) { /* IFF removed */
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name + 3, "_SLAVE")) { /* IFF removed */ 0 0 0
275 #ifdef IFF_SLAVE
276 0 return IFF_SLAVE;
277 #else
278 goto not_there;
279 #endif
280 }
281 case 'U':
282 1 ▶ if (strEQ(name + 3, "_UP")) { /* IFF removed */
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name + 3, "_UP")) { /* IFF removed */ 1 0 1
283 #ifdef IFF_UP
284 1 return IFF_UP;
285 #else
286 goto not_there;
287 #endif
288 }
289 }
290 0 errno = EINVAL;
291 0 return 0;
292  
293 not_there:
294 errno = ENOENT;
295 return 0;
296 }
297  
298 static double
299 4 constant_I(char *name, int len, int arg)
300 {
301 4 errno = 0;
302 4 ▶ if (1 + 1 >= len ) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 + 1 >= len ) { 0 4 4
303 0 errno = EINVAL;
304 0 return 0;
305 }
306 4 switch (name[1 + 1]) {
307 4 case 'F':
308 4 ▶ if (!strnEQ(name + 1,"F", 1))
Branch: 1 branch
BranchTrueFalseTotal
if (!strnEQ(name + 1,"F", 1)) 0 4 4
309 0 break;
310 4 return constant_IFF(name, len, arg);
311 0 case 'H':
312 0 ▶ if (strEQ(name + 1, "FHWADDRLEN")) { /* I removed */
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name + 1, "FHWADDRLEN")) { /* I removed */ 0 0 0
313 #ifdef IFHWADDRLEN
314 0 return IFHWADDRLEN;
315 #else
316 goto not_there;
317 #endif
318 }
319 case 'N':
320 0 ▶ if (strEQ(name + 1, "FNAMSIZ")) { /* I removed */
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name + 1, "FNAMSIZ")) { /* I removed */ 0 0 0
321 #ifdef IFNAMSIZ
322 0 return IFNAMSIZ;
323 #else
324 goto not_there;
325 #endif
326 }
327 }
328 0 errno = EINVAL;
329 0 return 0;
330  
331 not_there:
332 errno = ENOENT;
333 return 0;
334 }
335  
336 static double
337 4 constant(char *name, int len, int arg)
338 {
339 4 errno = 0;
340 4 ▶ switch (name[0 + 0]) {
Branch: 1 branch
BranchTrueFalseTotal
switch (name[0 + 0]) { 4 0 4
341 4 case 'I':
342 4 return constant_I(name, len, arg);
343 }
344 0 errno = EINVAL;
345 0 return 0;
346  
347 not_there:
348 errno = ENOENT;
349 return 0;
350 }
351  
352 20 int Ioctl (InputStream sock, IOCTL_CMD_T operation,void* result) {
353 20 int fd = PerlIO_fileno(sock);
354 20 return ioctl(fd,operation,result) == 0;
355 }
356  
357 #ifdef IFHWADDRLEN
358 0 char* parse_hwaddr (char *string, struct sockaddr* hwaddr) {
359 int len,i,consumed;
360 unsigned int converted;
361 char* s;
362 0 s = string;
363 0 len = strlen(s);
364 0 ▶ for (i = 0; i < IFHWADDRLEN && len > 0; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < IFHWADDRLEN && len > 0; i++) { 0 0 0
for (i = 0; i < IFHWADDRLEN && len > 0; i++) { 0 0 0
365 0 ▶ if (sscanf(s,"%x%n",&converted,&consumed) <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (sscanf(s,"%x%n",&converted,&consumed) <= 0) 0 0 0
366 0 break;
367 0 hwaddr->sa_data[i] = converted;
368 0 s += consumed + 1;
369 0 len -= consumed + 1;
370 }
371 0 ▶ if (i != IFHWADDRLEN)
Branch: 1 branch
BranchTrueFalseTotal
if (i != IFHWADDRLEN) 0 0 0
372 0 return NULL;
373 else
374 0 return string;
375 }
376  
377 /* No checking for string buffer length. Caller must ensure at least
378 3*4 + 3 + 1 = 16 bytes long */
379 0 char* format_hwaddr (char *string, struct sockaddr* hwaddr) {
380 int i,len;
381 char *s;
382 0 s = string;
383 0 s[0] = '\0';
384 0 ▶ for (i = 0; i < IFHWADDRLEN; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < IFHWADDRLEN; i++) { 0 0 0
385 0 ▶ if (i < IFHWADDRLEN-1)
Branch: 1 branch
BranchTrueFalseTotal
if (i < IFHWADDRLEN-1) 0 0 0
386 0 len = sprintf(s,"%02x:",(unsigned char)hwaddr->sa_data[i]);
387 else
388 0 len = sprintf(s,"%02x",(unsigned char)hwaddr->sa_data[i]);
389 0 s += len;
390 }
391 0 return string;
392 }
393 #endif
394  
395 MODULE = IO::Interface PACKAGE = IO::Interface
396  
397 double
398 constant(sv,arg)
399 PREINIT:
400 STRLEN len;
401 PROTOTYPE: $;$
402 INPUT:
403 SV * sv
404 char * s = SvPV(sv, len);
405 int arg
406 CODE:
407 4 RETVAL = constant(s,len,arg);
408 OUTPUT:
409 RETVAL
410  
411 char*
412 if_addr(sock, name, ...)
413 InputStream sock
414 char* name
415 PROTOTYPE: $$;$
416 PREINIT:
417 STRLEN len;
418 IOCTL_CMD_T operation;
419 struct ifreq ifr;
420 char* newaddr;
421 CODE:
422 {
423 #if !(defined(HAS_IOCTL) && defined(SIOCGIFADDR))
424 XSRETURN_UNDEF;
425 #else
426 6 ▶ if (strncmp(name,"any",3) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strncmp(name,"any",3) == 0) { 0 6 6
427 0 RETVAL = "0.0.0.0";
428 } else {
429 6 bzero((void*)&ifr,sizeof(struct ifreq));
430 6 strncpy(ifr.ifr_name,name,IFNAMSIZ-1);
431 6 ifr.ifr_addr.sa_family = AF_INET;
432 6 ▶ if (items > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 2) { 0 6 6
433 0 newaddr = SvPV(ST(2),len);
434 0 ▶ if ( inet_aton(newaddr,&((struct sockaddr_in*)&ifr.ifr_addr)->sin_addr) == 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ( inet_aton(newaddr,&((struct sockaddr_in*)&ifr.ifr_addr)->sin_addr) == 0 ) 0 0 0
435 0 croak("Invalid inet address");
436 #if defined(SIOCSIFADDR)
437 0 operation = SIOCSIFADDR;
438 #else
439 croak("Cannot set interface address on this platform");
440 #endif
441 } else {
442 6 operation = SIOCGIFADDR;
443 }
444 6 ▶ if (!Ioctl(sock,operation,&ifr)) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!Ioctl(sock,operation,&ifr)) XSRETURN_UNDEF; 0 6 6
445 6 ▶ if (ifr.ifr_addr.sa_family != AF_INET) croak ("Address is not in the AF_INET family.\n");
Branch: 1 branch
BranchTrueFalseTotal
if (ifr.ifr_addr.sa_family != AF_INET) croak ("Address is not in the AF_INET family.\n"); 0 6 6
446 6 RETVAL = inet_ntoa(((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr);
447 }
448 #endif
449 }
450 OUTPUT:
451 RETVAL
452  
453 char*
454 if_broadcast(sock, name, ...)
455 InputStream sock
456 char* name
457 PROTOTYPE: $$;$
458 PREINIT:
459 STRLEN len;
460 IOCTL_CMD_T operation;
461 struct ifreq ifr;
462 char* newaddr;
463 CODE:
464 {
465 #if !(defined(HAS_IOCTL) && defined(SIOCGIFBRDADDR))
466 XSRETURN_UNDEF;
467 #else
468 0 bzero((void*)&ifr,sizeof(struct ifreq));
469 0 strncpy(ifr.ifr_name,name,IFNAMSIZ-1);
470 0 ifr.ifr_addr.sa_family = AF_INET;
471 0 ▶ if (items > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 2) { 0 0 0
472 0 newaddr = SvPV(ST(2),len);
473 0 ▶ if ( inet_aton(newaddr,&((struct sockaddr_in*)&ifr.ifr_addr)->sin_addr) == 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ( inet_aton(newaddr,&((struct sockaddr_in*)&ifr.ifr_addr)->sin_addr) == 0 ) 0 0 0
474 0 croak("Invalid inet address");
475 #if defined(SIOCSIFBRDADDR)
476 0 operation = SIOCSIFBRDADDR;
477 #else
478 croak("Cannot set broadcast address on this platform");
479 #endif
480 } else {
481 0 operation = SIOCGIFBRDADDR;
482 }
483 0 ▶ if (!Ioctl(sock,operation,&ifr)) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!Ioctl(sock,operation,&ifr)) XSRETURN_UNDEF; 0 0 0
484 0 ▶ if (ifr.ifr_addr.sa_family != AF_INET) croak ("Address is not in the AF_INET family.\n");
Branch: 1 branch
BranchTrueFalseTotal
if (ifr.ifr_addr.sa_family != AF_INET) croak ("Address is not in the AF_INET family.\n"); 0 0 0
485 0 RETVAL = inet_ntoa(((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr);
486 #endif
487 }
488 OUTPUT:
489 RETVAL
490  
491 char*
492 if_netmask(sock, name, ...)
493 InputStream sock
494 char* name
495 PROTOTYPE: $$;$
496 PREINIT:
497 STRLEN len;
498 IOCTL_CMD_T operation;
499 struct ifreq ifr;
500 char* newaddr;
501 CODE:
502 {
503 #if !(defined(HAS_IOCTL) && defined(SIOCGIFNETMASK))
504 XSRETURN_UNDEF;
505 #else
506 1 bzero((void*)&ifr,sizeof(struct ifreq));
507 1 strncpy(ifr.ifr_name,name,IFNAMSIZ-1);
508 1 ifr.ifr_addr.sa_family = AF_INET;
509 1 ▶ if (items > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 2) { 0 1 1
510 0 newaddr = SvPV(ST(2),len);
511 0 ▶ if ( inet_aton(newaddr,&((struct sockaddr_in*)&ifr.ifr_addr)->sin_addr) == 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ( inet_aton(newaddr,&((struct sockaddr_in*)&ifr.ifr_addr)->sin_addr) == 0 ) 0 0 0
512 0 croak("Invalid inet address");
513 #if defined(SIOCSIFNETMASK)
514 0 operation = SIOCSIFNETMASK;
515 #else
516 croak("Cannot set netmask on this platform");
517 #endif
518 } else {
519 1 operation = SIOCGIFNETMASK;
520 }
521 1 ▶ if (!Ioctl(sock,operation,&ifr)) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!Ioctl(sock,operation,&ifr)) XSRETURN_UNDEF; 0 1 1
522 #if defined(__NetBSD__) || defined(__OpenBSD__)
523 ifr.ifr_addr.sa_family = AF_INET;
524 #endif
525 1 ▶ if (ifr.ifr_addr.sa_family != AF_INET) croak ("Address is not in the AF_INET family.\n");
Branch: 1 branch
BranchTrueFalseTotal
if (ifr.ifr_addr.sa_family != AF_INET) croak ("Address is not in the AF_INET family.\n"); 0 1 1
526 1 RETVAL = inet_ntoa(((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr);
527 #endif
528 }
529 OUTPUT:
530 RETVAL
531  
532 char*
533 if_dstaddr(sock, name, ...)
534 InputStream sock
535 char* name
536 PROTOTYPE: $$;$
537 PREINIT:
538 STRLEN len;
539 IOCTL_CMD_T operation;
540 struct ifreq ifr;
541 char* newaddr;
542 CODE:
543 {
544 #if !(defined(HAS_IOCTL) && defined(SIOCGIFDSTADDR))
545 XSRETURN_UNDEF;
546 #else
547 0 bzero((void*)&ifr,sizeof(struct ifreq));
548 0 strncpy(ifr.ifr_name,name,IFNAMSIZ-1);
549 0 ifr.ifr_addr.sa_family = AF_INET;
550 0 ▶ if (items > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 2) { 0 0 0
551 0 newaddr = SvPV(ST(2),len);
552 0 ▶ if ( inet_aton(newaddr,&((struct sockaddr_in*)&ifr.ifr_addr)->sin_addr) == 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ( inet_aton(newaddr,&((struct sockaddr_in*)&ifr.ifr_addr)->sin_addr) == 0 ) 0 0 0
553 0 croak("Invalid inet address");
554 #if defined(SIOCSIFDSTADDR)
555 0 operation = SIOCSIFDSTADDR;
556 #else
557 croak("Cannot set destination address on this platform");
558 #endif
559 } else {
560 0 operation = SIOCGIFDSTADDR;
561 }
562 0 ▶ if (!Ioctl(sock,operation,&ifr)) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!Ioctl(sock,operation,&ifr)) XSRETURN_UNDEF; 0 0 0
563 0 ▶ if (ifr.ifr_addr.sa_family != AF_INET) croak ("Address is not in the AF_INET family.\n");
Branch: 1 branch
BranchTrueFalseTotal
if (ifr.ifr_addr.sa_family != AF_INET) croak ("Address is not in the AF_INET family.\n"); 0 0 0
564 0 RETVAL = inet_ntoa(((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr);
565 #endif
566 }
567 OUTPUT:
568 RETVAL
569  
570 char*
571 if_hwaddr(sock, name, ...)
572 InputStream sock
573 char* name
574 PROTOTYPE: $$;$
575 PREINIT:
576 STRLEN len;
577 IOCTL_CMD_T operation;
578 struct ifreq ifr;
579 #if (defined(USE_GETIFADDRS) && defined(HAVE_SOCKADDR_DL_STRUCT))
580 struct ifaddrs *ifap, *ifa;
581 struct sockaddr_dl* sdl;
582 sa_family_t family;
583 char *sdlname, *haddr, *s;
584 int hlen = 0;
585 int i;
586 #endif
587 char *newaddr,hwaddr[128];
588 CODE:
589 {
590 #if !((defined(HAS_IOCTL) && defined(SIOCGIFHWADDR)) || defined(USE_GETIFADDRS))
591 XSRETURN_UNDEF;
592 #endif
593 #if (defined(USE_GETIFADDRS) && defined(HAVE_SOCKADDR_DL_STRUCT))
594 getifaddrs(&ifap);
595  
596 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
597 if (strncmp(name, ifa->ifa_name, IFNAMSIZ) == 0) {
598 family = ifa->ifa_addr->sa_family;
599 if (family == AF_LINK) {
600 sdl = (struct sockaddr_dl *) ifa->ifa_addr;
601 haddr = sdl->sdl_data + sdl->sdl_nlen;
602 hlen = sdl->sdl_alen;
603 break;
604 }
605 }
606 }
607  
608 s = hwaddr;
609 s[0] = '\0';
610 if (ifap != NULL) {
611 for (i = 0; i < hlen; i++) {
612 if (i < hlen - 1)
613 len = sprintf(s,"%02x:",(unsigned char)haddr[i]);
614 else
615 len = sprintf(s,"%02x",(unsigned char)haddr[i]);
616 s += len;
617 }
618 }
619  
620 freeifaddrs(ifap);
621  
622 RETVAL = hwaddr;
623 #elif (defined(HAS_IOCTL) && defined(SIOCGIFHWADDR))
624 0 bzero((void*)&ifr,sizeof(struct ifreq));
625 0 strncpy(ifr.ifr_name,name,IFNAMSIZ-1);
626 0 ifr.ifr_hwaddr.sa_family = AF_UNSPEC;
627 0 ▶ if (items > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 2) { 0 0 0
628 0 newaddr = SvPV(ST(2),len);
629 0 ▶ if (parse_hwaddr(newaddr,&ifr.ifr_hwaddr) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (parse_hwaddr(newaddr,&ifr.ifr_hwaddr) == NULL) 0 0 0
630 0 croak("Invalid hardware address");
631 #if defined(SIOCSIFHWADDR)
632 0 operation = SIOCSIFHWADDR;
633 #else
634 croak("Cannot set hw address on this platform");
635 #endif
636 } else {
637 0 operation = SIOCGIFHWADDR;
638 }
639 0 ▶ if (!Ioctl(sock,operation,&ifr)) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!Ioctl(sock,operation,&ifr)) XSRETURN_UNDEF; 0 0 0
640 0 RETVAL = format_hwaddr(hwaddr,&ifr.ifr_hwaddr);
641 #endif
642 }
643 OUTPUT:
644 RETVAL
645  
646  
647 int
648 if_flags(sock, name, ...)
649 InputStream sock
650 char* name
651 PROTOTYPE: $$;$
652 PREINIT:
653 IOCTL_CMD_T operation;
654 int flags;
655 struct ifreq ifr;
656 CODE:
657 {
658 #if !(defined(HAS_IOCTL) && defined(SIOCGIFFLAGS))
659 XSRETURN_UNDEF;
660 #endif
661 8 bzero((void*)&ifr,sizeof(struct ifreq));
662 8 strncpy(ifr.ifr_name,name,IFNAMSIZ-1);
663 8 ▶ if (items > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 2) { 0 8 8
664 0 ifr.ifr_flags = SvIV(ST(2));
665 #if defined(SIOCSIFFLAGS)
666 0 operation = SIOCSIFFLAGS;
667 #else
668 croak("Cannot set flags on this platform.");
669 #endif
670 } else {
671 8 operation = SIOCGIFFLAGS;
672 }
673 8 ▶ if (!Ioctl(sock,operation,&ifr)) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!Ioctl(sock,operation,&ifr)) XSRETURN_UNDEF; 0 8 8
674 8 ▶ RETVAL = ifr.ifr_flags;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = ifr.ifr_flags; 5 3 8
675 }
676 OUTPUT:
677 RETVAL
678  
679 int
680 if_mtu(sock, name, ...)
681 InputStream sock
682 char* name
683 PROTOTYPE: $$;$
684 PREINIT:
685 IOCTL_CMD_T operation;
686 int flags;
687 struct ifreq ifr;
688 CODE:
689 {
690 #if !(defined(HAS_IOCTL) && defined(SIOCGIFFLAGS))
691 XSRETURN_UNDEF;
692 #endif
693 5 bzero((void*)&ifr,sizeof(struct ifreq));
694 5 strncpy(ifr.ifr_name,name,IFNAMSIZ-1);
695 5 ▶ if (items > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 2) { 0 5 5
696 0 ifr.ifr_flags = SvIV(ST(2));
697 #if defined(SIOCSIFMTU)
698 0 operation = SIOCSIFMTU;
699 #else
700 croak("Cannot set MTU on this platform.");
701 #endif
702 } else {
703 5 operation = SIOCGIFMTU;
704 }
705 5 ▶ if (!Ioctl(sock,operation,&ifr)) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!Ioctl(sock,operation,&ifr)) XSRETURN_UNDEF; 1 4 5
706 4 ▶ RETVAL = ifr.ifr_mtu;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = ifr.ifr_mtu; 3 1 4
707 }
708 OUTPUT:
709 RETVAL
710  
711 int
712 if_metric(sock, name, ...)
713 InputStream sock
714 char* name
715 PROTOTYPE: $$;$
716 PREINIT:
717 IOCTL_CMD_T operation;
718 int flags;
719 struct ifreq ifr;
720 CODE:
721 {
722 #if !(defined(HAS_IOCTL) && defined(SIOCGIFFLAGS))
723 XSRETURN_UNDEF;
724 #endif
725 0 bzero((void*)&ifr,sizeof(struct ifreq));
726 0 strncpy(ifr.ifr_name,name,IFNAMSIZ-1);
727 0 ▶ if (items > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 2) { 0 0 0
728 0 ifr.ifr_flags = SvIV(ST(2));
729 #if defined(SIOCSIFMETRIC)
730 0 operation = SIOCSIFMETRIC;
731 #else
732 croak("Cannot set metric on this platform.");
733 #endif
734 } else {
735 0 operation = SIOCGIFMETRIC;
736 }
737 0 ▶ if (!Ioctl(sock,operation,&ifr)) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!Ioctl(sock,operation,&ifr)) XSRETURN_UNDEF; 0 0 0
738 0 ▶ RETVAL = ifr.ifr_metric;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = ifr.ifr_metric; 0 0 0
739 }
740 OUTPUT:
741 RETVAL
742  
743 int
744 if_index(sock, name, ...)
745 InputStream sock
746 char* name
747 PROTOTYPE: $$;$
748 CODE:
749 {
750 #ifdef __USE_BSD
751 3 ▶ RETVAL = if_nametoindex(name);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = if_nametoindex(name); 2 1 3
752 #else
753 XSRETURN_UNDEF;
754 #endif
755 }
756 OUTPUT:
757 RETVAL
758  
759 char*
760 if_indextoname(sock, index, ...)
761 InputStream sock
762 int index
763 PROTOTYPE: $$;$
764 PREINIT:
765 char name[IFNAMSIZ];
766 CODE:
767 {
768 #ifdef __USE_BSD
769 2 RETVAL = if_indextoname(index,name);
770 #else
771 XSRETURN_UNDEF;
772 #endif
773 }
774 OUTPUT:
775 RETVAL
776  
777 void
778 _if_list(sock)
779 InputStream sock
780 PROTOTYPE: $
781 PREINIT:
782 #ifdef USE_GETIFADDRS
783 struct ifaddrs *ifa_start;
784 struct ifaddrs *ifa;
785 #else
786 struct ifconf ifc;
787 struct ifreq *ifr;
788 int lastlen,len;
789 char *buf,*ptr;
790 #endif
791 PPCODE:
792 #ifdef USE_GETIFADDRS
793 4 ▶ if (getifaddrs(&ifa_start) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (getifaddrs(&ifa_start) < 0) 0 4 4
794 0 XSRETURN_EMPTY;
795  
796 24 ▶ for (ifa = ifa_start ; ifa ; ifa = ifa->ifa_next)
Branch: 1 branch
BranchTrueFalseTotal
for (ifa = ifa_start ; ifa ; ifa = ifa->ifa_next) 20 4 24
797 20 ▶ XPUSHs(sv_2mortal(newSVpv(ifa->ifa_name,0)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpv(ifa->ifa_name,0))); 0 20 20
798  
799 4 freeifaddrs(ifa_start);
800 #else
801 lastlen = 0;
802 len = 10 * sizeof(struct ifreq); /* initial buffer size guess */
803 for ( ; ; ) {
804 if ( (buf = safemalloc(len)) == NULL)
805 croak("Couldn't malloc buffer for ioctl: %s",strerror(errno));
806 ifc.ifc_len = len;
807 ifc.ifc_buf = buf;
808 if (ioctl(PerlIO_fileno(sock),MY_SIOCGIFCONF,&ifc) < 0) {
809 if (errno != EINVAL || lastlen != 0)
810 XSRETURN_EMPTY;
811 } else {
812 if (ifc.ifc_len == lastlen) break; /* success, len has not changed */
813 lastlen = ifc.ifc_len;
814 }
815 len += 10 * sizeof(struct ifreq); /* increment */
816 safefree(buf);
817 }
818
819 for (ptr = buf ; ptr < buf + ifc.ifc_len ; ptr += sizeof(struct ifreq)) {
820 ifr = (struct ifreq*) ptr;
821 XPUSHs(sv_2mortal(newSVpv(ifr->ifr_name,0)));
822 }
823 safefree(buf);
824 #endif
825  
« blib/lib/IO/Interface/Simple.pm ↑ Summary