File: Platform_NetLib_Unix.cpp

package info (click to toggle)
pose 3.0a3-3
  • links: PTS
  • area: contrib
  • in suites: potato
  • size: 15,500 kB
  • ctags: 20,548
  • sloc: ansic: 72,579; cpp: 50,198; perl: 1,336; python: 1,242; sh: 363; makefile: 290
file content (950 lines) | stat: -rw-r--r-- 23,649 bytes parent folder | download
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
/* -*- mode: C++; tab-width: 4 -*- */
/* ================================================================================== */
/* Copyright (c) 1998-1999 3Com Corporation or its subsidiaries. All rights reserved. */
/* ================================================================================== */

#include "EmulatorCommon.h"

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <unistd.h>
#include <errno.h>
#include <netdb.h>

#include "Platform_NetLib.h"

// ===== COPIED FROM NETPRV.H =====

// Max # of open built-in sockets at one time.
#define 	netMaxNumSockets		4						// Max # of sockets
#define 	netMinSocketRefNum	(sysFileDescNet)							 // lowest refNum
#define 	netMaxSocketRefNum	(sysFileDescNet+netMaxNumSockets-1) // highest refNum

static long gOpenCount;


Bool Platform::NetLib::Redirecting (void)
{
	return true;
}


//--------------------------------------------------
// Library initialization, shutdown, sleep and wake
//--------------------------------------------------
Err    Platform::NetLib::Open(Word libRefnum, WordPtr netIFErrsP)
{
	if (gOpenCount > 0) {
		++gOpenCount;
		return netErrAlreadyOpen;
	}
	++gOpenCount;
	return errNone;
}

Err    Platform::NetLib::Close(Word libRefnum, Word immediate)
{
	if (gOpenCount <= 0) {
		return netErrNotOpen;
	}
	--gOpenCount;
	return errNone;
}

Err    Platform::NetLib::Sleep(Word libRefnum)
{
	// stub. Do Nothing
	return errNone;
}

Err Platform::NetLib::Wake(Word libRefnum)
{
	// stub. Do Nothing
	return errNone;
}

// This call forces the library to complete a close if it's
//	currently in the close-wait state. Returns 0 if library is closed,
//	Returns netErrFullyOpen if library is still open by some other task.
Err    Platform::NetLib::FinishCloseWait(Word libRefnum)
{
	// stub. Do Nothing
	return errNone;
}

// This call is for use by the Network preference panel only. It
// causes the NetLib to fully open if it's currently in the close-wait 
//	state. If it's not in the close wait state, it returns an error code
Err    Platform::NetLib::OpenIfCloseWait(Word libRefnum)
{
	return netErrNotOpen;
}

// Get the open Count of the NetLib
Err    Platform::NetLib::OpenCount(Word refNum, WordPtr countP)
{
	*countP = gOpenCount;
	return errNone;
}
// Give NetLib a chance to close the connection down in response
// to a power off event. Returns non-zero if power should not be
//	turned off. EventP points to the event that initiated the power off
//	which is either a keyDownEvent of the hardPowerChr or the autoOffChr.
// Don't include unless building for Viewer
Err    Platform::NetLib::HandlePowerOff (Word refNum, EventPtr eventP)
{
	return Platform::NetLib::Close(refNum, true);
}
// Check status or try and reconnect any interfaces which have come down.
// This call can be made by applications when they suspect that an interface
// has come down (like PPP or SLIP). NOTE: This call can display UI 
// (if 'refresh' is true) so it MUST be called from the UI task. 
Err    Platform::NetLib::ConnectionRefresh(Word refNum, 
										   Boolean refresh, 
										   BytePtr allInterfacesUpP, 
										   WordPtr netIFErrP)
{
	*netIFErrP =0;
	return errNone;
}


#if 0
//--------------------------------------------------
// Net address translation and conversion routines.
//--------------------------------------------------

