aboutsummaryrefslogtreecommitdiff
path: root/src/wsocket.c
blob: af3f8d8f1103cf2cca8cca4d75b6e721d64c4110 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*=========================================================================*\
* Socket compatibilization module for Win32
* LuaSocket toolkit
*
* We also exchanged the order of the calls to send/recv and select.
* The idea is that the outer loop (whoever is calling sock_send/recv)
* will call the function again if we didn't time out, so we can
* call write and then select only if it fails. This moves the penalty
* to when data is not available, maximizing the bandwidth if data is 
* always available. 
*
* RCS ID: $Id$
\*=========================================================================*/
#include <string.h>

#include "socket.h"

static const char *sock_createstrerror(int err);
static const char *sock_bindstrerror(int err);
static const char *sock_connectstrerror(int err);
static const char *sock_acceptstrerror(int err);
static const char *sock_listenstrerror(int err);

/*-------------------------------------------------------------------------*\
* Initializes module 
\*-------------------------------------------------------------------------*/
int sock_open(void)
{
    WSADATA wsaData;
    WORD wVersionRequested = MAKEWORD(2, 0); 
    int err = WSAStartup(wVersionRequested, &wsaData );
    if (err != 0) return 0;
    if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 0) {
        WSACleanup();
        return 0; 
    }
    return 1;
}

/*-------------------------------------------------------------------------*\
* Select with int timeout in ms
\*-------------------------------------------------------------------------*/
int sock_select(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, int timeout)
{
    struct timeval tv;
    tv.tv_sec = timeout / 1000;
    tv.tv_usec = (timeout % 1000) * 1000;
    return select(n, rfds, wfds, efds, timeout >= 0? &tv: NULL);
}

/*-------------------------------------------------------------------------*\
* Close and inutilize socket
\*-------------------------------------------------------------------------*/
void sock_destroy(p_sock ps)
{
    if (*ps != SOCK_INVALID) {
        sock_setblocking(ps); /* close can take a long time on WIN32 */
        closesocket(*ps);
        *ps = SOCK_INVALID;
    }
}

/*-------------------------------------------------------------------------*\
* 
\*-------------------------------------------------------------------------*/
void sock_shutdown(p_sock ps, int how)
{
    sock_setblocking(ps);
    shutdown(*ps, how);
    sock_setnonblocking(ps);
}

/*-------------------------------------------------------------------------*\
* Creates and sets up a socket
\*-------------------------------------------------------------------------*/
const char *sock_create(p_sock ps, int domain, int type, int protocol)
{
    t_sock sock = socket(domain, type, protocol);
    if (sock == SOCK_INVALID) 
        return sock_createstrerror(WSAGetLastError());
    *ps = sock;
    sock_setnonblocking(ps);
    return NULL;
}

/*-------------------------------------------------------------------------*\
* Connects or returns error message
\*-------------------------------------------------------------------------*/
const char *sock_connect(p_sock ps, SA *addr, socklen_t addr_len, p_tm tm)
{
    t_sock sock = *ps;
    int err, timeout = tm_getretry(tm);
    fd_set efds, wfds;
    /* don't call on closed socket */
    if (sock == SOCK_INVALID) return io_strerror(IO_CLOSED);
    /* ask system to connect */
    err = connect(sock, addr, addr_len);
    /* if no error, we're done */
    if (err == 0) return NULL;
    /* make sure the system is trying to connect */
    err = WSAGetLastError(); 
    if (err != WSAEWOULDBLOCK) return sock_connectstrerror(err);
    /* wait for a timeout or for the system's answer */
    FD_ZERO(&wfds); FD_SET(sock, &wfds);
    FD_ZERO(&efds); FD_SET(sock, &efds);
    /* we run select to wait */
    err = sock_select(0, NULL, &wfds, &efds, timeout);
    /* if select returned due to an event */
    if (err > 0 ) {
        /* if was in efds, we failed */
        if (FD_ISSET(sock, &efds)) {
            int why, len = sizeof(why);
            /* give windows time to set the error (disgusting) */
            Sleep(0);
            /* find out why we failed */
            getsockopt(sock, SOL_SOCKET, SO_ERROR, (char *)&why, &len); 
            /* we KNOW there was an error. if why is 0, we will return
             * "unknown error", but it's not really our fault */
            return sock_connectstrerror(why); 
        /* otherwise it must be in wfds, so we succeeded */
        } else return NULL;
    /* if no event happened, we timed out */
    } else return io_strerror(IO_TIMEOUT);
}

