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 "sockadapt.h"
29 #ifndef WIN32 /* I_NETINET_TCP */
30 #include <netinet/tcp.h>
46 #define INADDR_NONE 0xffffffff
47 #endif /* INADDR_NONE */
48 #ifndef INADDR_BROADCAST
49 #define INADDR_BROADCAST 0xffffffff
50 #endif /* INADDR_BROADCAST */
51 #ifndef INADDR_LOOPBACK
52 #define INADDR_LOOPBACK 0x7F000001
53 #endif /* INADDR_LOOPBACK */
58 * Check whether "cp" is a valid ascii representation
59 * of an Internet address and convert to a binary address.
60 * Returns 1 if the address is valid, 0 if not.
61 * This replaces inet_addr, the return value from which
62 * cannot distinguish between failure and a local broadcast address.
65 my_inet_aton(register const char *cp, struct in_addr *addr)
72 unsigned int parts[4];
73 register unsigned int *pp = parts;
79 * Collect number up to ``.''.
80 * Values are specified as for C:
81 * 0x=hex, 0=octal, other=decimal.
85 if (*++cp == 'x' || *cp == 'X')
90 while ((c = *cp) != '\0') {
92 val = (val * base) + (c - '0');
96 if (base == 16 && (s=strchr(PL_hexdigit,c))) {
98 ((s - PL_hexdigit) & 15);
108 * a.b.c (with c treated as 16-bits)
109 * a.b (with b treated as 24 bits)
111 if (pp >= parts + 3 || val > 0xff)
118 * Check for trailing characters.
120 if (*cp && !isSPACE(*cp))
123 * Concoct the address according to
124 * the number of parts specified.
126 nparts = pp - parts + 1; /* force to an int for switch() */
129 case 1: /* a -- 32 bits */
132 case 2: /* a.b -- 8.24 bits */
135 val |= parts[0] << 24;
138 case 3: /* a.b.c -- 8.8.16 bits */
141 val |= (parts[0] << 24) | (parts[1] << 16);
144 case 4: /* a.b.c.d -- 8.8.8.8 bits */
147 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
150 addr->s_addr = htonl(val);
155 #define inet_aton my_inet_aton
157 #endif /* ! HAS_INET_ATON */
163 croak("Socket::%s not implemented on this architecture", s);
168 constant(char *name, int arg)
173 if (strEQ(name, "AF_802"))
179 if (strEQ(name, "AF_APPLETALK"))
185 if (strEQ(name, "AF_CCITT"))
191 if (strEQ(name, "AF_CHAOS"))
197 if (strEQ(name, "AF_DATAKIT"))
203 if (strEQ(name, "AF_DECnet"))
209 if (strEQ(name, "AF_DLI"))
215 if (strEQ(name, "AF_ECMA"))
221 if (strEQ(name, "AF_GOSIP"))
227 if (strEQ(name, "AF_HYLINK"))
233 if (strEQ(name, "AF_IMPLINK"))
239 if (strEQ(name, "AF_INET"))
245 if (strEQ(name, "AF_LAT"))
251 if (strEQ(name, "AF_MAX"))
257 if (strEQ(name, "AF_NBS"))
263 if (strEQ(name, "AF_NIT"))
269 if (strEQ(name, "AF_NS"))
275 if (strEQ(name, "AF_OSI"))
281 if (strEQ(name, "AF_OSINET"))
287 if (strEQ(name, "AF_PUP"))
293 if (strEQ(name, "AF_SNA"))
299 if (strEQ(name, "AF_UNIX"))
305 if (strEQ(name, "AF_UNSPEC"))
311 if (strEQ(name, "AF_X25"))
333 if (strEQ(name, "IOV_MAX"))
339 if (strEQ(name, "IPPROTO_TCP"))
353 if (strEQ(name, "MSG_BCAST"))
359 if (strEQ(name, "MSG_CTLFLAGS"))
365 if (strEQ(name, "MSG_CTLIGNORE"))
367 return MSG_CTLIGNORE;
371 if (strEQ(name, "MSG_CTRUNC"))
372 #if defined(MSG_TRUNC) || defined(HAS_MSG_CTRUNC) /* might be an enum */
377 if (strEQ(name, "MSG_DONTROUTE"))
378 #if defined(MSG_DONTROUTE) || defined(HAS_MSG_DONTROUTE) /* might be an enum */
379 return MSG_DONTROUTE;
383 if (strEQ(name, "MSG_DONTWAIT"))
389 if (strEQ(name, "MSG_EOF"))
395 if (strEQ(name, "MSG_EOR"))
401 if (strEQ(name, "MSG_ERRQUEUE"))
407 if (strEQ(name, "MSG_FIN"))
413 if (strEQ(name, "MSG_MAXIOVLEN"))
415 return MSG_MAXIOVLEN;
419 if (strEQ(name, "MSG_MCAST"))
425 if (strEQ(name, "MSG_NOSIGNAL"))
431 if (strEQ(name, "MSG_OOB"))
432 #if defined(MSG_OOB) || defined(HAS_MSG_OOB) /* might be an enum */
437 if (strEQ(name, "MSG_PEEK"))
438 #if defined(MSG_PEEK) || defined(HAS_MSG_PEEK) /* might be an enum */
443 if (strEQ(name, "MSG_PROXY"))
444 #if defined(MSG_PROXY) || defined(HAS_MSG_PROXY) /* might be an enum */
449 if (strEQ(name, "MSG_RST"))
455 if (strEQ(name, "MSG_SYN"))
461 if (strEQ(name, "MSG_TRUNC"))
467 if (strEQ(name, "MSG_WAITALL"))
479 if (strEQ(name, "PF_802"))
485 if (strEQ(name, "PF_APPLETALK"))
491 if (strEQ(name, "PF_CCITT"))
497 if (strEQ(name, "PF_CHAOS"))
503 if (strEQ(name, "PF_DATAKIT"))
509 if (strEQ(name, "PF_DECnet"))
515 if (strEQ(name, "PF_DLI"))
521 if (strEQ(name, "PF_ECMA"))
527 if (strEQ(name, "PF_GOSIP"))
533 if (strEQ(name, "PF_HYLINK"))
539 if (strEQ(name, "PF_IMPLINK"))
545 if (strEQ(name, "PF_INET"))
551 if (strEQ(name, "PF_LAT"))
557 if (strEQ(name, "PF_MAX"))
563 if (strEQ(name, "PF_NBS"))
569 if (strEQ(name, "PF_NIT"))
575 if (strEQ(name, "PF_NS"))
581 if (strEQ(name, "PF_OSI"))
587 if (strEQ(name, "PF_OSINET"))
593 if (strEQ(name, "PF_PUP"))
599 if (strEQ(name, "PF_SNA"))
605 if (strEQ(name, "PF_UNIX"))
611 if (strEQ(name, "PF_UNSPEC"))
617 if (strEQ(name, "PF_X25"))
629 if (strEQ(name, "SCM_CONNECT"))
635 if (strEQ(name, "SCM_CREDENTIALS"))
636 #ifdef SCM_CREDENTIALS
637 return SCM_CREDENTIALS;
641 if (strEQ(name, "SCM_CREDS"))
647 if (strEQ(name, "SCM_RIGHTS"))
648 #if defined(SCM_RIGHTS) || defined(HAS_SCM_RIGHTS) /* might be an enum */
653 if (strEQ(name, "SCM_TIMESTAMP"))
655 return SCM_TIMESTAMP;
659 if (strEQ(name, "SOCK_DGRAM"))
665 if (strEQ(name, "SOCK_RAW"))
671 if (strEQ(name, "SOCK_RDM"))
677 if (strEQ(name, "SOCK_SEQPACKET"))
678 #ifdef SOCK_SEQPACKET
679 return SOCK_SEQPACKET;
683 if (strEQ(name, "SOCK_STREAM"))
689 if (strEQ(name, "SOL_SOCKET"))
695 if (strEQ(name, "SOMAXCONN"))
701 if (strEQ(name, "SO_ACCEPTCONN"))
703 return SO_ACCEPTCONN;
707 if (strEQ(name, "SO_BROADCAST"))
713 if (strEQ(name, "SO_DEBUG"))
719 if (strEQ(name, "SO_DONTLINGER"))
721 return SO_DONTLINGER;
725 if (strEQ(name, "SO_DONTROUTE"))
731 if (strEQ(name, "SO_ERROR"))
737 if (strEQ(name, "SO_KEEPALIVE"))
743 if (strEQ(name, "SO_LINGER"))
749 if (strEQ(name, "SO_OOBINLINE"))
755 if (strEQ(name, "SO_RCVBUF"))
761 if (strEQ(name, "SO_RCVLOWAT"))
767 if (strEQ(name, "SO_RCVTIMEO"))
773 if (strEQ(name, "SO_REUSEADDR"))
779 if (strEQ(name, "SO_REUSEPORT"))
785 if (strEQ(name, "SO_SNDBUF"))
791 if (strEQ(name, "SO_SNDLOWAT"))
797 if (strEQ(name, "SO_SNDTIMEO"))
803 if (strEQ(name, "SO_TYPE"))
809 if (strEQ(name, "SO_USELOOPBACK"))
810 #ifdef SO_USELOOPBACK
811 return SO_USELOOPBACK;
817 if (strEQ(name, "TCP_KEEPALIVE"))
819 return TCP_KEEPALIVE;
823 if (strEQ(name, "TCP_MAXRT"))
829 if (strEQ(name, "TCP_MAXSEG"))
835 if (strEQ(name, "TCP_NODELAY"))
841 if (strEQ(name, "TCP_STDURG"))
849 if (strEQ(name, "UIO_MAXIOV"))
876 MODULE = Socket PACKAGE = Socket
889 struct in_addr ip_address;
890 struct hostent * phe;
891 int ok = inet_aton(host, &ip_address);
893 if (!ok && (phe = gethostbyname(host))) {
894 Copy( phe->h_addr, &ip_address, phe->h_length, char );
898 ST(0) = sv_newmortal();
900 sv_setpvn( ST(0), (char *)&ip_address, sizeof ip_address );
905 inet_ntoa(ip_address_sv)
912 char * ip_address = SvPV(ip_address_sv,addrlen);
913 if (addrlen != sizeof(addr)) {
914 croak("Bad arg length for %s, length is %d, should be %d",
916 addrlen, sizeof(addr));
919 Copy( ip_address, &addr, sizeof addr, char );
920 addr_str = inet_ntoa(addr);
922 ST(0) = sv_2mortal(newSVpvn(addr_str, strlen(addr_str)));
926 pack_sockaddr_un(pathname)
931 struct sockaddr_un sun_ad; /* fear using sun */
933 Zero( &sun_ad, sizeof sun_ad, char );
934 sun_ad.sun_family = AF_UNIX;
935 len = strlen(pathname);
936 if (len > sizeof(sun_ad.sun_path))
937 len = sizeof(sun_ad.sun_path);
938 Copy( pathname, sun_ad.sun_path, len, char );
939 ST(0) = sv_2mortal(newSVpvn((char *)&sun_ad, sizeof sun_ad));
941 ST(0) = (SV *) not_here("pack_sockaddr_un");
947 unpack_sockaddr_un(sun_sv)
952 struct sockaddr_un addr;
954 char * sun_ad = SvPV(sun_sv,sockaddrlen);
957 if (sockaddrlen != sizeof(addr)) {
958 croak("Bad arg length for %s, length is %d, should be %d",
959 "Socket::unpack_sockaddr_un",
960 sockaddrlen, sizeof(addr));
963 Copy( sun_ad, &addr, sizeof addr, char );
965 if ( addr.sun_family != AF_UNIX ) {
966 croak("Bad address family for %s, got %d, should be %d",
967 "Socket::unpack_sockaddr_un",
972 while (*e && e < addr.sun_path + sizeof addr.sun_path)
974 ST(0) = sv_2mortal(newSVpvn(addr.sun_path, e - addr.sun_path));
976 ST(0) = (SV *) not_here("unpack_sockaddr_un");
981 pack_sockaddr_in(port,ip_address)
986 struct sockaddr_in sin;
988 Zero( &sin, sizeof sin, char );
989 sin.sin_family = AF_INET;
990 sin.sin_port = htons(port);
991 Copy( ip_address, &sin.sin_addr, sizeof sin.sin_addr, char );
993 ST(0) = sv_2mortal(newSVpvn((char *)&sin, sizeof sin));
997 unpack_sockaddr_in(sin_sv)
1002 struct sockaddr_in addr;
1003 unsigned short port;
1004 struct in_addr ip_address;
1005 char * sin = SvPV(sin_sv,sockaddrlen);
1006 if (sockaddrlen != sizeof(addr)) {
1007 croak("Bad arg length for %s, length is %d, should be %d",
1008 "Socket::unpack_sockaddr_in",
1009 sockaddrlen, sizeof(addr));
1011 Copy( sin, &addr,sizeof addr, char );
1012 if ( addr.sin_family != AF_INET ) {
1013 croak("Bad address family for %s, got %d, should be %d",
1014 "Socket::unpack_sockaddr_in",
1018 port = ntohs(addr.sin_port);
1019 ip_address = addr.sin_addr;
1022 PUSHs(sv_2mortal(newSViv((IV) port)));
1023 PUSHs(sv_2mortal(newSVpvn((char *)&ip_address,sizeof ip_address)));
1030 struct in_addr ip_address;
1031 ip_address.s_addr = htonl(INADDR_ANY);
1032 ST(0) = sv_2mortal(newSVpvn((char *)&ip_address,sizeof ip_address ));
1039 struct in_addr ip_address;
1040 ip_address.s_addr = htonl(INADDR_LOOPBACK);
1041 ST(0) = sv_2mortal(newSVpvn((char *)&ip_address,sizeof ip_address));
1048 struct in_addr ip_address;
1049 ip_address.s_addr = htonl(INADDR_NONE);
1050 ST(0) = sv_2mortal(newSVpvn((char *)&ip_address,sizeof ip_address));
1057 struct in_addr ip_address;
1058 ip_address.s_addr = htonl(INADDR_BROADCAST);
1059 ST(0) = sv_2mortal(newSVpvn((char *)&ip_address,sizeof ip_address));