Interface.xs

filter: Statementstmt 36.9% 31 / 84 Branchbran 25.0% 11 / 44 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 32.8% 42 / 128 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« const-xs.inc ↑ Summary blib/lib/Socket/More/Interface.pm »
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5  
6 #include "ppport.h"
7  
8 #include "const-c.inc"
9  
10  
11 #include <sys/types.h>
12 #include <sys/socket.h>
13 #include <net/if.h>
14 #include <ifaddrs.h>
15 #include <sys/un.h>
16  
17 //Copy a sockaddr into an sv. BSD type systems have length field in struct
18 //sockaddr linux does not. So we cast the pointer to the correct family and
19 //then access the length field
20 //Returns a SV with the full sockaddr_* data
21 10 SV* sv_from_sockaddr(pTHX_ struct sockaddr *sockaddr){
22  
23  
24 10 switch(sockaddr->sa_family){
25 6 case AF_INET:
26 6 return newSVpv((char *)sockaddr, sizeof(struct sockaddr_in));
27 break;
28 2 case AF_INET6:
29 2 return newSVpv((char *)sockaddr, sizeof(struct sockaddr_in6));
30 break;
31 0 case AF_UNIX:
32 0 return newSVpv((char *)sockaddr, sizeof(struct sockaddr_un));
33 break;
34 2 default:
35 //TODO: use sockaddr_storage on linux?
36 2 return newSVpv((char *)sockaddr, sizeof(struct sockaddr));
37 break;
38 }
39 return NULL;
40 }
41  
42  
43 MODULE = Socket::More::Interface PACKAGE = Socket::More::Interface
44  
45 INCLUDE: const-xs.inc
46  
47 void
48 getifaddrs()
49  
50  
51 INIT:
52 struct ifaddrs *a;
53 struct ifaddrs *next;
54 int ret;
55 HV* h;
56 1 UV count=0;
57  
58 PPCODE:
59 1 ret=getifaddrs(&a);
60  
61 1 ▶ if(ret<0){
Branch: 1 branch
BranchTrueFalseTotal
if(ret<0){ 0 1 1
62 0 switch(GIMME_V){
63 0 case G_SCALAR:
64 case G_VOID:
65 0 XSRETURN_UNDEF;
66 break;
67 0 case G_ARRAY:
68 0 XSRETURN_EMPTY;
69 break;
70 0 default:
71 0 break;
72 }
73 }
74 else{
75 1 next=a->ifa_next;
76 1 switch(GIMME_V){
77 0 case G_SCALAR:
78 0 ▶ while(next){
Branch: 1 branch
BranchTrueFalseTotal
while(next){ 0 0 0
79 0 count++;
80 0 next=next->ifa_next;
81 }
82 //Free contents
83 0 freeifaddrs(a);
84 0 ▶ mXPUSHs(newSVuv(count));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newSVuv(count)); 0 0 0
85 0 XSRETURN(1);
86 break;
87  
88 0 case G_VOID:
89 0 XSRETURN_UNDEF;
90 break;
91  
92 1 case G_ARRAY:
93  
94 //Copy contents
95 5 ▶ while(next){
Branch: 1 branch
BranchTrueFalseTotal
while(next){ 4 1 5
96 //Create hash
97 4 h=newHV();
98 //Copy Values
99 4 ▶ if(next->ifa_name){
Branch: 1 branch
BranchTrueFalseTotal
if(next->ifa_name){ 4 0 4
100 4 hv_stores(h, "name", newSVpv(next->ifa_name,0));
101 }
102 4 ▶ if(next->ifa_flags){
Branch: 1 branch
BranchTrueFalseTotal
if(next->ifa_flags){ 4 0 4
103 4 hv_stores(h, "flags",newSVuv(next->ifa_flags));
104 }
105 4 ▶ if(next->ifa_addr){
Branch: 1 branch
BranchTrueFalseTotal
if(next->ifa_addr){ 4 0 4
106 4 hv_stores(h, "addr", sv_from_sockaddr(aTHX_ next->ifa_addr));
107 }
108 4 ▶ if(next->ifa_netmask){
Branch: 1 branch
BranchTrueFalseTotal
if(next->ifa_netmask){ 3 1 4
109 3 hv_stores(h, "netmask", sv_from_sockaddr(aTHX_ next->ifa_netmask));
110 }
111 4 ▶ if(next->ifa_dstaddr){
Branch: 1 branch
BranchTrueFalseTotal
if(next->ifa_dstaddr){ 3 1 4
112 3 hv_stores(h, "dstaddr", sv_from_sockaddr(aTHX_ next->ifa_dstaddr));
113 }
114  
115 //a->ifa_data... read into this more
116 4 next=next->ifa_next;
117 4 ▶ mXPUSHs(newRV((SV *)h));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV((SV *)h)); 0 4 4
118 4 count++;
119 }
120 //Free contents
121 1 freeifaddrs(a);
122 1 XSRETURN(count);
123 break;
124 0 default:
125 0 break;
126 }
127 }
128
129  
130 SV *
131 if_nametoindex(name)
132 SV *name;
133 INIT:
134 char *p;
135 unsigned int ret;
136 int len;
137 PPCODE:
138  
139 0 ▶ if(SvOK(name)&&SvPOK(name)){
Branch: 2 branches
BranchTrueFalseTotal
if(SvOK(name)&&SvPOK(name)){ 0 0 0
if(SvOK(name)&&SvPOK(name)){ 0 0 0
140 0 len=SvCUR(name);
141 0 ▶ p=SvGROW(name, len+1);
Branch: 2 branches
BranchTrueFalseTotal
p=SvGROW(name, len+1); 0 0 0
p=SvGROW(name, len+1); 0 0 0
142 0 p[len]='\0';
143  
144 0 ret=if_nametoindex(p);
145 0 ▶ mXPUSHs(newSVuv(ret));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newSVuv(ret)); 0 0 0
146 0 XSRETURN(1);
147 }
148 else {
149 0 XSRETURN_UNDEF;
150 }
151  
152  
153 SV *
154 if_indextoname(index)
155  
156 SV *index;
157  
158 INIT:
159  
160 0 SV *result=newSV(IF_NAMESIZE);
161 0 char *p=SvPVX(result);
162 char *ret;
163
164 PPCODE:
165 0 ▶ if(SvOK(index)){
Branch: 1 branch
BranchTrueFalseTotal
if(SvOK(index)){ 0 0 0
166 0 ret=if_indextoname(SvUV(index), p);
167 0 ▶ if(ret == p){
Branch: 1 branch
BranchTrueFalseTotal
if(ret == p){ 0 0 0
168
169 0 SvPOK_on(result);
170 0 SvCUR_set(result, strlen(p));
171 0 ▶ mXPUSHs(result);
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(result); 0 0 0
172 0 XSRETURN(1);
173 }
174 else {
175 0 XSRETURN_UNDEF;
176 }
177 }
178 else {
179 0 XSRETURN_UNDEF;
180 }
181  
182 void
183 if_nameindex()
184  
185 INIT:
186  
187 0 UV count=0;
188 struct if_nameindex *results, *next;
189 PPCODE:
190  
191 0 results=if_nameindex();
192
193 0 ▶ if(results ==NULL){
Branch: 1 branch
BranchTrueFalseTotal
if(results ==NULL){ 0 0 0
194  
195 0 XSRETURN_UNDEF;
196 }
197 else {
198 0 next=results;
199 0 ▶ while((next->if_index !=0 )&&
Branch: 1 branch
BranchTrueFalseTotal
while((next->if_index !=0 )&& 0 0 0
200 0 ▶ (next->if_name != NULL)){
Branch: 1 branch
BranchTrueFalseTotal
(next->if_name != NULL)){ 0 0 0
201 0 ▶ EXTEND(SP,2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP,2); 0 0 0
202 0 mPUSHs(newSVuv(next->if_index));
203 0 mPUSHs(newSVpv(next->if_name, 0));
204 0 count++;
205 0 next=results+count;
206 }
207 0 if_freenameindex(results);
208 0 XSRETURN(count);
209 }
210  
211  
« const-xs.inc ↑ Summary blib/lib/Socket/More/Interface.pm »