{$ifdef fpc}\r
{$mode delphi}\r
{$endif}\r
+\r
+{$include lcoreconfig.inc}\r
+\r
interface\r
uses\r
sysutils,\r
1: (S_un_w: SunW);\r
2: (S_addr: cardinal);\r
end;\r
- {$ifdef ipv6}\r
- {$ifdef ver1_0}\r
- cuint16=word;\r
- cuint32=dword;\r
- sa_family_t=word;\r
-\r
-\r
- TInetSockAddr6 = packed Record\r
- sin6_family : sa_family_t;\r
- sin6_port : cuint16;\r
- sin6_flowinfo : cuint32;\r
- sin6_addr : Tin6_addr;\r
- sin6_scope_id : cuint32;\r
- end;\r
- {$endif}\r
- {$endif}\r
- TinetSockAddrv = packed record\r
- case integer of\r
- 0: (InAddr:TInetSockAddr);\r
- {$ifdef ipv6}\r
- 1: (InAddr6:TInetSockAddr6);\r
- {$endif}\r
- end;\r
- Pinetsockaddrv = ^Tinetsockaddrv;\r
-\r
-\r
- type\r
- tsockaddrin=TInetSockAddr;\r
\r
type\r
TLsocket = class(tlasio)\r
//a: string;\r
\r
inAddr : TInetSockAddrV;\r
+\r
+ biniplist:tbiniplist;\r
+ trymoreips:boolean;\r
+ currentip:integer;\r
+ connecttimeout:tltimer;\r
+\r
{ inAddrSize:integer;}\r
\r
//host : THostentry ;\r
proto:string;\r
udp:boolean;\r
listenqueue:integer;\r
+ procedure connectionfailedhandler(error:word);\r
+ procedure connecttimeouthandler(sender:tobject);\r
+ procedure connectsuccesshandler;\r
function getaddrsize:integer;\r
procedure connect; virtual;\r
+ procedure realconnect;\r
procedure bindsocket;\r
procedure listen;\r
function accept : longint;\r
- function sendto(dest:TInetSockAddr;destlen:integer;data:pointer;len:integer):integer; virtual;\r
- function receivefrom(data:pointer;len:integer;var src:TInetSockAddr;var srclen:integer):integer; virtual;\r
+ function sendto(dest:TInetSockAddrV;destlen:integer;data:pointer;len:integer):integer; virtual;\r
+ function receivefrom(data:pointer;len:integer;var src:TInetSockAddrV;var srclen:integer):integer; virtual;\r
//procedure internalclose(error:word);override;\r
procedure handlefdtrigger(readtrigger,writetrigger:boolean); override;\r
function send(data:pointer;len:integer):integer;override;\r
\r
twsocket=tlsocket; {easy}\r
\r
-function htons(w:word):word;\r
-function htonl(i:integer):integer;\r
+\r
{!!!function longipdns(s:string):longint;}\r
\r
{$ifdef ipv6}\r
implementation\r
{$include unixstuff.inc}\r
\r
-function longip(s:string):longint;{$ifdef fpc}inline;{$endif}\r
-var\r
- l:longint;\r
- a,b:integer;\r
-\r
-function convertbyte(const s:string):integer;{$ifdef fpc}inline;{$endif}\r
-begin\r
- result := strtointdef(s,-1);\r
- if result < 0 then exit;\r
- if result > 255 then exit;\r
-\r
- {01 exception}\r
- if (result <> 0) and (s[1] = '0') then begin\r
- result := -1;\r
- exit;\r
- end;\r
-\r
- {+1 exception}\r
- if not (s[1] in ['0'..'9']) then begin\r
- result := -1;\r
- exit\r
- end;\r
-end;\r
-\r
-begin\r
- result := 0;\r
- a := pos('.',s);\r
- if a = 0 then exit;\r
- b := convertbyte(copy(s,1,a-1));if (b < 0) then exit;\r
- l := b shl 24;\r
- s := copy(s,a+1,256);\r
- a := pos('.',s);\r
- if a = 0 then exit;\r
- b := convertbyte(copy(s,1,a-1));if (b < 0) then exit;\r
- l := l or b shl 16;\r
- s := copy(s,a+1,256);\r
- a := pos('.',s);\r
- if a = 0 then exit;\r
- b := convertbyte(copy(s,1,a-1));if (b < 0) then exit;\r
- l := l or b shl 8;\r
- s := copy(s,a+1,256);\r
- b := convertbyte(copy(s,1,256));if (b < 0) then exit;\r
- l := l or b;\r
- result := l;\r
-end;\r
-\r
-(*!!!\r
-function longipdns(s:string):longint;\r
-var\r
- host : thostentry;\r
-begin\r
- if s = '0.0.0.0' then begin\r
- result := 0;\r
- end else begin\r
- result := longip(s);\r
- if result = 0 then begin\r
- if gethostbyname(s,host) then begin;\r
- result := htonl(Longint(Host.Addr));\r
- end;\r
- //writeln(inttohex(longint(host.addr),8))\r
- end;\r
- if result = 0 then begin\r
- if resolvehostbyname(s,host) then begin;\r
- result := htonl(Longint(Host.Addr));\r
- end;\r
- //writeln(inttohex(longint(host.addr),8))\r
- end;\r
- end;\r
-end;\r
-*)\r
-\r
-\r
-function htons(w:word):word;\r
-begin\r
- {$ifndef ENDIAN_BIG}\r
- result := ((w and $ff00) shr 8) or ((w and $ff) shl 8);\r
- {$else}\r
- result := w;\r
- {$endif}\r
-end;\r
-\r
-function htonl(i:integer):integer;\r
-begin\r
- {$ifndef ENDIAN_BIG}\r
- result := (i shr 24) or (i shr 8 and $ff00) or (i shl 8 and $ff0000) or (i shl 24 and $ff000000);\r
- {$else}\r
- result := i;\r
- {$endif}\r
-end;\r
\r
function tlsocket.getaddrsize:integer;\r
begin\r
- {$ifdef ipv6}\r
- if inaddr.inaddr.family = AF_INET6 then result := sizeof(tinetsockaddr6) else\r
- {$endif}\r
- result := sizeof(tinetsockaddr);\r
+ result := inaddrsize(inaddr);\r
end;\r
\r
-function makeinaddrv(addr,port:string;var inaddr:tinetsockaddrv):integer;\r
-var\r
- biniptemp:tbinip;\r
-begin\r
- result := 0;\r
- biniptemp := forwardlookup(addr,10);\r
- fillchar(inaddr,sizeof(inaddr),0);\r
- //writeln('converted address '+addr+' to binip '+ipbintostr(biniptemp));\r
- if biniptemp.family = AF_INET then begin\r
- inAddr.InAddr.family:=AF_INET;\r
- inAddr.InAddr.port:=htons(strtointdef(port,0));\r
- inAddr.InAddr.addr:=biniptemp.ip;\r
- result := sizeof(tinetsockaddr);\r
- end else\r
- {$ifdef ipv6}\r
- if biniptemp.family = AF_INET6 then begin\r
- inAddr.InAddr6.sin6_family:=AF_INET6;\r
- inAddr.InAddr6.sin6_port:=htons(strtointdef(port,0));\r
- inAddr.InAddr6.sin6_addr:=biniptemp.ip6;\r
- result := sizeof(tinetsockaddr6);\r
- end else\r
- {$endif}\r
- raise esocketexception.create('unable to resolve address: '+addr);\r
-end;\r
\r
-procedure tlsocket.connect;\r
+procedure tlsocket.realconnect;\r
var\r
a:integer;\r
-begin\r
- if state <> wsclosed then close;\r
- //prevtime := 0;\r
- makeinaddrv(addr,port,inaddr);\r
\r
+begin\r
+// writeln('trying to connect to ',ipbintostr(biniplist_get(biniplist,currentip)),'#',port);\r
+ makeinaddrv(biniplist_get(biniplist,currentip),port,inaddr);\r
+ inc(currentip);\r
+ if (currentip >= biniplist_getcount(biniplist)) then trymoreips := false;\r
udp := uppercase(proto) = 'UDP';\r
if udp then a := SOCK_DGRAM else a := SOCK_STREAM;\r
a := Socket(inaddr.inaddr.family,a,0);\r
-\r
//writeln(ord(inaddr.inaddr.family));\r
if a = -1 then begin\r
lasterror := {$ifdef win32}getlasterror{$else}socketerror{$endif};\r
{$endif}\r
state := wsconnected;\r
if assigned(onsessionconnected) then onsessionconnected(self,0);\r
+\r
+ eventcore.rmasterset(fdhandlein,false);\r
+ eventcore.wmasterclr(fdhandleout);\r
end else begin\r
state :=wsconnecting;\r
{$ifdef win32}\r
{$else}\r
sockets.Connect(fdhandlein,inADDR,getaddrsize);\r
{$endif}\r
- end;\r
- eventcore.rmasterset(fdhandlein,false);\r
- if udp then begin\r
- eventcore.wmasterclr(fdhandleout);\r
- end else begin\r
+ eventcore.rmasterset(fdhandlein,false);\r
eventcore.wmasterset(fdhandleout);\r
+ if trymoreips then connecttimeout.enabled := true;\r
end;\r
//sendq := '';\r
except\r
raise; //reraise the exception\r
end;\r
end;\r
+\r
+end;\r
+\r
+procedure tlsocket.connecttimeouthandler(sender:tobject);\r
+begin\r
+ connecttimeout.enabled := false;\r
+ destroying := true; //hack to not cause handler to trigger\r
+ internalclose(0);\r
+ destroying := false;\r
+ realconnect;\r
+end;\r
+\r
+procedure tlsocket.connect;\r
+var\r
+ a:integer;\r
+ ip:tbinip;\r
+begin\r
+ if state <> wsclosed then close;\r
+ //prevtime := 0;\r
+\r
+ biniplist := forwardlookuplist(addr,0);\r
+ if biniplist_getcount(biniplist) = 0 then raise exception.create('unable to resolve '+addr);\r
+\r
+ //makeinaddrv(addr,port,inaddr);\r
+\r
+ currentip := 0;\r
+ if not assigned(connecttimeout) then begin\r
+ connecttimeout := tltimer.create(self);\r
+ connecttimeout.Tag := integer(self);\r
+ connecttimeout.ontimer := connecttimeouthandler;\r
+ connecttimeout.interval := 2500;\r
+ connecttimeout.enabled := false;\r
+ end;\r
+ realconnect;\r
end;\r
\r
procedure tlsocket.sendstr(const str : string);\r
function tlsocket.send(data:pointer;len:integer):integer;\r
begin\r
if udp then begin\r
- //writeln('sending to '+inttohex(inaddr.inaddr.addr,8));\r
- result := sendto(inaddr.inaddr,getaddrsize,data,len)\r
-;\r
- //writeln('send result',result);\r
- //writeln('errno',errno);\r
+// writeln('sending to '+ipbintostr(inaddrvtobinip(inaddr)),' ',htons(inaddr.inaddr.port),' ',len,' bytes');\r
+ result := sendto(inaddr,getaddrsize,data,len);\r
+\r
+// writeln('send result ',result);\r
+// writeln('errno',errno);\r
end else begin\r
result := inherited send(data,len);\r
end;\r
end;\r
//gethostbyname(localaddr,host);\r
\r
- inaddrtempsize := makeinaddrv(localaddr,localport,inaddrtemp);\r
+ inaddrtempsize := makeinaddrv(forwardlookup(localaddr,0),localport,inaddrtemp);\r
\r
If Bind(fdhandlein,inaddrtempx,inaddrtempsize)<> {$ifdef win32}0{$else}true{$endif} Then begin\r
state := wsclosed;\r
fdhandlein := -1;\r
end;\r
end else begin\r
- eventcore.rmasterset(fdhandlein,true);\r
+ eventcore.rmasterset(fdhandlein,not udp);\r
end;\r
if fdhandleout >= 0 then eventcore.wmasterclr(fdhandleout);\r
end;\r
- //writeln('listened on addr '+addr+':'+port+' '+proto+' using socket number ',fdhandlein); \r
+ //writeln('listened on addr '+addr+':'+port+' '+proto+' using socket number ',fdhandlein);\r
end;\r
\r
function tlsocket.accept : longint;\r
var\r
FromAddrSize : LongInt; // i don't realy know what to do with these at this\r
FromAddr : TInetSockAddrV; // at this point time will tell :)\r
+ a:integer;\r
begin\r
\r
FromAddrSize := Sizeof(FromAddr);\r
//now we have accepted one request start monitoring for more again\r
eventcore.rmasterset(fdhandlein,true);\r
\r
- if result = -1 then raise esocketexception.create('error '+inttostr({$ifdef win32}getlasterror{$else}socketerror{$endif})+' while accepting');\r
+ if result = -1 then begin\r
+ raise esocketexception.create('error '+inttostr({$ifdef win32}getlasterror{$else}socketerror{$endif})+' while accepting');\r
+ end;\r
if result > absoloutemaxs then begin\r
myfdclose(result);\r
+ a := result;\r
result := -1;\r
- raise esocketexception.create('file discriptor out of range');\r
+ raise esocketexception.create('file discriptor out of range: '+inttostr(a));\r
end;\r
end;\r
\r
-function tlsocket.sendto(dest:TInetSockAddr;destlen:integer;data:pointer;len:integer):integer;\r
+function tlsocket.sendto(dest:TInetSockAddrV;destlen:integer;data:pointer;len:integer):integer;\r
var\r
- destx : {$ifdef win32}winsock.TSockAddr{$else}TInetSockAddr{$endif} absolute dest;\r
+ destx : {$ifdef win32}winsock.TSockAddr{$else}TInetSockAddrV{$endif} absolute dest;\r
begin\r
result := {$ifdef win32}winsock{$else}sockets{$endif}.sendto(self.fdhandleout,data^,len,0,destx,destlen);\r
end;\r
\r
-function tlsocket.receivefrom(data:pointer;len:integer;var src:TInetSockAddr;var srclen:integer):integer;\r
+function tlsocket.receivefrom(data:pointer;len:integer;var src:TInetSockAddrV;var srclen:integer):integer;\r
var\r
- srcx : {$ifdef win32}winsock.TSockAddr{$else}TInetSockAddr{$endif} absolute src;\r
+ srcx : {$ifdef win32}winsock.TSockAddr{$else}TInetSockAddrV{$endif} absolute src;\r
begin\r
result := {$ifdef win32}winsock{$else}sockets{$endif}.recvfrom(self.fdhandlein,data^,len,0,srcx,srclen);\r
end;\r
\r
+procedure tlsocket.connectionfailedhandler(error:word);\r
+begin\r
+ if trymoreips then begin\r
+// writeln('failed with error ',error);\r
+ connecttimeout.enabled := false;\r
+ destroying := true;\r
+ state := wsconnected;\r
+ self.internalclose(0);\r
+ destroying := false;\r
+ realconnect;\r
+ end else begin\r
+ state := wsconnected;\r
+ if assigned(onsessionconnected) then onsessionconnected(self,error);\r
+ self.internalclose(0);\r
+ recvq.del(maxlongint);\r
+ end;\r
+end;\r
+\r
+procedure tlsocket.connectsuccesshandler;\r
+begin\r
+ trymoreips := false;\r
+ connecttimeout.enabled := false;\r
+ if assigned(onsessionconnected) then onsessionconnected(self,0);\r
+end;\r
+\r
+\r
procedure tlsocket.handlefdtrigger(readtrigger,writetrigger:boolean);\r
var\r
tempbuf:array[0..receivebufsize-1] of byte;\r
begin\r
- //writeln('got a fd trigger, readtrigger=',readtrigger,' writetrigger=',writetrigger);\r
+// writeln('got a fd trigger, readtrigger=',readtrigger,' writetrigger=',writetrigger,' state=',integer(state));\r
if (state =wslistening) and readtrigger then begin\r
{ debugout('listening socket triggered on read');}\r
eventcore.rmasterclr(fdhandlein);\r
// the read event\r
if not readtrigger then begin\r
state := wsconnected;\r
- if assigned(onsessionconnected) then onsessionconnected(self,0);\r
+ connectsuccesshandler;\r
end else begin\r
numread := myfdread(fdhandlein,tempbuf,sizeof(tempbuf));\r
if numread <> -1 then begin\r
state := wsconnected;\r
- if assigned(onsessionconnected) then onsessionconnected(self,0);\r
+ connectsuccesshandler;\r
//connectread := true;\r
recvq.add(@tempbuf,numread);\r
end else begin\r
- state := wsconnected;\r
- if assigned(onsessionconnected) then onsessionconnected(self,{$ifdef win32}wsagetlasterror{$else}linuxerror{$endif});\r
-{ debugout('connect fail');}\r
- self.internalclose(0);\r
- recvq.del(maxlongint);\r
+ connectionfailedhandler({$ifdef win32}wsagetlasterror{$else}linuxerror{$endif});\r
+ exit;\r
end;\r
// if things went well here we are now in the state wsconnected with data sitting in our receive buffer\r
// so we drop down into the processing for data availible\r
begin\r
inherited create(aowner);\r
closehandles := true;\r
+ trymoreips := true;\r
end;\r
\r
\r