-
Notifications
You must be signed in to change notification settings - Fork 3
/
socket.c
1728 lines (1568 loc) · 42.4 KB
/
socket.c
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "compat.h"
#include <lua.h>
#include <lauxlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/un.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <poll.h>
#include <signal.h>
#include "timeout.h"
#include "buffer.h"
#define _VERSION "0.0.1"
#define TCPSOCK_TYPENAME "TCPSOCKET*"
#define UDPSOCK_TYPENAME "UDPSOCKET*"
/* Socket address */
typedef union {
struct sockaddr sa;
struct sockaddr_in in;
struct sockaddr_un un;
} sockaddr_t;
/* Convert "sockaddr_t" to "struct sockaddr *". */
#define SAS2SA(x) (&((x)->sa))
/* Socket Object */
struct sockobj {
int fd;
int sock_family;
double sock_timeout; /* in seconds */
struct buffer *buf; /* used for buffer reading */
};
#define getsockobj(L) ((struct sockobj *)lua_touserdata(L, 1));
#define CHECK_ERRNO(expected) (errno == expected)
/* Custom socket error strings */
#define ERROR_TIMEOUT "Operation timed out"
#define ERROR_CLOSED "Connection closed"
#define ERROR_REFUSED "Connection refused"
/* Options */
#define OPT_TCP_NODELAY "tcp_nodelay"
#define OPT_TCP_KEEPALIVE "tcp_keepalive"
#define OPT_TCP_REUSEADDR "tcp_reuseaddr"
#define RECV_BUFSIZE 8192
/**
* Function to perform the setting of socket blocking mode.
*/
static void
__setblocking(int fd, int block)
{
int flags = fcntl(fd, F_GETFL, 0);
if (block) {
flags &= (~O_NONBLOCK);
} else {
flags |= O_NONBLOCK;
}
fcntl(fd, F_SETFL, flags);
}
/**
* Do a event polling on the socket, if necessary (sock_timeout > 0).
*
* Returns:
* 1 on timeout
* -1 on error
* 0 success
*/
#define EVENT_NONE 0
#define EVENT_READABLE POLLIN
#define EVENT_WRITABLE POLLOUT
#define EVENT_ANY (POLLIN | POLLOUT)
static int
__waitfd(struct sockobj *s, int event, struct timeout *tm)
{
int ret;
// Nothing to do if socket is closed.
if (s->fd < 0)
return 0;
struct pollfd pollfd;
pollfd.fd = s->fd;
pollfd.events = event;
do {
// Handling this condition here simplifies the loops.
double left = timeout_left(tm);
if (left == 0.0)
return 1;
int timeout = (int)(left * 1e3);
ret = poll(&pollfd, 1, timeout >= 0 ? timeout : -1);
} while (ret == -1 && CHECK_ERRNO(EINTR));
if (ret < 0) {
return -1;
} else if (ret == 0) {
return 1;
} else {
return 0;
}
}
int
__select(int nfds, fd_set * readfds, fd_set * writefds, fd_set * errorfds,
struct timeout *tm)
{
int ret;
do {
struct timeval tv = { 0, 0 };
double t = timeout_left(tm);
if (t >= 0) {
tv.tv_sec = (int)t;
tv.tv_usec = (int)((t - tv.tv_sec) * 1.0e6);
}
ret = select(nfds, readfds, writefds, errorfds, (t >= 0) ? &tv : NULL);
} while (ret < 0 && errno == EINTR);
return ret;
}
/**
* Get the address length according to the socket object's address family.
* Return 1 if the family is known, 0 otherwise. The length is returned through
* len_ret.
*/
static int
__getsockaddrlen(struct sockobj *s, socklen_t * len_ret)
{
switch (s->sock_family) {
case AF_UNIX:
*len_ret = sizeof(struct sockaddr_un);
return 1;
case AF_INET:
*len_ret = sizeof(struct sockaddr_in);
return 1;
case AF_INET6:
*len_ret = sizeof(struct sockaddr_in6);
return 1;
default:
return 0;
}
}
/*
* Convert a string specifying a host name or one of a few symbolic names to a
* numeric IP address.
*/
static int
__sockobj_setipaddr(lua_State *L, const char *name, struct sockaddr *addr_ret, size_t addr_ret_size, int af)
{
struct addrinfo hints, *res;
int err;
int d1, d2, d3, d4;
char ch;
memset((void *)addr_ret, 0, addr_ret_size);
if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4
&& 0 <= d1 && d1 <= 255
&& 0 <= d2 && d2 <= 255
&& 0 <= d3 && d3 <= 255
&& 0 <= d4 && d4 <= 255) {
struct sockaddr_in *sin;
sin = (struct sockaddr_in *)addr_ret;
sin->sin_addr.s_addr = htonl(((long)d1 << 24) | ((long)d2 << 16) | ((long)d3 << 8) | ((long)d4 << 0));
sin->sin_family = AF_INET;
return 0;
}
memset(&hints, 0, sizeof(hints));
hints.ai_family = af;
err = getaddrinfo(name, NULL, &hints, &res);
if (err) {
lua_pushnil(L);
lua_pushstring(L, gai_strerror(errno));
return -1;
}
if (res->ai_addrlen < addr_ret_size)
addr_ret_size = res->ai_addrlen;
memcpy((char *)addr_ret, res->ai_addr, addr_ret_size);
freeaddrinfo(res);
return 0;
}
/**
* Parse socket address arguments.
*
* Socket addresses are represented as follows:
* - A single string is used for the AF_UNIX address family.
* - Two arguments (host, port) is used for the AF_INET address family,
* where host is a string representing either a hostname in Internet Domain
* Notation like 'www.example.com' or an IPv4 address like '8.8.8.8', and port
* is an number.
* If you use a hostname in the host portion of IPv4/IPv6 socket address, the
* program may show a nondeterministic behavior, as we use the first address
* returned from the DNS resolution. The socket address will be resolved
* differently into an actual IPv4/v6 address, depending on the results from DNS
* resolution and/or the host configuration. For deterministic behavior use a
* numeric address in host portion.
*
* This method assumed that address arguments start after offset index.
*
* Returns 0 on success, -1 on failure.
*/
static int
__sockobj_getaddrfromarg(lua_State * L, struct sockobj *s, struct sockaddr *addr_ret,
socklen_t * len_ret, int offset)
{
int n;
n = lua_gettop(L);
if (n != 1 + offset && n != 2 + offset) {
lua_pushnil(L);
lua_pushfstring(L, "expecting %d or %d arguments"
" (including the object itself), but seen %d", offset + 1, offset + 2, n);
return -1;
}
if (n == 2 + offset) {
s->sock_family = AF_INET;
} else if (n == 1 + offset) {
s->sock_family = AF_UNIX;
}
if (s->sock_family == AF_INET) {
struct sockaddr_in *addr = (struct sockaddr_in *)addr_ret;
const char *host;
int port;
host = luaL_checkstring(L, 1 + offset);
port = luaL_checknumber(L, 2 + offset);
if (__sockobj_setipaddr(L, host, (struct sockaddr *)addr, sizeof(*addr), AF_INET) != 0) {
return -1;
}
addr->sin_family = AF_INET;
addr->sin_port = htons(port);
*len_ret = sizeof(*addr);
} else if (s->sock_family == AF_UNIX) {
struct sockaddr_un *addr = (struct sockaddr_un *)addr_ret;
const char *path = luaL_checkstring(L, 1 + offset);
addr->sun_family = AF_UNIX;
strncpy(addr->sun_path, path, sizeof(addr->sun_path) - 1);
*len_ret = sizeof(*addr);
} else {
assert(0);
}
return 0;
}
/**
* Create a table, push address info into it.
*
* The family field os the socket object is inspected to determine what kind of
* address it really is.
*
* In case of success, a table associated with address info pushed on the stack;
* In case of error, a nil value with a string describing the error pushed on
* the stack.
*/
static int
__sockobj_makeaddr(lua_State * L, struct sockobj *s, struct sockaddr *addr,
socklen_t addrlen)
{
lua_newtable(L);
switch (addr->sa_family) {
case AF_INET:
{
struct sockaddr_in *a = (struct sockaddr_in *)addr;
char buf[NI_MAXHOST];
int err = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0,
NI_NUMERICHOST);
if (err) {
err = errno;
lua_pushnil(L);
lua_pushstring(L, gai_strerror(errno));
return -1;
}
lua_pushnumber(L, 1);
lua_pushstring(L, buf);
lua_settable(L, -3);
lua_pushnumber(L, 2);
lua_pushnumber(L, ntohs(a->sin_port));
lua_settable(L, -3);
return 0;
}
case AF_UNIX:
{
struct sockaddr_un *a = (struct sockaddr_un *)addr;
#ifdef linux
if (a->sun_path[0] == 0) { /* Linux abstract namespace */
addrlen -= offset(struct sockaddr_un, sun_path);
lua_pushlstring(L, a->sun_path, addrlen);
} else
#endif
{
/* regular NULL-terminated string */
lua_pushstring(L, a->sun_path);
}
return 0;
}
default:
/* If we don't know the address family, return it as an {int, bytes}
* table. */
lua_pushnumber(L, 1);
lua_pushnumber(L, addr->sa_family);
lua_settable(L, -3);
lua_pushnumber(L, 2);
lua_pushstring(L, addr->sa_data);
lua_settable(L, -3);
return 0;
}
}
/**
* Generic socket object creation.
*/
struct sockobj *
__sockobj_create(lua_State *L, const char *tname)
{
struct sockobj *s =
(struct sockobj *)lua_newuserdata(L, sizeof(struct sockobj));
if (!s) {
return NULL;
}
s->fd = -1;
s->sock_timeout = -1;
s->sock_family = 0;
s->buf = NULL;
luaL_setmetatable(L, tname);
return s;
}
/**
* Generic socket fd creation.
*/
static int
__sockobj_createsocket(lua_State *L, struct sockobj *s, int type)
{
int fd;
assert(s->fd == -1);
if ((fd = socket(s->sock_family, type, 0)) == -1) {
lua_pushnil(L);
lua_pushfstring(L, "failed to create socket: %s", strerror(errno));
return -1;
}
s->fd = fd;
// 100% non-blocking
__setblocking(s->fd, 0);
return 0;
}
/**
* Close associated socket and buffers.
*/
static int
__sockobj_close(lua_State *L, struct sockobj *s)
{
if (s->fd != -1) {
if (close(s->fd) != 0) {
lua_pushnil(L);
lua_pushstring(L, strerror(errno));
return -1;
}
s->fd = -1;
}
if (s->buf) {
buffer_delete(s->buf);
s->buf = NULL;
}
return 0;
}
/**
* Generic socket connection.
*/
static int
__sockobj_connect(lua_State *L, struct sockobj *s, struct sockaddr *addr, socklen_t len)
{
int ret;
char *errstr = NULL;
struct timeout tm;
timeout_init(&tm, s->sock_timeout);
assert(s->fd > 0);
errno = 0;
ret = connect(s->fd, addr, len);
if (CHECK_ERRNO(EINPROGRESS)) {
/* Connecting in progress with timeout, wait until we have the result of
* the connection attempt or timeout.
*/
int timeout = __waitfd(s, EVENT_WRITABLE, &tm);
if (timeout == 1) {
errstr = ERROR_TIMEOUT;
goto err;
} else if (timeout == 0) {
// In case of EINPROGRESS, use getsockopt(SO_ERROR) to get the real
// error, when the connection attempt finished.
socklen_t ret_size = sizeof(ret);
getsockopt(s->fd, SOL_SOCKET, SO_ERROR, &ret, &ret_size);
if (ret == EISCONN) {
errno = 0;
} else {
errno = ret;
}
} else {
errstr = strerror(errno);
goto err;
}
}
if (errno) {
errstr = strerror(errno);
goto err;
}
return 0;
err:
assert(errstr);
__sockobj_close(L, s);
lua_pushnil(L);
lua_pushstring(L, errstr);
return -1;
}
static int
__sockobj_send(lua_State *L, struct sockobj *s, const char *buf, size_t len, size_t *sent, struct timeout *tm) {
char *errstr;
if (s->fd == -1) {
errstr = ERROR_CLOSED;
goto err;
}
while (1) {
int timeout = __waitfd(s, EVENT_WRITABLE, tm);
if (timeout == -1) {
errstr = strerror(errno);
goto err;
} else if (timeout == 1) {
errstr = ERROR_TIMEOUT;
goto err;
} else {
int n = send(s->fd, buf, len, 0);
if (n < 0) {
switch (errno) {
case EINTR:
case EAGAIN:
continue;
case EPIPE:
// EPIPE means the connection was closed.
errstr = ERROR_CLOSED;
goto err;
default:
errstr = strerror(errno);
goto err;
}
} else {
*sent = n;
return 0;
}
}
}
err:
assert(errstr);
lua_pushnil(L);
lua_pushstring(L, errstr);
return -1;
}
static int
__sockobj_sendto(lua_State *L, struct sockobj *s, const char *buf, size_t len, size_t *sent, struct sockaddr *addr, socklen_t addrlen, struct timeout *tm) {
char *errstr;
if (s->fd == -1) {
errstr = ERROR_CLOSED;
goto err;
}
while (1) {
int timeout = __waitfd(s, EVENT_WRITABLE, tm);
if (timeout == -1) {
errstr = strerror(errno);
goto err;
} else if (timeout == 1) {
errstr = ERROR_TIMEOUT;
goto err;
} else {
int n = sendto(s->fd, buf, len, 0, addr, addrlen);
if (n < 0) {
switch (errno) {
case EINTR:
case EAGAIN:
continue;
case EPIPE:
// EPIPE means the connection was closed.
errstr = ERROR_CLOSED;
goto err;
default:
errstr = strerror(errno);
goto err;
}
} else {
*sent = n;
return 0;
}
}
}
err:
assert(errstr);
lua_pushnil(L);
lua_pushstring(L, errstr);
return -1;
}
static int
__sockobj_write(lua_State *L, struct sockobj *s, const char *buf, size_t len) {
char *errstr;
size_t total_sent = 0;
if (s->fd == -1) {
errstr = ERROR_CLOSED;
goto err;
}
struct timeout tm;
timeout_init(&tm, s->sock_timeout);
while (1) {
int timeout = __waitfd(s, EVENT_WRITABLE, &tm);
if (timeout == -1) {
errstr = strerror(errno);
goto err;
} else if (timeout == 1) {
errstr = ERROR_TIMEOUT;
goto err;
} else {
int n = send(s->fd, buf + total_sent, len - total_sent, 0);
if (n < 0) {
switch (errno) {
case EINTR:
case EAGAIN:
continue;
case EPIPE:
// EPIPE means the connection was closed.
errstr = ERROR_CLOSED;
goto err;
default:
errstr = strerror(errno);
goto err;
}
} else {
total_sent += n;
if (len - total_sent <= 0) {
break;
}
}
}
}
assert(total_sent == len);
lua_pushinteger(L, total_sent);
return 0;
err:
assert(errstr);
lua_pushnil(L);
lua_pushstring(L, errstr);
return -1;
}
static int
__sockobj_recv(lua_State *L, struct sockobj *s, char *buf, size_t buffersize, size_t *received, struct timeout *tm)
{
char *errstr = NULL;
if (s->fd == -1) {
errstr = ERROR_CLOSED;
goto err;
}
while (1) {
int timeout = __waitfd(s, EVENT_READABLE, tm);
if (timeout == -1) {
errstr = strerror(errno);
goto err;
} else if (timeout == 1) {
errstr = ERROR_TIMEOUT;
goto err;
} else {
int bytes_read = recv(s->fd, buf, buffersize, 0);
if (bytes_read > 0) {
*received = bytes_read;
return 0;
} else if (bytes_read == 0) {
errstr = ERROR_CLOSED;
goto err;
} else {
switch (errno) {
case EINTR:
case EAGAIN:
// do nothing, continue
continue;
default:
errstr = strerror(errno);
goto err;
}
}
}
}
err:
assert(errstr);
lua_pushnil(L);
lua_pushstring(L, errstr);
return -1;
}
static int
__sockobj_recvfrom(lua_State *L, struct sockobj *s, char *buf, size_t buffersize, size_t *received, struct sockaddr *addr, socklen_t *addrlen, struct timeout *tm)
{
char *errstr = NULL;
if (s->fd == -1) {
errstr = ERROR_CLOSED;
goto err;
}
while (1) {
int timeout = __waitfd(s, EVENT_READABLE, tm);
if (timeout == -1) {
errstr = strerror(errno);
goto err;
} else if (timeout == 1) {
errstr = ERROR_TIMEOUT;
goto err;
} else {
int bytes_read = recvfrom(s->fd, buf, buffersize, 0, addr, addrlen);
if (bytes_read > 0) {
*received = bytes_read;
return 0;
} else if (bytes_read == 0) {
errstr = ERROR_CLOSED;
goto err;
} else {
switch (errno) {
case EINTR:
case EAGAIN:
// do nothing, continue
continue;
default:
errstr = strerror(errno);
goto err;
}
}
}
}
err:
assert(errstr);
lua_pushnil(L);
lua_pushstring(L, errstr);
return -1;
}
/**
* tcpsock, err = socket.tcp()
*/
static int
socket_tcp(lua_State * L)
{
struct sockobj *s = __sockobj_create(L, TCPSOCK_TYPENAME);
if (!s) {
return luaL_error(L, "out of memory");
}
return 1;
}
/**
* udpsock, err = socket.udp()
*/
static int
socket_udp(lua_State * L)
{
struct sockobj *s = __sockobj_create(L, UDPSOCK_TYPENAME);
if (!s) {
return luaL_error(L, "out of memory");
}
return 1;
}
static void
__collect_fds(lua_State * L, int tab, fd_set * set, int *max_fd)
{
if (lua_isnil(L, tab))
return;
luaL_checktype(L, tab, LUA_TTABLE);
int i = 1;
while (1) {
int fd = -1;
lua_pushnumber(L, i);
lua_gettable(L, tab); // get ith fd
if (lua_isnil(L, -1)) {
// end of table loop
lua_pop(L, 1);
break;
}
if (lua_isnumber(L, -1)) {
fd = lua_tonumber(L, -1);
if (fd < 0) {
fd = -1;
}
} else {
// ignore
lua_pop(L, 1);
continue;
}
if (fd != -1) {
if (fd >= FD_SETSIZE) {
luaL_argerror(L, tab, "descriptor too large for set size");
}
if (*max_fd < fd) {
*max_fd = fd;
}
FD_SET(fd, set);
}
lua_pop(L, 1);
i++;
}
}
static void
__return_fd(lua_State * L, fd_set * set, int max_fd)
{
int fd;
int i = 1;
lua_newtable(L);
for (fd = 0; fd < max_fd; fd++) {
if (FD_ISSET(fd, set)) {
lua_pushnumber(L, i);
lua_pushnumber(L, fd);
lua_settable(L, -3);
}
}
}
/**
* readfds, writefds, err = socket.select(readfds, writefds[, timeout=-1])
*
* `readfds`, `writefds` are all table of fds (which was returned from
* sockobj:fileno()).
*/
static int
socket_select(lua_State * L)
{
int max_fd = -1;
fd_set rset, wset;
struct timeout tm;
double timeout = luaL_optnumber(L, 3, -1);
timeout_init(&tm, timeout);
FD_ZERO(&rset);
FD_ZERO(&wset);
__collect_fds(L, 1, &rset, &max_fd);
__collect_fds(L, 2, &wset, &max_fd);
int ret = __select(max_fd + 1, &rset, &wset, NULL, &tm);
if (ret > 0) {
__return_fd(L, &rset, max_fd + 1);
__return_fd(L, &wset, max_fd + 1);
return 2;
} else if (ret == 0) {
lua_pushnil(L);
lua_pushnil(L);
lua_pushstring(L, ERROR_TIMEOUT);
return 3;
} else {
lua_pushnil(L);
lua_pushnil(L);
lua_pushstring(L, strerror(errno));
return 3;
}
}
/*** sock_* methods are common to tcpsocket or udpsocket ***/
/**
* fd = sockobj:fileno()
*
* Return the integer file descriptor of the socket.
*/
static int
sockobj_fileno(lua_State * L)
{
struct sockobj *s = getsockobj(L);
lua_pushnumber(L, s->fd);
return 1;
}
/**
* ok, err = sockobj:close()
*
* Close the socket.
*/
static int
sockobj_close(lua_State * L)
{
struct sockobj *s = getsockobj(L);
if (__sockobj_close(L, s) == -1)
return 2;
lua_pushboolean(L, 1);
return 1;
}
static int
sockobj_tostring(lua_State * L)
{
struct sockobj *s = getsockobj(L);
assert(lua_getmetatable(L, -1));
luaL_getmetatable(L, TCPSOCK_TYPENAME);
if (lua_rawequal(L, -1, -2)) {
lua_pop(L, 2);
lua_pushfstring(L, "<tcpsock: %d>", s->fd);
} else {
lua_pop(L, 2);
lua_pushfstring(L, "<udpsock: %d>", s->fd);
}
return 1;
}
/**
* sockobj:settimeout(timeout)
*
* Set the timeout in seconds for subsequent socket operations.
* A negative timeout indicates that timeout is disabled, which is default.
*/
static int
sockobj_settimeout(lua_State * L)
{
struct sockobj *s = getsockobj(L);
double timeout = (double)luaL_checknumber(L, 2);
s->sock_timeout = timeout;
return 0;
}
/*
* timeout = sockobj:gettimeout()
*
* Returns the timeout in seconds associated with socket.
* A negative timeout indicates that timeout is disabled, which is default.
*/
static int
sockobj_gettimeout(lua_State * L)
{
struct sockobj *s = getsockobj(L);
lua_pushnumber(L, s->sock_timeout);
return 1;
}
/**
* ok, err = tcpsock:connect(host, port)
* ok, err = tcpsock:connect("unix:/path/to/unix-domain.sock")
*
* Attempts to connect to TCP socket object to a remote server or to a stream
* unix domain socket file.
*/
static int
tcpsock_connect(lua_State * L)
{
struct sockobj *s = getsockobj(L);
sockaddr_t addr;
socklen_t len;
if (s->fd > 0) {
return luaL_error(L, "already connected");
}
if (__sockobj_getaddrfromarg(L, s, SAS2SA(&addr), &len, 1)) {
return 2;
}
if (__sockobj_createsocket(L, s, SOCK_STREAM) == -1) {
return 2;
}
if (__sockobj_connect(L, s, SAS2SA(&addr), len) == -1)
return 2;
lua_pushboolean(L, 1);
return 1;
}
/**
* ok, err = tcpsock:bind(host, port)
* ok, err = tcpsock:connect("unix:/path/to/unix-domain.sock")
*/
static int
tcpsock_bind(lua_State * L)
{
struct sockobj *s = getsockobj(L);
sockaddr_t addr;
socklen_t len;
char *errstr = NULL;
if (s->fd > 0) {
return luaL_error(L, "already bound");
}
if (__sockobj_getaddrfromarg(L, s, SAS2SA(&addr), &len, 1)) {
return 2;
}
if (__sockobj_createsocket(L, s, SOCK_STREAM) == -1) {
return 2;
}
if (bind(s->fd, SAS2SA(&addr), len) < 0) {
errstr = strerror(errno);
goto err;
}
lua_pushboolean(L, 1);
return 1;
err:
assert(errstr);
lua_pushnil(L);
lua_pushstring(L, errstr);
return 2;
}
/**
* ok, err = tcpsock:listen(backlog)
*
* Listen for connections make to the socket.
* The backlog argument specifies the maximum number of queue connections and
* should be at least 0.
*/
static int
tcpsock_listen(lua_State * L)
{
struct sockobj *s = getsockobj(L);
int backlog;
int ret;
char *errstr = NULL;
backlog = luaL_checknumber(L, 2);
/* To avoid problems on systems that don't allow a negative backlog, force
* minimu value of 0. */
if (backlog < 0) {
backlog = 0;
}
ret = listen(s->fd, backlog);
if (ret < 0) {
errstr = strerror(ret);
goto err;
}
lua_pushboolean(L, 1);
return 1;
err:
assert(errstr);
lua_pushnil(L);
lua_pushstring(L, errstr);
return 2;
}
/**
* sock, err = tcpsock:accept()
*
* Accept a connection. The socket must be bound to an address and listening for
* connections.
*
* In case of success, it returns a socket object usable to read/write data on
* the connection. Otherwise, it returns nil and a string describing the error.
*/
static int
tcpsock_accept(lua_State * L)
{