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"
42 #define INADDR_NONE 0xffffffff
43 #endif /* INADDR_NONE */
44 #ifndef INADDR_BROADCAST
45 #define INADDR_BROADCAST 0xffffffff
46 #endif /* INADDR_BROADCAST */
47 #ifndef INADDR_LOOPBACK
48 #define INADDR_LOOPBACK 0x7F000001
49 #endif /* INADDR_LOOPBACK */
54 * Check whether "cp" is a valid ascii representation
55 * of an Internet address and convert to a binary address.
56 * Returns 1 if the address is valid, 0 if not.
57 * This replaces inet_addr, the return value from which
58 * cannot distinguish between failure and a local broadcast address.
61 my_inet_aton(register const char *cp, struct in_addr *addr)
68 unsigned int parts[4];
69 register unsigned int *pp = parts;
75 * Collect number up to ``.''.
76 * Values are specified as for C:
77 * 0x=hex, 0=octal, other=decimal.
81 if (*++cp == 'x' || *cp == 'X')
86 while ((c = *cp) != '\0') {
88 val = (val * base) + (c - '0');
92 if (base == 16 && (s=strchr(PL_hexdigit,c))) {
94 ((s - PL_hexdigit) & 15);
104 * a.b.c (with c treated as 16-bits)
105 * a.b (with b treated as 24 bits)
107 if (pp >= parts + 3 || val > 0xff)
114 * Check for trailing characters.
116 if (*cp && !isSPACE(*cp))
119 * Concoct the address according to
120 * the number of parts specified.
122 nparts = pp - parts + 1; /* force to an int for switch() */
125 case 1: /* a -- 32 bits */
128 case 2: /* a.b -- 8.24 bits */
131 val |= parts[0] << 24;
134 case 3: /* a.b.c -- 8.8.16 bits */
137 val |= (parts[0] << 24) | (parts[1] << 16);
140 case 4: /* a.b.c.d -- 8.8.8.8 bits */
143 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
146 addr->s_addr = htonl(val);
151 #define inet_aton my_inet_aton
153 #endif /* ! HAS_INET_ATON */
159 croak("Socket::%s not implemented on this architecture", s);
164 constant(char *name, int arg)
169 if (strEQ(name, "AF_802"))
175 if (strEQ(name, "AF_APPLETALK"))
181 if (strEQ(name, "AF_CCITT"))
187 if (strEQ(name, "AF_CHAOS"))
193 if (strEQ(name, "AF_DATAKIT"))
199 if (strEQ(name, "AF_DECnet"))
205 if (strEQ(name, "AF_DLI"))
211 if (strEQ(name, "AF_ECMA"))
217 if (strEQ(name, "AF_GOSIP"))
223 if (strEQ(name, "AF_HYLINK"))
229 if (strEQ(name, "AF_IMPLINK"))
235 if (strEQ(name, "AF_INET"))
241 if (strEQ(name, "AF_LAT"))
247 if (strEQ(name, "AF_MAX"))
253 if (strEQ(name, "AF_NBS"))
259 if (strEQ(name, "AF_NIT"))
265 if (strEQ(name, "AF_NS"))
271 if (strEQ(name, "AF_OSI"))
277 if (strEQ(name, "AF_OSINET"))
283 if (strEQ(name, "AF_PUP"))
289 if (strEQ(name, "AF_SNA"))
295 if (strEQ(name, "AF_UNIX"))
301 if (strEQ(name, "AF_UNSPEC"))
307 if (strEQ(name, "AF_X25"))
329 if (strEQ(name, "IOV_MAX"))
343 if (strEQ(name, "MSG_BCAST"))
349 if (strEQ(name, "MSG_CTLFLAGS"))
355 if (strEQ(name, "MSG_CTLIGNORE"))
357 return MSG_CTLIGNORE;
361 if (strEQ(name, "MSG_CTRUNC"))
362 #if defined(MSG_TRUNC) || defined(HAS_MSG_CTRUNC) /* might be an enum */
367 if (strEQ(name, "MSG_DONTROUTE"))
368 #if defined(MSG_DONTROUTE) || defined(HAS_MSG_DONTROUTE) /* might be an enum */
369 return MSG_DONTROUTE;
373 if (strEQ(name, "MSG_DONTWAIT"))
379 if (strEQ(name, "MSG_EOF"))
385 if (strEQ(name, "MSG_EOR"))
391 if (strEQ(name, "MSG_ERRQUEUE"))
397 if (strEQ(name, "MSG_FIN"))
403 if (strEQ(name, "MSG_MAXIOVLEN"))
405 return MSG_MAXIOVLEN;
409 if (strEQ(name, "MSG_MCAST"))
415 if (strEQ(name, "MSG_NOSIGNAL"))
421 if (strEQ(name, "MSG_OOB"))
422 #if defined(MSG_OOB) || defined(HAS_MSG_OOB) /* might be an enum */
427 if (strEQ(name, "MSG_PEEK"))
428 #if defined(MSG_PEEK) || defined(HAS_MSG_PEEK) /* might be an enum */
433 if (strEQ(name, "MSG_PROXY"))
434 #if defined(MSG_PROXY) || defined(HAS_MSG_PROXY) /* might be an enum */
439 if (strEQ(name, "MSG_RST"))
445 if (strEQ(name, "MSG_SYN"))
451 if (strEQ(name, "MSG_TRUNC"))
457 if (strEQ(name, "MSG_WAITALL"))
469 if (strEQ(name, "PF_802"))
475 if (strEQ(name, "PF_APPLETALK"))
481 if (strEQ(name, "PF_CCITT"))
487 if (strEQ(name, "PF_CHAOS"))
493 if (strEQ(name, "PF_DATAKIT"))
499 if (strEQ(name, "PF_DECnet"))
505 if (strEQ(name, "PF_DLI"))
511 if (strEQ(name, "PF_ECMA"))
517 if (strEQ(name, "PF_GOSIP"))
523 if (strEQ(name, "PF_HYLINK"))
529 if (strEQ(name, "PF_IMPLINK"))
535 if (strEQ(name, "PF_INET"))
541 if (strEQ(name, "PF_LAT"))
547 if (strEQ(name, "PF_MAX"))
553 if (strEQ(name, "PF_NBS"))
559 if (strEQ(name, "PF_NIT"))
565 if (strEQ(name, "PF_NS"))
571 if (strEQ(name, "PF_OSI"))
577 if (strEQ(name, "PF_OSINET"))
583 if (strEQ(name, "PF_PUP"))
589 if (strEQ(name, "PF_SNA"))
595 if (strEQ(name, "PF_UNIX"))
601 if (strEQ(name, "PF_UNSPEC"))
607 if (strEQ(name, "PF_X25"))
619 if (strEQ(name, "SCM_CONNECT"))
625 if (strEQ(name, "SCM_CREDENTIALS"))
626 #ifdef SCM_CREDENTIALS
627 return SCM_CREDENTIALS;
631 if (strEQ(name, "SCM_CREDS"))
637 if (strEQ(name, "SCM_RIGHTS"))
638 #if defined(SCM_RIGHTS) || defined(HAS_SCM_RIGHTS) /* might be an enum */
643 if (strEQ(name, "SCM_TIMESTAMP"))
645 return SCM_TIMESTAMP;
649 if (strEQ(name, "SOCK_DGRAM"))
655 if (strEQ(name, "SOCK_RAW"))
661 if (strEQ(name, "SOCK_RDM"))
667 if (strEQ(name, "SOCK_SEQPACKET"))
668 #ifdef SOCK_SEQPACKET
669 return SOCK_SEQPACKET;
673 if (strEQ(name, "SOCK_STREAM"))
679 if (strEQ(name, "SOL_SOCKET"))
685 if (strEQ(name, "SOMAXCONN"))
691 if (strEQ(name, "SO_ACCEPTCONN"))
693 return SO_ACCEPTCONN;
697 if (strEQ(name, "SO_BROADCAST"))
703 if (strEQ(name, "SO_DEBUG"))
709 if (strEQ(name, "SO_DONTLINGER"))
711 return SO_DONTLINGER;
715 if (strEQ(name, "SO_DONTROUTE"))
721 if (strEQ(name, "SO_ERROR"))
727 if (strEQ(name, "SO_KEEPALIVE"))
733 if (strEQ(name, "SO_LINGER"))
739 if (strEQ(name, "SO_OOBINLINE"))
745 if (strEQ(name, "SO_RCVBUF"))
751 if (strEQ(name, "SO_RCVLOWAT"))
757 if (strEQ(name, "SO_RCVTIMEO"))
763 if (strEQ(name, "SO_REUSEADDR"))
769 if (strEQ(name, "SO_REUSEPORT"))
775 if (strEQ(name, "SO_SNDBUF"))
781 if (strEQ(name, "SO_SNDLOWAT"))
787 if (strEQ(name, "SO_SNDTIMEO"))
793 if (strEQ(name, "SO_TYPE"))
799 if (strEQ(name, "SO_USELOOPBACK"))
800 #ifdef SO_USELOOPBACK
801 return SO_USELOOPBACK;
809 if (strEQ(name, "UIO_MAXIOV"))
836 MODULE = Socket PACKAGE = Socket
849 struct in_addr ip_address;
850 struct hostent * phe;
851 int ok = inet_aton(host, &ip_address);
853 if (!ok && (phe = gethostbyname(host))) {
854 Copy( phe->h_addr, &ip_address, phe->h_length, char );
858 ST(0) = sv_newmortal();
860 sv_setpvn( ST(0), (char *)&ip_address, sizeof ip_address );
865 inet_ntoa(ip_address_sv)
872 char * ip_address = SvPV(ip_address_sv,addrlen);
873 if (addrlen != sizeof(addr)) {
874 croak("Bad arg length for %s, length is %d, should be %d",
876 addrlen, sizeof(addr));
879 Copy( ip_address, &addr, sizeof addr, char );
880 addr_str = inet_ntoa(addr);
882 ST(0) = sv_2mortal(newSVpv(addr_str, strlen(addr_str)));
886 pack_sockaddr_un(pathname)
891 struct sockaddr_un sun_ad; /* fear using sun */
893 Zero( &sun_ad, sizeof sun_ad, char );
894 sun_ad.sun_family = AF_UNIX;
895 len = strlen(pathname);
896 if (len > sizeof(sun_ad.sun_path))
897 len = sizeof(sun_ad.sun_path);
898 Copy( pathname, sun_ad.sun_path, len, char );
899 ST(0) = sv_2mortal(newSVpv((char *)&sun_ad, sizeof sun_ad));
901 ST(0) = (SV *) not_here("pack_sockaddr_un");
907 unpack_sockaddr_un(sun_sv)
912 struct sockaddr_un addr;
914 char * sun_ad = SvPV(sun_sv,sockaddrlen);
917 if (sockaddrlen != sizeof(addr)) {
918 croak("Bad arg length for %s, length is %d, should be %d",
919 "Socket::unpack_sockaddr_un",
920 sockaddrlen, sizeof(addr));
923 Copy( sun_ad, &addr, sizeof addr, char );
925 if ( addr.sun_family != AF_UNIX ) {
926 croak("Bad address family for %s, got %d, should be %d",
927 "Socket::unpack_sockaddr_un",
932 while (*e && e < addr.sun_path + sizeof addr.sun_path)
934 ST(0) = sv_2mortal(newSVpv(addr.sun_path, e - addr.sun_path));
936 ST(0) = (SV *) not_here("unpack_sockaddr_un");
941 pack_sockaddr_in(port,ip_address)
946 struct sockaddr_in sin;
948 Zero( &sin, sizeof sin, char );
949 sin.sin_family = AF_INET;
950 sin.sin_port = htons(port);
951 Copy( ip_address, &sin.sin_addr, sizeof sin.sin_addr, char );
953 ST(0) = sv_2mortal(newSVpv((char *)&sin, sizeof sin));
957 unpack_sockaddr_in(sin_sv)
962 struct sockaddr_in addr;
964 struct in_addr ip_address;
965 char * sin = SvPV(sin_sv,sockaddrlen);
966 if (sockaddrlen != sizeof(addr)) {
967 croak("Bad arg length for %s, length is %d, should be %d",
968 "Socket::unpack_sockaddr_in",
969 sockaddrlen, sizeof(addr));
971 Copy( sin, &addr,sizeof addr, char );
972 if ( addr.sin_family != AF_INET ) {
973 croak("Bad address family for %s, got %d, should be %d",
974 "Socket::unpack_sockaddr_in",
978 port = ntohs(addr.sin_port);
979 ip_address = addr.sin_addr;
982 PUSHs(sv_2mortal(newSViv((IV) port)));
983 PUSHs(sv_2mortal(newSVpv((char *)&ip_address,sizeof ip_address)));
990 struct in_addr ip_address;
991 ip_address.s_addr = htonl(INADDR_ANY);
992 ST(0) = sv_2mortal(newSVpv((char *)&ip_address,sizeof ip_address ));
999 struct in_addr ip_address;
1000 ip_address.s_addr = htonl(INADDR_LOOPBACK);
1001 ST(0) = sv_2mortal(newSVpv((char *)&ip_address,sizeof ip_address));
1008 struct in_addr ip_address;
1009 ip_address.s_addr = htonl(INADDR_NONE);
1010 ST(0) = sv_2mortal(newSVpv((char *)&ip_address,sizeof ip_address));
1017 struct in_addr ip_address;
1018 ip_address.s_addr = htonl(INADDR_BROADCAST);
1019 ST(0) = sv_2mortal(newSVpv((char *)&ip_address,sizeof ip_address));