1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
|
/* You may distribute under the terms of either the GNU General Public License
* or the Artistic License (the same terms as Perl itself)
*
* (C) Paul Evans, 2008-2011 -- leonerd@leonerd.org.uk
*/
#include "../../socket-gai-config.h"
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#define NEED_newCONSTSUB
#define NEED_newRV_noinc
#define NEED_sv_2pv_flags
#include "../../ppport.h"
#ifdef HAS_GETADDRINFO
#include <stdlib.h>
#include <stdio.h>
#ifdef WIN32
# undef WINVER
# define WINVER 0x0501
# ifdef __GNUC__
# define USE_W32_SOCKETS
# endif
# include <winsock2.h>
/* We need to include ws2tcpip.h to get the IPv6 definitions.
* It will in turn include wspiapi.h. Later versions of that
* header in the Windows SDK generate C++ template code that
* can't be compiled with VC6 anymore. The _WSPIAPI_COUNTOF
* definition below prevents wspiapi.h from generating this
* incompatible code.
*/
# define _WSPIAPI_COUNTOF(_Array) (sizeof(_Array) / sizeof(_Array[0]))
# undef UNICODE
# include <ws2tcpip.h>
# ifndef NI_NUMERICSERV
# error Microsoft Platform SDK (Aug. 2001) or later required.
# endif
# ifdef _MSC_VER
# pragma comment(lib, "Ws2_32.lib")
# endif
#else
# include <sys/types.h>
# include <sys/socket.h>
# include <netdb.h>
#endif
/* These are not gni() constants; they're extensions for the perl API */
#define NIx_NOHOST (1 << 0)
#define NIx_NOSERV (1 << 1)
static SV *err_to_SV(pTHX_ int err)
{
SV *ret = sv_newmortal();
SvUPGRADE(ret, SVt_PVNV);
if(err) {
const char *error = gai_strerror(err);
sv_setpv(ret, error);
}
else {
sv_setpv(ret, "");
}
SvIV_set(ret, err); SvIOK_on(ret);
return ret;
}
static void setup_constants(void)
{
HV *stash;
AV *export;
stash = gv_stashpvn("Socket::GetAddrInfo", 19, TRUE);
export = get_av("Socket::GetAddrInfo::EXPORT_OK", TRUE);
#define DO_CONSTANT(c) \
newCONSTSUB(stash, #c, newSViv(c)); \
av_push(export, newSVpv(#c, 0));
DO_CONSTANT(AI_PASSIVE)
DO_CONSTANT(AI_CANONNAME)
DO_CONSTANT(AI_NUMERICHOST)
#ifdef AI_V4MAPPED
DO_CONSTANT(AI_V4MAPPED)
#endif
#ifdef AI_ALL
DO_CONSTANT(AI_ALL)
#endif
#ifdef AI_ADDRCONFIG
DO_CONSTANT(AI_ADDRCONFIG)
#endif
#ifdef AI_NUMERICSERV
DO_CONSTANT(AI_NUMERICSERV)
#endif
#ifdef AI_IDN
DO_CONSTANT(AI_IDN)
#endif
#ifdef AI_CANONIDN
DO_CONSTANT(AI_CANONIDN)
#endif
#ifdef AI_IDN_ALLOW_UNASSIGNED
DO_CONSTANT(AI_IDN_ALLOW_UNASSIGNED)
#endif
#ifdef AI_IDN_USE_STD3_ASCII_RULES
DO_CONSTANT(AI_IDN_USE_STD3_ASCII_RULES)
#endif
DO_CONSTANT(EAI_BADFLAGS)
DO_CONSTANT(EAI_NONAME)
DO_CONSTANT(EAI_AGAIN)
DO_CONSTANT(EAI_FAIL)
#ifdef EAI_NODATA
DO_CONSTANT(EAI_NODATA)
#endif
DO_CONSTANT(EAI_FAMILY)
DO_CONSTANT(EAI_SOCKTYPE)
DO_CONSTANT(EAI_SERVICE)
#ifdef EAI_ADDRFAMILY
DO_CONSTANT(EAI_ADDRFAMILY)
#endif
DO_CONSTANT(EAI_MEMORY)
#ifdef EAI_SYSTEM
DO_CONSTANT(EAI_SYSTEM)
#endif
#ifdef EAI_BADHINTS
DO_CONSTANT(EAI_BADHINTS)
#endif
#ifdef EAI_PROTOCOL
DO_CONSTANT(EAI_PROTOCOL)
#endif
DO_CONSTANT(NI_NUMERICHOST)
DO_CONSTANT(NI_NUMERICSERV)
DO_CONSTANT(NI_NAMEREQD)
DO_CONSTANT(NI_DGRAM)
#ifdef NI_NOFQDN
DO_CONSTANT(NI_NOFQDN)
#endif
#ifdef NI_IDN
DO_CONSTANT(NI_IDN)
#endif
#ifdef NI_IDN_ALLOW_UNASSIGNED
DO_CONSTANT(NI_IDN_ALLOW_UNASSIGNED)
#endif
#ifdef NI_IDN_USE_STD3_ASCII_RULES
DO_CONSTANT(NI_IDN_USE_STD3_ASCII_RULES)
#endif
DO_CONSTANT(NIx_NOHOST)
DO_CONSTANT(NIx_NOSERV)
}
static void xs_getaddrinfo(pTHX_ CV *cv)
{
dVAR;
dXSARGS;
SV *host;
SV *service;
SV *hints;
char *hostname = NULL;
char *servicename = NULL;
STRLEN len;
struct addrinfo hints_s;
struct addrinfo *res;
struct addrinfo *res_iter;
int err;
int n_res;
if(items > 3)
croak("Usage: Socket::GetAddrInfo(host, service, hints)");
SP -= items;
if(items < 1)
host = &PL_sv_undef;
else
host = ST(0);
if(items < 2)
service = &PL_sv_undef;
else
service = ST(1);
if(items < 3)
hints = NULL;
else
hints = ST(2);
SvGETMAGIC(host);
if(SvOK(host)) {
hostname = SvPV_nomg(host, len);
if (!len)
hostname = NULL;
}
SvGETMAGIC(service);
if(SvOK(service)) {
servicename = SvPV_nomg(service, len);
if (!len)
servicename = NULL;
}
Zero(&hints_s, sizeof hints_s, char);
hints_s.ai_family = PF_UNSPEC;
if(hints && SvOK(hints)) {
HV *hintshash;
SV **valp;
if(!SvROK(hints) || SvTYPE(SvRV(hints)) != SVt_PVHV)
croak("hints is not a HASH reference");
hintshash = (HV*)SvRV(hints);
if((valp = hv_fetch(hintshash, "flags", 5, 0)) != NULL)
hints_s.ai_flags = SvIV(*valp);
if((valp = hv_fetch(hintshash, "family", 6, 0)) != NULL)
hints_s.ai_family = SvIV(*valp);
if((valp = hv_fetch(hintshash, "socktype", 8, 0)) != NULL)
hints_s.ai_socktype = SvIV(*valp);
if((valp = hv_fetch(hintshash, "protocol", 8, 0)) != NULL)
hints_s.ai_protocol = SvIV(*valp);
}
err = getaddrinfo(hostname, servicename, &hints_s, &res);
XPUSHs(err_to_SV(aTHX_ err));
if(err)
XSRETURN(1);
n_res = 0;
for(res_iter = res; res_iter; res_iter = res_iter->ai_next) {
HV *res_hv = newHV();
(void)hv_stores(res_hv, "family", newSViv(res_iter->ai_family));
(void)hv_stores(res_hv, "socktype", newSViv(res_iter->ai_socktype));
(void)hv_stores(res_hv, "protocol", newSViv(res_iter->ai_protocol));
(void)hv_stores(res_hv, "addr", newSVpvn((char*)res_iter->ai_addr, res_iter->ai_addrlen));
if(res_iter->ai_canonname)
(void)hv_stores(res_hv, "canonname", newSVpv(res_iter->ai_canonname, 0));
else
(void)hv_stores(res_hv, "canonname", newSV(0));
XPUSHs(sv_2mortal(newRV_noinc((SV*)res_hv)));
n_res++;
}
freeaddrinfo(res);
XSRETURN(1 + n_res);
}
static void xs_getnameinfo(pTHX_ CV *cv)
{
dVAR;
dXSARGS;
SV *addr;
int flags;
int xflags;
char host[1024];
char serv[256];
char *sa; /* we'll cast to struct sockaddr * when necessary */
STRLEN addr_len;
int err;
int want_host, want_serv;
if(items < 1 || items > 3)
croak("Usage: Socket::GetAddrInfo(addr, flags=0, xflags=0)");
SP -= items;
addr = ST(0);
if(items < 2)
flags = 0;
else
flags = SvIV(ST(1));
if(items < 3)
xflags = 0;
else
xflags = SvIV(ST(2));
want_host = !(xflags & NIx_NOHOST);
want_serv = !(xflags & NIx_NOSERV);
if(!SvPOK(addr))
croak("addr is not a string");
addr_len = SvCUR(addr);
/* We need to ensure the sockaddr is aligned, because a random SvPV might
* not be due to SvOOK */
Newx(sa, addr_len, char);
Copy(SvPV_nolen(addr), sa, addr_len, char);
#ifdef HAS_SOCKADDR_SA_LEN
((struct sockaddr *)sa)->sa_len = addr_len;
#endif
err = getnameinfo((struct sockaddr *)sa, addr_len,
want_host ? host : NULL, want_host ? sizeof(host) : 0,
want_serv ? serv : NULL, want_serv ? sizeof(serv) : 0,
flags);
Safefree(sa);
XPUSHs(err_to_SV(aTHX_ err));
if(err)
XSRETURN(1);
XPUSHs(want_host ? sv_2mortal(newSVpv(host, 0)) : &PL_sv_undef);
XPUSHs(want_serv ? sv_2mortal(newSVpv(serv, 0)) : &PL_sv_undef);
XSRETURN(3);
}
#endif
MODULE = Socket::GetAddrInfo PACKAGE = Socket::GetAddrInfo
BOOT:
#ifdef HAS_GETADDRINFO
setup_constants();
newXS("Socket::GetAddrInfo::getaddrinfo", xs_getaddrinfo, __FILE__);
newXS("Socket::GetAddrInfo::getnameinfo", xs_getnameinfo, __FILE__);
#endif
|