// Convert 32-bit IP address to ascii dotted decimal form. The Sockets glue
//	macro inet_ntoa will pass the address of an application global string in
//	spaceP.
CharPtr Platform::NetLib::AddrINToA(Word libRefnum, 
									NetIPAddr	inet, 
									CharPtr spaceP)
{
	CharPtr piece;
	UInt		i;
	union {
		Byte		bytes[4];
		DWord	wholeThing;
	} inPieces;
	
	
	// Fill in the union with the 32-bit form.
	inPieces.wholeThing = inet;
	piece = spaceP;
	
	// Print ascii format to piece string
	for (i = 0; i < 4; i++) {
		if (inPieces.bytes[i] >= 100)
			*piece++ = (char)('0' + ((inPieces.bytes[i]/100) % 10));
		if (inPieces.bytes[i] >= 10)
			*piece++ = (char)('0' + ((inPieces.bytes[i]/10) % 10));
		*piece++ = (char)('0' + inPieces.bytes[i] % 10);
		if (i == 3)
			*piece = '\0';
		else
			*piece++ = '.';
	}
	
	// Return pointer to string
	return spaceP;
	
}

// Convert a dotted decimal ascii string format of an IP address into
//	a 32-bit value.
NetIPAddr Platform::NetLib::AddrAToIN(Word libRefnum, CharPtr name)
{
	UInt		i;
	CharPtr	piece;
	
	union {
		Byte	bytes[4];
		DWord	wholeThing;
	} inPieces;
	
	
	/* Skip leading whitespace */
	while ((*name == ' ') || (*name == '\t'))
		name++;
	
	for (i = 0; i < 4; i++) {
		piece = name;
		
		// Skip to the end of this dotted decimal
		while ((*name >= '0') && (*name <= '9'))
			name++;
		
		// Drop out if 0 length
		if (piece == name)
			return -1;				   /* zero length piece */
		
		// Convert this section to a byte
		inPieces.bytes[i] = atoi(piece);
		
		// Drop out if last byte
		if (i == 3)
			break;					  /* don't need a . after last piece */
		
		// Make sure it's followed by a '.'
		if (*name != '.')
			return -1;				   /* do need a . after other pieces */
		
		// On to next section
		name++;
	}
	
	return inPieces.wholeThing;
}
#endif


//--------------------------------------------------
// Socket creation and option setting
//--------------------------------------------------

// Create a socket and return a refnum to it. Protocol is normally 0. 
// Returns 0 on success, -1 on error. If error, *errP gets filled in with error code.
NetSocketRef Platform::NetLib::SocketOpen(Word libRefnum, 
										  NetSocketAddrEnum socketDomain, 
										  NetSocketTypeEnum socketType, 
										  SWord socketProtocol, 
										  SDWord timeout, 
										  Err* errP)
{
	int result;
	
	*errP = 0;
	
	result = socket (socketDomain, socketType, socketProtocol);
	
	if (result == -1){
		*errP = errno;
	}
	return result;
}

// Close a socket. 
// Returns 0 on success, -1 on error. If error, *errP gets filled in with error code.
SWord Platform::NetLib::SocketClose(Word libRefnum, 
									NetSocketRef sRef, 
									SDWord timeout, 							
									Err* errP)
{
	int iResult;
	
	*errP = 0;
	
	if (sRef == -1) {
		*errP = netErrSocketNotOpen;
		return -1;
	}
	
	iResult = close(sRef);
	if (iResult) {
		*errP = errno;
		return -1;
	}
	return 0;
}

// Set a socket option.    Level is usually netSocketOptLevelSocket. Option is one of
//	netSocketOptXXXXX. OptValueP is a pointer to the new value and optValueLen is
//	the length of the option value. 			   
// Returns 0 on success, -1 on error. If error, *errP gets filled in with error code.
SWord Platform::NetLib::SocketOptionSet(Word libRefnum, 
										NetSocketRef sRef,
										NetSocketOptLevelEnum level, 
										NetSocketOptEnum opt, 
										VoidPtr optvalP, 
										Word optlen,
										SDWord timeout, 
										Err* errP)
{
	int iResult;
	
	*errP = 0;
	iResult = setsockopt(sRef, level, opt, (const char *)optvalP, optlen);
	if (iResult) {
		*errP = errno;
		return -1;
	}
	return 0;
}