/*-------------------------------------------------------------------------*\
* Binds or returns error message
\*-------------------------------------------------------------------------*/
const char *sock_bind(p_sock ps, SA *addr, socklen_t addr_len)
{
    const char *err = NULL;
    sock_setblocking(ps);
    if (bind(*ps, addr, addr_len) < 0) 
        err = sock_bindstrerror(WSAGetLastError());
    sock_setnonblocking(ps);
    return err;
}

/*-------------------------------------------------------------------------*\
* 
\*-------------------------------------------------------------------------*/
const char *sock_listen(p_sock ps, int backlog)
{
    const char *err = NULL;
    sock_setblocking(ps);
    if (listen(*ps, backlog) < 0) 
        err = sock_listenstrerror(WSAGetLastError());
    sock_setnonblocking(ps);
    return err;
}

/*-------------------------------------------------------------------------*\
* Accept with timeout
\*-------------------------------------------------------------------------*/
const char *sock_accept(p_sock ps, p_sock pa, SA *addr, 
        socklen_t *addr_len, p_tm tm)
{
    t_sock sock = *ps;
    SA dummy_addr;
    socklen_t dummy_len = sizeof(dummy_addr);
    if (sock == SOCK_INVALID) return io_strerror(IO_CLOSED);
    if (!addr) addr = &dummy_addr;
    if (!addr_len) addr_len = &dummy_len;
    for (;;) {
        fd_set rfds;
        int timeout = tm_getretry(tm);
        int err;
        /* try to get client socket */
        *pa = accept(sock, addr, addr_len);
        /* if return is valid, we are done */
        if (*pa != SOCK_INVALID) return NULL;
        /* optimization */
        if (timeout == 0) return io_strerror(IO_TIMEOUT);
        /* otherwise find out why we failed */
        err = WSAGetLastError(); 
        /* if we failed because there was no connectoin, keep trying*/
        if (err != WSAEWOULDBLOCK) return sock_acceptstrerror(err);
        /* call select to avoid busy wait */
        FD_ZERO(&rfds);
        FD_SET(sock, &rfds);
        err = sock_select(0, &rfds, NULL, NULL, timeout);
        if (err <= 0) return io_strerror(IO_TIMEOUT);
    } 
    return io_strerror(IO_TIMEOUT); /* can't get here */
}

/*-------------------------------------------------------------------------*\
* Send with timeout
\*-------------------------------------------------------------------------*/
int sock_send(p_sock ps, const char *data, size_t count, size_t *sent, 
        int timeout)
{
    t_sock sock = *ps;
    int put;
    int ret;
    /* avoid making system calls on closed sockets */
    if (sock == SOCK_INVALID) return IO_CLOSED;
    /* try to send something */
    put = send(sock, data, (int) count, 0);
    /* deal with failure */
    if (put <= 0) {
        /* in any case, nothing has been sent */
        *sent = 0;
        /* run select to avoid busy wait */
        if (WSAGetLastError() == WSAEWOULDBLOCK) {
            fd_set fds;
            FD_ZERO(&fds);
            FD_SET(sock, &fds);
            ret = sock_select(0, NULL, &fds, NULL, timeout);
            /* tell the caller to call us again because there is more data */
            if (ret > 0) return IO_DONE;
            /* tell the caller there was no data before timeout */
            else return IO_TIMEOUT;
        /* here we know the connection has been closed */
        } else return IO_CLOSED;
    /* here we successfully sent something */
    } else {
        *sent = put;
        return IO_DONE;
    }
}

