readcapfile_example.c Source File

Reference Documentation

Platform
Napatech SmartNIC
Content Type
Reference Information
Capture Software Version
Link™ Capture Software 12.15
Napatech Software Suite: examples/net/readcapfile/readcapfile_example.c Source File
readcapfile_example.c
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2025 Napatech A/S. All Rights Reserved.
4  *
5  * 1. Copying, modification, and distribution of this file, or executable
6  * versions of this file, is governed by the terms of the Napatech Software
7  * license agreement under which this file was made available. If you do not
8  * agree to the terms of the license do not install, copy, access or
9  * otherwise use this file.
10  *
11  * 2. Under the Napatech Software license agreement you are granted a
12  * limited, non-exclusive, non-assignable, copyright license to copy, modify
13  * and distribute this file in conjunction with Napatech SmartNIC's and
14  * similar hardware manufactured or supplied by Napatech A/S.
15  *
16  * 3. The full Napatech Software License Agreement is included in this
17  * distribution, please see "NA-0009 Software License Agreement.pdf"
18  *
19  * 4. Redistributions of source code must retain this copyright notice,
20  * list of conditions and the following disclaimer.
21  *
22  * THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTIES, EXPRESS OR
23  * IMPLIED, AND NAPATECH DISCLAIMS ALL IMPLIED WARRANTIES INCLUDING ANY
24  * IMPLIED WARRANTY OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, OR OF
25  * FITNESS FOR A PARTICULAR PURPOSE. TO THE EXTENT NOT PROHIBITED BY
26  * APPLICABLE LAW, IN NO EVENT SHALL NAPATECH BE LIABLE FOR PERSONAL INJURY,
27  * OR ANY INCIDENTAL, SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES WHATSOEVER,
28  * INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF PROFITS, CORRUPTION OR
29  * LOSS OF DATA, FAILURE TO TRANSMIT OR RECEIVE ANY DATA OR INFORMATION,
30  * BUSINESS INTERRUPTION OR ANY OTHER COMMERCIAL DAMAGES OR LOSSES, ARISING
31  * OUT OF OR RELATED TO YOUR USE OR INABILITY TO USE NAPATECH SOFTWARE OR
32  * SERVICES OR ANY THIRD PARTY SOFTWARE OR APPLICATIONS IN CONJUNCTION WITH
33  * THE NAPATECH SOFTWARE OR SERVICES, HOWEVER CAUSED, REGARDLESS OF THE THEORY
34  * OF LIABILITY (CONTRACT, TORT OR OTHERWISE) AND EVEN IF NAPATECH HAS BEEN
35  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME JURISDICTIONS DO NOT ALLOW
36  * THE EXCLUSION OR LIMITATION OF LIABILITY FOR PERSONAL INJURY, OR OF
37  * INCIDENTAL OR CONSEQUENTIAL DAMAGES, SO THIS LIMITATION MAY NOT APPLY TO YOU.
38  *
39  *
40 
41  */
42 
43 /**
44  *
45  * @example net/readcapfile/readcapfile_example.c
46  *
47  * @section readcapfile_example_description Description
48  *
49  * This source file is an example of how to read a capture file using NTAPI.
50  *
51  * The NT_NetFile...() functions does not require NTservice to be running.
52  *
53  * The following NTAPI functions are used:
54  * - @ref NT_Init()
55  * - @ref NT_NetFileOpen()
56  * - @ref NT_NetFileGet()
57  * - @ref NT_NET_GET_PKT_WIRE_LENGTH()
58  * - @ref NT_NetFileRelease()
59  * - @ref NT_NetFileClose()
60  * - @ref NT_ExplainError()
61  *
62  * @section readcapfile_example_prerequisites Prerequisites
63  *
64  * - Capture file, that has been captured with the @ref
65  * net/capture/capture_example.c "net/capture/capture_example.c" example.
66  *
67  * - The capture filename should be given as first argument to this example.
68  *
69  * @section readcapfile_example_flow Program flow
70  * @{
71  * The following is required to perform replay of a captured file
72  *
73  * - \#include/nt.h - Applications/Tools only need to include @ref nt.h
74  * to obtain prototypes, macros etc. from NTAPI.
75  * - @ref NT_Init(@ref NTAPI_VERSION) - Initialize the NTAPI
76  * library. @ref NTAPI_VERSION is a define that describes the version
77  * of the API described in the header files included by @ref
78  * nt.h. NT_Init() will ask the NTAPI library to convert return data
79  * to the @ref NTAPI_VERSION if possible. This will ensure that
80  * applications can run on NTAPI libraries of newer versions.
81  * - @ref NT_NetFileOpen() - Open the captured file and assign it to a stream.
82  * - @ref NT_NetFileGet() - Get a segment from the file. This call will
83  * return @ref NT_SUCCESS upon return of a segment and
84  * @ref NT_STATUS_END_OF_FILE when there is no more segments
85  * avaialble. The latter will cause the example to exit.
86  * - @ref NT_NetFileRelease() - Release the segment from the file stream.
87  * - @ref NT_NetFileClose() - Close the file stream when no more segments can be found.
88  *
89  *<hr>
90  *
91  * @section readcapfile_example_code Code
92  * @}
93  *
94  */
95 
96 // Include this in order to access the Napatech API
97 #include <nt.h>
98 
99 #include <argparse.h>
100 
101 static const char *usageText[] = {
102  "USAGE: readcapfile_example <nt3gd_capture_filename>\n"
103  "Commands:\n",
104  NULL};
105 
106 /**
107  * Table of valid options.
108  */
110  OPT_HELP(),
111  OPT_END(),
112 };
113 
114 
115 int main(int argc, const char** argv)
116 {
117  int status; // Status variable
118  char errorBuffer[NT_ERRBUF_SIZE]; // Error buffer
119  int numPackets = 0; // The number of packets replayed
120  int numBytes = 0; // The number of bytes replayed
121  int pktLen = 0; // packet lenght
122  int option;
123  const char* ntcap_filename = NULL;
124  NtNetStreamFile_t hNetFile; // Handle to the File stream
125  NtNetBuf_t hNetBufFile; // Net buffer container. Used to return segments from the file stream
126  struct argparse argparse;
127 
128  argparse_init(&argparse, arg_options, usageText, 0);
129  option = argparse_parse(&argparse, argc, argv);
130 
131  if ((option >= 1) && (argv[0] != NULL)) {
132  ntcap_filename = argv[0];
133  } else {
134  argparse_usage(&argparse);
135  exit(0);
136  }
137 
138  // Initialize the NTAPI library and thereby check if NTAPI_VERSION can be used together with this library
139  if ((status = NT_Init(NTAPI_VERSION)) != NT_SUCCESS) {
140  if ( status == NT_ERROR_NT_SERVICE_NOT_STARTED) {
141  printf("NOTE: NT serivice is not started.\n");
142  } else {
143  // Get the status code as text
144  NT_ExplainError(status, errorBuffer, sizeof(errorBuffer));
145  fprintf(stderr, "NT_Init() failed: %s\n", errorBuffer);
146  return -1;
147  }
148  }
149 
150  // Open the capture file to replay (captured with the capture example)
151  if ((status = NT_NetFileOpen(&hNetFile, "FileStream", NT_NET_INTERFACE_PACKET, ntcap_filename)) != NT_SUCCESS) {
152  // Get the status code as text
153  NT_ExplainError(status, errorBuffer, sizeof(errorBuffer));
154  fprintf(stderr, "NT_NetFileOpen() failed: %s\n", errorBuffer);
155  return -1;
156  }
157 
158  // Get packets from the file - update counters
159  while (1) {
160  // Get the packet
161  if ((status = NT_NetFileGet(hNetFile, &hNetBufFile)) != NT_SUCCESS) {
162  if (status == NT_STATUS_END_OF_FILE) {
163  // The file has no more data
164  break;
165  }
166  // Get the status code as text
167  NT_ExplainError(status, errorBuffer, sizeof(errorBuffer));
168  fprintf(stderr, "NT_NetFileGet() failed: %s\n", errorBuffer);
169  return -1;
170  }
171  numPackets++,
172  pktLen = NT_NET_GET_PKT_WIRE_LENGTH((hNetBufFile));
173  numBytes += pktLen;
174  printf("Fetched packet: #%d: packet length: %d bytes - total capture length: %d bytes\n", numPackets, pktLen, numBytes);
175 
176  // Release the file packet
177  if ((status = NT_NetFileRelease(hNetFile, hNetBufFile)) != NT_SUCCESS) {
178  // Get the status code as text
179  NT_ExplainError(status, errorBuffer, sizeof(errorBuffer));
180  fprintf(stderr, "NT_NetFileRelease() failed: %s\n", errorBuffer);
181  return -1;
182  }
183  }
184 
185  // Close the file stream
186  NT_NetFileClose(hNetFile);
187 
188  printf("Done: %d packets %d bytes has been read\n", numPackets, numBytes);
189  return 0;
190 }
191 
192 //
193 // EOF
194 //