commit 4bb61f538a25a4f55b27d6e71c690df5980fbf68 Author: Vasily Chekalkin Date: Wed Apr 1 07:08:12 2009 +1100 Implement lookup table for PIO_SOCK_* constants diff --git a/include/parrot/io.h b/include/parrot/io.h index 3460363..77c9c6e 100644 --- a/include/parrot/io.h +++ b/include/parrot/io.h @@ -922,12 +922,13 @@ typedef enum { } Socket_Protocol_Family; typedef enum { + PIO_SOCK_PACKET = 0, PIO_SOCK_STREAM = 1, PIO_SOCK_DGRAM = 2, PIO_SOCK_RAW = 3, PIO_SOCK_RDM = 4, PIO_SOCK_SEQPACKET = 5, - PIO_SOCK_PACKET = 10, + PIO_SOCK_MAX = 6 /* last element */ } Socket_Socket_Type; typedef enum { diff --git a/src/io/socket_unix.c b/src/io/socket_unix.c index b7d02f0..35b0be0 100644 --- a/src/io/socket_unix.c +++ b/src/io/socket_unix.c @@ -114,7 +114,7 @@ Parrot_io_sockaddr_in(PARROT_INTERP, ARGIN(STRING *addr), INTVAL port) # if PARROT_NET_DEVEL /* - * Mappping between PIO_PF_* constants and system-specific PF_* constants. + * Mapping between PIO_PF_* constants and system-specific PF_* constants. * * Uses -1 for unsupported protocols. */ @@ -143,6 +143,44 @@ static int pio_pf[PIO_PF_MAX+1] = { }; /* + * Mapping between PIO_SOCK_* constants and system-specific SOCK_* constants. + * Uses -1 for unsupported socket types. + */ + +static int pio_sock[PIO_SOCK_MAX+1] = { +# ifdef SOCK_PACKET + SOCK_PACKET, /* PIO_SOCK_PACKET */ +# else + -1, /* PIO_SOCK_PACKET */ +# endif +# ifdef SOCK_STREAM + SOCK_STREAM, /* PIO_SOCK_STREAM */ +# else + -1, /* PIO_SOCK_STREAM */ +# endif +# ifdef SOCK_DGRAM + SOCK_DGRAM, /* PIO_SOCK_DGRAM */ +# else + -1, /* PIO_SOCK_DGRAM */ +# endif +# ifdef SOCK_RAW + SOCK_RAW, /* PIO_SOCK_RAW */ +# else + -1, /* PIO_SOCK_RAW */ +# endif +#ifdef SOCK_RDM + SOCK_RDM, /* PIO_SOCK_RDM */ +# else + -1, /* PIO_SOCK_RDM */ +# endif +# ifdef SOCK_SEQPACKET + SOCK_SEQPACKET, /* PIO_SOCK_SEQPACKET */ +# else + -1, /* PIO_SOCK_SEQPACKET */ +# endif +}; + +/* =item C @@ -168,6 +206,13 @@ Parrot_io_socket_unix(PARROT_INTERP, ARGIN(PMC *s), int fam, int type, int proto if (fam < 0) return -1; + /* convert Parrot's socket type to system type */ + if (type < 0 || type >= PIO_SOCK_MAX) + return -1; + type = pio_sock[type]; + if (type < 0) + return -1; + sock = socket(fam, type, proto); if (sock >= 0) { setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &i, sizeof (i)); diff --git a/src/io/socket_win32.c b/src/io/socket_win32.c index a6e3403..c48b9d3 100644 --- a/src/io/socket_win32.c +++ b/src/io/socket_win32.c @@ -60,7 +60,7 @@ static void get_sockaddr_in(PARROT_INTERP, PARROT_SOCKET((p))->remote)) /* - * Mappping between PIO_PF_* constants and system-specific PF_* constants. + * Mapping between PIO_PF_* constants and system-specific PF_* constants. * * Uses -1 for unsupported protocols. */ @@ -89,6 +89,44 @@ static int pio_pf[PIO_PF_MAX+1] = { }; /* + * Mapping between PIO_SOCK_* constants and system-specific SOCK_* constants. + * Uses -1 for unsupported socket types. + */ + +static int pio_sock[PIO_SOCK_MAX+1] = { +# ifdef SOCK_PACKET + SOCK_PACKET, /* PIO_SOCK_PACKET */ +# else + -1, /* PIO_SOCK_PACKET */ +# endif +# ifdef SOCK_STREAM + SOCK_STREAM, /* PIO_SOCK_STREAM */ +# else + -1, /* PIO_SOCK_STREAM */ +# endif +# ifdef SOCK_DGRAM + SOCK_DGRAM, /* PIO_SOCK_DGRAM */ +# else + -1, /* PIO_SOCK_DGRAM */ +# endif +# ifdef SOCK_RAW + SOCK_RAW, /* PIO_SOCK_RAW */ +# else + -1, /* PIO_SOCK_RAW */ +# endif +#ifdef SOCK_RDM + SOCK_RDM, /* PIO_SOCK_RDM */ +# else + -1, /* PIO_SOCK_RDM */ +# endif +# ifdef SOCK_SEQPACKET + SOCK_SEQPACKET, /* PIO_SOCK_SEQPACKET */ +# else + -1, /* PIO_SOCK_SEQPACKET */ +# endif +}; + +/* =item C @@ -114,6 +152,13 @@ Parrot_io_socket_win32(PARROT_INTERP, ARGIN(PMC * s), int fam, int type, int pro if (fam < 0) return -1; + /* convert Parrot's socket type to system type */ + if (type < 0 || type >= PIO_SOCK_MAX) + return -1; + type = pio_sock[type]; + if (type < 0) + return -1; + sock = socket(fam, type, proto); if (sock >= 0) { setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&i, sizeof (i));