예제 #1
0
파일: Guest.cs 프로젝트: orf53975/PeaRoxy
        private Unmanaged.ConnectStatus Connect(IntPtr socket, ref Unmanaged.SocketAddressIn address, int addressSize)
        {
            try
            {
                IPEndPoint proxyEndPoint = this.hostConnector.GetProxyEndPoint(this.hostProcessId);

                bool isActive = address.Family == AddressFamily.InterNetwork && proxyEndPoint != null &&
                                !address.IPAddress.IpAddress.Equals(proxyEndPoint.Address) &&
                                !address.IPAddress.IpAddress.Equals(IPAddress.Loopback);
                if (isActive)
                {
                    string internetIpAddress = address.IPAddress.IpAddress.ToString();
                    string hostName          = (this.dnsReverseDictionary.ContainsKey(internetIpAddress)
                                           ? this.dnsReverseDictionary[internetIpAddress]
                                           : internetIpAddress);
                    GuestSocketInfo socketInfo = new GuestSocketInfo
                    {
                        HostName  = hostName,
                        NoSendYet = true,
                        Port      = (uint)address.Port,
                        Socket    = socket
                    };
                    if (!this.dnsSocketDictionary.ContainsKey(socket))
                    {
                        this.dnsSocketDictionary.Add(socket, socketInfo);
                    }
                    else
                    {
                        this.dnsSocketDictionary[socket] = socketInfo;
                    }

                    address.IPAddress.IpAddress = proxyEndPoint.Address;
                    address.Port = proxyEndPoint.Port;

                    this.ReportMessage("Connect: " + socketInfo.HostName);
                }
            }
            catch (Exception e)
            {
                this.ReportMessage(e.ToString());
            }

            Unmanaged.ConnectStatus returnValue = Unmanaged.WS2_Connect(socket, ref address, addressSize);
            if (returnValue == Unmanaged.ConnectStatus.Error &&
                (Unmanaged.WSAGetLastError() == SocketError.WouldBlock ||
                 Unmanaged.WSAGetLastError() == SocketError.Success))
            {
                // Non blocking mode
                returnValue = Unmanaged.ConnectStatus.Ok;
            }

            return(returnValue);
        }
예제 #2
0
파일: Guest.cs 프로젝트: orf53975/PeaRoxy
        private SocketError GetAddressInfo(
            IntPtr nodeName,
            IntPtr serviceName,
            IntPtr hints,
            out IntPtr results,
            bool isUnicode)
        {
            SocketError returnValue = isUnicode
                                          ? Unmanaged.GetAddressInfoUni(nodeName, serviceName, hints, out results)
                                          : Unmanaged.GetAddressInfo(nodeName, serviceName, hints, out results);

            try
            {
                string           hostname            = isUnicode ? Marshal.PtrToStringUni(nodeName) : Marshal.PtrToStringAnsi(nodeName);
                List <IPAddress> internetIpAddresses = new List <IPAddress>();
                if (returnValue == SocketError.Success || returnValue == SocketError.HostNotFound)
                {
                    if (returnValue == SocketError.Success)
                    {
                        IntPtr handle = new IntPtr(results.ToInt64());
                        do
                        {
                            Unmanaged.AddressInfo addressInfo =
                                (Unmanaged.AddressInfo)Marshal.PtrToStructure(handle, typeof(Unmanaged.AddressInfo));

                            if (addressInfo.Family == AddressFamily.InterNetwork && addressInfo.AddressLen >= 8)
                            {
                                Unmanaged.SocketAddressIn socketAddress =
                                    (Unmanaged.SocketAddressIn)
                                    Marshal.PtrToStructure(addressInfo.Address, typeof(Unmanaged.SocketAddressIn));

                                if (this.dnsGrabbeRegex != null &&
                                    this.dnsGrabbeRegex.IsMatch(socketAddress.IPAddress.IpAddress.ToString()))
                                {
                                    internetIpAddresses.Clear();
                                    break;
                                }
                                internetIpAddresses.Add(socketAddress.IPAddress.IpAddress);
                            }
                            handle = addressInfo.Next;
                        }while (handle != IntPtr.Zero);
                    }

                    if (internetIpAddresses.Count == 0)
                    {
                        internetIpAddresses.Add(this.GenerateInternalIpAddress());
                        if (isUnicode)
                        {
                            if (!results.Equals(IntPtr.Zero))
                            {
                                Unmanaged.FreeAddressInfoUni(results);
                            }
                            returnValue =
                                Unmanaged.GetAddressInfoUni(
                                    Marshal.StringToHGlobalUni(internetIpAddresses[0].ToString()),
                                    serviceName,
                                    hints,
                                    out results);
                        }
                        else
                        {
                            if (!results.Equals(IntPtr.Zero))
                            {
                                Unmanaged.FreeAddressInfo(results);
                            }
                            returnValue =
                                Unmanaged.GetAddressInfo(
                                    Marshal.StringToHGlobalAnsi(internetIpAddresses[0].ToString()),
                                    serviceName,
                                    hints,
                                    out results);
                        }
                    }
                    if (internetIpAddresses.Count > 0)
                    {
                        foreach (IPAddress internetIpAddress in internetIpAddresses)
                        {
                            if (this.dnsReverseDictionary.ContainsKey(internetIpAddress.ToString()))
                            {
                                this.dnsReverseDictionary[internetIpAddress.ToString()] = hostname;
                            }
                            else
                            {
                                this.dnsReverseDictionary.Add(internetIpAddress.ToString(), hostname);
                            }

                            this.ReportMessage("GetAddressInfo: " + hostname + " -> " + internetIpAddress);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                this.ReportMessage(e.ToString());
            }

            return(returnValue);
        }