// Get a socket option. 				   
// Returns 0 on success, -1 on error. If error, *errP gets filled in with error code.
SWord Platform::NetLib::SocketOptionGet(Word libRefnum, 
										NetSocketRef sRef,
										NetSocketOptLevelEnum level, 
										NetSocketOptEnum opt,
										VoidPtr optValueP, 
										WordPtr optValueLenP,
										SDWord timeout, 
										Err* errP)
{
	int iResult;
	
	*errP = 0;
	iResult = getsockopt( sRef, level, opt, (char *)optValueP, (int *)optValueLenP);
	
	if (iResult) {
		*errP = errno;
		return -1;
	}
	return 0;
	
	
}

//--------------------------------------------------
// Socket Control
//--------------------------------------------------

// Bind a source address and port number to a socket. This makes the
//	socket accept incoming packets destined for the given socket address.
// Returns 0 on success, -1 on error. If error, *errP gets filled in with error code.
SWord Platform::NetLib::SocketBind(Word libRefnum, 
								   NetSocketRef sRef,
								   NetSocketAddrType* sockAddrP, 
								   SWord addrLen, 
								   SDWord timeout, 
								   Err* errP)
{
	int iResult;
	
	*errP = 0;
	iResult = bind( sRef, (const struct sockaddr *)sockAddrP, addrLen);
	
	if (iResult) {
		*errP = errno;
		return -1;
	}
	return 0;
}


// Connect to a remote socket. For a stream based socket (i.e. TCP), this initiates
//	a 3-way handshake with the remote machine to establish a connection. For
//	non-stream based socket, this merely specifies a destination address and port
//	number for future outgoing packets from this socket.
// Returns 0 on success, -1 on error. If error, *errP gets filled in with error code.
SWord Platform::NetLib::SocketConnect(Word libRefnum, 
									  NetSocketRef sRef,
									  NetSocketAddrType* sockAddrP, 
									  SWord addrLen, 
									  SDWord timeout, 
									  Err* errP)
{
	int iResult;
	
	*errP = 0;
	iResult = connect( sRef, (const struct sockaddr *)sockAddrP, addrLen);
	
	if (iResult) {
		*errP = errno;
		return -1;
	}
	return 0;
}
// Makes a socket ready to accept incoming connection requests. The queueLen 
//	specifies the max number of pending connection requests that will be enqueued
//	while the server is busy handling other requests.
//	Only applies to stream based (i.e. TCP) sockets.
// Returns 0 on success, -1 on error. If error, *errP gets filled in with error code.
SWord Platform::NetLib::SocketListen(Word libRefnum, 
									 NetSocketRef sRef,
									 Word	 queueLen, 
									 SDWord timeout, 
									 Err* errP)
{
	int iResult;
	
	*errP = 0;
	iResult = listen(sRef, queueLen);
	
	if (iResult) {
		*errP = errno;
		return -1;
	}
	return 0;
}

// Blocks the current process waiting for an incoming connection request. The socket
//	must have previously be put into listen mode through the NetLibSocketListen call.
//	On return, *sockAddrP will have the remote machines address and port number.
//	Only applies to stream based (i.e. TCP) sockets.
// Returns 0 on success, -1 on error. If error, *errP gets filled in with error code.
SWord Platform::NetLib::SocketAccept(Word libRefnum, 
									 NetSocketRef sRef,
									 NetSocketAddrType* sockAddrP, 
									 SWord* addrLenP, 
									 SDWord timeout,
									 Err* errP)
{
	int iResult;
	
	*errP = 0;
	iResult = accept( sRef, (struct sockaddr *)sockAddrP, (int *)addrLenP);
	
	if (iResult) {
		*errP = errno;
		return -1;
	}
	return 0;
}

