X-Git-Url: http://www.lcore.org/git/lcore.git/blobdiff_plain/82d4e52fba54be50d719f6185ec381f2dc87fc6c..5ba06ea7bd30494fe55f81a3bc07c8d1eb3702e0:/lsocket.pas diff --git a/lsocket.pas b/lsocket.pas index c37a66a..ddee93e 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; @@ -92,11 +99,14 @@ type localaddr:string; localport:string; proto:string; - udp:boolean; + udp,dgram:boolean; listenqueue:integer; - procedure connectionfailedhandler(error:word); - procedure connecttimeouthandler(sender:tobject); - procedure connectsuccesshandler; + {$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; @@ -105,7 +115,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; @@ -118,6 +128,14 @@ type function getXport:string; virtual; function getpeerport:string; virtual; constructor Create(AOwner: TComponent); override; + + //this one has to be kept public for now because lcorewsaasyncselect calls it + procedure connectionfailedhandler(error:word); + private + procedure taskcallconnectionfailedhandler(wparam,lparam : longint); + + procedure connecttimeouthandler(sender:tobject); + procedure connectsuccesshandler; {$ifdef win32} procedure myfdclose(fd : integer); override; function myfdwrite(fd: LongInt;const buf;size: LongInt):LongInt; override; @@ -129,14 +147,6 @@ type twsocket=tlsocket; {easy} -{!!!function longipdns(s:string):longint;} - -{$ifdef ipv6} -const - v4listendefault:boolean=false; -{$endif} - - const TCP_NODELAY=1; IPPROTO_TCP=6; @@ -153,27 +163,51 @@ end; procedure tlsocket.realconnect; var - a:integer; - + a,b:integer; begin -// writeln('trying to connect to ',ipbintostr(biniplist_get(biniplist,currentip)),'#',port); + //writeln('trying to connect to ',ipbintostr(biniplist_get(biniplist,currentip)),'#',port); makeinaddrv(biniplist_get(biniplist,currentip),port,inaddr); inc(currentip); if (currentip >= biniplist_getcount(biniplist)) then trymoreips := false; - udp := uppercase(proto) = 'UDP'; - if udp then a := SOCK_DGRAM else a := SOCK_STREAM; - a := Socket(inaddr.inaddr.family,a,0); + + 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 + raise ESocketException.create('unrecognised protocol'); + end; + + a := Socket(inaddr.inaddr.family,a,b); //writeln(ord(inaddr.inaddr.family)); if a = -1 then begin - lasterror := {$ifdef win32}getlasterror{$else}socketerror{$endif}; - 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 win32}wsagetlasterror{$else}socketerror{$endif},0); + exit; end; try dup(a); bindsocket; - if udp then begin + if dgram then begin {$ifndef win32} 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); @@ -226,7 +260,6 @@ begin currentip := 0; if not assigned(connecttimeout) then begin connecttimeout := tltimer.create(self); - connecttimeout.Tag := integer(self); connecttimeout.ontimer := connecttimeouthandler; connecttimeout.interval := 2500; connecttimeout.enabled := false; @@ -236,7 +269,7 @@ end; procedure tlsocket.sendstr(const str : string); begin - if udp then begin + if dgram then begin send(@str[1],length(str)) end else begin inherited sendstr(str); @@ -245,7 +278,7 @@ end; function tlsocket.send(data:pointer;len:integer):integer; begin - if udp then begin + if dgram then begin // writeln('sending to '+ipbintostr(inaddrvtobinip(inaddr)),' ',htons(inaddr.inaddr.port),' ',len,' bytes'); result := sendto(inaddr,getaddrsize,data,len); @@ -259,8 +292,14 @@ end; function tlsocket.receive(Buf:Pointer;BufSize:integer):integer; begin - if udp then begin - result := myfdread(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; @@ -282,13 +321,12 @@ begin localaddr := '0.0.0.0'; end; //gethostbyname(localaddr,host); - inaddrtempsize := makeinaddrv(forwardlookup(localaddr,0),localport,inaddrtemp); 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; @@ -309,7 +347,10 @@ var 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 @@ -329,7 +370,8 @@ begin fdhandlein := socket(AF_INET,socktype,0); end; {$endif} - if fdhandlein = -1 then raise ESocketException.create('unable to create socket'); + + if fdhandlein = -1 then raise ESocketException.create('unable to create socket'{$ifdef win32}+' 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 @@ -349,14 +391,38 @@ begin if not udp then begin {!!! allow custom queue length? default 5} if listenqueue = 0 then listenqueue := 5; - If {$ifdef win32}winsock{$else}sockets{$endif}.Listen(fdhandlein,listenqueue)<>{$ifdef win32}0{$else}true{$endif} Then raise esocketexception.create('unable to listen'); + If {$ifdef win32}winsock{$else}sockets{$endif}.Listen(fdhandlein,listenqueue)<>{$ifdef win32}0{$else}true{$endif} Then raise +esocketexception.create('unable to listen'); state := wsListening; end else begin {$ifndef win32} 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 @@ -374,12 +440,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 +499,47 @@ 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.taskcallconnectionfailedhandler(wparam,lparam : longint); +begin + connectionfailedhandler(wparam); end; procedure tlsocket.connectionfailedhandler(error:word); @@ -451,7 +578,7 @@ begin 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; @@ -529,15 +656,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 +673,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;