-
Notifications
You must be signed in to change notification settings - Fork 0
/
c.mojo
2141 lines (1707 loc) · 56.6 KB
/
c.mojo
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
"""FFI utils for the C programming language.
Notes:
The functions in this module follow only the POSIX standard.
"""
from sys.intrinsics import _mlirtype_is_eq
from utils import StaticTuple
# Adapted from https://github.com/crisadamo/mojo-Libc which doesn't currently
# (2024-07-22) have a licence, so I'll assume MIT licence.
# Huge thanks for the work done.
struct C:
"""C types. This assumes that the platform is 32 or 64 bit, and char is
always 8 bit (POSIX standard).
"""
alias char = Int8
"""C type: `char`. The signedness of `char` is platform specific. Most
systems, including x86 GNU/Linux and Windows, use `signed char`, but those
based on PowerPC and ARM processors typically use `unsigned char`."""
alias s_char = Int8
"""C type: `signed char`."""
alias u_char = UInt8
"""C type: `unsigned char`."""
alias short = Int16
"""C type: `short`."""
alias u_short = UInt16
"""C type: `unsigned short`."""
alias int = Int32
"""C type: `int`."""
alias u_int = UInt32
"""C type: `unsigned int`."""
alias long = Int64
"""C type: `long`."""
alias u_long = UInt64
"""C type: `unsigned long`."""
alias long_long = Int64
"""C type: `long long`."""
alias u_long_long = UInt64
"""C type: `unsigned long long`."""
alias float = Float32
"""C type: `float`."""
alias double = Float64
"""C type: `double`."""
alias void = Int8
"""C type: `void`."""
# --- ( error.h Constants )-----------------------------------------------------
alias EPERM = 1
alias ENOENT = 2
alias ESRCH = 3
alias EINTR = 4
alias EIO = 5
alias ENXIO = 6
alias E2BIG = 7
alias ENOEXEC = 8
alias EBADF = 9
alias ECHILD = 10
alias EAGAIN = 11
alias ENOMEM = 12
alias EACCES = 13
alias EFAULT = 14
alias ENOTBLK = 15
alias EBUSY = 16
alias EEXIST = 17
alias EXDEV = 18
alias ENODEV = 19
alias ENOTDIR = 20
alias EISDIR = 21
alias EINVAL = 22
alias ENFILE = 23
alias EMFILE = 24
alias ENOTTY = 25
alias ETXTBSY = 26
alias EFBIG = 27
alias ENOSPC = 28
alias ESPIPE = 29
alias EROFS = 30
alias EMLINK = 31
alias EPIPE = 32
alias EDOM = 33
alias ERANGE = 34
alias EWOULDBLOCK = EAGAIN
fn to_char_ptr(s: String) -> UnsafePointer[C.char]:
"""Only ASCII-based strings."""
var ptr = UnsafePointer[C.char]().alloc(len(s))
for i in range(len(s)):
ptr[i] = ord(s[i])
return ptr
fn fptr_to_string(s: UnsafePointer[C.char]) -> String:
return String(s.bitcast[UInt8](), int(strlen(s)))
# fn cftob(val: C.int) -> Bool:
# """Convert C-like failure (-1) to Bool"""
# return rebind[Bool](val > 0)
# --- ( Network Related Constants )---------------------------------------------
alias sa_family_t = C.u_short
alias socklen_t = C.u_int
alias in_addr_t = C.u_int
alias in_port_t = C.u_short
# Address Family Constants
alias AF_UNSPEC = 0
alias AF_UNIX = 1
alias AF_LOCAL = AF_UNIX
alias AF_INET = 2
alias AF_AX25 = 3
alias AF_IPX = 4
alias AF_APPLETALK = 5
alias AF_NETROM = 6
alias AF_BRIDGE = 7
alias AF_ATMPVC = 8
alias AF_X25 = 9
alias AF_INET6 = 10
alias AF_ROSE = 11
alias AF_DECnet = 12
alias AF_NETBEUI = 13
alias AF_SECURITY = 14
alias AF_KEY = 15
alias AF_NETLINK = 16
alias AF_ROUTE = AF_NETLINK
alias AF_PACKET = 17
alias AF_ASH = 18
alias AF_ECONET = 19
alias AF_ATMSVC = 20
alias AF_RDS = 21
alias AF_SNA = 22
alias AF_IRDA = 23
alias AF_PPPOX = 24
alias AF_WANPIPE = 25
alias AF_LLC = 26
alias AF_CAN = 29
alias AF_TIPC = 30
alias AF_BLUETOOTH = 31
alias AF_IUCV = 32
alias AF_RXRPC = 33
alias AF_ISDN = 34
alias AF_PHONET = 35
alias AF_IEEE802154 = 36
alias AF_CAIF = 37
alias AF_ALG = 38
alias AF_NFC = 39
alias AF_VSOCK = 40
alias AF_KCM = 41
alias AF_QIPCRTR = 42
alias AF_MAX = 43
alias PF_UNSPEC = AF_UNSPEC
alias PF_UNIX = AF_UNIX
alias PF_LOCAL = AF_LOCAL
alias PF_INET = AF_INET
alias PF_AX25 = AF_AX25
alias PF_IPX = AF_IPX
alias PF_APPLETALK = AF_APPLETALK
alias PF_NETROM = AF_NETROM
alias PF_BRIDGE = AF_BRIDGE
alias PF_ATMPVC = AF_ATMPVC
alias PF_X25 = AF_X25
alias PF_INET6 = AF_INET6
alias PF_ROSE = AF_ROSE
alias PF_DECnet = AF_DECnet
alias PF_NETBEUI = AF_NETBEUI
alias PF_SECURITY = AF_SECURITY
alias PF_KEY = AF_KEY
alias PF_NETLINK = AF_NETLINK
alias PF_ROUTE = AF_ROUTE
alias PF_PACKET = AF_PACKET
alias PF_ASH = AF_ASH
alias PF_ECONET = AF_ECONET
alias PF_ATMSVC = AF_ATMSVC
alias PF_RDS = AF_RDS
alias PF_SNA = AF_SNA
alias PF_IRDA = AF_IRDA
alias PF_PPPOX = AF_PPPOX
alias PF_WANPIPE = AF_WANPIPE
alias PF_LLC = AF_LLC
alias PF_CAN = AF_CAN
alias PF_TIPC = AF_TIPC
alias PF_BLUETOOTH = AF_BLUETOOTH
alias PF_IUCV = AF_IUCV
alias PF_RXRPC = AF_RXRPC
alias PF_ISDN = AF_ISDN
alias PF_PHONET = AF_PHONET
alias PF_IEEE802154 = AF_IEEE802154
alias PF_CAIF = AF_CAIF
alias PF_ALG = AF_ALG
alias PF_NFC = AF_NFC
alias PF_VSOCK = AF_VSOCK
alias PF_KCM = AF_KCM
alias PF_QIPCRTR = AF_QIPCRTR
alias PF_MAX = AF_MAX
# Socket Type constants
alias SOCK_STREAM = 1
alias SOCK_DGRAM = 2
alias SOCK_RAW = 3
alias SOCK_RDM = 4
alias SOCK_SEQPACKET = 5
alias SOCK_DCCP = 6
alias SOCK_PACKET = 10
# alias SOCK_CLOEXEC = O_CLOEXEC
# alias SOCK_NONBLOCK = O_NONBLOCK
# Address Information
alias AI_PASSIVE = 1
alias AI_CANONNAME = 2
alias AI_NUMERICHOST = 4
alias AI_V4MAPPED = 8
alias AI_ALL = 16
alias AI_ADDRCONFIG = 32
alias AI_IDN = 64
alias INET_ADDRSTRLEN = 16
alias INET6_ADDRSTRLEN = 46
alias SHUT_RD = 0
alias SHUT_WR = 1
alias SHUT_RDWR = 2
alias SOL_SOCKET = 1
alias SO_DEBUG = 1
alias SO_REUSEADDR = 2
alias SO_TYPE = 3
alias SO_ERROR = 4
alias SO_DONTROUTE = 5
alias SO_BROADCAST = 6
alias SO_SNDBUF = 7
alias SO_RCVBUF = 8
alias SO_KEEPALIVE = 9
alias SO_OOBINLINE = 10
alias SO_NO_CHECK = 11
alias SO_PRIORITY = 12
alias SO_LINGER = 13
alias SO_BSDCOMPAT = 14
alias SO_REUSEPORT = 15
alias SO_PASSCRED = 16
alias SO_PEERCRED = 17
alias SO_RCVLOWAT = 18
alias SO_SNDLOWAT = 19
alias SO_RCVTIMEO = 20
alias SO_SNDTIMEO = 21
# alias SO_RCVTIMEO_OLD = 20
# alias SO_SNDTIMEO_OLD = 21
alias SO_SECURITY_AUTHENTICATION = 22
alias SO_SECURITY_ENCRYPTION_TRANSPORT = 23
alias SO_SECURITY_ENCRYPTION_NETWORK = 24
alias SO_BINDTODEVICE = 25
alias SO_ATTACH_FILTER = 26
alias SO_DETACH_FILTER = 27
alias SO_GET_FILTER = SO_ATTACH_FILTER
alias SO_PEERNAME = 28
alias SO_TIMESTAMP = 29
# alias SO_TIMESTAMP_OLD = 29
alias SO_ACCEPTCONN = 30
alias SO_PEERSEC = 31
alias SO_SNDBUFFORCE = 32
alias SO_RCVBUFFORCE = 33
alias SO_PASSSEC = 34
alias SO_TIMESTAMPNS = 35
# alias SO_TIMESTAMPNS_OLD = 35
alias SO_MARK = 36
alias SO_TIMESTAMPING = 37
# alias SO_TIMESTAMPING_OLD = 37
alias SO_PROTOCOL = 38
alias SO_DOMAIN = 39
alias SO_RXQ_OVFL = 40
alias SO_WIFI_STATUS = 41
alias SCM_WIFI_STATUS = SO_WIFI_STATUS
alias SO_PEEK_OFF = 42
alias SO_NOFCS = 43
alias SO_LOCK_FILTER = 44
alias SO_SELECT_ERR_QUEUE = 45
alias SO_BUSY_POLL = 46
alias SO_MAX_PACING_RATE = 47
alias SO_BPF_EXTENSIONS = 48
alias SO_INCOMING_CPU = 49
alias SO_ATTACH_BPF = 50
alias SO_DETACH_BPF = SO_DETACH_FILTER
alias SO_ATTACH_REUSEPORT_CBPF = 51
alias SO_ATTACH_REUSEPORT_EBPF = 52
alias SO_CNX_ADVICE = 53
alias SCM_TIMESTAMPING_OPT_STATS = 54
alias SO_MEMINFO = 55
alias SO_INCOMING_NAPI_ID = 56
alias SO_COOKIE = 57
alias SCM_TIMESTAMPING_PKTINFO = 58
alias SO_PEERGROUPS = 59
alias SO_ZEROCOPY = 60
alias SO_TXTIME = 61
alias SCM_TXTIME = SO_TXTIME
alias SO_BINDTOIFINDEX = 62
alias SO_TIMESTAMP_NEW = 63
alias SO_TIMESTAMPNS_NEW = 64
alias SO_TIMESTAMPING_NEW = 65
alias SO_RCVTIMEO_NEW = 66
alias SO_SNDTIMEO_NEW = 67
alias SO_DETACH_REUSEPORT_BPF = 68
# --- ( Network Related Structs )-----------------------------------------------
@value
@register_passable("trivial")
struct in_addr:
var s_addr: in_addr_t
@value
@register_passable("trivial")
struct in6_addr:
var s6_addr: StaticTuple[C.char, 16]
@value
@register_passable("trivial")
struct sockaddr:
var sa_family: sa_family_t
var sa_data: StaticTuple[C.char, 14]
@value
@register_passable("trivial")
struct sockaddr_in:
var sin_family: sa_family_t
var sin_port: in_port_t
var sin_addr: in_addr
var sin_zero: StaticTuple[C.char, 8]
@value
@register_passable("trivial")
struct sockaddr_in6:
var sin6_family: sa_family_t
var sin6_port: in_port_t
var sin6_flowinfo: C.u_int
var sin6_addr: in6_addr
var sin6_scope_id: C.u_int
@value
@register_passable("trivial")
struct addrinfo:
var ai_flags: C.int
var ai_family: C.int
var ai_socktype: C.int
var ai_protocol: C.int
var ai_addrlen: socklen_t
var ai_addr: UnsafePointer[sockaddr]
var ai_canonname: UnsafePointer[C.char]
# FIXME: This should be UnsafePointer[addrinfo]
var ai_next: UnsafePointer[C.void]
fn __init__() -> Self:
return Self(
0,
0,
0,
0,
0,
UnsafePointer[sockaddr](),
UnsafePointer[C.char](),
UnsafePointer[C.void](),
)
fn strlen(s: UnsafePointer[C.char]) -> C.u_int:
"""Libc POSIX `strlen` function.
Args:
s: A pointer to a C string.
Returns:
The length of the string.
Notes:
[Reference](https://man7.org/linux/man-pages/man3/strlen.3p.html).
Fn signature: `size_t strlen(const char *s)`.
"""
return external_call["strlen", C.u_int, UnsafePointer[C.char]](s)
# --- ( Network Related Syscalls & Structs )------------------------------------
fn htonl(hostlong: C.u_int) -> C.u_int:
"""Libc POSIX `htonl` function.
Args:
hostlong: A 32-bit unsigned integer in host byte order.
Returns:
A 32-bit unsigned integer in network byte order.
Notes:
[Reference](https://man7.org/linux/man-pages/man3/htonl.3p.html).
Fn signature: `uint32_t htonl(uint32_t hostlong)`.
"""
return external_call["htonl", C.u_int, C.u_int](hostlong)
fn htons(hostshort: C.u_short) -> C.u_short:
"""Libc POSIX `htons` function.
Args:
hostshort: A 16-bit unsigned integer in host byte order.
Returns:
A 16-bit unsigned integer in network byte order.
Notes:
[Reference](https://man7.org/linux/man-pages/man3/htonl.3p.html).
Fn signature: `uint16_t htons(uint16_t hostshort)`.
"""
return external_call["htons", C.u_short, C.u_short](hostshort)
fn ntohl(netlong: C.u_int) -> C.u_int:
"""Libc POSIX `ntohl` function.
Args:
netlong: A 32-bit unsigned integer in network byte order.
Returns:
A 32-bit unsigned integer in host byte order.
Notes:
[Reference](https://man7.org/linux/man-pages/man3/htonl.3p.html).
Fn signature: `uint32_t ntohl(uint32_t netlong)`.
"""
return external_call["ntohl", C.u_int, C.u_int](netlong)
fn ntohs(netshort: C.u_short) -> C.u_short:
"""Libc POSIX `ntohs` function.
Args:
netshort: A 16-bit unsigned integer in network byte order.
Returns:
A 16-bit unsigned integer in host byte order.
Notes:
[Reference](https://man7.org/linux/man-pages/man3/htonl.3p.html).
Fn signature: `uint16_t ntohs(uint16_t netshort)`.
"""
return external_call["ntohs", C.u_short, C.u_short](netshort)
fn inet_ntop(
af: C.int,
src: UnsafePointer[C.void],
dst: UnsafePointer[C.char],
size: socklen_t,
) -> UnsafePointer[C.char]:
"""Libc POSIX `inet_ntop` function.
Args:
af: Address Family see AF_ alises.
src: A pointer to a binary address.
dst: A pointer to a buffer to store the string representation of the
address.
size: The size of the buffer pointed by dst.
Returns:
A pointer.
Notes:
[Reference](https://man7.org/linux/man-pages/man3/inet_ntop.3p.html.).
Fn signature: `const char *inet_ntop(int af, const void *restrict src,
char *restrict dst, socklen_t size)`.
"""
return external_call[
"inet_ntop",
UnsafePointer[C.char],
C.int,
UnsafePointer[C.void],
UnsafePointer[C.char],
socklen_t,
](af, src, dst, size)
fn inet_pton(
af: C.int, src: UnsafePointer[C.char], dst: UnsafePointer[C.void]
) -> C.int:
"""Libc POSIX `inet_pton` function.
Args:
af: Address Family see AF_ alises.
src: A pointer to a string representation of an address.
dst: A pointer to a buffer to store the binary address.
Returns:
A pointer.
Notes:
[Reference](https://man7.org/linux/man-pages/man3/inet_ntop.3p.html).
Fn signature: `int inet_pton(int af, const char *restrict src,
void *restrict dst)`.
"""
return external_call[
"inet_pton", C.int, C.int, UnsafePointer[C.char], UnsafePointer[C.void]
](af, src, dst)
fn inet_addr(cp: UnsafePointer[C.char]) -> in_addr_t:
"""Libc POSIX `inet_addr` function.
Args:
cp: A pointer to a string representation of an address.
Returns:
If the input is invalid, INADDR_NONE (usually -1) is
returned. Use of this function is problematic because -1 is a
valid address (255.255.255.255). Avoid its use in favor of
inet_aton(), inet_pton(3), or getaddrinfo(3), which provide a
cleaner way to indicate error return.
Notes:
[Reference](https://man7.org/linux/man-pages/man3/inet_addr.3p.html).
Fn signature: `in_addr_t inet_addr(const char *cp)`.
"""
return external_call["inet_addr", in_addr_t, UnsafePointer[C.char]](cp)
fn inet_aton(cp: UnsafePointer[C.char], addr: in_addr) -> C.int:
"""Libc POSIX `inet_aton` function.
Args:
cp: A pointer to a string representation of an address.
addr: A pointer to a binary address.
Returns:
1 if the supplied string was successfully interpreted, or 0 if the
string is invalid.
Notes:
[Reference](https://man7.org/linux/man-pages/man3/inet_aton.3.html).
Fn signature: `int inet_aton(const char *cp, struct in_addr *inp)`.
"""
return external_call["inet_aton", C.int, UnsafePointer[C.char], in_addr](
cp, addr
)
fn inet_ntoa(addr: in_addr) -> UnsafePointer[C.char]:
"""Libc POSIX `inet_ntoa` function.
Args:
addr: A pointer to a binary address.
Returns:
A pointer to the string in IPv4 dotted-decimal notation.
Notes:
[Reference](https://man7.org/linux/man-pages/man3/inet_addr.3p.html).
Fn signature: `char *inet_ntoa(struct in_addr in)`.
Allocated buffer is 16-18 bytes depending on implementation.
"""
return external_call["inet_ntoa", UnsafePointer[C.char], in_addr](addr)
fn socket(domain: C.int, type: C.int, protocol: C.int) -> C.int:
"""Libc POSIX `socket` function.
Args:
domain: Address Family see AF_ alises.
type: Socket Type see SOCK_ alises.
protocol: Protocol see IPPROTO_ alises.
Returns:
A pointer to a socket.
Notes:
[Reference](https://man7.org/linux/man-pages/man3/socket.3p.html).
Fn signature: `int socket(int domain, int type, int protocol)`.
"""
return external_call["socket", C.int, C.int, C.int, C.int](
domain, type, protocol
)
fn setsockopt(
socket: C.int,
level: C.int,
option_name: C.int,
option_value: UnsafePointer[C.void],
option_len: socklen_t,
) -> C.int:
"""Libc POSIX `setsockopt` function.
Args:
socket: A pointer to a socket.
level: Protocol Level see SOL_ alises.
option_name: Option name see SO_ alises.
option_value: A pointer to a buffer containing the option value.
option_len: The size of the buffer pointed by option_value.
Returns:
A pointer to a socket opt.
Notes:
[Reference](https://man7.org/linux/man-pages/man3/setsockopt.3p.html).
Fn signature: `int setsockopt(int socket, int level, int option_name,
const void *option_value, socklen_t option_len)`.
"""
return external_call[
"setsockopt",
C.int,
C.int,
C.int,
C.int,
UnsafePointer[C.void],
socklen_t,
](socket, level, option_name, option_value, option_len)
fn bind(
socket: C.int, address: UnsafePointer[sockaddr], address_len: socklen_t
) -> C.int:
"""Libc POSIX `bind` function.
Args:
socket: The socket.
address: A pointer to the address.
address_len: The length of the pointer.
Returns:
An int.
Notes:
[Reference](https://man7.org/linux/man-pages/man3/bind.3p.html).
Fn signature: `int bind(int socket, const struct sockaddr *address,
socklen_t address_len)`.
"""
return external_call[
"bind", C.int, C.int, UnsafePointer[sockaddr], socklen_t
](socket, address, address_len)
fn listen(socket: C.int, backlog: C.int) -> C.int:
"""Libc POSIX `listen` function.
Args:
socket: A pointer to a socket.
backlog: The maximum length to which the queue of pending connections
for socket may grow.
Returns:
A pointer to a socket.
Notes:
[Reference](https://man7.org/linux/man-pages/man3/listen.3p.html).
Fn signature: `int listen(int socket, int backlog)`.
"""
return external_call["listen", C.int, C.int, C.int](socket, backlog)
fn accept(
socket: C.int,
address: UnsafePointer[sockaddr],
address_len: UnsafePointer[socklen_t],
) -> C.int:
"""Libc POSIX `accept` function.
Args:
socket: A pointer to a socket.
address: A pointer to a buffer to store the address of the accepted
socket.
address_len: A pointer to a buffer to store the length of the address of
the accepted socket.
Returns:
A pointer to a socket.
Notes:
[Reference](https://man7.org/linux/man-pages/man3/accept.3p.html).
Fn signature: `int accept(int socket, struct sockaddr *restrict address,
socklen_t *restrict address_len);`.
"""
return external_call[
"accept",
C.int,
C.int,
UnsafePointer[sockaddr],
UnsafePointer[socklen_t],
](socket, address, address_len)
fn connect(
socket: C.int, address: UnsafePointer[sockaddr], address_len: socklen_t
) -> C.int:
"""Libc POSIX `connect` function.
Args:
socket: A pointer to a socket.
address: A pointer to a buffer to store the address of the accepted
socket.
address_len: A pointer to a buffer to store the length of the address of
the accepted socket.
Returns:
A pointer to a socket.
Notes:
[Reference](https://man7.org/linux/man-pages/man3/connect.3p.html).
Fn signature: `int connect(int socket, const struct sockaddr *address,
socklen_t address_len)`.
"""
return external_call[
"connect", C.int, C.int, UnsafePointer[sockaddr], socklen_t
](socket, address, address_len)
fn recv(
socket: C.int, buffer: UnsafePointer[C.void], length: C.u_int, flags: C.int
) -> C.u_int:
"""Libc POSIX `recv` function.
Notes:
[Reference](https://man7.org/linux/man-pages/man3/recv.3p.html).
Fn signature: `ssize_t recv(int socket, void *buffer, size_t length,
int flags)`.
"""
return external_call[
"recv", C.u_int, C.int, UnsafePointer[C.void], C.u_int, C.int
](socket, buffer, length, flags)
fn recvfrom(
socket: C.int,
buffer: UnsafePointer[C.void],
length: C.u_int,
flags: C.int,
address: UnsafePointer[sockaddr],
address_len: UnsafePointer[socklen_t],
) -> C.u_int:
"""Libc POSIX `recvfrom` function.
Notes:
[Reference](https://man7.org/linux/man-pages/man3/recvfrom.3p.html).
Fn signature: `ssize_t recvfrom(int socket, void *restrict buffer,
size_t length, int flags, struct sockaddr *restrict address,
socklen_t *restrict address_len)`.
"""
return external_call[
"recvfrom",
C.u_int,
C.int,
UnsafePointer[C.void],
C.u_int,
C.int,
UnsafePointer[sockaddr],
UnsafePointer[socklen_t],
](socket, buffer, length, flags, address, address_len)
fn send(
socket: C.int, buffer: UnsafePointer[C.void], length: C.u_int, flags: C.int
) -> C.u_int:
"""Libc POSIX `send` function.
Args:
socket: A pointer to a socket.
buffer: A pointer to a buffer to store the address of the accepted
socket.
length: A pointer to a buffer to store the length of the address of the
accepted socket.
flags: A pointer to a buffer to store the length of the address of the
accepted socket.
Returns:
A pointer to a socket.
Notes:
[Reference](https://man7.org/linux/man-pages/man3/send.3p.html).
Fn signature: `ssize_t send(int socket, const void *buffer,
size_t length, int flags)`.
"""
return external_call[
"send", C.u_int, C.int, UnsafePointer[C.void], C.u_int, C.int
](socket, buffer, length, flags)
fn sendto(
socket: C.int,
message: UnsafePointer[C.void],
length: C.u_int,
flags: C.int,
dest_addr: UnsafePointer[sockaddr],
dest_len: socklen_t,
) -> C.u_int:
"""Libc POSIX `sendto` function.
Args:
socket: A pointer to a socket.
message: A pointer to a buffer to store the address of the accepted
socket.
length: A pointer to a buffer to store the length of the address of the
accepted socket.
flags: A pointer to a buffer to store the length of the address of the
accepted socket.
dest_addr: A pointer to a buffer to store the length of the address of
the accepted socket.
dest_len: A pointer to a buffer to store the length of the address of
the accepted socket.
Returns:
A pointer to a socket.
Notes:
[Reference](https://man7.org/linux/man-pages/man3/sendto.3p.html).
Fn signature: `ssize_t sendto(int socket, const void *message,
size_t length, int flags, const struct sockaddr *dest_addr,
socklen_t dest_len)`.
"""
return external_call[
"sendto",
C.u_int,
C.int,
UnsafePointer[C.void],
C.u_int,
C.int,
UnsafePointer[sockaddr],
socklen_t,
](socket, message, length, flags, dest_addr, dest_len)
fn shutdown(socket: C.int, how: C.int) -> C.int:
"""Libc POSIX `shutdown` function.
Args:
socket: A pointer to a socket.
how: A pointer to a buffer to store the length of the address of the
accepted socket.
Returns:
A pointer to a socket.
Notes:
[Reference](https://man7.org/linux/man-pages/man3/shutdown.3p.html).
Fn signature: `int shutdown(int socket, int how)`.
"""
return external_call["shutdown", C.int, C.int, C.int](socket, how)
fn getaddrinfo(
nodename: UnsafePointer[C.char],
servname: UnsafePointer[C.char],
hints: UnsafePointer[addrinfo],
res: UnsafePointer[UnsafePointer[addrinfo]],
) -> C.int:
"""Libc POSIX `getaddrinfo` function.
Notes:
[Reference](https://man7.org/linux/man-pages/man3/getaddrinfo.3p.html).
Fn signature: `int getaddrinfo(const char *restrict nodename,
const char *restrict servname, const struct addrinfo *restrict hints
, struct addrinfo **restrict res)`.
"""
return external_call[
"getaddrinfo",
C.int,
UnsafePointer[C.char],
UnsafePointer[C.char],
UnsafePointer[addrinfo],
UnsafePointer[UnsafePointer[addrinfo]],
](nodename, servname, hints, res)
fn gai_strerror(ecode: C.int) -> UnsafePointer[C.char]:
"""Libc POSIX `gai_strerror` function.
Args:
ecode: A pointer to a socket.
Returns:
A pointer to a socket.
Notes:
[Reference](https://man7.org/linux/man-pages/man3/gai_strerror.3p.html).
Fn signature: `const char *gai_strerror(int ecode)`.
"""
return external_call["gai_strerror", UnsafePointer[C.char], C.int](ecode)
# fn get_addr(ptr: UnsafePointer[sockaddr]) -> sockaddr:
# if ptr.load().sa_family == AF_INET:
# ptr.bitcast[sockaddr_in]().load().sin_addr
# return ptr.bitcast[sockaddr_in6]().load().sin6_addr
fn inet_pton(address_family: Int, address: String) -> Int:
var ip_buf_size = 4
if address_family == AF_INET6:
ip_buf_size = 16
var ip_buf = UnsafePointer[C.void].alloc(ip_buf_size)
_ = inet_pton(rebind[C.int](address_family), to_char_ptr(address), ip_buf)
return int(ip_buf.bitcast[C.u_int]()[])
# --- ( File Related Syscalls & Structs )---------------------------------------
alias off_t = Int64
alias mode_t = UInt32
alias FM_READ = "r"
alias FM_WRITE = "w"
alias FM_APPEND = "a"
alias FM_BINARY = "b"
alias FM_PLUS = "+"
alias SEEK_SET = 0
alias SEEK_CUR = 1
alias SEEK_END = 2
alias O_RDONLY = 0
alias O_WRONLY = 1
alias O_RDWR = 2
alias O_APPEND = 8
alias O_CREAT = 512
alias O_TRUNC = 1024
alias O_EXCL = 2048
alias O_SYNC = 8192
alias O_NONBLOCK = 16384
alias O_ACCMODE = 3
alias O_CLOEXEC = 524288
# from fcntl.h
alias O_EXEC = -1
alias O_SEARCH = -1
alias O_DIRECTORY = -1
alias O_DSYNC = -1
alias O_NOCTTY = -1
alias O_NOFOLLOW = -1
alias O_RSYNC = -1
alias O_TTY_INIT = -1
alias STDIN_FILENO = 0
alias STDOUT_FILENO = 1
alias STDERR_FILENO = 2
alias F_DUPFD = 0
alias F_GETFD = 1
alias F_SETFD = 2
alias F_GETFL = 3
alias F_SETFL = 4
alias F_GETOWN = 5
alias F_SETOWN = 6
alias F_GETLK = 7
alias F_SETLK = 8
alias F_SETLKW = 9
alias F_RGETLK = 10
alias F_RSETLK = 11
alias F_CNVT = 12
alias F_RSETLKW = 13
alias F_DUPFD_CLOEXEC = 14
# TODO
alias FD_CLOEXEC = -1
alias F_RDLCK = -1
alias F_UNLCK = -1
alias F_WRLCK = -1
alias AT_EACCESS = 512
alias AT_FDCWD = -100
alias AT_SYMLINK_NOFOLLOW = 256
alias AT_REMOVEDIR = 512
alias AT_SYMLINK_FOLLOW = 1024
alias AT_NO_AUTOMOUNT = 2048
alias AT_EMPTY_PATH = 4096
alias AT_RECURSIVE = 32768
@register_passable("trivial")
struct FILE:
pass
# TODO: this should take in *args: *T
fn fcntl(fildes: C.int, cmd: C.int) -> C.int:
"""Libc POSIX `fcntl` function.
Args:
fildes: A File Descriptor to close.
cmd: A command to execute.
Returns: