File: inet.pp

package info (click to toggle)
imapcopy 1.04-2.1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 348 kB
  • ctags: 277
  • sloc: pascal: 2,939; xml: 103; sh: 19; makefile: 17
file content (538 lines) | stat: -rw-r--r-- 12,772 bytes parent folder | download | duplicates (4)
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
Unit inet;

{ --------------------------------------------------------------------
  Unit for internet domain calls.
  Copyright (C) 1997  Michael Van Canneyt

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 1, or (at your option)
  any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

  ChangeLog
  ---------

  Current version is 0.6

  Version          Date             Remarks
  -------          ----             ----
  0.1              07/16/97         Unit started. Michael.
  0.2              07/06/98         Updated for version 0.99.5
  0.4              08/01/98         Objects for name lookup implemented
  0.5              09/10/98         Updated calls for 0.99.8.
  0.6              05/04/99         Added explicit asmmode.

  ------------------------------------------------------------------- }


interface

{$LINKLIB c}

Const
  { Net type }
  AF_INET = 2;

  { Error constants. Returned by LastError method of THost, TNet}

  NETDB_INTERNAL= -1;       { see errno }
  NETDB_SUCCESS = 0;        { no problem }
  HOST_NOT_FOUND= 1;        { Authoritative Answer Host not found }
  TRY_AGAIN     = 2;        { Non-Authoritive Host not found, or SERVERFAIL }
  NO_RECOVERY   = 3;        { Non recoverable errors, FORMERR, REFUSED, NOTIMP }
  NO_DATA       = 4;        { Valid name, no data record of requested type }
  NO_ADDRESS    = NO_DATA;  { no address, look for MX record }




Type
  THostAddr = array[1..4] of byte;
  PHostAddr = ^THostAddr;

Const
  NoAddress : THostAddr = (0,0,0,0);

Type

  { THostEnt Object }
  THostEnt = record
    Name     : pchar;   { Official name }
    Aliases  : ppchar;  { Null-terminated list of aliases}
    Addrtype : longint; { Host address type }
    Addrlen  : longint; { Length of address }
    Addrlist : ppchar;  { null-terminated list of adresses }
  end;
  PHostEnt = ^THostEnt;

  { TNetEnt object }
  TNetEnt = record
    Name     : pchar;   { Official name }
    Aliases  : ppchar;  { Nill-terminated alias list }
    AddrType : longint; { Net address type }
    net      : Longint; { Network number }
  end;
  PNetEnt = ^TNetEnt;

  TServEnt = record
    name    : pchar;    { Service name }
    aliases : ppchar;   { Null-terminated alias list }
    port    : longint;  { Port number }
    proto   : pchar;    { Protocol to use }
  end;
  PServEnt = ^TServEnt;

  { Pascal Wrapper objects }

  TSelectType = (stFirst,stNext,stPrevious);

  THost = Object
    FHostEntry : PHostEnt;
    FAlias,FAddr,FError : Longint;
    Constructor NameLookup (HostName : String);
    Constructor AddressLookup (Const Address : THostAddr);
    Destructor Done;
    Function Name : String;
    Function GetAddress (Select : TSelectType) : String;
    Function GetAlias (Select : TSelectType) : String;
    Function IPAddress : THostAddr;
    Function IPString : String;
    Function LastError : Longint;
  end;

  TNet = Object
    FNetEntry : PNetEnt;
    FAlias,FError : Longint;
    Constructor NameLookup (NetName : String);
    Constructor AddressLookup (Const Address : Longint);
    Destructor Done;
    Function Name : String;
    Function GetAlias (Select : TSelectType) : String;
    Function IPAddress : Longint;
    Function IPString : String;
    Function LastError : Longint;
  end;

  TService = Object
    FServiceEntry : PServEnt;
    FAlias,FError : Longint;
    Constructor NameLookup (ServiceName,Proto : String);
    Constructor PortLookup (APort : Longint; Proto: string);
    Destructor Done;
    Function Name : String;
    Function Protocol : String;
    Function GetAlias (Select : TSelectType) : String;
    Function Port : Longint;
    Function LastError : Longint;
  end;



{ Pascal style calls }

function HostAddrToStr (Entry : THostAddr) : String;
function StrToHostAddr (IP : String) : THostAddr;
function NetAddrToStr (Entry : Longint) : String;
function StrToNetAddr (IP : String) : Longint;
Function HostToNet (Host : Longint) : Longint;
Function NetToHost (Net : Longint) : Longint;
Function ShortHostToNet (Host : integer) : integer;
Function ShortNetToHost (Net : integer) : integer;

{ C style calls, linked in from Libc }

function gethostent : PHostEnt; cdecl; external;
function gethostbyname ( Name : Pchar) : PHostEnt; cdecl; external;
function gethostbyaddr ( Addr : PHostAddr; Len : Longint; HType : Longint) : PHostent ; cdecl; external;
procedure sethostent (stayopen : longint); cdecl; external;
procedure endhostent; cdecl; external;

