private void DebugMessage(string scope, string message, params object[] args) { var lastError = Socket.WSAGetLastError(); try { if (string.IsNullOrWhiteSpace(LogPath)) { return; } var space = Math.Max(15 - scope.Length, 0); message = string.Format( "{0:s} - [`{1}`] : {2}{3}", DateTime.UtcNow, scope, new string(' ', space), args?.Length > 0 ? string.Format(message, args) : message ); Debug.WriteLine(message); File.AppendAllText(LogPath, message + Environment.NewLine); } catch { // ignored } Socket.WSASetLastError(lastError); }
private SocketError OnConnect(IntPtr socket, IntPtr address, int addressSize) { var socketAddress = GetSocketAddress(address); DebugMessage( nameof(OnConnect), "Connecting to `{0}:{1}` ...", socketAddress?.Address?.IPAddress, socketAddress?.Port ); var bindResult = BindSocketByAddress(socket, socketAddress); DebugMessage( nameof(OnConnect), "Binding `{0}:{1}` to interface resulted in a `{2}` response. [WSALastError = `{3}`]", socketAddress?.Address?.IPAddress, socketAddress?.Port, bindResult, Socket.WSAGetLastError() ); if (bindResult != SocketError.Success) { if (bindResult != SocketError.SocketError) { DebugMessage(nameof(OnConnect), Socket.WSAGetLastError().ToString()); } DebugMessage( nameof(OnConnect), "Connecting to `{0}:{1}` rejected.", socketAddress?.Address?.IPAddress, socketAddress?.Port ); return(SocketError.SocketError); } var returnValue = Socket.Connect(socket, address, addressSize); DebugMessage( nameof(OnConnect), "Connecting to `{0}:{1}` resulted in a `{2}` response. [WSALastError = `{3}`]", socketAddress?.Address?.IPAddress, socketAddress?.Port, returnValue, Socket.WSAGetLastError() ); //if (returnValue == SocketError.SocketError && Socket.WSAGetLastError() == SocketError.Success) //{ // returnValue = SocketError.Success; //} return(returnValue); }
private SocketError OnBind(IntPtr socket, IntPtr address, int addressSize) { var socketAddress = GetSocketAddress(address); DebugMessage( nameof(OnBind), "Binding to `{0}:{1}` ...", socketAddress?.Address?.IPAddress, socketAddress?.Port ); var networkInterface = GetNetworkInterface(); var interfaceAddress = GetInterfaceAddress(networkInterface, socketAddress?.AddressFamily, true); if (socketAddress?.Address == null || networkInterface == null || interfaceAddress == null) { DebugMessage( nameof(OnBind), "Binding to `{0}:{1}` rejected due to lack of a valid interface address.", socketAddress?.Address?.IPAddress, socketAddress?.Port ); return(SocketError.SocketError); } SocketError bindResult; if (interfaceAddress.AddressFamily == AddressFamily.InterNetwork && !interfaceAddress.Equals(socketAddress.Address.IPAddress)) { var bindIn = new SocketAddressIn { Address = new AddressIn { IPAddress = interfaceAddress }, AddressFamily = interfaceAddress.AddressFamily, Port = socketAddress.Port }; DebugMessage( nameof(OnBind), "Binding to `{0}:{1}` replaced by a binding request to `{2}:{3}`.", socketAddress.Address.IPAddress, socketAddress.Port, bindIn.Address.IPAddress, bindIn.Port ); socketAddress = bindIn; bindResult = Socket.Bind(socket, ref bindIn, Marshal.SizeOf(bindIn)); } else if (interfaceAddress.AddressFamily == AddressFamily.InterNetworkV6 && !interfaceAddress.Equals(socketAddress.Address.IPAddress)) { var scopeId = (uint?)networkInterface.GetIPProperties()?.GetIPv6Properties()?.Index ?? 0; var flowInfo = (socketAddress as SocketAddressIn6?)?.FlowInfo ?? 0; var bindIn6 = new SocketAddressIn6 { Address = new AddressIn6 { IPAddress = interfaceAddress }, AddressFamily = interfaceAddress.AddressFamily, Port = socketAddress.Port, // Assign a random port ScopeId = scopeId, FlowInfo = flowInfo }; DebugMessage( nameof(OnBind), "Binding to `{0}:{1}` replaced by a binding request to `{2}:{3}`.", socketAddress.Address.IPAddress, socketAddress.Port, bindIn6.Address.IPAddress, bindIn6.Port ); socketAddress = bindIn6; bindResult = Socket.Bind(socket, ref bindIn6, Marshal.SizeOf(bindIn6)); } else { bindResult = Socket.Bind(socket, address, addressSize); } DebugMessage( nameof(OnBind), "Binding to `{0}:{1}` resulted in a `{2}` response. [WSALastError = `{3}`]", socketAddress.Address.IPAddress, socketAddress.Port, bindResult, Socket.WSAGetLastError() ); return(bindResult); }