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
|
/*********************************************************************
Project : GUSI - Grand Unified Socket Interface
File : GUSINetDB.cp - Convert internet names to adresses
Author : Matthias Neeracher
This file was derived from the socket library by
Charlie Reiman <creiman@ncsa.uiuc.edu> and
Tom Milligan <milligan@madhaus.utcs.utoronto.ca>
Language : MPW C++
$Log$
Revision 1.1 2000/09/12 20:24:49 jack
Moved to Unsupported.
Revision 1.1 1998/08/18 14:52:38 jack
Putting Python-specific GUSI modifications under CVS.
Revision 1.3 1994/08/10 00:07:30 neeri
Sanitized for universal headers.
Revision 1.2 1994/05/01 23:43:31 neeri
getservbyname() without /etc/services would fail.
Revision 1.1 1994/02/25 02:29:36 neeri
Initial revision
Revision 0.5 1993/10/31 00:00:00 neeri
Deferred opening of resolver
Revision 0.4 1993/07/29 00:00:00 neeri
Real getservent code (adapted from Sak Wathanasin)
Revision 0.3 1993/01/19 00:00:00 neeri
Can't set aliases to NULL.
Revision 0.2 1992/11/21 00:00:00 neeri
Remove force_active
Revision 0.1 1992/09/14 00:00:00 neeri
Maybe it works, maybe it doesn't
*********************************************************************/
#include "GUSIINET_P.h"
#include "TFileSpec.h"
#include "Folders.h"
#include "PLStringFuncs.h"
#ifdef __MWERKS__
//
// I disapprove of the way dnr.c is written
// This disapproval gets stronger with every version
//
#include "dnr.c"
#pragma require_prototypes reset
#pragma cplusplus reset
#endif
#if GENERATING68K
#pragma segment GUSIINET
#endif
static pascal void DNRDone(struct hostInfo *, Boolean * done)
{
*done = true;
}
#if GENERATINGCFM
RoutineDescriptor uDNRDone =
BUILD_ROUTINE_DESCRIPTOR(uppResultProcInfo, DNRDone);
#else
#define uDNRDone DNRDone
#endif
int h_errno;
/*
* Gethostbyname and gethostbyaddr each return a pointer to an
* object with the following structure describing an Internet
* host referenced by name or by address, respectively. This
* structure contains the information obtained from the MacTCP
* name server.
*
* struct hostent
* {
* char *h_name;
* char **h_aliases;
* int h_addrtype;
* int h_length;
* char **h_addr_list;
* };
* #define h_addr h_addr_list[0]
*
* The members of this structure are:
*
* h_name Official name of the host.
*
* h_aliases A zero terminated array of alternate names for the host.
*
* h_addrtype The type of address being returned; always AF_INET.
*
* h_length The length, in bytes, of the address.
*
* h_addr_list A zero terminated array of network addresses for the host.
*
* Error return status from gethostbyname and gethostbyaddr is
* indicated by return of a null pointer. The external integer
* h_errno may then be checked to see whether this is a
* temporary failure or an invalid or unknown host. The
* routine herror can be used to print an error message
* describing the failure. If its argument string is non-NULL,
* it is printed, followed by a colon and a space. The error
* message is printed with a trailing newline.
*
* h_errno can have the following values:
*
* HOST_NOT_FOUND No such host is known.
*
* TRY_AGAIN This is usually a temporary error and
* means that the local server did not
* receive a response from an authoritative
* server. A retry at some later time may
* succeed.
*
* NO_RECOVERY Some unexpected server failure was encountered.
* This is a non-recoverable error.
*
* NO_DATA The requested name is valid but does not
* have an IP address; this is not a
* temporary error. This means that the name
* is known to the name server but there is
* no address associated with this name.
* Another type of request to the name server
* using this domain name will result in an
* answer; for example, a mail-forwarder may
* be registered for this domain.
* (NOT GENERATED BY THIS IMPLEMENTATION)
*/
static struct hostInfo macHost;
#define MAXALIASES 0
static char *aliasPtrs[MAXALIASES+1] = {NULL};
static ip_addr *addrPtrs[NUM_ALT_ADDRS+1];
static struct hostent unixHost =
{
macHost.cname,
aliasPtrs,
AF_INET,
sizeof(ip_addr),
(char **) addrPtrs
};
inline struct in_addr make_in_addr(ip_addr addr)
{
struct in_addr res;
res.s_addr = addr;
return res;
}
struct hostent * gethostbyname(char *name)
{
Boolean done;
int i;
if (!strcmp(name, "localhost")) {
in_addr ipaddr;
ipaddr = make_in_addr(ip_addr(gethostid()));
if (ipaddr.s_addr)
return gethostbyaddr((char *) &ipaddr, sizeof(in_addr), AF_INET);
h_errno = HOST_NOT_FOUND;
return NULL;
}
if (INETSockets.Resolver()) {
h_errno = NO_RECOVERY;
return NULL;
}
for (i=0; i<NUM_ALT_ADDRS; i++)
macHost.addr[i] = 0;
done = false;
if (StrToAddr(name, &macHost, ResultUPP(&uDNRDone), (char *) &done) == cacheFault)
SPINP(!done,SP_NAME,0L);
switch (macHost.rtnCode) {
case noErr: break;
case nameSyntaxErr: h_errno = HOST_NOT_FOUND; return(NULL);
case cacheFault: h_errno = NO_RECOVERY; return(NULL);
case noResultProc: h_errno = NO_RECOVERY; return(NULL);
case noNameServer: h_errno = HOST_NOT_FOUND; return(NULL);
case authNameErr: h_errno = HOST_NOT_FOUND; return(NULL);
case noAnsErr: h_errno = TRY_AGAIN; return(NULL);
case dnrErr: h_errno = NO_RECOVERY; return(NULL);
case outOfMemory: h_errno = TRY_AGAIN; return(NULL);
default: h_errno = NO_RECOVERY; return(NULL);
}
/* was the 'name' an IP address? */
if (macHost.cname[0] == 0) {
h_errno = HOST_NOT_FOUND;
return(NULL);
}
/* for some reason there is a dot at the end of the name */
i = int(strlen(macHost.cname)) - 1;
if (macHost.cname[i] == '.')
macHost.cname[i] = 0;
for (i=0; i<NUM_ALT_ADDRS && macHost.addr[i]!=0; i++)
addrPtrs[i] = (ip_addr *) &macHost.addr[i];
addrPtrs[i] = NULL;
return &unixHost;
}
struct hostent * gethostbyaddr(const char *addrP, int, int)
{
Boolean done;
int i;
if (INETSockets.Resolver()) {
h_errno = NO_RECOVERY;
return NULL;
}
for (i=0; i<NUM_ALT_ADDRS; i++)
macHost.addr[i] = 0;
done = false;
ip_addr addr = FIX_LOOPBACK(*(ip_addr *)addrP);
if (AddrToName(addr, &macHost, ResultUPP(&uDNRDone), (char *) &done) == cacheFault)
SPINP(!done,SP_ADDR,0L);
switch (macHost.rtnCode) {
case noErr: break;
case cacheFault: h_errno = NO_RECOVERY; return(NULL);
case noNameServer: h_errno = HOST_NOT_FOUND; return(NULL);
case authNameErr: h_errno = HOST_NOT_FOUND; return(NULL);
case noAnsErr: h_errno = TRY_AGAIN; return(NULL);
case dnrErr: h_errno = NO_RECOVERY; return(NULL);
case outOfMemory: h_errno = TRY_AGAIN; return(NULL);
default: h_errno = NO_RECOVERY; return(NULL);
}
/* for some reason there is a dot at the end of the name */
i = int(strlen(macHost.cname)) - 1;
if (macHost.cname[i] == '.')
macHost.cname[i] = 0;
/* For some reason, the IP address usually seems to be set to 0 */
if (!macHost.addr[0])
macHost.addr[0] = addr;
for (i=0; i<NUM_ALT_ADDRS; i++)
addrPtrs[i] = (ip_addr *) &macHost.addr[i];
addrPtrs[NUM_ALT_ADDRS] = NULL;
return &unixHost;
}
char * inet_ntoa(struct in_addr inaddr)
{
if (INETSockets.Resolver()) {
h_errno = NO_RECOVERY;
return NULL;
}
(void) AddrToStr(inaddr.s_addr, macHost.cname);
return macHost.cname;
}
struct in_addr inet_addr(char *address)
{
if (INETSockets.Resolver()) {
h_errno = NO_RECOVERY;
return make_in_addr(0xFFFFFFFF);
}
if (StrToAddr(address,&macHost,NULL,NULL) != noErr)
return make_in_addr(0xFFFFFFFF);
/* was the 'address' really a name? */
if (macHost.cname[0] != 0)
return make_in_addr(0xFFFFFFFF);
return make_in_addr(macHost.addr[0]);
}
/*
* gethostid()
*
* Get internet address of current host
*/
long gethostid()
{
static long sHostID = 0;
if (sHostID)
return sHostID;
struct GetAddrParamBlock pbr;
pbr.ioCRefNum = INETSockets.Driver();
pbr.csCode = ipctlGetAddr;
if (PBControlSync(ParmBlkPtr(&pbr)))
return 0;
else
return sHostID = (long)pbr.ourAddress;
}
/*
* gethostname()
*
* Try to get my host name from DNR. If it fails, just return my
* IP address as ASCII. This is non-standard, but it's a mac,
* what do you want me to do?
*/
int gethostname(char *machname, int buflen)
{
static char * sHostName = nil;
if (!sHostName) {
in_addr ipaddr;
struct hostent *hp;
ipaddr = make_in_addr(ip_addr(gethostid()));
if (!ipaddr.s_addr) // TCP/IP not up at all
return GUSI_error(ENETDOWN);
hp = gethostbyaddr((char *) &ipaddr, sizeof(in_addr), AF_INET);
if (!hp) {
// No good name
if (buflen < 16) // Not enough space
return GUSI_error(EINVAL);
sprintf(machname, "%d.%d.%d.%d",
ipaddr.s_addr>>24,
ipaddr.s_addr>>16 & 0xff,
ipaddr.s_addr>>8 & 0xff,
ipaddr.s_addr & 0xff);
return 0;
} else {
// We only cache satisfactory replies in sHostName
sHostName = new char[strlen(hp->h_name)+1];
strcpy(sHostName, hp->h_name);
}
}
strncpy(machname, sHostName, unsigned(buflen));
machname[buflen-1] = 0; /* extra safeguard */
return 0;
}
/*
* getservbybname()
*
*/
static char * servlist[] =
{
"echo 7/udp",
"discard 9/udp",
"time 37/udp",
"domain 53/udp",
"sunrpc 111/udp",
"tftp 69/udp",
"biff 512/udp",
"who 513/udp",
"talk 517/udp",
"ftp-data 20/tcp",
"ftp 21/tcp",
"telnet 23/tcp",
"smtp 25/tcp",
"time 37/tcp",
"whois 43/tcp",
"domain 53/tcp",
"hostnames 101/tcp",
"nntp 119/tcp",
"finger 79/tcp",
"ntp 123/tcp",
"uucp 540/tcp",
NULL
};
static char servline[128];
static struct servent serv;
static FILE * servfil;
static int servptr;
static char * servalias[8];
static int servstay = 0;
void setservent(int stayopen)
{
if (servfil && servfil != (FILE *) -1) {
rewind(servfil);
}
servptr = 0;
servstay = servstay || stayopen;
}
void endservent()
{
if (servfil && servfil != (FILE *) -1) {
fclose(servfil);
servfil = NULL;
}
servstay = 0;
}
struct servent * getservent()
{
char * p;
int aliascount;
if (!servfil) {
TFileSpec serv;
if (!FindFolder(
kOnSystemDisk,
kPreferencesFolderType,
kDontCreateFolder,
&serv.vRefNum,
&serv.parID)
) {
PLstrcpy(serv.name, (StringPtr) "\p/etc/services");
if (servfil = fopen(serv.FullPath(), "r"))
goto retry;
}
servfil = (FILE *) -1;
servptr = 0;
}
retry:
if (servfil == (FILE *) -1)
if (!servlist[servptr])
return (struct servent *) NULL;
else
strcpy(servline, servlist[servptr++]);
else if (!(fgets(servline, 128, servfil)))
return (struct servent *) NULL;
if (p = strpbrk(servline, "#\n\r"))
*p = 0;
if (!servline[0])
goto retry;
if (!(serv.s_name = strtok(servline, " \t")))
goto retry;
if (!(p = strtok(NULL, " \t")))
goto retry;
if (!(serv.s_proto = strpbrk(p, "/,")))
goto retry;
*serv.s_proto++ = 0;
serv.s_port = htons(atoi(p));
serv.s_aliases = servalias;
for (aliascount = 0; aliascount < 7; )
if (!(servalias[aliascount++] = strtok(NULL, " \t")))
break;
servalias[aliascount] = NULL;
return &serv;
}
struct servent * getservbyname(const char * name, const char * proto)
{
struct servent * ent;
char ** al;
setservent(0);
while (ent = getservent()) {
if (!strcmp(name, ent->s_name))
goto haveName;
for (al = ent->s_aliases; *al; ++al)
if (!strcmp(name, *al))
goto haveName;
continue;
haveName:
if (!proto || !strcmp(proto, ent->s_proto))
break;
}
if (!servstay)
endservent();
return ent;
}
struct servent * getservbyport(int port, const char * proto)
{
struct servent * ent;
setservent(0);
while (ent = getservent())
if (port == ent->s_port && (!proto || !strcmp(proto, ent->s_proto)))
break;
if (!servstay)
endservent();
return ent;
}
static char tcp[] = "tcp";
static char udp[] = "udp";
#define MAX_PROTOENT 10
static struct protoent protoents[MAX_PROTOENT];
static int protoent_count=0;
struct protoent * getprotobyname(const char * name)
{
struct protoent *pe;
pe = &protoents[protoent_count];
if (strcmp(name, "udp") == 0) {
pe->p_name = udp;
pe->p_proto = IPPROTO_UDP;
} else if (strcmp (name, "tcp") == 0) {
pe->p_name = tcp;
pe->p_proto = IPPROTO_TCP;
} else {
errno = EPROTONOSUPPORT;
return NULL;
}
pe->p_aliases = aliasPtrs;
protoent_count = (protoent_count +1) % MAX_PROTOENT;
return pe;
}
struct protoent * getprotobynumber(int proto)
{
struct protoent *pe;
pe = &protoents[protoent_count];
if (proto == IPPROTO_UDP) {
pe->p_name = udp;
pe->p_proto = IPPROTO_UDP;
} else if (proto == IPPROTO_TCP) {
pe->p_name = tcp;
pe->p_proto = IPPROTO_TCP;
} else {
errno = EPROTONOSUPPORT;
return NULL;
}
pe->p_aliases = aliasPtrs;
protoent_count = (protoent_count +1) % MAX_PROTOENT;
return pe;
}
|