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 internalcose, set fd's 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 debuggin 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
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 win32}wsagetlasterror{$else}socketerror{$endif},0);
\r
245 if dgram then begin
\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 win32}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 win32}0{$else}true{$endif} Then begin
\r
368 lasterror := {$ifdef win32}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
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 win32}+' 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
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
457 if not udp then begin
\r
458 {!!! allow custom queue length? default 5}
\r
459 if listenqueue = 0 then listenqueue := 5;
\r
460 If {$ifdef win32}winsock.listen{$else}system_listen{$endif}(fdhandlein,listenqueue)<>{$ifdef win32}0{$else}true{$endif} Then raise
\r
461 esocketexception.create('unable to listen');
\r
462 state := wsListening;
\r
465 SetSocketOptions(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));
\r
467 SetSockOpt(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));
\r
469 state := wsconnected;
\r
472 {$ifdef secondlistener}
\r
473 //listening on ::. try to listen on 0.0.0.0 as well for platforms which don't already do that
\r
474 if addr = '::' then begin
\r
475 secondlistener := tlsocket.create(nil);
\r
476 secondlistener.proto := proto;
\r
477 secondlistener.addr := '0.0.0.0';
\r
478 secondlistener.port := port;
\r
480 secondlistener.ondataavailable := secondaccepthandler;
\r
482 secondlistener.onsessionAvailable := secondaccepthandler;
\r
485 secondlistener.listen;
\r
487 secondlistener.destroy;
\r
488 secondlistener := nil;
\r
493 if state = wsclosed then begin
\r
494 if fdhandlein >= 0 then begin
\r
495 {one *can* get here without fd -beware}
\r
496 eventcore.rmasterclr(fdhandlein);
\r
497 myfdclose(fdhandlein); // we musnt leak file discriptors
\r
498 eventcore.setfdreverse(fdhandlein,nil);
\r
502 eventcore.rmasterset(fdhandlein,not udp);
\r
504 if fdhandleout >= 0 then eventcore.wmasterclr(fdhandleout);
\r
506 //writeln('listened on addr '+addr+':'+port+' '+proto+' using socket number ',fdhandlein);
\r
509 {$ifdef secondlistener}
\r
510 procedure tlsocket.internalclose(error:word);
\r
512 if assigned(secondlistener) then begin
\r
513 secondlistener.destroy;
\r
514 secondlistener := nil;
\r
516 inherited internalclose(error);
\r
519 procedure tlsocket.secondaccepthandler;
\r
521 lastsessionfromsecond := true;
\r
523 ondataavailable(self,error);
\r
525 if assigned(onsessionavailable) then onsessionavailable(self,error);
\r
530 function tlsocket.accept : longint;
\r
532 FromAddrSize : LongInt; // i don't realy know what to do with these at this
\r
533 FromAddr : TInetSockAddrV; // at this point time will tell :)
\r
536 {$ifdef secondlistener}
\r
537 if (lastsessionfromsecond) then begin
\r
538 lastsessionfromsecond := false;
\r
539 result := secondlistener.accept;
\r
544 FromAddrSize := Sizeof(FromAddr);
\r
546 result := winsock.accept(fdhandlein,@fromaddr,@fromaddrsize);
\r
548 result := system_accept(fdhandlein,fromaddr,fromaddrsize);
\r
550 //now we have accepted one request start monitoring for more again
\r
551 eventcore.rmasterset(fdhandlein,true);
\r
553 if result = -1 then begin
\r
554 raise esocketexception.create('error '+inttostr({$ifdef win32}getlasterror{$else}socketerror{$endif})+' while accepting');
\r
556 if result > absoloutemaxs then begin
\r
560 raise esocketexception.create('file discriptor out of range: '+inttostr(a));
\r
565 function tlsocket.sendto(dest:TInetSockAddrV;destlen:integer;data:pointer;len:integer):integer;
\r
568 realdest : tinetsockaddrv;
\r
569 biniptemp : tbinip;
\r
571 destx : {$ifdef win32}winsock.pSockAddr{$else}pInetSockAddrV{$endif};
\r
574 {$ifdef secondlistener}
\r
575 if assigned(secondlistener) then if (dest.inaddr.family = AF_INET) then begin
\r
576 result := secondlistener.sendto(dest,destlen,data,len);
\r
581 if isv6socket then begin
\r
582 biniptemp := inaddrvtobinip(dest);
\r
583 converttov6(biniptemp);
\r
584 destlen := makeinaddrv(biniptemp,inttostr(ntohs(dest.InAddr.port)),realdest);
\r
585 destx := {$ifdef win32}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@realdest)
\r
587 destx := {$ifdef win32}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@dest)
\r
590 destx := {$ifdef win32}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@dest);
\r
593 result := {$ifdef win32}winsock.sendto{$else}system_sendto{$endif}(self.fdhandleout,data^,len,0,destx^,destlen);
\r
597 function tlsocket.receivefrom(data:pointer;len:integer;var src:TInetSockAddrV;var srclen:integer):integer;
\r
599 tempsrc:TInetSockAddrV;
\r
600 tempsrclen:integer;
\r
601 srcx : {$ifdef win32}winsock.TSockAddr{$else}TInetSockAddrV{$endif} absolute tempsrc;
\r
604 {$ifdef secondlistener}
\r
605 if assigned(secondlistener) then if lastsessionfromsecond then begin
\r
606 lastsessionfromsecond := false;
\r
607 result := secondlistener.receivefrom(data,len,src,srclen);
\r
611 tempsrclen := sizeof(tempsrc);
\r
612 result := recvfrom(self.fdhandlein,data^,len,0,srcx,tempsrclen);
\r
615 biniptemp := inaddrvtobinip(tempsrc);
\r
616 if needconverttov4(biniptemp) then begin
\r
617 converttov4(biniptemp);
\r
618 tempsrclen := makeinaddrv(biniptemp,inttostr(ntohs(tempsrc.InAddr.port)),tempsrc);
\r
622 move(tempsrc,src,srclen);
\r
623 srclen := tempsrclen;
\r
626 procedure tlsocket.taskcallconnectionfailedhandler(wparam,lparam : longint);
\r
628 connectionfailedhandler(wparam);
\r
631 procedure tlsocket.connectionfailedhandler(error:word);
\r
633 if trymoreips then begin
\r
634 // writeln('failed with error ',error);
\r
635 connecttimeout.enabled := false;
\r
636 destroying := true;
\r
637 state := wsconnected;
\r
638 self.internalclose(0);
\r
639 destroying := false;
\r
642 state := wsconnected;
\r
643 if assigned(onsessionconnected) then onsessionconnected(self,error);
\r
644 self.internalclose(0);
\r
645 recvq.del(maxlongint);
\r
649 procedure tlsocket.connectsuccesshandler;
\r
651 trymoreips := false;
\r
652 connecttimeout.enabled := false;
\r
653 if assigned(onsessionconnected) then onsessionconnected(self,0);
\r
657 procedure tlsocket.handlefdtrigger(readtrigger,writetrigger:boolean);
\r
659 tempbuf:array[0..receivebufsize-1] of byte;
\r
661 // writeln('got a fd trigger, readtrigger=',readtrigger,' writetrigger=',writetrigger,' state=',integer(state));
\r
662 if (state =wslistening) and readtrigger then begin
\r
663 { debugout('listening socket triggered on read');}
\r
664 eventcore.rmasterclr(fdhandlein);
\r
665 if assigned(onsessionAvailable) then onsessionAvailable(self,0);
\r
667 if dgram and readtrigger then begin
\r
668 if assigned(ondataAvailable) then ondataAvailable(self,0);
\r
672 if (state =wsconnecting) and writetrigger then begin
\r
673 // code for dealing with the reults of a non-blocking connect is
\r
675 // if just write is triggered it means connect suceeded
\r
676 // if both read and write are triggered it can mean 2 things
\r
677 // 1: connect ok and data availible
\r
679 // to find out which you must read from the socket and look for errors
\r
680 // there if we read successfully we drop through into the code for fireing
\r
682 if not readtrigger then begin
\r
683 state := wsconnected;
\r
684 connectsuccesshandler;
\r
686 numread := myfdread(fdhandlein,tempbuf,sizeof(tempbuf));
\r
687 if numread <> -1 then begin
\r
688 state := wsconnected;
\r
689 connectsuccesshandler;
\r
690 //connectread := true;
\r
691 recvq.add(@tempbuf,numread);
\r
693 connectionfailedhandler({$ifdef win32}wsagetlasterror{$else}linuxerror{$endif});
\r
696 // if things went well here we are now in the state wsconnected with data sitting in our receive buffer
\r
697 // so we drop down into the processing for data availible
\r
699 if fdhandlein >= 0 then begin
\r
700 if state = wsconnected then begin
\r
701 eventcore.rmasterset(fdhandlein,false);
\r
703 eventcore.rmasterclr(fdhandlein);
\r
706 if fdhandleout >= 0 then begin
\r
707 if sendq.size = 0 then begin
\r
708 //don't clear the bit in fdswmaster if data is in the sendq
\r
709 eventcore.wmasterclr(fdhandleout);
\r
714 inherited handlefdtrigger(readtrigger,writetrigger);
\r
717 constructor tlsocket.Create(AOwner: TComponent);
\r
719 inherited create(aowner);
\r
720 closehandles := true;
\r
721 trymoreips := true;
\r
726 function tlsocket.getpeername(var addr:tsockaddrin;addrlen:integer):integer;
\r
728 addrx : {$ifdef win32}winsock.tsockaddr{$else}tsockaddrin{$endif} absolute addr;
\r
730 result := {$ifdef win32}winsock.getpeername{$else}system_getpeername{$endif}(self.fdhandlein,addrx,addrlen);
\r
733 procedure tlsocket.getxaddrbin(var binip:tbinip);
\r
735 addr:tinetsockaddrv;
\r
739 fillchar(addr,sizeof(addr),0);
\r
742 winsock.getsockname(self.fdhandlein,psockaddr(@addr)^,i);
\r
744 getsocketname(self.fdhandlein,addr,i);
\r
746 binip := inaddrvtobinip(addr);
\r
747 converttov4(binip);
\r
750 procedure tlsocket.getpeeraddrbin(var binip:tbinip);
\r
752 addr:tinetsockaddrv;
\r
756 fillchar(addr,sizeof(addr),0);
\r
758 winsock.getpeername(self.fdhandlein,psockaddr(@addr)^,i);
\r
760 system_getpeername(self.fdhandlein,addr,i);
\r
763 binip := inaddrvtobinip(addr);
\r
764 converttov4(binip);
\r
767 function tlsocket.getXaddr:thostname;
\r
771 getxaddrbin(biniptemp);
\r
772 result := ipbintostr(biniptemp);
\r
773 if result = '' then result := 'error';
\r
776 function tlsocket.getpeeraddr:thostname;
\r
780 getpeeraddrbin(biniptemp);
\r
781 result := ipbintostr(biniptemp);
\r
782 if result = '' then result := 'error';
\r
785 function tlsocket.getXport:ansistring;
\r
787 addr:tinetsockaddrv;
\r
792 winsock.getsockname(self.fdhandlein,psockaddrin(@addr)^,i);
\r
795 getsocketname(self.fdhandlein,addr,i);
\r
798 result := inttostr(htons(addr.InAddr.port));
\r
801 function tlsocket.getpeerport:ansistring;
\r
803 addr:tinetsockaddrv;
\r
808 winsock.getpeername(self.fdhandlein,psockaddrin(@addr)^,i);
\r
811 system_getpeername(self.fdhandlein,addr,i);
\r
814 result := inttostr(htons(addr.InAddr.port));
\r
818 procedure tlsocket.myfdclose(fd : integer);
\r
822 function tlsocket.myfdwrite(fd: LongInt;const buf;size: LongInt):LongInt;
\r
824 result := winsock.send(fd,(@buf)^,size,0);
\r
826 function tlsocket.myfdread(fd: LongInt;var buf;size: LongInt):LongInt;
\r
828 result := winsock.recv(fd,buf,size,0);
\r