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
22 /* XXX Configure test for <netinet/in_systm.h needed XXX */
23 # if defined(NeXT) || defined(__NeXT__)
24 # include <netinet/in_systm.h>
27 # include <netinet/in.h>
33 # include <arpa/inet.h>
36 # include <netinet/tcp.h>
39 # include "sockadapt.h"
55 # define INADDR_NONE 0xffffffff
56 #endif /* INADDR_NONE */
57 #ifndef INADDR_BROADCAST
58 # define INADDR_BROADCAST 0xffffffff
59 #endif /* INADDR_BROADCAST */
60 #ifndef INADDR_LOOPBACK
61 # define INADDR_LOOPBACK 0x7F000001
62 #endif /* INADDR_LOOPBACK */
67 * Check whether "cp" is a valid ascii representation
68 * of an Internet address and convert to a binary address.
69 * Returns 1 if the address is valid, 0 if not.
70 * This replaces inet_addr, the return value from which
71 * cannot distinguish between failure and a local broadcast address.
74 my_inet_aton(register const char *cp, struct in_addr *addr)
82 unsigned int parts[4];
83 register unsigned int *pp = parts;
89 * Collect number up to ``.''.
90 * Values are specified as for C:
91 * 0x=hex, 0=octal, other=decimal.
95 if (*++cp == 'x' || *cp == 'X')
100 while ((c = *cp) != '\0') {
102 val = (val * base) + (c - '0');
106 if (base == 16 && (s=strchr(PL_hexdigit,c))) {
108 ((s - PL_hexdigit) & 15);
118 * a.b.c (with c treated as 16-bits)
119 * a.b (with b treated as 24 bits)
121 if (pp >= parts + 3 || val > 0xff)
128 * Check for trailing characters.
130 if (*cp && !isSPACE(*cp))
133 * Concoct the address according to
134 * the number of parts specified.
136 nparts = pp - parts + 1; /* force to an int for switch() */
139 case 1: /* a -- 32 bits */
142 case 2: /* a.b -- 8.24 bits */
145 val |= parts[0] << 24;
148 case 3: /* a.b.c -- 8.8.16 bits */
151 val |= (parts[0] << 24) | (parts[1] << 16);
154 case 4: /* a.b.c.d -- 8.8.8.8 bits */
157 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
160 addr->s_addr = htonl(val);
165 #define inet_aton my_inet_aton
167 #endif /* ! HAS_INET_ATON */
173 croak("Socket::%s not implemented on this architecture", s);
178 constant(char *name, int arg)
183 if (strEQ(name, "AF_802"))
189 if (strEQ(name, "AF_APPLETALK"))
195 if (strEQ(name, "AF_CCITT"))
201 if (strEQ(name, "AF_CHAOS"))
207 if (strEQ(name, "AF_DATAKIT"))
213 if (strEQ(name, "AF_DECnet"))
219 if (strEQ(name, "AF_DLI"))
225 if (strEQ(name, "AF_ECMA"))
231 if (strEQ(name, "AF_GOSIP"))
237 if (strEQ(name, "AF_HYLINK"))
243 if (strEQ(name, "AF_IMPLINK"))
249 if (strEQ(name, "AF_INET"))
255 if (strEQ(name, "AF_LAT"))
261 if (strEQ(name, "AF_MAX"))
267 if (strEQ(name, "AF_NBS"))
273 if (strEQ(name, "AF_NIT"))
279 if (strEQ(name, "AF_NS"))
285 if (strEQ(name, "AF_OSI"))
291 if (strEQ(name, "AF_OSINET"))
297 if (strEQ(name, "AF_PUP"))
303 if (strEQ(name, "AF_SNA"))
309 if (strEQ(name, "AF_UNIX"))
315 if (strEQ(name, "AF_UNSPEC"))
321 if (strEQ(name, "AF_X25"))
343 if (strEQ(name, "IOV_MAX"))
349 if (strEQ(name, "IPPROTO_TCP"))
363 if (strEQ(name, "MSG_BCAST"))
369 if (strEQ(name, "MSG_CTLFLAGS"))
375 if (strEQ(name, "MSG_CTLIGNORE"))
377 return MSG_CTLIGNORE;
381 if (strEQ(name, "MSG_CTRUNC"))
382 #if defined(MSG_TRUNC) || defined(HAS_MSG_CTRUNC) /* might be an enum */
387 if (strEQ(name, "MSG_DONTROUTE"))
388 #if defined(MSG_DONTROUTE) || defined(HAS_MSG_DONTROUTE) /* might be an enum */
389 return MSG_DONTROUTE;
393 if (strEQ(name, "MSG_DONTWAIT"))
399 if (strEQ(name, "MSG_EOF"))
405 if (strEQ(name, "MSG_EOR"))
411 if (strEQ(name, "MSG_ERRQUEUE"))
417 if (strEQ(name, "MSG_FIN"))
423 if (strEQ(name, "MSG_MAXIOVLEN"))
425 return MSG_MAXIOVLEN;
429 if (strEQ(name, "MSG_MCAST"))
435 if (strEQ(name, "MSG_NOSIGNAL"))
441 if (strEQ(name, "MSG_OOB"))
442 #if defined(MSG_OOB) || defined(HAS_MSG_OOB) /* might be an enum */
447 if (strEQ(name, "MSG_PEEK"))
448 #if defined(MSG_PEEK) || defined(HAS_MSG_PEEK) /* might be an enum */
453 if (strEQ(name, "MSG_PROXY"))
454 #if defined(MSG_PROXY) || defined(HAS_MSG_PROXY) /* might be an enum */
459 if (strEQ(name, "MSG_RST"))
465 if (strEQ(name, "MSG_SYN"))
471 if (strEQ(name, "MSG_TRUNC"))
477 if (strEQ(name, "MSG_WAITALL"))
489 if (strEQ(name, "PF_802"))
495 if (strEQ(name, "PF_APPLETALK"))
501 if (strEQ(name, "PF_CCITT"))
507 if (strEQ(name, "PF_CHAOS"))
513 if (strEQ(name, "PF_DATAKIT"))
519 if (strEQ(name, "PF_DECnet"))
525 if (strEQ(name, "PF_DLI"))
531 if (strEQ(name, "PF_ECMA"))
537 if (strEQ(name, "PF_GOSIP"))
543 if (strEQ(name, "PF_HYLINK"))
549 if (strEQ(name, "PF_IMPLINK"))
555 if (strEQ(name, "PF_INET"))
561 if (strEQ(name, "PF_LAT"))
567 if (strEQ(name, "PF_MAX"))
573 if (strEQ(name, "PF_NBS"))
579 if (strEQ(name, "PF_NIT"))
585 if (strEQ(name, "PF_NS"))
591 if (strEQ(name, "PF_OSI"))
597 if (strEQ(name, "PF_OSINET"))
603 if (strEQ(name, "PF_PUP"))
609 if (strEQ(name, "PF_SNA"))
615 if (strEQ(name, "PF_UNIX"))
621 if (strEQ(name, "PF_UNSPEC"))
627 if (strEQ(name, "PF_X25"))
639 if (strEQ(name, "SCM_CONNECT"))
645 if (strEQ(name, "SCM_CREDENTIALS"))
646 #ifdef SCM_CREDENTIALS
647 return SCM_CREDENTIALS;
651 if (strEQ(name, "SCM_CREDS"))
657 if (strEQ(name, "SCM_RIGHTS"))
658 #if defined(SCM_RIGHTS) || defined(HAS_SCM_RIGHTS) /* might be an enum */
663 if (strEQ(name, "SCM_TIMESTAMP"))
665 return SCM_TIMESTAMP;
669 if (strEQ(name, "SHUT_RD"))
675 if (strEQ(name, "SHUT_RDWR"))
681 if (strEQ(name, "SHUT_WR"))
687 if (strEQ(name, "SOCK_DGRAM"))
693 if (strEQ(name, "SOCK_RAW"))
699 if (strEQ(name, "SOCK_RDM"))
705 if (strEQ(name, "SOCK_SEQPACKET"))
706 #ifdef SOCK_SEQPACKET
707 return SOCK_SEQPACKET;
711 if (strEQ(name, "SOCK_STREAM"))
717 if (strEQ(name, "SOL_SOCKET"))
723 if (strEQ(name, "SOMAXCONN"))
729 if (strEQ(name, "SO_ACCEPTCONN"))
731 return SO_ACCEPTCONN;
735 if (strEQ(name, "SO_BROADCAST"))
741 if (strEQ(name, "SO_DEBUG"))
747 if (strEQ(name, "SO_DONTLINGER"))
749 return SO_DONTLINGER;
753 if (strEQ(name, "SO_DONTROUTE"))
759 if (strEQ(name, "SO_ERROR"))
765 if (strEQ(name, "SO_KEEPALIVE"))
771 if (strEQ(name, "SO_LINGER"))
777 if (strEQ(name, "SO_OOBINLINE"))
783 if (strEQ(name, "SO_RCVBUF"))
789 if (strEQ(name, "SO_RCVLOWAT"))
795 if (strEQ(name, "SO_RCVTIMEO"))
801 if (strEQ(name, "SO_REUSEADDR"))
807 if (strEQ(name, "SO_REUSEPORT"))
813 if (strEQ(name, "SO_SNDBUF"))
819 if (strEQ(name, "SO_SNDLOWAT"))
825 if (strEQ(name, "SO_SNDTIMEO"))
831 if (strEQ(name, "SO_TYPE"))
837 if (strEQ(name, "SO_USELOOPBACK"))
838 #ifdef SO_USELOOPBACK
839 return SO_USELOOPBACK;
845 if (strEQ(name, "TCP_KEEPALIVE"))
847 return TCP_KEEPALIVE;
851 if (strEQ(name, "TCP_MAXRT"))
857 if (strEQ(name, "TCP_MAXSEG"))
863 if (strEQ(name, "TCP_NODELAY"))
869 if (strEQ(name, "TCP_STDURG"))
877 if (strEQ(name, "UIO_MAXIOV"))
904 MODULE = Socket PACKAGE = Socket
917 struct in_addr ip_address;
918 struct hostent * phe;
919 int ok = inet_aton(host, &ip_address);
921 if (!ok && (phe = gethostbyname(host))) {
922 Copy( phe->h_addr, &ip_address, phe->h_length, char );
926 ST(0) = sv_newmortal();
928 sv_setpvn( ST(0), (char *)&ip_address, sizeof ip_address );
933 inet_ntoa(ip_address_sv)
940 char * ip_address = SvPV(ip_address_sv,addrlen);
941 if (addrlen != sizeof(addr)) {
942 croak("Bad arg length for %s, length is %d, should be %d",
944 addrlen, sizeof(addr));
947 Copy( ip_address, &addr, sizeof addr, char );
948 addr_str = inet_ntoa(addr);
950 ST(0) = sv_2mortal(newSVpvn(addr_str, strlen(addr_str)));
954 pack_sockaddr_un(pathname)
959 struct sockaddr_un sun_ad; /* fear using sun */
962 Zero( &sun_ad, sizeof sun_ad, char );
963 sun_ad.sun_family = AF_UNIX;
964 len = strlen(pathname);
965 if (len > sizeof(sun_ad.sun_path))
966 len = sizeof(sun_ad.sun_path);
967 # ifdef OS2 /* Name should start with \socket\ and contain backslashes! */
972 if (pathname[0] != '/' && pathname[0] != '\\')
973 croak("Relative UNIX domain socket name '%s' unsupported", pathname);
975 || pathname[7] != '/' && pathname[7] != '\\'
976 || !strnicmp(pathname + 1, "socket", 6))
979 off = 0; /* Preserve names starting with \socket\ */
980 Copy( "\\socket", sun_ad.sun_path, off, char);
981 Copy( pathname, sun_ad.sun_path + off, len, char );
983 s = sun_ad.sun_path + off - 1;
989 # else /* !( defined OS2 ) */
990 Copy( pathname, sun_ad.sun_path, len, char );
992 ST(0) = sv_2mortal(newSVpvn((char *)&sun_ad, sizeof sun_ad));
994 ST(0) = (SV *) not_here("pack_sockaddr_un");
1000 unpack_sockaddr_un(sun_sv)
1005 struct sockaddr_un addr;
1007 char * sun_ad = SvPV(sun_sv,sockaddrlen);
1010 if (sockaddrlen != sizeof(addr)) {
1011 croak("Bad arg length for %s, length is %d, should be %d",
1012 "Socket::unpack_sockaddr_un",
1013 sockaddrlen, sizeof(addr));
1016 Copy( sun_ad, &addr, sizeof addr, char );
1018 if ( addr.sun_family != AF_UNIX ) {
1019 croak("Bad address family for %s, got %d, should be %d",
1020 "Socket::unpack_sockaddr_un",
1025 while (*e && e < addr.sun_path + sizeof addr.sun_path)
1027 ST(0) = sv_2mortal(newSVpvn(addr.sun_path, e - addr.sun_path));
1029 ST(0) = (SV *) not_here("unpack_sockaddr_un");
1034 pack_sockaddr_in(port,ip_address)
1039 struct sockaddr_in sin;
1041 Zero( &sin, sizeof sin, char );
1042 sin.sin_family = AF_INET;
1043 sin.sin_port = htons(port);
1044 Copy( ip_address, &sin.sin_addr, sizeof sin.sin_addr, char );
1046 ST(0) = sv_2mortal(newSVpvn((char *)&sin, sizeof sin));
1050 unpack_sockaddr_in(sin_sv)
1055 struct sockaddr_in addr;
1056 unsigned short port;
1057 struct in_addr ip_address;
1058 char * sin = SvPV(sin_sv,sockaddrlen);
1059 if (sockaddrlen != sizeof(addr)) {
1060 croak("Bad arg length for %s, length is %d, should be %d",
1061 "Socket::unpack_sockaddr_in",
1062 sockaddrlen, sizeof(addr));
1064 Copy( sin, &addr,sizeof addr, char );
1065 if ( addr.sin_family != AF_INET ) {
1066 croak("Bad address family for %s, got %d, should be %d",
1067 "Socket::unpack_sockaddr_in",
1071 port = ntohs(addr.sin_port);
1072 ip_address = addr.sin_addr;
1075 PUSHs(sv_2mortal(newSViv((IV) port)));
1076 PUSHs(sv_2mortal(newSVpvn((char *)&ip_address,sizeof ip_address)));
1083 struct in_addr ip_address;
1084 ip_address.s_addr = htonl(INADDR_ANY);
1085 ST(0) = sv_2mortal(newSVpvn((char *)&ip_address,sizeof ip_address ));
1092 struct in_addr ip_address;
1093 ip_address.s_addr = htonl(INADDR_LOOPBACK);
1094 ST(0) = sv_2mortal(newSVpvn((char *)&ip_address,sizeof ip_address));
1101 struct in_addr ip_address;
1102 ip_address.s_addr = htonl(INADDR_NONE);
1103 ST(0) = sv_2mortal(newSVpvn((char *)&ip_address,sizeof ip_address));
1110 struct in_addr ip_address;
1111 ip_address.s_addr = htonl(INADDR_BROADCAST);
1112 ST(0) = sv_2mortal(newSVpvn((char *)&ip_address,sizeof ip_address));