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