unixsock.inc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. {
  2. This file is part of the Free Pascal run time library.
  3. (c) 2004 by Marco van de Voort
  4. member of the Free Pascal development team.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. socket call implementations for Linux
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY;without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. }
  12. {$ifndef cpux86_64}
  13. {$define NEED_SOCKETCALL}
  14. {$endif}
  15. {******************************************************************************
  16. Basic Socket Functions
  17. ******************************************************************************}
  18. {$ifdef NEED_SOCKETCALL}
  19. Const
  20. {
  21. Arguments to the Linux Kernel system call for sockets. All
  22. Socket Connected calls go through the same system call,
  23. with an extra argument to determine what action to take.
  24. }
  25. Socket_Sys_SOCKET = 1;
  26. Socket_Sys_BIND = 2;
  27. Socket_Sys_CONNECT = 3;
  28. Socket_Sys_LISTEN = 4;
  29. Socket_Sys_ACCEPT = 5;
  30. Socket_Sys_GETSOCKNAME = 6;
  31. Socket_Sys_GETPEERNAME = 7;
  32. Socket_Sys_SOCKETPAIR = 8;
  33. Socket_Sys_SEND = 9;
  34. Socket_Sys_RECV = 10;
  35. Socket_Sys_SENDTO = 11;
  36. Socket_Sys_RECVFROM = 12;
  37. Socket_Sys_SHUTDOWN = 13;
  38. Socket_Sys_SETSOCKOPT = 14;
  39. Socket_Sys_GETSOCKOPT = 15;
  40. Socket_Sys_SENDMSG = 16;
  41. Socket_Sys_RECVMSG = 17;
  42. Function SocketCall(SockCallNr,a1,a2,a3,a4,a5,a6:TSysParam):longint;
  43. var
  44. Args:array[1..6] of TSysParam;
  45. begin
  46. args[1]:=a1;
  47. args[2]:=a2;
  48. args[3]:=a3;
  49. args[4]:=a4;
  50. args[5]:=a5;
  51. args[6]:=a6;
  52. SocketCall:=do_Syscall(syscall_nr_socketcall,sockcallnr,TSysParam(@args));
  53. end;
  54. function SocketCall(SockCallNr,a1,a2,a3:TSysParam):longint;
  55. begin
  56. SocketCall:=SocketCall(SockCallNr,a1,a2,a3,0,0,0);
  57. end;
  58. function fpsocket (domain:cint; xtype:cint; protocol: cint):cint;
  59. begin
  60. fpSocket:=SocketCall(Socket_Sys_socket,Domain,xtype,Protocol);
  61. end;
  62. function fpsend (s:cint; msg:pointer; len:size_t; flags:cint):ssize_t;
  63. begin
  64. fpSend:=SocketCall(Socket_Sys_sendto,S,TSysParam(msg),Len,Flags,0,0);
  65. end;
  66. function fpsendto (s:cint; msg:pointer; len:size_t; flags:cint; tox :psockaddr; tolen: tsocklen):ssize_t;
  67. begin
  68. fpSendto:=SocketCall(Socket_Sys_sendto,S,TSysParam(msg),Len,Flags,TSysParam(tox),tolen);
  69. end;
  70. function fprecv (s:cint; buf: pointer; len: size_t; flags:cint):ssize_t;
  71. begin
  72. fpRecv:=SocketCall(Socket_Sys_Recvfrom,S,tsysparam(buf),len,flags,0,0);
  73. end;
  74. function fprecvfrom (s:cint; buf: pointer; len: size_t; flags: cint; from : psockaddr; fromlen : psocklen):ssize_t;
  75. begin
  76. fpRecvFrom:=SocketCall(Socket_Sys_Recvfrom,S,TSysParam(buf),len,flags,TSysParam(from),TSysParam(fromlen));
  77. end;
  78. function fpbind (s:cint; addrx : psockaddr; addrlen : tsocklen):cint;
  79. begin
  80. fpBind:=SocketCall(Socket_Sys_Bind,S,TSysParam(addrx),addrlen);
  81. end;
  82. function fplisten (s:cint; backlog : cint):cint;
  83. begin
  84. fpListen:=SocketCall(Socket_Sys_Listen,S,backlog,0);
  85. end;
  86. function fpaccept (s:cint; addrx : psockaddr; addrlen : psocklen):cint;
  87. begin
  88. fpAccept:=SocketCall(Socket_Sys_accept,S,TSysParam(addrx),TSysParam(addrlen));
  89. end;
  90. function fpconnect (s:cint; name : psockaddr; namelen : tsocklen):cint;
  91. begin
  92. fpConnect:=SocketCall(Socket_Sys_connect,S,TSysParam(name),namelen);
  93. end;
  94. function fpshutdown (s:cint; how:cint):cint;
  95. begin
  96. fpShutDown:=SocketCall(Socket_Sys_shutdown,S,how,0);
  97. end;
  98. function fpgetsockname (s:cint; name : psockaddr; namelen : psocklen):cint;
  99. begin
  100. fpGetSockName:=SocketCall(Socket_Sys_GetSockName,S,TSysParam(name),TSysParam(namelen));
  101. end;
  102. function fpgetpeername (s:cint; name : psockaddr; namelen : psocklen):cint;
  103. begin
  104. fpGetPeerName:=SocketCall(Socket_Sys_GetPeerName,S,TSysParam(name),TSysParam(namelen));
  105. end;
  106. function fpsetsockopt (s:cint; level:cint; optname:cint; optval:pointer; optlen : tsocklen):cint;
  107. begin
  108. fpSetSockOpt:=SocketCall(Socket_Sys_SetSockOpt,S,level,optname,TSysParam(optval),optlen,0);
  109. end;
  110. function fpgetsockopt (s:cint; level:cint; optname:cint; optval:pointer; optlen : psocklen):cint;
  111. begin
  112. fpGetSockOpt:=SocketCall(Socket_Sys_GetSockOpt,S,level,TSysParam(optname),TSysParam(optval),TSysParam(optlen),0);
  113. end;
  114. function fpsocketpair (d:cint; xtype:cint; protocol:cint; sv:pcint):cint;
  115. begin
  116. fpSocketPair:=SocketCall(Socket_Sys_SocketPair,d,xtype,protocol,TSysParam(sv),0,0);
  117. end;
  118. {$else NEED_SOCKETCALL}
  119. function fpsocket (domain:cint; xtype:cint; protocol: cint):cint;
  120. begin
  121. fpSocket:=do_syscall(syscall_nr_socket,Domain,xtype,Protocol);
  122. end;
  123. function fpsend (s:cint; msg:pointer; len:size_t; flags:cint):ssize_t;
  124. begin
  125. fpSend:=do_syscall(syscall_nr_sendto,S,TSysParam(msg),Len,Flags,0,0);
  126. end;
  127. function fpsendto (s:cint; msg:pointer; len:size_t; flags:cint; tox :psockaddr; tolen: tsocklen):ssize_t;
  128. begin
  129. fpSendto:=do_syscall(syscall_nr_sendto,S,TSysParam(msg),Len,Flags,TSysParam(tox),tolen);
  130. end;
  131. function fprecv (s:cint; buf: pointer; len: size_t; flags:cint):ssize_t;
  132. begin
  133. fpRecv:=do_syscall(syscall_nr_Recvfrom,S,tsysparam(buf),len,flags,0,0);
  134. end;
  135. function fprecvfrom (s:cint; buf: pointer; len: size_t; flags: cint; from : psockaddr; fromlen : psocklen):ssize_t;
  136. begin
  137. fpRecvFrom:=do_syscall(syscall_nr_Recvfrom,S,TSysParam(buf),len,flags,TSysParam(from),TSysParam(fromlen));
  138. end;
  139. function fpbind (s:cint; addrx : psockaddr; addrlen : tsocklen):cint;
  140. begin
  141. fpBind:=do_syscall(syscall_nr_Bind,S,TSysParam(addrx),addrlen);
  142. end;
  143. function fplisten (s:cint; backlog : cint):cint;
  144. begin
  145. fpListen:=do_syscall(syscall_nr_Listen,S,backlog);
  146. end;
  147. function fpaccept (s:cint; addrx : psockaddr; addrlen : psocklen):cint;
  148. begin
  149. fpAccept:=do_syscall(syscall_nr_accept,S,TSysParam(addrx),TSysParam(addrlen));
  150. end;
  151. function fpconnect (s:cint; name : psockaddr; namelen : tsocklen):cint;
  152. begin
  153. fpConnect:=do_syscall(syscall_nr_connect,S,TSysParam(name),namelen);
  154. end;
  155. function fpshutdown (s:cint; how:cint):cint;
  156. begin
  157. fpShutDown:=do_syscall(syscall_nr_shutdown,S,how);
  158. end;
  159. function fpgetsockname (s:cint; name : psockaddr; namelen : psocklen):cint;
  160. begin
  161. fpGetSockName:=do_syscall(syscall_nr_GetSockName,S,TSysParam(name),TSysParam(namelen));
  162. end;
  163. function fpgetpeername (s:cint; name : psockaddr; namelen : psocklen):cint;
  164. begin
  165. fpGetPeerName:=do_syscall(syscall_nr_GetPeerName,S,TSysParam(name),TSysParam(namelen));
  166. end;
  167. function fpsetsockopt (s:cint; level:cint; optname:cint; optval:pointer; optlen : tsocklen):cint;
  168. begin
  169. fpSetSockOpt:=do_syscall(syscall_nr_SetSockOpt,S,level,optname,TSysParam(optval),optlen);
  170. end;
  171. function fpgetsockopt (s:cint; level:cint; optname:cint; optval:pointer; optlen : psocklen):cint;
  172. begin
  173. fpGetSockOpt:=do_syscall(syscall_nr_GetSockOpt,S,level,TSysParam(optname),TSysParam(optval),TSysParam(optlen));
  174. end;
  175. function fpsocketpair (d:cint; xtype:cint; protocol:cint; sv:pcint):cint;
  176. begin
  177. fpSocketPair:=do_syscall(syscall_nr_SocketPair,d,xtype,protocol,TSysParam(sv));
  178. end;
  179. {$endif NEED_do_syscall}