aboutsummaryrefslogtreecommitdiff
path: root/src/usocket.c
diff options
context:
space:
mode:
authorE. Westbrook <github@westbrook.io>2019-02-25 16:04:16 -0700
committerE. Westbrook <github@westbrook.io>2019-02-25 16:04:16 -0700
commitd7ffc2f4e69ff24a88f00456cd9a538ecc90d14c (patch)
tree658c7fc5129c57262a25bbcad246adaecf697a5a /src/usocket.c
parentfe437ee844e8e51ef0b7437bb34efd8c18927a75 (diff)
downloadluasocket-d7ffc2f4e69ff24a88f00456cd9a538ecc90d14c.tar.gz
luasocket-d7ffc2f4e69ff24a88f00456cd9a538ecc90d14c.tar.bz2
luasocket-d7ffc2f4e69ff24a88f00456cd9a538ecc90d14c.zip
usocket.c use LUASOCKET_PRIVATE
Diffstat (limited to 'src/usocket.c')
-rw-r--r--src/usocket.c58
1 files changed, 30 insertions, 28 deletions
diff --git a/src/usocket.c b/src/usocket.c
index 6e7f8f6..08a961d 100644
--- a/src/usocket.c
+++ b/src/usocket.c
@@ -6,12 +6,14 @@
6* The penalty of calling select to avoid busy-wait is only paid when 6* The penalty of calling select to avoid busy-wait is only paid when
7* the I/O call fail in the first place. 7* the I/O call fail in the first place.
8\*=========================================================================*/ 8\*=========================================================================*/
9#include <string.h> 9#include "luasocket.h"
10#include <signal.h>
11 10
12#include "socket.h" 11#include "socket.h"
13#include "pierror.h" 12#include "pierror.h"
14 13
14#include <string.h>
15#include <signal.h>
16
15/*-------------------------------------------------------------------------*\ 17/*-------------------------------------------------------------------------*\
16* Wait for readable/writable/connected socket with timeout 18* Wait for readable/writable/connected socket with timeout
17\*-------------------------------------------------------------------------*/ 19\*-------------------------------------------------------------------------*/
@@ -21,7 +23,7 @@
21#define WAITFD_R POLLIN 23#define WAITFD_R POLLIN
22#define WAITFD_W POLLOUT 24#define WAITFD_W POLLOUT
23#define WAITFD_C (POLLIN|POLLOUT) 25#define WAITFD_C (POLLIN|POLLOUT)
24int socket_waitfd(p_socket ps, int sw, p_timeout tm) { 26LUASOCKET_PRIVATE int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
25 int ret; 27 int ret;
26 struct pollfd pfd; 28 struct pollfd pfd;
27 pfd.fd = *ps; 29 pfd.fd = *ps;
@@ -43,7 +45,7 @@ int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
43#define WAITFD_W 2 45#define WAITFD_W 2
44#define WAITFD_C (WAITFD_R|WAITFD_W) 46#define WAITFD_C (WAITFD_R|WAITFD_W)
45 47
46int socket_waitfd(p_socket ps, int sw, p_timeout tm) { 48LUASOCKET_PRIVATE int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
47 int ret; 49 int ret;
48 fd_set rfds, wfds, *rp, *wp; 50 fd_set rfds, wfds, *rp, *wp;
49 struct timeval tv, *tp; 51 struct timeval tv, *tp;
@@ -75,7 +77,7 @@ int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
75/*-------------------------------------------------------------------------*\ 77/*-------------------------------------------------------------------------*\
76* Initializes module 78* Initializes module
77\*-------------------------------------------------------------------------*/ 79\*-------------------------------------------------------------------------*/
78int socket_open(void) { 80LUASOCKET_PRIVATE int socket_open(void) {
79 /* installs a handler to ignore sigpipe or it will crash us */ 81 /* installs a handler to ignore sigpipe or it will crash us */
80 signal(SIGPIPE, SIG_IGN); 82 signal(SIGPIPE, SIG_IGN);
81 return 1; 83 return 1;
@@ -84,14 +86,14 @@ int socket_open(void) {
84/*-------------------------------------------------------------------------*\ 86/*-------------------------------------------------------------------------*\
85* Close module 87* Close module
86\*-------------------------------------------------------------------------*/ 88\*-------------------------------------------------------------------------*/
87int socket_close(void) { 89LUASOCKET_PRIVATE int socket_close(void) {
88 return 1; 90 return 1;
89} 91}
90 92
91/*-------------------------------------------------------------------------*\ 93/*-------------------------------------------------------------------------*\
92* Close and inutilize socket 94* Close and inutilize socket
93\*-------------------------------------------------------------------------*/ 95\*-------------------------------------------------------------------------*/
94void socket_destroy(p_socket ps) { 96LUASOCKET_PRIVATE void socket_destroy(p_socket ps) {
95 if (*ps != SOCKET_INVALID) { 97 if (*ps != SOCKET_INVALID) {
96 close(*ps); 98 close(*ps);
97 *ps = SOCKET_INVALID; 99 *ps = SOCKET_INVALID;
@@ -101,7 +103,7 @@ void socket_destroy(p_socket ps) {
101/*-------------------------------------------------------------------------*\ 103/*-------------------------------------------------------------------------*\
102* Select with timeout control 104* Select with timeout control
103\*-------------------------------------------------------------------------*/ 105\*-------------------------------------------------------------------------*/
104int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_set *efds, 106LUASOCKET_PRIVATE int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_set *efds,
105 p_timeout tm) { 107 p_timeout tm) {
106 int ret; 108 int ret;
107 do { 109 do {
@@ -118,7 +120,7 @@ int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_set *efds,
118/*-------------------------------------------------------------------------*\ 120/*-------------------------------------------------------------------------*\
119* Creates and sets up a socket 121* Creates and sets up a socket
120\*-------------------------------------------------------------------------*/ 122\*-------------------------------------------------------------------------*/
121int socket_create(p_socket ps, int domain, int type, int protocol) { 123LUASOCKET_PRIVATE int socket_create(p_socket ps, int domain, int type, int protocol) {
122 *ps = socket(domain, type, protocol); 124 *ps = socket(domain, type, protocol);
123 if (*ps != SOCKET_INVALID) return IO_DONE; 125 if (*ps != SOCKET_INVALID) return IO_DONE;
124 else return errno; 126 else return errno;
@@ -127,7 +129,7 @@ int socket_create(p_socket ps, int domain, int type, int protocol) {
127/*-------------------------------------------------------------------------*\ 129/*-------------------------------------------------------------------------*\
128* Binds or returns error message 130* Binds or returns error message
129\*-------------------------------------------------------------------------*/ 131\*-------------------------------------------------------------------------*/
130int socket_bind(p_socket ps, SA *addr, socklen_t len) { 132LUASOCKET_PRIVATE int socket_bind(p_socket ps, SA *addr, socklen_t len) {
131 int err = IO_DONE; 133 int err = IO_DONE;
132 socket_setblocking(ps); 134 socket_setblocking(ps);
133 if (bind(*ps, addr, len) < 0) err = errno; 135 if (bind(*ps, addr, len) < 0) err = errno;
@@ -138,7 +140,7 @@ int socket_bind(p_socket ps, SA *addr, socklen_t len) {
138/*-------------------------------------------------------------------------*\ 140/*-------------------------------------------------------------------------*\
139* 141*
140\*-------------------------------------------------------------------------*/ 142\*-------------------------------------------------------------------------*/
141int socket_listen(p_socket ps, int backlog) { 143LUASOCKET_PRIVATE int socket_listen(p_socket ps, int backlog) {
142 int err = IO_DONE; 144 int err = IO_DONE;
143 if (listen(*ps, backlog)) err = errno; 145 if (listen(*ps, backlog)) err = errno;
144 return err; 146 return err;
@@ -147,14 +149,14 @@ int socket_listen(p_socket ps, int backlog) {
147/*-------------------------------------------------------------------------*\ 149/*-------------------------------------------------------------------------*\
148* 150*
149\*-------------------------------------------------------------------------*/ 151\*-------------------------------------------------------------------------*/
150void socket_shutdown(p_socket ps, int how) { 152LUASOCKET_PRIVATE void socket_shutdown(p_socket ps, int how) {
151 shutdown(*ps, how); 153 shutdown(*ps, how);
152} 154}
153 155
154/*-------------------------------------------------------------------------*\ 156/*-------------------------------------------------------------------------*\
155* Connects or returns error message 157* Connects or returns error message
156\*-------------------------------------------------------------------------*/ 158\*-------------------------------------------------------------------------*/
157int socket_connect(p_socket ps, SA *addr, socklen_t len, p_timeout tm) { 159LUASOCKET_PRIVATE int socket_connect(p_socket ps, SA *addr, socklen_t len, p_timeout tm) {
158 int err; 160 int err;
159 /* avoid calling on closed sockets */ 161 /* avoid calling on closed sockets */
160 if (*ps == SOCKET_INVALID) return IO_CLOSED; 162 if (*ps == SOCKET_INVALID) return IO_CLOSED;
@@ -176,7 +178,7 @@ int socket_connect(p_socket ps, SA *addr, socklen_t len, p_timeout tm) {
176/*-------------------------------------------------------------------------*\ 178/*-------------------------------------------------------------------------*\
177* Accept with timeout 179* Accept with timeout
178\*-------------------------------------------------------------------------*/ 180\*-------------------------------------------------------------------------*/
179int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len, p_timeout tm) { 181LUASOCKET_PRIVATE int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len, p_timeout tm) {
180 if (*ps == SOCKET_INVALID) return IO_CLOSED; 182 if (*ps == SOCKET_INVALID) return IO_CLOSED;
181 for ( ;; ) { 183 for ( ;; ) {
182 int err; 184 int err;
@@ -193,7 +195,7 @@ int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len, p_timeout
193/*-------------------------------------------------------------------------*\ 195/*-------------------------------------------------------------------------*\
194* Send with timeout 196* Send with timeout
195\*-------------------------------------------------------------------------*/ 197\*-------------------------------------------------------------------------*/
196int socket_send(p_socket ps, const char *data, size_t count, 198LUASOCKET_PRIVATE int socket_send(p_socket ps, const char *data, size_t count,
197 size_t *sent, p_timeout tm) 199 size_t *sent, p_timeout tm)
198{ 200{
199 int err; 201 int err;
@@ -227,7 +229,7 @@ int socket_send(p_socket ps, const char *data, size_t count,
227/*-------------------------------------------------------------------------*\ 229/*-------------------------------------------------------------------------*\
228* Sendto with timeout 230* Sendto with timeout
229\*-------------------------------------------------------------------------*/ 231\*-------------------------------------------------------------------------*/
230int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent, 232LUASOCKET_PRIVATE int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent,
231 SA *addr, socklen_t len, p_timeout tm) 233 SA *addr, socklen_t len, p_timeout tm)
232{ 234{
233 int err; 235 int err;
@@ -252,7 +254,7 @@ int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent,
252/*-------------------------------------------------------------------------*\ 254/*-------------------------------------------------------------------------*\
253* Receive with timeout 255* Receive with timeout
254\*-------------------------------------------------------------------------*/ 256\*-------------------------------------------------------------------------*/
255int socket_recv(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm) { 257LUASOCKET_PRIVATE int socket_recv(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm) {
256 int err; 258 int err;
257 *got = 0; 259 *got = 0;
258 if (*ps == SOCKET_INVALID) return IO_CLOSED; 260 if (*ps == SOCKET_INVALID) return IO_CLOSED;
@@ -274,7 +276,7 @@ int socket_recv(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm
274/*-------------------------------------------------------------------------*\ 276/*-------------------------------------------------------------------------*\
275* Recvfrom with timeout 277* Recvfrom with timeout
276\*-------------------------------------------------------------------------*/ 278\*-------------------------------------------------------------------------*/
277int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got, 279LUASOCKET_PRIVATE int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got,
278 SA *addr, socklen_t *len, p_timeout tm) { 280 SA *addr, socklen_t *len, p_timeout tm) {
279 int err; 281 int err;
280 *got = 0; 282 *got = 0;
@@ -302,7 +304,7 @@ int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got,
302* with send/recv replaced with write/read. We can't just use write/read 304* with send/recv replaced with write/read. We can't just use write/read
303* in the socket version, because behaviour when size is zero is different. 305* in the socket version, because behaviour when size is zero is different.
304\*-------------------------------------------------------------------------*/ 306\*-------------------------------------------------------------------------*/
305int socket_write(p_socket ps, const char *data, size_t count, 307LUASOCKET_PRIVATE int socket_write(p_socket ps, const char *data, size_t count,
306 size_t *sent, p_timeout tm) 308 size_t *sent, p_timeout tm)
307{ 309{
308 int err; 310 int err;
@@ -337,7 +339,7 @@ int socket_write(p_socket ps, const char *data, size_t count,
337* Read with timeout 339* Read with timeout
338* See note for socket_write 340* See note for socket_write
339\*-------------------------------------------------------------------------*/ 341\*-------------------------------------------------------------------------*/
340int socket_read(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm) { 342LUASOCKET_PRIVATE int socket_read(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm) {
341 int err; 343 int err;
342 *got = 0; 344 *got = 0;
343 if (*ps == SOCKET_INVALID) return IO_CLOSED; 345 if (*ps == SOCKET_INVALID) return IO_CLOSED;
@@ -359,7 +361,7 @@ int socket_read(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm
359/*-------------------------------------------------------------------------*\ 361/*-------------------------------------------------------------------------*\
360* Put socket into blocking mode 362* Put socket into blocking mode
361\*-------------------------------------------------------------------------*/ 363\*-------------------------------------------------------------------------*/
362void socket_setblocking(p_socket ps) { 364LUASOCKET_PRIVATE void socket_setblocking(p_socket ps) {
363 int flags = fcntl(*ps, F_GETFL, 0); 365 int flags = fcntl(*ps, F_GETFL, 0);
364 flags &= (~(O_NONBLOCK)); 366 flags &= (~(O_NONBLOCK));
365 fcntl(*ps, F_SETFL, flags); 367 fcntl(*ps, F_SETFL, flags);
@@ -368,7 +370,7 @@ void socket_setblocking(p_socket ps) {
368/*-------------------------------------------------------------------------*\ 370/*-------------------------------------------------------------------------*\
369* Put socket into non-blocking mode 371* Put socket into non-blocking mode
370\*-------------------------------------------------------------------------*/ 372\*-------------------------------------------------------------------------*/
371void socket_setnonblocking(p_socket ps) { 373LUASOCKET_PRIVATE void socket_setnonblocking(p_socket ps) {
372 int flags = fcntl(*ps, F_GETFL, 0); 374 int flags = fcntl(*ps, F_GETFL, 0);
373 flags |= O_NONBLOCK; 375 flags |= O_NONBLOCK;
374 fcntl(*ps, F_SETFL, flags); 376 fcntl(*ps, F_SETFL, flags);
@@ -377,7 +379,7 @@ void socket_setnonblocking(p_socket ps) {
377/*-------------------------------------------------------------------------*\ 379/*-------------------------------------------------------------------------*\
378* DNS helpers 380* DNS helpers
379\*-------------------------------------------------------------------------*/ 381\*-------------------------------------------------------------------------*/
380int socket_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp) { 382LUASOCKET_PRIVATE int socket_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp) {
381 *hp = gethostbyaddr(addr, len, AF_INET); 383 *hp = gethostbyaddr(addr, len, AF_INET);
382 if (*hp) return IO_DONE; 384 if (*hp) return IO_DONE;
383 else if (h_errno) return h_errno; 385 else if (h_errno) return h_errno;
@@ -385,7 +387,7 @@ int socket_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp) {
385 else return IO_UNKNOWN; 387 else return IO_UNKNOWN;
386} 388}
387 389
388int socket_gethostbyname(const char *addr, struct hostent **hp) { 390LUASOCKET_PRIVATE int socket_gethostbyname(const char *addr, struct hostent **hp) {
389 *hp = gethostbyname(addr); 391 *hp = gethostbyname(addr);
390 if (*hp) return IO_DONE; 392 if (*hp) return IO_DONE;
391 else if (h_errno) return h_errno; 393 else if (h_errno) return h_errno;
@@ -397,7 +399,7 @@ int socket_gethostbyname(const char *addr, struct hostent **hp) {
397* Error translation functions 399* Error translation functions
398* Make sure important error messages are standard 400* Make sure important error messages are standard
399\*-------------------------------------------------------------------------*/ 401\*-------------------------------------------------------------------------*/
400const char *socket_hoststrerror(int err) { 402LUASOCKET_PRIVATE const char *socket_hoststrerror(int err) {
401 if (err <= 0) return io_strerror(err); 403 if (err <= 0) return io_strerror(err);
402 switch (err) { 404 switch (err) {
403 case HOST_NOT_FOUND: return PIE_HOST_NOT_FOUND; 405 case HOST_NOT_FOUND: return PIE_HOST_NOT_FOUND;
@@ -405,7 +407,7 @@ const char *socket_hoststrerror(int err) {
405 } 407 }
406} 408}
407 409
408const char *socket_strerror(int err) { 410LUASOCKET_PRIVATE const char *socket_strerror(int err) {
409 if (err <= 0) return io_strerror(err); 411 if (err <= 0) return io_strerror(err);
410 switch (err) { 412 switch (err) {
411 case EADDRINUSE: return PIE_ADDRINUSE; 413 case EADDRINUSE: return PIE_ADDRINUSE;
@@ -421,12 +423,12 @@ const char *socket_strerror(int err) {
421 } 423 }
422} 424}
423 425
424const char *socket_ioerror(p_socket ps, int err) { 426LUASOCKET_PRIVATE const char *socket_ioerror(p_socket ps, int err) {
425 (void) ps; 427 (void) ps;
426 return socket_strerror(err); 428 return socket_strerror(err);
427} 429}
428 430
429const char *socket_gaistrerror(int err) { 431LUASOCKET_PRIVATE const char *socket_gaistrerror(int err) {
430 if (err == 0) return NULL; 432 if (err == 0) return NULL;
431 switch (err) { 433 switch (err) {
432 case EAI_AGAIN: return PIE_AGAIN; 434 case EAI_AGAIN: return PIE_AGAIN;