sockets,\r
{$endif}\r
classes,{pgdebugout,}pgtypes,lcore,fd_utils,binipstuff,dnssync;\r
+\r
+{$ifdef ipv6}\r
+const\r
+ v4listendefault:boolean=false;\r
+{$endif}\r
+\r
+\r
type\r
sunB = packed record\r
s_b1, s_b2, s_b3, s_b4: byte;\r
proto:string;\r
udp:boolean;\r
listenqueue:integer;\r
+ {$ifdef secondlistener}\r
+ secondlistener:tlsocket;\r
+ lastsessionfromsecond:boolean;\r
+ procedure secondaccepthandler(sender:tobject;error:word);\r
+ procedure internalclose(error:word);override;\r
+ {$endif}\r
procedure connectionfailedhandler(error:word);\r
procedure connecttimeouthandler(sender:tobject);\r
procedure connectsuccesshandler;\r
function accept : longint;\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
+\r
procedure handlefdtrigger(readtrigger,writetrigger:boolean); override;\r
function send(data:pointer;len:integer):integer;override;\r
procedure sendstr(const str : string);override;\r
twsocket=tlsocket; {easy}\r
\r
\r
-{!!!function longipdns(s:string):longint;}\r
-\r
-{$ifdef ipv6}\r
-const\r
- v4listendefault:boolean=false;\r
-{$endif}\r
-\r
-\r
const\r
TCP_NODELAY=1;\r
IPPROTO_TCP=6;\r
begin\r
if state <> wsclosed then close;\r
//prevtime := 0;\r
-\r
- biniplist := forwardlookuplist(addr,0);\r
+ if isbiniplist(addr) then biniplist := addr else 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
function tlsocket.receive(Buf:Pointer;BufSize:integer):integer;\r
begin\r
if udp then begin\r
- result := myfdread(self.fdhandlein,buf^,bufsize);\r
+ {$ifdef secondlistener}\r
+ if lastsessionfromsecond then begin\r
+ result := secondlistener.receive(buf,bufsize);\r
+ lastsessionfromsecond := false;\r
+ end else\r
+ {$endif}\r
+ result := myfdread(self.fdhandlein,buf^,bufsize);\r
end else begin\r
result := inherited receive(buf,bufsize);\r
end;\r
{$endif}\r
addr := '0.0.0.0';\r
end;\r
- biniptemp := forwardlookup(addr,10);\r
+ if isbiniplist(addr) then biniptemp := biniplist_get(addr,0) else biniptemp := forwardlookup(addr,10);\r
addr := ipbintostr(biniptemp);\r
fdhandlein := socket(biniptemp.family,socktype,0);\r
{$ifdef ipv6}\r
fdhandlein := socket(AF_INET,socktype,0);\r
end;\r
{$endif}\r
+\r
if fdhandlein = -1 then raise ESocketException.create('unable to create socket');\r
dupnowatch(fdhandlein); // sets up maxs and copies handle to fdhandleout among other things\r
//eventcore.setfdreverse(fdhandlein,self); //already taken care of by dup\r
{$endif}\r
state := wsconnected;\r
end;\r
+\r
+ {$ifdef secondlistener}\r
+ //listening on ::. try to listen on 0.0.0.0 as well for platforms which don't already do that\r
+ if addr = '::' then begin\r
+ secondlistener := tlsocket.create(nil);\r
+ secondlistener.proto := proto;\r
+ secondlistener.addr := '0.0.0.0';\r
+ secondlistener.port := port;\r
+ if udp then begin\r
+ secondlistener.ondataavailable := secondaccepthandler;\r
+ end else begin\r
+ secondlistener.onsessionAvailable := secondaccepthandler;\r
+ end;\r
+ try\r
+ secondlistener.listen;\r
+ except\r
+ secondlistener.destroy;\r
+ secondlistener := nil;\r
+ end;\r
+ end;\r
+ {$endif}\r
finally\r
if state = wsclosed then begin\r
if fdhandlein >= 0 then begin\r
//writeln('listened on addr '+addr+':'+port+' '+proto+' using socket number ',fdhandlein);\r
end;\r
\r
+{$ifdef secondlistener}\r
+procedure tlsocket.internalclose(error:word);\r
+begin\r
+ if assigned(secondlistener) then begin\r
+ secondlistener.destroy;\r
+ secondlistener := nil;\r
+ end;\r
+ inherited internalclose(error);\r
+end;\r
+\r
+procedure tlsocket.secondaccepthandler;\r
+begin\r
+ lastsessionfromsecond := true;\r
+ if udp then begin\r
+ ondataavailable(self,error);\r
+ end else begin\r
+ if assigned(onsessionavailable) then onsessionavailable(self,error);\r
+ end;\r
+end;\r
+{$endif}\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
+ {$ifdef secondlistener}\r
+ if (lastsessionfromsecond) then begin\r
+ lastsessionfromsecond := false;\r
+ result := secondlistener.accept;\r
+ exit;\r
+ end;\r
+ {$endif}\r
\r
FromAddrSize := Sizeof(FromAddr);\r
{$ifdef win32}\r
var\r
destx : {$ifdef win32}winsock.TSockAddr{$else}TInetSockAddrV{$endif} absolute dest;\r
begin\r
+ {$ifdef secondlistener}\r
+ if assigned(secondlistener) then if (dest.inaddr.family = AF_INET) then begin\r
+ result := secondlistener.sendto(dest,destlen,data,len);\r
+ exit;\r
+ end;\r
+ {$endif}\r
result := {$ifdef win32}winsock{$else}sockets{$endif}.sendto(self.fdhandleout,data^,len,0,destx,destlen);\r
end;\r
\r
var\r
srcx : {$ifdef win32}winsock.TSockAddr{$else}TInetSockAddrV{$endif} absolute src;\r
begin\r
+ {$ifdef secondlistener}\r
+ if assigned(secondlistener) then if lastsessionfromsecond then begin\r
+ lastsessionfromsecond := false;\r
+ result := secondlistener.receivefrom(data,len,src,srclen);\r
+ exit;\r
+ end;\r
+ {$endif}\r
result := {$ifdef win32}winsock{$else}sockets{$endif}.recvfrom(self.fdhandlein,data^,len,0,srcx,srclen);\r
end;\r
\r