This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Remove explicit support for pre-7.0 VMS and pre-6.0 DEC C.
[perl5.git] / cpan / Socket / Socket.xs
CommitLineData
c5be433b 1#define PERL_NO_GET_CONTEXT
a0d0e21e
LW
2#include "EXTERN.h"
3#include "perl.h"
4#include "XSUB.h"
5
fc87b71c
NIS
6#include <stddef.h>
7
046cc26c 8#ifdef I_SYS_TYPES
8e07c86e 9# include <sys/types.h>
046cc26c
CB
10#endif
11#if !defined(ultrix) /* Avoid double definition. */
43db6828 12# include <sys/socket.h>
046cc26c
CB
13#endif
14#if defined(USE_SOCKS) && defined(I_SOCKS)
86959918 15# include <socks.h>
046cc26c
CB
16#endif
17#ifdef MPE
9cc6feab
JH
18# define PF_INET AF_INET
19# define PF_UNIX AF_UNIX
20# define SOCK_RAW 3
046cc26c
CB
21#endif
22#ifdef I_SYS_UN
9cc6feab 23# include <sys/un.h>
046cc26c 24#endif
bf9b2f8f 25/* XXX Configure test for <netinet/in_systm.h needed XXX */
046cc26c 26#if defined(NeXT) || defined(__NeXT__)
bf9b2f8f 27# include <netinet/in_systm.h>
046cc26c
CB
28#endif
29#if defined(__sgi) && !defined(AF_LINK) && defined(PF_LINK) && PF_LINK == AF_LNK
3b8fd1d5 30# undef PF_LINK
046cc26c
CB
31#endif
32#if defined(I_NETINET_IN) || defined(__ultrix__)
8e07c86e 33# include <netinet/in.h>
046cc26c
CB
34#endif
35#ifdef I_NETDB
87edd446
JH
36# if !defined(ultrix) /* Avoid double definition. */
37# include <netdb.h>
38# endif
046cc26c
CB
39#endif
40#ifdef I_ARPA_INET
9cc6feab 41# include <arpa/inet.h>
046cc26c
CB
42#endif
43#ifdef I_NETINET_TCP
9cc6feab 44# include <netinet/tcp.h>
8e07c86e 45#endif
a0d0e21e 46
2986a63f
JH
47#ifdef NETWARE
48NETDB_DEFINE_CONTEXT
49NETINET_DEFINE_CONTEXT
50#endif
51
6b1016b5
JH
52#ifdef I_SYSUIO
53# include <sys/uio.h>
54#endif
55
a0d0e21e 56#ifndef AF_NBS
9cc6feab 57# undef PF_NBS
a0d0e21e
LW
58#endif
59
60#ifndef AF_X25
9cc6feab 61# undef PF_X25
a0d0e21e
LW
62#endif
63
8e07c86e 64#ifndef INADDR_NONE
9cc6feab 65# define INADDR_NONE 0xffffffff
8e07c86e 66#endif /* INADDR_NONE */
7e1af8bc 67#ifndef INADDR_BROADCAST
9cc6feab 68# define INADDR_BROADCAST 0xffffffff
7e1af8bc 69#endif /* INADDR_BROADCAST */
8e07c86e 70#ifndef INADDR_LOOPBACK
9cc6feab 71# define INADDR_LOOPBACK 0x7F000001
8e07c86e
AD
72#endif /* INADDR_LOOPBACK */
73
4c3d8e12
PLE
74#ifndef croak_sv
75# define croak_sv(sv) croak(SvPV_nolen(sv))
76#endif
77
7e1af8bc
PP
78#ifndef HAS_INET_ATON
79
a062834f 80/*
7e1af8bc
PP
81 * Check whether "cp" is a valid ascii representation
82 * of an Internet address and convert to a binary address.
83 * Returns 1 if the address is valid, 0 if not.
84 * This replaces inet_addr, the return value from which
85 * cannot distinguish between failure and a local broadcast address.
86 */
87static int
f0f333f4 88my_inet_aton(register const char *cp, struct in_addr *addr)
7e1af8bc 89{
c5be433b 90 dTHX;
0caed002 91 register U32 val;
7e1af8bc
PP
92 register int base;
93 register char c;
94 int nparts;
95 const char *s;
96 unsigned int parts[4];
97 register unsigned int *pp = parts;
98
dfb6a7ef 99 if (!cp || !*cp)
0caed002 100 return 0;
7e1af8bc
PP
101 for (;;) {
102 /*
882ce583 103 * Collect number up to ".".
7e1af8bc
PP
104 * Values are specified as for C:
105 * 0x=hex, 0=octal, other=decimal.
106 */
107 val = 0; base = 10;
108 if (*cp == '0') {
109 if (*++cp == 'x' || *cp == 'X')
110 base = 16, cp++;
111 else
112 base = 8;
113 }
114 while ((c = *cp) != '\0') {
115 if (isDIGIT(c)) {
116 val = (val * base) + (c - '0');
117 cp++;
118 continue;
119 }
3280af22 120 if (base == 16 && (s=strchr(PL_hexdigit,c))) {
a062834f 121 val = (val << 4) +
3280af22 122 ((s - PL_hexdigit) & 15);
7e1af8bc
PP
123 cp++;
124 continue;
125 }
126 break;
127 }
128 if (*cp == '.') {
129 /*
130 * Internet format:
131 * a.b.c.d
132 * a.b.c (with c treated as 16-bits)
133 * a.b (with b treated as 24 bits)
134 */
135 if (pp >= parts + 3 || val > 0xff)
136 return 0;
137 *pp++ = val, cp++;
138 } else
139 break;
140 }
141 /*
142 * Check for trailing characters.
143 */
144 if (*cp && !isSPACE(*cp))
145 return 0;
146 /*
147 * Concoct the address according to
148 * the number of parts specified.
149 */
150 nparts = pp - parts + 1; /* force to an int for switch() */
151 switch (nparts) {
152
153 case 1: /* a -- 32 bits */
154 break;
155
156 case 2: /* a.b -- 8.24 bits */
157 if (val > 0xffffff)
158 return 0;
159 val |= parts[0] << 24;
160 break;
161
162 case 3: /* a.b.c -- 8.8.16 bits */
163 if (val > 0xffff)
164 return 0;
165 val |= (parts[0] << 24) | (parts[1] << 16);
166 break;
167
168 case 4: /* a.b.c.d -- 8.8.8.8 bits */
169 if (val > 0xff)
170 return 0;
171 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
172 break;
173 }
174 addr->s_addr = htonl(val);
175 return 1;
176}
177
178#undef inet_aton
179#define inet_aton my_inet_aton
180
181#endif /* ! HAS_INET_ATON */
182
8e07c86e 183
a0d0e21e 184static int
d3f5e399 185not_here(const char *s)
a0d0e21e
LW
186{
187 croak("Socket::%s not implemented on this architecture", s);
188 return -1;
189}
190
2528d3bc
JH
191#define PERL_IN_ADDR_S_ADDR_SIZE 4
192
193/*
194* Bad assumptions possible here.
195*
196* Bad Assumption 1: struct in_addr has no other fields
197* than the s_addr (which is the field we care about
198* in here, really). However, we can be fed either 4-byte
199* addresses (from pack("N", ...), or va.b.c.d, or ...),
200* or full struct in_addrs (from e.g. pack_sockaddr_in()),
201* which may or may not be 4 bytes in size.
202*
203* Bad Assumption 2: the s_addr field is a simple type
204* (such as an int, u_int32_t). It can be a bit field,
205* in which case using & (address-of) on it or taking sizeof()
206* wouldn't go over too well. (Those are not attempted
207* now but in case someone thinks to change the below code
208* to use addr.s_addr instead of addr, you have been warned.)
209*
210* Bad Assumption 3: the s_addr is the first field in
211* an in_addr, or that its bytes are the first bytes in
212* an in_addr.
213*
214* These bad assumptions are wrong in UNICOS which has
215* struct in_addr { struct { u_long st_addr:32; } s_da };
216* #define s_addr s_da.st_addr
217* and u_long is 64 bits.
218*
219* --jhi */
220
1cb0fb50 221#include "const-c.inc"
8e07c86e 222
eccda089
PLE
223#ifdef HAS_GETADDRINFO
224static SV *err_to_SV(pTHX_ int err)
225{
0f41f5e2
PLE
226 SV *ret = sv_newmortal();
227 SvUPGRADE(ret, SVt_PVNV);
eccda089 228
0f41f5e2
PLE
229 if(err) {
230 const char *error = gai_strerror(err);
231 sv_setpv(ret, error);
232 }
233 else {
234 sv_setpv(ret, "");
235 }
eccda089 236
0f41f5e2 237 SvIV_set(ret, err); SvIOK_on(ret);
eccda089 238
0f41f5e2 239 return ret;
eccda089
PLE
240}
241
242static void xs_getaddrinfo(pTHX_ CV *cv)
243{
0f41f5e2
PLE
244 dXSARGS;
245
246 SV *host;
247 SV *service;
248 SV *hints;
249
250 char *hostname = NULL;
251 char *servicename = NULL;
252 STRLEN len;
253 struct addrinfo hints_s;
254 struct addrinfo *res;
255 struct addrinfo *res_iter;
256 int err;
257 int n_res;
258
259 if(items > 3)
1584259f 260 croak("Usage: Socket::getaddrinfo(host, service, hints)");
0f41f5e2
PLE
261
262 SP -= items;
263
264 if(items < 1)
265 host = &PL_sv_undef;
266 else
267 host = ST(0);
268
269 if(items < 2)
270 service = &PL_sv_undef;
271 else
272 service = ST(1);
273
274 if(items < 3)
275 hints = NULL;
276 else
277 hints = ST(2);
278
279 SvGETMAGIC(host);
280 if(SvOK(host)) {
281 hostname = SvPV_nomg(host, len);
282 if (!len)
283 hostname = NULL;
284 }
285
286 SvGETMAGIC(service);
287 if(SvOK(service)) {
288 servicename = SvPV_nomg(service, len);
289 if (!len)
290 servicename = NULL;
291 }
292
293 Zero(&hints_s, sizeof hints_s, char);
0cd14f2e 294 hints_s.ai_family = PF_UNSPEC;
0f41f5e2
PLE
295
296 if(hints && SvOK(hints)) {
297 HV *hintshash;
298 SV **valp;
299
300 if(!SvROK(hints) || SvTYPE(SvRV(hints)) != SVt_PVHV)
301 croak("hints is not a HASH reference");
302
303 hintshash = (HV*)SvRV(hints);
304
305 if((valp = hv_fetch(hintshash, "flags", 5, 0)) != NULL)
306 hints_s.ai_flags = SvIV(*valp);
307 if((valp = hv_fetch(hintshash, "family", 6, 0)) != NULL)
308 hints_s.ai_family = SvIV(*valp);
309 if((valp = hv_fetch(hintshash, "socktype", 8, 0)) != NULL)
310 hints_s.ai_socktype = SvIV(*valp);
311 if((valp = hv_fetch(hintshash, "protocol", 8, 0)) != NULL)
312 hints_s.ai_protocol = SvIV(*valp);
313 }
314
315 err = getaddrinfo(hostname, servicename, &hints_s, &res);
316
317 XPUSHs(err_to_SV(aTHX_ err));
318
319 if(err)
320 XSRETURN(1);
321
322 n_res = 0;
323 for(res_iter = res; res_iter; res_iter = res_iter->ai_next) {
324 HV *res_hv = newHV();
325
326 (void)hv_stores(res_hv, "family", newSViv(res_iter->ai_family));
327 (void)hv_stores(res_hv, "socktype", newSViv(res_iter->ai_socktype));
328 (void)hv_stores(res_hv, "protocol", newSViv(res_iter->ai_protocol));
329
330 (void)hv_stores(res_hv, "addr", newSVpvn((char*)res_iter->ai_addr, res_iter->ai_addrlen));
331
332 if(res_iter->ai_canonname)
333 (void)hv_stores(res_hv, "canonname", newSVpv(res_iter->ai_canonname, 0));
334 else
335 (void)hv_stores(res_hv, "canonname", newSV(0));
336
337 XPUSHs(sv_2mortal(newRV_noinc((SV*)res_hv)));
338 n_res++;
339 }
340
341 freeaddrinfo(res);
342
343 XSRETURN(1 + n_res);
eccda089
PLE
344}
345#endif
346
347#ifdef HAS_GETNAMEINFO
348static void xs_getnameinfo(pTHX_ CV *cv)
349{
0f41f5e2 350 dXSARGS;
eccda089 351
0f41f5e2
PLE
352 SV *addr;
353 int flags;
eccda089 354
0f41f5e2
PLE
355 char host[1024];
356 char serv[256];
357 char *sa; /* we'll cast to struct sockaddr * when necessary */
358 STRLEN addr_len;
359 int err;
eccda089 360
0f41f5e2 361 if(items < 1 || items > 2)
1584259f 362 croak("Usage: Socket::getnameinfo(addr, flags=0)");
eccda089 363
0f41f5e2 364 SP -= items;
eccda089 365
0f41f5e2 366 addr = ST(0);
eccda089 367
0f41f5e2
PLE
368 if(items < 2)
369 flags = 0;
370 else
371 flags = SvIV(ST(1));
eccda089 372
0f41f5e2
PLE
373 if(!SvPOK(addr))
374 croak("addr is not a string");
eccda089 375
0f41f5e2 376 addr_len = SvCUR(addr);
eccda089 377
0f41f5e2
PLE
378 /* We need to ensure the sockaddr is aligned, because a random SvPV might
379 * not be due to SvOOK */
380 Newx(sa, addr_len, char);
381 Copy(SvPV_nolen(addr), sa, addr_len, char);
eccda089 382#ifdef HAS_SOCKADDR_SA_LEN
0f41f5e2 383 ((struct sockaddr *)sa)->sa_len = addr_len;
eccda089
PLE
384#endif
385
0f41f5e2
PLE
386 err = getnameinfo((struct sockaddr *)sa, addr_len,
387 host, sizeof(host),
388 serv, sizeof(serv),
389 flags);
eccda089 390
0f41f5e2 391 Safefree(sa);
eccda089 392
0f41f5e2 393 XPUSHs(err_to_SV(aTHX_ err));
eccda089 394
0f41f5e2
PLE
395 if(err)
396 XSRETURN(1);
eccda089 397
0f41f5e2
PLE
398 XPUSHs(sv_2mortal(newSVpv(host, 0)));
399 XPUSHs(sv_2mortal(newSVpv(serv, 0)));
eccda089 400
0f41f5e2 401 XSRETURN(3);
eccda089
PLE
402}
403#endif
404
a0d0e21e
LW
405MODULE = Socket PACKAGE = Socket
406
1cb0fb50 407INCLUDE: const-xs.inc
8e07c86e 408
eccda089
PLE
409BOOT:
410#ifdef HAS_GETADDRINFO
411 newXS("Socket::getaddrinfo", xs_getaddrinfo, __FILE__);
412#endif
413#ifdef HAS_GETNAMEINFO
414 newXS("Socket::getnameinfo", xs_getnameinfo, __FILE__);
415#endif
416
8e07c86e
AD
417void
418inet_aton(host)
419 char * host
420 CODE:
421 {
fbf77aee 422 struct in_addr ip_address;
8e07c86e
AD
423 struct hostent * phe;
424
4596752b
NC
425 if ((*host != '\0') && inet_aton(host, &ip_address)) {
426 ST(0) = newSVpvn_flags((char *)&ip_address, sizeof ip_address, SVs_TEMP);
427 XSRETURN(1);
428 }
429
430 phe = gethostbyname(host);
431 if (phe && phe->h_addrtype == AF_INET && phe->h_length == 4) {
432 ST(0) = newSVpvn_flags((char *)phe->h_addr, phe->h_length, SVs_TEMP);
433 XSRETURN(1);
8e07c86e
AD
434 }
435
4596752b 436 XSRETURN_UNDEF;
8e07c86e 437 }
8e07c86e
AD
438
439void
440inet_ntoa(ip_address_sv)
441 SV * ip_address_sv
442 CODE:
443 {
444 STRLEN addrlen;
445 struct in_addr addr;
1d68b939
JH
446 char * ip_address;
447 if (DO_UTF8(ip_address_sv) && !sv_utf8_downgrade(ip_address_sv, 1))
bc354c70 448 croak("Wide character in %s", "Socket::inet_ntoa");
f780604f 449 ip_address = SvPVbyte(ip_address_sv, addrlen);
2528d3bc
JH
450 if (addrlen == sizeof(addr) || addrlen == 4)
451 addr.s_addr =
452 (ip_address[0] & 0xFF) << 24 |
453 (ip_address[1] & 0xFF) << 16 |
454 (ip_address[2] & 0xFF) << 8 |
455 (ip_address[3] & 0xFF);
456 else
457 croak("Bad arg length for %s, length is %d, should be %d",
458 "Socket::inet_ntoa",
459 addrlen, sizeof(addr));
460 /* We could use inet_ntoa() but that is broken
461 * in HP-UX + GCC + 64bitint (returns "0.0.0.0"),
1d88b533
JH
462 * so let's use this sprintf() workaround everywhere.
463 * This is also more threadsafe than using inet_ntoa(). */
8063d419
NC
464 ST(0) = sv_2mortal(Perl_newSVpvf(aTHX_ "%d.%d.%d.%d", /* IPv6? */
465 ((addr.s_addr >> 24) & 0xFF),
466 ((addr.s_addr >> 16) & 0xFF),
467 ((addr.s_addr >> 8) & 0xFF),
468 ( addr.s_addr & 0xFF)));
8e07c86e
AD
469 }
470
471void
2a84dff3
GA
472sockaddr_family(sockaddr)
473 SV * sockaddr
474 PREINIT:
475 STRLEN sockaddr_len;
f780604f 476 char *sockaddr_pv = SvPVbyte(sockaddr, sockaddr_len);
2a84dff3
GA
477 CODE:
478 if (sockaddr_len < offsetof(struct sockaddr, sa_data)) {
479 croak("Bad arg length for %s, length is %d, should be at least %d",
480 "Socket::sockaddr_family", sockaddr_len,
481 offsetof(struct sockaddr, sa_data));
482 }
483 ST(0) = sv_2mortal(newSViv(((struct sockaddr*)sockaddr_pv)->sa_family));
484
485void
4633a7c4 486pack_sockaddr_un(pathname)
7513c55b 487 SV * pathname
4633a7c4
LW
488 CODE:
489 {
25f94b33 490#ifdef I_SYS_UN
4633a7c4 491 struct sockaddr_un sun_ad; /* fear using sun */
fcdb74fc 492 STRLEN len;
7513c55b 493 char * pathname_pv;
99f13d4c 494 int addr_len;
202975e6 495
4633a7c4
LW
496 Zero( &sun_ad, sizeof sun_ad, char );
497 sun_ad.sun_family = AF_UNIX;
7513c55b 498 pathname_pv = SvPV(pathname,len);
20408e3c
GS
499 if (len > sizeof(sun_ad.sun_path))
500 len = sizeof(sun_ad.sun_path);
202975e6
IZ
501# ifdef OS2 /* Name should start with \socket\ and contain backslashes! */
502 {
503 int off;
504 char *s, *e;
505
7513c55b
RGS
506 if (pathname_pv[0] != '/' && pathname_pv[0] != '\\')
507 croak("Relative UNIX domain socket name '%s' unsupported",
508 pathname_pv);
a062834f 509 else if (len < 8
7513c55b
RGS
510 || pathname_pv[7] != '/' && pathname_pv[7] != '\\'
511 || !strnicmp(pathname_pv + 1, "socket", 6))
202975e6
IZ
512 off = 7;
513 else
514 off = 0; /* Preserve names starting with \socket\ */
515 Copy( "\\socket", sun_ad.sun_path, off, char);
7513c55b 516 Copy( pathname_pv, sun_ad.sun_path + off, len, char );
202975e6
IZ
517
518 s = sun_ad.sun_path + off - 1;
519 e = s + len + 1;
520 while (++s < e)
521 if (*s = '/')
522 *s = '\\';
523 }
a062834f 524# else /* !( defined OS2 ) */
7513c55b 525 Copy( pathname_pv, sun_ad.sun_path, len, char );
202975e6 526# endif
a062834f 527 if (0) not_here("dummy");
99f13d4c
LR
528 if (len > 1 && sun_ad.sun_path[0] == '\0') {
529 /* Linux-style abstract-namespace socket.
530 * The name is not a file name, but an array of arbitrary
531 * character, starting with \0 and possibly including \0s,
532 * therefore the length of the structure must denote the
533 * end of that character array */
178e6016 534 addr_len = (char *)&(sun_ad.sun_path) - (char *)&sun_ad + len;
99f13d4c
LR
535 } else {
536 addr_len = sizeof sun_ad;
537 }
5897b17c
PLE
538# ifdef HAS_SOCKADDR_SA_LEN
539 sun_ad.sun_len = addr_len;
540# endif
d3d34884 541 ST(0) = newSVpvn_flags((char *)&sun_ad, addr_len, SVs_TEMP);
25f94b33
AD
542#else
543 ST(0) = (SV *) not_here("pack_sockaddr_un");
544#endif
44559db1 545
4633a7c4
LW
546 }
547
548void
549unpack_sockaddr_un(sun_sv)
550 SV * sun_sv
f13e1b2f 551 CODE:
4633a7c4 552 {
25f94b33 553#ifdef I_SYS_UN
4633a7c4 554 struct sockaddr_un addr;
fcdb74fc 555 STRLEN sockaddrlen;
f780604f 556 char * sun_ad = SvPVbyte(sun_sv,sockaddrlen);
89904c08 557 int addr_len;
36902e12
RG
558# ifndef __linux__
559 /* On Linux sockaddrlen on sockets returned by accept, recvfrom,
560 getpeername and getsockname is not equal to sizeof(addr). */
4633a7c4
LW
561 if (sockaddrlen != sizeof(addr)) {
562 croak("Bad arg length for %s, length is %d, should be %d",
563 "Socket::unpack_sockaddr_un",
564 sockaddrlen, sizeof(addr));
565 }
36902e12 566# endif
4633a7c4
LW
567
568 Copy( sun_ad, &addr, sizeof addr, char );
569
570 if ( addr.sun_family != AF_UNIX ) {
571 croak("Bad address family for %s, got %d, should be %d",
572 "Socket::unpack_sockaddr_un",
573 addr.sun_family,
574 AF_UNIX);
fcdb74fc 575 }
89904c08
LR
576
577 if (addr.sun_path[0] == '\0') {
578 /* Linux-style abstract socket address begins with a nul
579 * and can contain nuls. */
178e6016 580 addr_len = (char *)&addr - (char *)&(addr.sun_path) + sockaddrlen;
89904c08
LR
581 } else {
582 for (addr_len = 0; addr.sun_path[addr_len]
c33e8be1 583 && addr_len < (int)sizeof(addr.sun_path); addr_len++);
89904c08
LR
584 }
585
d3d34884 586 ST(0) = newSVpvn_flags(addr.sun_path, addr_len, SVs_TEMP);
25f94b33
AD
587#else
588 ST(0) = (SV *) not_here("unpack_sockaddr_un");
589#endif
4633a7c4
LW
590 }
591
592void
2528d3bc 593pack_sockaddr_in(port, ip_address_sv)
2c129a17 594 unsigned short port
2528d3bc 595 SV * ip_address_sv
8e07c86e
AD
596 CODE:
597 {
598 struct sockaddr_in sin;
2528d3bc
JH
599 struct in_addr addr;
600 STRLEN addrlen;
601 char * ip_address;
602 if (DO_UTF8(ip_address_sv) && !sv_utf8_downgrade(ip_address_sv, 1))
bc354c70 603 croak("Wide character in %s", "Socket::pack_sockaddr_in");
f780604f 604 ip_address = SvPVbyte(ip_address_sv, addrlen);
2528d3bc
JH
605 if (addrlen == sizeof(addr) || addrlen == 4)
606 addr.s_addr =
607 (ip_address[0] & 0xFF) << 24 |
608 (ip_address[1] & 0xFF) << 16 |
609 (ip_address[2] & 0xFF) << 8 |
610 (ip_address[3] & 0xFF);
611 else
612 croak("Bad arg length for %s, length is %d, should be %d",
613 "Socket::pack_sockaddr_in",
614 addrlen, sizeof(addr));
8e07c86e 615 Zero( &sin, sizeof sin, char );
4633a7c4 616 sin.sin_family = AF_INET;
8e07c86e 617 sin.sin_port = htons(port);
2528d3bc 618 sin.sin_addr.s_addr = htonl(addr.s_addr);
5897b17c
PLE
619# ifdef HAS_SOCKADDR_SA_LEN
620 sin.sin_len = sizeof (sin);
621# endif
d3d34884 622 ST(0) = newSVpvn_flags((char *)&sin, sizeof (sin), SVs_TEMP);
8e07c86e
AD
623 }
624
625void
626unpack_sockaddr_in(sin_sv)
627 SV * sin_sv
628 PPCODE:
629 {
630 STRLEN sockaddrlen;
631 struct sockaddr_in addr;
2c129a17 632 unsigned short port;
2528d3bc 633 struct in_addr ip_address;
f780604f 634 char * sin = SvPVbyte(sin_sv,sockaddrlen);
8e07c86e
AD
635 if (sockaddrlen != sizeof(addr)) {
636 croak("Bad arg length for %s, length is %d, should be %d",
637 "Socket::unpack_sockaddr_in",
638 sockaddrlen, sizeof(addr));
639 }
8e07c86e 640 Copy( sin, &addr,sizeof addr, char );
4633a7c4
LW
641 if ( addr.sin_family != AF_INET ) {
642 croak("Bad address family for %s, got %d, should be %d",
643 "Socket::unpack_sockaddr_in",
644 addr.sin_family,
645 AF_INET);
a062834f 646 }
8e07c86e
AD
647 port = ntohs(addr.sin_port);
648 ip_address = addr.sin_addr;
649
924508f0 650 EXTEND(SP, 2);
2c129a17 651 PUSHs(sv_2mortal(newSViv((IV) port)));
d3d34884 652 PUSHs(newSVpvn_flags((char *)&ip_address, sizeof(ip_address), SVs_TEMP));
8e07c86e 653 }
036d8bd4
SP
654
655void
9987e04d
PLE
656pack_sockaddr_in6(port, sin6_addr, scope_id=0, flowinfo=0)
657 unsigned short port
658 SV * sin6_addr
659 unsigned long scope_id
660 unsigned long flowinfo
661 CODE:
662 {
663#ifdef AF_INET6
664 struct sockaddr_in6 sin6;
665 char * addrbytes;
666 STRLEN addrlen;
667 if (DO_UTF8(sin6_addr) && !sv_utf8_downgrade(sin6_addr, 1))
668 croak("Wide character in %s", "Socket::pack_sockaddr_in6");
669 addrbytes = SvPVbyte(sin6_addr, addrlen);
670 if(addrlen != sizeof(sin6.sin6_addr))
671 croak("Bad arg length %s, length is %d, should be %d",
672 "Socket::pack_sockaddr_in6", addrlen, sizeof(sin6.sin6_addr));
673 Zero(&sin6, sizeof(sin6), char);
674 sin6.sin6_family = AF_INET6;
675 sin6.sin6_port = htons(port);
676 sin6.sin6_flowinfo = htonl(flowinfo);
677 Copy(addrbytes, &sin6.sin6_addr, sizeof(sin6.sin6_addr), char);
24b8ee2e 678# ifdef HAS_SIN6_SCOPE_ID
9987e04d 679 sin6.sin6_scope_id = scope_id;
79c932f0
PLE
680# else
681 if(scope_id != 0)
682 warn("%s cannot represent non-zero scope_id %d",
683 "Socket::pack_sockaddr_in6", scope_id);
24b8ee2e 684# endif
5897b17c
PLE
685# ifdef HAS_SOCKADDR_SA_LEN
686 sin6.sin6_len = sizeof(sin6);
687# endif
9987e04d
PLE
688 ST(0) = newSVpvn_flags((char *)&sin6, sizeof(sin6), SVs_TEMP);
689#else
690 ST(0) = (SV*)not_here("pack_sockaddr_in6");
691#endif
692 }
693
694void
695unpack_sockaddr_in6(sin6_sv)
696 SV * sin6_sv
697 PPCODE:
698 {
699#ifdef AF_INET6
700 STRLEN addrlen;
701 struct sockaddr_in6 sin6;
702 char * addrbytes = SvPVbyte(sin6_sv, addrlen);
703 if (addrlen != sizeof(sin6))
704 croak("Bad arg length for %s, length is %d, should be %d",
705 "Socket::unpack_sockaddr_in6",
706 addrlen, sizeof(sin6));
707 Copy(addrbytes, &sin6, sizeof(sin6), char);
708 if(sin6.sin6_family != AF_INET6)
709 croak("Bad address family for %s, got %d, should be %d",
710 "Socket::unpack_sockaddr_in6",
711 sin6.sin6_family, AF_INET6);
712 EXTEND(SP, 4);
713 mPUSHi(ntohs(sin6.sin6_port));
714 mPUSHp((char *)&sin6.sin6_addr, sizeof(sin6.sin6_addr));
24b8ee2e 715# ifdef HAS_SIN6_SCOPE_ID
9987e04d 716 mPUSHi(sin6.sin6_scope_id);
24b8ee2e 717# else
8b079db6 718 mPUSHi(0);
24b8ee2e 719# endif
9987e04d
PLE
720 mPUSHi(ntohl(sin6.sin6_flowinfo));
721#else
722 ST(0) = (SV*)not_here("pack_sockaddr_in6");
723#endif
724 }
725
726void
036d8bd4
SP
727inet_ntop(af, ip_address_sv)
728 int af
729 SV * ip_address_sv
730 CODE:
731#ifdef HAS_INETNTOP
732 STRLEN addrlen, struct_size;
a5addb16 733#ifdef AF_INET6
036d8bd4
SP
734 struct in6_addr addr;
735 char str[INET6_ADDRSTRLEN];
a5addb16
PG
736#else
737 struct in_addr addr;
738 char str[INET_ADDRSTRLEN];
739#endif
036d8bd4
SP
740 char *ip_address = SvPV(ip_address_sv, addrlen);
741
a5addb16
PG
742 struct_size = sizeof(addr);
743
744 if(af != AF_INET
745#ifdef AF_INET6
746 && af != AF_INET6
747#endif
748 ) {
749 croak("Bad address family for %s, got %d, should be"
750#ifdef AF_INET6
751 " either AF_INET or AF_INET6",
752#else
753 " AF_INET",
754#endif
bc354c70 755 "Socket::inet_ntop",
036d8bd4
SP
756 af);
757 }
758
759 Copy( ip_address, &addr, sizeof addr, char );
a5addb16 760 inet_ntop(af, &addr, str, sizeof str);
036d8bd4 761
d3d34884 762 ST(0) = newSVpvn_flags(str, strlen(str), SVs_TEMP);
036d8bd4
SP
763#else
764 ST(0) = (SV *)not_here("inet_ntop");
765#endif
766
767void
768inet_pton(af, host)
769 int af
770 const char * host
771 CODE:
772#ifdef HAS_INETPTON
5a8cf367 773 int ok;
a5addb16
PG
774#ifdef AF_INET6
775 struct in6_addr ip_address;
776#else
777 struct in_addr ip_address;
778#endif
779
780 if(af != AF_INET
781#ifdef AF_INET6
782 && af != AF_INET6
783#endif
784 ) {
785 croak("Bad address family for %s, got %d, should be"
786#ifdef AF_INET6
787 " either AF_INET or AF_INET6",
788#else
789 " AF_INET",
790#endif
036d8bd4
SP
791 "Socket::inet_pton",
792 af);
793 }
5a8cf367 794 ok = (*host != '\0') && inet_pton(af, host, &ip_address);
036d8bd4
SP
795
796 ST(0) = sv_newmortal();
797 if (ok) {
a5addb16 798 sv_setpvn( ST(0), (char *)&ip_address, sizeof(ip_address) );
036d8bd4
SP
799 }
800#else
801 ST(0) = (SV *)not_here("inet_pton");
802#endif
4f5bb87f
PLE
803
804void
805pack_ipv6_mreq(addr, interface)
806 SV * addr
807 unsigned int interface
808 CODE:
809 {
810#ifdef AF_INET6
811 struct ipv6_mreq mreq;
812 char * addrbytes;
813 STRLEN addrlen;
814 if (DO_UTF8(addr) && !sv_utf8_downgrade(addr, 1))
815 croak("Wide character in %s", "Socket::pack_ipv6_mreq");
816 addrbytes = SvPVbyte(addr, addrlen);
817 if(addrlen != sizeof(mreq.ipv6mr_multiaddr))
818 croak("Bad arg length %s, length is %d, should be %d",
819 "Socket::pack_ipv6_mreq", addrlen, sizeof(mreq.ipv6mr_multiaddr));
820 Zero(&mreq, sizeof(mreq), char);
821 Copy(addrbytes, &mreq.ipv6mr_multiaddr, sizeof(mreq.ipv6mr_multiaddr), char);
822 mreq.ipv6mr_interface = interface;
823 ST(0) = newSVpvn_flags((char *)&mreq, sizeof(mreq), SVs_TEMP);
824#else
825 ST(0) = (SV*)not_here("pack_ipv6_mreq");
826#endif
827 }
828
829void
830unpack_ipv6_mreq(mreq_sv)
831 SV * mreq_sv
832 PPCODE:
833 {
834#ifdef AF_INET6
835 struct ipv6_mreq mreq;
836 STRLEN mreqlen;
837 char * mreqbytes = SvPVbyte(mreq_sv, mreqlen);
838 if (mreqlen != sizeof(mreq))
839 croak("Bad arg length for %s, length is %d, should be %d",
840 "Socket::unpack_ipv6_mreq",
841 mreqlen, sizeof(mreq));
842 Copy(mreqbytes, &mreq, sizeof(mreq), char);
843 EXTEND(SP, 2);
844 mPUSHp((char *)&mreq.ipv6mr_multiaddr, sizeof(mreq.ipv6mr_multiaddr));
845 mPUSHi(mreq.ipv6mr_interface);
846#else
847 not_here("unpack_ipv6_mreq");
848#endif
849 }