Skip to content

Commit

Permalink
Fix network server TLS bug, extend debug logging
Browse files Browse the repository at this point in the history
  • Loading branch information
drkno committed Jan 3, 2016
1 parent 87aece3 commit fe551c5
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 44 deletions.
1 change: 1 addition & 0 deletions PCR1000.Network/PCR1000.Network.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="PcrNetworkComm.cs" />
<Compile Include="PcrNetworkProtocolHandler.cs" />
<Compile Include="PcrNetworkServer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down
18 changes: 11 additions & 7 deletions PCR1000.Network/PcrNetworkComm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,26 +86,30 @@ private void ListenThread()
datarecv = datarecv.Substring(0, lData);
datarecv = datarecv.Trim(TrimChars);
if (datarecv.Length <= 0) continue;
#if DEBUG
#if DEBUG
Debug.WriteLineIf(!_debugLogger, "PcrNetwork Data Recv");
#endif
if (AutoUpdate && DataReceived != null)
#endif
if (AutoUpdate)
{
DataReceived(this, DateTime.Now, datarecv);
DataReceived?.Invoke(this, DateTime.Now, datarecv);
}

_msgSlot2 = _msgSlot1;
_msgSlot1 = new RecvMsg { Message = datarecv, Time = DateTime.Now };
_msgSlot1 = new RecvMsg {Message = datarecv, Time = DateTime.Now};

#if DEBUG
#if DEBUG
Debug.WriteLineIf(_debugLogger, _server + ":" + _port + " : RECV -> " + datarecv);
#endif
#endif
}
}
catch (ThreadAbortException)
{
Debug.WriteLine("Listen thread aborting...");
}
catch (IOException e)
{
Debug.WriteLine("A socket read failure occurred:\n" + e.Message + "\r\n" + e.StackTrace);
}
}

/// <summary>
Expand Down
23 changes: 14 additions & 9 deletions PCR1000.Network/PcrNetworkServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class PcrNetworkServer

private TcpClient _tcpClient;

private readonly bool _ssl;
private readonly bool _tls;
private readonly string _password;
private bool _isAuthenticated;

Expand All @@ -37,12 +37,12 @@ public class PcrNetworkServer
/// </summary>
/// <param name="pcrComm">Method of communication to use to connect to the radio.</param>
/// <param name="netport">Network port to communucate on. Defaults to 4456.</param>
/// <param name="ssl">Use SSL to secure connections. This MUST be symmetric.</param>
/// <param name="tls">Use TLS to secure connections. This MUST be symmetric.</param>
/// <param name="password">Password to use. Defaults to none.</param>
public PcrNetworkServer(IComm pcrComm, int netport = 4456, bool ssl = false, string password = "")
public PcrNetworkServer(IComm pcrComm, int netport = 4456, bool tls = false, string password = "")
{
Debug.WriteLine($"PcrNetwork Being Created: port={netport} ssl={ssl} password=\"{password}\"");
_ssl = ssl;
Debug.WriteLine($"PcrNetwork Being Created: port={netport} tls={tls} password=\"{password}\"");
_tls = tls;
_password = password;
if (string.IsNullOrEmpty(_password)) _isAuthenticated = true;
_port = netport;
Expand Down Expand Up @@ -97,8 +97,7 @@ private void ListenForCommands(object obj)
{
Debug.WriteLine("Network: Client Connected");
_tcpClient = (TcpClient)obj;
var clientStream = _ssl ? (Stream)new SslStream(_tcpClient.GetStream()) : _tcpClient.GetStream();

var clientStream = _tls ? (Stream) new SslStream(_tcpClient.GetStream()) : _tcpClient.GetStream();
while (true)
{
try
Expand Down Expand Up @@ -144,6 +143,12 @@ private void ListenForCommands(object obj)
}
catch (Exception e)
{
// client connected to a tls server without a tls stream
if (e is InvalidOperationException && e.Message.Contains("authenticated") && clientStream is SslStream)
{
Debug.WriteLine("Client connected to TLS server without TLS stream. Disconnecting...");
return;
}
Debug.WriteLine("Client disconnect with exception: " + e.Message + "\n" + e.StackTrace);
break;
}
Expand Down Expand Up @@ -190,9 +195,9 @@ private void PcrCommOnDataReceived(IComm sender, DateTime recvTime, string data)
Debug.WriteLineIf(_debugLogger, "RECV -> " + data);
#endif
}
catch (Exception)
catch (Exception e)
{
Debug.WriteLine("RECV:ERR");
Debug.WriteLine("RECV:ERR " + e.Message + "\n" + e.StackTrace);
}
}

