Skip to content

Commit

Permalink
Merge pull request #1 from enlyze/fix/worst-case
Browse files Browse the repository at this point in the history
Handle the worst case of 1-byte requests at 115200 baud.
  • Loading branch information
ColinFinck authored May 26, 2021
2 parents 7115c16 + 8bec978 commit 5407c21
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
10 changes: 8 additions & 2 deletions src/driver/EnlyzePortSniffer.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// PortSniffer - Monitor the traffic of arbitrary serial or parallel ports
// Copyright 2020 Colin Finck, ENLYZE GmbH <[email protected]>
// Copyright 2020-2021 Colin Finck, ENLYZE GmbH <[email protected]>
//
// SPDX-License-Identifier: MIT
//
Expand All @@ -16,9 +16,15 @@

#define CONTROL_DEVICE_NAME_STRING L"\\Device\\EnlyzePortSniffer"
#define CONTROL_SYMBOLIC_LINK_NAME_STRING L"\\DosDevices\\EnlyzePortSniffer"
#define MAX_LOG_ENTRIES_PER_PORT 32
#define POOL_TAG (ULONG)'nSoP'

// The worst case is a serial port at 115200 baud, which is read via 1-byte requests.
// 115200 baud makes 14400 bytes/second. Considering that the PortSniffer-Tool polls the
// driver in 10 millisecond intervals (100 times/second), we need to buffer up to
// 144 1-byte log entries.
// Choose 160 (divisible by 32) as the upper limit here to be on the safe side.
#define MAX_LOG_ENTRIES_PER_PORT 160


typedef struct _PORTLOG_ENTRY
{
Expand Down
19 changes: 14 additions & 5 deletions src/tool/monitoring.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// PortSniffer - Monitor the traffic of arbitrary serial or parallel ports
// Copyright 2020 Colin Finck, ENLYZE GmbH <[email protected]>
// Copyright 2020-2021 Colin Finck, ENLYZE GmbH <[email protected]>
//
// SPDX-License-Identifier: MIT
//
Expand Down Expand Up @@ -138,7 +138,16 @@ HandleMonitorParameter(
&cbReturned,
NULL))
{
fprintf(stderr, "DeviceIoControl failed for PORTSNIFFER_IOCTL_CONTROL_RESET_PORT_MONITORING, last error is %lu.\n", GetLastError());
if (GetLastError() == ERROR_FILE_NOT_FOUND)
{
fprintf(stderr, "The PortSniffer Driver is not attached to %S!\n", pwszPort);
fprintf(stderr, "Please run this tool using the /attach option.\n");
}
else
{
fprintf(stderr, "DeviceIoControl failed for PORTSNIFFER_IOCTL_CONTROL_RESET_PORT_MONITORING, last error is %lu.\n", GetLastError());
}

goto Cleanup;
}

Expand All @@ -154,8 +163,6 @@ HandleMonitorParameter(
// Fetch new port log entries from our driver until we are terminated.
while (!_bTerminationRequested)
{
Sleep(250);

if (!DeviceIoControl(hPortSniffer,
(DWORD)PORTSNIFFER_IOCTL_CONTROL_POP_PORTLOG_ENTRY,
&PopRequest,
Expand All @@ -167,12 +174,14 @@ HandleMonitorParameter(
{
if (GetLastError() == ERROR_NO_MORE_ITEMS)
{
Sleep(10);
continue;
}
else if (GetLastError() == ERROR_FILE_NOT_FOUND)
{
fprintf(stderr, "The PortSniffer Driver is not attached to %S!\n", pwszPort);
fprintf(stderr, "The PortSniffer Driver is no longer attached to %S!\n", pwszPort);
fprintf(stderr, "Please run this tool using the /attach option.\n");
goto Cleanup;
}
else
{
Expand Down

0 comments on commit 5407c21

Please sign in to comment.