|
@@ -14,19 +14,8 @@
|
|
unit Sockets;
|
|
unit Sockets;
|
|
Interface
|
|
Interface
|
|
|
|
|
|
-Const
|
|
|
|
- { Socket Types }
|
|
|
|
- SOCK_STREAM = 1; { stream (connection) socket }
|
|
|
|
- SOCK_DGRAM = 2; { datagram (conn.less) socket }
|
|
|
|
- SOCK_RAW = 3; { raw socket }
|
|
|
|
- SOCK_RDM = 4; { reliably-delivered message }
|
|
|
|
- SOCK_SEQPACKET = 5; { sequential packet socket }
|
|
|
|
- SOCK_PACKET =10;
|
|
|
|
-
|
|
|
|
- { Adress families }
|
|
|
|
- AF_UNSPEC = 0;
|
|
|
|
- AF_UNIX = 1; { Unix domain sockets }
|
|
|
|
- AF_INET = 2; { Internet IP Protocol }
|
|
|
|
|
|
+const
|
|
|
|
+ { Adress families, Linux specific }
|
|
AF_AX25 = 3; { Amateur Radio AX.25 }
|
|
AF_AX25 = 3; { Amateur Radio AX.25 }
|
|
AF_IPX = 4; { Novell IPX }
|
|
AF_IPX = 4; { Novell IPX }
|
|
AF_APPLETALK = 5; { Appletalk DDP }
|
|
AF_APPLETALK = 5; { Appletalk DDP }
|
|
@@ -37,11 +26,8 @@ Const
|
|
AF_INET6 = 10; { IP version 6 }
|
|
AF_INET6 = 10; { IP version 6 }
|
|
AF_MAX = 12;
|
|
AF_MAX = 12;
|
|
|
|
|
|
- { Protocol Families }
|
|
|
|
|
|
+ SOCK_PACKET = 10;
|
|
|
|
|
|
- PF_UNSPEC = AF_UNSPEC;
|
|
|
|
- PF_UNIX = AF_UNIX;
|
|
|
|
- PF_INET = AF_INET;
|
|
|
|
PF_AX25 = AF_AX25;
|
|
PF_AX25 = AF_AX25;
|
|
PF_IPX = AF_IPX;
|
|
PF_IPX = AF_IPX;
|
|
PF_APPLETALK = AF_APPLETALK;
|
|
PF_APPLETALK = AF_APPLETALK;
|
|
@@ -53,67 +39,21 @@ Const
|
|
|
|
|
|
PF_MAX = AF_MAX;
|
|
PF_MAX = AF_MAX;
|
|
|
|
|
|
-const
|
|
|
|
- { Two constants to determine whether part of soket is for in or output }
|
|
|
|
- S_IN = 0;
|
|
|
|
- S_OUT = 1;
|
|
|
|
-
|
|
|
|
-Type
|
|
|
|
- TSockAddr = packed Record
|
|
|
|
- family:word; { was byte, fixed }
|
|
|
|
- data :array [0..13] of char;
|
|
|
|
- end;
|
|
|
|
-
|
|
|
|
|
|
+type
|
|
TUnixSockAddr = packed Record
|
|
TUnixSockAddr = packed Record
|
|
family:word; { was byte, fixed }
|
|
family:word; { was byte, fixed }
|
|
path:array[0..108] of char;
|
|
path:array[0..108] of char;
|
|
end;
|
|
end;
|
|
|
|
|
|
- TInetSockAddr = packed Record
|
|
|
|
- family:Word;
|
|
|
|
- port :Word;
|
|
|
|
- addr :Cardinal;
|
|
|
|
- pad :array [1..8] of byte; { to get to the size of sockaddr... }
|
|
|
|
- end;
|
|
|
|
-
|
|
|
|
- TSockArray = Array[1..2] of Longint;
|
|
|
|
-
|
|
|
|
-Var
|
|
|
|
- SocketError:Longint;
|
|
|
|
|
|
+{$i socketsh.inc}
|
|
|
|
|
|
-{Basic Socket Functions}
|
|
|
|
-Function Socket(Domain,SocketType,Protocol:Longint):Longint;
|
|
|
|
-Function Send(Sock:Longint;Var Addr;AddrLen,Flags:Longint):Longint;
|
|
|
|
-Function Recv(Sock:Longint;Var Addr;AddrLen,Flags:Longint):Longint;
|
|
|
|
-Function Bind(Sock:Longint;Var Addr;AddrLen:Longint):Boolean;
|
|
|
|
-Function Listen (Sock,MaxConnect:Longint):Boolean;
|
|
|
|
-Function Accept(Sock:Longint;Var Addr;Var Addrlen:Longint):Longint;
|
|
|
|
-Function Connect(Sock:Longint;Var Addr;Addrlen:Longint):Longint;
|
|
|
|
-Function Shutdown(Sock:Longint;How:Longint):Longint;
|
|
|
|
-Function GetSocketName(Sock:Longint;Var Addr;Var Addrlen:Longint):Longint;
|
|
|
|
-Function GetPeerName(Sock:Longint;Var Addr;Var Addrlen:Longint):Longint;
|
|
|
|
-Function SetSocketOptions(Sock,Level,OptName:Longint;Var OptVal;optlen:longint):Longint;
|
|
|
|
-Function GetSocketOptions(Sock,Level,OptName:Longint;Var OptVal;optlen:longint):Longint;
|
|
|
|
-Function SocketPair(Domain,SocketType,Protocol:Longint;var Pair:TSockArray):Longint;
|
|
|
|
-
|
|
|
|
-{Text Support}
|
|
|
|
-Procedure Sock2Text(Sock:Longint;Var SockIn,SockOut:Text);
|
|
|
|
-
|
|
|
|
-{Untyped File Support}
|
|
|
|
-Procedure Sock2File(Sock:Longint;Var SockIn,SockOut:File);
|
|
|
|
-
|
|
|
|
-{Better Pascal Calling, Overloaded Functions!}
|
|
|
|
|
|
+{ unix socket specific functions }
|
|
Procedure Str2UnixSockAddr(const addr:string;var t:TUnixSockAddr;var len:longint);
|
|
Procedure Str2UnixSockAddr(const addr:string;var t:TUnixSockAddr;var len:longint);
|
|
Function Bind(Sock:longint;const addr:string):boolean;
|
|
Function Bind(Sock:longint;const addr:string):boolean;
|
|
-Function Accept(Sock:longint;var addr:string;var SockIn,SockOut:text):Boolean;
|
|
|
|
-Function Accept(Sock:longint;var addr:string;var SockIn,SockOut:File):Boolean;
|
|
|
|
-Function Accept(Sock:longint;var addr:TInetSockAddr;var SockIn,SockOut:File):Boolean;
|
|
|
|
-Function Accept(Sock:longint;var addr:TInetSockAddr;var SockIn,SockOut:text):Boolean;
|
|
|
|
Function Connect(Sock:longint;const addr:string;var SockIn,SockOut:text):Boolean;
|
|
Function Connect(Sock:longint;const addr:string;var SockIn,SockOut:text):Boolean;
|
|
Function Connect(Sock:longint;const addr:string;var SockIn,SockOut:file):Boolean;
|
|
Function Connect(Sock:longint;const addr:string;var SockIn,SockOut:file):Boolean;
|
|
-Function Connect(Sock:longint;const addr:TInetSockAddr;var SockIn,SockOut:text):Boolean;
|
|
|
|
-Function Connect(Sock:longint;const addr:TInetSockAddr;var SockIn,SockOut:file):Boolean;
|
|
|
|
-
|
|
|
|
|
|
+Function Accept(Sock:longint;var addr:string;var SockIn,SockOut:text):Boolean;
|
|
|
|
+Function Accept(Sock:longint;var addr:string;var SockIn,SockOut:File):Boolean;
|
|
|
|
|
|
Implementation
|
|
Implementation
|
|
Uses Linux;
|
|
Uses Linux;
|
|
@@ -221,7 +161,7 @@ end;
|
|
Function Accept(Sock:Longint;Var Addr;Var Addrlen:Longint):Longint;
|
|
Function Accept(Sock:Longint;Var Addr;Var Addrlen:Longint):Longint;
|
|
begin
|
|
begin
|
|
Accept:=SocketCall(Socket_Sys_Accept,Sock,longint(@Addr),longint(@AddrLen));
|
|
Accept:=SocketCall(Socket_Sys_Accept,Sock,longint(@Addr),longint(@AddrLen));
|
|
- If Accept<0 Then
|
|
|
|
|
|
+ If Accept<0 Then
|
|
Accept:=-1;
|
|
Accept:=-1;
|
|
end;
|
|
end;
|
|
|
|
|
|
@@ -277,97 +217,6 @@ begin
|
|
SocketPair:=SocketCall(Socket_Sys_SocketPair,Domain,SocketType,Protocol,longint(@Pair),0,0);
|
|
SocketPair:=SocketCall(Socket_Sys_SocketPair,Domain,SocketType,Protocol,longint(@Pair),0,0);
|
|
end;
|
|
end;
|
|
|
|
|
|
-
|
|
|
|
-{******************************************************************************
|
|
|
|
- Text File Writeln/ReadLn Support
|
|
|
|
-******************************************************************************}
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-Procedure OpenSock(var F:Text);
|
|
|
|
-begin
|
|
|
|
- if textrec(f).handle=UnusedHandle then
|
|
|
|
- textrec(f).mode:=fmclosed
|
|
|
|
- else
|
|
|
|
- case textrec(f).userdata[1] of
|
|
|
|
- S_OUT : textrec(f).mode:=fmoutput;
|
|
|
|
- S_IN : textrec(f).mode:=fminput;
|
|
|
|
- else
|
|
|
|
- textrec(f).mode:=fmclosed;
|
|
|
|
- end;
|
|
|
|
-end;
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-Procedure IOSock(var F:text);
|
|
|
|
-begin
|
|
|
|
- case textrec(f).mode of
|
|
|
|
- fmoutput : fdWrite(textrec(f).handle,textrec(f).bufptr^,textrec(f).bufpos);
|
|
|
|
- fminput : textrec(f).BufEnd:=fdRead(textrec(f).handle,textrec(f).bufptr^,textrec(f).bufsize);
|
|
|
|
- end;
|
|
|
|
- textrec(f).bufpos:=0;
|
|
|
|
-end;
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-Procedure FlushSock(var F:Text);
|
|
|
|
-begin
|
|
|
|
- if (textrec(f).mode=fmoutput) and (textrec(f).bufpos<>0) then
|
|
|
|
- IOSock(f);
|
|
|
|
- textrec(f).bufpos:=0;
|
|
|
|
-end;
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-Procedure CloseSock(var F:text);
|
|
|
|
-begin
|
|
|
|
- Close(f);
|
|
|
|
-end;
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-Procedure Sock2Text(Sock:Longint;Var SockIn,SockOut:Text);
|
|
|
|
-{
|
|
|
|
- Set up two Pascal Text file descriptors for reading and writing)
|
|
|
|
-}
|
|
|
|
-begin
|
|
|
|
-{ First the reading part.}
|
|
|
|
- Assign(SockIn,'.');
|
|
|
|
- Textrec(SockIn).Handle:=Sock;
|
|
|
|
- Textrec(Sockin).userdata[1]:=S_IN;
|
|
|
|
- TextRec(SockIn).OpenFunc:=@OpenSock;
|
|
|
|
- TextRec(SockIn).InOutFunc:=@IOSock;
|
|
|
|
- TextRec(SockIn).FlushFunc:=@FlushSock;
|
|
|
|
- TextRec(SockIn).CloseFunc:=@CloseSock;
|
|
|
|
-{ Now the writing part. }
|
|
|
|
- Assign(SockOut,'.');
|
|
|
|
- Textrec(SockOut).Handle:=Sock;
|
|
|
|
- Textrec(SockOut).userdata[1]:=S_OUT;
|
|
|
|
- TextRec(SockOut).OpenFunc:=@OpenSock;
|
|
|
|
- TextRec(SockOut).InOutFunc:=@IOSock;
|
|
|
|
- TextRec(SockOut).FlushFunc:=@FlushSock;
|
|
|
|
- TextRec(SockOut).CloseFunc:=@CloseSock;
|
|
|
|
-end;
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-{******************************************************************************
|
|
|
|
- Untyped File
|
|
|
|
-******************************************************************************}
|
|
|
|
-
|
|
|
|
-Procedure Sock2File(Sock:Longint;Var SockIn,SockOut:File);
|
|
|
|
-begin
|
|
|
|
-{Input}
|
|
|
|
- Assign(SockIn,'.');
|
|
|
|
- FileRec(SockIn).Handle:=Sock;
|
|
|
|
- FileRec(SockIn).RecSize:=1;
|
|
|
|
- FileRec(Sockin).userdata[1]:=S_IN;
|
|
|
|
-{Output}
|
|
|
|
- Assign(SockOut,'.');
|
|
|
|
- FileRec(SockOut).Handle:=Sock;
|
|
|
|
- FileRec(SockOut).RecSize:=1;
|
|
|
|
- FileRec(SockOut).userdata[1]:=S_OUT;
|
|
|
|
-end;
|
|
|
|
-
|
|
|
|
-
|
|
|
|
{******************************************************************************
|
|
{******************************************************************************
|
|
UnixSock
|
|
UnixSock
|
|
******************************************************************************}
|
|
******************************************************************************}
|
|
@@ -479,103 +328,16 @@ begin
|
|
Connect:=false;
|
|
Connect:=false;
|
|
end;
|
|
end;
|
|
|
|
|
|
-{******************************************************************************
|
|
|
|
- InetSock
|
|
|
|
-******************************************************************************}
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-Function DoAccept(Sock:longint;Var addr:TInetSockAddr):longint;
|
|
|
|
-
|
|
|
|
-Var AddrLen : Longint;
|
|
|
|
-
|
|
|
|
-begin
|
|
|
|
- AddrLEn:=SizeOf(Addr);
|
|
|
|
- DoAccept:=Accept(Sock,Addr,AddrLen);
|
|
|
|
-end;
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-Function DoConnect(Sock:longint;const addr: TInetSockAddr): Longint;
|
|
|
|
-
|
|
|
|
-begin
|
|
|
|
- DoConnect:=Connect(Sock,Addr,SizeOF(TInetSockAddr));
|
|
|
|
-end;
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-Function Connect(Sock:longint;const addr: TInetSockAddr;var SockIn,SockOut:text):Boolean;
|
|
|
|
-
|
|
|
|
-Var FD : Longint;
|
|
|
|
-
|
|
|
|
-begin
|
|
|
|
- FD:=DoConnect(Sock,addr);
|
|
|
|
- If Not(FD=-1) then
|
|
|
|
- begin
|
|
|
|
- Sock2Text(FD,SockIn,SockOut);
|
|
|
|
- Connect:=true;
|
|
|
|
- end
|
|
|
|
- else
|
|
|
|
- Connect:=false;
|
|
|
|
-end;
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-Function Connect(Sock:longint;const addr:TInetSockAddr;var SockIn,SockOut:file):Boolean;
|
|
|
|
-
|
|
|
|
-Var FD : Longint;
|
|
|
|
-
|
|
|
|
-begin
|
|
|
|
- FD:=DoConnect(Sock,addr);
|
|
|
|
- If Not (FD=-1) then
|
|
|
|
- begin
|
|
|
|
- Sock2File(FD,SockIn,SockOut);
|
|
|
|
- Connect:=true;
|
|
|
|
- end
|
|
|
|
- else
|
|
|
|
- Connect:=false;
|
|
|
|
-end;
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-Function Accept(Sock:longint;var addr:TInetSockAddr;var SockIn,SockOut:text):Boolean;
|
|
|
|
-var
|
|
|
|
- s : longint;
|
|
|
|
-begin
|
|
|
|
- S:=DoAccept(Sock,addr);
|
|
|
|
- if S>0 then
|
|
|
|
- begin
|
|
|
|
- Sock2Text(S,SockIn,SockOut);
|
|
|
|
- Accept:=true;
|
|
|
|
- end
|
|
|
|
- else
|
|
|
|
- Accept:=false;
|
|
|
|
-end;
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-Function Accept(Sock:longint;var addr:TInetSockAddr;var SockIn,SockOut:File):Boolean;
|
|
|
|
-var
|
|
|
|
- s : longint;
|
|
|
|
-begin
|
|
|
|
- S:=DoAccept(Sock,addr);
|
|
|
|
- if S>0 then
|
|
|
|
- begin
|
|
|
|
- Sock2File(S,SockIn,SockOut);
|
|
|
|
- Accept:=true;
|
|
|
|
- end
|
|
|
|
- else
|
|
|
|
- Accept:=false;
|
|
|
|
-end;
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
|
|
+{$i sockets.inc}
|
|
|
|
|
|
end.
|
|
end.
|
|
|
|
|
|
{
|
|
{
|
|
$Log$
|
|
$Log$
|
|
- Revision 1.7 1999-06-08 18:35:24 michael
|
|
|
|
|
|
+ Revision 1.8 1999-06-27 16:04:25 michael
|
|
|
|
+ + Updated to Florians system independent sockets unit
|
|
|
|
+
|
|
|
|
+ Revision 1.7 1999/06/08 18:35:24 michael
|
|
+ Beter return values for connect and accept
|
|
+ Beter return values for connect and accept
|
|
|
|
|
|
Revision 1.6 1999/06/08 18:19:24 michael
|
|
Revision 1.6 1999/06/08 18:19:24 michael
|
|
@@ -599,4 +361,4 @@ end.
|
|
Revision 1.1 1998/02/13 08:35:05 michael
|
|
Revision 1.1 1998/02/13 08:35:05 michael
|
|
+ Initial implementation
|
|
+ Initial implementation
|
|
|
|
|
|
-}
|
|
|
|
|
|
+}
|