// Shutdown a connection in one or both directions.  
//	Only applies to stream based (i.e. TCP) sockets.
// Returns 0 on success, -1 on error. If error, *errP gets filled in with error code.
SWord Platform::NetLib::SocketShutdown(Word libRefnum, 
									   NetSocketRef sRef, 
									   SWord direction, 
									   SDWord timeout, 
									   Err* errP)
{
	int iResult;
	
	*errP = 0;
	iResult = shutdown(sRef,direction);
	
	if (iResult) {
		*errP = errno;
		return -1;
	}
	return 0;
}

// Gets the local and remote addresses of a socket. Useful for TCP sockets that 
//	get dynamically bound at connect time. 
// Returns 0 on success, -1 on error. If error, *errP gets filled in with error code.
SWord Platform::NetLib::SocketAddr(Word libRefnum, 
								   NetSocketRef sRef,
								   NetSocketAddrType* locAddrP, 
								   SWord* locAddrLenP, 
								   NetSocketAddrType* remAddrP, 
								   SWord* remAddrLenP, 
								   SDWord timeout, 
								   Err* errP)
{
	int iResult;
	
	*errP = 0;
	if ((locAddrP != NULL) && (locAddrLenP != NULL)) {
		iResult = getsockname( sRef, 
			(struct sockaddr *)locAddrP, 
			(int *)locAddrLenP);
	} else { // if ((remAddrP != NULL) && (remAddrLenP != NULL)){
		iResult = getpeername( sRef, 
			(struct sockaddr *)remAddrP,
			(int *)remAddrLenP);
	}
	
	if (iResult) {
		*errP = errno;
		return -1;
	}
	return 0;
}

//--------------------------------------------------
// Sending and Receiving
//--------------------------------------------------
// Send data through a socket. The data is specified through the NetIOParamType
//	structure.
// Flags is one or more of netMsgFlagXXX.
// Returns # of bytes sent on success, or -1 on error. If error, *errP gets filled 
//	in with error code.
SWord Platform::NetLib::SendPB(Word libRefNum, 
							   NetSocketRef sRef,
							   NetIOParamType* pbP, 
							   Word flags, 
							   SDWord timeout, 
							   Err* errP)
{
	int iResult;
	
	*errP = 0;
	// TODO - what about recvmsg and sendmsg?
	iResult = send( sRef, (const char *)pbP, sizeof(NetIOParamType), flags);
	
	if (iResult) {
		*errP = errno;
		return -1;
	}
	return 0;
}


// Send data through a socket. The data to send is passed in a single buffer,
//	unlike NetLibSendPB. If toAddrP is not nil, the data will be sent to 
//	address *toAddrP.
// Flags is one or more of netMsgFlagXXX.
// Returns # of bytes sent on success, or -1 on error. If error, *errP gets filled 
//	in with error code.
SWord Platform::NetLib::Send(Word libRefNum, 
							 NetSocketRef sRef,
							 const VoidPtr bufP, 
							 Word bufLen, 
							 Word flags,
							 VoidPtr toAddrP, 
							 Word toLen, 
							 SDWord timeout, 
							 Err* errP)
{
	int iResult;
	
	*errP = 0;
	iResult = send( sRef, (const char *)bufP, (int)bufLen, (int)flags);
	
	if (iResult) {
		*errP = errno;
		return -1;
	}
	return 0;
}

// Receive data from a socket. The data is gatthered into buffers specified in the 
//	NetIOParamType structure.
// Flags is one or more of netMsgFlagXXX.
// Timeout is max # of ticks to wait, or -1 for infinite, or 0 for none.
// Returns # of bytes received, or -1 on error. If error, *errP gets filled in 
//	with error code.
SWord Platform::NetLib::ReceivePB(Word libRefNum, 
								  NetSocketRef sRef,
								  NetIOParamType* pbP, 
								  Word flags, 
								  SDWord timeout, 
								  Err* errP)
{
	int iResult;
	
	*errP = 0;
	// TODO - what about recvmsg and sendmsg?
	iResult = recv( sRef, (char *)pbP, sizeof(NetIOParamType), flags);
	
	if (iResult) {
		*errP = errno;
		return -1;
	}
	return 0;
}

