1 #define PERL_NO_GET_CONTEXT
8 # include <sys/types.h>
10 # include <sys/socket.h>
11 # if defined(USE_SOCKS) && defined(I_SOCKS)
15 # define PF_INET AF_INET
16 # define PF_UNIX AF_UNIX
23 # include <netinet/in.h>
29 # include <arpa/inet.h>
32 # include <netinet/tcp.h>
35 # include "sockadapt.h"
51 # define INADDR_NONE 0xffffffff
52 #endif /* INADDR_NONE */
53 #ifndef INADDR_BROADCAST
54 # define INADDR_BROADCAST 0xffffffff
55 #endif /* INADDR_BROADCAST */
56 #ifndef INADDR_LOOPBACK
57 # define INADDR_LOOPBACK 0x7F000001
58 #endif /* INADDR_LOOPBACK */
63 * Check whether "cp" is a valid ascii representation
64 * of an Internet address and convert to a binary address.
65 * Returns 1 if the address is valid, 0 if not.
66 * This replaces inet_addr, the return value from which
67 * cannot distinguish between failure and a local broadcast address.
70 my_inet_aton(register const char *cp, struct in_addr *addr)
78 unsigned int parts[4];
79 register unsigned int *pp = parts;
85 * Collect number up to ``.''.
86 * Values are specified as for C:
87 * 0x=hex, 0=octal, other=decimal.
91 if (*++cp == 'x' || *cp == 'X')
96 while ((c = *cp) != '\0') {
98 val = (val * base) + (c - '0');
102 if (base == 16 && (s=strchr(PL_hexdigit,c))) {
104 ((s - PL_hexdigit) & 15);
114 * a.b.c (with c treated as 16-bits)
115 * a.b (with b treated as 24 bits)
117 if (pp >= parts + 3 || val > 0xff)
124 * Check for trailing characters.
126 if (*cp && !isSPACE(*cp))
129 * Concoct the address according to
130 * the number of parts specified.
132 nparts = pp - parts + 1; /* force to an int for switch() */
135 case 1: /* a -- 32 bits */
138 case 2: /* a.b -- 8.24 bits */
141 val |= parts[0] << 24;
144 case 3: /* a.b.c -- 8.8.16 bits */
147 val |= (parts[0] << 24) | (parts[1] << 16);
150 case 4: /* a.b.c.d -- 8.8.8.8 bits */
153 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
156 addr->s_addr = htonl(val);
161 #define inet_aton my_inet_aton
163 #endif /* ! HAS_INET_ATON */
169 croak("Socket::%s not implemented on this architecture", s);
174 constant(char *name, int arg)
179 if (strEQ(name, "AF_802"))
185 if (strEQ(name, "AF_APPLETALK"))
191 if (strEQ(name, "AF_CCITT"))
197 if (strEQ(name, "AF_CHAOS"))
203 if (strEQ(name, "AF_DATAKIT"))
209 if (strEQ(name, "AF_DECnet"))
215 if (strEQ(name, "AF_DLI"))
221 if (strEQ(name, "AF_ECMA"))
227 if (strEQ(name, "AF_GOSIP"))
233 if (strEQ(name, "AF_HYLINK"))
239 if (strEQ(name, "AF_IMPLINK"))
245 if (strEQ(name, "AF_INET"))
251 if (strEQ(name, "AF_LAT"))
257 if (strEQ(name, "AF_MAX"))
263 if (strEQ(name, "AF_NBS"))
269 if (strEQ(name, "AF_NIT"))
275 if (strEQ(name, "AF_NS"))
281 if (strEQ(name, "AF_OSI"))
287 if (strEQ(name, "AF_OSINET"))
293 if (strEQ(name, "AF_PUP"))
299 if (strEQ(name, "AF_SNA"))
305 if (strEQ(name, "AF_UNIX"))
311 if (strEQ(name, "AF_UNSPEC"))
317 if (strEQ(name, "AF_X25"))
339 if (strEQ(name, "IOV_MAX"))
345 if (strEQ(name, "IPPROTO_TCP"))
359 if (strEQ(name, "MSG_BCAST"))
365 if (strEQ(name, "MSG_CTLFLAGS"))
371 if (strEQ(name, "MSG_CTLIGNORE"))
373 return MSG_CTLIGNORE;
377 if (strEQ(name, "MSG_CTRUNC"))
378 #if defined(MSG_TRUNC) || defined(HAS_MSG_CTRUNC) /* might be an enum */
383 if (strEQ(name, "MSG_DONTROUTE"))
384 #if defined(MSG_DONTROUTE) || defined(HAS_MSG_DONTROUTE) /* might be an enum */
385 return MSG_DONTROUTE;
389 if (strEQ(name, "MSG_DONTWAIT"))
395 if (strEQ(name, "MSG_EOF"))
401 if (strEQ(name, "MSG_EOR"))
407 if (strEQ(name, "MSG_ERRQUEUE"))
413 if (strEQ(name, "MSG_FIN"))
419 if (strEQ(name, "MSG_MAXIOVLEN"))
421 return MSG_MAXIOVLEN;
425 if (strEQ(name, "MSG_MCAST"))
431 if (strEQ(name, "MSG_NOSIGNAL"))
437 if (strEQ(name, "MSG_OOB"))
438 #if defined(MSG_OOB) || defined(HAS_MSG_OOB) /* might be an enum */
443 if (strEQ(name, "MSG_PEEK"))
444 #if defined(MSG_PEEK) || defined(HAS_MSG_PEEK) /* might be an enum */
449 if (strEQ(name, "MSG_PROXY"))
450 #if defined(MSG_PROXY) || defined(HAS_MSG_PROXY) /* might be an enum */
455 if (strEQ(name, "MSG_RST"))
461 if (strEQ(name, "MSG_SYN"))
467 if (strEQ(name, "MSG_TRUNC"))
473 if (strEQ(name, "MSG_WAITALL"))
485 if (strEQ(name, "PF_802"))
491 if (strEQ(name, "PF_APPLETALK"))
497 if (strEQ(name, "PF_CCITT"))
503 if (strEQ(name, "PF_CHAOS"))
509 if (strEQ(name, "PF_DATAKIT"))
515 if (strEQ(name, "PF_DECnet"))
521 if (strEQ(name, "PF_DLI"))
527 if (strEQ(name, "PF_ECMA"))
533 if (strEQ(name, "PF_GOSIP"))
539 if (strEQ(name, "PF_HYLINK"))
545 if (strEQ(name, "PF_IMPLINK"))
551 if (strEQ(name, "PF_INET"))
557 if (strEQ(name, "PF_LAT"))
563 if (strEQ(name, "PF_MAX"))
569 if (strEQ(name, "PF_NBS"))
575 if (strEQ(name, "PF_NIT"))
581 if (strEQ(name, "PF_NS"))
587 if (strEQ(name, "PF_OSI"))
593 if (strEQ(name, "PF_OSINET"))
599 if (strEQ(name, "PF_PUP"))
605 if (strEQ(name, "PF_SNA"))
611 if (strEQ(name, "PF_UNIX"))
617 if (strEQ(name, "PF_UNSPEC"))
623 if (strEQ(name, "PF_X25"))
635 if (strEQ(name, "SCM_CONNECT"))
641 if (strEQ(name, "SCM_CREDENTIALS"))
642 #ifdef SCM_CREDENTIALS
643 return SCM_CREDENTIALS;
647 if (strEQ(name, "SCM_CREDS"))
653 if (strEQ(name, "SCM_RIGHTS"))
654 #if defined(SCM_RIGHTS) || defined(HAS_SCM_RIGHTS) /* might be an enum */
659 if (strEQ(name, "SCM_TIMESTAMP"))
661 return SCM_TIMESTAMP;
665 if (strEQ(name, "SOCK_DGRAM"))
671 if (strEQ(name, "SOCK_RAW"))
677 if (strEQ(name, "SOCK_RDM"))
683 if (strEQ(name, "SOCK_SEQPACKET"))
684 #ifdef SOCK_SEQPACKET
685 return SOCK_SEQPACKET;
689 if (strEQ(name, "SOCK_STREAM"))
695 if (strEQ(name, "SOL_SOCKET"))
701 if (strEQ(name, "SOMAXCONN"))
707 if (strEQ(name, "SO_ACCEPTCONN"))
709 return SO_ACCEPTCONN;
713 if (strEQ(name, "SO_BROADCAST"))
719 if (strEQ(name, "SO_DEBUG"))
725 if (strEQ(name, "SO_DONTLINGER"))
727 return SO_DONTLINGER;
731 if (strEQ(name, "SO_DONTROUTE"))
737 if (strEQ(name, "SO_ERROR"))
743 if (strEQ(name, "SO_KEEPALIVE"))
749 if (strEQ(name, "SO_LINGER"))
755 if (strEQ(name, "SO_OOBINLINE"))
761 if (strEQ(name, "SO_RCVBUF"))
767 if (strEQ(name, "SO_RCVLOWAT"))
773 if (strEQ(name, "SO_RCVTIMEO"))
779 if (strEQ(name, "SO_REUSEADDR"))
785 if (strEQ(name, "SO_REUSEPORT"))
791 if (strEQ(name, "SO_SNDBUF"))
797 if (strEQ(name, "SO_SNDLOWAT"))
803 if (strEQ(name, "SO_SNDTIMEO"))
809 if (strEQ(name, "SO_TYPE"))
815 if (strEQ(name, "SO_USELOOPBACK"))
816 #ifdef SO_USELOOPBACK
817 return SO_USELOOPBACK;
823 if (strEQ(name, "TCP_KEEPALIVE"))
825 return TCP_KEEPALIVE;
829 if (strEQ(name, "TCP_MAXRT"))
835 if (strEQ(name, "TCP_MAXSEG"))
841 if (strEQ(name, "TCP_NODELAY"))
847 if (strEQ(name, "TCP_STDURG"))
855 if (strEQ(name, "UIO_MAXIOV"))
882 MODULE = Socket PACKAGE = Socket
895 struct in_addr ip_address;
896 struct hostent * phe;
897 int ok = inet_aton(host, &ip_address);
899 if (!ok && (phe = gethostbyname(host))) {
900 Copy( phe->h_addr, &ip_address, phe->h_length, char );
904 ST(0) = sv_newmortal();
906 sv_setpvn( ST(0), (char *)&ip_address, sizeof ip_address );
911 inet_ntoa(ip_address_sv)
918 char * ip_address = SvPV(ip_address_sv,addrlen);
919 if (addrlen != sizeof(addr)) {
920 croak("Bad arg length for %s, length is %d, should be %d",
922 addrlen, sizeof(addr));
925 Copy( ip_address, &addr, sizeof addr, char );
926 addr_str = inet_ntoa(addr);
928 ST(0) = sv_2mortal(newSVpvn(addr_str, strlen(addr_str)));
932 pack_sockaddr_un(pathname)
937 struct sockaddr_un sun_ad; /* fear using sun */
940 Zero( &sun_ad, sizeof sun_ad, char );
941 sun_ad.sun_family = AF_UNIX;
942 len = strlen(pathname);
943 if (len > sizeof(sun_ad.sun_path))
944 len = sizeof(sun_ad.sun_path);
945 # ifdef OS2 /* Name should start with \socket\ and contain backslashes! */
950 if (pathname[0] != '/' && pathname[0] != '\\')
951 croak("Relative UNIX domain socket name '%s' unsupported", pathname);
953 || pathname[7] != '/' && pathname[7] != '\\'
954 || !strnicmp(pathname + 1, "socket", 6))
957 off = 0; /* Preserve names starting with \socket\ */
958 Copy( "\\socket", sun_ad.sun_path, off, char);
959 Copy( pathname, sun_ad.sun_path + off, len, char );
961 s = sun_ad.sun_path + off - 1;
967 # else /* !( defined OS2 ) */
968 Copy( pathname, sun_ad.sun_path, len, char );
970 ST(0) = sv_2mortal(newSVpvn((char *)&sun_ad, sizeof sun_ad));
972 ST(0) = (SV *) not_here("pack_sockaddr_un");
978 unpack_sockaddr_un(sun_sv)
983 struct sockaddr_un addr;
985 char * sun_ad = SvPV(sun_sv,sockaddrlen);
988 if (sockaddrlen != sizeof(addr)) {
989 croak("Bad arg length for %s, length is %d, should be %d",
990 "Socket::unpack_sockaddr_un",
991 sockaddrlen, sizeof(addr));
994 Copy( sun_ad, &addr, sizeof addr, char );
996 if ( addr.sun_family != AF_UNIX ) {
997 croak("Bad address family for %s, got %d, should be %d",
998 "Socket::unpack_sockaddr_un",
1003 while (*e && e < addr.sun_path + sizeof addr.sun_path)
1005 ST(0) = sv_2mortal(newSVpvn(addr.sun_path, e - addr.sun_path));
1007 ST(0) = (SV *) not_here("unpack_sockaddr_un");
1012 pack_sockaddr_in(port,ip_address)
1017 struct sockaddr_in sin;
1019 Zero( &sin, sizeof sin, char );
1020 sin.sin_family = AF_INET;
1021 sin.sin_port = htons(port);
1022 Copy( ip_address, &sin.sin_addr, sizeof sin.sin_addr, char );
1024 ST(0) = sv_2mortal(newSVpvn((char *)&sin, sizeof sin));
1028 unpack_sockaddr_in(sin_sv)
1033 struct sockaddr_in addr;
1034 unsigned short port;
1035 struct in_addr ip_address;
1036 char * sin = SvPV(sin_sv,sockaddrlen);
1037 if (sockaddrlen != sizeof(addr)) {
1038 croak("Bad arg length for %s, length is %d, should be %d",
1039 "Socket::unpack_sockaddr_in",
1040 sockaddrlen, sizeof(addr));
1042 Copy( sin, &addr,sizeof addr, char );
1043 if ( addr.sin_family != AF_INET ) {
1044 croak("Bad address family for %s, got %d, should be %d",
1045 "Socket::unpack_sockaddr_in",
1049 port = ntohs(addr.sin_port);
1050 ip_address = addr.sin_addr;
1053 PUSHs(sv_2mortal(newSViv((IV) port)));
1054 PUSHs(sv_2mortal(newSVpvn((char *)&ip_address,sizeof ip_address)));
1061 struct in_addr ip_address;
1062 ip_address.s_addr = htonl(INADDR_ANY);
1063 ST(0) = sv_2mortal(newSVpvn((char *)&ip_address,sizeof ip_address ));
1070 struct in_addr ip_address;
1071 ip_address.s_addr = htonl(INADDR_LOOPBACK);
1072 ST(0) = sv_2mortal(newSVpvn((char *)&ip_address,sizeof ip_address));
1079 struct in_addr ip_address;
1080 ip_address.s_addr = htonl(INADDR_NONE);
1081 ST(0) = sv_2mortal(newSVpvn((char *)&ip_address,sizeof ip_address));
1088 struct in_addr ip_address;
1089 ip_address.s_addr = htonl(INADDR_BROADCAST);
1090 ST(0) = sv_2mortal(newSVpvn((char *)&ip_address,sizeof ip_address));