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
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 : string);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:string; virtual;
\r
127 function getpeeraddr:string; virtual;
\r
128 function getXport:string; virtual;
\r
129 function getpeerport:string; 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
135 isv6socket : boolean; //identifies if the socket is v6, set by bindsocket
\r
136 procedure taskcallconnectionfailedhandler(wparam,lparam : longint);
\r
138 procedure connecttimeouthandler(sender:tobject);
\r
139 procedure connectsuccesshandler;
\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
146 tsocket=longint; // for compatibility with twsocket
\r
148 twsocket=tlsocket; {easy}
\r
156 {$include unixstuff.inc}
\r
159 function tlsocket.getaddrsize:integer;
\r
161 result := inaddrsize(inaddr);
\r
164 //I used to use the system versions of these from within lsocket (which has
\r
165 //functions whose name clashes with them) by using sockets.* and but I can't do
\r
166 //that anymore since in some cases connect is now provided by unixstuff.inc
\r
167 //hence these wrapper functions --plugwash
\r
169 function system_Connect(Sock: LongInt;const Addr;Addrlen: LongInt):Boolean;
\r
171 result := connect(sock,addr,addrlen);
\r
173 function system_SendTo(Sock: LongInt; const Buf;BufLen: LongInt;Flags: LongInt;var Addr;AddrLen: LongInt):LongInt;
\r
175 result := sendto(sock,buf,buflen,flags,addr,addrlen);
\r
177 function system_getpeername(Sock: LongInt;var Addr;var Addrlen: LongInt):LongInt;
\r
179 result := getpeername(sock,addr,addrlen);
\r
181 function system_listen(Sock: LongInt; MaxConnect: LongInt):Boolean;
\r
183 result := listen(sock,maxconnect);
\r
185 function system_Accept(Sock: LongInt;var Addr;var Addrlen: LongInt):LongInt;
\r
187 result := accept(sock,addr,addrlen);
\r
191 procedure tlsocket.realconnect;
\r
195 //writeln('trying to connect to ',ipbintostr(biniplist_get(biniplist,currentip)),'#',port);
\r
196 makeinaddrv(biniplist_get(biniplist,currentip),port,inaddr);
\r
198 if (currentip >= biniplist_getcount(biniplist)) then trymoreips := false;
\r
201 if (uppercase(proto) = 'UDP') then begin
\r
206 end else if (uppercase(proto) = 'TCP') or (uppercase(proto) = '') then begin
\r
210 end else if (uppercase(proto) = 'ICMP') or (strtointdef(proto,256) < 256) then begin
\r
211 b := strtointdef(proto,IPPROTO_ICMP);
\r
215 raise ESocketException.create('unrecognised protocol');
\r
218 a := Socket(inaddr.inaddr.family,a,b);
\r
219 //writeln(ord(inaddr.inaddr.family));
\r
220 if a = -1 then begin
\r
221 //unable to create socket, fire an error event (better to use an error event
\r
222 //to avoid poor interaction with multilistener stuff.
\r
223 //a socket value of -2 is a special value to say there is no socket but
\r
224 //we want internalclose to act as if there was
\r
227 tltask.create(taskcallconnectionfailedhandler,self,{$ifdef win32}wsagetlasterror{$else}socketerror{$endif},0);
\r
233 if dgram then begin
\r
235 SetSocketOptions(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));
\r
237 SetSockOpt(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));
\r
239 state := wsconnected;
\r
240 if assigned(onsessionconnected) then onsessionconnected(self,0);
\r
242 eventcore.rmasterset(fdhandlein,false);
\r
243 eventcore.wmasterclr(fdhandleout);
\r
245 state :=wsconnecting;
\r
247 //writeln(inaddr.inaddr.port);
\r
248 winsock.Connect(fdhandlein,winsock.psockaddr(@inADDR)^,getaddrsize);
\r
250 system_Connect(fdhandlein,inADDR,getaddrsize);
\r
252 eventcore.rmasterset(fdhandlein,false);
\r
253 eventcore.wmasterset(fdhandleout);
\r
254 if trymoreips then connecttimeout.enabled := true;
\r
258 on e: exception do begin
\r
260 raise; //reraise the exception
\r
266 procedure tlsocket.connecttimeouthandler(sender:tobject);
\r
268 connecttimeout.enabled := false;
\r
269 destroying := true; //hack to not cause handler to trigger
\r
271 destroying := false;
\r
278 procedure tlsocket.connect;
\r
283 if state <> wsclosed then close;
\r
285 if isbiniplist(addr) then biniplist := addr else biniplist := forwardlookuplist(addr,0);
\r
286 if biniplist_getcount(biniplist) = 0 then raise exception.create('unable to resolve '+addr);
\r
288 //makeinaddrv(addr,port,inaddr);
\r
291 if not assigned(connecttimeout) then begin
\r
292 connecttimeout := tltimer.create(self);
\r
293 connecttimeout.ontimer := connecttimeouthandler;
\r
294 connecttimeout.interval := 2500;
\r
295 connecttimeout.enabled := false;
\r
300 procedure tlsocket.sendstr(const str : string);
\r
302 if dgram then begin
\r
303 send(@str[1],length(str))
\r
305 inherited sendstr(str);
\r
309 function tlsocket.send(data:pointer;len:integer):integer;
\r
311 if dgram then begin
\r
312 // writeln('sending to '+ipbintostr(inaddrvtobinip(inaddr)),' ',htons(inaddr.inaddr.port),' ',len,' bytes');
\r
313 result := sendto(inaddr,getaddrsize,data,len);
\r
315 // writeln('send result ',result);
\r
316 // writeln('errno',errno);
\r
318 result := inherited send(data,len);
\r
323 function tlsocket.receive(Buf:Pointer;BufSize:integer):integer;
\r
325 if dgram then begin
\r
326 {$ifdef secondlistener}
\r
327 if lastsessionfromsecond then begin
\r
328 result := secondlistener.receive(buf,bufsize);
\r
329 lastsessionfromsecond := false;
\r
332 result := myfdread(self.fdhandlein,buf^,bufsize);
\r
334 result := inherited receive(buf,bufsize);
\r
338 procedure tlsocket.bindsocket;
\r
341 inAddrtemp:TInetSockAddrV;
\r
342 inAddrtempx:{$ifdef win32}winsock.TSockaddr{$else}TInetSockAddrV{$endif} absolute inaddrtemp;
\r
343 inaddrtempsize:integer;
\r
346 if (localaddr <> '') or (localport <> '') then begin
\r
347 if localaddr = '' then begin
\r
349 if inaddr.inaddr.family = AF_INET6 then localaddr := '::' else
\r
351 localaddr := '0.0.0.0';
\r
353 //gethostbyname(localaddr,host);
\r
354 inaddrtempsize := makeinaddrv(forwardlookup(localaddr,0),localport,inaddrtemp);
\r
355 isv6socket := (inaddrtemp.inaddr.family = AF_INET6);
\r
356 If Bind(fdhandlein,inaddrtempx,inaddrtempsize)<> {$ifdef win32}0{$else}true{$endif} Then begin
\r
358 lasterror := {$ifdef win32}getlasterror{$else}socketerror{$endif};
\r
359 raise ESocketException.create('unable to bind on address '+localaddr+'#'+localport+', error '+inttostr(lasterror));
\r
364 on e: exception do begin
\r
366 raise; //reraise the exception
\r
371 procedure tlsocket.listen;
\r
378 if state <> wsclosed then close;
\r
379 udp := uppercase(proto) = 'UDP';
\r
381 socktype := SOCK_DGRAM;
\r
383 end else socktype := SOCK_STREAM;
\r
386 if addr = '' then begin
\r
388 //writeln('ipv6 is defined');
\r
389 if not v4listendefault then begin
\r
390 //writeln('setting addr to ::');
\r
396 if isbiniplist(addr) then biniptemp := biniplist_get(addr,0) else biniptemp := forwardlookup(addr,10);
\r
397 addr := ipbintostr(biniptemp);
\r
398 //writeln('after ipbintostr call addr =',addr);
\r
399 //writeln('biniptemp.family =',biniptemp.family);
\r
400 //writeln('AF_INET6=',AF_INET6);
\r
401 //writeln('PF_INET6=',PF_INET6);
\r
402 //writeln('AF_INET=',AF_INET);
\r
403 //writeln('PF_INET=',PF_INET);
\r
405 fdhandlein := socket(biniptemp.family,socktype,0);
\r
407 if (addr = '::') and (origaddr = '') and (fdhandlein < 0) then begin
\r
408 writeln('failed to create an IPV6 socket with error ',socketerror,'. trying to create an IPV4 one instead');
\r
410 fdhandlein := socket(PF_INET,socktype,0);
\r
414 if fdhandlein = -1 then raise ESocketException.create('unable to create socket'{$ifdef win32}+' error='+inttostr(wsagetlasterror){$endif});
\r
415 dupnowatch(fdhandlein); // sets up maxs and copies handle to fdhandleout among other things
\r
416 //eventcore.setfdreverse(fdhandlein,self); //already taken care of by dup
\r
417 state := wsclosed; // then set this back as it was an undesired side effect of dup
\r
420 yes := $01010101; {Copied this from existing code. Value is empiric,
\r
421 but works. (yes=true<>0) }
\r
424 if SetSocketOptions(fdhandlein, SOL_SOCKET, SO_REUSEADDR,yes,sizeof(yes))=-1 then begin
\r
425 raise ESocketException.create('unable to set SO_REUSEADDR socket option');
\r
427 //writeln('addr=',addr);
\r
428 //writeln('setting IPV6_V6ONLY option to 0');
\r
429 //allow v4 connections on v6 sockets
\r
430 if biniptemp.family = af_inet6 then begin
\r
431 if SetSocketOptions(fdhandlein, IPPROTO_IPV6,IPV6_V6ONLY,no,sizeof(no))=-1 then begin
\r
432 writeln(IPPROTO_IPV6);
\r
433 writeln(IPV6_V6ONLY);
\r
434 raise ESocketException.create('unable to set IPV6_V6ONLY socket option error='+inttostr(socketerror));
\r
443 if not udp then begin
\r
444 {!!! allow custom queue length? default 5}
\r
445 if listenqueue = 0 then listenqueue := 5;
\r
446 If {$ifdef win32}winsock.listen{$else}system_listen{$endif}(fdhandlein,listenqueue)<>{$ifdef win32}0{$else}true{$endif} Then raise
\r
447 esocketexception.create('unable to listen');
\r
448 state := wsListening;
\r
451 SetSocketOptions(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));
\r
453 SetSockOpt(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));
\r
455 state := wsconnected;
\r
458 {$ifdef secondlistener}
\r
459 //listening on ::. try to listen on 0.0.0.0 as well for platforms which don't already do that
\r
460 if addr = '::' then begin
\r
461 secondlistener := tlsocket.create(nil);
\r
462 secondlistener.proto := proto;
\r
463 secondlistener.addr := '0.0.0.0';
\r
464 secondlistener.port := port;
\r
466 secondlistener.ondataavailable := secondaccepthandler;
\r
468 secondlistener.onsessionAvailable := secondaccepthandler;
\r
471 secondlistener.listen;
\r
473 secondlistener.destroy;
\r
474 secondlistener := nil;
\r
479 if state = wsclosed then begin
\r
480 if fdhandlein >= 0 then begin
\r
481 {one *can* get here without fd -beware}
\r
482 eventcore.rmasterclr(fdhandlein);
\r
483 myfdclose(fdhandlein); // we musnt leak file discriptors
\r
484 eventcore.setfdreverse(fdhandlein,nil);
\r
488 eventcore.rmasterset(fdhandlein,not udp);
\r
490 if fdhandleout >= 0 then eventcore.wmasterclr(fdhandleout);
\r
492 //writeln('listened on addr '+addr+':'+port+' '+proto+' using socket number ',fdhandlein);
\r
495 {$ifdef secondlistener}
\r
496 procedure tlsocket.internalclose(error:word);
\r
498 if assigned(secondlistener) then begin
\r
499 secondlistener.destroy;
\r
500 secondlistener := nil;
\r
502 inherited internalclose(error);
\r
505 procedure tlsocket.secondaccepthandler;
\r
507 lastsessionfromsecond := true;
\r
509 ondataavailable(self,error);
\r
511 if assigned(onsessionavailable) then onsessionavailable(self,error);
\r
516 function tlsocket.accept : longint;
\r
518 FromAddrSize : LongInt; // i don't realy know what to do with these at this
\r
519 FromAddr : TInetSockAddrV; // at this point time will tell :)
\r
522 {$ifdef secondlistener}
\r
523 if (lastsessionfromsecond) then begin
\r
524 lastsessionfromsecond := false;
\r
525 result := secondlistener.accept;
\r
530 FromAddrSize := Sizeof(FromAddr);
\r
532 result := winsock.accept(fdhandlein,@fromaddr,@fromaddrsize);
\r
534 result := system_accept(fdhandlein,fromaddr,fromaddrsize);
\r
536 //now we have accepted one request start monitoring for more again
\r
537 eventcore.rmasterset(fdhandlein,true);
\r
539 if result = -1 then begin
\r
540 raise esocketexception.create('error '+inttostr({$ifdef win32}getlasterror{$else}socketerror{$endif})+' while accepting');
\r
542 if result > absoloutemaxs then begin
\r
546 raise esocketexception.create('file discriptor out of range: '+inttostr(a));
\r
551 function tlsocket.sendto(dest:TInetSockAddrV;destlen:integer;data:pointer;len:integer):integer;
\r
554 realdest : tinetsockaddrv;
\r
555 biniptemp : tbinip;
\r
557 destx : {$ifdef win32}winsock.pSockAddr{$else}pInetSockAddrV{$endif};
\r
560 {$ifdef secondlistener}
\r
561 if assigned(secondlistener) then if (dest.inaddr.family = AF_INET) then begin
\r
562 result := secondlistener.sendto(dest,destlen,data,len);
\r
567 if isv6socket then begin
\r
568 biniptemp := inaddrvtobinip(dest);
\r
569 converttov6(biniptemp);
\r
570 destlen := makeinaddrv(biniptemp,inttostr(ntohs(dest.InAddr.port)),realdest);
\r
571 destx := {$ifdef win32}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@realdest)
\r
573 destx := {$ifdef win32}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@dest)
\r
576 destx := {$ifdef win32}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@dest)
\r
579 result := {$ifdef win32}winsock.sendto{$else}system_sendto{$endif}(self.fdhandleout,data^,len,0,destx^,destlen);
\r
583 function tlsocket.receivefrom(data:pointer;len:integer;var src:TInetSockAddrV;var srclen:integer):integer;
\r
585 tempsrc:TInetSockAddrV;
\r
586 tempsrclen:integer;
\r
587 srcx : {$ifdef win32}winsock.TSockAddr{$else}TInetSockAddrV{$endif} absolute tempsrc;
\r
590 {$ifdef secondlistener}
\r
591 if assigned(secondlistener) then if lastsessionfromsecond then begin
\r
592 lastsessionfromsecond := false;
\r
593 result := secondlistener.receivefrom(data,len,src,srclen);
\r
597 tempsrclen := sizeof(tempsrc);
\r
598 result := recvfrom(self.fdhandlein,data^,len,0,srcx,tempsrclen);
\r
601 biniptemp := inaddrvtobinip(tempsrc);
\r
602 if needconverttov4(biniptemp) then begin
\r
603 converttov4(biniptemp);
\r
604 tempsrclen := makeinaddrv(biniptemp,inttostr(ntohs(tempsrc.InAddr.port)),tempsrc);
\r
608 move(tempsrc,src,srclen);
\r
609 srclen := tempsrclen;
\r
612 procedure tlsocket.taskcallconnectionfailedhandler(wparam,lparam : longint);
\r
614 connectionfailedhandler(wparam);
\r
617 procedure tlsocket.connectionfailedhandler(error:word);
\r
619 if trymoreips then begin
\r
620 // writeln('failed with error ',error);
\r
621 connecttimeout.enabled := false;
\r
622 destroying := true;
\r
623 state := wsconnected;
\r
624 self.internalclose(0);
\r
625 destroying := false;
\r
628 state := wsconnected;
\r
629 if assigned(onsessionconnected) then onsessionconnected(self,error);
\r
630 self.internalclose(0);
\r
631 recvq.del(maxlongint);
\r
635 procedure tlsocket.connectsuccesshandler;
\r
637 trymoreips := false;
\r
638 connecttimeout.enabled := false;
\r
639 if assigned(onsessionconnected) then onsessionconnected(self,0);
\r
643 procedure tlsocket.handlefdtrigger(readtrigger,writetrigger:boolean);
\r
645 tempbuf:array[0..receivebufsize-1] of byte;
\r
647 // writeln('got a fd trigger, readtrigger=',readtrigger,' writetrigger=',writetrigger,' state=',integer(state));
\r
648 if (state =wslistening) and readtrigger then begin
\r
649 { debugout('listening socket triggered on read');}
\r
650 eventcore.rmasterclr(fdhandlein);
\r
651 if assigned(onsessionAvailable) then onsessionAvailable(self,0);
\r
653 if dgram and readtrigger then begin
\r
654 if assigned(ondataAvailable) then ondataAvailable(self,0);
\r
658 if (state =wsconnecting) and writetrigger then begin
\r
659 // code for dealing with the reults of a non-blocking connect is
\r
661 // if just write is triggered it means connect suceeded
\r
662 // if both read and write are triggered it can mean 2 things
\r
663 // 1: connect ok and data availible
\r
665 // to find out which you must read from the socket and look for errors
\r
666 // there if we read successfully we drop through into the code for fireing
\r
668 if not readtrigger then begin
\r
669 state := wsconnected;
\r
670 connectsuccesshandler;
\r
672 numread := myfdread(fdhandlein,tempbuf,sizeof(tempbuf));
\r
673 if numread <> -1 then begin
\r
674 state := wsconnected;
\r
675 connectsuccesshandler;
\r
676 //connectread := true;
\r
677 recvq.add(@tempbuf,numread);
\r
679 connectionfailedhandler({$ifdef win32}wsagetlasterror{$else}linuxerror{$endif});
\r
682 // if things went well here we are now in the state wsconnected with data sitting in our receive buffer
\r
683 // so we drop down into the processing for data availible
\r
685 if fdhandlein >= 0 then begin
\r
686 if state = wsconnected then begin
\r
687 eventcore.rmasterset(fdhandlein,false);
\r
689 eventcore.rmasterclr(fdhandlein);
\r
692 if fdhandleout >= 0 then begin
\r
693 if sendq.size = 0 then begin
\r
694 //don't clear the bit in fdswmaster if data is in the sendq
\r
695 eventcore.wmasterclr(fdhandleout);
\r
700 inherited handlefdtrigger(readtrigger,writetrigger);
\r
703 constructor tlsocket.Create(AOwner: TComponent);
\r
705 inherited create(aowner);
\r
706 closehandles := true;
\r
707 trymoreips := true;
\r
712 function tlsocket.getpeername(var addr:tsockaddrin;addrlen:integer):integer;
\r
714 addrx : {$ifdef win32}winsock.tsockaddr{$else}tsockaddrin{$endif} absolute addr;
\r
716 result := {$ifdef win32}winsock.getpeername{$else}system_getpeername{$endif}(self.fdhandlein,addrx,addrlen);
\r
719 procedure tlsocket.getxaddrbin(var binip:tbinip);
\r
721 addr:tinetsockaddrv;
\r
725 fillchar(addr,sizeof(addr),0);
\r
728 winsock.getsockname(self.fdhandlein,psockaddr(@addr)^,i);
\r
730 getsocketname(self.fdhandlein,addr,i);
\r
732 binip := inaddrvtobinip(addr);
\r
733 converttov4(binip);
\r
736 procedure tlsocket.getpeeraddrbin(var binip:tbinip);
\r
738 addr:tinetsockaddrv;
\r
742 fillchar(addr,sizeof(addr),0);
\r
744 winsock.getpeername(self.fdhandlein,psockaddr(@addr)^,i);
\r
746 system_getpeername(self.fdhandlein,addr,i);
\r
749 binip := inaddrvtobinip(addr);
\r
750 converttov4(binip);
\r
753 function tlsocket.getXaddr:string;
\r
757 getxaddrbin(biniptemp);
\r
758 result := ipbintostr(biniptemp);
\r
759 if result = '' then result := 'error';
\r
762 function tlsocket.getpeeraddr:string;
\r
766 getpeeraddrbin(biniptemp);
\r
767 result := ipbintostr(biniptemp);
\r
768 if result = '' then result := 'error';
\r
771 function tlsocket.getXport:string;
\r
773 addr:tinetsockaddrv;
\r
778 winsock.getsockname(self.fdhandlein,psockaddrin(@addr)^,i);
\r
781 getsocketname(self.fdhandlein,addr,i);
\r
784 result := inttostr(htons(addr.InAddr.port));
\r
787 function tlsocket.getpeerport:string;
\r
789 addr:tinetsockaddrv;
\r
794 winsock.getpeername(self.fdhandlein,psockaddrin(@addr)^,i);
\r
797 system_getpeername(self.fdhandlein,addr,i);
\r
800 result := inttostr(htons(addr.InAddr.port));
\r
804 procedure tlsocket.myfdclose(fd : integer);
\r
808 function tlsocket.myfdwrite(fd: LongInt;const buf;size: LongInt):LongInt;
\r
810 result := winsock.send(fd,(@buf)^,size,0);
\r
812 function tlsocket.myfdread(fd: LongInt;var buf;size: LongInt):LongInt;
\r
814 result := winsock.recv(fd,buf,size,0);
\r