Socket.xs revision 7c478bd95313f5f23a4c958a745db2134aa03244
#define PERL_NO_GET_CONTEXT
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#ifndef VMS
# ifdef I_SYS_TYPES
# endif
# include <socks.h>
# endif
# ifdef MPE
# define SOCK_RAW 3
# endif
# ifdef I_SYS_UN
# endif
/* XXX Configure test for <netinet/in_systm.h needed XXX */
# include <netinet/in_systm.h>
# endif
# ifdef I_NETINET_IN
# endif
# ifdef I_NETDB
# include <netdb.h>
# endif
# ifdef I_ARPA_INET
# endif
# ifdef I_NETINET_TCP
# endif
#else
# include "sockadapt.h"
#endif
#ifdef I_SYSUIO
#endif
#ifndef AF_NBS
#endif
#ifndef AF_X25
#endif
#ifndef INADDR_NONE
# define INADDR_NONE 0xffffffff
#endif /* INADDR_NONE */
#ifndef INADDR_BROADCAST
# define INADDR_BROADCAST 0xffffffff
#endif /* INADDR_BROADCAST */
#ifndef INADDR_LOOPBACK
# define INADDR_LOOPBACK 0x7F000001
#endif /* INADDR_LOOPBACK */
#ifndef HAS_INET_ATON
/*
* Check whether "cp" is a valid ascii representation
* of an Internet address and convert to a binary address.
* Returns 1 if the address is valid, 0 if not.
* This replaces inet_addr, the return value from which
* cannot distinguish between failure and a local broadcast address.
*/
static int
{
dTHX;
register int base;
register char c;
int nparts;
const char *s;
unsigned int parts[4];
if (!cp)
return 0;
for (;;) {
/*
* Collect number up to ``.''.
* Values are specified as for C:
* 0x=hex, 0=octal, other=decimal.
*/
if (*cp == '0') {
else
base = 8;
}
while ((c = *cp) != '\0') {
if (isDIGIT(c)) {
cp++;
continue;
}
((s - PL_hexdigit) & 15);
cp++;
continue;
}
break;
}
if (*cp == '.') {
/*
* Internet format:
* a.b.c.d
* a.b.c (with c treated as 16-bits)
* a.b (with b treated as 24 bits)
*/
return 0;
} else
break;
}
/*
* Check for trailing characters.
*/
return 0;
/*
* Concoct the address according to
* the number of parts specified.
*/
switch (nparts) {
case 1: /* a -- 32 bits */
break;
case 2: /* a.b -- 8.24 bits */
if (val > 0xffffff)
return 0;
break;
case 3: /* a.b.c -- 8.8.16 bits */
if (val > 0xffff)
return 0;
break;
case 4: /* a.b.c.d -- 8.8.8.8 bits */
if (val > 0xff)
return 0;
break;
}
return 1;
}
#define inet_aton my_inet_aton
#endif /* ! HAS_INET_ATON */
static int
not_here(char *s)
{
croak("Socket::%s not implemented on this architecture", s);
return -1;
}
static double
{
errno = 0;
switch (*name) {
case 'A':
#ifdef AF_802
return AF_802;
#else
goto not_there;
#endif
#ifdef AF_APPLETALK
return AF_APPLETALK;
#else
goto not_there;
#endif
#ifdef AF_CCITT
return AF_CCITT;
#else
goto not_there;
#endif
#ifdef AF_CHAOS
return AF_CHAOS;
#else
goto not_there;
#endif
#ifdef AF_DATAKIT
return AF_DATAKIT;
#else
goto not_there;
#endif
#ifdef AF_DECnet
return AF_DECnet;
#else
goto not_there;
#endif
#ifdef AF_DLI
return AF_DLI;
#else
goto not_there;
#endif
#ifdef AF_ECMA
return AF_ECMA;
#else
goto not_there;
#endif
#ifdef AF_GOSIP
return AF_GOSIP;
#else
goto not_there;
#endif
#ifdef AF_HYLINK
return AF_HYLINK;
#else
goto not_there;
#endif
#ifdef AF_IMPLINK
return AF_IMPLINK;
#else
goto not_there;
#endif
#ifdef AF_INET
return AF_INET;
#else
goto not_there;
#endif
#ifdef AF_LAT
return AF_LAT;
#else
goto not_there;
#endif
#ifdef AF_MAX
return AF_MAX;
#else
goto not_there;
#endif
#ifdef AF_NBS
return AF_NBS;
#else
goto not_there;
#endif
#ifdef AF_NIT
return AF_NIT;
#else
goto not_there;
#endif
#ifdef AF_NS
return AF_NS;
#else
goto not_there;
#endif
#ifdef AF_OSI
return AF_OSI;
#else
goto not_there;
#endif
#ifdef AF_OSINET
return AF_OSINET;
#else
goto not_there;
#endif
#ifdef AF_PUP
return AF_PUP;
#else
goto not_there;
#endif
#ifdef AF_SNA
return AF_SNA;
#else
goto not_there;
#endif
#ifdef AF_UNIX
return AF_UNIX;
#else
goto not_there;
#endif
#ifdef AF_UNSPEC
return AF_UNSPEC;
#else
goto not_there;
#endif
#ifdef AF_X25
return AF_X25;
#else
goto not_there;
#endif
break;
case 'B':
break;
case 'C':
break;
case 'D':
break;
case 'E':
break;
case 'F':
break;
case 'G':
break;
case 'H':
break;
case 'I':
#ifdef IOV_MAX
return IOV_MAX;
#else
goto not_there;
#endif
#ifdef IPPROTO_TCP
return IPPROTO_TCP;
#else
goto not_there;
#endif
break;
case 'J':
break;
case 'K':
break;
case 'L':
break;
case 'M':
#ifdef MSG_BCAST
return MSG_BCAST;
#else
goto not_there;
#endif
#ifdef MSG_CTLFLAGS
return MSG_CTLFLAGS;
#else
goto not_there;
#endif
#ifdef MSG_CTLIGNORE
return MSG_CTLIGNORE;
#else
goto not_there;
#endif
return MSG_CTRUNC;
#else
goto not_there;
#endif
return MSG_DONTROUTE;
#else
goto not_there;
#endif
#ifdef MSG_DONTWAIT
return MSG_DONTWAIT;
#else
goto not_there;
#endif
#ifdef MSG_EOF
return MSG_EOF;
#else
goto not_there;
#endif
#ifdef MSG_EOR
return MSG_EOR;
#else
goto not_there;
#endif
#ifdef MSG_ERRQUEUE
return MSG_ERRQUEUE;
#else
goto not_there;
#endif
#ifdef MSG_FIN
return MSG_FIN;
#else
goto not_there;
#endif
#ifdef MSG_MAXIOVLEN
return MSG_MAXIOVLEN;
#else
goto not_there;
#endif
#ifdef MSG_MCAST
return MSG_MCAST;
#else
goto not_there;
#endif
#ifdef MSG_NOSIGNAL
return MSG_NOSIGNAL;
#else
goto not_there;
#endif
return MSG_OOB;
#else
goto not_there;
#endif
return MSG_PEEK;
#else
goto not_there;
#endif
return MSG_PROXY;
#else
goto not_there;
#endif
#ifdef MSG_RST
return MSG_RST;
#else
goto not_there;
#endif
#ifdef MSG_SYN
return MSG_SYN;
#else
goto not_there;
#endif
#ifdef MSG_TRUNC
return MSG_TRUNC;
#else
goto not_there;
#endif
#ifdef MSG_WAITALL
return MSG_WAITALL;
#else
goto not_there;
#endif
break;
case 'N':
break;
case 'O':
break;
case 'P':
#ifdef PF_802
return PF_802;
#else
goto not_there;
#endif
#ifdef PF_APPLETALK
return PF_APPLETALK;
#else
goto not_there;
#endif
#ifdef PF_CCITT
return PF_CCITT;
#else
goto not_there;
#endif
#ifdef PF_CHAOS
return PF_CHAOS;
#else
goto not_there;
#endif
#ifdef PF_DATAKIT
return PF_DATAKIT;
#else
goto not_there;
#endif
#ifdef PF_DECnet
return PF_DECnet;
#else
goto not_there;
#endif
#ifdef PF_DLI
return PF_DLI;
#else
goto not_there;
#endif
#ifdef PF_ECMA
return PF_ECMA;
#else
goto not_there;
#endif
#ifdef PF_GOSIP
return PF_GOSIP;
#else
goto not_there;
#endif
#ifdef PF_HYLINK
return PF_HYLINK;
#else
goto not_there;
#endif
#ifdef PF_IMPLINK
return PF_IMPLINK;
#else
goto not_there;
#endif
#ifdef PF_INET
return PF_INET;
#else
goto not_there;
#endif
#ifdef PF_LAT
return PF_LAT;
#else
goto not_there;
#endif
#ifdef PF_MAX
return PF_MAX;
#else
goto not_there;
#endif
#ifdef PF_NBS
return PF_NBS;
#else
goto not_there;
#endif
#ifdef PF_NIT
return PF_NIT;
#else
goto not_there;
#endif
#ifdef PF_NS
return PF_NS;
#else
goto not_there;
#endif
#ifdef PF_OSI
return PF_OSI;
#else
goto not_there;
#endif
#ifdef PF_OSINET
return PF_OSINET;
#else
goto not_there;
#endif
#ifdef PF_PUP
return PF_PUP;
#else
goto not_there;
#endif
#ifdef PF_SNA
return PF_SNA;
#else
goto not_there;
#endif
#ifdef PF_UNIX
return PF_UNIX;
#else
goto not_there;
#endif
#ifdef PF_UNSPEC
return PF_UNSPEC;
#else
goto not_there;
#endif
#ifdef PF_X25
return PF_X25;
#else
goto not_there;
#endif
break;
case 'Q':
break;
case 'R':
break;
case 'S':
#ifdef SCM_CONNECT
return SCM_CONNECT;
#else
goto not_there;
#endif
#ifdef SCM_CREDENTIALS
return SCM_CREDENTIALS;
#else
goto not_there;
#endif
#ifdef SCM_CREDS
return SCM_CREDS;
#else
goto not_there;
#endif
return SCM_RIGHTS;
#else
goto not_there;
#endif
#ifdef SCM_TIMESTAMP
return SCM_TIMESTAMP;
#else
goto not_there;
#endif
#ifdef SHUT_RD
return SHUT_RD;
#else
return 0;
#endif
#ifdef SHUT_RDWR
return SHUT_RDWR;
#else
return 2;
#endif
#ifdef SHUT_WR
return SHUT_WR;
#else
return 1;
#endif
#ifdef SOCK_DGRAM
return SOCK_DGRAM;
#else
goto not_there;
#endif
#ifdef SOCK_RAW
return SOCK_RAW;
#else
goto not_there;
#endif
#ifdef SOCK_RDM
return SOCK_RDM;
#else
goto not_there;
#endif
#ifdef SOCK_SEQPACKET
return SOCK_SEQPACKET;
#else
goto not_there;
#endif
#ifdef SOCK_STREAM
return SOCK_STREAM;
#else
goto not_there;
#endif
#ifdef SOL_SOCKET
return SOL_SOCKET;
#else
goto not_there;
#endif
#ifdef SOMAXCONN
return SOMAXCONN;
#else
goto not_there;
#endif
#ifdef SO_ACCEPTCONN
return SO_ACCEPTCONN;
#else
goto not_there;
#endif
#ifdef SO_BROADCAST
return SO_BROADCAST;
#else
goto not_there;
#endif
#ifdef SO_DEBUG
return SO_DEBUG;
#else
goto not_there;
#endif
#ifdef SO_DONTLINGER
return SO_DONTLINGER;
#else
goto not_there;
#endif
#ifdef SO_DONTROUTE
return SO_DONTROUTE;
#else
goto not_there;
#endif
#ifdef SO_ERROR
return SO_ERROR;
#else
goto not_there;
#endif
#ifdef SO_KEEPALIVE
return SO_KEEPALIVE;
#else
goto not_there;
#endif
#ifdef SO_LINGER
return SO_LINGER;
#else
goto not_there;
#endif
#ifdef SO_OOBINLINE
return SO_OOBINLINE;
#else
goto not_there;
#endif
#ifdef SO_RCVBUF
return SO_RCVBUF;
#else
goto not_there;
#endif
#ifdef SO_RCVLOWAT
return SO_RCVLOWAT;
#else
goto not_there;
#endif
#ifdef SO_RCVTIMEO
return SO_RCVTIMEO;
#else
goto not_there;
#endif
#ifdef SO_REUSEADDR
return SO_REUSEADDR;
#else
goto not_there;
#endif
#ifdef SO_REUSEPORT
return SO_REUSEPORT;
#else
goto not_there;
#endif
#ifdef SO_SNDBUF
return SO_SNDBUF;
#else
goto not_there;
#endif
#ifdef SO_SNDLOWAT
return SO_SNDLOWAT;
#else
goto not_there;
#endif
#ifdef SO_SNDTIMEO
return SO_SNDTIMEO;
#else
goto not_there;
#endif
#ifdef SO_TYPE
return SO_TYPE;
#else
goto not_there;
#endif
#ifdef SO_USELOOPBACK
return SO_USELOOPBACK;
#else
goto not_there;
#endif
break;
case 'T':
#ifdef TCP_KEEPALIVE
return TCP_KEEPALIVE;
#else
goto not_there;
#endif
#ifdef TCP_MAXRT
return TCP_MAXRT;
#else
goto not_there;
#endif
#ifdef TCP_MAXSEG
return TCP_MAXSEG;
#else
goto not_there;
#endif
#ifdef TCP_NODELAY
return TCP_NODELAY;
#else
goto not_there;
#endif
#ifdef TCP_STDURG
return TCP_STDURG;
#else
goto not_there;
#endif
break;
case 'U':
#ifdef UIO_MAXIOV
return UIO_MAXIOV;
#else
goto not_there;
#endif
break;
case 'V':
break;
case 'W':
break;
case 'X':
break;
case 'Y':
break;
case 'Z':
break;
}
return 0;
return 0;
}
double
char * name
int arg
void
char * host
CODE:
{
struct in_addr ip_address;
ok = 1;
}
ST(0) = sv_newmortal();
if (ok) {
}
}
void
CODE:
{
char * addr_str;
croak("Bad arg length for %s, length is %d, should be %d",
"Socket::inet_ntoa",
}
}
void
char * pathname
CODE:
{
#ifdef I_SYS_UN
# ifdef OS2 /* Name should start with \socket\ and contain backslashes! */
{
int off;
char *s, *e;
else if (len < 8
off = 7;
else
off = 0; /* Preserve names starting with \socket\ */
e = s + len + 1;
while (++s < e)
if (*s = '/')
*s = '\\';
}
# else /* !( defined OS2 ) */
# endif
#else
#endif
}
void
CODE:
{
#ifdef I_SYS_UN
struct sockaddr_un addr;
char * e;
# ifndef __linux__
/* On Linux sockaddrlen on sockets returned by accept, recvfrom,
getpeername and getsockname is not equal to sizeof(addr). */
if (sockaddrlen != sizeof(addr)) {
croak("Bad arg length for %s, length is %d, should be %d",
"Socket::unpack_sockaddr_un",
sockaddrlen, sizeof(addr));
}
# endif
croak("Bad address family for %s, got %d, should be %d",
"Socket::unpack_sockaddr_un",
AF_UNIX);
}
++e;
#else
#endif
}
void
unsigned short port
char * ip_address
CODE:
{
struct sockaddr_in sin;
}
void
{
struct sockaddr_in addr;
unsigned short port;
struct in_addr ip_address;
if (sockaddrlen != sizeof(addr)) {
croak("Bad arg length for %s, length is %d, should be %d",
"Socket::unpack_sockaddr_in",
sockaddrlen, sizeof(addr));
}
croak("Bad address family for %s, got %d, should be %d",
"Socket::unpack_sockaddr_in",
AF_INET);
}
}
void
CODE:
{
struct in_addr ip_address;
}
void
CODE:
{
struct in_addr ip_address;
}
void
CODE:
{
struct in_addr ip_address;
}
void
CODE:
{
struct in_addr ip_address;
}