X-Git-Url: http://www.lcore.org/git/lcore.git/blobdiff_plain/0a9944546bc1cf591dfce10112a4385049909f02..69598fec083b67c8567293c7b8a397b64175bd45:/lsocket.pas?ds=sidebyside diff --git a/lsocket.pas b/lsocket.pas old mode 100755 new mode 100644 index 509bf8e..6a4034d --- a/lsocket.pas +++ b/lsocket.pas @@ -2,12 +2,10 @@ {socket code by plugwash} -{use this code for whatever you like in programs under whater licence you like - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. -} - +{ Copyright (C) 2005 Bas Steendijk and Peter Green + For conditions of distribution and use, see copyright notice in zlib_license.txt + which is included in the package + ----------------------------------------------------------------------------- } { changes by plugwash (20030728) * created handlefdtrigger virtual method in tlasio (overridden in tlsocket) and moved a lot of code from messageloop into it @@ -24,7 +22,7 @@ changes by beware (20030903) beware (20030905) * if connect failed (conn refused) set state to connected and call internalclose, to get closed handler (instead of fdclose) -* (lcore) if closing the fd's in internalcose, set fd's to -1 because closing an fd makes it invalid +* (lcore) if closing the fd's in internalclose, set fds to -1 because closing an fd makes it invalid beware (20030927) * fixed: on connect failed, tried to close fdhandle's which were already set to -1, added check @@ -35,16 +33,34 @@ beware (20031017) unit lsocket; -{$mode delphi} +{$ifdef fpc} + {$mode delphi} +{$endif} + +{$include lcoreconfig.inc} + interface uses sysutils, - {$ifdef VER1_0} - linux, + {$ifdef mswindows} + windows,winsock, {$else} - baseunix,unix, + + {$ifdef VER1_0} + linux, + {$else} + baseunix,unix,unixutil, + {$endif} + sockets, {$endif} - sockets,classes,pgdebugout,pgtypes,lcore,fd_utils,binipstuff,dnssync; + classes,{pgdebugout,}pgtypes,lcore,fd_utils,binipstuff,dnssync; + +{$ifdef ipv6} +const + v4listendefault:boolean=false; +{$endif} + + type sunB = packed record s_b1, s_b2, s_b3, s_b4: byte; @@ -60,32 +76,6 @@ type 1: (S_un_w: SunW); 2: (S_addr: cardinal); end; - {$ifdef ipv6} - {$ifdef ver1_0} - cuint16=word; - cuint32=dword; - sa_family_t=word; - - - TInetSockAddr6 = packed Record - sin6_family : sa_family_t; - sin6_port : cuint16; - sin6_flowinfo : cuint32; - sin6_addr : Tin6_addr; - sin6_scope_id : cuint32; - end; - {$endif} - {$endif} - TinetSockAddrv = packed record - case integer of - 0: (InAddr:TInetSockAddr); - {$ifdef ipv6} - 1: (InAddr6:TInetSockAddr6); - {$endif} - end; - - type - tsockaddrin=TInetSockAddr; type TLsocket = class(tlasio) @@ -93,52 +83,79 @@ type //a: string; inAddr : TInetSockAddrV; + + biniplist:tbiniplist; + trymoreips:boolean; + currentip:integer; + connecttimeout:tltimer; + { inAddrSize:integer;} //host : THostentry ; - //mainthread : boolean ; //for debuggin only - addr:string; - port:string; - localaddr:string; - localport:string; - proto:string; - udp:boolean; + //mainthread : boolean ; //for debugging only + addr:thostname; + port:ansistring; + localaddr:thostname; + localport:ansistring; + proto:ansistring; + udp,dgram:boolean; listenqueue:integer; + + onconnecttryip:procedure(sender:tobject; const ip:tbinip) of object; + + {$ifdef secondlistener} + secondlistener:tlsocket; + lastsessionfromsecond:boolean; + procedure secondaccepthandler(sender:tobject;error:word); + procedure internalclose(error:word);override; + {$endif} function getaddrsize:integer; procedure connect; virtual; + procedure realconnect; procedure bindsocket; procedure listen; function accept : longint; - function sendto(dest:TInetSockAddr;destlen:integer;data:pointer;len:integer):integer; virtual; - function receivefrom(data:pointer;len:integer;var src:TInetSockAddr;var srclen:integer):integer; virtual; - //procedure internalclose(error:word);override; + function sendto(dest:TInetSockAddrV;destlen:integer;data:pointer;len:integer):integer; virtual; + function receivefrom(data:pointer;len:integer;var src:TInetSockAddrV;var srclen:integer):integer; virtual; + procedure handlefdtrigger(readtrigger,writetrigger:boolean); override; function send(data:pointer;len:integer):integer;override; - procedure sendstr(const str : string);override; + procedure sendstr(const str : tbufferstring);override; function Receive(Buf:Pointer;BufSize:integer):integer; override; function getpeername(var addr:tsockaddrin;addrlen:integer):integer; virtual; procedure getXaddrbin(var binip:tbinip); virtual; procedure getpeeraddrbin(var binip:tbinip); virtual; - function getXaddr:string; virtual; - function getpeeraddr:string; virtual; - function getXport:string; virtual; - function getpeerport:string; virtual; + function getXaddr:thostname; virtual; + function getpeeraddr:thostname; virtual; + function getXport:ansistring; virtual; + function getpeerport:ansistring; virtual; constructor Create(AOwner: TComponent); override; + + //this one has to be kept public for now because lcorewsaasyncselect calls it + procedure connectionfailedhandler(error:word); + + {public in tlasio, and can't be private in both places, so should be public here. + fixes delphi warning --beware} + {$ifdef mswindows} + procedure myfdclose(fd : integer); override; + function myfdwrite(fd: LongInt;const buf;size: LongInt):LongInt; override; + function myfdread(fd: LongInt;var buf;size: LongInt):LongInt; override; + {$endif} + + private + {$ifdef ipv6} + isv6socket : boolean; //identifies if the socket is v6, set by bindsocket + {$endif} + procedure taskcallconnectionfailedhandler(wparam,lparam : longint); + + procedure connecttimeouthandler(sender:tobject); + procedure connectsuccesshandler; end; tsocket=longint; // for compatibility with twsocket twsocket=tlsocket; {easy} -function htons(w:word):word; -function htonl(i:integer):integer; -{!!!function longipdns(s:string):longint;} - -{$ifdef ipv6} -const - v4listendefault:boolean=false; -{$endif} - const TCP_NODELAY=1; @@ -147,159 +164,109 @@ const implementation {$include unixstuff.inc} -function longip(s:string):longint;inline; -var - l:longint; - a,b:integer; -function convertbyte(const s:string):integer;inline; +function tlsocket.getaddrsize:integer; begin - result := strtointdef(s,-1); - if result < 0 then exit; - if result > 255 then exit; + result := inaddrsize(inaddr); +end; - {01 exception} - if (result <> 0) and (s[1] = '0') then begin - result := -1; - exit; +//I used to use the system versions of these from within lsocket (which has +//functions whose name clashes with them) by using sockets.* and but I can't do +//that anymore since in some cases connect is now provided by unixstuff.inc +//hence these wrapper functions --plugwash +{$ifndef mswindows} + function system_Connect(Sock: LongInt;const Addr;Addrlen: LongInt):Boolean; + begin + result := connect(sock,addr,addrlen); end; - - {+1 exception} - if not (s[1] in ['0'..'9']) then begin - result := -1; - exit + function system_SendTo(Sock: LongInt; const Buf;BufLen: LongInt;Flags: LongInt;var Addr;AddrLen: LongInt):LongInt; + begin + result := sendto(sock,buf,buflen,flags,addr,addrlen); end; -end; - -begin - result := 0; - a := pos('.',s); - if a = 0 then exit; - b := convertbyte(copy(s,1,a-1));if (b < 0) then exit; - l := b shl 24; - s := copy(s,a+1,256); - a := pos('.',s); - if a = 0 then exit; - b := convertbyte(copy(s,1,a-1));if (b < 0) then exit; - l := l or b shl 16; - s := copy(s,a+1,256); - a := pos('.',s); - if a = 0 then exit; - b := convertbyte(copy(s,1,a-1));if (b < 0) then exit; - l := l or b shl 8; - s := copy(s,a+1,256); - b := convertbyte(copy(s,1,256));if (b < 0) then exit; - l := l or b; - result := l; -end; + function system_getpeername(Sock: LongInt;var Addr;var Addrlen: LongInt):LongInt; + begin + result := getpeername(sock,addr,addrlen); + end; + function system_listen(Sock: LongInt; MaxConnect: LongInt):Boolean; + begin + result := listen(sock,maxconnect); + end; + function system_Accept(Sock: LongInt;var Addr;var Addrlen: LongInt):LongInt; + begin + result := accept(sock,addr,addrlen); + end; +{$endif} -(*!!! -function longipdns(s:string):longint; +procedure tlsocket.realconnect; var - host : thostentry; + a,b:integer; + iptemp:tbinip; begin - if s = '0.0.0.0' then begin - result := 0; + iptemp := biniplist_get(biniplist,currentip); + //writeln('trying to connect to ',ipbintostr(biniplist_get(biniplist,currentip)),'#',port); + if assigned(onconnecttryip) then onconnecttryip(self,iptemp); + makeinaddrv(iptemp,port,inaddr); + inc(currentip); + if (currentip >= biniplist_getcount(biniplist)) then trymoreips := false; + + udp := false; + if (uppercase(proto) = 'UDP') then begin + b := IPPROTO_UDP; + a := SOCK_DGRAM; + udp := true; + dgram := true; + end else if (uppercase(proto) = 'TCP') or (uppercase(proto) = '') then begin + b := IPPROTO_TCP; + a := SOCK_STREAM; + dgram := false; + end else if (uppercase(proto) = 'ICMP') or (strtointdef(proto,256) < 256) then begin + b := strtointdef(proto,IPPROTO_ICMP); + a := SOCK_RAW; + dgram := true; end else begin - result := longip(s); - if result = 0 then begin - if gethostbyname(s,host) then begin; - result := htonl(Longint(Host.Addr)); - end; - //writeln(inttohex(longint(host.addr),8)) - end; - if result = 0 then begin - if resolvehostbyname(s,host) then begin; - result := htonl(Longint(Host.Addr)); - end; - //writeln(inttohex(longint(host.addr),8)) - end; + raise ESocketException.create('unrecognised protocol'); end; -end; -*) - - -function htons(w:word):word; -begin - {$ifdef ENDIAN_LITTLE} - result := ((w and $ff00) shr 8) or ((w and $ff) shl 8); - {$else} - result := w; - {$endif} -end; - -function htonl(i:integer):integer; -begin - {$ifdef ENDIAN_LITTLE} - result := (i shr 24) or (i shr 8 and $ff00) or (i shl 8 and $ff0000) or (i shl 24 and $ff000000); - {$else} - result := i; - {$endif} -end; - -function tlsocket.getaddrsize:integer; -begin - {$ifdef ipv6} - if inaddr.inaddr.family = AF_INET6 then result := sizeof(tinetsockaddr6) else - {$endif} - result := sizeof(tinetsockaddr); -end; - -function makeinaddrv(addr,port:string;var inaddr:tinetsockaddrv):integer; -var - biniptemp:tbinip; -begin - result := 0; - biniptemp := forwardlookup(addr,10); - fillchar(inaddr,sizeof(inaddr),0); - if biniptemp.family = AF_INET then begin - inAddr.InAddr.family:=AF_INET; - inAddr.InAddr.port:=htons(strtointdef(port,0)); - inAddr.InAddr.addr:=biniptemp.ip; - result := sizeof(tinetsockaddr); - end else - {$ifdef ipv6} - if biniptemp.family = AF_INET6 then begin - inAddr.InAddr6.sin6_family:=AF_INET6; - inAddr.InAddr6.sin6_port:=htons(strtointdef(port,0)); - inAddr.InAddr6.sin6_addr:=biniptemp.ip6; - result := sizeof(tinetsockaddr6); - end else - {$endif} - raise esocketexception.create('unable to resolve address: '+addr); -end; - -procedure tlsocket.connect; -var - a:integer; -begin - if state <> wsclosed then close; - //prevtime := 0; - makeinaddrv(addr,port,inaddr); - - udp := uppercase(proto) = 'UDP'; - if udp then a := SOCK_DGRAM else a := SOCK_STREAM; - a := Socket(inaddr.inaddr.family,a,0); + a := Socket(inaddr.inaddr.family,a,b); + //writeln(ord(inaddr.inaddr.family)); if a = -1 then begin - lasterror := socketerror; - raise esocketexception.create('unable to create socket'); + //unable to create socket, fire an error event (better to use an error event + //to avoid poor interaction with multilistener stuff. + //a socket value of -2 is a special value to say there is no socket but + //we want internalclose to act as if there was + fdhandlein := -2; + fdhandleout := -2; + tltask.create(taskcallconnectionfailedhandler,self,{$ifdef mswindows}wsagetlasterror{$else}socketerror{$endif},0); + exit; end; try dup(a); bindsocket; - if udp then begin + if dgram then begin + {$ifndef mswindows} + SetSocketOptions(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE')); + {$else} + SetSockOpt(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE')); + {$endif} state := wsconnected; if assigned(onsessionconnected) then onsessionconnected(self,0); + + eventcore.rmasterset(fdhandlein,false); + eventcore.wmasterclr(fdhandleout); end else begin state :=wsconnecting; - sockets.Connect(fdhandlein,inADDR,getaddrsize); - end; - rmasterset(fdhandlein); - if udp then begin - wmasterclr(fdhandleout); - end else begin - wmasterset(fdhandleout); + {$ifdef mswindows} + //beware: atleast on windows, wsaasyncselect set interest in events before connecting, or a connect error isn't seen if it happens immediately during connect + eventcore.rmasterset(fdhandlein,false); + eventcore.wmasterset(fdhandleout); + //writeln(inaddr.inaddr.port); + winsock.Connect(fdhandlein,winsock.psockaddr(@inADDR)^,getaddrsize); + {$else} + system_Connect(fdhandlein,inADDR,getaddrsize); + eventcore.rmasterset(fdhandlein,false); + eventcore.wmasterset(fdhandleout); + {$endif} + if trymoreips then connecttimeout.enabled := true; end; //sendq := ''; except @@ -308,11 +275,43 @@ begin raise; //reraise the exception end; end; + end; -procedure tlsocket.sendstr(const str : string); +procedure tlsocket.connecttimeouthandler(sender:tobject); begin - if udp then begin + connecttimeout.enabled := false; + destroying := true; //hack to not cause handler to trigger + internalclose(0); + destroying := false; + realconnect; +end; + + + + +procedure tlsocket.connect; +begin + if state <> wsclosed then close; + //prevtime := 0; + if isbiniplist(addr) then biniplist := addr else biniplist := forwardlookuplist(addr,0); + if biniplist_getcount(biniplist) = 0 then raise exception.create('unable to resolve '+addr); + + //makeinaddrv(addr,port,inaddr); + + currentip := 0; + if not assigned(connecttimeout) then begin + connecttimeout := tltimer.create(self); + connecttimeout.ontimer := connecttimeouthandler; + connecttimeout.interval := 5000; + connecttimeout.enabled := false; + end; + realconnect; +end; + +procedure tlsocket.sendstr(const str : tbufferstring); +begin + if dgram then begin send(@str[1],length(str)) end else begin inherited sendstr(str); @@ -321,8 +320,12 @@ end; function tlsocket.send(data:pointer;len:integer):integer; begin - if udp then begin - result := sendto(inaddr.inaddr,getaddrsize,data,len) + if dgram then begin +// writeln('sending to '+ipbintostr(inaddrvtobinip(inaddr)),' ',htons(inaddr.inaddr.port),' ',len,' bytes'); + result := sendto(inaddr,getaddrsize,data,len); + +// writeln('send result ',result); +// writeln('errno',errno); end else begin result := inherited send(data,len); end; @@ -331,8 +334,14 @@ end; function tlsocket.receive(Buf:Pointer;BufSize:integer):integer; begin - if udp then begin - result := fdread(self.fdhandlein,buf^,bufsize); + if dgram then begin + {$ifdef secondlistener} + if lastsessionfromsecond then begin + result := secondlistener.receive(buf,bufsize); + lastsessionfromsecond := false; + end else + {$endif} + result := myfdread(self.fdhandlein,buf^,bufsize); end else begin result := inherited receive(buf,bufsize); end; @@ -340,8 +349,8 @@ end; procedure tlsocket.bindsocket; var - a:integer; inAddrtemp:TInetSockAddrV; + inAddrtempx:{$ifdef mswindows}winsock.TSockaddr{$else}TInetSockAddrV{$endif} absolute inaddrtemp; inaddrtempsize:integer; begin try @@ -353,13 +362,14 @@ begin localaddr := '0.0.0.0'; end; //gethostbyname(localaddr,host); - - inaddrtempsize := makeinaddrv(localaddr,localport,inaddrtemp); - - If Not Bind(fdhandlein,inaddrtemp,inaddrtempsize) Then begin + inaddrtempsize := makeinaddrv(forwardlookup(localaddr,0),localport,inaddrtemp); + {$ifdef ipv6} + isv6socket := (inaddrtemp.inaddr.family = AF_INET6); + {$endif} + If Bind(fdhandlein,inaddrtempx,inaddrtempsize)<> {$ifdef mswindows}0{$else}true{$endif} Then begin state := wsclosed; - lasterror := socketerror; - raise ESocketException.create('unable to bind, error '+inttostr(lasterror)); + lasterror := {$ifdef mswindows}getlasterror{$else}socketerror{$endif}; + raise ESocketException.create('unable to bind on address '+localaddr+'#'+localport+', error '+inttostr(lasterror)); end; state := wsbound; end; @@ -373,45 +383,79 @@ end; procedure tlsocket.listen; var - yes:longint; + {$ifndef mswindows} + yes,no:longint; + {$endif} socktype:integer; biniptemp:tbinip; - origaddr:string; + origaddr:thostname; begin if state <> wsclosed then close; udp := uppercase(proto) = 'UDP'; - if udp then socktype := SOCK_DGRAM else socktype := SOCK_STREAM; + if udp then begin + socktype := SOCK_DGRAM; + dgram := true; + end else socktype := SOCK_STREAM; origaddr := addr; if addr = '' then begin {$ifdef ipv6} + //writeln('ipv6 is defined'); if not v4listendefault then begin + //writeln('setting addr to ::'); addr := '::'; end else {$endif} addr := '0.0.0.0'; end; - biniptemp := forwardlookup(addr,10); + if isbiniplist(addr) then biniptemp := biniplist_get(addr,0) else biniptemp := forwardlookup(addr,10); addr := ipbintostr(biniptemp); + //writeln('after ipbintostr call addr =',addr); + //writeln('biniptemp.family =',biniptemp.family); + //writeln('AF_INET6=',AF_INET6); + //writeln('PF_INET6=',PF_INET6); + //writeln('AF_INET=',AF_INET); + //writeln('PF_INET=',PF_INET); + fdhandlein := socket(biniptemp.family,socktype,0); {$ifdef ipv6} if (addr = '::') and (origaddr = '') and (fdhandlein < 0) then begin + {writeln('failed to create an IPV6 socket with error ',socketerror,'. trying to create an IPV4 one instead');} addr := '0.0.0.0'; - fdhandlein := socket(AF_INET,socktype,0); + biniptemp := ipstrtobinf(addr); + fdhandlein := socket(PF_INET,socktype,0); end; {$endif} - if fdhandlein = -1 then raise ESocketException.create('unable to create socket'); - dup(fdhandlein); // sets up maxs and copies handle to fdhandleout among other things - fdreverse[fdhandlein] := self; + + if fdhandlein = -1 then raise ESocketException.create('unable to create socket'{$ifdef mswindows}+' error='+inttostr(wsagetlasterror){$endif}); + dupnowatch(fdhandlein); // sets up maxs and copies handle to fdhandleout among other things + //eventcore.setfdreverse(fdhandlein,self); //already taken care of by dup state := wsclosed; // then set this back as it was an undesired side effect of dup try - yes := $01010101; {Copied this from existing code. Value is empiric, + {$ifndef mswindows} + yes := $01010101; {Copied this from existing code. Value is empiric, but works. (yes=true<>0) } - if SetSocketOptions(fdhandlein, SOL_SOCKET, SO_REUSEADDR,yes,sizeof(yes))=-1 then begin - raise ESocketException.create('unable to set socket options'); - end; + no := 0; + + if SetSocketOptions(fdhandlein, SOL_SOCKET, SO_REUSEADDR,yes,sizeof(yes))=-1 then begin + raise ESocketException.create('unable to set SO_REUSEADDR socket option'); + end; + //writeln('addr=',addr); + //writeln('setting IPV6_V6ONLY option to 0'); + //allow v4 connections on v6 sockets + if biniptemp.family = af_inet6 then begin + if SetSocketOptions(fdhandlein, IPPROTO_IPV6,IPV6_V6ONLY,no,sizeof(no))=-1 then begin + writeln(IPPROTO_IPV6); + writeln(IPV6_V6ONLY); + raise ESocketException.create('unable to set IPV6_V6ONLY socket option error='+inttostr(socketerror)); + + end; + end; + {$else} + SetSockOpt(fdhandlein, SOL_SOCKET, SO_REUSEADDR, 'TRUE', Length('TRUE')); + {$endif} localaddr := addr; localport := port; bindsocket; @@ -419,108 +463,259 @@ begin if not udp then begin {!!! allow custom queue length? default 5} if listenqueue = 0 then listenqueue := 5; - If Not sockets.Listen(fdhandlein,listenqueue) Then raise esocketexception.create('unable to listen'); + If {$ifdef mswindows}winsock.listen{$else}system_listen{$endif}(fdhandlein,listenqueue)<>{$ifdef mswindows}0{$else}true{$endif} Then raise +esocketexception.create('unable to listen'); state := wsListening; end else begin + {$ifndef mswindows} + SetSocketOptions(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE')); + {$else} + SetSockOpt(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE')); + {$endif} state := wsconnected; end; + + {$ifdef secondlistener} + //listening on ::. try to listen on 0.0.0.0 as well for platforms which don't already do that + if addr = '::' then begin + secondlistener := tlsocket.create(nil); + secondlistener.proto := proto; + secondlistener.addr := '0.0.0.0'; + secondlistener.port := port; + if udp then begin + secondlistener.ondataavailable := secondaccepthandler; + end else begin + secondlistener.onsessionAvailable := secondaccepthandler; + end; + try + secondlistener.listen; + except + secondlistener.destroy; + secondlistener := nil; + end; + end; + {$endif} finally if state = wsclosed then begin if fdhandlein >= 0 then begin {one *can* get here without fd -beware} - rmasterclr(fdhandlein); - fdclose(fdhandlein); // we musnt leak file discriptors - fdreverse[fdhandlein] := nil; + eventcore.rmasterclr(fdhandlein); + myfdclose(fdhandlein); // we musnt leak file descriptors + eventcore.setfdreverse(fdhandlein,nil); fdhandlein := -1; end; end else begin - rmasterset(fdhandlein); + eventcore.rmasterset(fdhandlein,not udp); end; - if fdhandleout >= 0 then wmasterclr(fdhandleout); + if fdhandleout >= 0 then eventcore.wmasterclr(fdhandleout); end; + //writeln('listened on addr '+addr+':'+port+' '+proto+' using socket number ',fdhandlein); end; +{$ifdef secondlistener} +procedure tlsocket.internalclose(error:word); +begin + if assigned(secondlistener) then begin + secondlistener.destroy; + secondlistener := nil; + end; + inherited internalclose(error); +end; + +procedure tlsocket.secondaccepthandler; +begin + lastsessionfromsecond := true; + if udp then begin + ondataavailable(self,error); + end else begin + if assigned(onsessionavailable) then onsessionavailable(self,error); + end; +end; +{$endif} + function tlsocket.accept : longint; var - FromAddrSize : LongInt; // i don't realy know what to do with these at this - FromAddr : TInetSockAddr; // at this point time will tell :) + FromAddrSize : LongInt; // i don't really know what to do with these at this + FromAddr : TInetSockAddrV; // at this point time will tell :) + a,acceptlasterror:integer; begin - rmasterset(fdhandlein); + {$ifdef secondlistener} + if (lastsessionfromsecond) then begin + lastsessionfromsecond := false; + result := secondlistener.accept; + exit; + end; + {$endif} + FromAddrSize := Sizeof(FromAddr); - result := sockets.accept(fdhandlein,fromaddr,fromaddrsize); - if result = -1 then raise esocketexception.create('error '+inttostr(socketerror)+' while accepting'); - if result > absoloutemaxs then begin - fdclose(result); - result := -1; - raise esocketexception.create('file discriptor out of range'); + {$ifdef mswindows} + result := winsock.accept(fdhandlein,@fromaddr,@fromaddrsize); + {$else} + result := system_accept(fdhandlein,fromaddr,fromaddrsize); + {$endif} + + if (result = -1) then acceptlasterror := {$ifdef mswindows}getlasterror{$else}socketerror{$endif} else acceptlasterror := 0; + + //now we have accepted one request start monitoring for more again + eventcore.rmasterset(fdhandlein,true); + + if result = -1 then begin + raise esocketexception.create('error '+inttostr(acceptlasterror)+' while accepting'); + end; + if result > absolutemaxs then begin + myfdclose(result); + a := result; +{ result := -1;} + raise esocketexception.create('file descriptor out of range: '+inttostr(a)); + end; +end; + + +function tlsocket.sendto(dest:TInetSockAddrV;destlen:integer;data:pointer;len:integer):integer; +var + {$ifdef ipv6} + realdest : tinetsockaddrv; + biniptemp : tbinip; + {$endif} + destx : {$ifdef mswindows}winsock.pSockAddr{$else}pInetSockAddrV{$endif}; + +begin + {$ifdef secondlistener} + if assigned(secondlistener) then if (dest.inaddr.family = AF_INET) then begin + result := secondlistener.sendto(dest,destlen,data,len); + exit; + end; + {$endif} + {$ifdef ipv6} + if isv6socket then begin + biniptemp := inaddrvtobinip(dest); + converttov6(biniptemp); + destlen := makeinaddrv(biniptemp,inttostr(ntohs(dest.InAddr.port)),realdest); + destx := {$ifdef mswindows}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@realdest) + end else begin + destx := {$ifdef mswindows}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@dest) + end; + {$else} + destx := {$ifdef mswindows}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@dest); + {$endif} + + result := {$ifdef mswindows}winsock.sendto{$else}system_sendto{$endif}(self.fdhandleout,data^,len,0,destx^,destlen); +end; + + +function tlsocket.receivefrom(data:pointer;len:integer;var src:TInetSockAddrV;var srclen:integer):integer; +var + tempsrc:TInetSockAddrV; + tempsrclen:integer; + srcx : {$ifdef mswindows}winsock.TSockAddr{$else}TInetSockAddrV{$endif} absolute tempsrc; + biniptemp:tbinip; +begin + {$ifdef secondlistener} + if assigned(secondlistener) then if lastsessionfromsecond then begin + lastsessionfromsecond := false; + result := secondlistener.receivefrom(data,len,src,srclen); + exit; + end; + {$endif} + tempsrclen := sizeof(tempsrc); + result := recvfrom(self.fdhandlein,data^,len,0,srcx,tempsrclen); + + {$ifdef ipv6} + biniptemp := inaddrvtobinip(tempsrc); + if needconverttov4(biniptemp) then begin + converttov4(biniptemp); + tempsrclen := makeinaddrv(biniptemp,inttostr(ntohs(tempsrc.InAddr.port)),tempsrc); end; + {$endif} + + move(tempsrc,src,srclen); + srclen := tempsrclen; end; -function tlsocket.sendto; +procedure tlsocket.taskcallconnectionfailedhandler(wparam,lparam : longint); begin - result := sockets.sendto(self.fdhandleout,data^,len,0,dest,destlen); + connectionfailedhandler(wparam); end; -function tlsocket.receivefrom; +procedure tlsocket.connectionfailedhandler(error:word); begin - result := sockets.recvfrom(self.fdhandlein,data^,len,0,src,srclen); + if trymoreips then begin +// writeln('failed with error ',error); + connecttimeout.enabled := false; + destroying := true; + state := wsconnected; + self.internalclose(0); + destroying := false; + realconnect; + end else begin + state := wsconnected; + if assigned(onsessionconnected) then onsessionconnected(self,error); + self.internalclose(0); + recvq.del(maxlongint); + end; end; +procedure tlsocket.connectsuccesshandler; +begin + trymoreips := false; + connecttimeout.enabled := false; + if assigned(onsessionconnected) then onsessionconnected(self,0); +end; + + procedure tlsocket.handlefdtrigger(readtrigger,writetrigger:boolean); var tempbuf:array[0..receivebufsize-1] of byte; begin +// writeln('got a fd trigger, readtrigger=',readtrigger,' writetrigger=',writetrigger,' state=',integer(state)); if (state =wslistening) and readtrigger then begin { debugout('listening socket triggered on read');} - rmasterclr(fdhandlein); + eventcore.rmasterclr(fdhandlein); if assigned(onsessionAvailable) then onsessionAvailable(self,0); end; - if udp and readtrigger then begin + if dgram and readtrigger then begin if assigned(ondataAvailable) then ondataAvailable(self,0); {!!!test} exit; end; if (state =wsconnecting) and writetrigger then begin - // code for dealing with the reults of a non-blocking connect is + // code for dealing with the results of a non-blocking connect is // rather complex - // if just write is triggered it means connect suceeded - // if both read and write are suceededed it can mean 2 things - // 1: connect ok and data availible + // if just write is triggered it means connect succeeded + // if both read and write are triggered it can mean 2 things + // 1: connect ok and data available // 2: connect fail // to find out which you must read from the socket and look for errors - // there if we read successfully we drop through into the code for fireing + // there if we read successfully we drop through into the code for firing // the read event if not readtrigger then begin state := wsconnected; - if assigned(onsessionconnected) then onsessionconnected(self,0); + connectsuccesshandler; end else begin - numread := fdread(fdhandlein,tempbuf,sizeof(tempbuf)); + numread := myfdread(fdhandlein,tempbuf,sizeof(tempbuf)); if numread <> -1 then begin state := wsconnected; - if assigned(onsessionconnected) then onsessionconnected(self,0); + connectsuccesshandler; //connectread := true; recvq.add(@tempbuf,numread); end else begin - state := wsconnected; - if assigned(onsessionconnected) then onsessionconnected(self,linuxerror); -{ debugout('connect fail');} - self.internalclose(0); - recvq.del(maxlongint); + connectionfailedhandler({$ifdef mswindows}wsagetlasterror{$else}linuxerror{$endif}); + exit; end; // if things went well here we are now in the state wsconnected with data sitting in our receive buffer - // so we drop down into the processing for data availible + // so we drop down into the processing for data available end; if fdhandlein >= 0 then begin if state = wsconnected then begin - rmasterset(fdhandlein); + eventcore.rmasterset(fdhandlein,false); end else begin - rmasterclr(fdhandlein); + eventcore.rmasterclr(fdhandlein); end; end; if fdhandleout >= 0 then begin if sendq.size = 0 then begin //don't clear the bit in fdswmaster if data is in the sendq - wmasterclr(fdhandleout); + eventcore.wmasterclr(fdhandleout); end; end; @@ -532,12 +727,16 @@ constructor tlsocket.Create(AOwner: TComponent); begin inherited create(aowner); closehandles := true; + trymoreips := true; end; + function tlsocket.getpeername(var addr:tsockaddrin;addrlen:integer):integer; +var + addrx : {$ifdef mswindows}winsock.tsockaddr{$else}tsockaddrin{$endif} absolute addr; begin - result := sockets.getpeername(self.fdhandlein,addr,addrlen); + result := {$ifdef mswindows}winsock.getpeername{$else}system_getpeername{$endif}(self.fdhandlein,addrx,addrlen); end; procedure tlsocket.getxaddrbin(var binip:tbinip); @@ -547,17 +746,13 @@ var begin i := sizeof(addr); fillchar(addr,sizeof(addr),0); - sockets.getsocketname(self.fdhandlein,addr,i); - binip.family := addr.inaddr.family; - {$ifdef ipv6} - if addr.inaddr6.sin6_family = AF_INET6 then begin - binip.ip6 := addr.inaddr6.sin6_addr; - end else + {$ifdef mswindows} + winsock.getsockname(self.fdhandlein,psockaddr(@addr)^,i); + {$else} + getsocketname(self.fdhandlein,addr,i); {$endif} - begin - binip.ip := addr.inaddr.addr; - end; + binip := inaddrvtobinip(addr); converttov4(binip); end; @@ -568,21 +763,17 @@ var begin i := sizeof(addr); fillchar(addr,sizeof(addr),0); - sockets.getpeername(self.fdhandlein,addr,i); - - binip.family := addr.inaddr.family; - {$ifdef ipv6} - if addr.inaddr6.sin6_family = AF_INET6 then begin - binip.ip6 := addr.inaddr6.sin6_addr; - end else + {$ifdef mswindows} + winsock.getpeername(self.fdhandlein,psockaddr(@addr)^,i); + {$else} + system_getpeername(self.fdhandlein,addr,i); {$endif} - begin - binip.ip := addr.inaddr.addr; - end; + + binip := inaddrvtobinip(addr); converttov4(binip); end; -function tlsocket.getXaddr:string; +function tlsocket.getXaddr:thostname; var biniptemp:tbinip; begin @@ -591,7 +782,7 @@ begin if result = '' then result := 'error'; end; -function tlsocket.getpeeraddr:string; +function tlsocket.getpeeraddr:thostname; var biniptemp:tbinip; begin @@ -600,26 +791,52 @@ begin if result = '' then result := 'error'; end; -function tlsocket.getXport:string; +function tlsocket.getXport:ansistring; var - addr:tinetsockaddr; + addr:tinetsockaddrv; i:integer; begin i := sizeof(addr); - sockets.getsocketname(self.fdhandlein,addr,i); - i := htons(addr.port); - result := inttostr(i); + {$ifdef mswindows} + winsock.getsockname(self.fdhandlein,psockaddrin(@addr)^,i); + + {$else} + getsocketname(self.fdhandlein,addr,i); + + {$endif} + result := inttostr(htons(addr.InAddr.port)); end; -function tlsocket.getpeerport:string; +function tlsocket.getpeerport:ansistring; var - addr:tinetsockaddr; + addr:tinetsockaddrv; i:integer; begin i := sizeof(addr); - sockets.getpeername(self.fdhandlein,addr,i); - i := htons(addr.port); - result := inttostr(i); + {$ifdef mswindows} + winsock.getpeername(self.fdhandlein,psockaddrin(@addr)^,i); + + {$else} + system_getpeername(self.fdhandlein,addr,i); + + {$endif} + result := inttostr(htons(addr.InAddr.port)); end; +{$ifdef mswindows} + procedure tlsocket.myfdclose(fd : integer); + begin + closesocket(fd); + end; + function tlsocket.myfdwrite(fd: LongInt;const buf;size: LongInt):LongInt; + begin + result := winsock.send(fd,(@buf)^,size,0); + end; + function tlsocket.myfdread(fd: LongInt;var buf;size: LongInt):LongInt; + begin + result := winsock.recv(fd,buf,size,0); + end; +{$endif} + end. +