-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs_io_net.c
executable file
·842 lines (778 loc) · 17.1 KB
/
fs_io_net.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
/*
InterNET File system module for 64net/2
(C) Copyright Paul Gardner-Stephen 1996
NOTES:
File system will appear as:
/hosts/<host_names>/
/nnn/nnn/nnn/nnn/
/listen/
/server/
Each with the following contents:
services/<service_names>
ports/<port_lsb>/<port_msb>
aliases
Description:
/hosts/ is the list of hostnames in /etc/hosts, or aliases in the
64net.hosts file (BUG! - not implemented). Each hostname
is a subdirectory to the internet services offered for any
server (services/ & ports/)
/nnn/nnn/nnn/nnn/ is the list of all possible IP numbers, with sub
dirs as described above.
/server/ is the server 64net resides upon
/aliases is a PRG file containing (in BASIC list form) the list
of IP numbers and names this server has. This will look
up the host to gain this info.
Legend:
nnn - IP Quad (eg 127.0.0.1 = 127/0/0/1) *OR* a byte of a port number
host_names - The list of hosts in /etc/hosts
service_name - The name of a service offered on a standard port #
(This inclusion is added as to simplify life)
This file system will provide several facilities:
* Anonymous FTP access
* HTTP Access
* IRC Access (pre-parsed??)
* TCP/IP Port level connections (active, and passive
on either any port, or a specified one)
* IP number & hostname of localhost (64net server)
This module will be (initially) unavailable in the MS-DOS and possibly
also AMIGA versions, pending information on using sockets under either
of these envirnoments. The code should be sufficiently general to work
under most unicies.
*/
#include "config.h"
#include "fs.h"
#include <fcntl.h>
#ifdef UNIX
#define net_errno errno
#define closesocket close
#ifdef BEOS
#include <NetKit.h>
/* dummy ones */
void setservent (int a) { }
void endservent (void) { }
void *getprotobyname (const char *a) { return NULL; }
/* !!! FIXME this is UGLY!!! */
#define getservbyport
//struct servent *getservbyport (int a, const char* b) { return NULL; }
#else
#include <arpa/inet.h>
#endif
void clear_net_errno(void) { net_errno = 0; }
#endif
#ifdef WINDOWS
#define net_errno WSAGetLastError()
/* dummy ones */
void setservent (int a) { }
void endservent (void) { }
void clear_net_errno(void) { }
#endif
#ifdef AMIGA
#include <bsdsocket.h>
#include <pragmas/socket_pragmas.h>
#include <proto/socket.h>
#include <exec/exec.h>
struct Library BSDBase;
#endif
int fs_net_dirtype (uchar *path);
/* --normal filesystem functions-------------------------------------- */
int
fs_net_createfile (uchar *path, uchar *name, int t, fs64_file * f)
{
if(no_net != 1)
{
/* cant create a new file on the net file space */
return (-1);
}
else
return (-1);
}
int
fs_net_closefile (fs64_file * f)
{
if(no_net != 1)
{
if (f->socket > -1)
closesocket (f->socket);
if ((f->msocket > -1) && (f->sockmode == 0))
closesocket (f->msocket);
f->open = 0;
return (0);
}
else
return (-1);
}
int
fs_net_readchar (fs64_file * f, uchar *c)
{
if(no_net != 1)
{
if (f->socket == -1)
{
if (f->sockmode == 0)
{
int spare;
/* listen with out a connection */
f->socket = accept (f->msocket, (struct sockaddr *) &f->sockaddr, (socklen_t*)&spare);
if (f->socket > -1)
{
/* woo hoo! connected! */
/*
#ifdef WINDOWS
int nonblock=1;
ioctlsocket(f->socket, FIONBIO, &nonblock);
#else
fcntl (f->socket, F_SETFL, O_NONBLOCK);
#endif
*/
if (net_errno) perror ("64net/2");
*c = 'C';
return (2);
}
else
{
*c = 0x00;
return (2);
}
}
else
{
/* bad socket on active */
f->open = 0;
*c = 199;
return (-1);
}
}
else
{
/* read a char from the stream */
clear_net_errno();
#ifdef DEBUG
debug_msg ("reading\n");
#endif
if (read (f->socket, c, 1) == 1)
{
/* success! */
#ifdef DEBUG
debug_msg ("read char\n");
#endif
if (net_errno)
perror ("64net/2");
return (0);
}
else
{
debug_msg ("read nothing - EOF or EDEADLK\n");
if (net_errno != 0)
{
if (net_errno == EDEADLK)
{
/* Resource Temporarily Unavailable
(ie no char yet) */
*c = 0;
return (2);
}
else
{
if (net_errno) perror ("64net/2");
debug_msg ("Closing file (%d)\n", net_errno);
fs_net_closefile(f);
// closesocket (f->socket);
// f->open = 0;
*c = 199;
return (-1);
}
}
else
{ /* remote end disconnected */
*c = 199;
return (-1);
}
}
return (0);
}
}
else
return (-1);
}
int
fs_net_writechar (fs64_file * f, uchar c)
{
if(no_net != 1)
{
if (f->socket > -1)
{
clear_net_errno();
write (f->socket, &c, 1);
if (net_errno)
perror ("64net/2");
}
return (0);
}
else
return (-1);
}
int
fs_net_getinfo (fs64_direntry * de)
{
if(no_net != 1)
return (0);
else
return (-1);
}
int
fs_net_openfile (fs64_file * f)
{
if(no_net != 1)
{
/* open a file */
debug_msg ("Openning\n");
if (f->sockmode == 0)
{
/* listen on port */
f->msocket = socket (AF_INET, SOCK_STREAM, 0);
f->sockaddr.sin_family = AF_INET;
f->sockaddr.sin_addr.s_addr = INADDR_ANY;
f->sockaddr.sin_port = htons (f->port);
f->socket = -1; /* connection not active */
if (bind (f->msocket, (struct sockaddr *) &f->sockaddr, sizeof (f->sockaddr)))
{
/* bind error */
debug_msg ("Bind error\n");
perror ("64net/2");
return (-1);
}
if (listen (f->msocket, 1))
{
/* socket listen error */
debug_msg ("listen error\n");
perror ("64net/2");
return (-1);
}
/* mark file open and return */
f->open = 1;
return (0);
}
else
{
/* active connect */
uchar temp[32];
clear_net_errno();
f->socket = socket (AF_INET, SOCK_STREAM, 0);
f->sockaddr.sin_family = AF_INET;
sprintf ((char*)temp, "%d.%d.%d.%d", ((f->ip >> 24) + 256) & 0xff, ((f->ip >> 16) + 256) & 0xff,
((f->ip >> 8) + 256) & 0xff, ((f->ip & 0xff) + 256) & 0xff);
f->sockaddr.sin_addr.s_addr = inet_addr ((char*)temp);
f->sockaddr.sin_port = htons (f->port);
if ((connect (f->socket, (struct sockaddr *) &f->sockaddr, sizeof (f->sockaddr))) == -1)
{
/* connect error */
debug_msg ("Connect error\n");
perror ("64net/2");
return (-1);
}
/*
#ifdef WINDOWS
{ int nonblock=1;
ioctlsocket(f->socket, FIONBIO, &nonblock);
}
#else
fcntl (f->socket, F_SETFL, O_NONBLOCK);
#endif
*/
if (net_errno) perror ("non-blocking");
f->open = 1;
return (0);
}
return (-1);
}
else
return (-1);
}
int
fs_net_getopenablename (fs64_file * f, fs64_direntry * de)
{
uchar pe[8][17];
unsigned int pec = 0, pel = 0;
unsigned int i;
uchar path[256];
if(no_net != 1)
{
strcpy ((char*)path, (char*)de->filesys.fspath);
/* seperate path elements into a list */
for (i = 2; i < strlen (path); i++)
{
if (path[i] == '/')
{
pe[pec++][pel] = 0;
pel = 0;
}
else
pe[pec][pel++] = path[i];
}
/* terminate last term */
if (pel)
pe[pec][pel] = 0;
else
pec--;
debug_msg ("PEC: %d\n", pec);
for (i = 0; i <= pec; i++)
debug_msg ("PE %d [%s]\n", i, pe[i]);
f->sockmode = 1; /* active */
/* Calculate internet address and socket modes etc */
debug_msg ("dir type %d\n", de->dirtype);
switch (de->dirtype)
{
case net_PORTDIR:
{
switch (pec)
{
case 5:
/* IP */
f->ip = (1 << 24) * atol (pe[0]);
f->ip += (1 << 16) * atol (pe[1]);
f->ip += (1 << 8) * atol (pe[2]);
f->ip += atol (pe[3]);
break;
case 2:
if (!strcmp ("LISTEN", (char*)pe[0]))
{
/* listen */
f->ip = 0xffffffff;
f->sockmode = 0;
}
else
{
/* server */
f->ip = 0x7f000001;
}
break;
case 3:
/* hosts */
break;
}
f->port = atol (pe[pec]) + 256 * atol (de->fs64name);
}
break;
case net_SERVICESDIR:
switch (pec)
{
case 4:
/* ip */
f->ip = (1 << 24) * atol (pe[0]);
f->ip += (1 << 16) * atol (pe[1]);
f->ip += (1 << 8) * atol (pe[2]);
f->ip += atol (pe[3]);
break;
case 1:
/* listen or server */
if (!strcmp ((char*)pe[0], "SERVER"))
f->ip = 0x7f000001; /* server */
else
{
f->ip = 0xffffffff; /* listen */
f->sockmode = 0;
}
break;
case 2:
/* hosts */
return (-1);
}
f->port = de->blocks;
break;
default:
return (-1);
}
debug_msg ("IP: %08x, port %04x, mode %d\n", f->ip, f->port, f->sockmode);
return (0);
}
else
return (-1);
}
int
fs_net_scratchfile (fs64_direntry * de)
{
if(no_net != 1)
return (1);
else
return (-1);
}
int
fs_net_headername (uchar *path, uchar *header, uchar *id, int par)
{
if(no_net != 1)
{
/* use right 16 chars from path */
int i, j;
header[0] = 0;
i = strlen (path) - 1;
/* strip trailing '/' */
if (path[i] == '/')
i--;
/* find start of this "bit" */
while ((i) && (path[i] != '/'))
i--;
/* strip leading '/' on name */
if (path[i] == '/')
i++;
/* copy chars */
j = 0;
for (; !((path[i] == '/') || (path[i] == 0)); i++)
header[j++] = path[i];
/* end f the string */
header[j] = 0;
/* default */
if ((!strcmp ((char*)path, "/")) || (header[0] == 0))
sprintf ((char*)header, "INTERNET");
strcpy ((char*)id, "TCPIP");
return (0);
}
else
return (-1);
}
int
fs_net_findnext (fs64_direntry * de)
{
int i;
uchar temp[255];
if(no_net != 1)
{
switch (de->dirtype)
{
case net_ROOTDIR:
{
for (i = 0; i < 16; i++)
de->fs64name[i] = 0xa0;
de->filetype = cbm_DIR | cbm_CLOSED | cbm_LOCKED;
de->blocks = 0;
de->invisible = 0;
switch (de->intcount)
{
case 0:
strcpy ((char*)de->fs64name, "SERVER");
break;
case 1:
strcpy ((char*)de->fs64name, "HOSTS");
break;
case 2:
strcpy ((char*)de->fs64name, "LISTEN");
break;
default:
sprintf ((char*)temp, "%d", de->intcount - 3);
strcpy ((char*)de->fs64name, (char*)temp);
}
if (de->intcount == 259)
{
de->active = 0;
return (1);
}
de->intcount++;
sprintf ((char*)de->realname, "%s%s/", de->filesys.fspath, de->fs64name);
for (i = 0; i < 16; i++)
if (de->fs64name[i] == 0)
de->fs64name[i] = 0xa0;
return (0);
};
break;
case net_SERVERDIR:
{
for (i = 0; i < 16; i++)
de->fs64name[i] = 0xa0;
de->filetype = cbm_DIR | cbm_CLOSED | cbm_LOCKED;
de->blocks = 0;
de->invisible = 0;
switch (de->intcount)
{
case 0:
strcpy ((char*)de->fs64name, "SERVICES");
break;
case 1:
strcpy ((char*)de->fs64name, "PORTS");
break;
case 2:
strcpy ((char*)de->fs64name, "ALIASES");
de->filetype = cbm_PRG | cbm_CLOSED | cbm_LOCKED;
break;
default:
{
de->active = 0;
return (1);
};
}
de->intcount++;
sprintf ((char*)de->realname, "%s%s/", de->filesys.fspath, de->fs64name);
for (i = 0; i < 16; i++)
if (de->fs64name[i] == 0)
de->fs64name[i] = 0xa0;
return (0);
};
break;
case net_BYTEDIR:
{
for (i = 0; i < 16; i++)
de->fs64name[i] = 0xa0;
de->filetype = cbm_DIR | cbm_CLOSED | cbm_LOCKED;
de->blocks = 0;
de->invisible = 0;
switch (de->intcount)
{
default:
sprintf ((char*)temp, "%d", de->intcount);
strcpy ((char*)de->fs64name, (char*)temp);
}
if (de->intcount == 256)
{
de->active = 0;
return (1);
}
de->intcount++;
sprintf ((char*)de->realname, "%s%s/", de->filesys.fspath, de->fs64name);
for (i = 0; i < 16; i++)
if (de->fs64name[i] == 0)
de->fs64name[i] = 0xa0;
return (0);
};
break;
case net_PORTDIR:
{
for (i = 0; i < 16; i++)
de->fs64name[i] = 0xa0;
de->filetype = cbm_NET | cbm_CLOSED;
de->blocks = 0;
de->invisible = 0;
switch (de->intcount)
{
default:
sprintf ((char*)temp, "%d", de->intcount);
strcpy ((char*)de->fs64name, (char*)temp);
}
if (de->intcount == 256)
{
de->active = 0;
return (1);
}
de->intcount++;
sprintf ((char*)de->realname, "%s%s/", de->filesys.fspath, de->fs64name);
for (i = 0; i < 16; i++)
if (de->fs64name[i] == 0)
de->fs64name[i] = 0xa0;
return (0);
};
break;
case net_SERVICESDIR:
{
for (i = 0; i < 16; i++)
de->fs64name[i] = 0xa0;
de->blocks = 0;
de->invisible = 0;
{
struct servent *se;
struct protoent *pn = getprotobyname ("tcp");
#ifdef BEOS
/* !!! FIXME !!! */
se = NULL;
#else
se = getservbyport (htons (de->intcount), pn->p_name);
#endif
while ((!se) && (de->intcount < 1023))
{
de->intcount++;
#ifdef BEOS
/* !!! FIXME !!! */
se = NULL;
#else
se = getservbyport (htons (de->intcount), pn->p_name);
#endif
}
if (se)
{
int j;
de->blocks = ntohs (se->s_port);
de->filetype = cbm_NET | cbm_CLOSED | cbm_LOCKED;
sprintf ((char*)temp, "%s", se->s_name);
temp[16] = 0;
for (j = 0; j < 16; j++)
temp[j] = toupper (temp[j]);
strcpy ((char*)de->fs64name, (char*)temp);
}
else
{
endservent ();
de->active = 0;
return (-1);
}
}
de->intcount++;
sprintf ((char*)de->realname, "%s%s/", de->filesys.fspath, de->fs64name);
for (i = 0; i < 16; i++)
if (de->fs64name[i] == 0)
de->fs64name[i] = 0xa0;
return (0);
};
break;
default:
de->active = 0;
return (-1);
}
}
else
return (-1);
}
int
fs_net_openfind (fs64_direntry * de, uchar *path2)
{
/* Dir searches are easy in the net fs
BUG: Doesnt allow for transparent FTP access yet */
uchar path[1024];
if(no_net != 1)
{
/* step 1 - get dir of preceding @ if present */
if (path2[0] == '@')
strcpy ((char*)path, (char*)path2);
else
strcpy ((char*)path, (char*)&path2[1]);
/* step 2 - work out the directory type */
de->dirtype = fs_net_dirtype (path);
/* rewind services file */
if (de->dirtype == 4)
setservent (0);
/* step 3 - search from start of dir */
de->intcount = 0;
/* step 4 - Copy path to de->filesys.fspath */
strcpy ((char*)de->filesys.fspath, (char*)path);
de->active = 1;
return (0);
}
else
return (-1);
}
int
fs_net_writeblock (fs64_file * f)
{
if(no_net != 1)
{
debug_msg ("Pretending to write a block to a inet stream\n");
return (-1);
}
else
return (-1);
}
int
fs_net_readblock (fs64_file * f)
{
if(no_net != 1)
{
/* Read a block from a network file stream */
return (-1);
}
else
return (-1);
}
/* --Other routines (inet and filesystem support etc..)--------------- */
int
fs_net_dirtype (uchar *path2)
{
/* what type of synthesised directory is this?
BUG: Doesnt allow for transparent FTP access yet */
uchar pe[8][17];
unsigned int pec = 0, pel = 0;
unsigned int i;
uchar path[256];
if(no_net != 1)
{
strcpy ((char*)path, (char*)&path2[1]);
if (!strcmp ((char*)path, "/"))
return (net_ROOTDIR);
/* seperate path elements into a list */
for (i = 1; i < strlen (path); i++)
{
if (path[i] == '/')
{
pe[pec++][pel] = 0;
pel = 0;
}
else
pe[pec][pel++] = path[i];
}
/* terminate last term */
if (pel)
pe[pec][pel] = 0;
else
pec--;
debug_msg ("PEC: %d\n", pec);
for (i = 0; i <= pec; i++)
debug_msg ("PE %d [%s]\n", i, pe[i]);
/* work out the dir type */
if (!strcmp ((char*)pe[0], "HOSTS"))
{
if (pec == 0)
return (net_HOSTSDIR);
if (pec == 1)
return (net_SERVERDIR);
if (!strcmp ((char*)pe[2], "SERVICES"))
return (net_SERVICESDIR);
if (!strcmp ((char*)pe[2], "PORTS"))
{
if (pec == 1)
return (net_BYTEDIR);
if (pec == 2)
return (net_PORTDIR);
}
}
if (!strcmp ((char*)pe[0], "LISTEN"))
{
if (pec == 0)
return (net_SERVERDIR);
if (!strcmp ((char*)pe[1], "SERVICES"))
return (net_SERVICESDIR);
if (!strcmp ((char*)pe[1], "PORTS"))
{
if (pec == 1)
return (net_BYTEDIR);
if (pec == 2)
return (net_PORTDIR);
}
}
if (!strcmp ((char*)pe[0], "SERVER"))
{
if (pec == 0)
return (net_SERVERDIR);
if (!strcmp ((char*)pe[1], "SERVICES"))
return (net_SERVICESDIR);
if (!strcmp ((char*)pe[1], "PORTS"))
{
if (pec == 1)
return (net_BYTEDIR);
if (pec == 2)
return (net_PORTDIR);
}
}
/* ip based directory */
if (pec < 3)
return (net_BYTEDIR);
if (pec == 3)
return (net_SERVERDIR);
if (pec == 4)
{
if (!strcmp ((char*)pe[4], "SERVICES"))
return (net_SERVICESDIR);
else
return (net_BYTEDIR);
}
if (pec == 5)
return (net_PORTDIR);
debug_msg ("Bad net dir!\n");
return (-1);
}
else
{
return (-1);
}
}
int
fs_net_blocksfree (fs64_filesystem * fs)
{
if(no_net != 1)
return (0);
else
return (-1);
}