-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinet.c
685 lines (593 loc) · 14.4 KB
/
inet.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
/* @(#)highwire/inet.c
*/
#if defined(USE_OVL)
# undef USE_OVL
# define _USE_OVL_
#elif defined (USE_INET)
# if defined(__GNUC__)
# define USE_MINT
# elif defined(__PUREC__)
# if !defined(USE_ICNN) && !defined(USE_STIK) && !defined(USE_MINT) && !defined(USE_MAGICNET)
# define USE_STIK /* also if USE_STNG is already defined, (nearly) same API */
# endif
# endif
static WORD sockets_free = 0;
#endif /* USE_INET */
#include <stddef.h>
#ifndef __HW_INET_H__
# include "hw-types.h"
# include "inet.h"
#endif
#if defined(_USE_OVL_) /*******************************************************/
# include <stdio.h>
# include "ovl_sys.h"
static OVL_METH * inet_ovl = NULL;
static short __CDECL demand_host_addr (const char * host, long * addr);
static long __CDECL demand_connect (long addr, long port, long tout_sec);
INET_FTAB inet_ftab = {
demand_host_addr,
demand_connect,
inet_send,
inet_recv,
inet_close,
inet_instat,
inet_select,
inet_info
}, backup;
/*----------------------------------------------------------------------------*/
static void ovl_invalid (void * ignore)
{
(void)ignore;
if (inet_ovl) {
inet_ovl = NULL;
inet_ftab = backup;
}
}
/*----------------------------------------------------------------------------*/
static BOOL ovl_load(void)
{
INET_FTAB * ovl_ftab = NULL;
if (inet_ovl) {
return TRUE;
} else if ((inet_ovl = load_ovl ("network.ovl", ovl_invalid)) == NULL) {
return FALSE; /* no OVL found */
} else if (inet_ovl->ftabtype != FTAB_NETWORK ||
(*inet_ovl->ovl_init)() < INET_VERSION ||
(ovl_ftab = (*inet_ovl->ovl_getftab)()) == NULL) {
puts ("inet::ovl_load(): wrong network.ovl!");
inet_ovl = NULL;
kill_ovl (inet_ovl);
return FALSE; /* wrong OVL */
}
backup = inet_ftab;
inet_ftab = *ovl_ftab;
return TRUE;
}
/*----------------------------------------------------------------------------*/
static short __CDECL demand_host_addr (const char * host, long * addr)
{
return (ovl_load() ? (*inet_ftab.host_addr)(host, addr)
: inet_host_addr (host, addr));
}
/*----------------------------------------------------------------------------*/
static long __CDECL demand_connect (long addr, long port, long tout_sec)
{
return (ovl_load() ? (*inet_ftab.connect)(addr, port, tout_sec)
: inet_connect (addr, port, tout_sec));
}
/* endif defined(_USE_OVL_) */
#elif defined(USE_MINT) || defined(USE_MAGICNET) /*****************************/
#if defined(USE_MINT)
# include <netdb.h>
# include <sys/socket.h>
# include <netinet/in.h>
# include <unistd.h>
# include <mintbind.h>
# include <errno.h>
#else /*USE_MAGICNET*/
# include <magicnet/netdb.h>
# include <magicnet/sys/socket.h>
# include <magicnet/netinet/in.h>
# include <unistd.h>
# include <mintbind.h>
# undef E_OK
# undef EPROTONOSUPPORT
# undef ECONNRESET
# undef ETIMEDOUT
# include <magicnet/errno.h>
#endif
/*----------------------------------------------------------------------------*/
static BOOL init_mintnet (void)
{
static BOOL __once = TRUE;
if (__once) {
short n;
sockets_free = 32;
for (n = 0; n < 32; n++) {
if (Finstat (n) >= 0 || Foutstat (n) >= 0) sockets_free--;
}
if ((sockets_free -= 2) > 0) { /* always save two free handles */
__once = FALSE;
} else {
sockets_free = 0;
}
}
return (sockets_free > 0);
}
#elif defined(USE_MAGICNET) /**************************************************/
# include <magicnet/netdb.h>
# include <magicnet/sys/socket.h>
# include <magicnet/netinet/in.h>
# include <magicnet/unistd.h>
# include <mintbind.h>
/*----------------------------------------------------------------------------*/
static BOOL init_mintnet (void)
{
static BOOL __once = TRUE;
if (__once) {
short n;
sockets_free = 32;
for (n = 0; n < 32; n++) {
if (Finstat (n) >= 0 || Foutstat (n) >= 0) sockets_free--;
}
if ((sockets_free -= 2) > 0) { /* always save two free handles */
__once = FALSE;
} else {
sockets_free = 0;
}
}
return (sockets_free > 0);
}
#elif defined(USE_ICNN) /******************************************************/
# include <time.h>
# include <string.h>
# include <iconnect/sockinit.h>
# include <iconnect/netdb.h>
# include <iconnect/types.h>
# include <iconnect/socket.h>
# include <iconnect/in.h>
# include <iconnect/sfcntl.h>
# include <iconnect/sockios.h>
/*----------------------------------------------------------------------------*/
static BOOL init_iconnect (void)
{
static int flag = -1;
if (flag < 0) {
flag = (sock_init() == E_OK ? 1 : 0);
sockets_free = 32;
}
return (flag > 0);
}
#elif defined(USE_STIK) /******************************************************/
# include <stdio.h> /*printf/puts */
# include <string.h>
# include <tos.h>
# include <time.h>
# ifdef USE_STNG
# include <sting/transprt.h>
# else /*STiK2*/
# include <sting/transprt.h>
# endif
#undef min
#define min(x,y) (((x)<(y))?(x):(y))
#define TCP_OBUFF_SIZE 2048 /* TCP_open/TCP_send */
static TPL * tpl = NULL;
/*----------------------------------------------------------------------------*/
static BOOL init_stik (void)
{
if (!tpl) {
struct {
long cktag;
long ckvalue;
} * jar = (void*)Setexc (0x5A0 /4, (void (*)())-1);
long tag = 'STiK';
while (jar->cktag) {
if (jar->cktag == tag) {
DRV_LIST * drivers = (DRV_LIST*)jar->ckvalue;
if (strcmp (STIK_DRVR_MAGIC, drivers->magic) == 0) {
tpl = (TPL*)get_dftab(TRANSPORT_DRIVER);
}
break;
}
jar++;
}
sockets_free = 32;
}
return (tpl != NULL);
}
#endif /* USE_STIK ************************************************************/
/*============================================================================*/
short __CDECL
inet_host_addr (const char * name, long * addr)
{
short ret = -1;
#if defined(USE_MINT) || defined(USE_MAGICNET)
struct hostent * host = gethostbyname (name);
if (host) {
*addr = *(long*)host->h_addr;
ret = E_OK;
} else {
ret = -errno;
}
#elif defined(USE_ICNN)
if (init_iconnect()) {
struct hostent * host = gethostbyname ((char*)name);
if (host) {
*addr = *(long*)host->h_addr;
ret = E_OK;
}
}
#elif defined(USE_STIK)
if (init_stik()) {
if (resolve ((char*)name, NULL, (uint32*)addr, 1) > 0) {
ret = E_OK;
}
}
#else
(void)name; (void)addr;
#endif
return ret;
}
/*----------------------------------------------------------------------------*/
#if defined(USE_MINT) || defined(USE_STIK) || defined(USE_MAGICNET)
static BOOL sig_tout = FALSE;
#ifndef __mint_sighandler_t_defined
typedef void *__mint_sighandler_t;
#endif
static void __CDECL sig_alrm (long sig)
{
(void)sig;
sig_tout = TRUE;
}
#endif
/*============================================================================*/
long __CDECL
inet_connect (long addr, long port, long tout_sec)
{
int fh = -1;
#if defined(USE_MINT) || defined(USE_MAGICNET)
if (!init_mintnet()) {
fh = -35/*EMFILE*/;
} else {
struct sockaddr_in s_in;
s_in.sin_family = AF_INET;
s_in.sin_port = htons ((short)port);
s_in.sin_addr.s_addr = addr;
if ((fh = socket (PF_INET, SOCK_STREAM, 0)) < 0) {
fh = -errno;
} else {
__mint_sighandler_t alrm = (__mint_sighandler_t)Psignal (14/*SIGALRM*/, sig_alrm);
if ((long)alrm >= 0) {
sig_tout = FALSE;
Talarm (tout_sec);
}
if (connect (fh, (struct sockaddr *)&s_in, sizeof (s_in)) < 0) {
close (fh);
fh = -(sig_tout && errno == EINTR ? ETIMEDOUT : errno);
} else {
sockets_free--;
}
if ((long)alrm >= 0) {
Talarm (0);
Psignal (14/*SIGALRM*/, alrm);
}
}
}
#elif defined(USE_ICNN)
if (sockets_free <= 0) {
fh = -35/*EMFILE*/;
} else {
clock_t timeout = 0;
sockaddr_in s_in;
s_in.sin_family = AF_INET;
s_in.sin_port = htons ((short)port);
s_in.sin_addr = addr;
do {
if ((fh = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
fh = -1;
break;
} else {
int n = connect ((int)fh, &s_in, (int)sizeof(s_in));
if (n == E_OK) {
sfcntl ((int)fh, F_SETFL, O_NDELAY);
sockets_free--;
break;
}
sclose ((int)fh);
fh = -1;
if (!timeout) {
timeout = clock() + tout_sec * CLK_TCK;
} else if (n != -ETIMEDOUT || clock() < timeout) {
break;
}
}
} while (1);
}
#elif defined(USE_STIK)
if (!init_stik()) {
puts ("No STiK/Sting");
} else if (sockets_free <= 0) {
fh = -35/*EMFILE*/;
} else {
__mint_sighandler_t alrm = (__mint_sighandler_t)Psignal (14/*SIGALRM*/, sig_alrm);
if ((long)alrm >= 0) {
sig_tout = FALSE;
Talarm (tout_sec);
}
if ((fh = TCP_open (addr, (short)port, 0, TCP_OBUFF_SIZE)) < 0) {
fh = -(fh == -1001L ? ETIMEDOUT : 1);
} else {
sockets_free--;
}
if ((long)alrm >= 0) {
Talarm (0);
Psignal (14/*SIGALRM*/, alrm);
}
}
#else
(void)addr; (void)port; (void)tout_sec;
#endif
return fh;
}
/*============================================================================*/
long __CDECL
inet_send (long fh, const char * buf, size_t len)
{
long ret = 0;
#if defined(USE_MINT) || defined(USE_MAGICNET)
while (len) {
long n = Fwrite (fh, len, buf);
if (n < 0) {
ret = n;
break;
} else {
ret += n;
buf += n;
len -= n;
}
}
#elif defined(USE_ICNN)
while (len) {
short n = swrite ((int)fh, buf, (int)min(len, 16384l));
if (n < 0) {
ret = n;
break;
} else {
ret += n;
buf += n;
len -= n;
}
}
#elif defined(USE_STIK)
if (!tpl) {
puts ("No STiK/Sting");
} else while (len) {
int16 n = (int16)min(len, TCP_OBUFF_SIZE);
int16 r;
if ((r = TCP_send ((int)fh, (char*)buf, n)) < E_OBUFFULL) {
ret = -1;
break;
} else if (r == E_NORMAL) {
ret += n;
buf += n;
len -= n;
}
}
#else
(void)fh; (void)buf; (void)len;
ret = -1;
#endif
return ret;
}
/*============================================================================*/
long __CDECL
inet_recv (long fh, char * buf, size_t len)
{
long ret = 0;
#if defined(USE_MINT) || defined(USE_MAGICNET)
while (len) {
long n = Finstat (fh);
if (n < 0) {
if (!ret) ret = n;
break;
} else if (n == 0x7FFFFFFFL) { /* connection closed */
if (!ret) ret = -ECONNRESET;
break;
} else if (n && (n = Fread (fh, (n < len ? n : len), buf)) < 0) {
if (!ret) ret = -errno;
break;
} else if (n) {
ret += n;
buf += n;
len -= n;
} else { /* no data available yet */
break;
}
}
#elif defined(USE_ICNN)
while (len) {
long n = sread ((int)fh, buf, len);
if (n < 0) {
if (!ret) ret = n;
break;
} else if (n) {
ret += n;
buf += n;
len -= n;
} else { /* no data available yet */
break;
}
}
#elif defined(USE_STIK)
if (!tpl) {
puts ("No STiK/Sting");
ret = -1;
} else while (len) {
short n = CNbyte_count ((int)fh);
if (n < E_NODATA) {
if (!ret) ret = (n == E_EOF || n == E_RRESET ? -ECONNRESET : -1);
break;
} else if (n > 0) {
if (n > len) n = len;
if ((n = CNget_block ((int)fh, buf, n)) < 0) {
if (!ret) ret = -1;
break;
} else {
ret += n;
buf += n;
len -= n;
}
} else { /* no data available yet */
break;
}
}
#else
(void)fh; (void)buf; (void)len;
ret = -1;
#endif
return ret;
}
/*============================================================================*/
void __CDECL
inet_close (long fh)
{
if (fh >= 0) {
#if defined(USE_MINT) || defined(USE_MAGICNET)
if (close ((int)fh) == 0) sockets_free++;
#elif defined(USE_ICNN)
if (sclose ((int)fh) == 0) sockets_free++;
#elif defined(USE_STIK)
if (!tpl) {
puts ("No STiK/Sting");
} else {
if (TCP_close ((int)fh, 0, NULL) == 0) sockets_free++;
}
#endif
}
}
/*============================================================================*/
long __CDECL
inet_instat (long fh)
{
long ret = -1;
#if defined(USE_MINT) || defined(USE_MAGICNET)
ret = Finstat (fh);
if (ret == 0x7FFFFFFFL) { /* connection closed */
ret = -ECONNRESET;
}
#elif defined(USE_ICNN)
char buf[1024];
ret = recv ((int)fh, buf, sizeof(buf), (int)MSG_PEEK);
#elif defined(USE_STIK)
if (!tpl) {
puts ("No STiK/Sting");
} else {
ret = CNbyte_count ((int)fh);
if (ret < E_NODATA) {
ret = (ret == E_EOF || ret == E_RRESET ? -ECONNRESET : -1);
}
}
#else
(void)fh;
#endif
return ret;
}
/*============================================================================*/
long __CDECL
inet_select (long timeout, long * rfds, long * wfds) /* timeout is milliseconds */
{
long ret = 0;
#if defined(USE_MINT) || defined(USE_MAGICNET)
ret = Fselect (timeout, rfds, wfds, NULL);
#elif defined(USE_ICNN)
struct timeval to_in;
fd_set * p_rf, * p_wf;
if (rfds && *rfds) {
static fd_set i_rf;
char * c_rf = (char*)FD_ZERO (&i_rf);
c_rf[0] = ((char*)&rfds)[3];
c_rf[1] = ((char*)&rfds)[2];
c_rf[2] = ((char*)&rfds)[1];
c_rf[3] = ((char*)&rfds)[0];
p_rf = &i_rf;
} else {
p_rf = NULL;
}
if (wfds && *wfds) {
static fd_set i_wf;
char * c_wf = (char*)FD_ZERO (&i_wf);
c_wf[0] = ((char*)&wfds)[3];
c_wf[1] = ((char*)&wfds)[2];
c_wf[2] = ((char*)&wfds)[1];
c_wf[3] = ((char*)&wfds)[0];
p_wf = &i_wf;
} else {
p_wf = NULL;
}
to_in.tv_sec = (int)(timeout /1000); /* calc seconds from milliseconds */
to_in.tv_usec = (int)((timeout%1000)*1000); /* calc remainder in microsecs */
ret = select((int)32, p_rf, p_wf, NULL, &to_in);
if (p_rf) {
char * c_rf = (char*)p_rf;
((char*)&rfds)[3] = c_rf[0];
((char*)&rfds)[2] = c_rf[1];
((char*)&rfds)[1] = c_rf[2];
((char*)&rfds)[0] = c_rf[3];
}
if (p_wf) {
char * c_wf = (char*)p_wf;
((char*)&wfds)[3] = c_wf[0];
((char*)&wfds)[2] = c_wf[1];
((char*)&wfds)[1] = c_wf[2];
((char*)&wfds)[0] = c_wf[3];
}
#elif defined(USE_STIK)
short bit;
long rf_in, wf_in, rf_out, wf_out;
rf_in = wf_in = rf_out = wf_out = 0;
if (rfds) rf_in = *rfds;
if (wfds) wf_in = *wfds;
do {
for (bit = 0; bit < 32; bit++) {
long mask = 1 << bit;
if ((rf_in & mask) || (wf_in & mask)) {
short n = CNbyte_count ((int)bit);
if (n == E_BADHANDLE) {
rf_out = wf_out = 0;
ret = -1;
break;
}
if ((rf_in & mask) && (n != 0)) {
ret++;
rf_out |= mask;
}
if (wf_in & mask) ret++;
}
}
} while ((timeout == 0) && (ret == 0));
if (ret > 0) wf_out = wf_in;
if (rfds) *rfds = rf_out;
if (wfds) *wfds = wf_out;
#else
(void)timeout; (void)rfds; (void)wfds;
#endif
return ret;
}
/*============================================================================*/
const char * __CDECL
inet_info (void)
{
#if defined(USE_MINT)
return "MiNTnet";
#elif defined(USE_MAGICNET)
return "MagiCNet";
#elif defined(USE_ICNN)
return "Iconnect";
#elif defined(USE_STNG)
return "Sting";
#elif defined(USE_STIK)
return "STiK2";
#else
return NULL;
#endif
}