1 /******************************************************************************
2 Utility to create a pcap file of a 6in4 stream present in an origin pcap file
4 Copyright (C) 2013 Samuel Jero <sj323707@ohio.edu>
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 Author: Samuel Jero <sj323707@ohio.edu>
21 ******************************************************************************/
22 #include "strip6in4.h"
25 #define STRIP6IN4_VERSION 0.1
26 #define COPYRIGHT_YEAR 2013
29 pcap_t* in; /*libpcap input file discriptor*/
30 pcap_dumper_t *out; /*libpcap output file discriptor*/
33 void handle_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *bytes);
40 /*Parse commandline options and open files*/
41 int main(int argc, char *argv[])
49 /*parse commandline options*/
54 /*loop through commandline options*/
55 for(int i=1; i < argc; i++){
56 if(argv[i][0]!='-' || (argv[i][0]=='-' && strlen(argv[i])==1)){
57 if(sfile==NULL || argv[i][0]=='-'){
58 /*assign first non-dash (or only dash) argument to the input file*/
62 dfile=argv[i]; /*assign second non-dash argument to the output file*/
68 if(argv[i][1]=='V' && strlen(argv[i])==2){ /* -V */
70 }else if(argv[i][1]=='h' && strlen(argv[i])==2){ /*-h*/
72 }else if(argv[i][1]=='v' && strlen(argv[i])==2){ /*-v*/
80 if(sfile==NULL || dfile==NULL){
84 /*all options validated*/
87 dbgprintf(1,"Input file: %s\n", sfile);
88 dbgprintf(1,"Output file: %s\n", dfile);
91 /*attempt to open input file*/
92 in=pcap_open_offline(sfile, erbuffer);
94 dbgprintf(0,"Error opening input file\n");
98 /*attempt to open output file*/
99 tmp=pcap_open_dead(DLT_RAW,65535);
100 out=pcap_dump_open(tmp,dfile);
102 dbgprintf(0,"Error opening output file\n");
107 u_char *user=(u_char*)out;
108 pcap_loop(in, -1, handle_packet, user);
113 pcap_dump_close(out);
118 /*call back function for pcap_loop--do basic packet handling*/
119 void handle_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *bytes)
122 struct const_packet old;
124 /*Determine the link type for this packet*/
125 link_type=pcap_datalink(in);
127 /*Setup packet struct*/
129 old.length=h->caplen;
133 /*do all the fancy conversions*/
134 if(!do_encap(link_type, &old)){
140 /*de-encapsulate packet*/
141 int decap_packet(const struct const_packet* old)
143 struct pcap_pkthdr h;
145 if(!old || !old->data || !old->h){
146 dbgprintf(0,"Error: decap_packet() given bad data!\n");
151 h.caplen=old->length;
154 pcap_dump((u_char*)old->private, &h, old->data);
160 dbgprintf(0, "strip6in4 version %.1f\n",STRIP6IN4_VERSION);
161 dbgprintf(0, "Copyright (C) %i Samuel Jero <sj323707@ohio.edu>\n",COPYRIGHT_YEAR);
162 dbgprintf(0, "This program comes with ABSOLUTELY NO WARRANTY.\n");
163 dbgprintf(0, "This is free software, and you are welcome to\n");
164 dbgprintf(0, "redistribute it under certain conditions.\n");
168 /*Usage information for program*/
171 dbgprintf(0,"Usage: strip6in4 [-v] [-h] [-V] input_file output_file\n");
172 dbgprintf(0, " -v verbose. May be repeated for additional verbosity.\n");
173 dbgprintf(0, " -V Version information\n");
174 dbgprintf(0, " -h Help\n");
179 void dbgprintf(int level, const char *fmt, ...)
184 vfprintf(stderr, fmt, args);