7 # include <sys/types.h>
9 # include <sys/socket.h>
11 # define PF_INET AF_INET
12 # define PF_UNIX AF_UNIX
19 # include <netinet/in.h>
23 # include <arpa/inet.h>
26 # include <netinet/tcp.h>
29 # include "sockadapt.h"
45 # define INADDR_NONE 0xffffffff
46 #endif /* INADDR_NONE */
47 #ifndef INADDR_BROADCAST
48 # define INADDR_BROADCAST 0xffffffff
49 #endif /* INADDR_BROADCAST */
50 #ifndef INADDR_LOOPBACK
51 # define INADDR_LOOPBACK 0x7F000001
52 #endif /* INADDR_LOOPBACK */
57 * Check whether "cp" is a valid ascii representation
58 * of an Internet address and convert to a binary address.
59 * Returns 1 if the address is valid, 0 if not.
60 * This replaces inet_addr, the return value from which
61 * cannot distinguish between failure and a local broadcast address.
64 my_inet_aton(register const char *cp, struct in_addr *addr)
71 unsigned int parts[4];
72 register unsigned int *pp = parts;
78 * Collect number up to ``.''.
79 * Values are specified as for C:
80 * 0x=hex, 0=octal, other=decimal.
84 if (*++cp == 'x' || *cp == 'X')
89 while ((c = *cp) != '\0') {
91 val = (val * base) + (c - '0');
95 if (base == 16 && (s=strchr(PL_hexdigit,c))) {
97 ((s - PL_hexdigit) & 15);
107 * a.b.c (with c treated as 16-bits)
108 * a.b (with b treated as 24 bits)
110 if (pp >= parts + 3 || val > 0xff)
117 * Check for trailing characters.
119 if (*cp && !isSPACE(*cp))
122 * Concoct the address according to
123 * the number of parts specified.
125 nparts = pp - parts + 1; /* force to an int for switch() */
128 case 1: /* a -- 32 bits */
131 case 2: /* a.b -- 8.24 bits */
134 val |= parts[0] << 24;
137 case 3: /* a.b.c -- 8.8.16 bits */
140 val |= (parts[0] << 24) | (parts[1] << 16);
143 case 4: /* a.b.c.d -- 8.8.8.8 bits */
146 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
149 addr->s_addr = htonl(val);
154 #define inet_aton my_inet_aton
156 #endif /* ! HAS_INET_ATON */
162 croak("Socket::%s not implemented on this architecture", s);
167 constant(char *name, int arg)
172 if (strEQ(name, "AF_802"))
178 if (strEQ(name, "AF_APPLETALK"))
184 if (strEQ(name, "AF_CCITT"))
190 if (strEQ(name, "AF_CHAOS"))
196 if (strEQ(name, "AF_DATAKIT"))
202 if (strEQ(name, "AF_DECnet"))
208 if (strEQ(name, "AF_DLI"))
214 if (strEQ(name, "AF_ECMA"))
220 if (strEQ(name, "AF_GOSIP"))
226 if (strEQ(name, "AF_HYLINK"))
232 if (strEQ(name, "AF_IMPLINK"))
238 if (strEQ(name, "AF_INET"))
244 if (strEQ(name, "AF_LAT"))
250 if (strEQ(name, "AF_MAX"))
256 if (strEQ(name, "AF_NBS"))
262 if (strEQ(name, "AF_NIT"))
268 if (strEQ(name, "AF_NS"))
274 if (strEQ(name, "AF_OSI"))
280 if (strEQ(name, "AF_OSINET"))
286 if (strEQ(name, "AF_PUP"))
292 if (strEQ(name, "AF_SNA"))
298 if (strEQ(name, "AF_UNIX"))
304 if (strEQ(name, "AF_UNSPEC"))
310 if (strEQ(name, "AF_X25"))
332 if (strEQ(name, "IOV_MAX"))
338 if (strEQ(name, "IPPROTO_TCP"))
352 if (strEQ(name, "MSG_BCAST"))
358 if (strEQ(name, "MSG_CTLFLAGS"))
364 if (strEQ(name, "MSG_CTLIGNORE"))
366 return MSG_CTLIGNORE;
370 if (strEQ(name, "MSG_CTRUNC"))
371 #if defined(MSG_TRUNC) || defined(HAS_MSG_CTRUNC) /* might be an enum */
376 if (strEQ(name, "MSG_DONTROUTE"))
377 #if defined(MSG_DONTROUTE) || defined(HAS_MSG_DONTROUTE) /* might be an enum */
378 return MSG_DONTROUTE;
382 if (strEQ(name, "MSG_DONTWAIT"))
388 if (strEQ(name, "MSG_EOF"))
394 if (strEQ(name, "MSG_EOR"))
400 if (strEQ(name, "MSG_ERRQUEUE"))
406 if (strEQ(name, "MSG_FIN"))
412 if (strEQ(name, "MSG_MAXIOVLEN"))
414 return MSG_MAXIOVLEN;
418 if (strEQ(name, "MSG_MCAST"))
424 if (strEQ(name, "MSG_NOSIGNAL"))
430 if (strEQ(name, "MSG_OOB"))
431 #if defined(MSG_OOB) || defined(HAS_MSG_OOB) /* might be an enum */
436 if (strEQ(name, "MSG_PEEK"))
437 #if defined(MSG_PEEK) || defined(HAS_MSG_PEEK) /* might be an enum */
442 if (strEQ(name, "MSG_PROXY"))
443 #if defined(MSG_PROXY) || defined(HAS_MSG_PROXY) /* might be an enum */
448 if (strEQ(name, "MSG_RST"))
454 if (strEQ(name, "MSG_SYN"))
460 if (strEQ(name, "MSG_TRUNC"))
466 if (strEQ(name, "MSG_WAITALL"))
478 if (strEQ(name, "PF_802"))
484 if (strEQ(name, "PF_APPLETALK"))
490 if (strEQ(name, "PF_CCITT"))
496 if (strEQ(name, "PF_CHAOS"))
502 if (strEQ(name, "PF_DATAKIT"))
508 if (strEQ(name, "PF_DECnet"))
514 if (strEQ(name, "PF_DLI"))
520 if (strEQ(name, "PF_ECMA"))
526 if (strEQ(name, "PF_GOSIP"))
532 if (strEQ(name, "PF_HYLINK"))
538 if (strEQ(name, "PF_IMPLINK"))
544 if (strEQ(name, "PF_INET"))
550 if (strEQ(name, "PF_LAT"))
556 if (strEQ(name, "PF_MAX"))
562 if (strEQ(name, "PF_NBS"))
568 if (strEQ(name, "PF_NIT"))
574 if (strEQ(name, "PF_NS"))
580 if (strEQ(name, "PF_OSI"))
586 if (strEQ(name, "PF_OSINET"))
592 if (strEQ(name, "PF_PUP"))
598 if (strEQ(name, "PF_SNA"))
604 if (strEQ(name, "PF_UNIX"))
610 if (strEQ(name, "PF_UNSPEC"))
616 if (strEQ(name, "PF_X25"))
628 if (strEQ(name, "SCM_CONNECT"))
634 if (strEQ(name, "SCM_CREDENTIALS"))
635 #ifdef SCM_CREDENTIALS
636 return SCM_CREDENTIALS;
640 if (strEQ(name, "SCM_CREDS"))
646 if (strEQ(name, "SCM_RIGHTS"))
647 #if defined(SCM_RIGHTS) || defined(HAS_SCM_RIGHTS) /* might be an enum */
652 if (strEQ(name, "SCM_TIMESTAMP"))
654 return SCM_TIMESTAMP;
658 if (strEQ(name, "SOCK_DGRAM"))
664 if (strEQ(name, "SOCK_RAW"))
670 if (strEQ(name, "SOCK_RDM"))
676 if (strEQ(name, "SOCK_SEQPACKET"))
677 #ifdef SOCK_SEQPACKET
678 return SOCK_SEQPACKET;
682 if (strEQ(name, "SOCK_STREAM"))
688 if (strEQ(name, "SOL_SOCKET"))
694 if (strEQ(name, "SOMAXCONN"))
700 if (strEQ(name, "SO_ACCEPTCONN"))
702 return SO_ACCEPTCONN;
706 if (strEQ(name, "SO_BROADCAST"))
712 if (strEQ(name, "SO_DEBUG"))
718 if (strEQ(name, "SO_DONTLINGER"))
720 return SO_DONTLINGER;
724 if (strEQ(name, "SO_DONTROUTE"))
730 if (strEQ(name, "SO_ERROR"))
736 if (strEQ(name, "SO_KEEPALIVE"))
742 if (strEQ(name, "SO_LINGER"))
748 if (strEQ(name, "SO_OOBINLINE"))
754 if (strEQ(name, "SO_RCVBUF"))
760 if (strEQ(name, "SO_RCVLOWAT"))
766 if (strEQ(name, "SO_RCVTIMEO"))
772 if (strEQ(name, "SO_REUSEADDR"))
778 if (strEQ(name, "SO_REUSEPORT"))
784 if (strEQ(name, "SO_SNDBUF"))
790 if (strEQ(name, "SO_SNDLOWAT"))
796 if (strEQ(name, "SO_SNDTIMEO"))
802 if (strEQ(name, "SO_TYPE"))
808 if (strEQ(name, "SO_USELOOPBACK"))
809 #ifdef SO_USELOOPBACK
810 return SO_USELOOPBACK;
816 if (strEQ(name, "TCP_KEEPALIVE"))
818 return TCP_KEEPALIVE;
822 if (strEQ(name, "TCP_MAXRT"))
828 if (strEQ(name, "TCP_MAXSEG"))
834 if (strEQ(name, "TCP_NODELAY"))
840 if (strEQ(name, "TCP_STDURG"))
848 if (strEQ(name, "UIO_MAXIOV"))
875 MODULE = Socket PACKAGE = Socket
888 struct in_addr ip_address;
889 struct hostent * phe;
890 int ok = inet_aton(host, &ip_address);
892 if (!ok && (phe = gethostbyname(host))) {
893 Copy( phe->h_addr, &ip_address, phe->h_length, char );
897 ST(0) = sv_newmortal();
899 sv_setpvn( ST(0), (char *)&ip_address, sizeof ip_address );
904 inet_ntoa(ip_address_sv)
911 char * ip_address = SvPV(ip_address_sv,addrlen);
912 if (addrlen != sizeof(addr)) {
913 croak("Bad arg length for %s, length is %d, should be %d",
915 addrlen, sizeof(addr));
918 Copy( ip_address, &addr, sizeof addr, char );
919 addr_str = inet_ntoa(addr);
921 ST(0) = sv_2mortal(newSVpvn(addr_str, strlen(addr_str)));
925 pack_sockaddr_un(pathname)
930 struct sockaddr_un sun_ad; /* fear using sun */
932 Zero( &sun_ad, sizeof sun_ad, char );
933 sun_ad.sun_family = AF_UNIX;
934 len = strlen(pathname);
935 if (len > sizeof(sun_ad.sun_path))
936 len = sizeof(sun_ad.sun_path);
937 Copy( pathname, sun_ad.sun_path, len, char );
938 ST(0) = sv_2mortal(newSVpvn((char *)&sun_ad, sizeof sun_ad));
940 ST(0) = (SV *) not_here("pack_sockaddr_un");
946 unpack_sockaddr_un(sun_sv)
951 struct sockaddr_un addr;
953 char * sun_ad = SvPV(sun_sv,sockaddrlen);
956 if (sockaddrlen != sizeof(addr)) {
957 croak("Bad arg length for %s, length is %d, should be %d",
958 "Socket::unpack_sockaddr_un",
959 sockaddrlen, sizeof(addr));
962 Copy( sun_ad, &addr, sizeof addr, char );
964 if ( addr.sun_family != AF_UNIX ) {
965 croak("Bad address family for %s, got %d, should be %d",
966 "Socket::unpack_sockaddr_un",
971 while (*e && e < addr.sun_path + sizeof addr.sun_path)
973 ST(0) = sv_2mortal(newSVpvn(addr.sun_path, e - addr.sun_path));
975 ST(0) = (SV *) not_here("unpack_sockaddr_un");
980 pack_sockaddr_in(port,ip_address)
985 struct sockaddr_in sin;
987 Zero( &sin, sizeof sin, char );
988 sin.sin_family = AF_INET;
989 sin.sin_port = htons(port);
990 Copy( ip_address, &sin.sin_addr, sizeof sin.sin_addr, char );
992 ST(0) = sv_2mortal(newSVpvn((char *)&sin, sizeof sin));
996 unpack_sockaddr_in(sin_sv)
1001 struct sockaddr_in addr;
1002 unsigned short port;
1003 struct in_addr ip_address;
1004 char * sin = SvPV(sin_sv,sockaddrlen);
1005 if (sockaddrlen != sizeof(addr)) {
1006 croak("Bad arg length for %s, length is %d, should be %d",
1007 "Socket::unpack_sockaddr_in",
1008 sockaddrlen, sizeof(addr));
1010 Copy( sin, &addr,sizeof addr, char );
1011 if ( addr.sin_family != AF_INET ) {
1012 croak("Bad address family for %s, got %d, should be %d",
1013 "Socket::unpack_sockaddr_in",
1017 port = ntohs(addr.sin_port);
1018 ip_address = addr.sin_addr;
1021 PUSHs(sv_2mortal(newSViv((IV) port)));
1022 PUSHs(sv_2mortal(newSVpvn((char *)&ip_address,sizeof ip_address)));
1029 struct in_addr ip_address;
1030 ip_address.s_addr = htonl(INADDR_ANY);
1031 ST(0) = sv_2mortal(newSVpvn((char *)&ip_address,sizeof ip_address ));
1038 struct in_addr ip_address;
1039 ip_address.s_addr = htonl(INADDR_LOOPBACK);
1040 ST(0) = sv_2mortal(newSVpvn((char *)&ip_address,sizeof ip_address));
1047 struct in_addr ip_address;
1048 ip_address.s_addr = htonl(INADDR_NONE);
1049 ST(0) = sv_2mortal(newSVpvn((char *)&ip_address,sizeof ip_address));
1056 struct in_addr ip_address;
1057 ip_address.s_addr = htonl(INADDR_BROADCAST);
1058 ST(0) = sv_2mortal(newSVpvn((char *)&ip_address,sizeof ip_address));