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); }
private IntPtr GetHostByName(string host) { IntPtr returnValue = Unmanaged.GetHostByName(host); try { List <IPAddress> internetIpAddresses = new List <IPAddress>(); if (returnValue != IntPtr.Zero) { IPHostEntry results = Unmanaged.NativeIpHostEntry.FromNative(returnValue); foreach (IPAddress ipAddress in results.AddressList) { if (this.dnsGrabbeRegex != null && this.dnsGrabbeRegex.IsMatch(ipAddress.ToString())) { internetIpAddresses.Clear(); break; } internetIpAddresses.Add(ipAddress); } } if (internetIpAddresses.Count == 0) { internetIpAddresses.Add(this.GenerateInternalIpAddress()); returnValue = Unmanaged.GetHostByName(internetIpAddresses[0].ToString()); } if (internetIpAddresses.Count > 0) { foreach (IPAddress internetIpAddress in internetIpAddresses) { if (this.dnsReverseDictionary.ContainsKey(internetIpAddress.ToString())) { this.dnsReverseDictionary[internetIpAddress.ToString()] = host; } else { this.dnsReverseDictionary.Add(internetIpAddress.ToString(), host); } this.ReportMessage("GetHostByName: " + host + " -> " + internetIpAddress); } } } catch (Exception e) { this.ReportMessage(e.ToString()); } return(returnValue); }
private SocketError WsaSend( IntPtr socket, IntPtr buffer, int len, out IntPtr numberOfBytesSent, SocketFlags flags, IntPtr overlapped, IntPtr completionRoutine) { try { if (this.dnsSocketDictionary.ContainsKey(socket) && this.dnsSocketDictionary[socket].NoSendYet) { lock (this.dnsSocketDictionary) { if (this.dnsSocketDictionary[socket].NoSendYet) { this.dnsSocketDictionary[socket].NoSendYet = false; Unmanaged.WsaBuffer sendBuffer = Unmanaged.WsaBuffer.FromString( "FASTCONNECT " + this.dnsSocketDictionary[socket].HostName + ":" + this.dnsSocketDictionary[socket].Port + "\r\n\r\n"); IntPtr bufferPointer = Marshal.AllocHGlobal(Marshal.SizeOf(sendBuffer)); Marshal.StructureToPtr(sendBuffer, bufferPointer, true); IntPtr o; SocketError error = Unmanaged.Send( socket, bufferPointer, 1, out o, SocketFlags.None, IntPtr.Zero, IntPtr.Zero); this.ReportMessage("WSASend: " + len + " - Result: " + error); } } } } catch (Exception e) { this.ReportMessage(e.ToString()); } return(Unmanaged.Send(socket, buffer, len, out numberOfBytesSent, flags, overlapped, completionRoutine)); }
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); }