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
104 {$ifdef secondlistener}
\r
105 secondlistener:tlsocket;
\r
106 lastsessionfromsecond:boolean;
\r
107 procedure secondaccepthandler(sender:tobject;error:word);
\r
108 procedure internalclose(error:word);override;
\r
110 function getaddrsize:integer;
\r
111 procedure connect; virtual;
\r
112 procedure realconnect;
\r
113 procedure bindsocket;
\r
115 function accept : longint;
\r
116 function sendto(dest:TInetSockAddrV;destlen:integer;data:pointer;len:integer):integer; virtual;
\r
117 function receivefrom(data:pointer;len:integer;var src:TInetSockAddrV;var srclen:integer):integer; virtual;
\r
119 procedure handlefdtrigger(readtrigger,writetrigger:boolean); override;
\r
120 function send(data:pointer;len:integer):integer;override;
\r
121 procedure sendstr(const str : tbufferstring);override;
\r
122 function Receive(Buf:Pointer;BufSize:integer):integer; override;
\r
123 function getpeername(var addr:tsockaddrin;addrlen:integer):integer; virtual;
\r
124 procedure getXaddrbin(var binip:tbinip); virtual;
\r
125 procedure getpeeraddrbin(var binip:tbinip); virtual;
\r
126 function getXaddr:thostname; virtual;
\r
127 function getpeeraddr:thostname; virtual;
\r
128 function getXport:ansistring; virtual;
\r
129 function getpeerport:ansistring; virtual;
\r
130 constructor Create(AOwner: TComponent); override;
\r
132 //this one has to be kept public for now because lcorewsaasyncselect calls it
\r
133 procedure connectionfailedhandler(error:word);
\r
136 isv6socket : boolean; //identifies if the socket is v6, set by bindsocket
\r
138 procedure taskcallconnectionfailedhandler(wparam,lparam : longint);
\r
140 procedure connecttimeouthandler(sender:tobject);
\r
141 procedure connectsuccesshandler;
\r
143 procedure myfdclose(fd : integer); override;
\r
144 function myfdwrite(fd: LongInt;const buf;size: LongInt):LongInt; override;
\r
145 function myfdread(fd: LongInt;var buf;size: LongInt):LongInt; override;
\r
148 tsocket=longint; // for compatibility with twsocket
\r
150 twsocket=tlsocket; {easy}
\r
158 {$include unixstuff.inc}
\r
161 function tlsocket.getaddrsize:integer;
\r
163 result := inaddrsize(inaddr);
\r
166 //I used to use the system versions of these from within lsocket (which has
\r
167 //functions whose name clashes with them) by using sockets.* and but I can't do
\r
168 //that anymore since in some cases connect is now provided by unixstuff.inc
\r
169 //hence these wrapper functions --plugwash
\r
171 function system_Connect(Sock: LongInt;const Addr;Addrlen: LongInt):Boolean;
\r
173 result := connect(sock,addr,addrlen);
\r
175 function system_SendTo(Sock: LongInt; const Buf;BufLen: LongInt;Flags: LongInt;var Addr;AddrLen: LongInt):LongInt;
\r
177 result := sendto(sock,buf,buflen,flags,addr,addrlen);
\r
179 function system_getpeername(Sock: LongInt;var Addr;var Addrlen: LongInt):LongInt;
\r
181 result := getpeername(sock,addr,addrlen);
\r
183 function system_listen(Sock: LongInt; MaxConnect: LongInt):Boolean;
\r
185 result := listen(sock,maxconnect);
\r
187 function system_Accept(Sock: LongInt;var Addr;var Addrlen: LongInt):LongInt;
\r
189 result := accept(sock,addr,addrlen);
\r
193 procedure tlsocket.realconnect;
\r
197 //writeln('trying to connect to ',ipbintostr(biniplist_get(biniplist,currentip)),'#',port);
\r
198 makeinaddrv(biniplist_get(biniplist,currentip),port,inaddr);
\r
200 if (currentip >= biniplist_getcount(biniplist)) then trymoreips := false;
\r
203 if (uppercase(proto) = 'UDP') then begin
\r
208 end else if (uppercase(proto) = 'TCP') or (uppercase(proto) = '') then begin
\r
212 end else if (uppercase(proto) = 'ICMP') or (strtointdef(proto,256) < 256) then begin
\r
213 b := strtointdef(proto,IPPROTO_ICMP);
\r
217 raise ESocketException.create('unrecognised protocol');
\r
220 a := Socket(inaddr.inaddr.family,a,b);
\r
221 //writeln(ord(inaddr.inaddr.family));
\r
222 if a = -1 then begin
\r
223 //unable to create socket, fire an error event (better to use an error event
\r
224 //to avoid poor interaction with multilistener stuff.
\r
225 //a socket value of -2 is a special value to say there is no socket but
\r
226 //we want internalclose to act as if there was
\r
229 tltask.create(taskcallconnectionfailedhandler,self,{$ifdef win32}wsagetlasterror{$else}socketerror{$endif},0);
\r
235 if dgram then begin
\r
237 SetSocketOptions(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));
\r
239 SetSockOpt(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));
\r
241 state := wsconnected;
\r
242 if assigned(onsessionconnected) then onsessionconnected(self,0);
\r
244 eventcore.rmasterset(fdhandlein,false);
\r
245 eventcore.wmasterclr(fdhandleout);
\r
247 state :=wsconnecting;
\r
249 //writeln(inaddr.inaddr.port);
\r
250 winsock.Connect(fdhandlein,winsock.psockaddr(@inADDR)^,getaddrsize);
\r
252 system_Connect(fdhandlein,inADDR,getaddrsize);
\r
254 eventcore.rmasterset(fdhandlein,false);
\r
255 eventcore.wmasterset(fdhandleout);
\r
256 if trymoreips then connecttimeout.enabled := true;
\r
260 on e: exception do begin
\r
262 raise; //reraise the exception
\r
268 procedure tlsocket.connecttimeouthandler(sender:tobject);
\r
270 connecttimeout.enabled := false;
\r
271 destroying := true; //hack to not cause handler to trigger
\r
273 destroying := false;
\r
280 procedure tlsocket.connect;
\r
285 if state <> wsclosed then close;
\r
287 if isbiniplist(addr) then biniplist := addr else biniplist := forwardlookuplist(addr,0);
\r
288 if biniplist_getcount(biniplist) = 0 then raise exception.create('unable to resolve '+addr);
\r
290 //makeinaddrv(addr,port,inaddr);
\r
293 if not assigned(connecttimeout) then begin
\r
294 connecttimeout := tltimer.create(self);
\r
295 connecttimeout.ontimer := connecttimeouthandler;
\r
296 connecttimeout.interval := 2500;
\r
297 connecttimeout.enabled := false;
\r
302 procedure tlsocket.sendstr(const str : tbufferstring);
\r
304 if dgram then begin
\r
305 send(@str[1],length(str))
\r
307 inherited sendstr(str);
\r
311 function tlsocket.send(data:pointer;len:integer):integer;
\r
313 if dgram then begin
\r
314 // writeln('sending to '+ipbintostr(inaddrvtobinip(inaddr)),' ',htons(inaddr.inaddr.port),' ',len,' bytes');
\r
315 result := sendto(inaddr,getaddrsize,data,len);
\r
317 // writeln('send result ',result);
\r
318 // writeln('errno',errno);
\r
320 result := inherited send(data,len);
\r
325 function tlsocket.receive(Buf:Pointer;BufSize:integer):integer;
\r
327 if dgram then begin
\r
328 {$ifdef secondlistener}
\r
329 if lastsessionfromsecond then begin
\r
330 result := secondlistener.receive(buf,bufsize);
\r
331 lastsessionfromsecond := false;
\r
334 result := myfdread(self.fdhandlein,buf^,bufsize);
\r
336 result := inherited receive(buf,bufsize);
\r
340 procedure tlsocket.bindsocket;
\r
343 inAddrtemp:TInetSockAddrV;
\r
344 inAddrtempx:{$ifdef win32}winsock.TSockaddr{$else}TInetSockAddrV{$endif} absolute inaddrtemp;
\r
345 inaddrtempsize:integer;
\r
348 if (localaddr <> '') or (localport <> '') then begin
\r
349 if localaddr = '' then begin
\r
351 if inaddr.inaddr.family = AF_INET6 then localaddr := '::' else
\r
353 localaddr := '0.0.0.0';
\r
355 //gethostbyname(localaddr,host);
\r
356 inaddrtempsize := makeinaddrv(forwardlookup(localaddr,0),localport,inaddrtemp);
\r
358 isv6socket := (inaddrtemp.inaddr.family = AF_INET6);
\r
360 If Bind(fdhandlein,inaddrtempx,inaddrtempsize)<> {$ifdef win32}0{$else}true{$endif} Then begin
\r
362 lasterror := {$ifdef win32}getlasterror{$else}socketerror{$endif};
\r
363 raise ESocketException.create('unable to bind on address '+localaddr+'#'+localport+', error '+inttostr(lasterror));
\r
368 on e: exception do begin
\r
370 raise; //reraise the exception
\r
375 procedure tlsocket.listen;
\r
380 origaddr:thostname;
\r
382 if state <> wsclosed then close;
\r
383 udp := uppercase(proto) = 'UDP';
\r
385 socktype := SOCK_DGRAM;
\r
387 end else socktype := SOCK_STREAM;
\r
390 if addr = '' then begin
\r
392 //writeln('ipv6 is defined');
\r
393 if not v4listendefault then begin
\r
394 //writeln('setting addr to ::');
\r
400 if isbiniplist(addr) then biniptemp := biniplist_get(addr,0) else biniptemp := forwardlookup(addr,10);
\r
401 addr := ipbintostr(biniptemp);
\r
402 //writeln('after ipbintostr call addr =',addr);
\r
403 //writeln('biniptemp.family =',biniptemp.family);
\r
404 //writeln('AF_INET6=',AF_INET6);
\r
405 //writeln('PF_INET6=',PF_INET6);
\r
406 //writeln('AF_INET=',AF_INET);
\r
407 //writeln('PF_INET=',PF_INET);
\r
409 fdhandlein := socket(biniptemp.family,socktype,0);
\r
411 if (addr = '::') and (origaddr = '') and (fdhandlein < 0) then begin
\r
412 {writeln('failed to create an IPV6 socket with error ',socketerror,'. trying to create an IPV4 one instead');}
\r
414 fdhandlein := socket(PF_INET,socktype,0);
\r
418 if fdhandlein = -1 then raise ESocketException.create('unable to create socket'{$ifdef win32}+' error='+inttostr(wsagetlasterror){$endif});
\r
419 dupnowatch(fdhandlein); // sets up maxs and copies handle to fdhandleout among other things
\r
420 //eventcore.setfdreverse(fdhandlein,self); //already taken care of by dup
\r
421 state := wsclosed; // then set this back as it was an undesired side effect of dup
\r
424 yes := $01010101; {Copied this from existing code. Value is empiric,
\r
425 but works. (yes=true<>0) }
\r
428 if SetSocketOptions(fdhandlein, SOL_SOCKET, SO_REUSEADDR,yes,sizeof(yes))=-1 then begin
\r
429 raise ESocketException.create('unable to set SO_REUSEADDR socket option');
\r
431 //writeln('addr=',addr);
\r
432 //writeln('setting IPV6_V6ONLY option to 0');
\r
433 //allow v4 connections on v6 sockets
\r
434 if biniptemp.family = af_inet6 then begin
\r
435 if SetSocketOptions(fdhandlein, IPPROTO_IPV6,IPV6_V6ONLY,no,sizeof(no))=-1 then begin
\r
436 writeln(IPPROTO_IPV6);
\r
437 writeln(IPV6_V6ONLY);
\r
438 raise ESocketException.create('unable to set IPV6_V6ONLY socket option error='+inttostr(socketerror));
\r
447 if not udp then begin
\r
448 {!!! allow custom queue length? default 5}
\r
449 if listenqueue = 0 then listenqueue := 5;
\r
450 If {$ifdef win32}winsock.listen{$else}system_listen{$endif}(fdhandlein,listenqueue)<>{$ifdef win32}0{$else}true{$endif} Then raise
\r
451 esocketexception.create('unable to listen');
\r
452 state := wsListening;
\r
455 SetSocketOptions(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));
\r
457 SetSockOpt(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));
\r
459 state := wsconnected;
\r
462 {$ifdef secondlistener}
\r
463 //listening on ::. try to listen on 0.0.0.0 as well for platforms which don't already do that
\r
464 if addr = '::' then begin
\r
465 secondlistener := tlsocket.create(nil);
\r
466 secondlistener.proto := proto;
\r
467 secondlistener.addr := '0.0.0.0';
\r
468 secondlistener.port := port;
\r
470 secondlistener.ondataavailable := secondaccepthandler;
\r
472 secondlistener.onsessionAvailable := secondaccepthandler;
\r
475 secondlistener.listen;
\r
477 secondlistener.destroy;
\r
478 secondlistener := nil;
\r
483 if state = wsclosed then begin
\r
484 if fdhandlein >= 0 then begin
\r
485 {one *can* get here without fd -beware}
\r
486 eventcore.rmasterclr(fdhandlein);
\r
487 myfdclose(fdhandlein); // we musnt leak file discriptors
\r
488 eventcore.setfdreverse(fdhandlein,nil);
\r
492 eventcore.rmasterset(fdhandlein,not udp);
\r
494 if fdhandleout >= 0 then eventcore.wmasterclr(fdhandleout);
\r
496 //writeln('listened on addr '+addr+':'+port+' '+proto+' using socket number ',fdhandlein);
\r
499 {$ifdef secondlistener}
\r
500 procedure tlsocket.internalclose(error:word);
\r
502 if assigned(secondlistener) then begin
\r
503 secondlistener.destroy;
\r
504 secondlistener := nil;
\r
506 inherited internalclose(error);
\r
509 procedure tlsocket.secondaccepthandler;
\r
511 lastsessionfromsecond := true;
\r
513 ondataavailable(self,error);
\r
515 if assigned(onsessionavailable) then onsessionavailable(self,error);
\r
520 function tlsocket.accept : longint;
\r
522 FromAddrSize : LongInt; // i don't realy know what to do with these at this
\r
523 FromAddr : TInetSockAddrV; // at this point time will tell :)
\r
526 {$ifdef secondlistener}
\r
527 if (lastsessionfromsecond) then begin
\r
528 lastsessionfromsecond := false;
\r
529 result := secondlistener.accept;
\r
534 FromAddrSize := Sizeof(FromAddr);
\r
536 result := winsock.accept(fdhandlein,@fromaddr,@fromaddrsize);
\r
538 result := system_accept(fdhandlein,fromaddr,fromaddrsize);
\r
540 //now we have accepted one request start monitoring for more again
\r
541 eventcore.rmasterset(fdhandlein,true);
\r
543 if result = -1 then begin
\r
544 raise esocketexception.create('error '+inttostr({$ifdef win32}getlasterror{$else}socketerror{$endif})+' while accepting');
\r
546 if result > absoloutemaxs then begin
\r
550 raise esocketexception.create('file discriptor out of range: '+inttostr(a));
\r
555 function tlsocket.sendto(dest:TInetSockAddrV;destlen:integer;data:pointer;len:integer):integer;
\r
558 realdest : tinetsockaddrv;
\r
559 biniptemp : tbinip;
\r
561 destx : {$ifdef win32}winsock.pSockAddr{$else}pInetSockAddrV{$endif};
\r
564 {$ifdef secondlistener}
\r
565 if assigned(secondlistener) then if (dest.inaddr.family = AF_INET) then begin
\r
566 result := secondlistener.sendto(dest,destlen,data,len);
\r
571 if isv6socket then begin
\r
572 biniptemp := inaddrvtobinip(dest);
\r
573 converttov6(biniptemp);
\r
574 destlen := makeinaddrv(biniptemp,inttostr(ntohs(dest.InAddr.port)),realdest);
\r
575 destx := {$ifdef win32}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@realdest)
\r
577 destx := {$ifdef win32}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@dest)
\r
580 destx := {$ifdef win32}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@dest)
\r
583 result := {$ifdef win32}winsock.sendto{$else}system_sendto{$endif}(self.fdhandleout,data^,len,0,destx^,destlen);
\r
587 function tlsocket.receivefrom(data:pointer;len:integer;var src:TInetSockAddrV;var srclen:integer):integer;
\r
589 tempsrc:TInetSockAddrV;
\r
590 tempsrclen:integer;
\r
591 srcx : {$ifdef win32}winsock.TSockAddr{$else}TInetSockAddrV{$endif} absolute tempsrc;
\r
594 {$ifdef secondlistener}
\r
595 if assigned(secondlistener) then if lastsessionfromsecond then begin
\r
596 lastsessionfromsecond := false;
\r
597 result := secondlistener.receivefrom(data,len,src,srclen);
\r
601 tempsrclen := sizeof(tempsrc);
\r
602 result := recvfrom(self.fdhandlein,data^,len,0,srcx,tempsrclen);
\r
605 biniptemp := inaddrvtobinip(tempsrc);
\r
606 if needconverttov4(biniptemp) then begin
\r
607 converttov4(biniptemp);
\r
608 tempsrclen := makeinaddrv(biniptemp,inttostr(ntohs(tempsrc.InAddr.port)),tempsrc);
\r
612 move(tempsrc,src,srclen);
\r
613 srclen := tempsrclen;
\r
616 procedure tlsocket.taskcallconnectionfailedhandler(wparam,lparam : longint);
\r
618 connectionfailedhandler(wparam);
\r
621 procedure tlsocket.connectionfailedhandler(error:word);
\r
623 if trymoreips then begin
\r
624 // writeln('failed with error ',error);
\r
625 connecttimeout.enabled := false;
\r
626 destroying := true;
\r
627 state := wsconnected;
\r
628 self.internalclose(0);
\r
629 destroying := false;
\r
632 state := wsconnected;
\r
633 if assigned(onsessionconnected) then onsessionconnected(self,error);
\r
634 self.internalclose(0);
\r
635 recvq.del(maxlongint);
\r
639 procedure tlsocket.connectsuccesshandler;
\r
641 trymoreips := false;
\r
642 connecttimeout.enabled := false;
\r
643 if assigned(onsessionconnected) then onsessionconnected(self,0);
\r
647 procedure tlsocket.handlefdtrigger(readtrigger,writetrigger:boolean);
\r
649 tempbuf:array[0..receivebufsize-1] of byte;
\r
651 // writeln('got a fd trigger, readtrigger=',readtrigger,' writetrigger=',writetrigger,' state=',integer(state));
\r
652 if (state =wslistening) and readtrigger then begin
\r
653 { debugout('listening socket triggered on read');}
\r
654 eventcore.rmasterclr(fdhandlein);
\r
655 if assigned(onsessionAvailable) then onsessionAvailable(self,0);
\r
657 if dgram and readtrigger then begin
\r
658 if assigned(ondataAvailable) then ondataAvailable(self,0);
\r
662 if (state =wsconnecting) and writetrigger then begin
\r
663 // code for dealing with the reults of a non-blocking connect is
\r
665 // if just write is triggered it means connect suceeded
\r
666 // if both read and write are triggered it can mean 2 things
\r
667 // 1: connect ok and data availible
\r
669 // to find out which you must read from the socket and look for errors
\r
670 // there if we read successfully we drop through into the code for fireing
\r
672 if not readtrigger then begin
\r
673 state := wsconnected;
\r
674 connectsuccesshandler;
\r
676 numread := myfdread(fdhandlein,tempbuf,sizeof(tempbuf));
\r
677 if numread <> -1 then begin
\r
678 state := wsconnected;
\r
679 connectsuccesshandler;
\r
680 //connectread := true;
\r
681 recvq.add(@tempbuf,numread);
\r
683 connectionfailedhandler({$ifdef win32}wsagetlasterror{$else}linuxerror{$endif});
\r
686 // if things went well here we are now in the state wsconnected with data sitting in our receive buffer
\r
687 // so we drop down into the processing for data availible
\r
689 if fdhandlein >= 0 then begin
\r
690 if state = wsconnected then begin
\r
691 eventcore.rmasterset(fdhandlein,false);
\r
693 eventcore.rmasterclr(fdhandlein);
\r
696 if fdhandleout >= 0 then begin
\r
697 if sendq.size = 0 then begin
\r
698 //don't clear the bit in fdswmaster if data is in the sendq
\r
699 eventcore.wmasterclr(fdhandleout);
\r
704 inherited handlefdtrigger(readtrigger,writetrigger);
\r
707 constructor tlsocket.Create(AOwner: TComponent);
\r
709 inherited create(aowner);
\r
710 closehandles := true;
\r
711 trymoreips := true;
\r
716 function tlsocket.getpeername(var addr:tsockaddrin;addrlen:integer):integer;
\r
718 addrx : {$ifdef win32}winsock.tsockaddr{$else}tsockaddrin{$endif} absolute addr;
\r
720 result := {$ifdef win32}winsock.getpeername{$else}system_getpeername{$endif}(self.fdhandlein,addrx,addrlen);
\r
723 procedure tlsocket.getxaddrbin(var binip:tbinip);
\r
725 addr:tinetsockaddrv;
\r
729 fillchar(addr,sizeof(addr),0);
\r
732 winsock.getsockname(self.fdhandlein,psockaddr(@addr)^,i);
\r
734 getsocketname(self.fdhandlein,addr,i);
\r
736 binip := inaddrvtobinip(addr);
\r
737 converttov4(binip);
\r
740 procedure tlsocket.getpeeraddrbin(var binip:tbinip);
\r
742 addr:tinetsockaddrv;
\r
746 fillchar(addr,sizeof(addr),0);
\r
748 winsock.getpeername(self.fdhandlein,psockaddr(@addr)^,i);
\r
750 system_getpeername(self.fdhandlein,addr,i);
\r
753 binip := inaddrvtobinip(addr);
\r
754 converttov4(binip);
\r
757 function tlsocket.getXaddr:thostname;
\r
761 getxaddrbin(biniptemp);
\r
762 result := ipbintostr(biniptemp);
\r
763 if result = '' then result := 'error';
\r
766 function tlsocket.getpeeraddr:thostname;
\r
770 getpeeraddrbin(biniptemp);
\r
771 result := ipbintostr(biniptemp);
\r
772 if result = '' then result := 'error';
\r
775 function tlsocket.getXport:ansistring;
\r
777 addr:tinetsockaddrv;
\r
782 winsock.getsockname(self.fdhandlein,psockaddrin(@addr)^,i);
\r
785 getsocketname(self.fdhandlein,addr,i);
\r
788 result := inttostr(htons(addr.InAddr.port));
\r
791 function tlsocket.getpeerport:ansistring;
\r
793 addr:tinetsockaddrv;
\r
798 winsock.getpeername(self.fdhandlein,psockaddrin(@addr)^,i);
\r
801 system_getpeername(self.fdhandlein,addr,i);
\r
804 result := inttostr(htons(addr.InAddr.port));
\r
808 procedure tlsocket.myfdclose(fd : integer);
\r
812 function tlsocket.myfdwrite(fd: LongInt;const buf;size: LongInt):LongInt;
\r
814 result := winsock.send(fd,(@buf)^,size,0);
\r
816 function tlsocket.myfdread(fd: LongInt;var buf;size: LongInt):LongInt;
\r
818 result := winsock.recv(fd,buf,size,0);
\r