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[])
48 /*parse commandline options*/
53 /*loop through commandline options*/
54 for(int i=1; i < argc; i++){
55 if(argv[i][0]!='-' || (argv[i][0]=='-' && strlen(argv[i])==1)){
56 if(sfile==NULL || argv[i][0]=='-'){
57 /*assign first non-dash (or only dash) argument to the input file*/
61 dfile=argv[i]; /*assign second non-dash argument to the output file*/
67 if(argv[i][1]=='V' && strlen(argv[i])==2){ /* -V */
69 }else if(argv[i][1]=='h' && strlen(argv[i])==2){ /*-h*/
71 }else if(argv[i][1]=='v' && strlen(argv[i])==2){ /*-v*/
79 if(sfile==NULL || dfile==NULL){
83 /*all options validated*/
86 dbgprintf(1,"Input file: %s\n", sfile);
87 dbgprintf(1,"Output file: %s\n", dfile);
90 /*attempt to open input file*/
91 in=pcap_open_offline(dfile, erbuffer);
93 dbgprintf(0,"Error opening input file\n");
97 /*attempt to open output file*/
98 out=pcap_dump_open(in,dfile);
100 dbgprintf(0,"Error opening output file\n");
105 u_char *user=(u_char*)out;
106 pcap_loop(in, -1, handle_packet, user);
110 pcap_dump_close(out);
115 /*call back function for pcap_loop--do basic packet handling*/
116 void handle_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *bytes)
119 struct const_packet old;
121 /*Determine the link type for this packet*/
122 link_type=pcap_datalink(in);
124 /*Setup packet struct*/
126 old.length=h->caplen;
130 /*do all the fancy conversions*/
131 if(!do_encap(link_type, &old)){
137 /*de-encapsulate packet*/
138 int decap_packet(const struct const_packet* old)
140 struct pcap_pkthdr h;
142 if(!old || !old->data || !old->h){
143 dbgprintf(0,"Error: decap_packet() given bad data!\n");
148 h.caplen=old->length;
151 pcap_dump((u_char*)old->private, &h, old->data);
157 dbgprintf(0, "strip6in4 version %.1f\n",STRIP6IN4_VERSION);
158 dbgprintf(0, "Copyright (C) %i Samuel Jero <sj323707@ohio.edu>\n",COPYRIGHT_YEAR);
159 dbgprintf(0, "This program comes with ABSOLUTELY NO WARRANTY.\n");
160 dbgprintf(0, "This is free software, and you are welcome to\n");
161 dbgprintf(0, "redistribute it under certain conditions.\n");
165 /*Usage information for program*/
168 dbgprintf(0,"Usage: strip6in4 [-v] [-h] [-V] input_file output_file\n");
169 dbgprintf(0, " -v verbose. May be repeated for additional verbosity.\n");
170 dbgprintf(0, " -V Version information\n");
171 dbgprintf(0, " -h Help\n");
176 void dbgprintf(int level, const char *fmt, ...)
181 vfprintf(stderr, fmt, args);