sockets.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. repeat
  144. DoAccept:=fpaccept(Sock,@Addr,@AddrLen);
  145. until (DoAccept<>-1) or (SocketError <> EsockEINTR);
  146. end;
  147. Function DoConnect(Sock:longint;const addr: TInetSockAddr): Boolean;
  148. var
  149. res: longint;
  150. begin
  151. repeat
  152. res:=fpconnect(Sock,@Addr,SizeOF(TInetSockAddr));
  153. until (res<>-1) or (SocketError <> EsockEINTR);
  154. DoConnect:= res = 0;
  155. end;
  156. {$warnings off}
  157. Function Connect(Sock:longint;const addr: TInetSockAddr;var SockIn,SockOut:text):Boolean;
  158. begin
  159. Connect:=DoConnect(Sock,addr);
  160. If Connect then
  161. Sock2Text(Sock,SockIn,SockOut);
  162. end;
  163. Function Connect(Sock:longint;const addr:TInetSockAddr;var SockIn,SockOut:file):Boolean;
  164. begin
  165. Connect:=DoConnect(Sock,addr);
  166. If Connect then
  167. Sock2File(Sock,SockIn,SockOut);
  168. end;
  169. Function Accept(Sock:longint;var addr:TInetSockAddr;var SockIn,SockOut:text):Boolean;
  170. var
  171. s : longint;
  172. begin
  173. S:=DoAccept(Sock,addr);
  174. if S>0 then
  175. begin
  176. Sock2Text(S,SockIn,SockOut);
  177. Accept:=true;
  178. end
  179. else
  180. Accept:=false;
  181. end;
  182. Function Accept(Sock:longint;var addr:TInetSockAddr;var SockIn,SockOut:File):Boolean;
  183. var
  184. s : longint;
  185. begin
  186. S:=DoAccept(Sock,addr);
  187. if S>0 then
  188. begin
  189. Sock2File(S,SockIn,SockOut);
  190. Accept:=true;
  191. end
  192. else
  193. Accept:=false;
  194. end;
  195. {$warnings on}
  196. type thostaddr= packed array[1..4] of byte;
  197. function htonl( host : longint):longint; inline;
  198. begin
  199. {$ifdef FPC_BIG_ENDIAN}
  200. htonl:=host;
  201. {$else}
  202. htonl:=THostAddr(host)[4];
  203. htonl:=htonl or longint( (THostAddr(host)[3]) shl 8);
  204. htonl:=htonl or longint( (THostAddr(host)[2]) shl 16);
  205. htonl:=htonl or longint( (THostAddr(host)[1]) shl 24);
  206. {$endif}
  207. end;
  208. Function NToHl (Net : Longint) : Longint; inline;
  209. begin
  210. {$ifdef FPC_BIG_ENDIAN}
  211. ntohl:=net;
  212. {$else}
  213. ntohl:=THostAddr(Net)[4];
  214. ntohl:=ntohl or longint( (THostAddr(Net)[3]) shl 8);
  215. ntohl:=ntohl or longint( (THostAddr(Net)[2]) shl 16);
  216. ntohl:=ntohl or longint( (THostAddr(Net)[1]) shl 24);
  217. {$endif}
  218. end;
  219. function htons( host : word):word; inline;
  220. begin
  221. {$ifdef FPC_BIG_ENDIAN}
  222. htons:=host;
  223. {$else}
  224. htons:=swap(host);
  225. {$endif}
  226. end;
  227. Function NToHs (Net : word):word; inline;
  228. begin
  229. {$ifdef FPC_BIG_ENDIAN}
  230. ntohs:=net;
  231. {$else}
  232. ntohs:=swap(net);
  233. {$endif}
  234. end;
  235. Type array4int = array[1..4] of byte;
  236. function NetAddrToStr (Entry : in_addr) : AnsiString;
  237. Var Dummy : Ansistring;
  238. i,j : Longint;
  239. begin
  240. NetAddrToStr:='';
  241. j:=entry.s_addr;
  242. For I:=1 to 4 do
  243. begin
  244. Str(array4int(j)[i],Dummy);
  245. NetAddrToStr:=NetAddrToStr+Dummy;
  246. If I<4 Then
  247. NetAddrToStr:=NetAddrToStr+'.';
  248. end;
  249. end;
  250. function HostAddrToStr (Entry : in_addr) : AnsiString;
  251. Var x: in_addr;
  252. begin
  253. x.s_addr:=htonl(entry.s_addr);
  254. HostAddrToStr:=NetAddrToStr(x);
  255. end;
  256. function StrToHostAddr(IP : AnsiString) : in_addr ;
  257. Var
  258. Dummy : AnsiString;
  259. I,j,k : Longint;
  260. Temp : in_addr;
  261. begin
  262. strtohostaddr.s_addr:=0; //:=NoAddress;
  263. For I:=1 to 4 do
  264. begin
  265. If I<4 Then
  266. begin
  267. J:=Pos('.',IP);
  268. If J=0 then
  269. exit;
  270. Dummy:=Copy(IP,1,J-1);
  271. Delete (IP,1,J);
  272. end
  273. else
  274. Dummy:=IP;
  275. Val (Dummy,k,J);
  276. array4int(temp.s_addr)[i]:=k;
  277. If J<>0 then Exit;
  278. end;
  279. strtohostaddr.s_addr:=ntohl(Temp.s_addr);
  280. end;
  281. function StrToNetAddr(IP : AnsiString) : in_addr;
  282. begin
  283. StrToNetAddr.s_addr:=htonl(StrToHostAddr(IP).s_addr);
  284. end;
  285. Function HostToNet (Host : in_addr):in_addr;
  286. begin
  287. HostToNet.s_addr:=htonl(host.s_addr);
  288. end;
  289. Function NetToHost (Net : in_addr) : in_addr;
  290. begin
  291. NetToHost.s_addr:=ntohl(net.s_addr);
  292. end;
  293. Function HostToNet (Host : Longint) : Longint;
  294. begin
  295. HostToNet:=htonl(host);
  296. end;
  297. Function NetToHost (Net : Longint) : Longint;
  298. begin
  299. NetToHost:=ntohl(net);
  300. end;
  301. Function ShortHostToNet (Host : Word) : Word;
  302. begin
  303. ShortHostToNet:=htons(host);
  304. end;
  305. Function ShortNetToHost (Net : Word) : Word;
  306. begin
  307. ShortNEtToHost:=ntohs(net);
  308. end;
  309. const digittab : shortstring = ('0123456789ABCDEF');
  310. function lclinttohex (i:integer;digits:longint): ansistring;
  311. begin
  312. SetLength(lclinttohex,4);
  313. lclinttohex[4]:=digittab[1+(i and 15)];
  314. lclinttohex[3]:=digittab[1+((i shr 4) and 15)];
  315. lclinttohex[2]:=digittab[1+((i shr 8) and 15)];
  316. lclinttohex[1]:=digittab[1+((i shr 12) and 15)];;
  317. end;
  318. function HostAddrToStr6 (Entry : TIn6_Addr) :ansiString;
  319. var
  320. i: byte;
  321. zr1,zr2: set of byte;
  322. zc1,zc2: byte;
  323. have_skipped: boolean;
  324. begin
  325. zr1 := [];
  326. zr2 := [];
  327. zc1 := 0;
  328. zc2 := 0;
  329. for i := 0 to 7 do begin
  330. if Entry.u6_addr16[i] = 0 then begin
  331. include(zr2, i);
  332. inc(zc2);
  333. end else begin
  334. if zc1 < zc2 then begin
  335. zc1 := zc2;
  336. zr1 := zr2;
  337. zc2 := 0; zr2 := [];
  338. end;
  339. end;
  340. end;
  341. if zc1 < zc2 then begin
  342. zc1 := zc2;
  343. zr1 := zr2;
  344. end;
  345. SetLength(HostAddrToStr6, 8*5-1);
  346. SetLength(HostAddrToStr6, 0);
  347. have_skipped := false;
  348. for i := 0 to 7 do begin
  349. if not (i in zr1) then begin
  350. if have_skipped then begin
  351. if HostAddrToStr6 = ''
  352. then HostAddrToStr6 := '::'
  353. else HostAddrToStr6 := HostAddrToStr6 + ':';
  354. have_skipped := false;
  355. end;
  356. // FIXME: is that shortnettohost really proper there? I wouldn't be too sure...
  357. HostAddrToStr6 := HostAddrToStr6 +lclIntToHex(ShortNetToHost(Entry.u6_addr16[i]), 1) + ':';
  358. end else begin
  359. have_skipped := true;
  360. end;
  361. end;
  362. if have_skipped then
  363. if HostAddrToStr6 = ''
  364. then HostAddrToStr6 := '::'
  365. else HostAddrToStr6 := HostAddrToStr6 + ':';
  366. if HostAddrToStr6 = '' then HostAddrToStr6 := '::';
  367. if not (7 in zr1) then
  368. SetLength(HostAddrToStr6, Length(HostAddrToStr6)-1);
  369. end;
  370. function StrToHostAddr6(IP : String) : TIn6_addr;
  371. Var Part : String;
  372. IPv6 : TIn6_addr;
  373. P,J : Integer;
  374. W : Word;
  375. Index : Integer;
  376. ZeroAt : Integer;
  377. Begin
  378. FillChar(IPv6,SizeOf(IPv6),0);
  379. { Every 16-bit block is converted at its own and stored into Result. When }
  380. { the '::' zero-spacer is found, its location is stored. Afterwards the }
  381. { address is shifted and zero-filled. }
  382. Index := 0; ZeroAt := -1;
  383. J := 0;
  384. P := Pos(':',IP);
  385. While (P > 0) and (Length(IP) > 0) and (Index < 8) do
  386. Begin
  387. Part := '$'+Copy(IP,1,P-1);
  388. Delete(IP,1,P);
  389. if Length(Part) > 1 then { is there a digit after the '$'? }
  390. Val(Part,W,J)
  391. else W := 0;
  392. IPv6.u6_addr16[Index] := HtoNS(W);
  393. if J <> 0 then
  394. Begin
  395. FillChar(IPv6,SizeOf(IPv6),0);
  396. Exit;
  397. End;
  398. if IP[1] = ':' then
  399. Begin
  400. ZeroAt := Index;
  401. Delete(IP,1,1);
  402. End;
  403. Inc(Index);
  404. P := Pos(':',IP); if P = 0 then P := Length(IP)+1;
  405. End;
  406. { address a:b:c::f:g:h }
  407. { Result now a : b : c : f : g : h : 0 : 0, ZeroAt = 2, Index = 6 }
  408. { Result after a : b : c : 0 : 0 : f : g : h }
  409. if ZeroAt >= 0 then
  410. Begin
  411. Move(IPv6.u6_addr16[ZeroAt+1],IPv6.u6_addr16[(8-Index)+ZeroAt+1],2*(Index-ZeroAt-1));
  412. FillChar(IPv6.u6_addr16[ZeroAt+1],2*(8-Index),0);
  413. End;
  414. StrToHostAddr6:=IPv6;
  415. End;
  416. function NetAddrToStr6 (Entry : TIn6_Addr) : ansiString;
  417. begin
  418. netaddrtostr6 := HostAddrToStr6((Entry));
  419. end;
  420. function StrToNetAddr6(IP : ansiString) : TIn6_Addr;
  421. begin
  422. StrToNetAddr6 := StrToHostAddr6(IP);
  423. end;