readcapfile_example.c Source File

Reference Documentation

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