sockets.pp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1993,97 by the Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. unit Sockets;
  12. Interface
  13. const
  14. { Adress families, Linux specific }
  15. AF_AX25 = 3; { Amateur Radio AX.25 }
  16. AF_IPX = 4; { Novell IPX }
  17. AF_APPLETALK = 5; { Appletalk DDP }
  18. AF_NETROM = 6; { Amateur radio NetROM }
  19. AF_BRIDGE = 7; { Multiprotocol bridge }
  20. AF_AAL5 = 8; { Reserved for Werner's ATM }
  21. AF_X25 = 9; { Reserved for X.25 project }
  22. AF_INET6 = 10; { IP version 6 }
  23. AF_MAX = 12;
  24. SOCK_PACKET = 10;
  25. PF_AX25 = AF_AX25;
  26. PF_IPX = AF_IPX;
  27. PF_APPLETALK = AF_APPLETALK;
  28. PF_NETROM = AF_NETROM;
  29. PF_BRIDGE = AF_BRIDGE;
  30. PF_AAL5 = AF_AAL5;
  31. PF_X25 = AF_X25;
  32. PF_INET6 = AF_INET6;
  33. PF_MAX = AF_MAX;
  34. type
  35. TUnixSockAddr = packed Record
  36. family:word; { was byte, fixed }
  37. path:array[0..108] of char;
  38. end;
  39. {$i socketsh.inc}
  40. { unix socket specific functions }
  41. Procedure Str2UnixSockAddr(const addr:string;var t:TUnixSockAddr;var len:longint);
  42. Function Bind(Sock:longint;const addr:string):boolean;
  43. Function Connect(Sock:longint;const addr:string;var SockIn,SockOut:text):Boolean;
  44. Function Connect(Sock:longint;const addr:string;var SockIn,SockOut:file):Boolean;
  45. Function Accept(Sock:longint;var addr:string;var SockIn,SockOut:text):Boolean;
  46. Function Accept(Sock:longint;var addr:string;var SockIn,SockOut:File):Boolean;
  47. Implementation
  48. Uses Linux;
  49. { Include filerec and textrec structures }
  50. {$i filerec.inc}
  51. {$i textrec.inc}
  52. {******************************************************************************
  53. Kernel Socket Callings
  54. ******************************************************************************}
  55. Const
  56. {
  57. Arguments to the Linux Kernel system call for sockets. All
  58. Socket Connected calls go through the same system call,
  59. with an extra argument to determine what action to take.
  60. }
  61. Socket_Sys_SOCKET = 1;
  62. Socket_Sys_BIND = 2;
  63. Socket_Sys_CONNECT = 3;
  64. Socket_Sys_LISTEN = 4;
  65. Socket_Sys_ACCEPT = 5;
  66. Socket_Sys_GETSOCKNAME = 6;
  67. Socket_Sys_GETPEERNAME = 7;
  68. Socket_Sys_SOCKETPAIR = 8;
  69. Socket_Sys_SEND = 9;
  70. Socket_Sys_RECV = 10;
  71. Socket_Sys_SENDTO = 11;
  72. Socket_Sys_RECVFROM = 12;
  73. Socket_Sys_SHUTDOWN = 13;
  74. Socket_Sys_SETSOCKOPT = 14;
  75. Socket_Sys_GETSOCKOPT = 15;
  76. Socket_Sys_SENDMSG = 16;
  77. Socket_Sys_RECVMSG = 17;
  78. Function SocketCall(SockCallNr,a1,a2,a3,a4,a5,a6:longint):longint;
  79. var
  80. Regs:SysCallRegs;
  81. Args:array[1..6] of longint;
  82. begin
  83. args[1]:=a1;
  84. args[2]:=a2;
  85. args[3]:=a3;
  86. args[4]:=a4;
  87. args[5]:=a5;
  88. args[6]:=a6;
  89. regs.reg2:=SockCallNr;
  90. regs.reg3:=Longint(@args);
  91. SocketCall:=Syscall(syscall_nr_socketcall,regs);
  92. If SocketCall<0 then
  93. SocketError:=Errno
  94. else
  95. SocketError:=0;
  96. end;
  97. Function SocketCall(SockCallNr,a1,a2,a3:longint):longint;
  98. begin
  99. SocketCall:=SocketCall(SockCallNr,a1,a2,a3,0,0,0);
  100. end;
  101. {******************************************************************************
  102. Basic Socket Functions
  103. ******************************************************************************}
  104. Function socket(Domain,SocketType,Protocol:Longint):Longint;
  105. begin
  106. Socket:=SocketCall(Socket_Sys_Socket,Domain,SocketType,ProtoCol);
  107. end;
  108. Function Send(Sock:Longint;Var Addr;AddrLen,Flags:Longint):Longint;
  109. begin
  110. Send:=SocketCall(Socket_Sys_Send,Sock,Longint(@Addr),AddrLen,Flags,0,0);
  111. end;
  112. Function Recv(Sock:Longint;Var Addr;AddrLen,Flags:Longint):Longint;
  113. begin
  114. Recv:=SocketCall(Socket_Sys_Recv,Sock,Longint(@Addr),AddrLen,Flags,0,0);
  115. end;
  116. Function Bind(Sock:Longint;Var Addr;AddrLen:Longint):Boolean;
  117. begin
  118. Bind:=(SocketCall(Socket_Sys_Bind,Sock,Longint(@Addr),AddrLen)=0);
  119. end;
  120. Function Listen(Sock,MaxConnect:Longint):Boolean;
  121. begin
  122. Listen:=(SocketCall(Socket_Sys_Listen,Sock,MaxConnect,0)=0);
  123. end;
  124. Function Accept(Sock:Longint;Var Addr;Var Addrlen:Longint):Longint;
  125. begin
  126. Accept:=SocketCall(Socket_Sys_Accept,Sock,longint(@Addr),longint(@AddrLen));
  127. If Accept<0 Then
  128. Accept:=-1;
  129. end;
  130. Function Connect(Sock:Longint;Var Addr;Addrlen:Longint):Longint;
  131. begin
  132. Connect:=SocketCall(Socket_Sys_Connect,Sock,longint(@Addr),AddrLen);
  133. If Connect<0 Then
  134. Connect:=-1;
  135. end;
  136. Function Shutdown(Sock:Longint;How:Longint):Longint;
  137. begin
  138. ShutDown:=SocketCall(Socket_Sys_ShutDown,Sock,How,0);
  139. end;
  140. Function GetSocketName(Sock:Longint;Var Addr;Var Addrlen:Longint):Longint;
  141. begin
  142. GetSocketName:=SocketCall(Socket_Sys_GetSockName,Sock,longint(@Addr),longint(@AddrLen));
  143. end;
  144. Function GetPeerName(Sock:Longint;Var Addr;Var Addrlen:Longint):Longint;
  145. begin
  146. GetPeerName:=SocketCall(Socket_Sys_GetPeerName,Sock,longint(@Addr),longint(@AddrLen));
  147. end;
  148. Function SetSocketOptions(Sock,Level,OptName:Longint;Var OptVal;optlen:longint):Longint;
  149. begin
  150. SetSocketOptions:=SocketCall(Socket_Sys_SetSockOpt,Sock,Level,OptName,Longint(@OptVal),OptLen,0);
  151. end;
  152. Function GetSocketOptions(Sock,Level,OptName:Longint;Var OptVal;optlen:longint):Longint;
  153. begin
  154. GetSocketOptions:=SocketCall(Socket_Sys_GetSockOpt,Sock,Level,OptName,Longint(@OptVal),OptLen,0);
  155. end;
  156. Function SocketPair(Domain,SocketType,Protocol:Longint;var Pair:TSockArray):Longint;
  157. begin
  158. SocketPair:=SocketCall(Socket_Sys_SocketPair,Domain,SocketType,Protocol,longint(@Pair),0,0);
  159. end;
  160. {******************************************************************************
  161. UnixSock
  162. ******************************************************************************}
  163. Procedure Str2UnixSockAddr(const addr:string;var t:TUnixSockAddr;var len:longint);
  164. begin
  165. Move(Addr[1],t.Path,length(Addr));
  166. t.Family:=AF_UNIX;
  167. t.Path[length(Addr)]:=#0;
  168. Len:=Length(Addr)+3;
  169. end;
  170. Function Bind(Sock:longint;const addr:string):boolean;
  171. var
  172. UnixAddr : TUnixSockAddr;
  173. AddrLen : longint;
  174. begin
  175. Str2UnixSockAddr(addr,UnixAddr,AddrLen);
  176. Bind(Sock,UnixAddr,AddrLen);
  177. Bind:=(SocketError=0);
  178. end;
  179. Function DoAccept(Sock:longint;var addr:string):longint;
  180. var
  181. UnixAddr : TUnixSockAddr;
  182. AddrLen : longint;
  183. begin
  184. AddrLen:=length(addr)+3;
  185. DoAccept:=Accept(Sock,UnixAddr,AddrLen);
  186. Move(UnixAddr.Path,Addr[1],AddrLen);
  187. SetLength(Addr,AddrLen);
  188. end;
  189. Function DoConnect(Sock:longint;const addr:string):Longint;
  190. var
  191. UnixAddr : TUnixSockAddr;
  192. AddrLen : longint;
  193. begin
  194. Str2UnixSockAddr(addr,UnixAddr,AddrLen);
  195. DoConnect:=Connect(Sock,UnixAddr,AddrLen);
  196. end;
  197. Function Accept(Sock:longint;var addr:string;var SockIn,SockOut:text):Boolean;
  198. var
  199. s : longint;
  200. begin
  201. S:=DoAccept(Sock,addr);
  202. if S>0 then
  203. begin
  204. Sock2Text(S,SockIn,SockOut);
  205. Accept:=true;
  206. end
  207. else
  208. Accept:=false;
  209. end;
  210. Function Accept(Sock:longint;var addr:string;var SockIn,SockOut:File):Boolean;
  211. var
  212. s : longint;
  213. begin
  214. S:=DoAccept(Sock,addr);
  215. if S>0 then
  216. begin
  217. Sock2File(S,SockIn,SockOut);
  218. Accept:=true;
  219. end
  220. else
  221. Accept:=false;
  222. end;
  223. Function Connect(Sock:longint;const addr:string;var SockIn,SockOut:text):Boolean;
  224. Var FD : Longint;
  225. begin
  226. FD:=DoConnect(Sock,addr);
  227. If Not(FD=-1) then
  228. begin
  229. Sock2Text(Sock,SockIn,SockOut);
  230. Connect:=true;
  231. end
  232. else
  233. Connect:=false;
  234. end;
  235. Function Connect(Sock:longint;const addr:string;var SockIn,SockOut:file):Boolean;
  236. Var FD : Longint;
  237. begin
  238. FD:=DoConnect(Sock,addr);
  239. if Not(FD=-1) then
  240. begin
  241. Sock2File(Sock,SockIn,SockOut);
  242. Connect:=true;
  243. end
  244. else
  245. Connect:=false;
  246. end;
  247. {$i sockets.inc}
  248. end.
  249. {
  250. $Log$
  251. Revision 1.8 1999-06-27 16:04:25 michael
  252. + Updated to Florians system independent sockets unit
  253. Revision 1.7 1999/06/08 18:35:24 michael
  254. + Beter return values for connect and accept
  255. Revision 1.6 1999/06/08 18:19:24 michael
  256. + Fixes for connect calls
  257. Revision 1.5 1999/06/08 16:08:33 michael
  258. + completed (hopefully) Fix by stian ([email protected])
  259. Revision 1.4 1999/06/08 16:05:08 michael
  260. + Fix by stian ([email protected])
  261. Revision 1.3 1998/11/16 10:21:30 peter
  262. * fixes for H+
  263. Revision 1.2 1998/07/16 10:36:45 michael
  264. + added connect call for inet sockets
  265. Revision 1.1.1.1 1998/03/25 11:18:43 root
  266. * Restored version
  267. Revision 1.1 1998/02/13 08:35:05 michael
  268. + Initial implementation
  269. }