function getnetent : PNetEnt; cdecl; external;
function getnetbyname ( Name : pchar) : PNetEnt; cdecl; external;
function getnetbyaddr ( Net : Longint; nettype : Longint) : PNetEnt; cdecl; external;
procedure setnetent ( Stayopen : Longint);  cdecl; external;
procedure endnetent; cdecl; external;

function getservent : PServEnt; cdecl; external;
function getservbyname (name : pchar  ; protocol : pchar) : PServEnt; cdecl; external;
function getservbyport (port : longint; protocol : pchar) : PServEnt; cdecl; external;
procedure setservent (StayOpen : longint); cdecl; external;
procedure endservent; cdecl; external;

//var
//  GetDNSError : longint;external name 'h_errno';
// This does not work with newer glibc's (i.e. RedHat 9)

// dummy for now
const GetDNSError = 1;


implementation

Uses strings;


function HostAddrToStr (Entry : THostAddr) : String;

Var Dummy : String[4];
    I : Longint;

begin
  HostAddrToStr:='';
  For I:=1 to 4 do
   begin
   Str(Entry[I],Dummy);
   HostAddrToStr:=HostAddrToStr+Dummy;
   If I<4 Then HostAddrToStr:=HostAddrToStr+'.';
   end;
end;

function StrToHostAddr(IP : String) : THostAddr ;

Var Dummy : String[4];
    I : Longint;
    J : Integer;
    Temp : THostAddr;

begin
  StrToHostAddr:=NoAddress;
  For I:=1 to 4 do
   begin
   If I<4 Then
     begin
     J:=Pos('.',IP);
     If J=0 then exit;
     Dummy:=Copy(IP,1,J-1);
     Delete (IP,1,J);
     end
   else
     Dummy:=IP;
   Val (Dummy,Temp[I],J);
   If J<>0 then Exit;
   end;
 StrToHostAddr:=Temp;
end;

function NetAddrToStr (Entry : longint) : String;

Var Dummy : String[4];
    I : Longint;

begin
  NetAddrToStr:='';
  For I:=4 downto 1 do
   begin
   Str(THostAddr(Entry)[I],Dummy);
   NetAddrToStr:=NetAddrToStr+Dummy;
   If I>1 Then NetAddrToStr:=NetAddrToStr+'.';
   end;
end;

function StrToNetAddr(IP : String) : Longint;

begin
  StrToNetAddr:=Longint(StrToHostAddr(IP));
end;


Constructor THost.NameLookup (HostName : String);

begin
  HostName:=HostName+#0;
  FHostEntry:=GetHostByName(pchar(@HostName[1]));
  If FHostEntry=Nil then
    FError:=GetDNSError
  else
    begin
    FAlias:=0;
    FAddr:=0;
    Ferror:=0;
    end;
end;

Constructor THost.AddressLookup (Const Address: THostAddr);

begin
  FHostEntry:=GetHostByAddr(PHostAddr(@Address),SizeOf(Address),AF_INET);
  If FHostEntry=Nil then
    FError:=GetDNSError
  else
    begin
    FAlias:=0;
    FAddr:=0;
    FError:=0;
    end;
end;


Function THost.Name : String;

begin
  Name:='';
  If (FHostEntry=Nil) or (FError<>0) then exit;
  Name:=StrPas(FHostEntry^.Name);
end;

Function THost.GetAlias (Select : TSelectType) : String;

begin
  GetAlias:='';
  If (FHostEntry=Nil) or (FError<>0) then exit;
  Case Select of
    stFirst     : FAlias:=0;
    stnext      : If FHostEntry^.Aliases[FAlias]<>Nil then
                    Falias:=Falias+1;
    stprevious  : If FAlias=0 Then Exit else FAlias:=FAlias-1;
  end;
  If FHostEntry^.Aliases[FAlias]<>Nil then
    GetAlias:=StrPas(FHostEntry^.Aliases[FAlias]);
end;

Function THost.GetAddress (Select : TSelectType) : String;

begin
  GetAddress:='';
  If (FHostEntry=Nil) or (FError<>0) then exit;
  Case Select of
    stFirst     : FAddr:=0;
    stnext      : If FHostEntry^.AddrList[FAddr]<>Nil then
                    FAddr:=FAddr+1;
    stprevious  : If FAddr=0 Then Exit else FAddr:=FAddr-1;
  end;
  If FHostEntry^.AddrList[FAddr]<>Nil then
    GetAddress:=HostAddrToStr(PHostAddr(FHostEntry^.AddrList[FAddr])^);
end;

Function THost.IPstring : String;

begin
  IPString:='';
  If (FHostEntry=Nil) or (FError<>0) then exit;
  If FHostEntry^.AddrList[0]<>Nil then
    IPString:=HostAddrToStr(PHostAddr(FHostEntry^.AddrList[0])^);
end;

Function THost.IPaddress : THostAddr;

begin
  IPAddress:=NoAddress;
  If (FHostEntry=Nil) or (FError<>0) then exit;
  IPAddress:=PHostAddr(FHostEntry^.AddrList[0])^;
end;

Destructor THost.Done;

begin
end;

Function THost.LastError : Longint;

begin
  LastError:=FError;
end;

Constructor TNet.NameLookup (NetName : String);

