net/readcapfile/readcapfile_example.c

Reference Documentation

product_line_custom
Napatech SmartNIC
category
Reference Information

Description

This source file is an example of how to read a capture file using NTAPI.

The NT_NetFile...() functions does not require NTservice to be running.

Prerequisites

  • Capture file, that has been captured with the net/capture/capture_example.c example.

  • The capture filename should be given as first argument to this example.

Program flow

The following is required to perform replay of a captured file

  • #include/nt.h - Applications/Tools only need to include nt.h to obtain prototypes, macros etc. from NTAPI.

  • NT_Init(NTAPI_VERSION) - Initialize the NTAPI library. NTAPI_VERSION is a define that describes the version of the API described in the header files included by nt.h. NT_Init() will ask the NTAPI library to convert return data to the NTAPI_VERSION if possible. This will ensure that applications can run on NTAPI libraries of newer versions.

  • NT_NetFileOpen() - Open the captured file and assign it to a stream.

  • NT_NetFileGet() - Get a segment from the file. This call will return NT_SUCCESS upon return of a segment and NT_STATUS_END_OF_FILE when there is no more segments avaialble. The latter will cause the example to exit.

  • NT_NetFileRelease() - Release the segment from the file stream.

  • NT_NetFileClose() - Close the file stream when no more segments can be found.

Code

/* * %NT_SOFTWARE_LICENSE% */ /** * * @example net/readcapfile/readcapfile_example.c * * @section readcapfile_example_description Description * * This source file is an example of how to read a capture file using NTAPI. * * The NT_NetFile...() functions does not require NTservice to be running. * * The following NTAPI functions are used: * - @ref NT_Init() * - @ref NT_NetFileOpen() * - @ref NT_NetFileGet() * - @ref NT_NET_GET_PKT_WIRE_LENGTH() * - @ref NT_NetFileRelease() * - @ref NT_NetFileClose() * - @ref NT_ExplainError() * * @section readcapfile_example_prerequisites Prerequisites * * - Capture file, that has been captured with the @ref * net/capture/capture_example.c "net/capture/capture_example.c" example. * * - The capture filename should be given as first argument to this example. * * @section readcapfile_example_flow Program flow * @{ * The following is required to perform replay of a captured file * * - \#include/nt.h - Applications/Tools only need to include @ref nt.h * to obtain prototypes, macros etc. from NTAPI. * - @ref NT_Init(@ref NTAPI_VERSION) - Initialize the NTAPI * library. @ref NTAPI_VERSION is a define that describes the version * of the API described in the header files included by @ref * nt.h. NT_Init() will ask the NTAPI library to convert return data * to the @ref NTAPI_VERSION if possible. This will ensure that * applications can run on NTAPI libraries of newer versions. * - @ref NT_NetFileOpen() - Open the captured file and assign it to a stream. * - @ref NT_NetFileGet() - Get a segment from the file. This call will * return @ref NT_SUCCESS upon return of a segment and * @ref NT_STATUS_END_OF_FILE when there is no more segments * avaialble. The latter will cause the example to exit. * - @ref NT_NetFileRelease() - Release the segment from the file stream. * - @ref NT_NetFileClose() - Close the file stream when no more segments can be found. * *<hr> * * @section readcapfile_example_code Code * @} * */ #include <argparse.h> #include <nt.h> static const char *usageText[] = { "USAGE: readcapfile_example <nt3gd_capture_filename>\n" "Commands:\n", NULL}; /** * Table of valid options. */ struct argparse_option arg_options[] = { OPT_HELP(), OPT_END(), }; int main(int argc, const char** argv) { int status; // Status variable char errorBuffer[NT_ERRBUF_SIZE]; // Error buffer int numPackets = 0; // The number of packets replayed int numBytes = 0; // The number of bytes replayed int pktLen = 0; // packet lenght int option; const char* ntcap_filename = NULL; NtNetStreamFile_t hNetFile; // Handle to the File stream NtNetBuf_t hNetBufFile; // Net buffer container. Used to return segments from the file stream struct argparse argparse; argparse_init(&argparse, arg_options, usageText, 0); option = argparse_parse(&argparse, argc, argv); if ((option >= 1) && (argv[0] != NULL)) { ntcap_filename = argv[0]; } else { argparse_usage(&argparse); exit(0); } // Initialize the NTAPI library and thereby check if NTAPI_VERSION can be used together with this library if ((status = NT_Init(NTAPI_VERSION)) != NT_SUCCESS) { if ( status == NT_ERROR_NT_SERVICE_NOT_STARTED) { printf("NOTE: NT serivice is not started.\n"); } else { // Get the status code as text NT_ExplainError(status, errorBuffer, sizeof(errorBuffer)); fprintf(stderr, "NT_Init() failed: %s\n", errorBuffer); return -1; } } // Open the capture file to replay (captured with the capture example) if ((status = NT_NetFileOpen(&hNetFile, "FileStream", NT_NET_INTERFACE_PACKET, ntcap_filename)) != NT_SUCCESS) { // Get the status code as text NT_ExplainError(status, errorBuffer, sizeof(errorBuffer)); fprintf(stderr, "NT_NetFileOpen() failed: %s\n", errorBuffer); return -1; } // Get packets from the file - update counters while (1) { // Get the packet if ((status = NT_NetFileGet(hNetFile, &hNetBufFile)) != NT_SUCCESS) { if (status == NT_STATUS_END_OF_FILE) { // The file has no more data break; } // Get the status code as text NT_ExplainError(status, errorBuffer, sizeof(errorBuffer)); fprintf(stderr, "NT_NetFileGet() failed: %s\n", errorBuffer); return -1; } numPackets++, pktLen = NT_NET_GET_PKT_WIRE_LENGTH((hNetBufFile)); numBytes += pktLen; printf("Fetched packet: #%d: packet length: %d bytes - total capture length: %d bytes\n", numPackets, pktLen, numBytes); // Release the file packet if ((status = NT_NetFileRelease(hNetFile, hNetBufFile)) != NT_SUCCESS) { // Get the status code as text NT_ExplainError(status, errorBuffer, sizeof(errorBuffer)); fprintf(stderr, "NT_NetFileRelease() failed: %s\n", errorBuffer); return -1; } } // Close the file stream NT_NetFileClose(hNetFile); printf("Done: %d packets %d bytes has been read\n", numPackets, numBytes); return 0; } // // EOF //