X-Git-Url: http://www.lcore.org/git/lcore.git/blobdiff_plain/82d4e52fba54be50d719f6185ec381f2dc87fc6c..85ef2ce64f0cc31a063fccea69fdcc7281d51548:/lsocket.pas?ds=sidebyside diff --git a/lsocket.pas b/lsocket.pas index c37a66a..2eb4dbd 100755 --- a/lsocket.pas +++ b/lsocket.pas @@ -54,6 +54,13 @@ interface sockets, {$endif} 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; @@ -94,6 +101,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 +118,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; @@ -129,14 +142,6 @@ type twsocket=tlsocket; {easy} -{!!!function longipdns(s:string):longint;} - -{$ifdef ipv6} -const - v4listendefault:boolean=false; -{$endif} - - const TCP_NODELAY=1; IPPROTO_TCP=6; @@ -260,7 +265,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; @@ -288,7 +299,7 @@ begin If Bind(fdhandlein,inaddrtempx,inaddrtempsize)<> {$ifdef win32}0{$else}true{$endif} Then begin state := wsclosed; lasterror := {$ifdef win32}getlasterror{$else}socketerror{$endif}; - raise ESocketException.create('unable to bind, error '+inttostr(lasterror)); + raise ESocketException.create('unable to bind on address '+localaddr+'#'+localport+', error '+inttostr(lasterror)); end; state := wsbound; end; @@ -329,6 +340,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 @@ -357,6 +369,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 @@ -374,12 +407,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} @@ -405,14 +466,42 @@ 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; function tlsocket.receivefrom(data:pointer;len:integer;var src:TInetSockAddrV;var srclen:integer):integer; var - srcx : {$ifdef win32}winsock.TSockAddr{$else}TInetSockAddrV{$endif} absolute src; + tempsrc:TInetSockAddrV; + tempsrclen:integer; + srcx : {$ifdef win32}winsock.TSockAddr{$else}TInetSockAddrV{$endif} absolute tempsrc; + biniptemp:tbinip; begin - result := {$ifdef win32}winsock{$else}sockets{$endif}.recvfrom(self.fdhandlein,data^,len,0,srcx,srclen); + {$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 := {$ifdef win32}winsock{$else}sockets{$endif}.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; procedure tlsocket.connectionfailedhandler(error:word); @@ -529,15 +618,7 @@ begin {$else} sockets.getsocketname(self.fdhandlein,addr,i); {$endif} - binip.family := addr.inaddr.family; - {$ifdef ipv6} - if addr.inaddr6.sin6_family = AF_INET6 then begin - binip.ip6 := addr.inaddr6.sin6_addr; - end else - {$endif} - begin - binip.ip := addr.inaddr.addr; - end; + binip := inaddrvtobinip(addr); converttov4(binip); end; @@ -554,15 +635,7 @@ begin sockets.getpeername(self.fdhandlein,addr,i); {$endif} - binip.family := addr.inaddr.family; - {$ifdef ipv6} - if addr.inaddr6.sin6_family = AF_INET6 then begin - binip.ip6 := addr.inaddr6.sin6_addr; - end else - {$endif} - begin - binip.ip := addr.inaddr.addr; - end; + binip := inaddrvtobinip(addr); converttov4(binip); end;