2 * pcap2cinit.c: convert pcap input to a u8 ** initializer
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @brief Functions to convert PCAP file format to a u8 ** initializer
22 #include <vppinfra/pcap.h>
23 #include <vnet/ethernet/packet.h>
26 pcap_main_t pcap_main;
29 * @brief Conversion of PCAP file to u8 ** initializer stanzas
31 * @param *pm - pcap_main_t
38 pcap2cinit (pcap_main_t * pm, FILE * ofp)
43 for (i = 0; i < vec_len (pm->packets_read); i++)
45 pkt = (u8 *) pm->packets_read[i];
47 fformat (ofp, "static u8 __pcap_pkt%d [] = {\n ", i);
49 for (j = 0; j < vec_len (pkt); j++)
51 if (((j + 1) % 8) == 0)
52 fformat (ofp, "0x%02x,\n ", pkt[j]);
54 fformat (ofp, "0x%02x, ", pkt[j]);
56 fformat (ofp, "\n};\n");
59 fformat (ofp, "static u8 *__pcap_pkts [] = {\n");
61 for (i = 0; i < vec_len (pm->packets_read); i++)
62 fformat (ofp, " __pcap_pkt%d, \n", i);
64 fformat (ofp, "};\n");
71 * usage: pcap2pg -i <input-file> [-o <output-file>]
74 main (int argc, char **argv)
76 unformat_input_t input;
77 pcap_main_t *pm = &pcap_main;
78 u8 *input_file = 0, *output_file = 0;
82 unformat_init_command_line (&input, argv);
84 while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
86 if (unformat (&input, "-i %s", &input_file)
87 || unformat (&input, "input %s", &input_file))
89 else if (unformat (&input, "-o %s", &output_file)
90 || unformat (&input, "output %s", &output_file))
96 "usage: pcap2pg -i <input-file> [-o <output-file>]\n");
104 pm->file_name = (char *) input_file;
105 error = pcap_read (pm);
109 clib_error_report (error);
115 ofp = fopen ((char *) output_file, "w+");
118 clib_unix_warning ("Couldn't create '%s'", output_file);
127 pcap2cinit (pm, ofp);
134 * fd.io coding-style-patch-verification: ON
137 * eval: (c-set-style "gnu")