7 # include <sys/types.h>
9 # include <sys/socket.h>
10 # if defined(USE_SOCKS) && defined(I_SOCKS)
14 # define PF_INET AF_INET
15 # define PF_UNIX AF_UNIX
22 # include <netinet/in.h>
28 # include <arpa/inet.h>
31 # include <netinet/tcp.h>
34 # include "sockadapt.h"
50 # define INADDR_NONE 0xffffffff
51 #endif /* INADDR_NONE */
52 #ifndef INADDR_BROADCAST
53 # define INADDR_BROADCAST 0xffffffff
54 #endif /* INADDR_BROADCAST */
55 #ifndef INADDR_LOOPBACK
56 # define INADDR_LOOPBACK 0x7F000001
57 #endif /* INADDR_LOOPBACK */
62 * Check whether "cp" is a valid ascii representation
63 * of an Internet address and convert to a binary address.
64 * Returns 1 if the address is valid, 0 if not.
65 * This replaces inet_addr, the return value from which
66 * cannot distinguish between failure and a local broadcast address.
69 my_inet_aton(register const char *cp, struct in_addr *addr)
76 unsigned int parts[4];
77 register unsigned int *pp = parts;
83 * Collect number up to ``.''.
84 * Values are specified as for C:
85 * 0x=hex, 0=octal, other=decimal.
89 if (*++cp == 'x' || *cp == 'X')
94 while ((c = *cp) != '\0') {
96 val = (val * base) + (c - '0');
100 if (base == 16 && (s=strchr(PL_hexdigit,c))) {
102 ((s - PL_hexdigit) & 15);
112 * a.b.c (with c treated as 16-bits)
113 * a.b (with b treated as 24 bits)
115 if (pp >= parts + 3 || val > 0xff)
122 * Check for trailing characters.
124 if (*cp && !isSPACE(*cp))
127 * Concoct the address according to
128 * the number of parts specified.
130 nparts = pp - parts + 1; /* force to an int for switch() */
133 case 1: /* a -- 32 bits */
136 case 2: /* a.b -- 8.24 bits */
139 val |= parts[0] << 24;
142 case 3: /* a.b.c -- 8.8.16 bits */
145 val |= (parts[0] << 24) | (parts[1] << 16);
148 case 4: /* a.b.c.d -- 8.8.8.8 bits */
151 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
154 addr->s_addr = htonl(val);
159 #define inet_aton my_inet_aton
161 #endif /* ! HAS_INET_ATON */
167 croak("Socket::%s not implemented on this architecture", s);
172 constant(char *name, int arg)
177 if (strEQ(name, "AF_802"))
183 if (strEQ(name, "AF_APPLETALK"))
189 if (strEQ(name, "AF_CCITT"))
195 if (strEQ(name, "AF_CHAOS"))
201 if (strEQ(name, "AF_DATAKIT"))
207 if (strEQ(name, "AF_DECnet"))
213 if (strEQ(name, "AF_DLI"))
219 if (strEQ(name, "AF_ECMA"))
225 if (strEQ(name, "AF_GOSIP"))
231 if (strEQ(name, "AF_HYLINK"))
237 if (strEQ(name, "AF_IMPLINK"))
243 if (strEQ(name, "AF_INET"))
249 if (strEQ(name, "AF_LAT"))
255 if (strEQ(name, "AF_MAX"))
261 if (strEQ(name, "AF_NBS"))
267 if (strEQ(name, "AF_NIT"))
273 if (strEQ(name, "AF_NS"))
279 if (strEQ(name, "AF_OSI"))
285 if (strEQ(name, "AF_OSINET"))
291 if (strEQ(name, "AF_PUP"))
297 if (strEQ(name, "AF_SNA"))
303 if (strEQ(name, "AF_UNIX"))
309 if (strEQ(name, "AF_UNSPEC"))
315 if (strEQ(name, "AF_X25"))
337 if (strEQ(name, "IOV_MAX"))
343 if (strEQ(name, "IPPROTO_TCP"))
357 if (strEQ(name, "MSG_BCAST"))
363 if (strEQ(name, "MSG_CTLFLAGS"))
369 if (strEQ(name, "MSG_CTLIGNORE"))
371 return MSG_CTLIGNORE;
375 if (strEQ(name, "MSG_CTRUNC"))
376 #if defined(MSG_TRUNC) || defined(HAS_MSG_CTRUNC) /* might be an enum */
381 if (strEQ(name, "MSG_DONTROUTE"))
382 #if defined(MSG_DONTROUTE) || defined(HAS_MSG_DONTROUTE) /* might be an enum */
383 return MSG_DONTROUTE;
387 if (strEQ(name, "MSG_DONTWAIT"))
393 if (strEQ(name, "MSG_EOF"))
399 if (strEQ(name, "MSG_EOR"))
405 if (strEQ(name, "MSG_ERRQUEUE"))
411 if (strEQ(name, "MSG_FIN"))
417 if (strEQ(name, "MSG_MAXIOVLEN"))
419 return MSG_MAXIOVLEN;
423 if (strEQ(name, "MSG_MCAST"))
429 if (strEQ(name, "MSG_NOSIGNAL"))
435 if (strEQ(name, "MSG_OOB"))
436 #if defined(MSG_OOB) || defined(HAS_MSG_OOB) /* might be an enum */
441 if (strEQ(name, "MSG_PEEK"))
442 #if defined(MSG_PEEK) || defined(HAS_MSG_PEEK) /* might be an enum */
447 if (strEQ(name, "MSG_PROXY"))
448 #if defined(MSG_PROXY) || defined(HAS_MSG_PROXY) /* might be an enum */
453 if (strEQ(name, "MSG_RST"))
459 if (strEQ(name, "MSG_SYN"))
465 if (strEQ(name, "MSG_TRUNC"))
471 if (strEQ(name, "MSG_WAITALL"))
483 if (strEQ(name, "PF_802"))
489 if (strEQ(name, "PF_APPLETALK"))
495 if (strEQ(name, "PF_CCITT"))
501 if (strEQ(name, "PF_CHAOS"))
507 if (strEQ(name, "PF_DATAKIT"))
513 if (strEQ(name, "PF_DECnet"))
519 if (strEQ(name, "PF_DLI"))
525 if (strEQ(name, "PF_ECMA"))
531 if (strEQ(name, "PF_GOSIP"))
537 if (strEQ(name, "PF_HYLINK"))
543 if (strEQ(name, "PF_IMPLINK"))
549 if (strEQ(name, "PF_INET"))
555 if (strEQ(name, "PF_LAT"))
561 if (strEQ(name, "PF_MAX"))
567 if (strEQ(name, "PF_NBS"))
573 if (strEQ(name, "PF_NIT"))
579 if (strEQ(name, "PF_NS"))
585 if (strEQ(name, "PF_OSI"))
591 if (strEQ(name, "PF_OSINET"))
597 if (strEQ(name, "PF_PUP"))
603 if (strEQ(name, "PF_SNA"))
609 if (strEQ(name, "PF_UNIX"))
615 if (strEQ(name, "PF_UNSPEC"))
621 if (strEQ(name, "PF_X25"))
633 if (strEQ(name, "SCM_CONNECT"))
639 if (strEQ(name, "SCM_CREDENTIALS"))
640 #ifdef SCM_CREDENTIALS
641 return SCM_CREDENTIALS;
645 if (strEQ(name, "SCM_CREDS"))
651 if (strEQ(name, "SCM_RIGHTS"))
652 #if defined(SCM_RIGHTS) || defined(HAS_SCM_RIGHTS) /* might be an enum */
657 if (strEQ(name, "SCM_TIMESTAMP"))
659 return SCM_TIMESTAMP;
663 if (strEQ(name, "SOCK_DGRAM"))
669 if (strEQ(name, "SOCK_RAW"))
675 if (strEQ(name, "SOCK_RDM"))
681 if (strEQ(name, "SOCK_SEQPACKET"))
682 #ifdef SOCK_SEQPACKET
683 return SOCK_SEQPACKET;
687 if (strEQ(name, "SOCK_STREAM"))
693 if (strEQ(name, "SOL_SOCKET"))
699 if (strEQ(name, "SOMAXCONN"))
705 if (strEQ(name, "SO_ACCEPTCONN"))
707 return SO_ACCEPTCONN;
711 if (strEQ(name, "SO_BROADCAST"))
717 if (strEQ(name, "SO_DEBUG"))
723 if (strEQ(name, "SO_DONTLINGER"))
725 return SO_DONTLINGER;
729 if (strEQ(name, "SO_DONTROUTE"))
735 if (strEQ(name, "SO_ERROR"))
741 if (strEQ(name, "SO_KEEPALIVE"))
747 if (strEQ(name, "SO_LINGER"))
753 if (strEQ(name, "SO_OOBINLINE"))
759 if (strEQ(name, "SO_RCVBUF"))
765 if (strEQ(name, "SO_RCVLOWAT"))
771 if (strEQ(name, "SO_RCVTIMEO"))
777 if (strEQ(name, "SO_REUSEADDR"))
783 if (strEQ(name, "SO_REUSEPORT"))
789 if (strEQ(name, "SO_SNDBUF"))
795 if (strEQ(name, "SO_SNDLOWAT"))
801 if (strEQ(name, "SO_SNDTIMEO"))
807 if (strEQ(name, "SO_TYPE"))
813 if (strEQ(name, "SO_USELOOPBACK"))
814 #ifdef SO_USELOOPBACK
815 return SO_USELOOPBACK;
821 if (strEQ(name, "TCP_KEEPALIVE"))
823 return TCP_KEEPALIVE;
827 if (strEQ(name, "TCP_MAXRT"))
833 if (strEQ(name, "TCP_MAXSEG"))
839 if (strEQ(name, "TCP_NODELAY"))
845 if (strEQ(name, "TCP_STDURG"))
853 if (strEQ(name, "UIO_MAXIOV"))
880 MODULE = Socket PACKAGE = Socket
893 struct in_addr ip_address;
894 struct hostent * phe;
895 int ok = inet_aton(host, &ip_address);
897 if (!ok && (phe = gethostbyname(host))) {
898 Copy( phe->h_addr, &ip_address, phe->h_length, char );
902 ST(0) = sv_newmortal();
904 sv_setpvn( ST(0), (char *)&ip_address, sizeof ip_address );
909 inet_ntoa(ip_address_sv)
916 char * ip_address = SvPV(ip_address_sv,addrlen);
917 if (addrlen != sizeof(addr)) {
918 croak("Bad arg length for %s, length is %d, should be %d",
920 addrlen, sizeof(addr));
923 Copy( ip_address, &addr, sizeof addr, char );
924 addr_str = inet_ntoa(addr);
926 ST(0) = sv_2mortal(newSVpvn(addr_str, strlen(addr_str)));
930 pack_sockaddr_un(pathname)
935 struct sockaddr_un sun_ad; /* fear using sun */
938 Zero( &sun_ad, sizeof sun_ad, char );
939 sun_ad.sun_family = AF_UNIX;
940 len = strlen(pathname);
941 if (len > sizeof(sun_ad.sun_path))
942 len = sizeof(sun_ad.sun_path);
943 # ifdef OS2 /* Name should start with \socket\ and contain backslashes! */
948 if (pathname[0] != '/' && pathname[0] != '\\')
949 croak("Relative UNIX domain socket name '%s' unsupported", pathname);
951 || pathname[7] != '/' && pathname[7] != '\\'
952 || !strnicmp(pathname + 1, "socket", 6))
955 off = 0; /* Preserve names starting with \socket\ */
956 Copy( "\\socket", sun_ad.sun_path, off, char);
957 Copy( pathname, sun_ad.sun_path + off, len, char );
959 s = sun_ad.sun_path + off - 1;
965 # else /* !( defined OS2 ) */
966 Copy( pathname, sun_ad.sun_path, len, char );
968 ST(0) = sv_2mortal(newSVpvn((char *)&sun_ad, sizeof sun_ad));
970 ST(0) = (SV *) not_here("pack_sockaddr_un");
976 unpack_sockaddr_un(sun_sv)
981 struct sockaddr_un addr;
983 char * sun_ad = SvPV(sun_sv,sockaddrlen);
986 if (sockaddrlen != sizeof(addr)) {
987 croak("Bad arg length for %s, length is %d, should be %d",
988 "Socket::unpack_sockaddr_un",
989 sockaddrlen, sizeof(addr));
992 Copy( sun_ad, &addr, sizeof addr, char );
994 if ( addr.sun_family != AF_UNIX ) {
995 croak("Bad address family for %s, got %d, should be %d",
996 "Socket::unpack_sockaddr_un",
1001 while (*e && e < addr.sun_path + sizeof addr.sun_path)
1003 ST(0) = sv_2mortal(newSVpvn(addr.sun_path, e - addr.sun_path));
1005 ST(0) = (SV *) not_here("unpack_sockaddr_un");
1010 pack_sockaddr_in(port,ip_address)
1015 struct sockaddr_in sin;
1017 Zero( &sin, sizeof sin, char );
1018 sin.sin_family = AF_INET;
1019 sin.sin_port = htons(port);
1020 Copy( ip_address, &sin.sin_addr, sizeof sin.sin_addr, char );
1022 ST(0) = sv_2mortal(newSVpvn((char *)&sin, sizeof sin));
1026 unpack_sockaddr_in(sin_sv)
1031 struct sockaddr_in addr;
1032 unsigned short port;
1033 struct in_addr ip_address;
1034 char * sin = SvPV(sin_sv,sockaddrlen);
1035 if (sockaddrlen != sizeof(addr)) {
1036 croak("Bad arg length for %s, length is %d, should be %d",
1037 "Socket::unpack_sockaddr_in",
1038 sockaddrlen, sizeof(addr));
1040 Copy( sin, &addr,sizeof addr, char );
1041 if ( addr.sin_family != AF_INET ) {
1042 croak("Bad address family for %s, got %d, should be %d",
1043 "Socket::unpack_sockaddr_in",
1047 port = ntohs(addr.sin_port);
1048 ip_address = addr.sin_addr;
1051 PUSHs(sv_2mortal(newSViv((IV) port)));
1052 PUSHs(sv_2mortal(newSVpvn((char *)&ip_address,sizeof ip_address)));
1059 struct in_addr ip_address;
1060 ip_address.s_addr = htonl(INADDR_ANY);
1061 ST(0) = sv_2mortal(newSVpvn((char *)&ip_address,sizeof ip_address ));
1068 struct in_addr ip_address;
1069 ip_address.s_addr = htonl(INADDR_LOOPBACK);
1070 ST(0) = sv_2mortal(newSVpvn((char *)&ip_address,sizeof ip_address));
1077 struct in_addr ip_address;
1078 ip_address.s_addr = htonl(INADDR_NONE);
1079 ST(0) = sv_2mortal(newSVpvn((char *)&ip_address,sizeof ip_address));
1086 struct in_addr ip_address;
1087 ip_address.s_addr = htonl(INADDR_BROADCAST);
1088 ST(0) = sv_2mortal(newSVpvn((char *)&ip_address,sizeof ip_address));