sockets.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by the Free Pascal development team
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************}
  10. {******************************************************************************
  11. Text File Writeln/ReadLn Support
  12. ******************************************************************************}
  13. Procedure OpenSock(var F:Text);
  14. begin
  15. if textrec(f).handle=UnusedHandle then
  16. textrec(f).mode:=fmclosed
  17. else
  18. case textrec(f).userdata[1] of
  19. S_OUT : textrec(f).mode:=fmoutput;
  20. S_IN : textrec(f).mode:=fminput;
  21. else
  22. textrec(f).mode:=fmclosed;
  23. end;
  24. end;
  25. procedure iosock(var f:text);
  26. var r:sizeint;
  27. def_error:word;
  28. begin
  29. with textrec(f) do
  30. begin
  31. case mode of
  32. fmoutput:
  33. begin
  34. repeat
  35. {$ifdef use_readwrite}
  36. r:=fpwrite(handle,bufptr^,bufpos);
  37. {$else}
  38. r:=fpsend(handle,bufptr,bufpos,0);
  39. {$endif}
  40. until (r<>-1) or (SocketError <> EsockEINTR);
  41. bufend:=r;
  42. def_error:=101; {File write error.}
  43. end;
  44. fminput:
  45. begin
  46. repeat
  47. {$ifdef use_readwrite}
  48. r:=fpread(handle,bufptr^,bufsize);
  49. {$else}
  50. r:=fprecv(handle,bufptr,bufsize,0);
  51. {$endif}
  52. until (r<>-1) or (SocketError <> EsockEINTR);
  53. bufend:=r;
  54. def_error:=100; {File read error.}
  55. end;
  56. end;
  57. if r=-1 then
  58. case SocketError of
  59. EsockEBADF:
  60. { EsysENOTSOCK:} {Why is this constant not defined? (DM)}
  61. inoutres:=6; {Invalid file handle.}
  62. EsockEFAULT:
  63. inoutres:=217;
  64. EsockEINVAL:
  65. inoutres:=218;
  66. else
  67. inoutres:=def_error;
  68. end;
  69. bufpos:=0;
  70. end;
  71. end;
  72. Procedure FlushSock(var F:Text);
  73. begin
  74. if (textrec(f).mode=fmoutput) and (textrec(f).bufpos<>0) then
  75. begin
  76. IOSock(f);
  77. textrec(f).bufpos:=0;
  78. end;
  79. end;
  80. Procedure CloseSock(var F:text);
  81. begin
  82. { Nothing special has to be done here }
  83. end;
  84. Procedure Sock2Text(Sock:Longint;Var SockIn,SockOut:Text);
  85. {
  86. Set up two Pascal Text file descriptors for reading and writing)
  87. }
  88. begin
  89. { First the reading part.}
  90. Assign(SockIn,'.');
  91. Textrec(SockIn).Handle:=Sock;
  92. Textrec(Sockin).userdata[1]:=S_IN;
  93. TextRec(SockIn).OpenFunc:=@OpenSock;
  94. TextRec(SockIn).InOutFunc:=@IOSock;
  95. TextRec(SockIn).FlushFunc:=@FlushSock;
  96. TextRec(SockIn).CloseFunc:=@CloseSock;
  97. TextRec(SockIn).Mode := fmInput;
  98. Case DefaultTextLineBreakStyle Of
  99. tlbsLF: TextRec(sockin).LineEnd := #10;
  100. tlbsCRLF: TextRec(sockin).LineEnd := #13#10;
  101. tlbsCR: TextRec(sockin).LineEnd := #13;
  102. End;
  103. { Now the writing part. }
  104. Assign(SockOut,'.');
  105. Textrec(SockOut).Handle:=Sock;
  106. Textrec(SockOut).userdata[1]:=S_OUT;
  107. TextRec(SockOut).OpenFunc:=@OpenSock;
  108. TextRec(SockOut).InOutFunc:=@IOSock;
  109. TextRec(SockOut).FlushFunc:=@FlushSock;
  110. TextRec(SockOut).CloseFunc:=@CloseSock;
  111. TextRec(SockOut).Mode := fmOutput;
  112. Case DefaultTextLineBreakStyle Of
  113. tlbsLF: TextRec(sockout).LineEnd := #10;
  114. tlbsCRLF: TextRec(sockout).LineEnd := #13#10;
  115. tlbsCR: TextRec(sockout).LineEnd := #13;
  116. End;
  117. end;
  118. {******************************************************************************
  119. Untyped File
  120. ******************************************************************************}
  121. Procedure Sock2File(Sock:Longint;Var SockIn,SockOut:File);
  122. begin
  123. {Input}
  124. Assign(SockIn,'.');
  125. FileRec(SockIn).Handle:=Sock;
  126. FileRec(SockIn).RecSize:=1;
  127. FileRec(Sockin).userdata[1]:=S_IN;
  128. FileRec(SockIn).Mode := fmInput;
  129. {Output}
  130. Assign(SockOut,'.');
  131. FileRec(SockOut).Handle:=Sock;
  132. FileRec(SockOut).RecSize:=1;
  133. FileRec(SockOut).userdata[1]:=S_OUT;
  134. FileRec(SockOut).Mode := fmOutput;
  135. end;
  136. {******************************************************************************
  137. InetSock
  138. ******************************************************************************}
  139. Function DoAccept(Sock:longint;Var addr:TInetSockAddr):longint;
  140. Var AddrLen : Longint;
  141. begin
  142. AddrLEn:=SizeOf(Addr);
  143. DoAccept:=fpaccept(Sock,@Addr,@AddrLen);
  144. end;
  145. Function DoConnect(Sock:longint;const addr: TInetSockAddr): Boolean;
  146. begin
  147. DoConnect:=fpconnect(Sock,@Addr,SizeOF(TInetSockAddr)) = 0;
  148. end;
  149. {$warnings off}
  150. Function Connect(Sock:longint;const addr: TInetSockAddr;var SockIn,SockOut:text):Boolean;
  151. begin
  152. Connect:=DoConnect(Sock,addr);
  153. If Connect then
  154. Sock2Text(Sock,SockIn,SockOut);
  155. end;
  156. Function Connect(Sock:longint;const addr:TInetSockAddr;var SockIn,SockOut:file):Boolean;
  157. begin
  158. Connect:=DoConnect(Sock,addr);
  159. If Connect then
  160. Sock2File(Sock,SockIn,SockOut);
  161. end;
  162. Function Accept(Sock:longint;var addr:TInetSockAddr;var SockIn,SockOut:text):Boolean;
  163. var
  164. s : longint;
  165. begin
  166. S:=DoAccept(Sock,addr);
  167. if S>0 then
  168. begin
  169. Sock2Text(S,SockIn,SockOut);
  170. Accept:=true;
  171. end
  172. else
  173. Accept:=false;
  174. end;
  175. Function Accept(Sock:longint;var addr:TInetSockAddr;var SockIn,SockOut:File):Boolean;
  176. var
  177. s : longint;
  178. begin
  179. S:=DoAccept(Sock,addr);
  180. if S>0 then
  181. begin
  182. Sock2File(S,SockIn,SockOut);
  183. Accept:=true;
  184. end
  185. else
  186. Accept:=false;
  187. end;
  188. {$warnings on}
  189. type thostaddr= packed array[1..4] of byte;
  190. function htonl( host : longint):longint; inline;
  191. begin
  192. {$ifdef FPC_BIG_ENDIAN}
  193. htonl:=host;
  194. {$else}
  195. htonl:=THostAddr(host)[4];
  196. htonl:=htonl or longint( (THostAddr(host)[3]) shl 8);
  197. htonl:=htonl or longint( (THostAddr(host)[2]) shl 16);
  198. htonl:=htonl or longint( (THostAddr(host)[1]) shl 24);
  199. {$endif}
  200. end;
  201. Function NToHl (Net : Longint) : Longint; inline;
  202. begin
  203. {$ifdef FPC_BIG_ENDIAN}
  204. ntohl:=net;
  205. {$else}
  206. ntohl:=THostAddr(Net)[4];
  207. ntohl:=ntohl or longint( (THostAddr(Net)[3]) shl 8);
  208. ntohl:=ntohl or longint( (THostAddr(Net)[2]) shl 16);
  209. ntohl:=ntohl or longint( (THostAddr(Net)[1]) shl 24);
  210. {$endif}
  211. end;
  212. function htons( host : word):word; inline;
  213. begin
  214. {$ifdef FPC_BIG_ENDIAN}
  215. htons:=host;
  216. {$else}
  217. htons:=swap(host);
  218. {$endif}
  219. end;
  220. Function NToHs (Net : word):word; inline;
  221. begin
  222. {$ifdef FPC_BIG_ENDIAN}
  223. ntohs:=net;
  224. {$else}
  225. ntohs:=swap(net);
  226. {$endif}
  227. end;
  228. Type array4int = array[1..4] of byte;
  229. function NetAddrToStr (Entry : in_addr) : AnsiString;
  230. Var Dummy : Ansistring;
  231. i,j : Longint;
  232. begin
  233. NetAddrToStr:='';
  234. j:=entry.s_addr;
  235. For I:=1 to 4 do
  236. begin
  237. Str(array4int(j)[i],Dummy);
  238. NetAddrToStr:=NetAddrToStr+Dummy;
  239. If I<4 Then
  240. NetAddrToStr:=NetAddrToStr+'.';
  241. end;
  242. end;
  243. function HostAddrToStr (Entry : in_addr) : AnsiString;
  244. Var x: in_addr;
  245. begin
  246. x.s_addr:=htonl(entry.s_addr);
  247. HostAddrToStr:=NetAddrToStr(x);
  248. end;
  249. function StrToHostAddr(IP : AnsiString) : in_addr ;
  250. Var
  251. Dummy : AnsiString;
  252. I,j,k : Longint;
  253. Temp : in_addr;
  254. begin
  255. strtohostaddr.s_addr:=0; //:=NoAddress;
  256. For I:=1 to 4 do
  257. begin
  258. If I<4 Then
  259. begin
  260. J:=Pos('.',IP);
  261. If J=0 then
  262. exit;
  263. Dummy:=Copy(IP,1,J-1);
  264. Delete (IP,1,J);
  265. end
  266. else
  267. Dummy:=IP;
  268. Val (Dummy,k,J);
  269. array4int(temp.s_addr)[i]:=k;
  270. If J<>0 then Exit;
  271. end;
  272. strtohostaddr.s_addr:=ntohl(Temp.s_addr);
  273. end;
  274. function StrToNetAddr(IP : AnsiString) : in_addr;
  275. begin
  276. StrToNetAddr.s_addr:=htonl(StrToHostAddr(IP).s_addr);
  277. end;
  278. Function HostToNet (Host : in_addr):in_addr;
  279. begin
  280. HostToNet.s_addr:=htonl(host.s_addr);
  281. end;
  282. Function NetToHost (Net : in_addr) : in_addr;
  283. begin
  284. NetToHost.s_addr:=ntohl(net.s_addr);
  285. end;
  286. Function HostToNet (Host : Longint) : Longint;
  287. begin
  288. HostToNet:=htonl(host);
  289. end;
  290. Function NetToHost (Net : Longint) : Longint;
  291. begin
  292. NetToHost:=ntohl(net);
  293. end;
  294. Function ShortHostToNet (Host : Word) : Word;
  295. begin
  296. ShortHostToNet:=htons(host);
  297. end;
  298. Function ShortNetToHost (Net : Word) : Word;
  299. begin
  300. ShortNEtToHost:=ntohs(net);
  301. end;
  302. const digittab : shortstring = ('0123456789ABCDEF');
  303. function lclinttohex (i:integer;digits:longint): ansistring;
  304. begin
  305. SetLength(lclinttohex,4);
  306. lclinttohex[4]:=digittab[1+(i and 15)];
  307. lclinttohex[3]:=digittab[1+((i shr 4) and 15)];
  308. lclinttohex[2]:=digittab[1+((i shr 8) and 15)];
  309. lclinttohex[1]:=digittab[1+((i shr 12) and 15)];;
  310. end;
  311. function HostAddrToStr6 (Entry : TIn6_Addr) :ansiString;
  312. var
  313. i: byte;
  314. zr1,zr2: set of byte;
  315. zc1,zc2: byte;
  316. have_skipped: boolean;
  317. begin
  318. zr1 := [];
  319. zr2 := [];
  320. zc1 := 0;
  321. zc2 := 0;
  322. for i := 0 to 7 do begin
  323. if Entry.u6_addr16[i] = 0 then begin
  324. include(zr2, i);
  325. inc(zc2);
  326. end else begin
  327. if zc1 < zc2 then begin
  328. zc1 := zc2;
  329. zr1 := zr2;
  330. zc2 := 0; zr2 := [];
  331. end;
  332. end;
  333. end;
  334. if zc1 < zc2 then begin
  335. zc1 := zc2;
  336. zr1 := zr2;
  337. end;
  338. SetLength(HostAddrToStr6, 8*5-1);
  339. SetLength(HostAddrToStr6, 0);
  340. have_skipped := false;
  341. for i := 0 to 7 do begin
  342. if not (i in zr1) then begin
  343. if have_skipped then begin
  344. if HostAddrToStr6 = ''
  345. then HostAddrToStr6 := '::'
  346. else HostAddrToStr6 := HostAddrToStr6 + ':';
  347. have_skipped := false;
  348. end;
  349. // FIXME: is that shortnettohost really proper there? I wouldn't be too sure...
  350. HostAddrToStr6 := HostAddrToStr6 +lclIntToHex(ShortNetToHost(Entry.u6_addr16[i]), 1) + ':';
  351. end else begin
  352. have_skipped := true;
  353. end;
  354. end;
  355. if have_skipped then
  356. if HostAddrToStr6 = ''
  357. then HostAddrToStr6 := '::'
  358. else HostAddrToStr6 := HostAddrToStr6 + ':';
  359. if HostAddrToStr6 = '' then HostAddrToStr6 := '::';
  360. if not (7 in zr1) then
  361. SetLength(HostAddrToStr6, Length(HostAddrToStr6)-1);
  362. end;
  363. function StrToHostAddr6(IP : String) : TIn6_addr;
  364. Var Part : String;
  365. IPv6 : TIn6_addr;
  366. P,J : Integer;
  367. W : Word;
  368. Index : Integer;
  369. ZeroAt : Integer;
  370. Begin
  371. FillChar(IPv6,SizeOf(IPv6),0);
  372. { Every 16-bit block is converted at its own and stored into Result. When }
  373. { the '::' zero-spacer is found, its location is stored. Afterwards the }
  374. { address is shifted and zero-filled. }
  375. Index := 0; ZeroAt := -1;
  376. J := 0;
  377. P := Pos(':',IP);
  378. While (P > 0) and (Length(IP) > 0) and (Index < 8) do
  379. Begin
  380. Part := '$'+Copy(IP,1,P-1);
  381. Delete(IP,1,P);
  382. if Length(Part) > 1 then { is there a digit after the '$'? }
  383. Val(Part,W,J)
  384. else W := 0;
  385. IPv6.u6_addr16[Index] := HtoNS(W);
  386. if J <> 0 then
  387. Begin
  388. FillChar(IPv6,SizeOf(IPv6),0);
  389. Exit;
  390. End;
  391. if IP[1] = ':' then
  392. Begin
  393. ZeroAt := Index;
  394. Delete(IP,1,1);
  395. End;
  396. Inc(Index);
  397. P := Pos(':',IP); if P = 0 then P := Length(IP)+1;
  398. End;
  399. { address a:b:c::f:g:h }
  400. { Result now a : b : c : f : g : h : 0 : 0, ZeroAt = 2, Index = 6 }
  401. { Result after a : b : c : 0 : 0 : f : g : h }
  402. if ZeroAt >= 0 then
  403. Begin
  404. Move(IPv6.u6_addr16[ZeroAt+1],IPv6.u6_addr16[(8-Index)+ZeroAt+1],2*(Index-ZeroAt-1));
  405. FillChar(IPv6.u6_addr16[ZeroAt+1],2*(8-Index),0);
  406. End;
  407. StrToHostAddr6:=IPv6;
  408. End;
  409. function NetAddrToStr6 (Entry : TIn6_Addr) : ansiString;
  410. begin
  411. netaddrtostr6 := HostAddrToStr6((Entry));
  412. end;
  413. function StrToNetAddr6(IP : ansiString) : TIn6_Addr;
  414. begin
  415. StrToNetAddr6 := StrToHostAddr6(IP);
  416. end;