addrinfo.xs

filter: Statementstmt 0.0% 0 / 47 Branchbran 0.0% 0 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 71 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Net/addrinfo.pm ↑ Summary const-c.inc »
1 /* -*- C -*-
2 addrinfo.xs -- Perl 5 interface to getaddrinfo(3) and related structs
3  
4 written by G. S. Marzot (marz@users.sourceforge.net)
5  
6 Copyright (c) 2006 G. S. Marzot. All rights reserved.
7  
8 Copyright (c) 2006-2008 SPARTA, Inc. All rights reserved.
9  
10 This program is free software; you can redistribute it and/or
11 modify it under the same terms as Perl itself.
12 */
13  
14 #include "EXTERN.h"
15 #include "perl.h"
16 #include "XSUB.h"
17  
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <stdarg.h>
22 #include <strings.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <netdb.h>
26 #include <arpa/inet.h>
27 #include <netinet/in.h>
28  
29 #ifndef na
30 #define na PL_na
31 #endif
32  
33 #ifndef sv_undef
34 #define sv_undef PL_sv_undef
35 #endif
36  
37 #define ADDRINFO_TYPE 0
38 #define VAL_ADDRINFO_TYPE 1
39  
40 typedef struct addrinfo AddrInfo;
41  
42 #if 0
43 static int
44 not_here(s)
45 char *s;
46 {
47 croak("%s not implemented on this architecture", s);
48 return -1;
49 }
50 #endif
51  
52 0 static struct addrinfo *ainfo_sv2c(SV *ainfo_ref, struct addrinfo *ainfo_ptr)
53 {
54 0 ▶ if (ainfo_ref && SvROK(ainfo_ref) && ainfo_ptr) {
Branch: 3 branches
BranchTrueFalseTotal
if (ainfo_ref && SvROK(ainfo_ref) && ainfo_ptr) { 0 0 0
if (ainfo_ref && SvROK(ainfo_ref) && ainfo_ptr) { 0 0 0
if (ainfo_ref && SvROK(ainfo_ref) && ainfo_ptr) { 0 0 0
55 0 SV **flags_svp = hv_fetch((HV*)SvRV(ainfo_ref), "flags", 5, 1);
56 0 SV **family_svp = hv_fetch((HV*)SvRV(ainfo_ref), "family", 6, 1);
57 0 SV **socktype_svp = hv_fetch((HV*)SvRV(ainfo_ref), "socktype", 8, 1);
58 0 SV **protocol_svp = hv_fetch((HV*)SvRV(ainfo_ref), "protocol", 8, 1);
59 0 SV **addr_svp = hv_fetch((HV*)SvRV(ainfo_ref), "addr", 4, 1);
60 0 SV **canonname_svp = hv_fetch((HV*)SvRV(ainfo_ref), "canonname", 9, 1);
61  
62 0 ▶ ainfo_ptr->ai_flags = (SvOK(*flags_svp) ? SvIV(*flags_svp) : 0);
Branch: 1 branch
BranchTrueFalseTotal
ainfo_ptr->ai_flags = (SvOK(*flags_svp) ? SvIV(*flags_svp) : 0); 0 0 0
63 0 ▶ ainfo_ptr->ai_family = (SvOK(*family_svp) ? SvIV(*family_svp) : 0);
Branch: 1 branch
BranchTrueFalseTotal
ainfo_ptr->ai_family = (SvOK(*family_svp) ? SvIV(*family_svp) : 0); 0 0 0
64 0 ▶ ainfo_ptr->ai_socktype = (SvOK(*socktype_svp) ? SvIV(*socktype_svp) : 0);
Branch: 1 branch
BranchTrueFalseTotal
ainfo_ptr->ai_socktype = (SvOK(*socktype_svp) ? SvIV(*socktype_svp) : 0); 0 0 0
65 0 ▶ ainfo_ptr->ai_protocol = (SvOK(*protocol_svp) ? SvIV(*protocol_svp) : 0);
Branch: 1 branch
BranchTrueFalseTotal
ainfo_ptr->ai_protocol = (SvOK(*protocol_svp) ? SvIV(*protocol_svp) : 0); 0 0 0
66 0 ▶ if (SvOK(*addr_svp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(*addr_svp)) { 0 0 0
67 0 ainfo_ptr->ai_addr = (struct sockaddr *) SvPV(*addr_svp,na); // borrowed
68 0 ainfo_ptr->ai_addrlen = SvLEN(*addr_svp); // ignore hash field addrlen?
69 } else {
70 0 ainfo_ptr->ai_addr = NULL;
71 0 ainfo_ptr->ai_addrlen = 0;
72 }
73 0 ainfo_ptr->ai_canonname = (SvOK(*canonname_svp) ?
74 0 ▶ SvPV(*canonname_svp,na) : NULL); // borrowed
Branch: 1 branch
BranchTrueFalseTotal
SvPV(*canonname_svp,na) : NULL); // borrowed 0 0 0
75  
76 // fprintf(stderr, "ainfo_ptr->ai_flags = %d\n", ainfo_ptr->ai_flags);
77 // fprintf(stderr, "ainfo_ptr->ai_family = %d\n", ainfo_ptr->ai_family);
78 // fprintf(stderr, "ainfo_ptr->ai_socktype = %d\n", ainfo_ptr->ai_socktype);
79 // fprintf(stderr, "ainfo_ptr->ai_protocol = %d\n", ainfo_ptr->ai_protocol);
80 // fprintf(stderr, "ainfo_ptr->ai_addrlen = %d\n", ainfo_ptr->ai_addrlen);
81 // fprintf(stderr,"ainfo_ptr->ai_canonname=%s\n",ainfo_ptr->ai_canonname);
82 } else {
83 0 return NULL;
84 }
85 0 return ainfo_ptr;
86 }
87  
88 0 SV *ainfo_c2sv(struct addrinfo *ainfo_ptr)
89 {
90 0 AV *ainfo_av = newAV();
91 0 SV *ainfo_av_ref = newRV_noinc((SV*)ainfo_av);
92  
93 0 ▶ for (;ainfo_ptr != NULL; ainfo_ptr = ainfo_ptr->ai_next) {
Branch: 1 branch
BranchTrueFalseTotal
for (;ainfo_ptr != NULL; ainfo_ptr = ainfo_ptr->ai_next) { 0 0 0
94 0 HV *ainfo_hv = newHV();
95 0 SV *ainfo_hv_ref = newRV_noinc((SV*)ainfo_hv);
96
97 0 sv_bless(ainfo_hv_ref, gv_stashpv("Net::addrinfo",0));
98  
99 // fprintf(stderr,"::ainfo_ptr->ai_flags=%d\n", ainfo_ptr->ai_flags);
100 // fprintf(stderr,"::ainfo_ptr->ai_family=%d\n", ainfo_ptr->ai_family);
101 // fprintf(stderr,"::ainfo_ptr->ai_socktype=%d\n", ainfo_ptr->ai_socktype);
102 // fprintf(stderr,"::ainfo_ptr->ai_protocol=%d\n", ainfo_ptr->ai_protocol);
103 // fprintf(stderr,"::ainfo_ptr->ai_addrlen=%d\n", ainfo_ptr->ai_addrlen);
104 //fprintf(stderr,"::ainfo_ptr->ai_canonname=%s\n",ainfo_ptr->ai_canonname);
105 0 (void)hv_store(ainfo_hv, "flags", strlen("flags"),
106 newSViv(ainfo_ptr->ai_flags), 0);
107 0 (void)hv_store(ainfo_hv, "family", strlen("family"),
108 newSViv(ainfo_ptr->ai_family), 0);
109 0 (void)hv_store(ainfo_hv, "socktype", strlen("socktype"),
110 newSViv(ainfo_ptr->ai_socktype), 0);
111 0 (void)hv_store(ainfo_hv, "protocol", strlen("protocol"),
112 newSViv(ainfo_ptr->ai_protocol), 0);
113 0 (void)hv_store(ainfo_hv, "addr", strlen("addr"),
114 newSVpv((char*)ainfo_ptr->ai_addr,
115 ainfo_ptr->ai_addrlen), 0);
116 0 (void)hv_store(ainfo_hv, "addrlen", strlen("addrlen"),
117 newSViv(ainfo_ptr->ai_addrlen), 0);
118 0 ▶ (void)hv_store(ainfo_hv, "canonname", strlen("canonname"),
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_store(ainfo_hv, "canonname", strlen("canonname"), 0 0 0
119 (ainfo_ptr->ai_canonname ?
120 newSVpv(ainfo_ptr->ai_canonname,
121 strlen(ainfo_ptr->ai_canonname)) :
122 &sv_undef), 0);
123  
124 0 av_push(ainfo_av, ainfo_hv_ref);
125 }
126 0 return ainfo_av_ref;
127 }
128  
129 #include "const-c.inc"
130  
131 MODULE = Net::addrinfo PACKAGE = Net::addrinfo PREFIX = addrinfo
132  
133 INCLUDE: const-xs.inc
134  
135 SV *
136 addrinfo_getaddrinfo(node=NULL,service=NULL,hints_ref=NULL)
137 char * node = (SvOK($arg) ? (char *)SvPV($arg,PL_na) : NULL);
138 char * service = (SvOK($arg) ? (char *)SvPV($arg,PL_na) : NULL);
139 SV * hints_ref = (SvOK($arg) ? $arg : NULL);
140 CODE:
141 {
142 struct addrinfo hints;
143 0 struct addrinfo *hints_ptr = NULL;
144 0 struct addrinfo *ainfo_ptr = NULL;
145 int res;
146  
147 0 hints_ptr = ainfo_sv2c(hints_ref, &hints);
148  
149 0 res = getaddrinfo(node, service, hints_ptr, &ainfo_ptr);
150  
151 0 ▶ if (res == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (res == 0) { 0 0 0
152 0 RETVAL = ainfo_c2sv(ainfo_ptr);
153 } else {
154 0 RETVAL = newSViv(res);
155 }
156  
157 0 freeaddrinfo(ainfo_ptr);
158 }
159 OUTPUT:
160 RETVAL
161  
162 char *
163 addrinfo_gai_strerror(err)
164 int err
165 CODE:
166 {
167 0 RETVAL = (char*)gai_strerror(err);
168 }
169 OUTPUT:
170 RETVAL
171  
172 MODULE = Net::addrinfo PACKAGE = AddrInfoPtr PREFIX = addr_info_
173  
174 void
175 addr_info_DESTROY(addrinfo_ptr)
176 AddrInfo *addrinfo_ptr
177 CODE:
178 {
179 0 freeaddrinfo( addrinfo_ptr );
180 }
181  
« blib/lib/Net/addrinfo.pm ↑ Summary const-c.inc »