This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Revert "Upgrade Socket to CPAN version 2.008"
[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
c2654555 36# if !defined(ultrix) /* Avoid double definition. */
87edd446
JH
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
3be135d8
TC
47#ifdef WIN32
48# include <ws2tcpip.h>
49#endif
50
2986a63f
JH
51#ifdef NETWARE
52NETDB_DEFINE_CONTEXT
53NETINET_DEFINE_CONTEXT
54#endif
55
6b1016b5
JH
56#ifdef I_SYSUIO
57# include <sys/uio.h>
58#endif
59
a0d0e21e 60#ifndef AF_NBS
9cc6feab 61# undef PF_NBS
a0d0e21e
LW
62#endif
63
64#ifndef AF_X25
9cc6feab 65# undef PF_X25
a0d0e21e
LW
66#endif
67
8e07c86e 68#ifndef INADDR_NONE
9cc6feab 69# define INADDR_NONE 0xffffffff
8e07c86e 70#endif /* INADDR_NONE */
7e1af8bc 71#ifndef INADDR_BROADCAST
9cc6feab 72# define INADDR_BROADCAST 0xffffffff
7e1af8bc 73#endif /* INADDR_BROADCAST */
8e07c86e 74#ifndef INADDR_LOOPBACK
c2654555 75# define INADDR_LOOPBACK 0x7F000001
8e07c86e
AD
76#endif /* INADDR_LOOPBACK */
77
9dea6244
CBW
78#ifndef C_ARRAY_LENGTH
79#define C_ARRAY_LENGTH(arr) (sizeof(arr) / sizeof(*(arr)))
80#endif /* !C_ARRAY_LENGTH */
81
82#ifndef PERL_UNUSED_VAR
83# define PERL_UNUSED_VAR(x) ((void)x)
84#endif /* !PERL_UNUSED_VAR */
85
86#ifndef PERL_UNUSED_ARG
87# define PERL_UNUSED_ARG(x) PERL_UNUSED_VAR(x)
88#endif /* !PERL_UNUSED_ARG */
89
90#ifndef Newx
91# define Newx(v,n,t) New(0,v,n,t)
92#endif /* !Newx */
93
4c3d8e12
PLE
94#ifndef croak_sv
95# define croak_sv(sv) croak(SvPV_nolen(sv))
96#endif
97
9dea6244
CBW
98#ifndef hv_stores
99# define hv_stores(hv, keystr, val) \
100 hv_store(hv, ""keystr"", sizeof(keystr)-1, val, 0)
101#endif /* !hv_stores */
c2654555 102
9dea6244
CBW
103#ifndef newSVpvn_flags
104# define newSVpvn_flags(s,len,flags) my_newSVpvn_flags(aTHX_ s,len,flags)
37b1de1b 105static SV *my_newSVpvn_flags(pTHX_ const char *s, STRLEN len, U32 flags)
c2654555
CBW
106{
107 SV *sv = newSVpvn(s, len);
108 SvFLAGS(sv) |= (flags & SVf_UTF8);
109 return (flags & SVs_TEMP) ? sv_2mortal(sv) : sv;
110}
9dea6244 111#endif /* !newSVpvn_flags */
37b1de1b 112
9dea6244
CBW
113#ifndef SvRV_set
114# define SvRV_set(sv, val) (SvRV(sv) = (val))
115#endif /* !SvRV_set */
116
117#ifndef SvPV_nomg
118# define SvPV_nomg SvPV
119#endif /* !SvPV_nomg */
120
121#ifndef HEK_FLAGS
122# define HEK_FLAGS(hek) 0
123# define HVhek_UTF8 1
124#endif /* !HEK_FLAGS */
125
126#ifndef hv_common
127/* These magic numbers are arbitrarily chosen (copied from perl core in fact)
128 * and only have to match between this definition and the code that uses them
129 */
130# define HV_FETCH_ISSTORE 0x04
131# define HV_FETCH_LVALUE 0x10
132# define hv_common(hv, keysv, key, klen, flags, act, val, hash) \
133 my_hv_common(aTHX_ hv, keysv, key, klen, flags, act, val, hash)
134static void *my_hv_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
135 int flags, int act, SV *val, U32 hash)
136{
137 /*
138 * This only handles the usage actually made by the code
139 * generated by ExtUtils::Constant. EU:C really ought to arrange
140 * portability of its generated code itself.
141 */
142 if (!keysv) {
143 keysv = sv_2mortal(newSVpvn(key, klen));
144 if (flags & HVhek_UTF8)
145 SvUTF8_on(keysv);
146 }
147 if (act == HV_FETCH_LVALUE) {
148 return (void*)hv_fetch_ent(hv, keysv, 1, hash);
149 } else if (act == HV_FETCH_ISSTORE) {
150 return (void*)hv_store_ent(hv, keysv, val, hash);
151 } else {
152 croak("panic: my_hv_common: act=0x%x", act);
153 }
154}
155#endif /* !hv_common */
156
157#ifndef hv_common_key_len
158# define hv_common_key_len(hv, key, kl, act, val, hash) \
159 my_hv_common_key_len(aTHX_ hv, key, kl, act, val, hash)
160static void *my_hv_common_key_len(pTHX_ HV *hv, const char *key, I32 kl,
161 int act, SV *val, U32 hash)
162{
163 STRLEN klen;
164 int flags;
165 if (kl < 0) {
166 klen = -kl;
167 flags = HVhek_UTF8;
168 } else {
169 klen = kl;
170 flags = 0;
171 }
172 return hv_common(hv, NULL, key, klen, flags, act, val, hash);
173}
174#endif /* !hv_common_key_len */
175
176#ifndef mPUSHi
177# define mPUSHi(i) sv_setiv_mg(PUSHs(sv_newmortal()), (IV)(i))
178#endif /* !mPUSHi */
179#ifndef mPUSHp
180# define mPUSHp(p,l) sv_setpvn_mg(PUSHs(sv_newmortal()), (p), (l))
181#endif /* !mPUSHp */
aff163d9
SH
182#ifndef mPUSHs
183# define mPUSHs(s) PUSHs(sv_2mortal(s))
184#endif /* !mPUSHs */
9dea6244
CBW
185
186#ifndef CvCONST_on
187# undef newCONSTSUB
188# define newCONSTSUB(stash, name, val) my_newCONSTSUB(aTHX_ stash, name, val)
189static CV *my_newCONSTSUB(pTHX_ HV *stash, char *name, SV *val)
190{
191 /*
192 * This has to satisfy code generated by ExtUtils::Constant.
193 * It depends on the 5.8+ layout of constant subs. It has
194 * two calls to newCONSTSUB(): one for real constants, and one
195 * for undefined constants. In the latter case, it turns the
196 * initially-generated constant subs into something else, and
197 * it needs the return value from newCONSTSUB() which Perl 5.6
198 * doesn't provide.
199 */
200 GV *gv;
201 CV *cv;
202 Perl_newCONSTSUB(aTHX_ stash, name, val);
203 ENTER;
204 SAVESPTR(PL_curstash);
205 PL_curstash = stash;
206 gv = gv_fetchpv(name, 0, SVt_PVCV);
207 cv = GvCV(gv);
208 LEAVE;
209 CvXSUBANY(cv).any_ptr = &PL_sv_undef;
210 return cv;
211}
212# define CvCONST_off(cv) my_CvCONST_off(aTHX_ cv)
213static void my_CvCONST_off(pTHX_ CV *cv)
214{
215 op_free(CvROOT(cv));
216 CvROOT(cv) = NULL;
217 CvSTART(cv) = NULL;
218}
219#endif /* !CvCONST_on */
c2654555 220
7e1af8bc
PP
221#ifndef HAS_INET_ATON
222
a062834f 223/*
7e1af8bc
PP
224 * Check whether "cp" is a valid ascii representation
225 * of an Internet address and convert to a binary address.
226 * Returns 1 if the address is valid, 0 if not.
227 * This replaces inet_addr, the return value from which
228 * cannot distinguish between failure and a local broadcast address.
229 */
230static int
f0f333f4 231my_inet_aton(register const char *cp, struct in_addr *addr)
7e1af8bc 232{
c5be433b 233 dTHX;
0caed002 234 register U32 val;
7e1af8bc
PP
235 register int base;
236 register char c;
237 int nparts;
238 const char *s;
239 unsigned int parts[4];
240 register unsigned int *pp = parts;
241
c2654555 242 if (!cp || !*cp)
0caed002 243 return 0;
7e1af8bc
PP
244 for (;;) {
245 /*
882ce583 246 * Collect number up to ".".
7e1af8bc
PP
247 * Values are specified as for C:
248 * 0x=hex, 0=octal, other=decimal.
249 */
250 val = 0; base = 10;
251 if (*cp == '0') {
252 if (*++cp == 'x' || *cp == 'X')
253 base = 16, cp++;
254 else
255 base = 8;
256 }
257 while ((c = *cp) != '\0') {
258 if (isDIGIT(c)) {
259 val = (val * base) + (c - '0');
260 cp++;
261 continue;
262 }
3280af22 263 if (base == 16 && (s=strchr(PL_hexdigit,c))) {
a062834f 264 val = (val << 4) +
3280af22 265 ((s - PL_hexdigit) & 15);
7e1af8bc
PP
266 cp++;
267 continue;
268 }
269 break;
270 }
271 if (*cp == '.') {
272 /*
273 * Internet format:
274 * a.b.c.d
275 * a.b.c (with c treated as 16-bits)
276 * a.b (with b treated as 24 bits)
277 */
278 if (pp >= parts + 3 || val > 0xff)
279 return 0;
280 *pp++ = val, cp++;
281 } else
282 break;
283 }
284 /*
285 * Check for trailing characters.
286 */
287 if (*cp && !isSPACE(*cp))
288 return 0;
289 /*
290 * Concoct the address according to
291 * the number of parts specified.
292 */
293 nparts = pp - parts + 1; /* force to an int for switch() */
294 switch (nparts) {
295
296 case 1: /* a -- 32 bits */
297 break;
298
299 case 2: /* a.b -- 8.24 bits */
300 if (val > 0xffffff)
301 return 0;
302 val |= parts[0] << 24;
303 break;
304
305 case 3: /* a.b.c -- 8.8.16 bits */
306 if (val > 0xffff)
307 return 0;
308 val |= (parts[0] << 24) | (parts[1] << 16);
309 break;
310
311 case 4: /* a.b.c.d -- 8.8.8.8 bits */
312 if (val > 0xff)
313 return 0;
314 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
315 break;
316 }
317 addr->s_addr = htonl(val);
318 return 1;
319}
320
321#undef inet_aton
322#define inet_aton my_inet_aton
323
324#endif /* ! HAS_INET_ATON */
325
c2654555
CBW
326/* These are not gni() constants; they're extensions for the perl API */
327/* The definitions in Socket.pm and Socket.xs must match */
328#define NIx_NOHOST (1 << 0)
329#define NIx_NOSERV (1 << 1)
330
8e07c86e 331
a0d0e21e 332static int
d3f5e399 333not_here(const char *s)
a0d0e21e
LW
334{
335 croak("Socket::%s not implemented on this architecture", s);
336 return -1;
337}
338
2528d3bc
JH
339#define PERL_IN_ADDR_S_ADDR_SIZE 4
340
341/*
342* Bad assumptions possible here.
343*
344* Bad Assumption 1: struct in_addr has no other fields
345* than the s_addr (which is the field we care about
346* in here, really). However, we can be fed either 4-byte
347* addresses (from pack("N", ...), or va.b.c.d, or ...),
348* or full struct in_addrs (from e.g. pack_sockaddr_in()),
349* which may or may not be 4 bytes in size.
350*
351* Bad Assumption 2: the s_addr field is a simple type
c2654555 352* (such as an int, u_int32_t). It can be a bit field,
2528d3bc
JH
353* in which case using & (address-of) on it or taking sizeof()
354* wouldn't go over too well. (Those are not attempted
355* now but in case someone thinks to change the below code
356* to use addr.s_addr instead of addr, you have been warned.)
357*
358* Bad Assumption 3: the s_addr is the first field in
359* an in_addr, or that its bytes are the first bytes in
360* an in_addr.
361*
362* These bad assumptions are wrong in UNICOS which has
363* struct in_addr { struct { u_long st_addr:32; } s_da };
364* #define s_addr s_da.st_addr
365* and u_long is 64 bits.
366*
367* --jhi */
368
1cb0fb50 369#include "const-c.inc"
8e07c86e 370
eccda089
PLE
371#ifdef HAS_GETADDRINFO
372static SV *err_to_SV(pTHX_ int err)
373{
0f41f5e2 374 SV *ret = sv_newmortal();
9dea6244 375 (void) SvUPGRADE(ret, SVt_PVNV);
eccda089 376
0f41f5e2
PLE
377 if(err) {
378 const char *error = gai_strerror(err);
379 sv_setpv(ret, error);
380 }
381 else {
382 sv_setpv(ret, "");
383 }
eccda089 384
0f41f5e2 385 SvIV_set(ret, err); SvIOK_on(ret);
eccda089 386
0f41f5e2 387 return ret;
eccda089
PLE
388}
389
390static void xs_getaddrinfo(pTHX_ CV *cv)
391{
0f41f5e2
PLE
392 dXSARGS;
393
394 SV *host;
395 SV *service;
396 SV *hints;
397
398 char *hostname = NULL;
399 char *servicename = NULL;
400 STRLEN len;
401 struct addrinfo hints_s;
402 struct addrinfo *res;
403 struct addrinfo *res_iter;
404 int err;
405 int n_res;
406
9dea6244 407 PERL_UNUSED_ARG(cv);
0f41f5e2 408 if(items > 3)
1584259f 409 croak("Usage: Socket::getaddrinfo(host, service, hints)");
0f41f5e2
PLE
410
411 SP -= items;
412
413 if(items < 1)
414 host = &PL_sv_undef;
415 else
416 host = ST(0);
417
418 if(items < 2)
419 service = &PL_sv_undef;
420 else
421 service = ST(1);
422
423 if(items < 3)
424 hints = NULL;
425 else
426 hints = ST(2);
427
428 SvGETMAGIC(host);
429 if(SvOK(host)) {
430 hostname = SvPV_nomg(host, len);
431 if (!len)
432 hostname = NULL;
433 }
434
435 SvGETMAGIC(service);
436 if(SvOK(service)) {
437 servicename = SvPV_nomg(service, len);
438 if (!len)
439 servicename = NULL;
440 }
441
c2654555 442 Zero(&hints_s, sizeof(hints_s), char);
0cd14f2e 443 hints_s.ai_family = PF_UNSPEC;
0f41f5e2
PLE
444
445 if(hints && SvOK(hints)) {
446 HV *hintshash;
447 SV **valp;
448
449 if(!SvROK(hints) || SvTYPE(SvRV(hints)) != SVt_PVHV)
450 croak("hints is not a HASH reference");
451
452 hintshash = (HV*)SvRV(hints);
453
23296748 454 if((valp = hv_fetch(hintshash, "flags", 5, 0)) != NULL && SvOK(*valp))
0f41f5e2 455 hints_s.ai_flags = SvIV(*valp);
23296748 456 if((valp = hv_fetch(hintshash, "family", 6, 0)) != NULL && SvOK(*valp))
0f41f5e2 457 hints_s.ai_family = SvIV(*valp);
23296748 458 if((valp = hv_fetch(hintshash, "socktype", 8, 0)) != NULL && SvOK(*valp))
0f41f5e2 459 hints_s.ai_socktype = SvIV(*valp);
23296748 460 if((valp = hv_fetch(hintshash, "protocol", 8, 0)) != NULL && SvOK(*valp))
0f41f5e2
PLE
461 hints_s.ai_protocol = SvIV(*valp);
462 }
463
464 err = getaddrinfo(hostname, servicename, &hints_s, &res);
465
466 XPUSHs(err_to_SV(aTHX_ err));
467
468 if(err)
469 XSRETURN(1);
470
471 n_res = 0;
472 for(res_iter = res; res_iter; res_iter = res_iter->ai_next) {
473 HV *res_hv = newHV();
474
475 (void)hv_stores(res_hv, "family", newSViv(res_iter->ai_family));
476 (void)hv_stores(res_hv, "socktype", newSViv(res_iter->ai_socktype));
477 (void)hv_stores(res_hv, "protocol", newSViv(res_iter->ai_protocol));
478
479 (void)hv_stores(res_hv, "addr", newSVpvn((char*)res_iter->ai_addr, res_iter->ai_addrlen));
480
481 if(res_iter->ai_canonname)
482 (void)hv_stores(res_hv, "canonname", newSVpv(res_iter->ai_canonname, 0));
483 else
484 (void)hv_stores(res_hv, "canonname", newSV(0));
485
486 XPUSHs(sv_2mortal(newRV_noinc((SV*)res_hv)));
487 n_res++;
488 }
489
490 freeaddrinfo(res);
491
492 XSRETURN(1 + n_res);
eccda089
PLE
493}
494#endif
495
496#ifdef HAS_GETNAMEINFO
497static void xs_getnameinfo(pTHX_ CV *cv)
498{
0f41f5e2 499 dXSARGS;
eccda089 500
0f41f5e2
PLE
501 SV *addr;
502 int flags;
c2654555 503 int xflags;
eccda089 504
0f41f5e2
PLE
505 char host[1024];
506 char serv[256];
507 char *sa; /* we'll cast to struct sockaddr * when necessary */
508 STRLEN addr_len;
509 int err;
eccda089 510
c2654555
CBW
511 int want_host, want_serv;
512
9dea6244 513 PERL_UNUSED_ARG(cv);
c2654555
CBW
514 if(items < 1 || items > 3)
515 croak("Usage: Socket::getnameinfo(addr, flags=0, xflags=0)");
eccda089 516
0f41f5e2 517 SP -= items;
eccda089 518
0f41f5e2 519 addr = ST(0);
eccda089 520
0f41f5e2
PLE
521 if(items < 2)
522 flags = 0;
523 else
524 flags = SvIV(ST(1));
eccda089 525
c2654555
CBW
526 if(items < 3)
527 xflags = 0;
528 else
529 xflags = SvIV(ST(2));
530
531 want_host = !(xflags & NIx_NOHOST);
532 want_serv = !(xflags & NIx_NOSERV);
533
0f41f5e2
PLE
534 if(!SvPOK(addr))
535 croak("addr is not a string");
eccda089 536
0f41f5e2 537 addr_len = SvCUR(addr);
eccda089 538
0f41f5e2
PLE
539 /* We need to ensure the sockaddr is aligned, because a random SvPV might
540 * not be due to SvOOK */
541 Newx(sa, addr_len, char);
542 Copy(SvPV_nolen(addr), sa, addr_len, char);
eccda089 543#ifdef HAS_SOCKADDR_SA_LEN
0f41f5e2 544 ((struct sockaddr *)sa)->sa_len = addr_len;
eccda089
PLE
545#endif
546
0f41f5e2 547 err = getnameinfo((struct sockaddr *)sa, addr_len,
c2654555
CBW
548 want_host ? host : NULL, want_host ? sizeof(host) : 0,
549 want_serv ? serv : NULL, want_serv ? sizeof(serv) : 0,
0f41f5e2 550 flags);
eccda089 551
0f41f5e2 552 Safefree(sa);
eccda089 553
0f41f5e2 554 XPUSHs(err_to_SV(aTHX_ err));
eccda089 555
0f41f5e2
PLE
556 if(err)
557 XSRETURN(1);
eccda089 558
c2654555
CBW
559 XPUSHs(want_host ? sv_2mortal(newSVpv(host, 0)) : &PL_sv_undef);
560 XPUSHs(want_serv ? sv_2mortal(newSVpv(serv, 0)) : &PL_sv_undef);
eccda089 561
0f41f5e2 562 XSRETURN(3);
eccda089
PLE
563}
564#endif
565
a0d0e21e
LW
566MODULE = Socket PACKAGE = Socket
567
1cb0fb50 568INCLUDE: const-xs.inc
8e07c86e 569
eccda089
PLE
570BOOT:
571#ifdef HAS_GETADDRINFO
c2654555 572 newXS("Socket::getaddrinfo", xs_getaddrinfo, __FILE__);
eccda089
PLE
573#endif
574#ifdef HAS_GETNAMEINFO
c2654555 575 newXS("Socket::getnameinfo", xs_getnameinfo, __FILE__);
eccda089
PLE
576#endif
577
8e07c86e
AD
578void
579inet_aton(host)
580 char * host
581 CODE:
582 {
fbf77aee 583 struct in_addr ip_address;
8e07c86e
AD
584 struct hostent * phe;
585
4596752b 586 if ((*host != '\0') && inet_aton(host, &ip_address)) {
c2654555 587 ST(0) = sv_2mortal(newSVpvn((char *)&ip_address, sizeof(ip_address)));
4596752b
NC
588 XSRETURN(1);
589 }
590
591 phe = gethostbyname(host);
592 if (phe && phe->h_addrtype == AF_INET && phe->h_length == 4) {
c2654555 593 ST(0) = sv_2mortal(newSVpvn((char *)phe->h_addr, phe->h_length));
4596752b 594 XSRETURN(1);
8e07c86e
AD
595 }
596
4596752b 597 XSRETURN_UNDEF;
8e07c86e 598 }
8e07c86e
AD
599
600void
601inet_ntoa(ip_address_sv)
602 SV * ip_address_sv
603 CODE:
604 {
605 STRLEN addrlen;
606 struct in_addr addr;
1d68b939
JH
607 char * ip_address;
608 if (DO_UTF8(ip_address_sv) && !sv_utf8_downgrade(ip_address_sv, 1))
c2654555 609 croak("Wide character in %s", "Socket::inet_ntoa");
f780604f 610 ip_address = SvPVbyte(ip_address_sv, addrlen);
2528d3bc 611 if (addrlen == sizeof(addr) || addrlen == 4)
c2654555 612 addr.s_addr =
2528d3bc
JH
613 (ip_address[0] & 0xFF) << 24 |
614 (ip_address[1] & 0xFF) << 16 |
615 (ip_address[2] & 0xFF) << 8 |
616 (ip_address[3] & 0xFF);
617 else
9dea6244
CBW
618 croak("Bad arg length for %s, length is %"UVuf", should be %"UVuf,
619 "Socket::inet_ntoa", (UV)addrlen, (UV)sizeof(addr));
2528d3bc
JH
620 /* We could use inet_ntoa() but that is broken
621 * in HP-UX + GCC + 64bitint (returns "0.0.0.0"),
1d88b533
JH
622 * so let's use this sprintf() workaround everywhere.
623 * This is also more threadsafe than using inet_ntoa(). */
8063d419
NC
624 ST(0) = sv_2mortal(Perl_newSVpvf(aTHX_ "%d.%d.%d.%d", /* IPv6? */
625 ((addr.s_addr >> 24) & 0xFF),
626 ((addr.s_addr >> 16) & 0xFF),
627 ((addr.s_addr >> 8) & 0xFF),
628 ( addr.s_addr & 0xFF)));
8e07c86e
AD
629 }
630
631void
2a84dff3
GA
632sockaddr_family(sockaddr)
633 SV * sockaddr
634 PREINIT:
635 STRLEN sockaddr_len;
f780604f 636 char *sockaddr_pv = SvPVbyte(sockaddr, sockaddr_len);
2a84dff3 637 CODE:
c2654555 638 if (sockaddr_len < offsetof(struct sockaddr, sa_data))
9dea6244
CBW
639 croak("Bad arg length for %s, length is %"UVuf", should be at least %"UVuf,
640 "Socket::sockaddr_family", (UV)sockaddr_len,
641 (UV)offsetof(struct sockaddr, sa_data));
2a84dff3
GA
642 ST(0) = sv_2mortal(newSViv(((struct sockaddr*)sockaddr_pv)->sa_family));
643
644void
4633a7c4 645pack_sockaddr_un(pathname)
7513c55b 646 SV * pathname
4633a7c4
LW
647 CODE:
648 {
25f94b33 649#ifdef I_SYS_UN
4633a7c4 650 struct sockaddr_un sun_ad; /* fear using sun */
fcdb74fc 651 STRLEN len;
7513c55b 652 char * pathname_pv;
99f13d4c 653 int addr_len;
202975e6 654
c2654555 655 Zero(&sun_ad, sizeof(sun_ad), char);
4633a7c4 656 sun_ad.sun_family = AF_UNIX;
7513c55b 657 pathname_pv = SvPV(pathname,len);
20408e3c
GS
658 if (len > sizeof(sun_ad.sun_path))
659 len = sizeof(sun_ad.sun_path);
202975e6
IZ
660# ifdef OS2 /* Name should start with \socket\ and contain backslashes! */
661 {
c2654555
CBW
662 int off;
663 char *s, *e;
664
665 if (pathname_pv[0] != '/' && pathname_pv[0] != '\\')
666 croak("Relative UNIX domain socket name '%s' unsupported",
667 pathname_pv);
668 else if (len < 8
669 || pathname_pv[7] != '/' && pathname_pv[7] != '\\'
670 || !strnicmp(pathname_pv + 1, "socket", 6))
671 off = 7;
672 else
673 off = 0; /* Preserve names starting with \socket\ */
674 Copy("\\socket", sun_ad.sun_path, off, char);
675 Copy(pathname_pv, sun_ad.sun_path + off, len, char);
676
677 s = sun_ad.sun_path + off - 1;
678 e = s + len + 1;
679 while (++s < e)
680 if (*s = '/')
681 *s = '\\';
202975e6 682 }
a062834f 683# else /* !( defined OS2 ) */
c2654555 684 Copy(pathname_pv, sun_ad.sun_path, len, char);
202975e6 685# endif
a062834f 686 if (0) not_here("dummy");
99f13d4c
LR
687 if (len > 1 && sun_ad.sun_path[0] == '\0') {
688 /* Linux-style abstract-namespace socket.
689 * The name is not a file name, but an array of arbitrary
690 * character, starting with \0 and possibly including \0s,
691 * therefore the length of the structure must denote the
692 * end of that character array */
178e6016 693 addr_len = (char *)&(sun_ad.sun_path) - (char *)&sun_ad + len;
99f13d4c 694 } else {
c2654555 695 addr_len = sizeof(sun_ad);
99f13d4c 696 }
5897b17c
PLE
697# ifdef HAS_SOCKADDR_SA_LEN
698 sun_ad.sun_len = addr_len;
699# endif
c2654555 700 ST(0) = sv_2mortal(newSVpvn((char *)&sun_ad, addr_len));
25f94b33 701#else
c2654555 702 ST(0) = (SV*)not_here("pack_sockaddr_un");
25f94b33 703#endif
c2654555 704
4633a7c4
LW
705 }
706
707void
708unpack_sockaddr_un(sun_sv)
709 SV * sun_sv
f13e1b2f 710 CODE:
4633a7c4 711 {
25f94b33 712#ifdef I_SYS_UN
4633a7c4 713 struct sockaddr_un addr;
fcdb74fc 714 STRLEN sockaddrlen;
f780604f 715 char * sun_ad = SvPVbyte(sun_sv,sockaddrlen);
89904c08 716 int addr_len;
eabcd9c8 717# ifdef __linux__
36902e12
RG
718 /* On Linux sockaddrlen on sockets returned by accept, recvfrom,
719 getpeername and getsockname is not equal to sizeof(addr). */
eabcd9c8
A
720 if (sockaddrlen < sizeof(addr)) {
721 Copy(sun_ad, &addr, sockaddrlen, char);
23296748 722 Zero(((char*)&addr) + sockaddrlen, sizeof(addr) - sockaddrlen, char);
eabcd9c8
A
723 } else {
724 Copy(sun_ad, &addr, sizeof(addr), char);
725 }
726# else
c2654555 727 if (sockaddrlen != sizeof(addr))
9dea6244
CBW
728 croak("Bad arg length for %s, length is %"UVuf", should be %"UVuf,
729 "Socket::unpack_sockaddr_un", (UV)sockaddrlen, (UV)sizeof(addr));
c2654555 730 Copy(sun_ad, &addr, sizeof(addr), char);
eabcd9c8 731# endif
4633a7c4 732
c2654555
CBW
733 if (addr.sun_family != AF_UNIX)
734 croak("Bad address family for %s, got %d, should be %d",
735 "Socket::unpack_sockaddr_un", addr.sun_family, AF_UNIX);
89904c08
LR
736
737 if (addr.sun_path[0] == '\0') {
738 /* Linux-style abstract socket address begins with a nul
739 * and can contain nuls. */
178e6016 740 addr_len = (char *)&addr - (char *)&(addr.sun_path) + sockaddrlen;
89904c08
LR
741 } else {
742 for (addr_len = 0; addr.sun_path[addr_len]
c33e8be1 743 && addr_len < (int)sizeof(addr.sun_path); addr_len++);
89904c08
LR
744 }
745
c2654555 746 ST(0) = sv_2mortal(newSVpvn(addr.sun_path, addr_len));
25f94b33 747#else
c2654555 748 ST(0) = (SV*)not_here("unpack_sockaddr_un");
25f94b33 749#endif
4633a7c4
LW
750 }
751
752void
2528d3bc 753pack_sockaddr_in(port, ip_address_sv)
2c129a17 754 unsigned short port
2528d3bc 755 SV * ip_address_sv
8e07c86e
AD
756 CODE:
757 {
758 struct sockaddr_in sin;
2528d3bc
JH
759 struct in_addr addr;
760 STRLEN addrlen;
761 char * ip_address;
762 if (DO_UTF8(ip_address_sv) && !sv_utf8_downgrade(ip_address_sv, 1))
c2654555 763 croak("Wide character in %s", "Socket::pack_sockaddr_in");
f780604f 764 ip_address = SvPVbyte(ip_address_sv, addrlen);
2528d3bc 765 if (addrlen == sizeof(addr) || addrlen == 4)
c2654555 766 addr.s_addr =
2528d3bc
JH
767 (ip_address[0] & 0xFF) << 24 |
768 (ip_address[1] & 0xFF) << 16 |
769 (ip_address[2] & 0xFF) << 8 |
770 (ip_address[3] & 0xFF);
771 else
9dea6244 772 croak("Bad arg length for %s, length is %"UVuf", should be %"UVuf,
2528d3bc 773 "Socket::pack_sockaddr_in",
9dea6244 774 (UV)addrlen, (UV)sizeof(addr));
c2654555 775 Zero(&sin, sizeof(sin), char);
4633a7c4 776 sin.sin_family = AF_INET;
8e07c86e 777 sin.sin_port = htons(port);
2528d3bc 778 sin.sin_addr.s_addr = htonl(addr.s_addr);
5897b17c 779# ifdef HAS_SOCKADDR_SA_LEN
c2654555 780 sin.sin_len = sizeof(sin);
5897b17c 781# endif
c2654555 782 ST(0) = sv_2mortal(newSVpvn((char *)&sin, sizeof(sin)));
8e07c86e
AD
783 }
784
785void
786unpack_sockaddr_in(sin_sv)
787 SV * sin_sv
788 PPCODE:
789 {
790 STRLEN sockaddrlen;
791 struct sockaddr_in addr;
aff163d9 792 SV *ip_address_sv;
f780604f 793 char * sin = SvPVbyte(sin_sv,sockaddrlen);
8e07c86e 794 if (sockaddrlen != sizeof(addr)) {
9dea6244 795 croak("Bad arg length for %s, length is %"UVuf", should be %"UVuf,
c2654555 796 "Socket::unpack_sockaddr_in", sockaddrlen, sizeof(addr));
8e07c86e 797 }
c2654555
CBW
798 Copy(sin, &addr, sizeof(addr), char);
799 if (addr.sin_family != AF_INET) {
4633a7c4 800 croak("Bad address family for %s, got %d, should be %d",
c2654555 801 "Socket::unpack_sockaddr_in", addr.sin_family, AF_INET);
a062834f 802 }
aff163d9 803 ip_address_sv = newSVpvn((char *)&addr.sin_addr, sizeof(addr.sin_addr));
8e07c86e 804
aff163d9
SH
805 if(GIMME_V == G_ARRAY) {
806 EXTEND(SP, 2);
807 mPUSHi(ntohs(addr.sin_port));
808 mPUSHs(ip_address_sv);
809 }
810 else {
811 mPUSHs(ip_address_sv);
812 }
8e07c86e 813 }
036d8bd4
SP
814
815void
9987e04d
PLE
816pack_sockaddr_in6(port, sin6_addr, scope_id=0, flowinfo=0)
817 unsigned short port
818 SV * sin6_addr
819 unsigned long scope_id
820 unsigned long flowinfo
821 CODE:
822 {
9dea6244 823#ifdef HAS_SOCKADDR_IN6
9987e04d
PLE
824 struct sockaddr_in6 sin6;
825 char * addrbytes;
826 STRLEN addrlen;
827 if (DO_UTF8(sin6_addr) && !sv_utf8_downgrade(sin6_addr, 1))
c2654555 828 croak("Wide character in %s", "Socket::pack_sockaddr_in6");
9987e04d 829 addrbytes = SvPVbyte(sin6_addr, addrlen);
c2654555 830 if (addrlen != sizeof(sin6.sin6_addr))
9dea6244
CBW
831 croak("Bad arg length %s, length is %"UVuf", should be %"UVuf,
832 "Socket::pack_sockaddr_in6", (UV)addrlen, (UV)sizeof(sin6.sin6_addr));
9987e04d
PLE
833 Zero(&sin6, sizeof(sin6), char);
834 sin6.sin6_family = AF_INET6;
835 sin6.sin6_port = htons(port);
836 sin6.sin6_flowinfo = htonl(flowinfo);
837 Copy(addrbytes, &sin6.sin6_addr, sizeof(sin6.sin6_addr), char);
24b8ee2e 838# ifdef HAS_SIN6_SCOPE_ID
9987e04d 839 sin6.sin6_scope_id = scope_id;
79c932f0 840# else
c2654555 841 if (scope_id != 0)
79c932f0 842 warn("%s cannot represent non-zero scope_id %d",
c2654555 843 "Socket::pack_sockaddr_in6", scope_id);
24b8ee2e 844# endif
5897b17c
PLE
845# ifdef HAS_SOCKADDR_SA_LEN
846 sin6.sin6_len = sizeof(sin6);
847# endif
c2654555 848 ST(0) = sv_2mortal(newSVpvn((char *)&sin6, sizeof(sin6)));
9987e04d
PLE
849#else
850 ST(0) = (SV*)not_here("pack_sockaddr_in6");
851#endif
852 }
853
854void
855unpack_sockaddr_in6(sin6_sv)
856 SV * sin6_sv
857 PPCODE:
858 {
9dea6244 859#ifdef HAS_SOCKADDR_IN6
9987e04d
PLE
860 STRLEN addrlen;
861 struct sockaddr_in6 sin6;
862 char * addrbytes = SvPVbyte(sin6_sv, addrlen);
aff163d9 863 SV *ip_address_sv;
9987e04d 864 if (addrlen != sizeof(sin6))
9dea6244
CBW
865 croak("Bad arg length for %s, length is %"UVuf", should be %"UVuf,
866 "Socket::unpack_sockaddr_in6", (UV)addrlen, (UV)sizeof(sin6));
9987e04d 867 Copy(addrbytes, &sin6, sizeof(sin6), char);
c2654555
CBW
868 if (sin6.sin6_family != AF_INET6)
869 croak("Bad address family for %s, got %d, should be %d",
870 "Socket::unpack_sockaddr_in6", sin6.sin6_family, AF_INET6);
aff163d9
SH
871 ip_address_sv = newSVpvn((char *)&sin6.sin6_addr, sizeof(sin6.sin6_addr));
872
873 if(GIMME_V == G_ARRAY) {
874 EXTEND(SP, 4);
875 mPUSHi(ntohs(sin6.sin6_port));
876 mPUSHs(ip_address_sv);
24b8ee2e 877# ifdef HAS_SIN6_SCOPE_ID
aff163d9 878 mPUSHi(sin6.sin6_scope_id);
24b8ee2e 879# else
aff163d9 880 mPUSHi(0);
24b8ee2e 881# endif
aff163d9
SH
882 mPUSHi(ntohl(sin6.sin6_flowinfo));
883 }
884 else {
885 mPUSHs(ip_address_sv);
886 }
9987e04d
PLE
887#else
888 ST(0) = (SV*)not_here("pack_sockaddr_in6");
889#endif
890 }
891
892void
036d8bd4 893inet_ntop(af, ip_address_sv)
c2654555
CBW
894 int af
895 SV * ip_address_sv
896 CODE:
036d8bd4 897#ifdef HAS_INETNTOP
d335a3e3 898 STRLEN addrlen, struct_size;
a5addb16 899#ifdef AF_INET6
036d8bd4
SP
900 struct in6_addr addr;
901 char str[INET6_ADDRSTRLEN];
a5addb16
PG
902#else
903 struct in_addr addr;
904 char str[INET_ADDRSTRLEN];
905#endif
eabcd9c8
A
906 char *ip_address;
907
908 if (DO_UTF8(ip_address_sv) && !sv_utf8_downgrade(ip_address_sv, 1))
909 croak("Wide character in %s", "Socket::inet_ntop");
910
911 ip_address = SvPV(ip_address_sv, addrlen);
036d8bd4 912
d335a3e3
RS
913 struct_size = sizeof(addr);
914
aff163d9
SH
915 switch(af) {
916 case AF_INET:
917 if(addrlen != 4)
918 croak("Bad address length for Socket::inet_ntop on AF_INET;"
d335a3e3 919 " got %d, should be 4", addrlen);
aff163d9 920 break;
a5addb16 921#ifdef AF_INET6
aff163d9
SH
922 case AF_INET6:
923 if(addrlen != 16)
924 croak("Bad address length for Socket::inet_ntop on AF_INET6;"
d335a3e3 925 " got %d, should be 16", addrlen);
aff163d9 926 break;
a5addb16 927#endif
aff163d9 928 default:
c2654555 929 croak("Bad address family for %s, got %d, should be"
a5addb16 930#ifdef AF_INET6
c2654555 931 " either AF_INET or AF_INET6",
a5addb16 932#else
c2654555 933 " AF_INET",
a5addb16 934#endif
c2654555
CBW
935 "Socket::inet_ntop", af);
936 }
036d8bd4 937
d335a3e3 938 Copy(ip_address, &addr, sizeof addr, char);
a5addb16 939 inet_ntop(af, &addr, str, sizeof str);
036d8bd4 940
c2654555 941 ST(0) = sv_2mortal(newSVpvn(str, strlen(str)));
036d8bd4 942#else
c2654555 943 ST(0) = (SV*)not_here("inet_ntop");
036d8bd4
SP
944#endif
945
946void
947inet_pton(af, host)
c2654555
CBW
948 int af
949 const char * host
950 CODE:
036d8bd4 951#ifdef HAS_INETPTON
c2654555 952 int ok;
eabcd9c8 953 int addrlen = 0;
a5addb16
PG
954#ifdef AF_INET6
955 struct in6_addr ip_address;
956#else
957 struct in_addr ip_address;
958#endif
959
eabcd9c8
A
960 switch(af) {
961 case AF_INET:
962 addrlen = 4;
963 break;
a5addb16 964#ifdef AF_INET6
eabcd9c8
A
965 case AF_INET6:
966 addrlen = 16;
967 break;
a5addb16 968#endif
eabcd9c8 969 default:
a5addb16
PG
970 croak("Bad address family for %s, got %d, should be"
971#ifdef AF_INET6
c2654555 972 " either AF_INET or AF_INET6",
a5addb16 973#else
c2654555 974 " AF_INET",
a5addb16 975#endif
c2654555
CBW
976 "Socket::inet_pton", af);
977 }
978 ok = (*host != '\0') && inet_pton(af, host, &ip_address);
036d8bd4 979
c2654555
CBW
980 ST(0) = sv_newmortal();
981 if (ok) {
eabcd9c8 982 sv_setpvn( ST(0), (char *)&ip_address, addrlen);
c2654555 983 }
036d8bd4 984#else
c2654555 985 ST(0) = (SV*)not_here("inet_pton");
036d8bd4 986#endif
4f5bb87f
PLE
987
988void
3be135d8
TC
989pack_ip_mreq(multiaddr, interface=&PL_sv_undef)
990 SV * multiaddr
991 SV * interface
992 CODE:
993 {
994#ifdef HAS_IP_MREQ
995 struct ip_mreq mreq;
996 char * multiaddrbytes;
997 char * interfacebytes;
998 STRLEN len;
999 if (DO_UTF8(multiaddr) && !sv_utf8_downgrade(multiaddr, 1))
1000 croak("Wide character in %s", "Socket::pack_ip_mreq");
1001 multiaddrbytes = SvPVbyte(multiaddr, len);
1002 if (len != sizeof(mreq.imr_multiaddr))
1003 croak("Bad arg length %s, length is %"UVuf", should be %"UVuf,
1004 "Socket::pack_ip_mreq", (UV)len, (UV)sizeof(mreq.imr_multiaddr));
1005 Zero(&mreq, sizeof(mreq), char);
1006 Copy(multiaddrbytes, &mreq.imr_multiaddr, sizeof(mreq.imr_multiaddr), char);
1007 if(SvOK(interface)) {
1008 if (DO_UTF8(interface) && !sv_utf8_downgrade(interface, 1))
1009 croak("Wide character in %s", "Socket::pack_ip_mreq");
1010 interfacebytes = SvPVbyte(interface, len);
1011 if (len != sizeof(mreq.imr_interface))
1012 croak("Bad arg length %s, length is %"UVuf", should be %"UVuf,
1013 "Socket::pack_ip_mreq", (UV)len, (UV)sizeof(mreq.imr_interface));
1014 Copy(interfacebytes, &mreq.imr_interface, sizeof(mreq.imr_interface), char);
1015 }
1016 else
1017 mreq.imr_interface.s_addr = INADDR_ANY;
1018 ST(0) = sv_2mortal(newSVpvn((char *)&mreq, sizeof(mreq)));
1019#else
1020 not_here("pack_ip_mreq");
1021#endif
1022 }
1023
1024void
1025unpack_ip_mreq(mreq_sv)
1026 SV * mreq_sv
1027 PPCODE:
1028 {
1029#ifdef HAS_IP_MREQ
1030 struct ip_mreq mreq;
1031 STRLEN mreqlen;
1032 char * mreqbytes = SvPVbyte(mreq_sv, mreqlen);
1033 if (mreqlen != sizeof(mreq))
1034 croak("Bad arg length for %s, length is %"UVuf", should be %"UVuf,
1035 "Socket::unpack_ip_mreq", (UV)mreqlen, (UV)sizeof(mreq));
1036 Copy(mreqbytes, &mreq, sizeof(mreq), char);
1037 EXTEND(SP, 2);
1038 mPUSHp((char *)&mreq.imr_multiaddr, sizeof(mreq.imr_multiaddr));
1039 mPUSHp((char *)&mreq.imr_interface, sizeof(mreq.imr_interface));
1040#else
1041 not_here("unpack_ip_mreq");
1042#endif
1043 }
1044
1045void
d4ada64a
SH
1046pack_ip_mreq_source(multiaddr, source, interface=&PL_sv_undef)
1047 SV * multiaddr
1048 SV * source
1049 SV * interface
1050 CODE:
1051 {
aff163d9 1052#if defined(HAS_IP_MREQ_SOURCE) && defined (IP_ADD_SOURCE_MEMBERSHIP)
d4ada64a
SH
1053 struct ip_mreq_source mreq;
1054 char * multiaddrbytes;
1055 char * sourcebytes;
1056 char * interfacebytes;
1057 STRLEN len;
1058 if (DO_UTF8(multiaddr) && !sv_utf8_downgrade(multiaddr, 1))
1059 croak("Wide character in %s", "Socket::pack_ip_mreq_source");
1060 multiaddrbytes = SvPVbyte(multiaddr, len);
1061 if (len != sizeof(mreq.imr_multiaddr))
1062 croak("Bad arg length %s, length is %"UVuf", should be %"UVuf,
1063 "Socket::pack_ip_mreq", (UV)len, (UV)sizeof(mreq.imr_multiaddr));
1064 if (DO_UTF8(source) && !sv_utf8_downgrade(source, 1))
1065 croak("Wide character in %s", "Socket::pack_ip_mreq_source");
1066 if (len != sizeof(mreq.imr_sourceaddr))
1067 croak("Bad arg length %s, length is %"UVuf", should be %"UVuf,
1068 "Socket::pack_ip_mreq", (UV)len, (UV)sizeof(mreq.imr_sourceaddr));
1069 sourcebytes = SvPVbyte(source, len);
1070 Zero(&mreq, sizeof(mreq), char);
1071 Copy(multiaddrbytes, &mreq.imr_multiaddr, sizeof(mreq.imr_multiaddr), char);
1072 Copy(sourcebytes, &mreq.imr_sourceaddr, sizeof(mreq.imr_sourceaddr), char);
1073 if(SvOK(interface)) {
1074 if (DO_UTF8(interface) && !sv_utf8_downgrade(interface, 1))
1075 croak("Wide character in %s", "Socket::pack_ip_mreq");
1076 interfacebytes = SvPVbyte(interface, len);
1077 if (len != sizeof(mreq.imr_interface))
1078 croak("Bad arg length %s, length is %"UVuf", should be %"UVuf,
1079 "Socket::pack_ip_mreq", (UV)len, (UV)sizeof(mreq.imr_interface));
1080 Copy(interfacebytes, &mreq.imr_interface, sizeof(mreq.imr_interface), char);
1081 }
1082 else
1083 mreq.imr_interface.s_addr = INADDR_ANY;
1084 ST(0) = sv_2mortal(newSVpvn((char *)&mreq, sizeof(mreq)));
1085#else
1086 not_here("pack_ip_mreq_source");
1087#endif
1088 }
1089
1090void
1091unpack_ip_mreq_source(mreq_sv)
1092 SV * mreq_sv
1093 PPCODE:
1094 {
aff163d9 1095#if defined(HAS_IP_MREQ_SOURCE) && defined (IP_ADD_SOURCE_MEMBERSHIP)
d4ada64a
SH
1096 struct ip_mreq_source mreq;
1097 STRLEN mreqlen;
1098 char * mreqbytes = SvPVbyte(mreq_sv, mreqlen);
1099 if (mreqlen != sizeof(mreq))
1100 croak("Bad arg length for %s, length is %"UVuf", should be %"UVuf,
1101 "Socket::unpack_ip_mreq_source", (UV)mreqlen, (UV)sizeof(mreq));
1102 Copy(mreqbytes, &mreq, sizeof(mreq), char);
1103 EXTEND(SP, 3);
1104 mPUSHp((char *)&mreq.imr_multiaddr, sizeof(mreq.imr_multiaddr));
1105 mPUSHp((char *)&mreq.imr_sourceaddr, sizeof(mreq.imr_sourceaddr));
1106 mPUSHp((char *)&mreq.imr_interface, sizeof(mreq.imr_interface));
1107#else
1108 not_here("unpack_ip_mreq_source");
1109#endif
1110 }
1111
1112void
3be135d8
TC
1113pack_ipv6_mreq(multiaddr, interface)
1114 SV * multiaddr
4f5bb87f
PLE
1115 unsigned int interface
1116 CODE:
1117 {
9dea6244 1118#ifdef HAS_IPV6_MREQ
4f5bb87f 1119 struct ipv6_mreq mreq;
3be135d8
TC
1120 char * multiaddrbytes;
1121 STRLEN len;
1122 if (DO_UTF8(multiaddr) && !sv_utf8_downgrade(multiaddr, 1))
c2654555 1123 croak("Wide character in %s", "Socket::pack_ipv6_mreq");
3be135d8
TC
1124 multiaddrbytes = SvPVbyte(multiaddr, len);
1125 if (len != sizeof(mreq.ipv6mr_multiaddr))
9dea6244 1126 croak("Bad arg length %s, length is %"UVuf", should be %"UVuf,
3be135d8 1127 "Socket::pack_ipv6_mreq", (UV)len, (UV)sizeof(mreq.ipv6mr_multiaddr));
4f5bb87f 1128 Zero(&mreq, sizeof(mreq), char);
3be135d8 1129 Copy(multiaddrbytes, &mreq.ipv6mr_multiaddr, sizeof(mreq.ipv6mr_multiaddr), char);
4f5bb87f 1130 mreq.ipv6mr_interface = interface;
c2654555 1131 ST(0) = sv_2mortal(newSVpvn((char *)&mreq, sizeof(mreq)));
4f5bb87f 1132#else
3be135d8 1133 not_here("pack_ipv6_mreq");
4f5bb87f
PLE
1134#endif
1135 }
1136
1137void
1138unpack_ipv6_mreq(mreq_sv)
1139 SV * mreq_sv
1140 PPCODE:
1141 {
9dea6244 1142#ifdef HAS_IPV6_MREQ
4f5bb87f
PLE
1143 struct ipv6_mreq mreq;
1144 STRLEN mreqlen;
1145 char * mreqbytes = SvPVbyte(mreq_sv, mreqlen);
1146 if (mreqlen != sizeof(mreq))
9dea6244
CBW
1147 croak("Bad arg length for %s, length is %"UVuf", should be %"UVuf,
1148 "Socket::unpack_ipv6_mreq", (UV)mreqlen, (UV)sizeof(mreq));
4f5bb87f
PLE
1149 Copy(mreqbytes, &mreq, sizeof(mreq), char);
1150 EXTEND(SP, 2);
1151 mPUSHp((char *)&mreq.ipv6mr_multiaddr, sizeof(mreq.ipv6mr_multiaddr));
1152 mPUSHi(mreq.ipv6mr_interface);
1153#else
1154 not_here("unpack_ipv6_mreq");
1155#endif
1156 }