lib/Socket/Netlink.xs

filter: Statementstmt 87.7% 100 / 114 Branchbran 57.4% 31 / 54 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 77.9% 131 / 168 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Socket/Netlink/Generic_const.pm ↑ Summary blib/lib/Socket/Netlink.pm »
1 /* You may distribute under the terms of either the GNU General Public License
2 * or the Artistic License (the same terms as Perl itself)
3 *
4 * (C) Paul Evans, 2010 -- leonerd@leonerd.org.uk
5 */
6  
7 #include "EXTERN.h"
8 #include "perl.h"
9 #include "XSUB.h"
10  
11 #include <sys/socket.h>
12 #include <linux/netlink.h>
13  
14 10 static void setup_constants(void)
15 {
16 HV *stash;
17 AV *export;
18  
19 10 stash = gv_stashpvn("Socket::Netlink", 15, TRUE);
20 10 export = get_av("Socket::Netlink::EXPORT", TRUE);
21  
22 #define DO_CONSTANT(c) \
23 newCONSTSUB(stash, #c, newSViv(c)); \
24 av_push(export, newSVpv(#c, 0));
25  
26  
27 10 DO_CONSTANT(PF_NETLINK)
28 10 DO_CONSTANT(AF_NETLINK)
29  
30 10 DO_CONSTANT(NLMSG_NOOP)
31 10 DO_CONSTANT(NLMSG_ERROR)
32 10 DO_CONSTANT(NLMSG_DONE)
33  
34 10 DO_CONSTANT(NLM_F_REQUEST)
35 10 DO_CONSTANT(NLM_F_MULTI)
36 10 DO_CONSTANT(NLM_F_ACK)
37 10 DO_CONSTANT(NLM_F_ECHO)
38  
39 10 DO_CONSTANT(NLM_F_ROOT)
40 10 DO_CONSTANT(NLM_F_MATCH)
41 10 DO_CONSTANT(NLM_F_ATOMIC)
42 10 DO_CONSTANT(NLM_F_DUMP)
43  
44 10 DO_CONSTANT(NLM_F_REPLACE)
45 10 DO_CONSTANT(NLM_F_EXCL)
46 10 DO_CONSTANT(NLM_F_CREATE)
47 10 DO_CONSTANT(NLM_F_APPEND)
48 10 }
49  
50 MODULE = Socket::Netlink PACKAGE = Socket::Netlink
51  
52 BOOT:
53 10 setup_constants();
54  
55 SV*
56 pack_sockaddr_nl(pid, groups)
57 unsigned long pid
58 unsigned long groups
59  
60 PREINIT:
61 struct sockaddr_nl snl;
62  
63 CODE:
64 8 Zero(&snl, sizeof snl, char);
65  
66 8 snl.nl_family = AF_NETLINK;
67 8 snl.nl_pid = pid;
68 8 snl.nl_groups = groups;
69 8 RETVAL = newSVpvn((char*)&snl, sizeof snl);
70 OUTPUT:
71 RETVAL
72  
73 void
74 unpack_sockaddr_nl(addr)
75 SV *addr
76  
77 PREINIT:
78 struct sockaddr_nl snl;
79  
80 PPCODE:
81 3 ▶ if(SvCUR(addr) != sizeof snl)
Branch: 1 branch
BranchTrueFalseTotal
if(SvCUR(addr) != sizeof snl) 0 3 3
82 0 croak("Expected %d byte address", sizeof snl);
83  
84 3 Copy(SvPVbyte_nolen(addr), &snl, sizeof snl, char);
85  
86 3 ▶ if(snl.nl_family != AF_NETLINK)
Branch: 1 branch
BranchTrueFalseTotal
if(snl.nl_family != AF_NETLINK) 0 3 3
87 0 croak("Expected AF_NETLINK");
88  
89 3 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 3 3
90 3 mPUSHi(snl.nl_pid);
91 3 mPUSHi(snl.nl_groups);
92  
93 SV*
94 pack_nlmsghdr(type, flags, seq, pid, body)
95 unsigned short type
96 unsigned short flags
97 unsigned long seq
98 unsigned long pid
99 SV *body
100  
101 PREINIT:
102 struct nlmsghdr nlmsghdr;
103 STRLEN bodylen;
104  
105 CODE:
106 9 ▶ if(!SvPOK(body))
Branch: 1 branch
BranchTrueFalseTotal
if(!SvPOK(body)) 0 9 9
107 0 croak("Expected a string body");
108  
109 9 nlmsghdr.nlmsg_type = type;
110 9 nlmsghdr.nlmsg_flags = flags;
111 9 nlmsghdr.nlmsg_seq = seq;
112 9 nlmsghdr.nlmsg_pid = pid;
113  
114 9 bodylen = SvCUR(body);
115  
116 9 nlmsghdr.nlmsg_len = NLMSG_LENGTH(bodylen);
117  
118 9 RETVAL = newSV(nlmsghdr.nlmsg_len);
119 9 SvPOK_on(RETVAL);
120 9 SvCUR_set(RETVAL, nlmsghdr.nlmsg_len);
121  
122 18 ▶ Zero(SvPVbyte_nolen(RETVAL), nlmsghdr.nlmsg_len, char);
Branch: 1 branch
BranchTrueFalseTotal
Zero(SvPVbyte_nolen(RETVAL), nlmsghdr.nlmsg_len, char); 9 0 9
123  
124 9 ▶ Copy(&nlmsghdr, SvPVbyte_nolen(RETVAL), sizeof(nlmsghdr), char);
Branch: 1 branch
BranchTrueFalseTotal
Copy(&nlmsghdr, SvPVbyte_nolen(RETVAL), sizeof(nlmsghdr), char); 9 0 9
125 18 ▶ Copy(SvPVbyte_nolen(body), SvPVbyte_nolen(RETVAL) + sizeof(nlmsghdr), bodylen, char);
Branch: 1 branch
BranchTrueFalseTotal
Copy(SvPVbyte_nolen(body), SvPVbyte_nolen(RETVAL) + sizeof(nlmsghdr), bodylen, char); 9 0 9
126  
127 OUTPUT:
128 RETVAL
129  
130 void
131 unpack_nlmsghdr(msg)
132 SV *msg
133  
134 PREINIT:
135 struct nlmsghdr nlmsghdr;
136 STRLEN msglen;
137  
138 PPCODE:
139 8 ▶ if(!SvPOK(msg))
Branch: 1 branch
BranchTrueFalseTotal
if(!SvPOK(msg)) 0 8 8
140 0 croak("Expected a string message");
141  
142 8 msglen = SvCUR(msg);
143  
144 8 Copy(SvPVbyte_nolen(msg), &nlmsghdr, sizeof(nlmsghdr), char);
145  
146 8 ▶ EXTEND(SP, 6);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 6); 0 8 8
147 8 PUSHs(sv_2mortal(newSViv(nlmsghdr.nlmsg_type)));
148 8 PUSHs(sv_2mortal(newSViv(nlmsghdr.nlmsg_flags)));
149 8 PUSHs(sv_2mortal(newSViv(nlmsghdr.nlmsg_seq)));
150 8 PUSHs(sv_2mortal(newSViv(nlmsghdr.nlmsg_pid)));
151 16 PUSHs(sv_2mortal(newSVpvn(SvPVbyte_nolen(msg) + sizeof(nlmsghdr), nlmsghdr.nlmsg_len - sizeof(nlmsghdr))));
152  
153 8 ▶ if(msglen > nlmsghdr.nlmsg_len) {
Branch: 1 branch
BranchTrueFalseTotal
if(msglen > nlmsghdr.nlmsg_len) { 0 8 8
154 // We have another message behind this
155 0 PUSHs(sv_2mortal(newSVpvn(SvPVbyte_nolen(msg) + nlmsghdr.nlmsg_len, msglen - nlmsghdr.nlmsg_len)));
156 }
157  
158 SV*
159 pack_nlmsgerr(error, msg)
160 unsigned int error
161 SV *msg
162  
163 PREINIT:
164 struct nlmsgerr nlmsgerr;
165 STRLEN msglen;
166  
167 CODE:
168 2 ▶ if(!SvPOK(msg))
Branch: 1 branch
BranchTrueFalseTotal
if(!SvPOK(msg)) 0 2 2
169 0 croak("Expected a string body");
170  
171 2 nlmsgerr.error = -error; /* kernel wants this negative */
172  
173 2 msglen = SvCUR(msg);
174 2 ▶ if(msglen > sizeof(nlmsgerr.msg))
Branch: 1 branch
BranchTrueFalseTotal
if(msglen > sizeof(nlmsgerr.msg)) 0 2 2
175 0 msglen = sizeof(nlmsgerr.msg);
176  
177 2 Zero(&nlmsgerr.msg, sizeof(nlmsgerr.msg), char);
178 2 Copy(SvPVbyte_nolen(msg), &nlmsgerr.msg, sizeof(nlmsgerr.msg), char);
179  
180 2 RETVAL = newSVpvn((char*)&nlmsgerr, sizeof(nlmsgerr));
181  
182 OUTPUT:
183 RETVAL
184  
185 void
186 unpack_nlmsgerr(msg)
187 SV *msg
188  
189 PREINIT:
190 struct nlmsgerr nlmsgerr;
191  
192 PPCODE:
193 1 ▶ if(!SvPOK(msg))
Branch: 1 branch
BranchTrueFalseTotal
if(!SvPOK(msg)) 0 1 1
194 0 croak("Expected a string message");
195 1 ▶ if(SvCUR(msg) != sizeof(nlmsgerr))
Branch: 1 branch
BranchTrueFalseTotal
if(SvCUR(msg) != sizeof(nlmsgerr)) 0 1 1
196 0 croak("Expected %d bytes of message", sizeof(nlmsgerr));
197  
198 1 Copy(SvPVbyte_nolen(msg), &nlmsgerr, sizeof(nlmsgerr), char);
199  
200 1 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 1 1
201 1 PUSHs(sv_2mortal(newSViv(-nlmsgerr.error))); /* kernel sends this negative */
202 1 PUSHs(sv_2mortal(newSVpvn((char*)&nlmsgerr.msg, sizeof(nlmsgerr.msg))));
203  
204 SV *
205 pack_nlattrs(...)
206 PREINIT:
207 12 STRLEN bufflen = 0;
208 int i;
209 char *buffer;
210  
211 CODE:
212 12 ▶ if(items % 2)
Branch: 1 branch
BranchTrueFalseTotal
if(items % 2) 0 12 12
213 0 croak("Expected even number of elements");
214  
215 32 ▶ for(i = 0; i < items; i+=2) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < items; i+=2) { 20 12 32
216 SV *value;
217  
218 20 value = ST(i+1);
219 20 ▶ if(!value || !SvPOK(value))
Branch: 2 branches
BranchTrueFalseTotal
if(!value || !SvPOK(value)) 20 0 20
if(!value || !SvPOK(value)) 0 20 20
220 0 croak("Expected string at parameter %d\n", i+1);
221  
222 20 bufflen += NLA_HDRLEN + NLA_ALIGN(SvCUR(value));
223 }
224  
225 12 ▶ if(items == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if(items == 0) { 1 11 12
226 /* newSV(0) doesn't behave right; special-case it */
227 1 RETVAL = newSVpvn("", 0);
228 }
229 else {
230 11 RETVAL = newSV(bufflen);
231 11 SvPOK_on(RETVAL);
232 11 SvCUR_set(RETVAL, bufflen);
233 }
234  
235 12 buffer = SvPVbyte_nolen(RETVAL);
236  
237 32 ▶ for(i = 0; i < items; i+=2) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < items; i+=2) { 20 12 32
238 struct nlattr attrhdr;
239 20 SV *value = ST(i+1);
240 20 STRLEN valuelen = SvCUR(value);
241  
242 20 attrhdr.nla_len = NLA_HDRLEN + valuelen;
243 20 attrhdr.nla_type = SvIV(ST(i));
244  
245 20 Copy((char*)&attrhdr, buffer, NLA_HDRLEN, char);
246 20 Copy(SvPVbyte_nolen(value), buffer + NLA_HDRLEN, valuelen, char);
247 20 Zero(buffer + NLA_HDRLEN + valuelen, NLA_ALIGN(valuelen) - valuelen, char);
248  
249 20 buffer += NLA_ALIGN(attrhdr.nla_len);
250 }
251  
252 OUTPUT:
253 RETVAL
254  
255 void
256 unpack_nlattrs(body)
257 SV *body
258  
259 INIT:
260 STRLEN bufflen;
261 char *buffer;
262  
263 PPCODE:
264 11 ▶ if(!SvPOK(body))
Branch: 1 branch
BranchTrueFalseTotal
if(!SvPOK(body)) 0 11 11
265 0 croak("Expected a string body");
266  
267 11 buffer = SvPVbyte(body, bufflen);
268  
269 63 ▶ while(bufflen > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while(bufflen > 0) { 52 11 63
270 struct nlattr attrhdr;
271  
272 52 ▶ if(bufflen < NLA_HDRLEN)
Branch: 1 branch
BranchTrueFalseTotal
if(bufflen < NLA_HDRLEN) 0 52 52
273 0 croak("Ran out of bytes for nlattr header");
274  
275 52 Copy(buffer, (char*)&attrhdr, NLA_HDRLEN, char);
276  
277 52 ▶ if(bufflen < attrhdr.nla_len)
Branch: 1 branch
BranchTrueFalseTotal
if(bufflen < attrhdr.nla_len) 0 52 52
278 0 croak("Ran out of bytes for nlattr body of %d bytes", attrhdr.nla_len);
279  
280 52 ▶ XPUSHs(sv_2mortal(newSViv(attrhdr.nla_type)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSViv(attrhdr.nla_type))); 0 52 52
281 52 ▶ XPUSHs(sv_2mortal(newSVpvn(buffer + NLA_HDRLEN, attrhdr.nla_len - NLA_HDRLEN)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvn(buffer + NLA_HDRLEN, attrhdr.nla_len - NLA_HDRLEN))); 0 52 52
282  
283 52 bufflen -= NLA_ALIGN(attrhdr.nla_len);
284 52 buffer += NLA_ALIGN(attrhdr.nla_len);
285 }
« blib/lib/Socket/Netlink/Generic_const.pm ↑ Summary blib/lib/Socket/Netlink.pm »