/*-------------------------------------------------------------------------*\
* Sendto with timeout
\*-------------------------------------------------------------------------*/
int sock_sendto(p_sock ps, const char *data, size_t count, size_t *sent, 
        SA *addr, socklen_t addr_len, int timeout)
{
    t_sock sock = *ps;
    int put;
    int ret;
    /* avoid making system calls on closed sockets */
    if (sock == SOCK_INVALID) return IO_CLOSED;
    /* try to send something */
    put = sendto(sock, data, (int) count, 0, addr, addr_len);
    /* deal with failure */
    if (put <= 0) {
        /* in any case, nothing has been sent */
        *sent = 0;
        /* run select to avoid busy wait */
        if (WSAGetLastError() == WSAEWOULDBLOCK) {
            fd_set fds;
            FD_ZERO(&fds);
            FD_SET(sock, &fds);
            ret = sock_select(0, NULL, &fds, NULL, timeout);
            /* tell the caller to call us again because there is more data */
            if (ret > 0) return IO_DONE;
            /* tell the caller there was no data before timeout */
            else return IO_TIMEOUT;
        /* here we know the connection has been closed */
        } else return IO_CLOSED;
    /* here we successfully sent something */
    } else {
        *sent = put;
        return IO_DONE;
    }
}

/*-------------------------------------------------------------------------*\
* Receive with timeout
\*-------------------------------------------------------------------------*/
int sock_recv(p_sock ps, char *data, size_t count, size_t *got, int timeout)
{
    t_sock sock = *ps;
    int taken;
    if (sock == SOCK_INVALID) return IO_CLOSED;
    taken = recv(sock, data, (int) count, 0);
    if (taken <= 0) {
        fd_set fds;
        int ret;
        *got = 0;
        if (taken == 0 || WSAGetLastError() != WSAEWOULDBLOCK) return IO_CLOSED;
        FD_ZERO(&fds);
        FD_SET(sock, &fds);
        ret = sock_select(0, &fds, NULL, NULL, timeout);
        if (ret > 0) return IO_DONE;
        else return IO_TIMEOUT;
    } else {
        *got = taken;
        return IO_DONE;
    }
}

/*-------------------------------------------------------------------------*\
* Recvfrom with timeout
\*-------------------------------------------------------------------------*/
int sock_recvfrom(p_sock ps, char *data, size_t count, size_t *got, 
        SA *addr, socklen_t *addr_len, int timeout)
{
    t_sock sock = *ps;
    int taken;
    if (sock == SOCK_INVALID) return IO_CLOSED;
    taken = recvfrom(sock, data, (int) count, 0, addr, addr_len);
    if (taken <= 0) {
        fd_set fds;
        int ret;
        *got = 0;
        if (taken == 0 || WSAGetLastError() != WSAEWOULDBLOCK) return IO_CLOSED;
        FD_ZERO(&fds);
        FD_SET(sock, &fds);
        ret = sock_select(0, &fds, NULL, NULL, timeout);
        if (ret > 0) return IO_DONE;
        else return IO_TIMEOUT;
    } else {
        *got = taken;
        return IO_DONE;
    }
}

/*-------------------------------------------------------------------------*\
* Put socket into blocking mode
\*-------------------------------------------------------------------------*/
void sock_setblocking(p_sock ps)
{
    u_long argp = 0;
    ioctlsocket(*ps, FIONBIO, &argp);
}

/*-------------------------------------------------------------------------*\
* Put socket into non-blocking mode
\*-------------------------------------------------------------------------*/
void sock_setnonblocking(p_sock ps)
{
    u_long argp = 1;
    ioctlsocket(*ps, FIONBIO, &argp);
}

/*-------------------------------------------------------------------------*\
* Error translation functions
\*-------------------------------------------------------------------------*/
/* return error messages for the known errors reported by gethostbyname */
const char *sock_hoststrerror(void)
{
    switch (WSAGetLastError()) {
        case WSANOTINITIALISED: return "not initialized";
        case WSAENETDOWN: return "network is down";
        case WSAHOST_NOT_FOUND: return "host not found";
        case WSATRY_AGAIN: return "name server unavailable, try again later";
        case WSANO_RECOVERY: return "name server error";
        case WSANO_DATA: return "host not found";
        case WSAEINPROGRESS: return "another call in progress";
        case WSAEFAULT: return "invalid memory address";
        case WSAEINTR: return "call interrupted";
        default: return "unknown error";
    }
}