// Receive data from a socket. The data is read into a single buffer, unlike
//	NetLibReceivePB. If fromAddrP is not nil, *fromLenP must be initialized to
//	the size of the buffer that fromAddrP points to and on exit *fromAddrP will
//	have the address of the sender in it.
// Flags is one or more of netMsgFlagXXX.
// Timeout is max # of ticks to wait, or -1 for infinite, or 0 for none.
// Returns # of bytes received, or -1 on error. If error, *errP gets filled in 
//	with error code.
SWord Platform::NetLib::Receive(Word libRefNum, 
								NetSocketRef sRef,
								VoidPtr bufP, 
								Word bufLen, 
								Word flags, 
								VoidPtr fromAddrP, 
								WordPtr fromLenP, 
								SDWord timeout, 
								Err* errP)
{
	int iResult;
	
	*errP = 0;
	iResult = recvfrom ( sRef, 
		(char * )bufP, 
		(int) bufLen, 
		(int) flags, 
		(struct sockaddr *) fromAddrP, 
		(int *) fromLenP); 
	if (iResult) {
		*errP = errno;
		return -1;
	}
	return 0;
}

// Receive data from a socket directly into a (write-protected) Data Manager 
//	record. 
// If fromAddrP is not nil, *fromLenP must be initialized to
//	the size of the buffer that fromAddrP points to and on exit *fromAddrP will
//	have the address of the sender in it.
// Flags is one or more of netMsgFlagXXX.
// Timeout is max # of ticks to wait, or -1 for infinite, or 0 for none.
// Returns # of bytes received, or -1 on error. If error, *errP gets filled in 
//	with error code.
SWord Platform::NetLib::DmReceive(Word libRefNum, 
								  NetSocketRef sRef,
								  VoidPtr recordP, 
								  DWord recordOffset, 
								  Word rcvLen, 
								  Word flags, 
								  VoidPtr fromAddrP, 
								  WordPtr fromLenP, 
								  SDWord timeout, 
								  Err* errP)
{
	return -1;
}

//--------------------------------------------------
// Name Lookups
//--------------------------------------------------
NetHostInfoPtr	  Platform::NetLib::GetHostByName(Word libRefNum, 
												  CharPtr nameP, 
												  NetHostInfoBufPtr bufP, 
												  Long	  timeout, 
												  Err* errP)
{
	struct hostent * pHostEnt;
	
	*errP = 0;
	
	pHostEnt = gethostbyname(nameP);
	if (!pHostEnt){
		*errP = errno;
		return NULL;
	}
	memcpy((void *)&(bufP->hostInfo), pHostEnt, sizeof(struct hostent));
	return &(bufP->hostInfo);
}

NetHostInfoPtr	  Platform::NetLib::GetHostByAddr(Word libRefNum, 
												  BytePtr addrP, 
												  Word len, 
												  Word type,
												  NetHostInfoBufPtr bufP, 
												  Long	  timeout, 
												  Err* errP)
{
	struct hostent * pHostEnt;
	
	*errP = 0;
	pHostEnt = gethostbyaddr( (const char *)addrP, len, type);
	
	if (!pHostEnt){
		*errP = errno;
		return NULL;
	}
	memcpy((void *)&(bufP->hostInfo), pHostEnt, sizeof(struct hostent));
	return &(bufP->hostInfo);
}

NetServInfoPtr	  Platform::NetLib::GetServByName(Word libRefNum, 
												  CharPtr servNameP, 
												  CharPtr protoNameP,  
												  NetServInfoBufPtr bufP, 
												  Long	  timeout, 
												  Err* errP)
{
	struct servent *pServent;
	
	*errP = 0;
	pServent = getservbyname(servNameP, protoNameP);
	
	if (!pServent) {
		*errP = errno;
		return NULL;
	}
	memcpy((void *)&(bufP->servInfo), pServent, sizeof(NetServInfoType));
	return &(bufP->servInfo);
}

