X-Git-Url: http://www.lcore.org/git/lcore.git/blobdiff_plain/6cb6b7ede2d178e03fa817bc28474c175f5a93b9..baf753e54d73673524de916757e66ef8c485bc0d:/lsocket.pas?ds=inline diff --git a/lsocket.pas b/lsocket.pas index e56a25d..3b2d918 100755 --- a/lsocket.pas +++ b/lsocket.pas @@ -54,6 +54,14 @@ interface sockets, {$endif} classes,{pgdebugout,}pgtypes,lcore,fd_utils,binipstuff,dnssync; + +{$ifdef ipv6} +{$define secondlistener} +const + v4listendefault:boolean=false; +{$endif} + + type sunB = packed record s_b1, s_b2, s_b3, s_b4: byte; @@ -94,6 +102,12 @@ type proto:string; udp:boolean; listenqueue:integer; + {$ifdef secondlistener} + secondlistener:tlsocket; + lastsessionfromsecond:boolean; + procedure secondaccepthandler(sender:tobject;error:word); + procedure internalclose(error:word);override; + {$endif} procedure connectionfailedhandler(error:word); procedure connecttimeouthandler(sender:tobject); procedure connectsuccesshandler; @@ -105,7 +119,7 @@ type function accept : longint; 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 internalclose(error:word);override; + procedure handlefdtrigger(readtrigger,writetrigger:boolean); override; function send(data:pointer;len:integer):integer;override; procedure sendstr(const str : string);override; @@ -131,12 +145,6 @@ type {!!!function longipdns(s:string):longint;} -{$ifdef ipv6} -const - v4listendefault:boolean=false; -{$endif} - - const TCP_NODELAY=1; IPPROTO_TCP=6; @@ -218,8 +226,7 @@ var begin if state <> wsclosed then close; //prevtime := 0; - - biniplist := forwardlookuplist(addr,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); @@ -261,7 +268,13 @@ end; function tlsocket.receive(Buf:Pointer;BufSize:integer):integer; begin if udp then begin - result := myfdread(self.fdhandlein,buf^,bufsize); + {$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; @@ -321,7 +334,7 @@ begin {$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); fdhandlein := socket(biniptemp.family,socktype,0); {$ifdef ipv6} @@ -330,6 +343,7 @@ begin fdhandlein := socket(AF_INET,socktype,0); end; {$endif} + if fdhandlein = -1 then raise ESocketException.create('unable to create socket'); dupnowatch(fdhandlein); // sets up maxs and copies handle to fdhandleout among other things //eventcore.setfdreverse(fdhandlein,self); //already taken care of by dup @@ -358,6 +372,27 @@ begin {$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 @@ -375,12 +410,40 @@ begin //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 : TInetSockAddrV; // at this point time will tell :) a:integer; begin + {$ifdef secondlistener} + if (lastsessionfromsecond) then begin + lastsessionfromsecond := false; + result := secondlistener.accept; + exit; + end; + {$endif} FromAddrSize := Sizeof(FromAddr); {$ifdef win32} @@ -406,6 +469,12 @@ function tlsocket.sendto(dest:TInetSockAddrV;destlen:integer;data:pointer;len:in var destx : {$ifdef win32}winsock.TSockAddr{$else}TInetSockAddrV{$endif} absolute dest; 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} result := {$ifdef win32}winsock{$else}sockets{$endif}.sendto(self.fdhandleout,data^,len,0,destx,destlen); end; @@ -413,6 +482,13 @@ function tlsocket.receivefrom(data:pointer;len:integer;var src:TInetSockAddrV;va var srcx : {$ifdef win32}winsock.TSockAddr{$else}TInetSockAddrV{$endif} absolute src; begin + {$ifdef secondlistener} + if assigned(secondlistener) then if lastsessionfromsecond then begin + lastsessionfromsecond := false; + result := secondlistener.receivefrom(data,len,src,srclen); + exit; + end; + {$endif} result := {$ifdef win32}winsock{$else}sockets{$endif}.recvfrom(self.fdhandlein,data^,len,0,srcx,srclen); end;