Dave Barach | a0cc5ab | 2018-02-23 12:09:41 -0500 | [diff] [blame] | 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 | */ |
Dave Barach | 3ae2873 | 2018-11-16 17:19:00 -0500 | [diff] [blame] | 22 | #include <vppinfra/pcap.h> |
Dave Barach | a0cc5ab | 2018-02-23 12:09:41 -0500 | [diff] [blame] | 23 | #include <vnet/ethernet/packet.h> |
| 24 | #include <stdio.h> |
| 25 | |
| 26 | pcap_main_t pcap_main; |
| 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 | |
| 84 | while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT) |
| 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 | */ |