3 {socket code by plugwash}
\r
5 { Copyright (C) 2005 Bas Steendijk and Peter Green
\r
6 For conditions of distribution and use, see copyright notice in zlib_license.txt
\r
7 which is included in the package
\r
8 ----------------------------------------------------------------------------- }
\r
10 changes by plugwash (20030728)
\r
11 * created handlefdtrigger virtual method in tlasio (overridden in tlsocket) and moved a lot of code from messageloop into it
\r
12 * changed tlasio to tlasio
\r
13 * split fdhandle into fdhandlein and fdhandleout
\r
14 * i now use fdsrmaster and fdswmaster instead of rebuilding the lists every loop
\r
15 * split lsocket.pas into lsocket.pas and lcore.pas
\r
18 changes by beware (20030903)
\r
19 * added getxaddr, getxport (local addr, port, as string)
\r
20 * added getpeername, remote addr+port as binary
\r
21 * added htons and htonl functions (endian swap, same interface as windows API)
\r
24 * if connect failed (conn refused) set state to connected and call internalclose, to get closed handler (instead of fdclose)
\r
25 * (lcore) if closing the fd's in internalclose, set fds to -1 because closing an fd makes it invalid
\r
28 * fixed: on connect failed, tried to close fdhandle's which were already set to -1, added check
\r
31 * added getpeeraddr, getpeerport, remote addr+port as string
\r
40 {$include lcoreconfig.inc}
\r
52 baseunix,unix,unixutil,
\r
56 classes,{pgdebugout,}pgtypes,lcore,fd_utils,binipstuff,dnssync;
\r
60 v4listendefault:boolean=false;
\r
65 sunB = packed record
\r
66 s_b1, s_b2, s_b3, s_b4: byte;
\r
69 SunW = packed record
\r
73 TInAddr = packed record
\r
77 2: (S_addr: cardinal);
\r
81 TLsocket = class(tlasio)
\r
85 inAddr : TInetSockAddrV;
\r
87 biniplist:tbiniplist;
\r
90 connecttimeout:tltimer;
\r
92 { inAddrSize:integer;}
\r
94 //host : THostentry ;
\r
96 //mainthread : boolean ; //for debugging only
\r
99 localaddr:thostname;
\r
100 localport:ansistring;
\r
103 listenqueue:integer;
\r
105 onconnecttryip:procedure(sender:tobject; const ip:tbinip) of object;
\r
107 {$ifdef secondlistener}
\r
108 secondlistener:tlsocket;
\r
109 lastsessionfromsecond:boolean;
\r
110 procedure secondaccepthandler(sender:tobject;error:word);
\r
111 procedure internalclose(error:word);override;
\r
113 function getaddrsize:integer;
\r
114 procedure connect; virtual;
\r
115 procedure realconnect;
\r
116 procedure bindsocket;
\r
118 function accept : longint;
\r
119 function sendto(dest:TInetSockAddrV;destlen:integer;data:pointer;len:integer):integer; virtual;
\r
120 function receivefrom(data:pointer;len:integer;var src:TInetSockAddrV;var srclen:integer):integer; virtual;
\r
122 procedure handlefdtrigger(readtrigger,writetrigger:boolean); override;
\r
123 function send(data:pointer;len:integer):integer;override;
\r
124 procedure sendstr(const str : tbufferstring);override;
\r
125 function Receive(Buf:Pointer;BufSize:integer):integer; override;
\r
126 function getpeername(var addr:tsockaddrin;addrlen:integer):integer; virtual;
\r
127 procedure getXaddrbin(var binip:tbinip); virtual;
\r
128 procedure getpeeraddrbin(var binip:tbinip); virtual;
\r
129 function getXaddr:thostname; virtual;
\r
130 function getpeeraddr:thostname; virtual;
\r
131 function getXport:ansistring; virtual;
\r
132 function getpeerport:ansistring; virtual;
\r
133 constructor Create(AOwner: TComponent); override;
\r
135 //this one has to be kept public for now because lcorewsaasyncselect calls it
\r
136 procedure connectionfailedhandler(error:word);
\r
138 {public in tlasio, and can't be private in both places, so should be public here.
\r
139 fixes delphi warning --beware}
\r
141 procedure myfdclose(fd : integer); override;
\r
142 function myfdwrite(fd: LongInt;const buf;size: LongInt):LongInt; override;
\r
143 function myfdread(fd: LongInt;var buf;size: LongInt):LongInt; override;
\r
148 isv6socket : boolean; //identifies if the socket is v6, set by bindsocket
\r
150 procedure taskcallconnectionfailedhandler(wparam,lparam : longint);
\r
152 procedure connecttimeouthandler(sender:tobject);
\r
153 procedure connectsuccesshandler;
\r
155 tsocket=longint; // for compatibility with twsocket
\r
157 twsocket=tlsocket; {easy}
\r
165 {$include unixstuff.inc}
\r
168 function tlsocket.getaddrsize:integer;
\r
170 result := inaddrsize(inaddr);
\r
173 //I used to use the system versions of these from within lsocket (which has
\r
174 //functions whose name clashes with them) by using sockets.* and but I can't do
\r
175 //that anymore since in some cases connect is now provided by unixstuff.inc
\r
176 //hence these wrapper functions --plugwash
\r
177 {$ifndef mswindows}
\r
178 function system_Connect(Sock: LongInt;const Addr;Addrlen: LongInt):Boolean;
\r
180 result := connect(sock,addr,addrlen);
\r
182 function system_SendTo(Sock: LongInt; const Buf;BufLen: LongInt;Flags: LongInt;var Addr;AddrLen: LongInt):LongInt;
\r
184 result := sendto(sock,buf,buflen,flags,addr,addrlen);
\r
186 function system_getpeername(Sock: LongInt;var Addr;var Addrlen: LongInt):LongInt;
\r
188 result := getpeername(sock,addr,addrlen);
\r
190 function system_listen(Sock: LongInt; MaxConnect: LongInt):Boolean;
\r
192 result := listen(sock,maxconnect);
\r
194 function system_Accept(Sock: LongInt;var Addr;var Addrlen: LongInt):LongInt;
\r
196 result := accept(sock,addr,addrlen);
\r
200 procedure tlsocket.realconnect;
\r
205 iptemp := biniplist_get(biniplist,currentip);
\r
206 //writeln('trying to connect to ',ipbintostr(biniplist_get(biniplist,currentip)),'#',port);
\r
207 if assigned(onconnecttryip) then onconnecttryip(self,iptemp);
\r
208 makeinaddrv(iptemp,port,inaddr);
\r
210 if (currentip >= biniplist_getcount(biniplist)) then trymoreips := false;
\r
213 if (uppercase(proto) = 'UDP') then begin
\r
218 end else if (uppercase(proto) = 'TCP') or (uppercase(proto) = '') then begin
\r
222 end else if (uppercase(proto) = 'ICMP') or (strtointdef(proto,256) < 256) then begin
\r
223 b := strtointdef(proto,IPPROTO_ICMP);
\r
227 raise ESocketException.create('unrecognised protocol');
\r
230 a := Socket(inaddr.inaddr.family,a,b);
\r
231 //writeln(ord(inaddr.inaddr.family));
\r
232 if a = -1 then begin
\r
233 //unable to create socket, fire an error event (better to use an error event
\r
234 //to avoid poor interaction with multilistener stuff.
\r
235 //a socket value of -2 is a special value to say there is no socket but
\r
236 //we want internalclose to act as if there was
\r
239 tltask.create(taskcallconnectionfailedhandler,self,{$ifdef mswindows}wsagetlasterror{$else}socketerror{$endif},0);
\r
245 if dgram then begin
\r
246 {$ifndef mswindows}
\r
247 SetSocketOptions(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));
\r
249 SetSockOpt(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));
\r
251 state := wsconnected;
\r
252 if assigned(onsessionconnected) then onsessionconnected(self,0);
\r
254 eventcore.rmasterset(fdhandlein,false);
\r
255 eventcore.wmasterclr(fdhandleout);
\r
257 state :=wsconnecting;
\r
259 //writeln(inaddr.inaddr.port);
\r
260 winsock.Connect(fdhandlein,winsock.psockaddr(@inADDR)^,getaddrsize);
\r
262 system_Connect(fdhandlein,inADDR,getaddrsize);
\r
264 eventcore.rmasterset(fdhandlein,false);
\r
265 eventcore.wmasterset(fdhandleout);
\r
266 if trymoreips then connecttimeout.enabled := true;
\r
270 on e: exception do begin
\r
272 raise; //reraise the exception
\r
278 procedure tlsocket.connecttimeouthandler(sender:tobject);
\r
280 connecttimeout.enabled := false;
\r
281 destroying := true; //hack to not cause handler to trigger
\r
283 destroying := false;
\r
290 procedure tlsocket.connect;
\r
292 if state <> wsclosed then close;
\r
294 if isbiniplist(addr) then biniplist := addr else biniplist := forwardlookuplist(addr,0);
\r
295 if biniplist_getcount(biniplist) = 0 then raise exception.create('unable to resolve '+addr);
\r
297 //makeinaddrv(addr,port,inaddr);
\r
300 if not assigned(connecttimeout) then begin
\r
301 connecttimeout := tltimer.create(self);
\r
302 connecttimeout.ontimer := connecttimeouthandler;
\r
303 connecttimeout.interval := 5000;
\r
304 connecttimeout.enabled := false;
\r
309 procedure tlsocket.sendstr(const str : tbufferstring);
\r
311 if dgram then begin
\r
312 send(@str[1],length(str))
\r
314 inherited sendstr(str);
\r
318 function tlsocket.send(data:pointer;len:integer):integer;
\r
320 if dgram then begin
\r
321 // writeln('sending to '+ipbintostr(inaddrvtobinip(inaddr)),' ',htons(inaddr.inaddr.port),' ',len,' bytes');
\r
322 result := sendto(inaddr,getaddrsize,data,len);
\r
324 // writeln('send result ',result);
\r
325 // writeln('errno',errno);
\r
327 result := inherited send(data,len);
\r
332 function tlsocket.receive(Buf:Pointer;BufSize:integer):integer;
\r
334 if dgram then begin
\r
335 {$ifdef secondlistener}
\r
336 if lastsessionfromsecond then begin
\r
337 result := secondlistener.receive(buf,bufsize);
\r
338 lastsessionfromsecond := false;
\r
341 result := myfdread(self.fdhandlein,buf^,bufsize);
\r
343 result := inherited receive(buf,bufsize);
\r
347 procedure tlsocket.bindsocket;
\r
349 inAddrtemp:TInetSockAddrV;
\r
350 inAddrtempx:{$ifdef mswindows}winsock.TSockaddr{$else}TInetSockAddrV{$endif} absolute inaddrtemp;
\r
351 inaddrtempsize:integer;
\r
354 if (localaddr <> '') or (localport <> '') then begin
\r
355 if localaddr = '' then begin
\r
357 if inaddr.inaddr.family = AF_INET6 then localaddr := '::' else
\r
359 localaddr := '0.0.0.0';
\r
361 //gethostbyname(localaddr,host);
\r
362 inaddrtempsize := makeinaddrv(forwardlookup(localaddr,0),localport,inaddrtemp);
\r
364 isv6socket := (inaddrtemp.inaddr.family = AF_INET6);
\r
366 If Bind(fdhandlein,inaddrtempx,inaddrtempsize)<> {$ifdef mswindows}0{$else}true{$endif} Then begin
\r
368 lasterror := {$ifdef mswindows}getlasterror{$else}socketerror{$endif};
\r
369 raise ESocketException.create('unable to bind on address '+localaddr+'#'+localport+', error '+inttostr(lasterror));
\r
374 on e: exception do begin
\r
376 raise; //reraise the exception
\r
381 procedure tlsocket.listen;
\r
383 {$ifndef mswindows}
\r
388 origaddr:thostname;
\r
390 if state <> wsclosed then close;
\r
391 udp := uppercase(proto) = 'UDP';
\r
393 socktype := SOCK_DGRAM;
\r
395 end else socktype := SOCK_STREAM;
\r
398 if addr = '' then begin
\r
400 //writeln('ipv6 is defined');
\r
401 if not v4listendefault then begin
\r
402 //writeln('setting addr to ::');
\r
408 if isbiniplist(addr) then biniptemp := biniplist_get(addr,0) else biniptemp := forwardlookup(addr,10);
\r
409 addr := ipbintostr(biniptemp);
\r
410 //writeln('after ipbintostr call addr =',addr);
\r
411 //writeln('biniptemp.family =',biniptemp.family);
\r
412 //writeln('AF_INET6=',AF_INET6);
\r
413 //writeln('PF_INET6=',PF_INET6);
\r
414 //writeln('AF_INET=',AF_INET);
\r
415 //writeln('PF_INET=',PF_INET);
\r
417 fdhandlein := socket(biniptemp.family,socktype,0);
\r
419 if (addr = '::') and (origaddr = '') and (fdhandlein < 0) then begin
\r
420 {writeln('failed to create an IPV6 socket with error ',socketerror,'. trying to create an IPV4 one instead');}
\r
422 biniptemp := ipstrtobinf(addr);
\r
423 fdhandlein := socket(PF_INET,socktype,0);
\r
427 if fdhandlein = -1 then raise ESocketException.create('unable to create socket'{$ifdef mswindows}+' error='+inttostr(wsagetlasterror){$endif});
\r
428 dupnowatch(fdhandlein); // sets up maxs and copies handle to fdhandleout among other things
\r
429 //eventcore.setfdreverse(fdhandlein,self); //already taken care of by dup
\r
430 state := wsclosed; // then set this back as it was an undesired side effect of dup
\r
433 {$ifndef mswindows}
\r
434 yes := $01010101; {Copied this from existing code. Value is empiric,
\r
435 but works. (yes=true<>0) }
\r
438 if SetSocketOptions(fdhandlein, SOL_SOCKET, SO_REUSEADDR,yes,sizeof(yes))=-1 then begin
\r
439 raise ESocketException.create('unable to set SO_REUSEADDR socket option');
\r
441 //writeln('addr=',addr);
\r
442 //writeln('setting IPV6_V6ONLY option to 0');
\r
443 //allow v4 connections on v6 sockets
\r
444 if biniptemp.family = af_inet6 then begin
\r
445 if SetSocketOptions(fdhandlein, IPPROTO_IPV6,IPV6_V6ONLY,no,sizeof(no))=-1 then begin
\r
446 writeln(IPPROTO_IPV6);
\r
447 writeln(IPV6_V6ONLY);
\r
448 raise ESocketException.create('unable to set IPV6_V6ONLY socket option error='+inttostr(socketerror));
\r
453 SetSockOpt(fdhandlein, SOL_SOCKET, SO_REUSEADDR, 'TRUE', Length('TRUE'));
\r
460 if not udp then begin
\r
461 {!!! allow custom queue length? default 5}
\r
462 if listenqueue = 0 then listenqueue := 5;
\r
463 If {$ifdef mswindows}winsock.listen{$else}system_listen{$endif}(fdhandlein,listenqueue)<>{$ifdef mswindows}0{$else}true{$endif} Then raise
\r
464 esocketexception.create('unable to listen');
\r
465 state := wsListening;
\r
467 {$ifndef mswindows}
\r
468 SetSocketOptions(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));
\r
470 SetSockOpt(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));
\r
472 state := wsconnected;
\r
475 {$ifdef secondlistener}
\r
476 //listening on ::. try to listen on 0.0.0.0 as well for platforms which don't already do that
\r
477 if addr = '::' then begin
\r
478 secondlistener := tlsocket.create(nil);
\r
479 secondlistener.proto := proto;
\r
480 secondlistener.addr := '0.0.0.0';
\r
481 secondlistener.port := port;
\r
483 secondlistener.ondataavailable := secondaccepthandler;
\r
485 secondlistener.onsessionAvailable := secondaccepthandler;
\r
488 secondlistener.listen;
\r
490 secondlistener.destroy;
\r
491 secondlistener := nil;
\r
496 if state = wsclosed then begin
\r
497 if fdhandlein >= 0 then begin
\r
498 {one *can* get here without fd -beware}
\r
499 eventcore.rmasterclr(fdhandlein);
\r
500 myfdclose(fdhandlein); // we musnt leak file descriptors
\r
501 eventcore.setfdreverse(fdhandlein,nil);
\r
505 eventcore.rmasterset(fdhandlein,not udp);
\r
507 if fdhandleout >= 0 then eventcore.wmasterclr(fdhandleout);
\r
509 //writeln('listened on addr '+addr+':'+port+' '+proto+' using socket number ',fdhandlein);
\r
512 {$ifdef secondlistener}
\r
513 procedure tlsocket.internalclose(error:word);
\r
515 if assigned(secondlistener) then begin
\r
516 secondlistener.destroy;
\r
517 secondlistener := nil;
\r
519 inherited internalclose(error);
\r
522 procedure tlsocket.secondaccepthandler;
\r
524 lastsessionfromsecond := true;
\r
526 ondataavailable(self,error);
\r
528 if assigned(onsessionavailable) then onsessionavailable(self,error);
\r
533 function tlsocket.accept : longint;
\r
535 FromAddrSize : LongInt; // i don't really know what to do with these at this
\r
536 FromAddr : TInetSockAddrV; // at this point time will tell :)
\r
537 a,acceptlasterror:integer;
\r
539 {$ifdef secondlistener}
\r
540 if (lastsessionfromsecond) then begin
\r
541 lastsessionfromsecond := false;
\r
542 result := secondlistener.accept;
\r
547 FromAddrSize := Sizeof(FromAddr);
\r
549 result := winsock.accept(fdhandlein,@fromaddr,@fromaddrsize);
\r
551 result := system_accept(fdhandlein,fromaddr,fromaddrsize);
\r
554 if (result = -1) then acceptlasterror := {$ifdef mswindows}getlasterror{$else}socketerror{$endif} else acceptlasterror := 0;
\r
556 //now we have accepted one request start monitoring for more again
\r
557 eventcore.rmasterset(fdhandlein,true);
\r
559 if result = -1 then begin
\r
560 raise esocketexception.create('error '+inttostr(acceptlasterror)+' while accepting');
\r
562 if result > absolutemaxs then begin
\r
566 raise esocketexception.create('file descriptor out of range: '+inttostr(a));
\r
571 function tlsocket.sendto(dest:TInetSockAddrV;destlen:integer;data:pointer;len:integer):integer;
\r
574 realdest : tinetsockaddrv;
\r
575 biniptemp : tbinip;
\r
577 destx : {$ifdef mswindows}winsock.pSockAddr{$else}pInetSockAddrV{$endif};
\r
580 {$ifdef secondlistener}
\r
581 if assigned(secondlistener) then if (dest.inaddr.family = AF_INET) then begin
\r
582 result := secondlistener.sendto(dest,destlen,data,len);
\r
587 if isv6socket then begin
\r
588 biniptemp := inaddrvtobinip(dest);
\r
589 converttov6(biniptemp);
\r
590 destlen := makeinaddrv(biniptemp,inttostr(ntohs(dest.InAddr.port)),realdest);
\r
591 destx := {$ifdef mswindows}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@realdest)
\r
593 destx := {$ifdef mswindows}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@dest)
\r
596 destx := {$ifdef mswindows}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@dest);
\r
599 result := {$ifdef mswindows}winsock.sendto{$else}system_sendto{$endif}(self.fdhandleout,data^,len,0,destx^,destlen);
\r
603 function tlsocket.receivefrom(data:pointer;len:integer;var src:TInetSockAddrV;var srclen:integer):integer;
\r
605 tempsrc:TInetSockAddrV;
\r
606 tempsrclen:integer;
\r
607 srcx : {$ifdef mswindows}winsock.TSockAddr{$else}TInetSockAddrV{$endif} absolute tempsrc;
\r
610 {$ifdef secondlistener}
\r
611 if assigned(secondlistener) then if lastsessionfromsecond then begin
\r
612 lastsessionfromsecond := false;
\r
613 result := secondlistener.receivefrom(data,len,src,srclen);
\r
617 tempsrclen := sizeof(tempsrc);
\r
618 result := recvfrom(self.fdhandlein,data^,len,0,srcx,tempsrclen);
\r
621 biniptemp := inaddrvtobinip(tempsrc);
\r
622 if needconverttov4(biniptemp) then begin
\r
623 converttov4(biniptemp);
\r
624 tempsrclen := makeinaddrv(biniptemp,inttostr(ntohs(tempsrc.InAddr.port)),tempsrc);
\r
628 move(tempsrc,src,srclen);
\r
629 srclen := tempsrclen;
\r
632 procedure tlsocket.taskcallconnectionfailedhandler(wparam,lparam : longint);
\r
634 connectionfailedhandler(wparam);
\r
637 procedure tlsocket.connectionfailedhandler(error:word);
\r
639 if trymoreips then begin
\r
640 // writeln('failed with error ',error);
\r
641 connecttimeout.enabled := false;
\r
642 destroying := true;
\r
643 state := wsconnected;
\r
644 self.internalclose(0);
\r
645 destroying := false;
\r
648 state := wsconnected;
\r
649 if assigned(onsessionconnected) then onsessionconnected(self,error);
\r
650 self.internalclose(0);
\r
651 recvq.del(maxlongint);
\r
655 procedure tlsocket.connectsuccesshandler;
\r
657 trymoreips := false;
\r
658 connecttimeout.enabled := false;
\r
659 if assigned(onsessionconnected) then onsessionconnected(self,0);
\r
663 procedure tlsocket.handlefdtrigger(readtrigger,writetrigger:boolean);
\r
665 tempbuf:array[0..receivebufsize-1] of byte;
\r
667 // writeln('got a fd trigger, readtrigger=',readtrigger,' writetrigger=',writetrigger,' state=',integer(state));
\r
668 if (state =wslistening) and readtrigger then begin
\r
669 { debugout('listening socket triggered on read');}
\r
670 eventcore.rmasterclr(fdhandlein);
\r
671 if assigned(onsessionAvailable) then onsessionAvailable(self,0);
\r
673 if dgram and readtrigger then begin
\r
674 if assigned(ondataAvailable) then ondataAvailable(self,0);
\r
678 if (state =wsconnecting) and writetrigger then begin
\r
679 // code for dealing with the results of a non-blocking connect is
\r
681 // if just write is triggered it means connect succeeded
\r
682 // if both read and write are triggered it can mean 2 things
\r
683 // 1: connect ok and data available
\r
685 // to find out which you must read from the socket and look for errors
\r
686 // there if we read successfully we drop through into the code for firing
\r
688 if not readtrigger then begin
\r
689 state := wsconnected;
\r
690 connectsuccesshandler;
\r
692 numread := myfdread(fdhandlein,tempbuf,sizeof(tempbuf));
\r
693 if numread <> -1 then begin
\r
694 state := wsconnected;
\r
695 connectsuccesshandler;
\r
696 //connectread := true;
\r
697 recvq.add(@tempbuf,numread);
\r
699 connectionfailedhandler({$ifdef mswindows}wsagetlasterror{$else}linuxerror{$endif});
\r
702 // if things went well here we are now in the state wsconnected with data sitting in our receive buffer
\r
703 // so we drop down into the processing for data available
\r
705 if fdhandlein >= 0 then begin
\r
706 if state = wsconnected then begin
\r
707 eventcore.rmasterset(fdhandlein,false);
\r
709 eventcore.rmasterclr(fdhandlein);
\r
712 if fdhandleout >= 0 then begin
\r
713 if sendq.size = 0 then begin
\r
714 //don't clear the bit in fdswmaster if data is in the sendq
\r
715 eventcore.wmasterclr(fdhandleout);
\r
720 inherited handlefdtrigger(readtrigger,writetrigger);
\r
723 constructor tlsocket.Create(AOwner: TComponent);
\r
725 inherited create(aowner);
\r
726 closehandles := true;
\r
727 trymoreips := true;
\r
732 function tlsocket.getpeername(var addr:tsockaddrin;addrlen:integer):integer;
\r
734 addrx : {$ifdef mswindows}winsock.tsockaddr{$else}tsockaddrin{$endif} absolute addr;
\r
736 result := {$ifdef mswindows}winsock.getpeername{$else}system_getpeername{$endif}(self.fdhandlein,addrx,addrlen);
\r
739 procedure tlsocket.getxaddrbin(var binip:tbinip);
\r
741 addr:tinetsockaddrv;
\r
745 fillchar(addr,sizeof(addr),0);
\r
748 winsock.getsockname(self.fdhandlein,psockaddr(@addr)^,i);
\r
750 getsocketname(self.fdhandlein,addr,i);
\r
752 binip := inaddrvtobinip(addr);
\r
753 converttov4(binip);
\r
756 procedure tlsocket.getpeeraddrbin(var binip:tbinip);
\r
758 addr:tinetsockaddrv;
\r
762 fillchar(addr,sizeof(addr),0);
\r
764 winsock.getpeername(self.fdhandlein,psockaddr(@addr)^,i);
\r
766 system_getpeername(self.fdhandlein,addr,i);
\r
769 binip := inaddrvtobinip(addr);
\r
770 converttov4(binip);
\r
773 function tlsocket.getXaddr:thostname;
\r
777 getxaddrbin(biniptemp);
\r
778 result := ipbintostr(biniptemp);
\r
779 if result = '' then result := 'error';
\r
782 function tlsocket.getpeeraddr:thostname;
\r
786 getpeeraddrbin(biniptemp);
\r
787 result := ipbintostr(biniptemp);
\r
788 if result = '' then result := 'error';
\r
791 function tlsocket.getXport:ansistring;
\r
793 addr:tinetsockaddrv;
\r
798 winsock.getsockname(self.fdhandlein,psockaddrin(@addr)^,i);
\r
801 getsocketname(self.fdhandlein,addr,i);
\r
804 result := inttostr(htons(addr.InAddr.port));
\r
807 function tlsocket.getpeerport:ansistring;
\r
809 addr:tinetsockaddrv;
\r
814 winsock.getpeername(self.fdhandlein,psockaddrin(@addr)^,i);
\r
817 system_getpeername(self.fdhandlein,addr,i);
\r
820 result := inttostr(htons(addr.InAddr.port));
\r
824 procedure tlsocket.myfdclose(fd : integer);
\r
828 function tlsocket.myfdwrite(fd: LongInt;const buf;size: LongInt):LongInt;
\r
830 result := winsock.send(fd,(@buf)^,size,0);
\r
832 function tlsocket.myfdread(fd: LongInt;var buf;size: LongInt):LongInt;
\r
834 result := winsock.recv(fd,buf,size,0);
\r