FD.io VPP  v20.05.1-6-gf53edbc3b
Vector Packet Processing
pcap2cinit.c
Go to the documentation of this file.
1 /*
2  * pcap2cinit.c: convert pcap input to a u8 ** initializer
3  *
4  * Copyright (c) 2018 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 /**
18  * @file
19  * @brief Functions to convert PCAP file format to a u8 ** initializer
20  *
21  */
22 #include <vppinfra/pcap.h>
23 #include <vnet/ethernet/packet.h>
24 #include <stdio.h>
25 
27 
28 /**
29  * @brief Conversion of PCAP file to u8 ** initializer stanzas
30  *
31  * @param *pm - pcap_main_t
32  * @param *ofp - FILE
33  *
34  * @return rc - int
35  *
36  */
37 int
38 pcap2cinit (pcap_main_t * pm, FILE * ofp)
39 {
40  int i, j;
41  u8 *pkt;
42 
43  for (i = 0; i < vec_len (pm->packets_read); i++)
44  {
45  pkt = (u8 *) pm->packets_read[i];
46 
47  fformat (ofp, "static u8 __pcap_pkt%d [] = {\n ", i);
48 
49  for (j = 0; j < vec_len (pkt); j++)
50  {
51  if (((j + 1) % 8) == 0)
52  fformat (ofp, "0x%02x,\n ", pkt[j]);
53  else
54  fformat (ofp, "0x%02x, ", pkt[j]);
55  }
56  fformat (ofp, "\n};\n");
57  }
58 
59  fformat (ofp, "static u8 *__pcap_pkts [] = {\n");
60 
61  for (i = 0; i < vec_len (pm->packets_read); i++)
62  fformat (ofp, " __pcap_pkt%d, \n", i);
63 
64  fformat (ofp, "};\n");
65 
66  return 0;
67 }
68 
69 /**
70  * @brief pcap2pg.
71  * usage: pcap2pg -i <input-file> [-o <output-file>]
72  */
73 int
74 main (int argc, char **argv)
75 {
76  unformat_input_t input;
77  pcap_main_t *pm = &pcap_main;
78  u8 *input_file = 0, *output_file = 0;
79  FILE *ofp;
80  clib_error_t *error;
81 
82  unformat_init_command_line (&input, argv);
83 
85  {
86  if (unformat (&input, "-i %s", &input_file)
87  || unformat (&input, "input %s", &input_file))
88  ;
89  else if (unformat (&input, "-o %s", &output_file)
90  || unformat (&input, "output %s", &output_file))
91  ;
92  else
93  {
94  usage:
95  fformat (stderr,
96  "usage: pcap2pg -i <input-file> [-o <output-file>]\n");
97  exit (1);
98  }
99  }
100 
101  if (input_file == 0)
102  goto usage;
103 
104  pm->file_name = (char *) input_file;
105  error = pcap_read (pm);
106 
107  if (error)
108  {
109  clib_error_report (error);
110  exit (1);
111  }
112 
113  if (output_file)
114  {
115  ofp = fopen ((char *) output_file, "w+");
116  if (ofp == NULL)
117  {
118  clib_unix_warning ("Couldn't create '%s'", output_file);
119  exit (1);
120  }
121  }
122  else
123  {
124  ofp = stdout;
125  }
126 
127  pcap2cinit (pm, ofp);
128 
129  fclose (ofp);
130  exit (0);
131 }
132 
133 /*
134  * fd.io coding-style-patch-verification: ON
135  *
136  * Local Variables:
137  * eval: (c-set-style "gnu")
138  * End:
139  */
char * file_name
File name of pcap output.
Definition: pcap.h:162
int pcap2cinit(pcap_main_t *pm, FILE *ofp)
Conversion of PCAP file to u8 ** initializer stanzas.
Definition: pcap2cinit.c:38
PCAP utility definitions.
static void usage(void)
Definition: health_check.c:14
unsigned char u8
Definition: types.h:56
PCAP main state data structure.
Definition: pcap.h:156
struct _unformat_input_t unformat_input_t
void unformat_init_command_line(unformat_input_t *input, char *argv[])
Definition: unformat.c:1013
clib_error_t * pcap_read(pcap_main_t *pm)
Read PCAP file.
Definition: pcap.c:176
word fformat(FILE *f, char *fmt,...)
Definition: format.c:462
#define UNFORMAT_END_OF_INPUT
Definition: format.h:145
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:317
#define clib_error_report(e)
Definition: error.h:113
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
#define clib_unix_warning(format, args...)
Definition: error.h:68
u8 ** packets_read
Packets read from file.
Definition: pcap.h:187
int main(int argc, char **argv)
pcap2pg.
Definition: pcap2cinit.c:74
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:171
pcap_main_t pcap_main
Definition: pcap2cinit.c:26