sockets.inc 11 KB

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