// Looks up a mail exchange name and returns a list of hostnames for it. Caller
//	must pass space for list of return names (hostNames), space for 
//	list of priorities for those hosts (priorities) and max # of names to 
//	return (maxEntries).
// Returns # of entries found, or -1 on error. If error, *errP gets filled in
//	with error code.
SWord Platform::NetLib::GetMailExchangeByName(Word libRefNum, CharPtr mailNameP, 
											  Word maxEntries, 
											  Char hostNames[][netDNSMaxDomainName+1], Word priorities[], 
											  Long	timeout, Err* errP)
{
	*errP = 0;
	// TODO
	return -1;
}

#if 0
//--------------------------------------------------
// Interface setup
//--------------------------------------------------
Err Platform::NetLib::IFGet(Word libRefNum, 
							Word index, 
							DWordPtr ifCreatorP, 
							WordPtr ifInstanceP)
{
	return netErrInvalidInterface;
}


Err Platform::NetLib::IFAttach(Word libRefNum, DWord ifCreator, Word ifInstance,
							   SDWord timeout)
{
	// TODO
	// This should probably validate the creator or something
	return netErrInvalidInterface;
}

Err Platform::NetLib::IFDetach(Word libRefNum, DWord ifCreator, Word ifInstance,
							   SDWord timeout)
{
	// TODO
	// This should probably validate the creator or something
	return netErrInvalidInterface;
}

Err Platform::NetLib::IFUp(Word libRefNum, DWord ifCreator, Word ifInstance)
{
	// TODO
	// This should probably validate the creator or something
	return 0;
}

Err Platform::NetLib::IFDown(Word libRefNum, DWord ifCreator, Word ifInstance,
							 SDWord timeout)
{
	// TODO
	// This should probably validate the creator or something
	return 0;
}
#endif



#if 0
//--------------------------------------------------
// Settings
//--------------------------------------------------
// General settings
Err Platform::NetLib::SettingGet(Word libRefNum,
								 Word /*NetSettingEnum*/ setting, 
								 VoidPtr valueP, 
								 WordPtr valueLenP)
{
	// TODO
	return -1;
}

Err Platform::NetLib::SettingSet(Word libRefNum, 
								 Word /*NetSettingEnum*/ setting, 
								 VoidPtr valueP, 
								 Word valueLen)
{
	// TODO
	return -1;
}

// Network interface specific settings.
Err Platform::NetLib::IFSettingGet(Word libRefNum, 
								   DWord ifCreator, 
								   Word ifInstance,
								   Word /*NetIFSettingEnum*/ setting, 
								   VoidPtr valueP, 
								   WordPtr valueLenP)
{
	// TODO
	return -1;
}

Err Platform::NetLib::IFSettingSet(Word libRefNum, 
								   DWord ifCreator, 
								   Word ifInstance,
								   Word /*NetIFSettingEnum*/ setting, 
								   VoidPtr valueP, 
								   Word valueLen)
{
	// TODO
	return -1;
}
#endif

