forked from zhaojh329/lua-eco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket.c
842 lines (685 loc) · 21 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
/* SPDX-License-Identifier: MIT */
/*
* Author: Jianhui Zhao <[email protected]>
*/
#include <linux/netlink.h>
#include <netinet/tcp.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <stdlib.h>
#include <sys/un.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include "eco.h"
struct sock_opt {
const char *name;
int (*func)(lua_State *L, int fd);
};
static void push_socket_addr(lua_State *L, struct sockaddr *addr)
{
int family = addr->sa_family;
char ip[INET6_ADDRSTRLEN];
lua_createtable(L, 0, 2);
lua_pushinteger(L, family);
lua_setfield(L, -2, "family");
if (family == AF_NETLINK) {
struct sockaddr_nl *nl = (struct sockaddr_nl *)addr;
lua_pushinteger(L, nl->nl_pid);
lua_setfield(L, -2, "pid");
} if (family == AF_UNIX) {
lua_pushstring(L, ((struct sockaddr_un *)addr)->sun_path);
lua_setfield(L, -2, "path");
} else if (family == AF_INET) {
struct sockaddr_in *in = (struct sockaddr_in *)addr;
lua_pushinteger(L, ntohs(in->sin_port));
lua_setfield(L, -2, "port");
lua_pushstring(L, inet_ntop(AF_INET, &in->sin_addr, ip, sizeof(ip)));
lua_setfield(L, -2, "ipaddr");
} else if (family == AF_INET6) {
struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)addr;
lua_pushinteger(L, ntohs(in6->sin6_port));
lua_setfield(L, -2, "port");
lua_pushstring(L, inet_ntop(AF_INET6, &in6->sin6_addr, ip, sizeof(ip)));
lua_setfield(L, -2, "ipaddr");
}
}
static int eco_socket_socket(lua_State *L)
{
int domain = luaL_checkinteger(L, 1);
int type = luaL_checkinteger(L, 2);
int protocol = lua_tointeger(L, 3);
int fd;
fd = socket(domain, type | SOCK_NONBLOCK | SOCK_CLOEXEC, protocol);
if (fd < 0) {
lua_pushnil(L);
lua_pushinteger(L, errno);
return 2;
}
lua_pushinteger(L, fd);
return 1;
}
static int eco_socket_bind_common(lua_State *L, int fd, struct sockaddr *addr, socklen_t addrlen)
{
if (bind(fd, addr, addrlen)) {
lua_pushboolean(L, false);
lua_pushinteger(L, errno);
return 2;
}
lua_pushboolean(L, true);
return 1;
}
static int eco_socket_bind(lua_State *L)
{
int fd = luaL_checkinteger(L, 1);
const char *ip = lua_tostring(L, 2);
int port = luaL_checkinteger(L, 3);
struct sockaddr_in addr = {
.sin_family = AF_INET,
.sin_port = htons(port)
};
if (ip && inet_pton(AF_INET, ip, &addr.sin_addr) != 1)
luaL_argerror(L, 2, "not a valid IPv4 address");
return eco_socket_bind_common(L, fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in));
}
static int eco_socket_bind6(lua_State *L)
{
int fd = luaL_checkinteger(L, 1);
const char *ip = lua_tostring(L, 2);
int port = luaL_checkinteger(L, 3);
struct sockaddr_in6 addr = {
.sin6_family = AF_INET6,
.sin6_port = htons(port)
};
if (ip && inet_pton(AF_INET6, ip, &addr.sin6_addr) != 1)
luaL_argerror(L, 2, "not a valid IPv6 address");
return eco_socket_bind_common(L, fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in6));
}
static int eco_socket_bind_unix(lua_State *L)
{
int fd = luaL_checkinteger(L, 1);
const char *path = luaL_checkstring(L, 2);
struct sockaddr_un addr = {
.sun_family = AF_UNIX
};
if (strlen(path) >= sizeof(addr.sun_path))
luaL_argerror(L, 2, "path too long");
strcpy(addr.sun_path, path);
return eco_socket_bind_common(L, fd, (struct sockaddr *)&addr, SUN_LEN(&addr));
}
static int eco_socket_bind_nl(lua_State *L)
{
int fd = luaL_checkinteger(L, 1);
struct sockaddr_nl addr = {
.nl_family = AF_NETLINK,
.nl_groups = luaL_optinteger(L, 2, 0),
.nl_pid = luaL_optinteger(L, 3, 0)
};
return eco_socket_bind_common(L, fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_nl));
}
static int eco_socket_listen(lua_State *L)
{
int fd = luaL_checkinteger(L, 1);
int backlog = luaL_optinteger(L, 2, SOMAXCONN);
if (listen(fd, backlog)) {
lua_pushboolean(L, false);
lua_pushinteger(L, errno);
return 2;
}
lua_pushboolean(L, true);
return 1;
}
static int eco_socket_accept(lua_State *L)
{
int lfd = luaL_checkinteger(L, 1);
union {
struct sockaddr_un un;
struct sockaddr_in in;
struct sockaddr_in6 in6;
} addr = {};
socklen_t addrlen = sizeof(addr);
int fd;
again:
fd = accept4(lfd, (struct sockaddr *)&addr, &addrlen, SOCK_NONBLOCK | SOCK_CLOEXEC);
if (fd < 0) {
if (errno == EINTR)
goto again;
lua_pushnil(L);
lua_pushinteger(L, errno);
return 2;
}
lua_pushinteger(L, fd);
push_socket_addr(L, (struct sockaddr *)&addr);
return 2;
}
static int eco_socket_connect_common(lua_State *L, int fd, struct sockaddr *addr, socklen_t addrlen)
{
if (connect(fd, addr, addrlen) < 0) {
lua_pushboolean(L, false);
lua_pushinteger(L, errno);
return 2;
}
lua_pushboolean(L, true);
return 1;
}
static int eco_socket_connect(lua_State *L)
{
int fd = luaL_checkinteger(L, 1);
const char *ip = luaL_checkstring(L, 2);
int port = luaL_checkinteger(L, 3);
struct sockaddr_in addr = {
.sin_family = AF_INET,
.sin_port = htons(port)
};
if (inet_pton(AF_INET, ip, &addr.sin_addr) != 1)
luaL_argerror(L, 2, "not a valid IPv4 address");
return eco_socket_connect_common(L, fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in));
}
static int eco_socket_connect6(lua_State *L)
{
int fd = luaL_checkinteger(L, 1);
const char *ip = luaL_checkstring(L, 2);
int port = luaL_checkinteger(L, 3);
struct sockaddr_in6 addr = {
.sin6_family = AF_INET6,
.sin6_port = htons(port)
};
if (inet_pton(AF_INET6, ip, &addr.sin6_addr) != 1)
luaL_argerror(L, 2, "not a valid IPv6 address");
return eco_socket_connect_common(L, fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in6));
}
static int eco_socket_connect_unix(lua_State *L)
{
int fd = luaL_checkinteger(L, 1);
const char *path = luaL_checkstring(L, 2);
struct sockaddr_un addr = {
.sun_family = AF_UNIX
};
if (strlen(path) >= sizeof(addr.sun_path))
luaL_argerror(L, 2, "path too long");
strcpy(addr.sun_path, path);
return eco_socket_connect_common(L, fd, (struct sockaddr *)&addr, SUN_LEN(&addr));
}
static int eco_socket_connect_nl(lua_State *L)
{
int fd = luaL_checkinteger(L, 1);
int pid = luaL_checkinteger(L, 2);
struct sockaddr_nl addr = {
.nl_family = AF_NETLINK,
.nl_pid = pid
};
return eco_socket_connect_common(L, fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_nl));
}
static int eco_socket_send(lua_State *L)
{
int fd = luaL_checkinteger(L, 1);
size_t len;
const char *data = luaL_checklstring(L, 2, &len);
int flags = luaL_optinteger(L, 3, 0);
int ret;
again:
ret = send(fd, data, len, flags);
if (ret < 0) {
if (errno == EINTR)
goto again;
lua_pushnil(L);
lua_pushstring(L, strerror(errno));
return 2;
}
lua_pushinteger(L, ret);
return 1;
}
static int eco_socket_recv(lua_State *L)
{
int fd = luaL_checkinteger(L, 1);
size_t n = luaL_checkinteger(L, 2);
int flags = luaL_optinteger(L, 3, 0);
ssize_t ret;
char *buf;
if (n < 1)
luaL_argerror(L, 2, "must be greater than 0");
buf = malloc(n);
if (!buf) {
lua_pushnil(L);
lua_pushstring(L, strerror(errno));
return 2;
}
again:
ret = recv(fd, buf, n, flags);
if (unlikely(ret < 0)) {
if (errno == EINTR)
goto again;
free(buf);
lua_pushnil(L);
lua_pushstring(L, strerror(errno));
return 2;
}
lua_pushlstring(L, buf, ret);
free(buf);
return 1;
}
static int eco_socket_sendto_common(lua_State *L, int fd, const void *data, size_t len,
struct sockaddr *addr, socklen_t addrlen, int flags)
{
int ret;
again:
ret = sendto(fd, data, len, flags, addr, addrlen);
if (ret < 0) {
if (errno == EINTR)
goto again;
lua_pushnil(L);
lua_pushstring(L, strerror(errno));
return 2;
}
lua_pushinteger(L, ret);
return 1;
}
static int eco_socket_sendto(lua_State *L)
{
int fd = luaL_checkinteger(L, 1);
size_t len;
const char *data = luaL_checklstring(L, 2, &len);
const char *ip = luaL_checkstring(L, 3);
int port = luaL_checkinteger(L, 4);
int flags = luaL_optinteger(L, 5, 0);
struct sockaddr_in addr = {
.sin_family = AF_INET,
.sin_port = htons(port)
};
if (inet_pton(AF_INET, ip, &addr.sin_addr) != 1)
luaL_argerror(L, 2, "not a valid IPv4 address");
return eco_socket_sendto_common(L, fd, data, len, (struct sockaddr *)&addr, sizeof(struct sockaddr_in), flags);
}
static int eco_socket_sendto6(lua_State *L)
{
int fd = luaL_checkinteger(L, 1);
size_t len;
const char *data = luaL_checklstring(L, 2, &len);
const char *ip = luaL_checkstring(L, 3);
int port = luaL_checkinteger(L, 4);
int flags = luaL_optinteger(L, 5, 0);
struct sockaddr_in6 addr = {
.sin6_family = AF_INET6,
.sin6_port = htons(port)
};
if (inet_pton(AF_INET6, ip, &addr.sin6_addr) != 1)
luaL_argerror(L, 2, "not a valid IPv6 address");
return eco_socket_sendto_common(L, fd, data, len, (struct sockaddr *)&addr, sizeof(struct sockaddr_in6), flags);
}
static int eco_socket_sendto_unix(lua_State *L)
{
int fd = luaL_checkinteger(L, 1);
size_t len;
const char *data = luaL_checklstring(L, 2, &len);
const char *path = luaL_checkstring(L, 3);
int flags = luaL_optinteger(L, 4, 0);
struct sockaddr_un addr = {
.sun_family = AF_UNIX
};
if (strlen(path) >= sizeof(addr.sun_path))
luaL_argerror(L, 2, "path too long");
strcpy(addr.sun_path, path);
return eco_socket_sendto_common(L, fd, data, len, (struct sockaddr *)&addr, SUN_LEN(&addr), flags);
}
static int eco_socket_sendto_nl(lua_State *L)
{
int fd = luaL_checkinteger(L, 1);
size_t len;
const char *data = luaL_checklstring(L, 2, &len);
int pid = luaL_optinteger(L, 3, 0);
int flags = luaL_optinteger(L, 4, 0);
struct sockaddr_nl addr = {
.nl_family = AF_NETLINK,
.nl_pid = pid
};
return eco_socket_sendto_common(L, fd, data, len, (struct sockaddr *)&addr, sizeof(struct sockaddr_nl), flags);
}
static int eco_socket_recvfrom(lua_State *L)
{
int fd = luaL_checkinteger(L, 1);
size_t n = luaL_checkinteger(L, 2);
int flags = luaL_optinteger(L, 3, 0);
union {
struct sockaddr_nl nl;
struct sockaddr_un un;
struct sockaddr_in in;
struct sockaddr_in6 in6;
} addr = {};
socklen_t addrlen = sizeof(addr);
ssize_t ret;
char *buf;
if (n < 1)
luaL_argerror(L, 2, "must be greater than 0");
buf = malloc(n);
if (!buf) {
lua_pushnil(L);
lua_pushstring(L, strerror(errno));
return 2;
}
again:
ret = recvfrom(fd, buf, n, flags, (struct sockaddr *)&addr, &addrlen);
if (ret < 0) {
if (errno == EINTR)
goto again;
free(buf);
lua_pushnil(L);
lua_pushstring(L, strerror(errno));
return 2;
}
lua_pushlstring(L, buf, ret);
free(buf);
push_socket_addr(L, (struct sockaddr *)&addr);
return 2;
}
static int eco_socket_getsockname(lua_State *L)
{
int fd = luaL_checkinteger(L, 1);
union {
struct sockaddr_un un;
struct sockaddr_in in;
struct sockaddr_in6 in6;
} addr = {};
socklen_t addrlen = sizeof(addr);
if (getsockname(fd, (struct sockaddr *)&addr, &addrlen)) {
lua_pushnil(L);
lua_pushstring(L, strerror(errno));
return 2;
}
push_socket_addr(L, (struct sockaddr *)&addr);
return 1;
}
static int eco_socket_getpeername(lua_State *L)
{
int fd = luaL_checkinteger(L, 1);
union {
struct sockaddr_un un;
struct sockaddr_in in;
struct sockaddr_in6 in6;
} addr = {};
socklen_t addrlen = sizeof(addr);
if (getpeername(fd, (struct sockaddr *)&addr, &addrlen)) {
lua_pushnil(L);
lua_pushstring(L, strerror(errno));
return 2;
}
push_socket_addr(L, (struct sockaddr *)&addr);
return 1;
}
static int opt_getboolean(lua_State *L, int fd, int level, int name)
{
socklen_t len = sizeof(int);
int val = 0;
if (getsockopt(fd, level, name, &val, &len) < 0) {
lua_pushnil(L);
lua_pushstring(L, strerror(errno));
return 2;
}
lua_pushboolean(L, val);
return 1;
}
static int opt_get_reuseaddr(lua_State *L, int fd)
{
return opt_getboolean(L, fd, SOL_SOCKET, SO_REUSEADDR);
}
static int opt_get_error(lua_State *L, int fd)
{
socklen_t len = sizeof(int);
int val = 0;
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &val, &len) < 0)
val = errno;
lua_pushinteger(L, val);
return 1;
}
static struct sock_opt optget[] = {
{"reuseaddr", opt_get_reuseaddr},
{"error", opt_get_error},
{NULL, NULL}
};
static int eco_socket_getoption(lua_State *L)
{
int fd = luaL_checkinteger(L, 1);
const char *name = luaL_checkstring(L, 2);
struct sock_opt *o = optget;
while (o->name && strcmp(name, o->name))
o++;
if (!o->func) {
char msg[60];
sprintf(msg, "unsupported option '%.35s'", name);
luaL_argerror(L, 2, msg);
}
return o->func(L, fd);
}
static int opt_set(lua_State *L, int fd, int level, int name, void *val, int len)
{
if (setsockopt(fd, level, name, val, len) < 0) {
lua_pushboolean(L, false);
lua_pushstring(L, strerror(errno));
return 2;
}
lua_pushboolean(L, true);
return 1;
}
static int opt_setboolean(lua_State *L, int fd, int level, int name)
{
int val;
luaL_checktype(L, 3, LUA_TBOOLEAN);
val = lua_toboolean(L, 3);
return opt_set(L, fd, level, name, &val, sizeof(val));
}
static int opt_setint(lua_State *L, int fd, int level, int name)
{
int val = luaL_checkinteger(L, 3);
return opt_set(L, fd, level, name, &val, sizeof(val));
}
static int opt_set_reuseaddr(lua_State *L, int fd)
{
return opt_setboolean(L, fd, SOL_SOCKET, SO_REUSEADDR);
}
static int opt_set_reuseport(lua_State *L, int fd)
{
return opt_setboolean(L, fd, SOL_SOCKET, SO_REUSEPORT);
}
static int opt_set_keepalive(lua_State *L, int fd)
{
return opt_setboolean(L, fd, SOL_SOCKET, SO_KEEPALIVE);
}
static int opt_set_tcp_keepidle(lua_State *L, int fd)
{
return opt_setint(L, fd, SOL_TCP, TCP_KEEPIDLE);
}
static int opt_set_tcp_keepintvl(lua_State *L, int fd)
{
return opt_setint(L, fd, SOL_TCP, TCP_KEEPINTVL);
}
static int opt_set_tcp_keepcnt(lua_State *L, int fd)
{
return opt_setint(L, fd, SOL_TCP, TCP_KEEPCNT);
}
static int opt_set_tcp_fastopen(lua_State *L, int fd)
{
return opt_setint(L, fd, SOL_TCP, TCP_FASTOPEN);
}
static int opt_set_tcp_nodelay(lua_State *L, int fd)
{
return opt_setboolean(L, fd, IPPROTO_TCP, TCP_NODELAY);
}
static int opt_set_ipv6_v6only(lua_State *L, int fd)
{
return opt_setboolean(L, fd, IPPROTO_IPV6, IPV6_V6ONLY);
}
static int opt_set_bindtodevice(lua_State *L, int fd)
{
const char *ifname = luaL_checkstring(L, 3);
struct ifreq ifr = {};
if (strlen(ifname) >= IFNAMSIZ)
luaL_argerror(L, 3, "ifname too long");
strcpy(ifr.ifr_name, ifname);
return opt_set(L, fd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr));
}
static int opt_set_netlink_add_membership(lua_State *L, int fd)
{
return opt_setint(L, fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP);
}
static int opt_set_netlink_drop_membership(lua_State *L, int fd)
{
return opt_setint(L, fd, SOL_NETLINK, NETLINK_DROP_MEMBERSHIP);
}
static struct sock_opt optset[] = {
{"reuseaddr", opt_set_reuseaddr},
{"reuseport", opt_set_reuseport},
{"keepalive", opt_set_keepalive},
{"tcp_keepidle", opt_set_tcp_keepidle},
{"tcp_keepintvl", opt_set_tcp_keepintvl},
{"tcp_keepcnt", opt_set_tcp_keepcnt},
{"tcp_fastopen", opt_set_tcp_fastopen},
{"tcp_nodelay", opt_set_tcp_nodelay},
{"ipv6_v6only", opt_set_ipv6_v6only},
{"bindtodevice", opt_set_bindtodevice},
{"netlink_add_membership", opt_set_netlink_add_membership},
{"netlink_drop_membership", opt_set_netlink_drop_membership},
{NULL, NULL}
};
static int eco_socket_setoption(lua_State *L)
{
int fd = luaL_checkinteger(L, 1);
const char *name = luaL_checkstring(L, 2);
struct sock_opt *o = optset;
while (o->name && strcmp(name, o->name))
o++;
if (!o->func) {
char msg[60];
sprintf(msg, "unsupported option '%.35s'", name);
luaL_argerror(L, 2, msg);
}
return o->func(L, fd);
}
static int eco_socket_is_ipv4_address(lua_State *L)
{
const char *ip = luaL_checkstring(L, 1);
struct in_addr addr;
lua_pushboolean(L, inet_pton(AF_INET, ip, &addr) == 1);
return 1;
}
static int eco_socket_is_ipv6_address(lua_State *L)
{
const char *ip = luaL_checkstring(L, 1);
struct in6_addr addr;
lua_pushboolean(L, inet_pton(AF_INET6, ip, &addr) == 1);
return 1;
}
static int eco_socket_inet_aton(lua_State *L)
{
const char *src = luaL_checkstring(L, 1);
struct in_addr in = {};
inet_aton(src, &in);
lua_pushinteger(L, in.s_addr);
return 1;
}
static int eco_socket_inet_ntoa(lua_State *L)
{
struct in_addr in;
in.s_addr = (uint32_t)luaL_checknumber(L, 1);
lua_pushstring(L, inet_ntoa(in));
return 1;
}
static int eco_socket_inet_ntop(lua_State *L)
{
int family = luaL_checkinteger(L, 1);
const void *src = luaL_checkstring(L, 2);
char dst[INET6_ADDRSTRLEN];
if (inet_ntop(family, src, dst, sizeof(dst)))
lua_pushstring(L, dst);
else
lua_pushnil(L);
return 1;
}
static int eco_socket_inet_pton(lua_State *L)
{
int family = luaL_checkinteger(L, 1);
const void *src = luaL_checkstring(L, 2);
char dst[sizeof(struct in6_addr)];
if (inet_pton(family, src, dst))
lua_pushlstring(L, dst, sizeof(dst));
else
lua_pushnil(L);
return 1;
}
static int eco_socket_if_nametoindex(lua_State *L)
{
const char *ifname = luaL_checkstring(L, 1);
unsigned int ifidx = if_nametoindex(ifname);
if (ifidx == 0)
lua_pushnil(L);
else
lua_pushinteger(L, ifidx);
return 1;
}
static int eco_socket_if_indextoname(lua_State *L)
{
int index = luaL_checkinteger(L, 1);
char ifname[IF_NAMESIZE] = "";
if_indextoname(index, ifname);
lua_pushstring(L, ifname);
return 1;
}
static const luaL_Reg funcs[] = {
{"socket", eco_socket_socket},
{"bind", eco_socket_bind},
{"bind6", eco_socket_bind6},
{"bind_unix", eco_socket_bind_unix},
{"bind_nl", eco_socket_bind_nl},
{"listen", eco_socket_listen},
{"accept", eco_socket_accept},
{"connect", eco_socket_connect},
{"connect6", eco_socket_connect6},
{"connect_unix", eco_socket_connect_unix},
{"connect_nl", eco_socket_connect_nl},
{"send", eco_socket_send},
{"recv", eco_socket_recv},
{"sendto", eco_socket_sendto},
{"sendto6", eco_socket_sendto6},
{"sendto_unix", eco_socket_sendto_unix},
{"sendto_nl", eco_socket_sendto_nl},
{"recvfrom", eco_socket_recvfrom},
{"getsockname", eco_socket_getsockname},
{"getpeername", eco_socket_getpeername},
{"getoption", eco_socket_getoption},
{"setoption", eco_socket_setoption},
{"is_ipv4_address", eco_socket_is_ipv4_address},
{"is_ipv6_address", eco_socket_is_ipv6_address},
{"inet_aton", eco_socket_inet_aton},
{"inet_ntoa", eco_socket_inet_ntoa},
{"inet_ntop", eco_socket_inet_ntop},
{"inet_pton", eco_socket_inet_pton},
{"if_nametoindex", eco_socket_if_nametoindex},
{"if_indextoname", eco_socket_if_indextoname},
{NULL, NULL}
};
int luaopen_eco_core_socket(lua_State *L)
{
luaL_newlib(L, funcs);
lua_add_constant(L, "AF_UNSPEC", AF_UNSPEC);
lua_add_constant(L, "AF_INET", AF_INET);
lua_add_constant(L, "AF_INET6", AF_INET6);
lua_add_constant(L, "AF_UNIX", AF_UNIX);
lua_add_constant(L, "AF_PACKET", AF_PACKET);
lua_add_constant(L, "AF_NETLINK", AF_NETLINK);
lua_add_constant(L, "SOCK_DGRAM", SOCK_DGRAM);
lua_add_constant(L, "SOCK_STREAM", SOCK_STREAM);
lua_add_constant(L, "SOCK_RAW", SOCK_RAW);
/* Bits in the FLAGS argument to `send', `recv' */
lua_add_constant(L, "MSG_OOB", MSG_OOB);
lua_add_constant(L, "MSG_PEEK", MSG_PEEK);
lua_add_constant(L, "MSG_DONTROUTE", MSG_DONTROUTE);
lua_add_constant(L, "MSG_TRUNC", MSG_TRUNC);
lua_add_constant(L, "MSG_DONTWAIT", MSG_DONTWAIT);
lua_add_constant(L, "MSG_EOR", MSG_EOR);
lua_add_constant(L, "MSG_WAITALL", MSG_WAITALL);
lua_add_constant(L, "MSG_CONFIRM", MSG_CONFIRM);
lua_add_constant(L, "MSG_ERRQUEUE", MSG_ERRQUEUE);
lua_add_constant(L, "MSG_NOSIGNAL", MSG_NOSIGNAL);
lua_add_constant(L, "MSG_MORE", MSG_MORE);
lua_add_constant(L, "MSG_CMSG_CLOEXEC", MSG_CMSG_CLOEXEC);
return 1;
}