{$include lcoreconfig.inc}\r
\r
{$ifndef win32}\r
-{$ifdef ipv6}\r
uses sockets;\r
{$endif}\r
-{$endif}\r
\r
{$ifdef fpc}\r
{$mode delphi}\r
\r
const\r
hexchars:array[0..15] of char='0123456789abcdef';\r
- AF_INET=2;\r
{$ifdef win32}\r
+ AF_INET=2;\r
AF_INET6=23;\r
{$else}\r
- AF_INET6=10;\r
+ //redeclare these constants so units that use us can use them\r
+ //without using sockets directly\r
+ AF_INET=AF_INET;\r
+ AF_INET6=AF_INET6;\r
+ //AF_INET6=10;\r
{$endif}\r
\r
type\r
\r
function needconverttov4(const ip:tbinip):boolean;\r
procedure converttov4(var ip:tbinip);\r
+procedure converttov6(var ip:tbinip);\r
\r
function inaddrvtobinip(inaddrv:tinetsockaddrv):tbinip;\r
function makeinaddrv(addr:tbinip;port:string;var inaddr:tinetsockaddrv):integer;\r
{$endif}\r
end;\r
\r
+\r
+{converts a binary IP to v6 if it is a v4 IP}\r
+procedure converttov6(var ip:tbinip);\r
+begin\r
+ {$ifdef ipv6}\r
+ if ip.family = AF_INET then begin\r
+ ip.family := AF_INET6;\r
+ ip.ip6.s6_addr32[3] := ip.ip; \r
+ ip.ip6.u6_addr32[0] := 0; \r
+ ip.ip6.u6_addr32[1] := 0;\r
+ ip.ip6.u6_addr16[4] := 0;\r
+ ip.ip6.u6_addr16[5] := $ffff;\r
+ end;\r
+ {$endif}\r
+end;\r
+\r
+\r
{-----------biniplist stuff--------------------------------------------------}\r
\r
const\r
//this one has to be kept public for now because lcorewsaasyncselect calls it\r
procedure connectionfailedhandler(error:word);\r
private\r
+ isv6socket : boolean; //identifies if the socket is v6, set by bindsocket\r
procedure taskcallconnectionfailedhandler(wparam,lparam : longint);\r
\r
procedure connecttimeouthandler(sender:tobject);\r
end;\r
//gethostbyname(localaddr,host);\r
inaddrtempsize := makeinaddrv(forwardlookup(localaddr,0),localport,inaddrtemp);\r
-\r
+ isv6socket := (inaddrtemp.inaddr.family = AF_INET6);\r
If Bind(fdhandlein,inaddrtempx,inaddrtempsize)<> {$ifdef win32}0{$else}true{$endif} Then begin\r
state := wsclosed;\r
lasterror := {$ifdef win32}getlasterror{$else}socketerror{$endif};\r
\r
if addr = '' then begin\r
{$ifdef ipv6}\r
+ //writeln('ipv6 is defined');\r
if not v4listendefault then begin\r
+ //writeln('setting addr to ::');\r
addr := '::';\r
end else\r
{$endif}\r
end;\r
if isbiniplist(addr) then biniptemp := biniplist_get(addr,0) else biniptemp := forwardlookup(addr,10);\r
addr := ipbintostr(biniptemp);\r
+ //writeln('after ipbintostr call addr =',addr);\r
+ //writeln('biniptemp.family =',biniptemp.family);\r
+ //writeln('AF_INET6=',AF_INET6);\r
+ //writeln('PF_INET6=',PF_INET6);\r
+ //writeln('AF_INET=',AF_INET);\r
+ //writeln('PF_INET=',PF_INET);\r
+ \r
fdhandlein := socket(biniptemp.family,socktype,0);\r
{$ifdef ipv6}\r
if (addr = '::') and (origaddr = '') and (fdhandlein < 0) then begin\r
+ writeln('failed to create an IPV6 socket with error ',socketerror,'. trying to create an IPV4 one instead');\r
addr := '0.0.0.0';\r
- fdhandlein := socket(AF_INET,socktype,0);\r
+ fdhandlein := socket(PF_INET,socktype,0);\r
end;\r
{$endif}\r
\r
no := 0;\r
{$ifndef win32}\r
if SetSocketOptions(fdhandlein, SOL_SOCKET, SO_REUSEADDR,yes,sizeof(yes))=-1 then begin\r
- raise ESocketException.create('unable to set socket options');\r
+ raise ESocketException.create('unable to set SO_REUSEADDR socket option');\r
end;\r
- if SetSocketOptions(fdhandlein, IPPROTO_IPV6,IPV6_V6ONLY,no,sizeof(no))=-1 then begin\r
- raise ESocketException.create('unable to set socket options');\r
+ //writeln('addr=',addr);\r
+ //writeln('setting IPV6_V6ONLY option to 0');\r
+ //allow v4 connections on v6 sockets\r
+ if biniptemp.family = af_inet6 then begin\r
+ if SetSocketOptions(fdhandlein, IPPROTO_IPV6,IPV6_V6ONLY,no,sizeof(no))=-1 then begin\r
+ writeln(IPPROTO_IPV6);\r
+ writeln(IPV6_V6ONLY);\r
+ raise ESocketException.create('unable to set IPV6_V6ONLY socket option error='+inttostr(socketerror));\r
+ \r
+ end;\r
end;\r
{$endif}\r
localaddr := addr;\r
\r
function tlsocket.sendto(dest:TInetSockAddrV;destlen:integer;data:pointer;len:integer):integer;\r
var\r
- destx : {$ifdef win32}winsock.TSockAddr{$else}TInetSockAddrV{$endif} absolute dest;\r
+ {$ifdef ipv6}\r
+ realdest : tinetsockaddrv;\r
+ biniptemp : tbinip;\r
+ {$endif}\r
+ destx : {$ifdef win32}winsock.pSockAddr{$else}pInetSockAddrV{$endif};\r
+\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
+ 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.sendto{$else}system_sendto{$endif}(self.fdhandleout,data^,len,0,destx,destlen);\r
+ {$ifdef ipv6}\r
+ if isv6socket then begin\r
+ biniptemp := inaddrvtobinip(dest);\r
+ converttov6(biniptemp);\r
+ destlen := makeinaddrv(biniptemp,inttostr(ntohs(dest.InAddr.port)),realdest);\r
+ destx := {$ifdef win32}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@realdest)\r
+ end else begin\r
+ destx := {$ifdef win32}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@dest)\r
+ end;\r
+ {$else}\r
+ destx := {$ifdef win32}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@dest)\r
+ {$endif}\r
+\r
+ result := {$ifdef win32}winsock.sendto{$else}system_sendto{$endif}(self.fdhandleout,data^,len,0,destx^,destlen);\r
end;\r
\r
\r