Expand Down
25 changes: 10 additions & 15 deletions PCR1000/PcrControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
using System;
using System.Diagnostics;
using System.Globalization;
using PCR1000.Annotations;

#endregion

Expand All @@ -36,11 +35,6 @@ public class PcrControl
/// </summary>
private IComm _pcrComm;

/// <summary>
/// Was there an error reading from the PComm object?
/// </summary>
[UsedImplicitly] private bool _pcrErrRead;

/// <summary>
/// Currently active radio data.
/// </summary>
Expand All @@ -55,6 +49,7 @@ public class PcrControl
/// Instantiates a new PCR1000 controller
/// </summary>
/// <param name="communicationChannel">Channel to use to communicate with the radio.</param>
/// <exception cref="UnauthorizedAccessException">If the communication channel cannot be opened.</exception>
public PcrControl(IComm communicationChannel)
{
_pcrRadio = new PRadInf();
Expand All @@ -71,6 +66,10 @@ public PcrControl(IComm communicationChannel)
_pcrRadio.PcrRfAttenuator = false;
_pcrRadio.PcrAutoUpdate = false;
_pcrStatus = false;
if (!_pcrComm.PcrOpen())
{
throw new UnauthorizedAccessException("Access was not granted to the communication channel.");
}
}

/// <summary>
Expand All @@ -92,15 +91,12 @@ private bool PcrCheckResponse(string response, bool overrideAutoupdate = false)

if (response == PcrDef.PCRAOK || response == PcrDef.PCRBOK)
{
_pcrErrRead = false;
return true;
}
if (response == PcrDef.PCRABAD)
{
_pcrErrRead = false;
return false;
}
_pcrErrRead = true;
return false;
}

Expand Down Expand Up @@ -661,21 +657,20 @@ public bool PcrSetNb(bool value)
}

