diff options
| author | E. Westbrook <github@westbrook.io> | 2019-02-25 16:04:29 -0700 |
|---|---|---|
| committer | E. Westbrook <github@westbrook.io> | 2019-02-25 16:04:29 -0700 |
| commit | f8183bab875f541af3932fe4e430fcff8bd8aba0 (patch) | |
| tree | dc8ae2b2eab799e26f0fe4dfd279caaef7d00d72 | |
| parent | d7ffc2f4e69ff24a88f00456cd9a538ecc90d14c (diff) | |
| download | luasocket-f8183bab875f541af3932fe4e430fcff8bd8aba0.tar.gz luasocket-f8183bab875f541af3932fe4e430fcff8bd8aba0.tar.bz2 luasocket-f8183bab875f541af3932fe4e430fcff8bd8aba0.zip | |
usocket.c: use LUASOCKET_PRIVATE
| -rwxr-xr-x | src/wsocket.c | 50 |
1 files changed, 26 insertions, 24 deletions
diff --git a/src/wsocket.c b/src/wsocket.c index ac8411f..c281058 100755 --- a/src/wsocket.c +++ b/src/wsocket.c | |||
| @@ -5,6 +5,8 @@ | |||
| 5 | * The penalty of calling select to avoid busy-wait is only paid when | 5 | * The penalty of calling select to avoid busy-wait is only paid when |
| 6 | * the I/O call fail in the first place. | 6 | * the I/O call fail in the first place. |
| 7 | \*=========================================================================*/ | 7 | \*=========================================================================*/ |
| 8 | #include "luasocket.h" | ||
| 9 | |||
| 8 | #include <string.h> | 10 | #include <string.h> |
| 9 | 11 | ||
| 10 | #include "socket.h" | 12 | #include "socket.h" |
| @@ -16,7 +18,7 @@ static const char *wstrerror(int err); | |||
| 16 | /*-------------------------------------------------------------------------*\ | 18 | /*-------------------------------------------------------------------------*\ |
| 17 | * Initializes module | 19 | * Initializes module |
| 18 | \*-------------------------------------------------------------------------*/ | 20 | \*-------------------------------------------------------------------------*/ |
| 19 | int socket_open(void) { | 21 | LUASOCKET_PRIVATE int socket_open(void) { |
| 20 | WSADATA wsaData; | 22 | WSADATA wsaData; |
| 21 | WORD wVersionRequested = MAKEWORD(2, 0); | 23 | WORD wVersionRequested = MAKEWORD(2, 0); |
| 22 | int err = WSAStartup(wVersionRequested, &wsaData ); | 24 | int err = WSAStartup(wVersionRequested, &wsaData ); |
| @@ -32,7 +34,7 @@ int socket_open(void) { | |||
| 32 | /*-------------------------------------------------------------------------*\ | 34 | /*-------------------------------------------------------------------------*\ |
| 33 | * Close module | 35 | * Close module |
| 34 | \*-------------------------------------------------------------------------*/ | 36 | \*-------------------------------------------------------------------------*/ |
| 35 | int socket_close(void) { | 37 | LUASOCKET_PRIVATE int socket_close(void) { |
| 36 | WSACleanup(); | 38 | WSACleanup(); |
| 37 | return 1; | 39 | return 1; |
| 38 | } | 40 | } |
| @@ -45,7 +47,7 @@ int socket_close(void) { | |||
| 45 | #define WAITFD_E 4 | 47 | #define WAITFD_E 4 |
| 46 | #define WAITFD_C (WAITFD_E|WAITFD_W) | 48 | #define WAITFD_C (WAITFD_E|WAITFD_W) |
| 47 | 49 | ||
| 48 | int socket_waitfd(p_socket ps, int sw, p_timeout tm) { | 50 | LUASOCKET_PRIVATE int socket_waitfd(p_socket ps, int sw, p_timeout tm) { |
| 49 | int ret; | 51 | int ret; |
| 50 | fd_set rfds, wfds, efds, *rp = NULL, *wp = NULL, *ep = NULL; | 52 | fd_set rfds, wfds, efds, *rp = NULL, *wp = NULL, *ep = NULL; |
| 51 | struct timeval tv, *tp = NULL; | 53 | struct timeval tv, *tp = NULL; |
| @@ -73,7 +75,7 @@ int socket_waitfd(p_socket ps, int sw, p_timeout tm) { | |||
| 73 | /*-------------------------------------------------------------------------*\ | 75 | /*-------------------------------------------------------------------------*\ |
| 74 | * Select with int timeout in ms | 76 | * Select with int timeout in ms |
| 75 | \*-------------------------------------------------------------------------*/ | 77 | \*-------------------------------------------------------------------------*/ |
| 76 | int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_set *efds, | 78 | LUASOCKET_PRIVATE int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_set *efds, |
| 77 | p_timeout tm) { | 79 | p_timeout tm) { |
| 78 | struct timeval tv; | 80 | struct timeval tv; |
| 79 | double t = timeout_get(tm); | 81 | double t = timeout_get(tm); |
| @@ -88,7 +90,7 @@ int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_set *efds, | |||
| 88 | /*-------------------------------------------------------------------------*\ | 90 | /*-------------------------------------------------------------------------*\ |
| 89 | * Close and inutilize socket | 91 | * Close and inutilize socket |
| 90 | \*-------------------------------------------------------------------------*/ | 92 | \*-------------------------------------------------------------------------*/ |
| 91 | void socket_destroy(p_socket ps) { | 93 | LUASOCKET_PRIVATE void socket_destroy(p_socket ps) { |
| 92 | if (*ps != SOCKET_INVALID) { | 94 | if (*ps != SOCKET_INVALID) { |
| 93 | socket_setblocking(ps); /* close can take a long time on WIN32 */ | 95 | socket_setblocking(ps); /* close can take a long time on WIN32 */ |
| 94 | closesocket(*ps); | 96 | closesocket(*ps); |
| @@ -99,7 +101,7 @@ void socket_destroy(p_socket ps) { | |||
| 99 | /*-------------------------------------------------------------------------*\ | 101 | /*-------------------------------------------------------------------------*\ |
| 100 | * | 102 | * |
| 101 | \*-------------------------------------------------------------------------*/ | 103 | \*-------------------------------------------------------------------------*/ |
| 102 | void socket_shutdown(p_socket ps, int how) { | 104 | LUASOCKET_PRIVATE void socket_shutdown(p_socket ps, int how) { |
| 103 | socket_setblocking(ps); | 105 | socket_setblocking(ps); |
| 104 | shutdown(*ps, how); | 106 | shutdown(*ps, how); |
| 105 | socket_setnonblocking(ps); | 107 | socket_setnonblocking(ps); |
| @@ -108,7 +110,7 @@ void socket_shutdown(p_socket ps, int how) { | |||
| 108 | /*-------------------------------------------------------------------------*\ | 110 | /*-------------------------------------------------------------------------*\ |
| 109 | * Creates and sets up a socket | 111 | * Creates and sets up a socket |
| 110 | \*-------------------------------------------------------------------------*/ | 112 | \*-------------------------------------------------------------------------*/ |
| 111 | int socket_create(p_socket ps, int domain, int type, int protocol) { | 113 | LUASOCKET_PRIVATE int socket_create(p_socket ps, int domain, int type, int protocol) { |
| 112 | *ps = socket(domain, type, protocol); | 114 | *ps = socket(domain, type, protocol); |
| 113 | if (*ps != SOCKET_INVALID) return IO_DONE; | 115 | if (*ps != SOCKET_INVALID) return IO_DONE; |
| 114 | else return WSAGetLastError(); | 116 | else return WSAGetLastError(); |
| @@ -117,7 +119,7 @@ int socket_create(p_socket ps, int domain, int type, int protocol) { | |||
| 117 | /*-------------------------------------------------------------------------*\ | 119 | /*-------------------------------------------------------------------------*\ |
| 118 | * Connects or returns error message | 120 | * Connects or returns error message |
| 119 | \*-------------------------------------------------------------------------*/ | 121 | \*-------------------------------------------------------------------------*/ |
| 120 | int socket_connect(p_socket ps, SA *addr, socklen_t len, p_timeout tm) { | 122 | LUASOCKET_PRIVATE int socket_connect(p_socket ps, SA *addr, socklen_t len, p_timeout tm) { |
| 121 | int err; | 123 | int err; |
| 122 | /* don't call on closed socket */ | 124 | /* don't call on closed socket */ |
| 123 | if (*ps == SOCKET_INVALID) return IO_CLOSED; | 125 | if (*ps == SOCKET_INVALID) return IO_CLOSED; |
| @@ -146,7 +148,7 @@ int socket_connect(p_socket ps, SA *addr, socklen_t len, p_timeout tm) { | |||
| 146 | /*-------------------------------------------------------------------------*\ | 148 | /*-------------------------------------------------------------------------*\ |
| 147 | * Binds or returns error message | 149 | * Binds or returns error message |
| 148 | \*-------------------------------------------------------------------------*/ | 150 | \*-------------------------------------------------------------------------*/ |
| 149 | int socket_bind(p_socket ps, SA *addr, socklen_t len) { | 151 | LUASOCKET_PRIVATE int socket_bind(p_socket ps, SA *addr, socklen_t len) { |
| 150 | int err = IO_DONE; | 152 | int err = IO_DONE; |
| 151 | socket_setblocking(ps); | 153 | socket_setblocking(ps); |
| 152 | if (bind(*ps, addr, len) < 0) err = WSAGetLastError(); | 154 | if (bind(*ps, addr, len) < 0) err = WSAGetLastError(); |
| @@ -157,7 +159,7 @@ int socket_bind(p_socket ps, SA *addr, socklen_t len) { | |||
| 157 | /*-------------------------------------------------------------------------*\ | 159 | /*-------------------------------------------------------------------------*\ |
| 158 | * | 160 | * |
| 159 | \*-------------------------------------------------------------------------*/ | 161 | \*-------------------------------------------------------------------------*/ |
| 160 | int socket_listen(p_socket ps, int backlog) { | 162 | LUASOCKET_PRIVATE int socket_listen(p_socket ps, int backlog) { |
| 161 | int err = IO_DONE; | 163 | int err = IO_DONE; |
| 162 | socket_setblocking(ps); | 164 | socket_setblocking(ps); |
| 163 | if (listen(*ps, backlog) < 0) err = WSAGetLastError(); | 165 | if (listen(*ps, backlog) < 0) err = WSAGetLastError(); |
| @@ -168,7 +170,7 @@ int socket_listen(p_socket ps, int backlog) { | |||
| 168 | /*-------------------------------------------------------------------------*\ | 170 | /*-------------------------------------------------------------------------*\ |
| 169 | * Accept with timeout | 171 | * Accept with timeout |
| 170 | \*-------------------------------------------------------------------------*/ | 172 | \*-------------------------------------------------------------------------*/ |
| 171 | int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len, | 173 | LUASOCKET_PRIVATE int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len, |
| 172 | p_timeout tm) { | 174 | p_timeout tm) { |
| 173 | if (*ps == SOCKET_INVALID) return IO_CLOSED; | 175 | if (*ps == SOCKET_INVALID) return IO_CLOSED; |
| 174 | for ( ;; ) { | 176 | for ( ;; ) { |
| @@ -190,7 +192,7 @@ int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len, | |||
| 190 | * this can take an awful lot of time and we will end up blocked. | 192 | * this can take an awful lot of time and we will end up blocked. |
| 191 | * Therefore, whoever calls this function should not pass a huge buffer. | 193 | * Therefore, whoever calls this function should not pass a huge buffer. |
| 192 | \*-------------------------------------------------------------------------*/ | 194 | \*-------------------------------------------------------------------------*/ |
| 193 | int socket_send(p_socket ps, const char *data, size_t count, | 195 | LUASOCKET_PRIVATE int socket_send(p_socket ps, const char *data, size_t count, |
| 194 | size_t *sent, p_timeout tm) | 196 | size_t *sent, p_timeout tm) |
| 195 | { | 197 | { |
| 196 | int err; | 198 | int err; |
| @@ -218,7 +220,7 @@ int socket_send(p_socket ps, const char *data, size_t count, | |||
| 218 | /*-------------------------------------------------------------------------*\ | 220 | /*-------------------------------------------------------------------------*\ |
| 219 | * Sendto with timeout | 221 | * Sendto with timeout |
| 220 | \*-------------------------------------------------------------------------*/ | 222 | \*-------------------------------------------------------------------------*/ |
| 221 | int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent, | 223 | LUASOCKET_PRIVATE int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent, |
| 222 | SA *addr, socklen_t len, p_timeout tm) | 224 | SA *addr, socklen_t len, p_timeout tm) |
| 223 | { | 225 | { |
| 224 | int err; | 226 | int err; |
| @@ -239,7 +241,7 @@ int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent, | |||
| 239 | /*-------------------------------------------------------------------------*\ | 241 | /*-------------------------------------------------------------------------*\ |
| 240 | * Receive with timeout | 242 | * Receive with timeout |
| 241 | \*-------------------------------------------------------------------------*/ | 243 | \*-------------------------------------------------------------------------*/ |
| 242 | int socket_recv(p_socket ps, char *data, size_t count, size_t *got, | 244 | LUASOCKET_PRIVATE int socket_recv(p_socket ps, char *data, size_t count, size_t *got, |
| 243 | p_timeout tm) | 245 | p_timeout tm) |
| 244 | { | 246 | { |
| 245 | int err, prev = IO_DONE; | 247 | int err, prev = IO_DONE; |
| @@ -268,7 +270,7 @@ int socket_recv(p_socket ps, char *data, size_t count, size_t *got, | |||
| 268 | /*-------------------------------------------------------------------------*\ | 270 | /*-------------------------------------------------------------------------*\ |
| 269 | * Recvfrom with timeout | 271 | * Recvfrom with timeout |
| 270 | \*-------------------------------------------------------------------------*/ | 272 | \*-------------------------------------------------------------------------*/ |
| 271 | int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got, | 273 | LUASOCKET_PRIVATE int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got, |
| 272 | SA *addr, socklen_t *len, p_timeout tm) | 274 | SA *addr, socklen_t *len, p_timeout tm) |
| 273 | { | 275 | { |
| 274 | int err, prev = IO_DONE; | 276 | int err, prev = IO_DONE; |
| @@ -297,7 +299,7 @@ int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got, | |||
| 297 | /*-------------------------------------------------------------------------*\ | 299 | /*-------------------------------------------------------------------------*\ |
| 298 | * Put socket into blocking mode | 300 | * Put socket into blocking mode |
| 299 | \*-------------------------------------------------------------------------*/ | 301 | \*-------------------------------------------------------------------------*/ |
| 300 | void socket_setblocking(p_socket ps) { | 302 | LUASOCKET_PRIVATE void socket_setblocking(p_socket ps) { |
| 301 | u_long argp = 0; | 303 | u_long argp = 0; |
| 302 | ioctlsocket(*ps, FIONBIO, &argp); | 304 | ioctlsocket(*ps, FIONBIO, &argp); |
| 303 | } | 305 | } |
| @@ -305,7 +307,7 @@ void socket_setblocking(p_socket ps) { | |||
| 305 | /*-------------------------------------------------------------------------*\ | 307 | /*-------------------------------------------------------------------------*\ |
| 306 | * Put socket into non-blocking mode | 308 | * Put socket into non-blocking mode |
| 307 | \*-------------------------------------------------------------------------*/ | 309 | \*-------------------------------------------------------------------------*/ |
| 308 | void socket_setnonblocking(p_socket ps) { | 310 | LUASOCKET_PRIVATE void socket_setnonblocking(p_socket ps) { |
| 309 | u_long argp = 1; | 311 | u_long argp = 1; |
| 310 | ioctlsocket(*ps, FIONBIO, &argp); | 312 | ioctlsocket(*ps, FIONBIO, &argp); |
| 311 | } | 313 | } |
| @@ -313,13 +315,13 @@ void socket_setnonblocking(p_socket ps) { | |||
| 313 | /*-------------------------------------------------------------------------*\ | 315 | /*-------------------------------------------------------------------------*\ |
| 314 | * DNS helpers | 316 | * DNS helpers |
| 315 | \*-------------------------------------------------------------------------*/ | 317 | \*-------------------------------------------------------------------------*/ |
| 316 | int socket_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp) { | 318 | LUASOCKET_PRIVATE int socket_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp) { |
| 317 | *hp = gethostbyaddr(addr, len, AF_INET); | 319 | *hp = gethostbyaddr(addr, len, AF_INET); |
| 318 | if (*hp) return IO_DONE; | 320 | if (*hp) return IO_DONE; |
| 319 | else return WSAGetLastError(); | 321 | else return WSAGetLastError(); |
| 320 | } | 322 | } |
| 321 | 323 | ||
| 322 | int socket_gethostbyname(const char *addr, struct hostent **hp) { | 324 | LUASOCKET_PRIVATE int socket_gethostbyname(const char *addr, struct hostent **hp) { |
| 323 | *hp = gethostbyname(addr); | 325 | *hp = gethostbyname(addr); |
| 324 | if (*hp) return IO_DONE; | 326 | if (*hp) return IO_DONE; |
| 325 | else return WSAGetLastError(); | 327 | else return WSAGetLastError(); |
| @@ -328,7 +330,7 @@ int socket_gethostbyname(const char *addr, struct hostent **hp) { | |||
| 328 | /*-------------------------------------------------------------------------*\ | 330 | /*-------------------------------------------------------------------------*\ |
| 329 | * Error translation functions | 331 | * Error translation functions |
| 330 | \*-------------------------------------------------------------------------*/ | 332 | \*-------------------------------------------------------------------------*/ |
| 331 | const char *socket_hoststrerror(int err) { | 333 | LUASOCKET_PRIVATE const char *socket_hoststrerror(int err) { |
| 332 | if (err <= 0) return io_strerror(err); | 334 | if (err <= 0) return io_strerror(err); |
| 333 | switch (err) { | 335 | switch (err) { |
| 334 | case WSAHOST_NOT_FOUND: return PIE_HOST_NOT_FOUND; | 336 | case WSAHOST_NOT_FOUND: return PIE_HOST_NOT_FOUND; |
| @@ -336,7 +338,7 @@ const char *socket_hoststrerror(int err) { | |||
| 336 | } | 338 | } |
| 337 | } | 339 | } |
| 338 | 340 | ||
| 339 | const char *socket_strerror(int err) { | 341 | LUASOCKET_PRIVATE const char *socket_strerror(int err) { |
| 340 | if (err <= 0) return io_strerror(err); | 342 | if (err <= 0) return io_strerror(err); |
| 341 | switch (err) { | 343 | switch (err) { |
| 342 | case WSAEADDRINUSE: return PIE_ADDRINUSE; | 344 | case WSAEADDRINUSE: return PIE_ADDRINUSE; |
| @@ -350,12 +352,12 @@ const char *socket_strerror(int err) { | |||
| 350 | } | 352 | } |
| 351 | } | 353 | } |
| 352 | 354 | ||
| 353 | const char *socket_ioerror(p_socket ps, int err) { | 355 | LUASOCKET_PRIVATE const char *socket_ioerror(p_socket ps, int err) { |
| 354 | (void) ps; | 356 | (void) ps; |
| 355 | return socket_strerror(err); | 357 | return socket_strerror(err); |
| 356 | } | 358 | } |
| 357 | 359 | ||
| 358 | static const char *wstrerror(int err) { | 360 | LUASOCKET_PRIVATE static const char *wstrerror(int err) { |
| 359 | switch (err) { | 361 | switch (err) { |
| 360 | case WSAEINTR: return "Interrupted function call"; | 362 | case WSAEINTR: return "Interrupted function call"; |
| 361 | case WSAEACCES: return PIE_ACCESS; // "Permission denied"; | 363 | case WSAEACCES: return PIE_ACCESS; // "Permission denied"; |
| @@ -404,7 +406,7 @@ static const char *wstrerror(int err) { | |||
| 404 | } | 406 | } |
| 405 | } | 407 | } |
| 406 | 408 | ||
| 407 | const char *socket_gaistrerror(int err) { | 409 | LUASOCKET_PRIVATE const char *socket_gaistrerror(int err) { |
| 408 | if (err == 0) return NULL; | 410 | if (err == 0) return NULL; |
| 409 | switch (err) { | 411 | switch (err) { |
| 410 | case EAI_AGAIN: return PIE_AGAIN; | 412 | case EAI_AGAIN: return PIE_AGAIN; |
