X-Git-Url: http://www.lcore.org/git/lcore.git/blobdiff_plain/4782a5c5afee47721cc617daa40dd29828342c2b..808a50cfd5b22c3503854d0d71075caec07c53cd:/lsocket.pas?ds=inline diff --git a/lsocket.pas b/lsocket.pas index 58f157d..e56a25d 100755 --- a/lsocket.pas +++ b/lsocket.pas @@ -36,6 +36,9 @@ unit lsocket; {$ifdef fpc} {$mode delphi} {$endif} + +{$include lcoreconfig.inc} + interface uses sysutils, @@ -66,34 +69,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; - Pinetsockaddrv = ^Tinetsockaddrv; - - - type - tsockaddrin=TInetSockAddr; type TLsocket = class(tlasio) @@ -101,6 +76,12 @@ type //a: string; inAddr : TInetSockAddrV; + + biniplist:tbiniplist; + trymoreips:boolean; + currentip:integer; + connecttimeout:tltimer; + { inAddrSize:integer;} //host : THostentry ; @@ -113,13 +94,17 @@ type proto:string; udp:boolean; listenqueue:integer; + procedure connectionfailedhandler(error:word); + procedure connecttimeouthandler(sender:tobject); + procedure connectsuccesshandler; 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; + 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; @@ -143,8 +128,7 @@ type twsocket=tlsocket; {easy} -function htons(w:word):word; -function htonl(i:integer):integer; + {!!!function longipdns(s:string):longint;} {$ifdef ipv6} @@ -160,141 +144,25 @@ const implementation {$include unixstuff.inc} -function longip(s:string):longint;{$ifdef fpc}inline;{$endif} -var - l:longint; - a,b:integer; - -function convertbyte(const s:string):integer;{$ifdef fpc}inline;{$endif} -begin - result := strtointdef(s,-1); - if result < 0 then exit; - if result > 255 then exit; - - {01 exception} - if (result <> 0) and (s[1] = '0') then begin - result := -1; - exit; - end; - - {+1 exception} - if not (s[1] in ['0'..'9']) then begin - result := -1; - exit - 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 longipdns(s:string):longint; -var - host : thostentry; -begin - if s = '0.0.0.0' then begin - result := 0; - 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; - end; -end; -*) - - -function htons(w:word):word; -begin - {$ifndef ENDIAN_BIG} - result := ((w and $ff00) shr 8) or ((w and $ff) shl 8); - {$else} - result := w; - {$endif} -end; - -function htonl(i:integer):integer; -begin - {$ifndef ENDIAN_BIG} - 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); + result := inaddrsize(inaddr); 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); - //writeln('converted address '+addr+' to binip '+ipbintostr(biniptemp)); - 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; +procedure tlsocket.realconnect; var a:integer; -begin - if state <> wsclosed then close; - //prevtime := 0; - makeinaddrv(addr,port,inaddr); +begin +// 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); - //writeln(ord(inaddr.inaddr.family)); if a = -1 then begin lasterror := {$ifdef win32}getlasterror{$else}socketerror{$endif}; @@ -309,6 +177,9 @@ begin {$endif} state := wsconnected; if assigned(onsessionconnected) then onsessionconnected(self,0); + + eventcore.rmasterset(fdhandlein,false); + eventcore.wmasterclr(fdhandleout); end else begin state :=wsconnecting; {$ifdef win32} @@ -317,12 +188,9 @@ begin {$else} sockets.Connect(fdhandlein,inADDR,getaddrsize); {$endif} - end; - eventcore.rmasterset(fdhandlein,false); - if udp then begin - eventcore.wmasterclr(fdhandleout); - end else begin + eventcore.rmasterset(fdhandlein,false); eventcore.wmasterset(fdhandleout); + if trymoreips then connecttimeout.enabled := true; end; //sendq := ''; except @@ -331,6 +199,40 @@ begin raise; //reraise the exception end; end; + +end; + +procedure tlsocket.connecttimeouthandler(sender:tobject); +begin + connecttimeout.enabled := false; + destroying := true; //hack to not cause handler to trigger + internalclose(0); + destroying := false; + realconnect; +end; + +procedure tlsocket.connect; +var + a:integer; + ip:tbinip; +begin + if state <> wsclosed then close; + //prevtime := 0; + + 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.Tag := integer(self); + connecttimeout.ontimer := connecttimeouthandler; + connecttimeout.interval := 2500; + connecttimeout.enabled := false; + end; + realconnect; end; procedure tlsocket.sendstr(const str : string); @@ -345,11 +247,11 @@ end; function tlsocket.send(data:pointer;len:integer):integer; begin if udp then begin - //writeln('sending to '+inttohex(inaddr.inaddr.addr,8)); - result := sendto(inaddr.inaddr,getaddrsize,data,len) -; - //writeln('send result',result); - //writeln('errno',errno); +// 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; @@ -382,7 +284,7 @@ begin end; //gethostbyname(localaddr,host); - inaddrtempsize := makeinaddrv(localaddr,localport,inaddrtemp); + inaddrtempsize := makeinaddrv(forwardlookup(localaddr,0),localport,inaddrtemp); If Bind(fdhandlein,inaddrtempx,inaddrtempsize)<> {$ifdef win32}0{$else}true{$endif} Then begin state := wsclosed; @@ -466,17 +368,18 @@ begin fdhandlein := -1; end; end else begin - eventcore.rmasterset(fdhandlein,true); + eventcore.rmasterset(fdhandlein,not udp); end; if fdhandleout >= 0 then eventcore.wmasterclr(fdhandleout); end; - //writeln('listened on addr '+addr+':'+port+' '+proto+' using socket number ',fdhandlein); + //writeln('listened on addr '+addr+':'+port+' '+proto+' using socket number ',fdhandlein); end; 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 FromAddrSize := Sizeof(FromAddr); @@ -488,33 +391,62 @@ begin //now we have accepted one request start monitoring for more again eventcore.rmasterset(fdhandlein,true); - if result = -1 then raise esocketexception.create('error '+inttostr({$ifdef win32}getlasterror{$else}socketerror{$endif})+' while accepting'); + if result = -1 then begin + raise esocketexception.create('error '+inttostr({$ifdef win32}getlasterror{$else}socketerror{$endif})+' while accepting'); + end; if result > absoloutemaxs then begin myfdclose(result); + a := result; result := -1; - raise esocketexception.create('file discriptor out of range'); + raise esocketexception.create('file discriptor out of range: '+inttostr(a)); end; end; -function tlsocket.sendto(dest:TInetSockAddr;destlen:integer;data:pointer;len:integer):integer; +function tlsocket.sendto(dest:TInetSockAddrV;destlen:integer;data:pointer;len:integer):integer; var - destx : {$ifdef win32}winsock.TSockAddr{$else}TInetSockAddr{$endif} absolute dest; + destx : {$ifdef win32}winsock.TSockAddr{$else}TInetSockAddrV{$endif} absolute dest; begin 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:TInetSockAddr;var srclen:integer):integer; +function tlsocket.receivefrom(data:pointer;len:integer;var src:TInetSockAddrV;var srclen:integer):integer; var - srcx : {$ifdef win32}winsock.TSockAddr{$else}TInetSockAddr{$endif} absolute src; + srcx : {$ifdef win32}winsock.TSockAddr{$else}TInetSockAddrV{$endif} absolute src; begin result := {$ifdef win32}winsock{$else}sockets{$endif}.recvfrom(self.fdhandlein,data^,len,0,srcx,srclen); end; +procedure tlsocket.connectionfailedhandler(error:word); +begin + 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); +// 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');} eventcore.rmasterclr(fdhandlein); @@ -537,20 +469,17 @@ begin // the read event if not readtrigger then begin state := wsconnected; - if assigned(onsessionconnected) then onsessionconnected(self,0); + connectsuccesshandler; end else begin 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,{$ifdef win32}wsagetlasterror{$else}linuxerror{$endif}); -{ debugout('connect fail');} - self.internalclose(0); - recvq.del(maxlongint); + connectionfailedhandler({$ifdef win32}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 @@ -577,6 +506,7 @@ constructor tlsocket.Create(AOwner: TComponent); begin inherited create(aowner); closehandles := true; + trymoreips := true; end;