/* return error messages for the known errors reported by socket */
static const char *sock_createstrerror(int err)
{
    switch (err) {
        case WSANOTINITIALISED: return "not initialized";
        case WSAENETDOWN: return "network is down";
        case WSAEAFNOSUPPORT: return "address family not supported";
        case WSAEINPROGRESS: return "another call in progress";
        case WSAEMFILE: return "descriptor table is full";
        case WSAENOBUFS: return "insufficient buffer space";
        case WSAEPROTONOSUPPORT: return "protocol not supported";
        case WSAEPROTOTYPE: return "wrong protocol type";
        case WSAESOCKTNOSUPPORT: return "socket type not supported by family";
        default: return "unknown error";
    }
}

/* return error messages for the known errors reported by accept */
static const char *sock_acceptstrerror(int err)
{
    switch (err) {
        case WSANOTINITIALISED: return "not initialized";
        case WSAENETDOWN: return "network is down";
        case WSAEFAULT: return "invalid memory address";
        case WSAEINTR: return "call interrupted";
        case WSAEINPROGRESS: return "another call in progress";
        case WSAEINVAL: return "not listening";
        case WSAEMFILE: return "descriptor table is full";
        case WSAENOBUFS: return "insufficient buffer space";
        case WSAENOTSOCK: return "descriptor not a socket";
        case WSAEOPNOTSUPP: return "not supported";
        case WSAEWOULDBLOCK: return "call would block";
        default: return "unknown error";
    }
}

/* return error messages for the known errors reported by bind */
static const char *sock_bindstrerror(int err)
{
    switch (err) {
        case WSANOTINITIALISED: return "not initialized";
        case WSAENETDOWN: return "network is down";
        case WSAEACCES: return "broadcast not enabled for socket";
        case WSAEADDRINUSE: return "address already in use";
        case WSAEADDRNOTAVAIL: return "address not available in local host";
        case WSAEFAULT: return "invalid memory address";
        case WSAEINPROGRESS: return "another call in progress";
        case WSAEINVAL: return "already bound";
        case WSAENOBUFS: return "insuficient buffer space";
        case WSAENOTSOCK: return "descriptor not a socket";
        default: return "unknown error";
    }
    
}

/* return error messages for the known errors reported by listen */
static const char *sock_listenstrerror(int err)
{
    switch (err) {
        case WSANOTINITIALISED: return "not initialized";
        case WSAENETDOWN: return "network is down";
        case WSAEADDRINUSE: return "local address already in use";
        case WSAEINPROGRESS: return "another call in progress";
        case WSAEINVAL: return "not bound";
        case WSAEISCONN: return "already connected";
        case WSAEMFILE: return "descriptor table is full";
        case WSAENOBUFS: return "insuficient buffer space";
        case WSAENOTSOCK: return "descriptor not a socket";
        case WSAEOPNOTSUPP: return "not supported";
        default: return "unknown error";
    }
}

/* return error messages for the known errors reported by connect */
static const char *sock_connectstrerror(int err)
{
    switch (err) {
        case WSANOTINITIALISED: return "not initialized";
        case WSAENETDOWN: return "network is down";
        case WSAEADDRINUSE: return "local address already in use";
        case WSAEINTR: return "call interrupted";
        case WSAEINPROGRESS: return "another call in progress";
        case WSAEALREADY: return "connect already in progress";
        case WSAEADDRNOTAVAIL: return "invalid remote address";
        case WSAEAFNOSUPPORT: return "address family not supported";
        case WSAECONNREFUSED: return "connection refused";
        case WSAEFAULT: return "invalid memory address";
        case WSAEINVAL: return "socket is listening";
        case WSAEISCONN: return "socket already connected";
        case WSAENETUNREACH: return "network is unreachable";
        case WSAENOTSOCK: return "descriptor not a socket";
        case WSAETIMEDOUT: return io_strerror(IO_TIMEOUT);
        case WSAEWOULDBLOCK: return "would block";
        case WSAEACCES: return "broadcast not enabled";
        default: return "unknown error";
    }
}