begin
  NetName:=NetName+#0;
  FNetEntry:=GetNetByName(pchar(@NetName[1]));
  If FNetEntry=Nil then
    FError:=GetDNSError
  else
    begin
    FAlias:=0;
    Ferror:=0;
    end;
end;

Constructor TNet.AddressLookup (Const Address: Longint);

begin
  FNetEntry:=GetNetByAddr(Address,AF_INET);
  If FNetEntry=Nil then
    FError:=GetDNSError
  else
    begin
    FAlias:=0;
    FError:=0;
    end;
end;


Function TNet.Name : String;

begin
  Name:='';
  If (FNetEntry=Nil) or (FError<>0) then exit;
  Name:=StrPas(FNetEntry^.Name);
end;

Function TNet.GetAlias (Select : TSelectType) : String;

begin
  GetAlias:='';
  If (FNetEntry=Nil) or (FError<>0) then exit;
  Case Select of
    stFirst     : FAlias:=0;
    stnext      : If FNetEntry^.Aliases[FAlias]<>Nil then
                    Falias:=Falias+1;
    stprevious  : If FAlias=0 Then Exit else FAlias:=FAlias-1;
  end;
  If FNetEntry^.Aliases[FAlias]<>Nil then
    GetAlias:=StrPas(FNetEntry^.Aliases[FAlias]);
end;

Function TNet.IPstring : String;

begin
  IPString:='';
  If (FNetEntry=Nil) or (FError<>0) then exit;
  IPString:=NetAddrToStr(FNetEntry^.Net);
end;

Function TNet.IPaddress : Longint;

begin
  IPAddress:=0;
  If (FNetEntry=Nil) or (FError<>0) then exit;
  IPAddress:=FNetEntry^.Net;
end;

Destructor TNet.Done;

begin
end;

Function TNet.LastError : Longint;

begin
  LastError:=FError;
end;

Constructor TService.NameLookup (ServiceName,Proto : String);

begin
  ServiceName:=ServiceName+#0;
  Proto:=Proto+#0;
  FServiceEntry:=GetServByName(pchar(@ServiceName[1]),pchar(@Proto[1]));
  If FServiceEntry=Nil then
    FError:=GetDNSError
  else
    begin
    FAlias:=0;
    Ferror:=0;
    end;
end;

Constructor TService.PortLookup (APort: Longint; Proto : String);

begin
  Proto:=proto+#0;
  FServiceEntry:=GetServByPort(APort,pchar(@proto[1]));
  If FServiceEntry=Nil then
    FError:=GetDNSError
  else
    begin
    FAlias:=0;
    FError:=0;
    end;
end;


Function TService.Name : String;

begin
  Name:='';
  If (FServiceEntry=Nil) or (FError<>0) then exit;
  Name:=StrPas(FServiceEntry^.Name);
end;

Function TService.GetAlias (Select : TSelectType) : String;

begin
  GetAlias:='';
  If (FServiceEntry=Nil) or (FError<>0) then exit;
  Case Select of
    stFirst     : FAlias:=0;
    stnext      : If FServiceEntry^.Aliases[FAlias]<>Nil then
                    Falias:=Falias+1;
    stprevious  : If FAlias=0 Then Exit else FAlias:=FAlias-1;
  end;
  If FServiceEntry^.Aliases[FAlias]<>Nil then
    GetAlias:=StrPas(FServiceEntry^.Aliases[FAlias]);
end;

Function TService.Protocol : String;

begin
  Protocol:='';
  If (FServiceEntry=Nil) or (FError<>0) then exit;
  Protocol:=Strpas(FServiceEntry^.proto);
end;

Function TService.Port : Longint;

begin
  Port:=0;
  If (FServiceEntry=Nil) or (FError<>0) then exit;
  Port:=FServiceEntry^.Port;
end;

Destructor TService.Done;

begin
end;

Function TService.LastError : Longint;

begin
  LastError:=FError;
end;

Function HostToNet (Host : Longint) : Longint;

begin
  HostToNet:=THostAddr(host)[1];
  HostToNEt:=HostTONet or ( (THostAddr(host)[2]) shl 8);
  HostToNEt:=HostToNet or ( (THostAddr(host)[3]) shl 16);
  HostToNEt:=HostToNet or ( (THostAddr(host)[4]) shl 24);
end;

Function NetToHost (Net : Longint) : Longint;

begin
  NetToHost:=THostAddr(Net)[1];
  NetToHost:=NetToHost or ( (THostAddr(Net)[2]) shl 8);
  NetToHost:=NetToHost or ( (THostAddr(Net)[3]) shl 16);
  NetToHost:=NetToHost or ( (THostAddr(Net)[4]) shl 24);
end;

Function ShortHostToNet (Host : integer) : integer;

begin
  ShortHostToNet:=lo(host)*256+Hi(Host);
end;

Function ShortNetToHost (Net : integer) : integer;

begin
  ShortNetToHost:=lo(Net)*256+Hi(Net);
end;

end.


   $Log: inet.pp,v $
   Revision 1.2  2000/07/13 11:33:26  michael
   + removed logs
 
}