connect ipv6 only client to ipv4(with DNS) only server fail with Access denied

asanaasana KRMember
edited May 2016 in Xamarin.iOS

Hello~

I used .net 2.0 Xamarin [actually, I use unity mono :( ]

I try connect ipv6 iOS app to DNS mapped ipv4 only server.

but, I faced problem with Access denied on TcpClient.EndConnect() combine TcpClient.BeginConnect( "dns", port, ResultConnect, connectFrame)

EndConnect() throw SocketException with Access denied message.

here some my code

                public int connectSocket(int reqID, ASSocket socket, string hostaddress, int port)
                {
                    lock (this)
                    {
                        theReceiver.writeString(string.Format("[MySocketLib] {0} TryConnect : {1}", aliasname, socket.theDESC.managedID));

                        if (theSockets.ContainsKey(socket.theDESC.managedID))
                        {
                            theReceiver.writeString(string.Format("[MySocketLib] {0} manid exists", socket.theDESC.managedID));
                            return 1;
                        }

                        theSockets.Add(socket.theDESC.managedID, socket);

                        ConnectFrame connectFrame = new ConnectFrame();
                        connectFrame.reqID = reqID;
                        connectFrame.socket = socket;
                        connectFrame.socket.theDESC.theSender = this;

                        IPAddress _ipaddress;

                        if (IPAddress.TryParse(hostaddress, out _ipaddress))
                        {

                            if (_ipaddress.AddressFamily == AddressFamily.InterNetworkV6)
                            {
                                socket.tcpclient = new TcpClient(AddressFamily.InterNetworkV6);
                                socket.tcpclient.BeginConnect(hostaddress, port, new AsyncCallback(ResultConnect), connectFrame);
                            }
                            else
                            {                                                
                                socket.tcpclient = new TcpClient();
                                socket.tcpclient.BeginConnect(hostaddress, port, new AsyncCallback(ResultConnect), connectFrame);
                            }
                        }
                        else
                        {                    
                            socket.tcpclient = new TcpClient();                    
                            socket.tcpclient.BeginConnect(hostaddress, port, new AsyncCallback(ResultConnect), connectFrame);
                        }

                    }
                    return 0;
                }

                private void ResultConnect(IAsyncResult ar)
                {
                    theReceiver.writeString(string.Format("[MySocketLib] {0} Invoked Connect", aliasname));
                    ConnectFrame connectFrame = (ar.AsyncState as ConnectFrame);
                    ASSOCKDESC desc = new ASSOCKDESC();
                    desc.createTick = connectFrame.socket.theDESC.createTick;
                    desc.managedID = connectFrame.socket.theDESC.managedID;
                    desc.theSender = connectFrame.socket.theDESC.theSender;

                    bool bConnectOk = false;

                    try
                    {
                        connectFrame.socket.tcpclient.EndConnect(ar);
                        theReceiver.writeString(string.Format("[MySocketLib] {0} ResultConnect", aliasname));
                        bConnectOk = true;
                        theReceiver.notifyConnectingResult(connectFrame.reqID, desc, true, null);

                        theReceiver.writeString(string.Format("[MySocketLib] {0} Try FirstRead", aliasname));

                        connectFrame.socket.enterIO();
                        try
                        {
                            IOFrame ioframe = new IOFrame();
                            ioframe.packet = new Packet();
                            ioframe.socket = connectFrame.socket;
                            connectFrame.socket.tcpclient.GetStream().BeginRead(ioframe.packet.Buffer, 0, ioframe.packet.Buffer.Length, new System.AsyncCallback(ResultRead), ioframe);
                        }
                        catch (Exception ex)
                        {
                            theReceiver.writeString(string.Format("[MySocketLib] {0} First BeginRead error {1}", aliasname, ex.Message));
                            ReleaseSocket(connectFrame.socket);
                        }

                    }
                    catch (Exception ex)
                    {
                        try
                        {
                            theReceiver.writeString(string.Format("[MySocketLib] {0} ResultConnect exception {1}", aliasname, ex.Message));
                            lock (this)
                            {
                                theSockets.Remove(desc.managedID);
                            }

                            if (bConnectOk)
                            {
                                theReceiver.writeString(string.Format("[MySocketLib] {0} notified ResultConnect but exception from callback scope", aliasname));
                                theReceiver.notifyReleaseSocket(desc, 1);
                            }
                            else
                            {
                                theReceiver.notifyConnectingResult(connectFrame.reqID, desc, false, ex);
                            }
                        }
                        catch(Exception iex)
                        {
                            theReceiver.writeString(string.Format("[MySocketLib] {0} {1} ResultConnect exception exception", aliasname, iex.Message ));
                        }
                        connectFrame.socket.tcpclient = null;
                    }
                }

I suppose this error occurred from API level.

Someone help me~ T_T

Tagged:

Answers

  • Had you resolved an issue? I have the same..

  • asanaasana KRMember

    @ThomasAndersen.7626
    not yet. But you maybe success by above code

        IPHostEntry heserver = Dns.GetHostEntry(dns);
            foreach (IPAddress _curAdd in heserver.AddressList)
                {
                    socket.tcpclient = new TcpClient(_curAdd.AddressFamily);
                    socket.tcpclient.BeginConnect(_curAdd, port, new AsyncCallback(ResultConnect), connectFrame);
                }
    

    But your server ipaddress(ipv4) must mapped to DNS64.
    No use for NO-DNS EndPoint

Sign In or Register to comment.