X-Git-Url: http://www.lcore.org/git/lcore.git/blobdiff_plain/170e475daffeb198914a020e26afaf803a6f9608..9a44a542cab11c03215fe3c844dde2529a87258d:/lsocket.pas?ds=inline diff --git a/lsocket.pas b/lsocket.pas index ce23301..53205e6 100755 --- a/lsocket.pas +++ b/lsocket.pas @@ -132,6 +132,7 @@ type //this one has to be kept public for now because lcorewsaasyncselect calls it procedure connectionfailedhandler(error:word); private + isv6socket : boolean; //identifies if the socket is v6, set by bindsocket procedure taskcallconnectionfailedhandler(wparam,lparam : longint); procedure connecttimeouthandler(sender:tobject); @@ -160,6 +161,32 @@ begin result := inaddrsize(inaddr); end; +//I used to use the system versions of these from within lsocket (which has +//functions whose name clashes with them) by using sockets.* and but I can't do +//that anymore since in some cases connect is now provided by unixstuff.inc +//hence these wrapper functions --plugwash +{$ifndef win32} + function system_Connect(Sock: LongInt;const Addr;Addrlen: LongInt):Boolean; + begin + result := connect(sock,addr,addrlen); + end; + function system_SendTo(Sock: LongInt; const Buf;BufLen: LongInt;Flags: LongInt;var Addr;AddrLen: LongInt):LongInt; + begin + result := sendto(sock,buf,buflen,flags,addr,addrlen); + end; + function system_getpeername(Sock: LongInt;var Addr;var Addrlen: LongInt):LongInt; + begin + result := getpeername(sock,addr,addrlen); + end; + function system_listen(Sock: LongInt; MaxConnect: LongInt):Boolean; + begin + result := listen(sock,maxconnect); + end; + function system_Accept(Sock: LongInt;var Addr;var Addrlen: LongInt):LongInt; + begin + result := accept(sock,addr,addrlen); + end; +{$endif} procedure tlsocket.realconnect; var @@ -220,7 +247,7 @@ begin //writeln(inaddr.inaddr.port); winsock.Connect(fdhandlein,winsock.psockaddr(@inADDR)^,getaddrsize); {$else} - sockets.Connect(fdhandlein,inADDR,getaddrsize); + system_Connect(fdhandlein,inADDR,getaddrsize); {$endif} eventcore.rmasterset(fdhandlein,false); eventcore.wmasterset(fdhandleout); @@ -245,6 +272,9 @@ begin realconnect; end; + + + procedure tlsocket.connect; var a:integer; @@ -260,7 +290,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; @@ -323,7 +352,7 @@ begin end; //gethostbyname(localaddr,host); inaddrtempsize := makeinaddrv(forwardlookup(localaddr,0),localport,inaddrtemp); - + isv6socket := (inaddrtemp.inaddr.family = AF_INET6); If Bind(fdhandlein,inaddrtempx,inaddrtempsize)<> {$ifdef win32}0{$else}true{$endif} Then begin state := wsclosed; lasterror := {$ifdef win32}getlasterror{$else}socketerror{$endif}; @@ -341,7 +370,7 @@ end; procedure tlsocket.listen; var - yes:longint; + yes,no:longint; socktype:integer; biniptemp:tbinip; origaddr:string; @@ -356,7 +385,9 @@ begin if addr = '' then begin {$ifdef ipv6} + //writeln('ipv6 is defined'); if not v4listendefault then begin + //writeln('setting addr to ::'); addr := '::'; end else {$endif} @@ -364,11 +395,19 @@ begin end; if isbiniplist(addr) then biniptemp := biniplist_get(addr,0) else biniptemp := forwardlookup(addr,10); addr := ipbintostr(biniptemp); + //writeln('after ipbintostr call addr =',addr); + //writeln('biniptemp.family =',biniptemp.family); + //writeln('AF_INET6=',AF_INET6); + //writeln('PF_INET6=',PF_INET6); + //writeln('AF_INET=',AF_INET); + //writeln('PF_INET=',PF_INET); + fdhandlein := socket(biniptemp.family,socktype,0); {$ifdef ipv6} if (addr = '::') and (origaddr = '') and (fdhandlein < 0) then begin + writeln('failed to create an IPV6 socket with error ',socketerror,'. trying to create an IPV4 one instead'); addr := '0.0.0.0'; - fdhandlein := socket(AF_INET,socktype,0); + fdhandlein := socket(PF_INET,socktype,0); end; {$endif} @@ -380,9 +419,21 @@ begin try yes := $01010101; {Copied this from existing code. Value is empiric, but works. (yes=true<>0) } + no := 0; {$ifndef win32} if SetSocketOptions(fdhandlein, SOL_SOCKET, SO_REUSEADDR,yes,sizeof(yes))=-1 then begin - raise ESocketException.create('unable to set socket options'); + raise ESocketException.create('unable to set SO_REUSEADDR socket option'); + end; + //writeln('addr=',addr); + //writeln('setting IPV6_V6ONLY option to 0'); + //allow v4 connections on v6 sockets + if biniptemp.family = af_inet6 then begin + if SetSocketOptions(fdhandlein, IPPROTO_IPV6,IPV6_V6ONLY,no,sizeof(no))=-1 then begin + writeln(IPPROTO_IPV6); + writeln(IPV6_V6ONLY); + raise ESocketException.create('unable to set IPV6_V6ONLY socket option error='+inttostr(socketerror)); + + end; end; {$endif} localaddr := addr; @@ -392,7 +443,7 @@ 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 + If {$ifdef win32}winsock.listen{$else}system_listen{$endif}(fdhandlein,listenqueue)<>{$ifdef win32}0{$else}true{$endif} Then raise esocketexception.create('unable to listen'); state := wsListening; end else begin @@ -480,7 +531,7 @@ begin {$ifdef win32} result := winsock.accept(fdhandlein,@fromaddr,@fromaddrsize); {$else} - result := sockets.accept(fdhandlein,fromaddr,fromaddrsize); + result := system_accept(fdhandlein,fromaddr,fromaddrsize); {$endif} //now we have accepted one request start monitoring for more again eventcore.rmasterset(fdhandlein,true); @@ -496,19 +547,39 @@ begin end; end; + function tlsocket.sendto(dest:TInetSockAddrV;destlen:integer;data:pointer;len:integer):integer; var - destx : {$ifdef win32}winsock.TSockAddr{$else}TInetSockAddrV{$endif} absolute dest; + {$ifdef ipv6} + realdest : tinetsockaddrv; + biniptemp : tbinip; + {$endif} + destx : {$ifdef win32}winsock.pSockAddr{$else}pInetSockAddrV{$endif}; + begin {$ifdef secondlistener} - if assigned(secondlistener) then if (dest.inaddr.family = AF_INET) then begin - result := secondlistener.sendto(dest,destlen,data,len); - exit; - end; + 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); + {$ifdef ipv6} + if isv6socket then begin + biniptemp := inaddrvtobinip(dest); + converttov6(biniptemp); + destlen := makeinaddrv(biniptemp,inttostr(ntohs(dest.InAddr.port)),realdest); + destx := {$ifdef win32}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@realdest) + end else begin + destx := {$ifdef win32}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@dest) + end; + {$else} + destx := {$ifdef win32}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@dest) + {$endif} + + result := {$ifdef win32}winsock.sendto{$else}system_sendto{$endif}(self.fdhandleout,data^,len,0,destx^,destlen); end; + function tlsocket.receivefrom(data:pointer;len:integer;var src:TInetSockAddrV;var srclen:integer):integer; var tempsrc:TInetSockAddrV; @@ -524,7 +595,7 @@ begin end; {$endif} tempsrclen := sizeof(tempsrc); - result := {$ifdef win32}winsock{$else}sockets{$endif}.recvfrom(self.fdhandlein,data^,len,0,srcx,tempsrclen); + result := recvfrom(self.fdhandlein,data^,len,0,srcx,tempsrclen); {$ifdef ipv6} biniptemp := inaddrvtobinip(tempsrc); @@ -637,11 +708,12 @@ begin end; + function tlsocket.getpeername(var addr:tsockaddrin;addrlen:integer):integer; var addrx : {$ifdef win32}winsock.tsockaddr{$else}tsockaddrin{$endif} absolute addr; begin - result := {$ifdef win32}winsock{$else}sockets{$endif}.getpeername(self.fdhandlein,addrx,addrlen); + result := {$ifdef win32}winsock.getpeername{$else}system_getpeername{$endif}(self.fdhandlein,addrx,addrlen); end; procedure tlsocket.getxaddrbin(var binip:tbinip); @@ -655,7 +727,7 @@ begin {$ifdef win32} winsock.getsockname(self.fdhandlein,psockaddr(@addr)^,i); {$else} - sockets.getsocketname(self.fdhandlein,addr,i); + getsocketname(self.fdhandlein,addr,i); {$endif} binip := inaddrvtobinip(addr); converttov4(binip); @@ -671,7 +743,7 @@ begin {$ifdef win32} winsock.getpeername(self.fdhandlein,psockaddr(@addr)^,i); {$else} - sockets.getpeername(self.fdhandlein,addr,i); + system_getpeername(self.fdhandlein,addr,i); {$endif} binip := inaddrvtobinip(addr); @@ -706,7 +778,7 @@ begin winsock.getsockname(self.fdhandlein,psockaddrin(@addr)^,i); {$else} - sockets.getsocketname(self.fdhandlein,addr,i); + getsocketname(self.fdhandlein,addr,i); {$endif} result := inttostr(htons(addr.InAddr.port)); @@ -722,7 +794,7 @@ begin winsock.getpeername(self.fdhandlein,psockaddrin(@addr)^,i); {$else} - sockets.getpeername(self.fdhandlein,addr,i); + system_getpeername(self.fdhandlein,addr,i); {$endif} result := inttostr(htons(addr.InAddr.port));