usocket: pragma visibility

This commit is contained in:
E. Westbrook 2019-02-27 21:02:24 -07:00
parent 4e363330a3
commit 8b2dcdcf7d
2 changed files with 57 additions and 51 deletions

View File

@ -23,7 +23,7 @@
#define WAITFD_R POLLIN
#define WAITFD_W POLLOUT
#define WAITFD_C (POLLIN|POLLOUT)
LUASOCKET_PRIVATE int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
int ret;
struct pollfd pfd;
pfd.fd = *ps;
@ -45,7 +45,7 @@ LUASOCKET_PRIVATE int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
#define WAITFD_W 2
#define WAITFD_C (WAITFD_R|WAITFD_W)
LUASOCKET_PRIVATE int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
int ret;
fd_set rfds, wfds, *rp, *wp;
struct timeval tv, *tp;
@ -77,7 +77,7 @@ LUASOCKET_PRIVATE int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
/*-------------------------------------------------------------------------*\
* Initializes module
\*-------------------------------------------------------------------------*/
LUASOCKET_PRIVATE int socket_open(void) {
int socket_open(void) {
/* installs a handler to ignore sigpipe or it will crash us */
signal(SIGPIPE, SIG_IGN);
return 1;
@ -86,14 +86,14 @@ LUASOCKET_PRIVATE int socket_open(void) {
/*-------------------------------------------------------------------------*\
* Close module
\*-------------------------------------------------------------------------*/
LUASOCKET_PRIVATE int socket_close(void) {
int socket_close(void) {
return 1;
}
/*-------------------------------------------------------------------------*\
* Close and inutilize socket
\*-------------------------------------------------------------------------*/
LUASOCKET_PRIVATE void socket_destroy(p_socket ps) {
void socket_destroy(p_socket ps) {
if (*ps != SOCKET_INVALID) {
close(*ps);
*ps = SOCKET_INVALID;
@ -103,7 +103,7 @@ LUASOCKET_PRIVATE void socket_destroy(p_socket ps) {
/*-------------------------------------------------------------------------*\
* Select with timeout control
\*-------------------------------------------------------------------------*/
LUASOCKET_PRIVATE int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_set *efds,
int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_set *efds,
p_timeout tm) {
int ret;
do {
@ -120,7 +120,7 @@ LUASOCKET_PRIVATE int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_s
/*-------------------------------------------------------------------------*\
* Creates and sets up a socket
\*-------------------------------------------------------------------------*/
LUASOCKET_PRIVATE int socket_create(p_socket ps, int domain, int type, int protocol) {
int socket_create(p_socket ps, int domain, int type, int protocol) {
*ps = socket(domain, type, protocol);
if (*ps != SOCKET_INVALID) return IO_DONE;
else return errno;
@ -129,7 +129,7 @@ LUASOCKET_PRIVATE int socket_create(p_socket ps, int domain, int type, int proto
/*-------------------------------------------------------------------------*\
* Binds or returns error message
\*-------------------------------------------------------------------------*/
LUASOCKET_PRIVATE int socket_bind(p_socket ps, SA *addr, socklen_t len) {
int socket_bind(p_socket ps, SA *addr, socklen_t len) {
int err = IO_DONE;
socket_setblocking(ps);
if (bind(*ps, addr, len) < 0) err = errno;
@ -140,7 +140,7 @@ LUASOCKET_PRIVATE int socket_bind(p_socket ps, SA *addr, socklen_t len) {
/*-------------------------------------------------------------------------*\
*
\*-------------------------------------------------------------------------*/
LUASOCKET_PRIVATE int socket_listen(p_socket ps, int backlog) {
int socket_listen(p_socket ps, int backlog) {
int err = IO_DONE;
if (listen(*ps, backlog)) err = errno;
return err;
@ -149,14 +149,14 @@ LUASOCKET_PRIVATE int socket_listen(p_socket ps, int backlog) {
/*-------------------------------------------------------------------------*\
*
\*-------------------------------------------------------------------------*/
LUASOCKET_PRIVATE void socket_shutdown(p_socket ps, int how) {
void socket_shutdown(p_socket ps, int how) {
shutdown(*ps, how);
}
/*-------------------------------------------------------------------------*\
* Connects or returns error message
\*-------------------------------------------------------------------------*/
LUASOCKET_PRIVATE int socket_connect(p_socket ps, SA *addr, socklen_t len, p_timeout tm) {
int socket_connect(p_socket ps, SA *addr, socklen_t len, p_timeout tm) {
int err;
/* avoid calling on closed sockets */
if (*ps == SOCKET_INVALID) return IO_CLOSED;
@ -178,7 +178,7 @@ LUASOCKET_PRIVATE int socket_connect(p_socket ps, SA *addr, socklen_t len, p_tim
/*-------------------------------------------------------------------------*\
* Accept with timeout
\*-------------------------------------------------------------------------*/
LUASOCKET_PRIVATE int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len, p_timeout tm) {
int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len, p_timeout tm) {
if (*ps == SOCKET_INVALID) return IO_CLOSED;
for ( ;; ) {
int err;
@ -195,7 +195,7 @@ LUASOCKET_PRIVATE int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_
/*-------------------------------------------------------------------------*\
* Send with timeout
\*-------------------------------------------------------------------------*/
LUASOCKET_PRIVATE int socket_send(p_socket ps, const char *data, size_t count,
int socket_send(p_socket ps, const char *data, size_t count,
size_t *sent, p_timeout tm)
{
int err;
@ -229,7 +229,7 @@ LUASOCKET_PRIVATE int socket_send(p_socket ps, const char *data, size_t count,
/*-------------------------------------------------------------------------*\
* Sendto with timeout
\*-------------------------------------------------------------------------*/
LUASOCKET_PRIVATE int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent,
int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent,
SA *addr, socklen_t len, p_timeout tm)
{
int err;
@ -254,7 +254,7 @@ LUASOCKET_PRIVATE int socket_sendto(p_socket ps, const char *data, size_t count,
/*-------------------------------------------------------------------------*\
* Receive with timeout
\*-------------------------------------------------------------------------*/
LUASOCKET_PRIVATE int socket_recv(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm) {
int socket_recv(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm) {
int err;
*got = 0;
if (*ps == SOCKET_INVALID) return IO_CLOSED;
@ -276,7 +276,7 @@ LUASOCKET_PRIVATE int socket_recv(p_socket ps, char *data, size_t count, size_t
/*-------------------------------------------------------------------------*\
* Recvfrom with timeout
\*-------------------------------------------------------------------------*/
LUASOCKET_PRIVATE int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got,
int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got,
SA *addr, socklen_t *len, p_timeout tm) {
int err;
*got = 0;
@ -304,7 +304,7 @@ LUASOCKET_PRIVATE int socket_recvfrom(p_socket ps, char *data, size_t count, siz
* with send/recv replaced with write/read. We can't just use write/read
* in the socket version, because behaviour when size is zero is different.
\*-------------------------------------------------------------------------*/
LUASOCKET_PRIVATE int socket_write(p_socket ps, const char *data, size_t count,
int socket_write(p_socket ps, const char *data, size_t count,
size_t *sent, p_timeout tm)
{
int err;
@ -339,7 +339,7 @@ LUASOCKET_PRIVATE int socket_write(p_socket ps, const char *data, size_t count,
* Read with timeout
* See note for socket_write
\*-------------------------------------------------------------------------*/
LUASOCKET_PRIVATE int socket_read(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm) {
int socket_read(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm) {
int err;
*got = 0;
if (*ps == SOCKET_INVALID) return IO_CLOSED;
@ -361,7 +361,7 @@ LUASOCKET_PRIVATE int socket_read(p_socket ps, char *data, size_t count, size_t
/*-------------------------------------------------------------------------*\
* Put socket into blocking mode
\*-------------------------------------------------------------------------*/
LUASOCKET_PRIVATE void socket_setblocking(p_socket ps) {
void socket_setblocking(p_socket ps) {
int flags = fcntl(*ps, F_GETFL, 0);
flags &= (~(O_NONBLOCK));
fcntl(*ps, F_SETFL, flags);
@ -370,7 +370,7 @@ LUASOCKET_PRIVATE void socket_setblocking(p_socket ps) {
/*-------------------------------------------------------------------------*\
* Put socket into non-blocking mode
\*-------------------------------------------------------------------------*/
LUASOCKET_PRIVATE void socket_setnonblocking(p_socket ps) {
void socket_setnonblocking(p_socket ps) {
int flags = fcntl(*ps, F_GETFL, 0);
flags |= O_NONBLOCK;
fcntl(*ps, F_SETFL, flags);
@ -379,7 +379,7 @@ LUASOCKET_PRIVATE void socket_setnonblocking(p_socket ps) {
/*-------------------------------------------------------------------------*\
* DNS helpers
\*-------------------------------------------------------------------------*/
LUASOCKET_PRIVATE int socket_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp) {
int socket_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp) {
*hp = gethostbyaddr(addr, len, AF_INET);
if (*hp) return IO_DONE;
else if (h_errno) return h_errno;
@ -387,7 +387,7 @@ LUASOCKET_PRIVATE int socket_gethostbyaddr(const char *addr, socklen_t len, stru
else return IO_UNKNOWN;
}
LUASOCKET_PRIVATE int socket_gethostbyname(const char *addr, struct hostent **hp) {
int socket_gethostbyname(const char *addr, struct hostent **hp) {
*hp = gethostbyname(addr);
if (*hp) return IO_DONE;
else if (h_errno) return h_errno;
@ -399,7 +399,7 @@ LUASOCKET_PRIVATE int socket_gethostbyname(const char *addr, struct hostent **hp
* Error translation functions
* Make sure important error messages are standard
\*-------------------------------------------------------------------------*/
LUASOCKET_PRIVATE const char *socket_hoststrerror(int err) {
const char *socket_hoststrerror(int err) {
if (err <= 0) return io_strerror(err);
switch (err) {
case HOST_NOT_FOUND: return PIE_HOST_NOT_FOUND;
@ -407,7 +407,7 @@ LUASOCKET_PRIVATE const char *socket_hoststrerror(int err) {
}
}
LUASOCKET_PRIVATE const char *socket_strerror(int err) {
const char *socket_strerror(int err) {
if (err <= 0) return io_strerror(err);
switch (err) {
case EADDRINUSE: return PIE_ADDRINUSE;
@ -423,12 +423,12 @@ LUASOCKET_PRIVATE const char *socket_strerror(int err) {
}
}
LUASOCKET_PRIVATE const char *socket_ioerror(p_socket ps, int err) {
const char *socket_ioerror(p_socket ps, int err) {
(void) ps;
return socket_strerror(err);
}
LUASOCKET_PRIVATE const char *socket_gaistrerror(int err) {
const char *socket_gaistrerror(int err) {
if (err == 0) return NULL;
switch (err) {
case EAI_AGAIN: return PIE_AGAIN;
@ -452,4 +452,3 @@ LUASOCKET_PRIVATE const char *socket_gaistrerror(int err) {
default: return gai_strerror(err);
}
}

View File

@ -12,13 +12,17 @@
#include "socket.h"
#include "pierror.h"
#ifndef _WIN32
#pragma GCC visibility push(hidden)
#endif
/* WinSock doesn't have a strerror... */
static const char *wstrerror(int err);
/*-------------------------------------------------------------------------*\
* Initializes module
\*-------------------------------------------------------------------------*/
LUASOCKET_PRIVATE int socket_open(void) {
int socket_open(void) {
WSADATA wsaData;
WORD wVersionRequested = MAKEWORD(2, 0);
int err = WSAStartup(wVersionRequested, &wsaData );
@ -34,7 +38,7 @@ LUASOCKET_PRIVATE int socket_open(void) {
/*-------------------------------------------------------------------------*\
* Close module
\*-------------------------------------------------------------------------*/
LUASOCKET_PRIVATE int socket_close(void) {
int socket_close(void) {
WSACleanup();
return 1;
}
@ -47,7 +51,7 @@ LUASOCKET_PRIVATE int socket_close(void) {
#define WAITFD_E 4
#define WAITFD_C (WAITFD_E|WAITFD_W)
LUASOCKET_PRIVATE int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
int ret;
fd_set rfds, wfds, efds, *rp = NULL, *wp = NULL, *ep = NULL;
struct timeval tv, *tp = NULL;
@ -75,7 +79,7 @@ LUASOCKET_PRIVATE int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
/*-------------------------------------------------------------------------*\
* Select with int timeout in ms
\*-------------------------------------------------------------------------*/
LUASOCKET_PRIVATE int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_set *efds,
int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_set *efds,
p_timeout tm) {
struct timeval tv;
double t = timeout_get(tm);
@ -90,7 +94,7 @@ LUASOCKET_PRIVATE int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_s
/*-------------------------------------------------------------------------*\
* Close and inutilize socket
\*-------------------------------------------------------------------------*/
LUASOCKET_PRIVATE void socket_destroy(p_socket ps) {
void socket_destroy(p_socket ps) {
if (*ps != SOCKET_INVALID) {
socket_setblocking(ps); /* close can take a long time on WIN32 */
closesocket(*ps);
@ -101,7 +105,7 @@ LUASOCKET_PRIVATE void socket_destroy(p_socket ps) {
/*-------------------------------------------------------------------------*\
*
\*-------------------------------------------------------------------------*/
LUASOCKET_PRIVATE void socket_shutdown(p_socket ps, int how) {
void socket_shutdown(p_socket ps, int how) {
socket_setblocking(ps);
shutdown(*ps, how);
socket_setnonblocking(ps);
@ -110,7 +114,7 @@ LUASOCKET_PRIVATE void socket_shutdown(p_socket ps, int how) {
/*-------------------------------------------------------------------------*\
* Creates and sets up a socket
\*-------------------------------------------------------------------------*/
LUASOCKET_PRIVATE int socket_create(p_socket ps, int domain, int type, int protocol) {
int socket_create(p_socket ps, int domain, int type, int protocol) {
*ps = socket(domain, type, protocol);
if (*ps != SOCKET_INVALID) return IO_DONE;
else return WSAGetLastError();
@ -119,7 +123,7 @@ LUASOCKET_PRIVATE int socket_create(p_socket ps, int domain, int type, int proto
/*-------------------------------------------------------------------------*\
* Connects or returns error message
\*-------------------------------------------------------------------------*/
LUASOCKET_PRIVATE int socket_connect(p_socket ps, SA *addr, socklen_t len, p_timeout tm) {
int socket_connect(p_socket ps, SA *addr, socklen_t len, p_timeout tm) {
int err;
/* don't call on closed socket */
if (*ps == SOCKET_INVALID) return IO_CLOSED;
@ -148,7 +152,7 @@ LUASOCKET_PRIVATE int socket_connect(p_socket ps, SA *addr, socklen_t len, p_tim
/*-------------------------------------------------------------------------*\
* Binds or returns error message
\*-------------------------------------------------------------------------*/
LUASOCKET_PRIVATE int socket_bind(p_socket ps, SA *addr, socklen_t len) {
int socket_bind(p_socket ps, SA *addr, socklen_t len) {
int err = IO_DONE;
socket_setblocking(ps);
if (bind(*ps, addr, len) < 0) err = WSAGetLastError();
@ -159,7 +163,7 @@ LUASOCKET_PRIVATE int socket_bind(p_socket ps, SA *addr, socklen_t len) {
/*-------------------------------------------------------------------------*\
*
\*-------------------------------------------------------------------------*/
LUASOCKET_PRIVATE int socket_listen(p_socket ps, int backlog) {
int socket_listen(p_socket ps, int backlog) {
int err = IO_DONE;
socket_setblocking(ps);
if (listen(*ps, backlog) < 0) err = WSAGetLastError();
@ -170,7 +174,7 @@ LUASOCKET_PRIVATE int socket_listen(p_socket ps, int backlog) {
/*-------------------------------------------------------------------------*\
* Accept with timeout
\*-------------------------------------------------------------------------*/
LUASOCKET_PRIVATE int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len,
int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len,
p_timeout tm) {
if (*ps == SOCKET_INVALID) return IO_CLOSED;
for ( ;; ) {
@ -192,7 +196,7 @@ LUASOCKET_PRIVATE int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_
* this can take an awful lot of time and we will end up blocked.
* Therefore, whoever calls this function should not pass a huge buffer.
\*-------------------------------------------------------------------------*/
LUASOCKET_PRIVATE int socket_send(p_socket ps, const char *data, size_t count,
int socket_send(p_socket ps, const char *data, size_t count,
size_t *sent, p_timeout tm)
{
int err;
@ -220,7 +224,7 @@ LUASOCKET_PRIVATE int socket_send(p_socket ps, const char *data, size_t count,
/*-------------------------------------------------------------------------*\
* Sendto with timeout
\*-------------------------------------------------------------------------*/
LUASOCKET_PRIVATE int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent,
int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent,
SA *addr, socklen_t len, p_timeout tm)
{
int err;
@ -241,7 +245,7 @@ LUASOCKET_PRIVATE int socket_sendto(p_socket ps, const char *data, size_t count,
/*-------------------------------------------------------------------------*\
* Receive with timeout
\*-------------------------------------------------------------------------*/
LUASOCKET_PRIVATE int socket_recv(p_socket ps, char *data, size_t count, size_t *got,
int socket_recv(p_socket ps, char *data, size_t count, size_t *got,
p_timeout tm)
{
int err, prev = IO_DONE;
@ -270,7 +274,7 @@ LUASOCKET_PRIVATE int socket_recv(p_socket ps, char *data, size_t count, size_t
/*-------------------------------------------------------------------------*\
* Recvfrom with timeout
\*-------------------------------------------------------------------------*/
LUASOCKET_PRIVATE int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got,
int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got,
SA *addr, socklen_t *len, p_timeout tm)
{
int err, prev = IO_DONE;
@ -299,7 +303,7 @@ LUASOCKET_PRIVATE int socket_recvfrom(p_socket ps, char *data, size_t count, siz
/*-------------------------------------------------------------------------*\
* Put socket into blocking mode
\*-------------------------------------------------------------------------*/
LUASOCKET_PRIVATE void socket_setblocking(p_socket ps) {
void socket_setblocking(p_socket ps) {
u_long argp = 0;
ioctlsocket(*ps, FIONBIO, &argp);
}
@ -307,7 +311,7 @@ LUASOCKET_PRIVATE void socket_setblocking(p_socket ps) {
/*-------------------------------------------------------------------------*\
* Put socket into non-blocking mode
\*-------------------------------------------------------------------------*/
LUASOCKET_PRIVATE void socket_setnonblocking(p_socket ps) {
void socket_setnonblocking(p_socket ps) {
u_long argp = 1;
ioctlsocket(*ps, FIONBIO, &argp);
}
@ -315,13 +319,13 @@ LUASOCKET_PRIVATE void socket_setnonblocking(p_socket ps) {
/*-------------------------------------------------------------------------*\
* DNS helpers
\*-------------------------------------------------------------------------*/
LUASOCKET_PRIVATE int socket_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp) {
int socket_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp) {
*hp = gethostbyaddr(addr, len, AF_INET);
if (*hp) return IO_DONE;
else return WSAGetLastError();
}
LUASOCKET_PRIVATE int socket_gethostbyname(const char *addr, struct hostent **hp) {
int socket_gethostbyname(const char *addr, struct hostent **hp) {
*hp = gethostbyname(addr);
if (*hp) return IO_DONE;
else return WSAGetLastError();
@ -330,7 +334,7 @@ LUASOCKET_PRIVATE int socket_gethostbyname(const char *addr, struct hostent **hp
/*-------------------------------------------------------------------------*\
* Error translation functions
\*-------------------------------------------------------------------------*/
LUASOCKET_PRIVATE const char *socket_hoststrerror(int err) {
const char *socket_hoststrerror(int err) {
if (err <= 0) return io_strerror(err);
switch (err) {
case WSAHOST_NOT_FOUND: return PIE_HOST_NOT_FOUND;
@ -338,7 +342,7 @@ LUASOCKET_PRIVATE const char *socket_hoststrerror(int err) {
}
}
LUASOCKET_PRIVATE const char *socket_strerror(int err) {
const char *socket_strerror(int err) {
if (err <= 0) return io_strerror(err);
switch (err) {
case WSAEADDRINUSE: return PIE_ADDRINUSE;
@ -352,12 +356,12 @@ LUASOCKET_PRIVATE const char *socket_strerror(int err) {
}
}
LUASOCKET_PRIVATE const char *socket_ioerror(p_socket ps, int err) {
const char *socket_ioerror(p_socket ps, int err) {
(void) ps;
return socket_strerror(err);
}
LUASOCKET_PRIVATE static const char *wstrerror(int err) {
static const char *wstrerror(int err) {
switch (err) {
case WSAEINTR: return "Interrupted function call";
case WSAEACCES: return PIE_ACCESS; // "Permission denied";
@ -406,7 +410,7 @@ LUASOCKET_PRIVATE static const char *wstrerror(int err) {
}
}
LUASOCKET_PRIVATE const char *socket_gaistrerror(int err) {
const char *socket_gaistrerror(int err) {
if (err == 0) return NULL;
switch (err) {
case EAI_AGAIN: return PIE_AGAIN;
@ -433,3 +437,6 @@ LUASOCKET_PRIVATE const char *socket_gaistrerror(int err) {
}
}
#ifndef _WIN32
#pragma GCC visibility pop
#endif