-
Notifications
You must be signed in to change notification settings - Fork 1
/
net.cc
2111 lines (1977 loc) · 50.1 KB
/
net.cc
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
#define ICI_CORE
/*
* $Id: net.c,v 1.24 2006/02/27 13:55:43 atrn Exp $
*
* net module - ici sockets interface
*
* This is a set of extra functions to access to BSD sockets and IP
* protocols. The ICI interface adopts practices suitable for the ICI
* environment and introduces a new object type, socket, to represent
* sockets. Functions follow the BSD model adapted to the ici
* environment. In particular network addresses are represented using
* strings and there is support for turning sockets into files to
* allow stream I/O via printf and the like.
*
* The module is mostly portable to Windows. Some Unix specific
* functions are not provided in the Windows version of the module
* (currently only the socketpair() function is missing).
*
*/
/*
* Sockets based networking
*
* The ICI 'net' module provides sockets style network interface functions.
* It is available on systems that provide BSD-compatible sockets calls and
* for Win32 platforms. The sockets extension is generally compatible with
* the C sockets functions, but uses types and calling semantics more akin to
* the ICI environment.
*
* The sockets extension introduces a new type, 'socket', to hold socket
* objects. The function, 'net.socket()', returns a 'socket'
* object.
*
* This --intro-- and --synopsis-- forms part of --ici-net-- documentation.
*/
#include "buf.h"
#include "cfunc.h"
#include "error.h"
#include "exec.h"
#include "file.h"
#include "ftype.h"
#include "fwd.h"
#include "handle.h"
#include "int.h"
#include "map.h"
#include "set.h"
#include "str.h"
#ifdef _WIN32
#define USE_WINSOCK /* Else use BSD sockets. */
#endif
#ifdef USE_WINSOCK
/*
* Windows uses a special type to represent its SOCKET descriptors.
* For correctness we include winsock.h here. Other platforms (i.e.,
* BSD) are easier and use integers.
*/
#include <winsock.h>
/*
* The f_hostname() function needs to know how long a host name may
* be. WINSOCK doesn't seem to want to tell us.
*/
#define MAXHOSTNAMELEN (64)
#else /* USE_WINSOCK */
#include <arpa/inet.h>
#include <errno.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/tcp.h> /* TCP_NODELAY */
#include <pwd.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#ifdef isset
#undef isset
#endif
namespace ici
{
/*
* For compatibility with WINSOCK we use its definitions and emulate
* them on Unix.
*/
using SOCKET = int;
inline int closesocket(SOCKET s)
{
return ::close(s);
}
constexpr int SOCKET_ERROR = -1;
#endif /* USE_WINSOCK */
#if !defined(INADDR_LOOPBACK)
/*
* Local loop back IP address (127.0.0.1) in host byte order.
*/
#define INADDR_LOOPBACK (uint32_t)0x7F000001
#endif
inline int socket_fd(handle *h)
{
const auto l = (long)h->h_ptr;
return int(l);
}
/*
* The handle representing our socket it about to be freed. Close the
* socket if it isn't already.
*/
static void socket_prefree(handle *h)
{
if (!h->hasflag(handle::CLOSED))
{
closesocket(socket_fd(h));
}
}
/*
* Create a new socket object with the given descriptor.
*/
static handle *new_netsocket(SOCKET fd)
{
handle *h;
long lfd = fd;
if ((h = new_handle((void *)lfd, SS(socket), nullptr, socket_prefree)) == nullptr)
{
return nullptr;
}
h->clr(handle::CLOSED);
/*
* Turn off super support. This means you can't assign or fetch
* values with a socket.
*/
h->clr(object::O_SUPER);
return h;
}
/*
* Is a socket closed? Set error if so.
*/
static int isclosed(handle *skt)
{
if (skt->hasflag(handle::CLOSED))
{
set_error("attempt to use closed socket");
return 1;
}
return 0;
}
/*
* Do what needs to be done just before calling a potentially
* blocking system call.
*/
static exec *potentially_block(void)
{
signals_invoke_immediately(1);
return leave();
}
static void unblock(exec *x)
{
signals_invoke_immediately(0);
enter(x);
}
/*
* The functions in the 'net' module use strings to specify IP network
* addresses. Addresses may be expressed in one of the forms:
*
* [host:]portnum
*
* or
*
* [host:]service[/protocol]
*
* where '[...]' are optional elements, and:
*
* portnum Is an integer port number.
*
* service Is a service name that will be looked up in the
* services database. (See '/etc/services' on UNIX-like
* systems).
*
* protocol Is either 'tcp' or 'udp'.
*
* host Is either a domain name, an IP address in dotted
* decimal notation, "." for the local address, "?" for
* any, or "*" for all.
*
* This also forms part of the --ici-net-- intro.
*/
/*
* Parse an IP address in the format described above. The host part is
* optional and if not specified defaults to the defhost parameter. The
* address may be nullptr to just initialise the socket address to defhost port
* 0.
*
* The sockaddr structure is filled in and 0 returned if all is okay. When a
* error occurs the error string is set and 1 is returned.
*/
static struct sockaddr_in *parseaddr(const char *raddr, long defhost, struct sockaddr_in *saddr)
{
char addr[512];
char *host;
char *ports;
short port;
/*
* Initialise address so if we fail to find a host or port
* in the string passed in we don't have to deal with it.
* The address is set to the default passed to us while the
* port is set to zero which is used in bind() to have the
* system allocate us a port.
*/
saddr->sin_family = AF_INET;
saddr->sin_addr.s_addr = htonl(defhost);
saddr->sin_port = 0;
port = 0;
if (raddr == nullptr)
{
return saddr;
}
if (strlen(raddr) > sizeof addr - 1)
{
set_error("network address string too long: \"%.32s\"", raddr);
return nullptr;
}
strcpy(addr, raddr);
host = addr;
if ((ports = strchr(addr, ':')) != nullptr)
{
if (ports != addr)
{
*ports++ = '\0';
}
}
else
{
host = nullptr;
ports = addr;
}
if (host != nullptr)
{
struct hostent *hostent;
uint32_t hostaddr;
if (*host == '\0')
{
hostaddr = htonl(INADDR_ANY);
}
else if (!strcmp(host, "."))
{
hostaddr = htonl(INADDR_LOOPBACK);
}
else if (!strcmp(host, "*"))
{
hostaddr = htonl(INADDR_ANY);
}
else if ((hostaddr = inet_addr(host)) != (in_addr_t)-1)
{
/* NOTHING */;
}
else if ((hostent = gethostbyname(host)) != nullptr)
{
memcpy(&hostaddr, (void *)hostent->h_addr, sizeof hostaddr);
}
else
{
set_error("unknown host: \"%.32s\"", host);
return nullptr;
}
saddr->sin_addr.s_addr = hostaddr;
}
if (sscanf(ports, "%hu", &port) != 1)
{
char *proto;
struct servent *servent;
if ((proto = strchr(addr, '/')) != nullptr)
{
*proto++ = 0;
}
if ((servent = getservbyname(ports, proto)) == nullptr)
{
set_error("unknown service: \"%s\"", ports);
return nullptr;
}
port = ntohs(servent->s_port);
}
saddr->sin_port = htons(port);
return saddr;
}
/*
* Turn a port number and IP address into a nice looking string ;-)
*/
static char *unparse_addr(struct sockaddr_in *addr)
{
static char addr_buf[256]; // FQDN limit from DNS
#if 0
struct servent *serv;
#endif
struct hostent *host;
int off;
#if 0
if ((serv = getservbyport(addr->sin_port, nullptr)) != nullptr)
{
strcpy(addr_buf, serv->s_name);
}
else
{
}
#endif
addr_buf[0] = '\0';
if ((host = gethostbyaddr((char *)&addr->sin_addr.s_addr, sizeof addr->sin_addr, AF_INET)) == nullptr)
{
strcat(addr_buf, inet_ntoa(addr->sin_addr));
}
else
{
strcat(addr_buf, host->h_name);
}
strcat(addr_buf, ":");
off = strlen(addr_buf);
if (addr->sin_addr.s_addr == INADDR_ANY)
{
strcpy(addr_buf + off, "*");
}
else if (addr->sin_addr.s_addr == INADDR_LOOPBACK)
{
strcat(addr_buf + off, ".");
}
else
{
sprintf(addr_buf + off, "%u", ntohs(addr->sin_port));
}
return addr_buf;
}
/*
* skt = net.socket([proto])
*
* Create a socket with a certain protocol (currently only TCP or UDP) and return
* its descriptor. Raises exception if the protocol is unknown or the socket
* cannot be created.
*
* Where proto is one of the strings 'tcp', 'tcp/ip', 'udp' or 'udp/ip'. The
* default, if no argument is passed, is 'tcp'. If proto is an int it is a
* file descriptor for a socket and is a socket object is created with that
* file descriptor.
*
* The '/ip' is the start of handling different protocol families (as
* implemented in BSD and WINSOCK 2). For compatibiliy with exisitng ICI
* sockets code the default protocol family is defined to be 'ip'.
*
* Returns a socket object representing a communications end-point.
*
* This --topic-- forms part of the --ici-net-- documentation.
*/
static int net_socket(void)
{
handle *skt;
str *proto;
int type;
SOCKET fd;
if (NARGS() == 0)
{
proto = SS(tcp);
}
else if (typecheck("q", &proto))
{
long sktfd;
if (typecheck("i", &sktfd))
{
return 1;
}
return ret_with_decref(new_netsocket(sktfd));
}
if (proto == SS(tcp))
{
type = SOCK_STREAM;
}
else if (proto == SS(udp))
{
type = SOCK_DGRAM;
}
else
{
return set_error("unsupported protocol or address family: %s", proto->s_chars);
}
if ((fd = socket(PF_INET, type, 0)) == SOCKET_ERROR)
{
return get_last_errno("net.socket", nullptr);
}
if ((skt = new_netsocket(fd)) == nullptr)
{
closesocket(fd);
return 1;
}
return ret_with_decref(skt);
}
/*
* net.close(socket)
*
* Close a socket.
*
* This --topic-- forms part of the --ici-net-- documentation.
*/
static int net_close(void)
{
handle *skt;
if (typecheck("h", SS(socket), &skt))
{
return 1;
}
if (isclosed(skt))
{
return 1;
}
closesocket(socket_fd(skt));
skt->set(handle::CLOSED);
return null_ret();
}
/*
* skt = net.listen(skt [, backlog])
*
* Notify the sytem of willingness to accept connections on 'skt'. This would
* be done to a socket created with 'net.socket()' prior to using
* 'net.accept()' to accept connections. The 'backlog' parameter defines the
* maximum length the queue of pending connections may grow to (default 5).
* Returns the given 'skt'.
*
* This --topic-- forms part of the --ici-net-- documentation.
*/
static int net_listen(void)
{
handle *skt;
long backlog = 5; /* ain't tradition grand */
switch (NARGS())
{
case 1:
if (typecheck("h", SS(socket), &skt))
{
return 1;
}
break;
case 2:
if (typecheck("hi", SS(socket), &skt, &backlog))
{
return 1;
}
break;
default:
return argcount(2);
}
if (isclosed(skt))
{
return 1;
}
if (listen(socket_fd(skt), (int)backlog) == SOCKET_ERROR)
{
return get_last_errno("net.listen", nullptr);
}
return ret_no_decref(skt);
}
/*
* new_skt = net.accept(skt)
*
* Wait for and accept a connection on the socket 'skt'. Returns the
* descriptor for a new socket connection. The argument 'skt' is a socket
* that has been created with 'net.socket()', bound to a local address with
* 'net.bind()', and has been conditioned to listen for connections by a call
* to 'net.listen()'.
*
* This may block, but will allow thread switching while blocked.
*
* This --topic-- forms part of the --ici-net-- documentation.
*/
static int net_accept(void)
{
handle *skt;
SOCKET fd;
exec *x;
if (typecheck("h", SS(socket), &skt))
{
return 1;
}
if (isclosed(skt))
{
return 1;
}
x = potentially_block();
fd = accept(socket_fd(skt), nullptr, nullptr);
unblock(x);
if (fd == SOCKET_ERROR)
{
return get_last_errno("net.accept", nullptr);
}
return ret_with_decref(new_netsocket(fd));
}
/*
* skt = net.connect(skt, address)
*
* Connect 'skt' to 'address'. Returns the socket passed; which is now
* connected to 'address'. While connected, sent data will be directed to the
* address, and only data received from the address will be accepted. See the
* introduction for a description of address formats.
*
* This may block, but will allow thread switching while blocked.
*
* This --topic-- forms part of the --ici-net-- documentation.
*/
static int net_connect(void)
{
handle *skt;
char *addr;
object *arg;
struct sockaddr_in saddr;
exec *x;
int rc;
if (typecheck("ho", SS(socket), &skt, &arg))
{
return 1;
}
if (isstring(arg))
{
addr = stringof(arg)->s_chars;
}
else if (isint(arg))
{
sprintf(buf, "%lld", static_cast<long long int>(intof(arg)->i_value));
addr = buf;
}
else
{
return argerror(1);
}
if (parseaddr(addr, INADDR_LOOPBACK, &saddr) == nullptr)
{
return 1;
}
if (isclosed(skt))
{
return 1;
}
x = potentially_block();
rc = connect(socket_fd(skt), (struct sockaddr *)&saddr, sizeof saddr);
unblock(x);
return rc == SOCKET_ERROR ? get_last_errno("net.connect", nullptr) : ret_no_decref(skt);
}
/*
* skt = net.bind(skt [, address])
*
* Bind the socket 'skt' to the local 'address' so that others may connect to
* it. The given socket is returned.
*
* If 'address' is not given or is 0, the system allocates a local address
* (including port number). In this case the port number allocated can be
* recovered with 'net.getportno()'.
*
* If 'address' is given, it may be an integer, in which case it is
* interpreted as a port number. Otherwise it must be a string and will be
* interpreted as described in the introduction.
*
* If 'address' is given, but has no host portion, "?" (any) is used. Thus,
* for example:
*
* skt = bind(socket("tcp"), port);
*
* Will create a socket that accepts connections to the given port originating
* from any network interface.
*
* This --topic-- forms part of the --ici-net-- documentation.
*/
static int net_bind(void)
{
handle *skt;
const char *addr;
struct sockaddr_in saddr;
if (NARGS() == 2)
{
skt = handleof(ARG(0));
if (!ishandleof(skt, SS(socket)))
{
return argerror(0);
}
if (isstring(ARG(1)))
{
addr = stringof(ARG(1))->s_chars;
}
else if (isint(ARG(1)))
{
sprintf(buf, "%lld", static_cast<long long int>(intof(ARG(1))->i_value));
addr = buf;
}
else
{
return argerror(1);
}
}
else
{
if (typecheck("h", SS(socket), &skt))
{
return 1;
}
addr = "0";
}
if (parseaddr(addr, INADDR_ANY, &saddr) == nullptr)
{
return 1;
}
if (isclosed(skt))
{
return 1;
}
if (bind(socket_fd(skt), (struct sockaddr *)&saddr, sizeof saddr) == SOCKET_ERROR)
{
return get_last_errno("net.bind", nullptr);
}
return ret_no_decref(skt);
}
/*
* Helper function for f_select(). Adds a set of ready socket descriptors
* to the struct object returned by f_select() by scanning the descriptors
* actually selected and seeing if they were returned as being ready. If
* so they are added to a set. Finally the set is added to the struct
* object. Uses, and updates, the count of ready descriptors that is
* returned by select(2) so we can avoid unneccesary work.
*/
static int select_add_result(map *result, str *key, set *theset, fd_set *fds, int *n)
{
set *rset;
SOCKET fd;
size_t i;
slot *sl;
if ((rset = new_set()) == nullptr)
{
return 1;
}
if (theset != nullptr)
{
for (i = 0; *n > 0 && i < theset->s_nslots; ++i)
{
if ((sl = (slot *)&theset->s_slots[i])->sl_key == nullptr)
{
continue;
}
if (!ishandleof(sl->sl_key, SS(socket)))
{
continue;
}
fd = socket_fd(handleof(sl->sl_key));
if (FD_ISSET(fd, fds))
{
--*n;
if (ici_assign(rset, handleof(sl->sl_key), o_one))
{
goto fail;
}
}
}
}
if (ici_assign(result, key, rset))
{
goto fail;
}
decref(rset);
return 0;
fail:
decref(rset);
return 1;
}
/*
* struct = net.select([int,] set|nullptr [, set|nullptr [, set|nullptr]])
*
* Checks sockets for I/O readiness with an optional timeout. Select may be
* passed up to three sets of sockets that are checked for readiness to
* perform I/O. The first set holds the sockets to test for input pending,
* the second set the sockets to test for output able and the third set the
* sockets to test for exceptional states. nullptr may be passed in place of a
* set parameter to avoid passing empty sets. An integer may also appear in
* the parameter list. This integer specifies the number of milliseconds to
* wait for the sockets to become ready. If a zero timeout is passed the
* sockets are polled to test their state. If no timeout is passed the call
* blocks until at least one of the sockets is ready for I/O.
*
* The result of select is a struct containing three sets, of sockets,
* identified by the keys 'read', 'write' and 'except'.
*
* This may block, but will allow thread switching while blocked.
*
* This --topic-- forms part of the --ici-net-- documentation.
*/
/*
* Aldem: dtabsize now is computed (as max found FD). It is more efficient
* than select() on ALL FDs.
*/
static int net_select()
{
int i;
int n;
int dtabsize = -1;
long timeout = -1;
fd_set fds[3];
fd_set *rfds = nullptr;
set *rset = nullptr;
fd_set *wfds = nullptr;
set *wset = nullptr;
fd_set *efds = nullptr;
set *eset = nullptr;
struct timeval timeval;
struct timeval *tv;
map *result;
set *theset = nullptr; /* Init. to remove compiler warning */
int whichset = -1; /* 0 == read, 1 == write, 2 == except*/
slot *sl;
exec *x;
if (NARGS() == 0)
{
return set_error("incorrect number of arguments for net.select()");
}
for (i = 0; i < NARGS(); ++i)
{
if (isint(ARG(i)))
{
if (timeout != -1)
{
return set_error("too many timeout parameters passed to net.select");
}
timeout = intof(ARG(i))->i_value;
if (timeout < 0)
{
return set_error("-ve timeout passed to net.select");
}
}
else if (isset(ARG(i)) || isnull(ARG(i)))
{
size_t j;
if (++whichset > 2)
{
return set_error("too many set/nullptr params to select()");
}
if (isset(ARG(i)))
{
fd_set *fs = 0;
switch (whichset)
{
case 0:
fs = rfds = &fds[0];
theset = rset = setof(ARG(i));
break;
case 1:
fs = wfds = &fds[1];
theset = wset = setof(ARG(i));
break;
case 2:
fs = efds = &fds[2];
theset = eset = setof(ARG(i));
break;
}
FD_ZERO(fs);
for (n = j = 0; j < theset->s_nslots; ++j)
{
int k;
if ((sl = (slot *)&theset->s_slots[j])->sl_key == nullptr)
{
continue;
}
if (!ishandleof(sl->sl_key, SS(socket)))
{
continue;
}
if (isclosed(handleof(sl->sl_key)))
{
// or just ignore closed sockets?
return set_error("attempt to use a closed socket in net.select");
}
k = socket_fd(handleof(sl->sl_key));
FD_SET(k, fs);
if (k > dtabsize)
{
dtabsize = k;
}
++n;
}
if (n == 0)
{
switch (whichset)
{
case 0:
rfds = nullptr;
rset = nullptr;
break;
case 1:
wfds = nullptr;
wset = nullptr;
break;
case 2:
efds = nullptr;
eset = nullptr;
break;
}
}
}
}
else
{
return argerror(i);
}
}
if (rfds == nullptr && wfds == nullptr && efds == nullptr)
{
return set_error("nothing to select, all socket sets are empty");
}
if (timeout == -1)
{
tv = nullptr;
}
else
{
tv = &timeval;
tv->tv_sec = timeout / 1000000;
tv->tv_usec = (timeout % 1000000); /* * 1000000.0; */
}
x = potentially_block();
n = select(dtabsize + 1, rfds, wfds, efds, tv);
unblock(x);
if (n < 0)
{
return get_last_errno("net.select", nullptr);
}
if ((result = new_map()) == nullptr)
{
return 1;
}
/* Add in count */
{
integer *nobj;
if ((nobj = new_int(n)) == nullptr)
{
goto fail;
}
if (ici_assign(result, SS(n), nobj))
{
decref(nobj);
goto fail;
}
decref(nobj);
}
if (select_add_result(result, SS(read), rset, rfds, &n))
{
goto fail;
}
/* Simpler return, one set of ready sockets */
if (NARGS() == 1 && isset(ARG(0)))
{
object *o;
o = ici_fetch(result, SS(read));
incref(o);
decref(result);
return ret_with_decref(o);
}
if (select_add_result(result, SS(write), wset, wfds, &n))
{
goto fail;
}
if (select_add_result(result, SS(except), eset, efds, &n))
{
goto fail;
}
return ret_with_decref(result);
fail:
decref(result);
return 1;
}
/*
* int = net.sendto(skt, msg, address)
*
* Send a 'msg' (a string) to a specific 'address'. This may be used even if
* 'skt' is not connected. If the host portion of the address is missing the
* local host address is used.
*
* Returns the count of the number of bytes transferred.
*
* This --topic-- forms part of the --ici-net-- documentation.
*/
static int net_sendto()
{
char *addr;
str *msg;
int n;
handle *skt;
struct sockaddr_in sockaddr;
if (typecheck("hos", SS(socket), &skt, &msg, &addr))
{
return 1;
}
if (!isstring(msg))
{
return argerror(1);
}
if (parseaddr(addr, INADDR_LOOPBACK, &sockaddr) == nullptr)
{
return 1;
}
if (isclosed(skt))
{
return 1;
}
n = sendto(socket_fd(skt), msg->s_chars, msg->s_nchars, 0, (struct sockaddr *)&sockaddr, sizeof sockaddr);
if (n < 0)
{
return get_last_errno("net.sendto", nullptr);
}
return int_ret(n);
}
#if 0
/*
* Turn a textual send option into the correct bits
*/
static int flagval(const char *flag)
{
if (!strcmp(flag, "oob"))
{
return MSG_OOB;
}
if (!strcmp(flag, "peek"))
{
return MSG_PEEK;
}
if (!strcmp(flag, "dontroute"))
{
return MSG_DONTROUTE;
}
return -1;
}
#endif
/*
* struct = net.recvfrom(skt, int)
*
* Receive a message on the socket 'skt' and return a struct containing the
* data of the message and the source address of the data. The 'int'
* parameter gives the maximum number of bytes to receive. The result is a
* struct with the keys 'msg' (a string) being the data data received and
* 'addr' (a string) the address it was received from (in one of the @
* forms described in the introduction).
*
* This may block, but will allow thread switching while blocked.
*
* This --topic-- forms part of the --ici-net-- documentation.
*/
static int net_recvfrom()
{
handle *skt;
int len;
int nb;
char *msg;
struct sockaddr_in addr;