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
139 isv6socket : boolean; //identifies if the socket is v6, set by bindsocket
\r
141 procedure taskcallconnectionfailedhandler(wparam,lparam : longint);
\r
143 procedure connecttimeouthandler(sender:tobject);
\r
144 procedure connectsuccesshandler;
\r
146 procedure myfdclose(fd : integer); override;
\r
147 function myfdwrite(fd: LongInt;const buf;size: LongInt):LongInt; override;
\r
148 function myfdread(fd: LongInt;var buf;size: LongInt):LongInt; override;
\r
151 tsocket=longint; // for compatibility with twsocket
\r
153 twsocket=tlsocket; {easy}
\r
161 {$include unixstuff.inc}
\r
164 function tlsocket.getaddrsize:integer;
\r
166 result := inaddrsize(inaddr);
\r
169 //I used to use the system versions of these from within lsocket (which has
\r
170 //functions whose name clashes with them) by using sockets.* and but I can't do
\r
171 //that anymore since in some cases connect is now provided by unixstuff.inc
\r
172 //hence these wrapper functions --plugwash
\r
174 function system_Connect(Sock: LongInt;const Addr;Addrlen: LongInt):Boolean;
\r
176 result := connect(sock,addr,addrlen);
\r
178 function system_SendTo(Sock: LongInt; const Buf;BufLen: LongInt;Flags: LongInt;var Addr;AddrLen: LongInt):LongInt;
\r
180 result := sendto(sock,buf,buflen,flags,addr,addrlen);
\r
182 function system_getpeername(Sock: LongInt;var Addr;var Addrlen: LongInt):LongInt;
\r
184 result := getpeername(sock,addr,addrlen);
\r
186 function system_listen(Sock: LongInt; MaxConnect: LongInt):Boolean;
\r
188 result := listen(sock,maxconnect);
\r
190 function system_Accept(Sock: LongInt;var Addr;var Addrlen: LongInt):LongInt;
\r
192 result := accept(sock,addr,addrlen);
\r
196 procedure tlsocket.realconnect;
\r
201 iptemp := biniplist_get(biniplist,currentip);
\r
202 //writeln('trying to connect to ',ipbintostr(biniplist_get(biniplist,currentip)),'#',port);
\r
203 if assigned(onconnecttryip) then onconnecttryip(self,iptemp);
\r
204 makeinaddrv(iptemp,port,inaddr);
\r
206 if (currentip >= biniplist_getcount(biniplist)) then trymoreips := false;
\r
209 if (uppercase(proto) = 'UDP') then begin
\r
214 end else if (uppercase(proto) = 'TCP') or (uppercase(proto) = '') then begin
\r
218 end else if (uppercase(proto) = 'ICMP') or (strtointdef(proto,256) < 256) then begin
\r
219 b := strtointdef(proto,IPPROTO_ICMP);
\r
223 raise ESocketException.create('unrecognised protocol');
\r
226 a := Socket(inaddr.inaddr.family,a,b);
\r
227 //writeln(ord(inaddr.inaddr.family));
\r
228 if a = -1 then begin
\r
229 //unable to create socket, fire an error event (better to use an error event
\r
230 //to avoid poor interaction with multilistener stuff.
\r
231 //a socket value of -2 is a special value to say there is no socket but
\r
232 //we want internalclose to act as if there was
\r
235 tltask.create(taskcallconnectionfailedhandler,self,{$ifdef win32}wsagetlasterror{$else}socketerror{$endif},0);
\r
241 if dgram then begin
\r
243 SetSocketOptions(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));
\r
245 SetSockOpt(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));
\r
247 state := wsconnected;
\r
248 if assigned(onsessionconnected) then onsessionconnected(self,0);
\r
250 eventcore.rmasterset(fdhandlein,false);
\r
251 eventcore.wmasterclr(fdhandleout);
\r
253 state :=wsconnecting;
\r
255 //writeln(inaddr.inaddr.port);
\r
256 winsock.Connect(fdhandlein,winsock.psockaddr(@inADDR)^,getaddrsize);
\r
258 system_Connect(fdhandlein,inADDR,getaddrsize);
\r
260 eventcore.rmasterset(fdhandlein,false);
\r
261 eventcore.wmasterset(fdhandleout);
\r
262 if trymoreips then connecttimeout.enabled := true;
\r
266 on e: exception do begin
\r
268 raise; //reraise the exception
\r
274 procedure tlsocket.connecttimeouthandler(sender:tobject);
\r
276 connecttimeout.enabled := false;
\r
277 destroying := true; //hack to not cause handler to trigger
\r
279 destroying := false;
\r
286 procedure tlsocket.connect;
\r
291 if state <> wsclosed then close;
\r
293 if isbiniplist(addr) then biniplist := addr else biniplist := forwardlookuplist(addr,0);
\r
294 if biniplist_getcount(biniplist) = 0 then raise exception.create('unable to resolve '+addr);
\r
296 //makeinaddrv(addr,port,inaddr);
\r
299 if not assigned(connecttimeout) then begin
\r
300 connecttimeout := tltimer.create(self);
\r
301 connecttimeout.ontimer := connecttimeouthandler;
\r
302 connecttimeout.interval := 5000;
\r
303 connecttimeout.enabled := false;
\r
308 procedure tlsocket.sendstr(const str : tbufferstring);
\r
310 if dgram then begin
\r
311 send(@str[1],length(str))
\r
313 inherited sendstr(str);
\r
317 function tlsocket.send(data:pointer;len:integer):integer;
\r
319 if dgram then begin
\r
320 // writeln('sending to '+ipbintostr(inaddrvtobinip(inaddr)),' ',htons(inaddr.inaddr.port),' ',len,' bytes');
\r
321 result := sendto(inaddr,getaddrsize,data,len);
\r
323 // writeln('send result ',result);
\r
324 // writeln('errno',errno);
\r
326 result := inherited send(data,len);
\r
331 function tlsocket.receive(Buf:Pointer;BufSize:integer):integer;
\r
333 if dgram then begin
\r
334 {$ifdef secondlistener}
\r
335 if lastsessionfromsecond then begin
\r
336 result := secondlistener.receive(buf,bufsize);
\r
337 lastsessionfromsecond := false;
\r
340 result := myfdread(self.fdhandlein,buf^,bufsize);
\r
342 result := inherited receive(buf,bufsize);
\r
346 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
386 origaddr:thostname;
\r
388 if state <> wsclosed then close;
\r
389 udp := uppercase(proto) = 'UDP';
\r
391 socktype := SOCK_DGRAM;
\r
393 end else socktype := SOCK_STREAM;
\r
396 if addr = '' then begin
\r
398 //writeln('ipv6 is defined');
\r
399 if not v4listendefault then begin
\r
400 //writeln('setting addr to ::');
\r
406 if isbiniplist(addr) then biniptemp := biniplist_get(addr,0) else biniptemp := forwardlookup(addr,10);
\r
407 addr := ipbintostr(biniptemp);
\r
408 //writeln('after ipbintostr call addr =',addr);
\r
409 //writeln('biniptemp.family =',biniptemp.family);
\r
410 //writeln('AF_INET6=',AF_INET6);
\r
411 //writeln('PF_INET6=',PF_INET6);
\r
412 //writeln('AF_INET=',AF_INET);
\r
413 //writeln('PF_INET=',PF_INET);
\r
415 fdhandlein := socket(biniptemp.family,socktype,0);
\r
417 if (addr = '::') and (origaddr = '') and (fdhandlein < 0) then begin
\r
418 {writeln('failed to create an IPV6 socket with error ',socketerror,'. trying to create an IPV4 one instead');}
\r
420 fdhandlein := socket(PF_INET,socktype,0);
\r
424 if fdhandlein = -1 then raise ESocketException.create('unable to create socket'{$ifdef win32}+' error='+inttostr(wsagetlasterror){$endif});
\r
425 dupnowatch(fdhandlein); // sets up maxs and copies handle to fdhandleout among other things
\r
426 //eventcore.setfdreverse(fdhandlein,self); //already taken care of by dup
\r
427 state := wsclosed; // then set this back as it was an undesired side effect of dup
\r
430 yes := $01010101; {Copied this from existing code. Value is empiric,
\r
431 but works. (yes=true<>0) }
\r
434 if SetSocketOptions(fdhandlein, SOL_SOCKET, SO_REUSEADDR,yes,sizeof(yes))=-1 then begin
\r
435 raise ESocketException.create('unable to set SO_REUSEADDR socket option');
\r
437 //writeln('addr=',addr);
\r
438 //writeln('setting IPV6_V6ONLY option to 0');
\r
439 //allow v4 connections on v6 sockets
\r
440 if biniptemp.family = af_inet6 then begin
\r
441 if SetSocketOptions(fdhandlein, IPPROTO_IPV6,IPV6_V6ONLY,no,sizeof(no))=-1 then begin
\r
442 writeln(IPPROTO_IPV6);
\r
443 writeln(IPV6_V6ONLY);
\r
444 raise ESocketException.create('unable to set IPV6_V6ONLY socket option error='+inttostr(socketerror));
\r
453 if not udp then begin
\r
454 {!!! allow custom queue length? default 5}
\r
455 if listenqueue = 0 then listenqueue := 5;
\r
456 If {$ifdef win32}winsock.listen{$else}system_listen{$endif}(fdhandlein,listenqueue)<>{$ifdef win32}0{$else}true{$endif} Then raise
\r
457 esocketexception.create('unable to listen');
\r
458 state := wsListening;
\r
461 SetSocketOptions(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));
\r
463 SetSockOpt(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));
\r
465 state := wsconnected;
\r
468 {$ifdef secondlistener}
\r
469 //listening on ::. try to listen on 0.0.0.0 as well for platforms which don't already do that
\r
470 if addr = '::' then begin
\r
471 secondlistener := tlsocket.create(nil);
\r
472 secondlistener.proto := proto;
\r
473 secondlistener.addr := '0.0.0.0';
\r
474 secondlistener.port := port;
\r
476 secondlistener.ondataavailable := secondaccepthandler;
\r
478 secondlistener.onsessionAvailable := secondaccepthandler;
\r
481 secondlistener.listen;
\r
483 secondlistener.destroy;
\r
484 secondlistener := nil;
\r
489 if state = wsclosed then begin
\r
490 if fdhandlein >= 0 then begin
\r
491 {one *can* get here without fd -beware}
\r
492 eventcore.rmasterclr(fdhandlein);
\r
493 myfdclose(fdhandlein); // we musnt leak file discriptors
\r
494 eventcore.setfdreverse(fdhandlein,nil);
\r
498 eventcore.rmasterset(fdhandlein,not udp);
\r
500 if fdhandleout >= 0 then eventcore.wmasterclr(fdhandleout);
\r
502 //writeln('listened on addr '+addr+':'+port+' '+proto+' using socket number ',fdhandlein);
\r
505 {$ifdef secondlistener}
\r
506 procedure tlsocket.internalclose(error:word);
\r
508 if assigned(secondlistener) then begin
\r
509 secondlistener.destroy;
\r
510 secondlistener := nil;
\r
512 inherited internalclose(error);
\r
515 procedure tlsocket.secondaccepthandler;
\r
517 lastsessionfromsecond := true;
\r
519 ondataavailable(self,error);
\r
521 if assigned(onsessionavailable) then onsessionavailable(self,error);
\r
526 function tlsocket.accept : longint;
\r
528 FromAddrSize : LongInt; // i don't realy know what to do with these at this
\r
529 FromAddr : TInetSockAddrV; // at this point time will tell :)
\r
532 {$ifdef secondlistener}
\r
533 if (lastsessionfromsecond) then begin
\r
534 lastsessionfromsecond := false;
\r
535 result := secondlistener.accept;
\r
540 FromAddrSize := Sizeof(FromAddr);
\r
542 result := winsock.accept(fdhandlein,@fromaddr,@fromaddrsize);
\r
544 result := system_accept(fdhandlein,fromaddr,fromaddrsize);
\r
546 //now we have accepted one request start monitoring for more again
\r
547 eventcore.rmasterset(fdhandlein,true);
\r
549 if result = -1 then begin
\r
550 raise esocketexception.create('error '+inttostr({$ifdef win32}getlasterror{$else}socketerror{$endif})+' while accepting');
\r
552 if result > absoloutemaxs then begin
\r
556 raise esocketexception.create('file discriptor out of range: '+inttostr(a));
\r
561 function tlsocket.sendto(dest:TInetSockAddrV;destlen:integer;data:pointer;len:integer):integer;
\r
564 realdest : tinetsockaddrv;
\r
565 biniptemp : tbinip;
\r
567 destx : {$ifdef win32}winsock.pSockAddr{$else}pInetSockAddrV{$endif};
\r
570 {$ifdef secondlistener}
\r
571 if assigned(secondlistener) then if (dest.inaddr.family = AF_INET) then begin
\r
572 result := secondlistener.sendto(dest,destlen,data,len);
\r
577 if isv6socket then begin
\r
578 biniptemp := inaddrvtobinip(dest);
\r
579 converttov6(biniptemp);
\r
580 destlen := makeinaddrv(biniptemp,inttostr(ntohs(dest.InAddr.port)),realdest);
\r
581 destx := {$ifdef win32}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@realdest)
\r
583 destx := {$ifdef win32}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@dest)
\r
586 destx := {$ifdef win32}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@dest)
\r
589 result := {$ifdef win32}winsock.sendto{$else}system_sendto{$endif}(self.fdhandleout,data^,len,0,destx^,destlen);
\r
593 function tlsocket.receivefrom(data:pointer;len:integer;var src:TInetSockAddrV;var srclen:integer):integer;
\r
595 tempsrc:TInetSockAddrV;
\r
596 tempsrclen:integer;
\r
597 srcx : {$ifdef win32}winsock.TSockAddr{$else}TInetSockAddrV{$endif} absolute tempsrc;
\r
600 {$ifdef secondlistener}
\r
601 if assigned(secondlistener) then if lastsessionfromsecond then begin
\r
602 lastsessionfromsecond := false;
\r
603 result := secondlistener.receivefrom(data,len,src,srclen);
\r
607 tempsrclen := sizeof(tempsrc);
\r
608 result := recvfrom(self.fdhandlein,data^,len,0,srcx,tempsrclen);
\r
611 biniptemp := inaddrvtobinip(tempsrc);
\r
612 if needconverttov4(biniptemp) then begin
\r
613 converttov4(biniptemp);
\r
614 tempsrclen := makeinaddrv(biniptemp,inttostr(ntohs(tempsrc.InAddr.port)),tempsrc);
\r
618 move(tempsrc,src,srclen);
\r
619 srclen := tempsrclen;
\r
622 procedure tlsocket.taskcallconnectionfailedhandler(wparam,lparam : longint);
\r
624 connectionfailedhandler(wparam);
\r
627 procedure tlsocket.connectionfailedhandler(error:word);
\r
629 if trymoreips then begin
\r
630 // writeln('failed with error ',error);
\r
631 connecttimeout.enabled := false;
\r
632 destroying := true;
\r
633 state := wsconnected;
\r
634 self.internalclose(0);
\r
635 destroying := false;
\r
638 state := wsconnected;
\r
639 if assigned(onsessionconnected) then onsessionconnected(self,error);
\r
640 self.internalclose(0);
\r
641 recvq.del(maxlongint);
\r
645 procedure tlsocket.connectsuccesshandler;
\r
647 trymoreips := false;
\r
648 connecttimeout.enabled := false;
\r
649 if assigned(onsessionconnected) then onsessionconnected(self,0);
\r
653 procedure tlsocket.handlefdtrigger(readtrigger,writetrigger:boolean);
\r
655 tempbuf:array[0..receivebufsize-1] of byte;
\r
657 // writeln('got a fd trigger, readtrigger=',readtrigger,' writetrigger=',writetrigger,' state=',integer(state));
\r
658 if (state =wslistening) and readtrigger then begin
\r
659 { debugout('listening socket triggered on read');}
\r
660 eventcore.rmasterclr(fdhandlein);
\r
661 if assigned(onsessionAvailable) then onsessionAvailable(self,0);
\r
663 if dgram and readtrigger then begin
\r
664 if assigned(ondataAvailable) then ondataAvailable(self,0);
\r
668 if (state =wsconnecting) and writetrigger then begin
\r
669 // code for dealing with the reults of a non-blocking connect is
\r
671 // if just write is triggered it means connect suceeded
\r
672 // if both read and write are triggered it can mean 2 things
\r
673 // 1: connect ok and data availible
\r
675 // to find out which you must read from the socket and look for errors
\r
676 // there if we read successfully we drop through into the code for fireing
\r
678 if not readtrigger then begin
\r
679 state := wsconnected;
\r
680 connectsuccesshandler;
\r
682 numread := myfdread(fdhandlein,tempbuf,sizeof(tempbuf));
\r
683 if numread <> -1 then begin
\r
684 state := wsconnected;
\r
685 connectsuccesshandler;
\r
686 //connectread := true;
\r
687 recvq.add(@tempbuf,numread);
\r
689 connectionfailedhandler({$ifdef win32}wsagetlasterror{$else}linuxerror{$endif});
\r
692 // if things went well here we are now in the state wsconnected with data sitting in our receive buffer
\r
693 // so we drop down into the processing for data availible
\r
695 if fdhandlein >= 0 then begin
\r
696 if state = wsconnected then begin
\r
697 eventcore.rmasterset(fdhandlein,false);
\r
699 eventcore.rmasterclr(fdhandlein);
\r
702 if fdhandleout >= 0 then begin
\r
703 if sendq.size = 0 then begin
\r
704 //don't clear the bit in fdswmaster if data is in the sendq
\r
705 eventcore.wmasterclr(fdhandleout);
\r
710 inherited handlefdtrigger(readtrigger,writetrigger);
\r
713 constructor tlsocket.Create(AOwner: TComponent);
\r
715 inherited create(aowner);
\r
716 closehandles := true;
\r
717 trymoreips := true;
\r
722 function tlsocket.getpeername(var addr:tsockaddrin;addrlen:integer):integer;
\r
724 addrx : {$ifdef win32}winsock.tsockaddr{$else}tsockaddrin{$endif} absolute addr;
\r
726 result := {$ifdef win32}winsock.getpeername{$else}system_getpeername{$endif}(self.fdhandlein,addrx,addrlen);
\r
729 procedure tlsocket.getxaddrbin(var binip:tbinip);
\r
731 addr:tinetsockaddrv;
\r
735 fillchar(addr,sizeof(addr),0);
\r
738 winsock.getsockname(self.fdhandlein,psockaddr(@addr)^,i);
\r
740 getsocketname(self.fdhandlein,addr,i);
\r
742 binip := inaddrvtobinip(addr);
\r
743 converttov4(binip);
\r
746 procedure tlsocket.getpeeraddrbin(var binip:tbinip);
\r
748 addr:tinetsockaddrv;
\r
752 fillchar(addr,sizeof(addr),0);
\r
754 winsock.getpeername(self.fdhandlein,psockaddr(@addr)^,i);
\r
756 system_getpeername(self.fdhandlein,addr,i);
\r
759 binip := inaddrvtobinip(addr);
\r
760 converttov4(binip);
\r
763 function tlsocket.getXaddr:thostname;
\r
767 getxaddrbin(biniptemp);
\r
768 result := ipbintostr(biniptemp);
\r
769 if result = '' then result := 'error';
\r
772 function tlsocket.getpeeraddr:thostname;
\r
776 getpeeraddrbin(biniptemp);
\r
777 result := ipbintostr(biniptemp);
\r
778 if result = '' then result := 'error';
\r
781 function tlsocket.getXport:ansistring;
\r
783 addr:tinetsockaddrv;
\r
788 winsock.getsockname(self.fdhandlein,psockaddrin(@addr)^,i);
\r
791 getsocketname(self.fdhandlein,addr,i);
\r
794 result := inttostr(htons(addr.InAddr.port));
\r
797 function tlsocket.getpeerport:ansistring;
\r
799 addr:tinetsockaddrv;
\r
804 winsock.getpeername(self.fdhandlein,psockaddrin(@addr)^,i);
\r
807 system_getpeername(self.fdhandlein,addr,i);
\r
810 result := inttostr(htons(addr.InAddr.port));
\r
814 procedure tlsocket.myfdclose(fd : integer);
\r
818 function tlsocket.myfdwrite(fd: LongInt;const buf;size: LongInt):LongInt;
\r
820 result := winsock.send(fd,(@buf)^,size,0);
\r
822 function tlsocket.myfdread(fd: LongInt;var buf;size: LongInt):LongInt;
\r
824 result := winsock.recv(fd,buf,size,0);
\r