//--------------------------------------------------
// System level
//--------------------------------------------------
SWord Platform::NetLib::Select(Word libRefNum, 
							   Word width, 
							   NetFDSetType* readFDs, 
							   NetFDSetType* writeFDs, 
							   NetFDSetType* exceptFDs,
							   Long    timeout, 
							   Err* errP)
{
	SWord sResult;
	fd_set readfds;
	fd_set writefds;
	fd_set exceptfds;
	timeval timeOut;
	timeOut.tv_usec = timeout;
	timeOut.tv_sec = 0;
	
	
	FD_ZERO(&readfds);
	FD_ZERO(&writefds);
	FD_ZERO(&exceptfds);
	
	unsigned int fd;
	
	if (readFDs) {
		
		// Check built-in sockets
		for (fd=0; fd <= netMaxSocketRefNum && fd < width; fd++)  {
			if (netFDIsSet(fd, readFDs)) {
				FD_SET(fd, &readfds);
			}
		}
	}
	
	// Check ready outputs
	if (writeFDs) {
		// check built-in sockets. 
		for (fd=0; fd <= netMaxSocketRefNum && fd < width; fd++)  {
			if (netFDIsSet(fd, writeFDs)) {
				FD_SET(fd, &writefds);
			}
		}
	}
	netFDZero(readFDs);
	netFDZero(writeFDs);
	netFDZero(exceptFDs);
	
	*errP = 0;
	sResult = select( width, &readfds, &writefds, &exceptfds, &timeOut);
	
	if (sResult == -1) {
		*errP = errno;
		return -1;
	}
	if (sResult) {
		// Check built-in sockets
		for (fd=0; fd <= netMaxSocketRefNum && fd < width; fd++)  {
			if (FD_ISSET(fd, &readfds)) {
				netFDSet(fd, readFDs);
			}
		}
		// check built-in sockets. 
		for (fd=0; fd <= netMaxSocketRefNum && fd < width; fd++)  {
			if (FD_ISSET(fd, &writefds)) {
				netFDSet(fd, writeFDs);
			}
		}
		// check built-in sockets. 
		for (fd=0; fd <= netMaxSocketRefNum && fd < width; fd++)  {
			if (FD_ISSET(fd, &exceptfds)) {
				netFDSet(fd, writeFDs);
			}
		}
	}
	return sResult;
}


#if 0
//--------------------------------------------------
// Debugging support
//--------------------------------------------------
Err Platform::NetLib::Master(Word libRefNum, 
							 Word cmd, 
							 NetMasterPBPtr pbP,
							 Long timeout)
{
	// TODO
	return 0;
}


Err Platform::NetLib::TracePrintF(Word libRefNum, CharPtr formatStr, ...)
{
	va_list argList;
	char szBuffer[256];
	
	va_start(argList, formatStr);
	sprintf(szBuffer, formatStr, argList);
	// TODO - some poser trace call.
	va_end(argList);
	return 0;
}


Err Platform::NetLib::TracePutS(Word libRefNum, CharPtr strP)
{
	// TODO - some poser trace call.
	return 0;
}
#endif


//--------------------------------------------------
// Configuration Calls
//--------------------------------------------------
Err Platform::NetLib::OpenConfig( Word refNum, 
								 Word configIndex, 
								 DWord openFlags,
								 WordPtr netIFErrP)
{
	int iResult;
	
	// Init return error 
	*netIFErrP = 0;
	// TODO
	return -1;
}

#if 0
Err Platform::NetLib::ConfigMakeActive( Word refNum, Word configIndex)
{
	// TODO
	return 0;
}

Err Platform::NetLib::ConfigList( Word refNum, 
								 NetConfigNameType nameArray[],
								 WordPtr arrayEntriesP)
{
	// TODO
	return -1;
}

Err Platform::NetLib::ConfigIndexFromName( Word refNum, 
										  NetConfigNamePtr nameP,
										  WordPtr indexP)
{
	// TODO
	return -1;
}

Err Platform::NetLib::ConfigDelete( Word refNum, Word index)
{
	// TODO
	return -1;
}

Err Platform::NetLib::ConfigSaveAs( Word refNum, NetConfigNamePtr nameP)
{
	// TODO
	return -1;
}

Err Platform::NetLib::ConfigRename( Word refNum, Word index,
								   NetConfigNamePtr newNameP)
{
	// TODO
	return -1;
}

Err Platform::NetLib::ConfigAliasSet( Word refNum, Word configIndex,
									 Word aliasToIndex)
{
	// TODO
	return -1;
}

Err Platform::NetLib::ConfigAliasGet( Word refNum, Word aliasIndex,
									 WordPtr indexP, BooleanPtr isAnotherAliasP)
{
	// TODO
	return -1;
}
#endif