Skip to content

Commit

Permalink
Update pcap_fopen example for VS2015
Browse files Browse the repository at this point in the history
  • Loading branch information
bonsaiviking committed Aug 17, 2018
1 parent 50f101e commit 32c2b94
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 376 deletions.
10 changes: 10 additions & 0 deletions Examples/MakeAll.sln
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "TestPacketSend", "PacketDri
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "pcap_filter", "pcap_filter\pcap_filter.vcxproj", "{B9F68F6F-22AE-47A2-A3A3-1212831AF9FF}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "pcap_fopen", "pcap_fopen\pcap_fopen.vcxproj", "{DD317CB5-6337-498F-A560-553B5558E133}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Expand Down Expand Up @@ -71,6 +73,14 @@ Global
{B9F68F6F-22AE-47A2-A3A3-1212831AF9FF}.Release|x64.Build.0 = Release|x64
{B9F68F6F-22AE-47A2-A3A3-1212831AF9FF}.Release|x86.ActiveCfg = Release|Win32
{B9F68F6F-22AE-47A2-A3A3-1212831AF9FF}.Release|x86.Build.0 = Release|Win32
{DD317CB5-6337-498F-A560-553B5558E133}.Debug|x64.ActiveCfg = Debug|x64
{DD317CB5-6337-498F-A560-553B5558E133}.Debug|x64.Build.0 = Debug|x64
{DD317CB5-6337-498F-A560-553B5558E133}.Debug|x86.ActiveCfg = Debug|Win32
{DD317CB5-6337-498F-A560-553B5558E133}.Debug|x86.Build.0 = Debug|Win32
{DD317CB5-6337-498F-A560-553B5558E133}.Release|x64.ActiveCfg = Release|x64
{DD317CB5-6337-498F-A560-553B5558E133}.Release|x64.Build.0 = Release|x64
{DD317CB5-6337-498F-A560-553B5558E133}.Release|x86.ActiveCfg = Release|Win32
{DD317CB5-6337-498F-A560-553B5558E133}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
48 changes: 36 additions & 12 deletions Examples/pcap_fopen/pcap_fopen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,40 +36,64 @@
#include <pcap.h>
#include <stdio.h>

BOOL LoadNpcapDlls()
{
_TCHAR npcap_dir[512];
UINT len;
len = GetSystemDirectory(npcap_dir, 480);
if (!len) {
fprintf(stderr, "Error in GetSystemDirectory: %x", GetLastError());
return FALSE;
}
_tcscat_s(npcap_dir, 512, _T("\\Npcap"));
if (SetDllDirectory(npcap_dir) == 0) {
fprintf(stderr, "Error in SetDllDirectory: %x", GetLastError());
return FALSE;
}
return TRUE;
}

/** Prints packet timestaps regardless of format*/
int _tmain(int argc, _TCHAR* argv[])
{
char errbuf[PCAP_ERRBUF_SIZE];
wchar_t cmd[1024];
wchar_t tshark_path[MAX_PATH];
wchar_t file_path[MAX_PATH];
_TCHAR cmd[1024];
_TCHAR tshark_path[MAX_PATH];
_TCHAR file_path[MAX_PATH];

/* Load Npcap and its functions. */
if (!LoadNpcapDlls())
{
fprintf(stderr, "Couldn't load Npcap\n");
exit(1);
}

if ( argc != 3 ) {
wprintf(L"Prints packet timestaps regardless of format.\n");
wprintf(L"Usage:\n\t%ls <tshark path> <trace file>\n", argv[0]);
_tprintf(_T("Prints packet timestaps regardless of format.\n"));
_tprintf(_T("Usage:\n\t%s <tshark path> <trace file>\n"), argv[0]);
return 1;
}

// conversion to short path name in case there are spaces
if ( ! GetShortPathNameW(argv[1], tshark_path, MAX_PATH) ||
! GetShortPathNameW(argv[2], file_path, MAX_PATH) )
if ( ! GetShortPathName(argv[1], tshark_path, MAX_PATH) ||
! GetShortPathName(argv[2], file_path, MAX_PATH) )
{
printf("Failed to convert paths to short form.");
_tprintf(_T("Failed to convert paths to short form."));
return 1;
}

// create tshark command, which will make the trace conversion and print in libpcap format to stdout
if ( swprintf_s(cmd, 1024, L"%ls -r %ls -w - -F libpcap", tshark_path, file_path) < 0 ) {
wprintf(L"Failed to create command\n");
if ( _stprintf_s(cmd, 1024, _T("%s -r %s -w - -F libpcap"), tshark_path, file_path) < 0 ) {
_tprintf(_T("Failed to create command\n"));
return 1;
}

// start tshark
FILE *tshark_out = _wpopen(cmd, L"rb");
FILE *tshark_out = _tpopen(cmd, _T("rb"));
if ( tshark_out == NULL ) {
strerror_s(errbuf, PCAP_ERRBUF_SIZE, errno);
printf("Failed run tshark: %s\n", errbuf);
wprintf(L"Command: %ls", cmd);
_tprintf(_T("Command: %s"), cmd);
return 1;
}

Expand Down
Loading

0 comments on commit 32c2b94

Please sign in to comment.