/// <summary>
/// Set the port for the current session.
/// Sets port by closing the filedes and reopening it
/// on the new port.
/// Set the communication port for the current session.
/// Sets port by closing the handle to the current one and opening the new one.
/// </summary>
/// <param name="port">The port</param>
/// <param name="communicationPort">The port</param>
/// <returns>
/// True or false if the serial device can be opened on the new port.
/// </returns>
public bool PcrSetPort(string port)
public bool PcrSetPort(IComm communicationPort)
{
Debug.WriteLine("PcrControl PcrSetPort");
_pcrComm.PcrClose();
try
{
_pcrComm = new PcrSerialComm(port);
_pcrComm = communicationPort;
_pcrComm.PcrOpen();
_pcrComm.AutoUpdate = _pcrRadio.PcrAutoUpdate;
return true;
Expand Down
19 changes: 10 additions & 9 deletions PCR1000/PcrSerialComm.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* PcrComm
* Serial communication component of the PCR1000 Library
*
*
* Copyright Matthew Knox © 2013-Present.
* This program is distributed with no warentee or garentee
* what so ever. Do what you want with it as long as attribution
Expand Down Expand Up @@ -78,7 +78,7 @@ private struct RecvMsg
/// <param name="baud">Baud rate to use. Defaults to 9600.</param>
public PcrSerialComm(string port = null, int baud = 9600)
{
Debug.WriteLine("PcrComm Being Created");
Debug.WriteLine($"PcrComm Being Created port={port} baud={baud}");
var portNames = SerialPort.GetPortNames();
if (string.IsNullOrWhiteSpace(port))
{
Expand All @@ -93,7 +93,7 @@ public PcrSerialComm(string port = null, int baud = 9600)
if (!portNames.Contains(port))
{
Debug.WriteLine("PcrComm Error: the serial port provided does not exist.");
throw new ArgumentException("Serial port provided does not exist.", nameof(port));
throw new ArgumentException("The serial port provided does not exist.", nameof(port));
}

_serialPort = IsRunningOnMono()
Expand All @@ -102,7 +102,7 @@ public PcrSerialComm(string port = null, int baud = 9600)
_serialPort.DataReceived += SerialPortDataReceived;
_serialPort.DtrEnable = true;
_serialPort.Handshake = Handshake.RequestToSend;
Debug.WriteLine("PcrComm Created");
Debug.WriteLine("PcrComm Successfully Created");
}

/// <summary>
Expand Down Expand Up @@ -139,7 +139,7 @@ public void SetDebugLogger(bool debug)
private void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs e)
{
#if DEBUG
Debug.WriteLineIf(!_debugLogger, "PcrComm Data Recv");
Debug.WriteLineIf(!_debugLogger, "PcrComm Data Received");
#endif
try
{
Expand Down Expand Up @@ -167,9 +167,9 @@ private void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs e
Debug.WriteLineIf(_debugLogger, _serialPort.PortName + ": RECV -> " + str);
#endif
}
catch (Exception)
catch (Exception ex)
{
Debug.WriteLine("RECV:ERR");
Debug.WriteLine("PcrComm Data Receive Error:\n" + ex.Message + "\n" + ex.StackTrace);
}
}

Expand All @@ -181,7 +181,7 @@ private void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs e
public bool Send(string cmd)
{
#if DEBUG
Debug.WriteLineIf(!_debugLogger, "PcrComm Data Send");
Debug.WriteLineIf(!_debugLogger, "PcrComm Sending Data");
Debug.WriteLineIf(_debugLogger, _serialPort.PortName + ": SEND -> " + cmd);
#endif
try
Expand All @@ -200,8 +200,9 @@ public bool Send(string cmd)
_serialPort.Write(cmd);
return true;
}
catch (Exception)
catch (Exception e)
{
Debug.WriteLine("PcrComm Failed to Send Date:\n" + e.Message + "\n" + e.StackTrace);
return false;
}
}
Expand Down
3 changes: 2 additions & 1 deletion PCR1000LibTest/Control.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading.Tasks;
using System.Windows.Forms;
using PCR1000;
using PCR1000.Network;

namespace PCR1000LibTest
{
Expand All @@ -16,7 +17,7 @@ public Control()
InitializeComponent();
}

readonly PcrControl _pcrControl = new PcrControl(new PcrSerialComm());
readonly PcrControl _pcrControl = new PcrControl(new PcrNetworkClient("192.168.1.9"));

private void button1_Click(object sender, EventArgs e)
{
Expand Down
4 changes: 4 additions & 0 deletions PCR1000LibTest/PCR1000LibTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\PCR1000.Network\PCR1000.Network.csproj">
<Project>{c4792356-055e-4181-9ece-ec75fb04fda0}</Project>
<Name>PCR1000.Network</Name>
</ProjectReference>
<ProjectReference Include="..\PCR1000\PCR1000.csproj">
<Project>{29487920-2926-48b7-bd8c-b2761696663b}</Project>
<Name>PCR1000</Name>
Expand Down
3 changes: 2 additions & 1 deletion PCRNetworkServer/PCRNetworkServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>start C:\Users\Matthew\Documents\Development\PCR1000\put.bat</PostBuildEvent>
<PostBuildEvent>
</PostBuildEvent>
</PropertyGroup>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
3 changes: 1 addition & 2 deletions PCRNetworkServer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ private static void ShowHelp()
null,
"Written by Matthew Knox.",
"Version:\t" + fvi.ProductVersion + " " + (Environment.Is64BitProcess ? "x64" : "x32") +
"\nBuild:\t\tr" + fvi.FileBuildPart +
"\nCLR Version:\t" + Environment.Version +
"\nOS Version:\t" + Environment.OSVersion.VersionString +
"\nReport {appName} bugs and above information to the bug tracker at\n" +
Expand Down Expand Up @@ -65,7 +64,7 @@ public static void Main(string[] args)
try
{
string ui = "cli", password = "", device = null;
var security = true;
var security = false;
var port = 4456;

OptionSet.Add("h|help", "Displays this help.", s => ShowHelp());
Expand Down

0 comments on commit fe551c5

Please sign in to comment.