]> sjero.net Git - strip6in4/blob - strip6in4.c
Modify to output RAW packet capture correctly
[strip6in4] / strip6in4.c
1 /******************************************************************************
2 Utility to create a pcap file of a 6in4 stream present in an origin pcap file
3
4 Copyright (C) 2013  Samuel Jero <sj323707@ohio.edu>
5
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.
10
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.
15
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/>.
18
19 Author: Samuel Jero <sj323707@ohio.edu>
20 Date: 03/2013
21 ******************************************************************************/
22 #include "strip6in4.h"
23
24
25 #define STRIP6IN4_VERSION 0.1
26 #define COPYRIGHT_YEAR 2013
27
28
29 pcap_t*                 in;                     /*libpcap input file discriptor*/
30 pcap_dumper_t   *out;           /*libpcap output file discriptor*/
31
32
33 void handle_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *bytes);
34 void version();
35 void usage();
36
37 int debug = 0;
38
39
40 /*Parse commandline options and open files*/
41 int main(int argc, char *argv[])
42 {
43         char ebuf[200];
44         char *erbuffer=ebuf;
45         char *sfile=NULL;
46         char *dfile=NULL;
47         pcap_t* tmp;
48
49         /*parse commandline options*/
50         if(argc > 9){
51                 usage();
52         }
53
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*/
59                                 sfile=argv[i];
60                         }else{
61                                 if(dfile==NULL){
62                                         dfile=argv[i]; /*assign second non-dash argument to the output file*/
63                                 }else{
64                                         usage();
65                                 }
66                         }
67                 }else{
68                         if(argv[i][1]=='V' && strlen(argv[i])==2){ /* -V */
69                                 version();
70                         }else if(argv[i][1]=='h' && strlen(argv[i])==2){ /*-h*/
71                                 usage();
72                         }else if(argv[i][1]=='v' && strlen(argv[i])==2){ /*-v*/
73                                 debug++;
74                         }else{
75                                 usage();
76                         }
77                 }
78         }
79         
80         if(sfile==NULL || dfile==NULL){
81                 usage();
82         }
83
84         /*all options validated*/
85
86         if(debug){
87                 dbgprintf(1,"Input file: %s\n", sfile);
88                 dbgprintf(1,"Output file: %s\n", dfile);
89         }
90
91         /*attempt to open input file*/
92         in=pcap_open_offline(sfile, erbuffer);
93         if(in==NULL){
94                 dbgprintf(0,"Error opening input file\n");
95                 exit(1);
96         }
97
98         /*attempt to open output file*/
99         tmp=pcap_open_dead(DLT_RAW,65535);
100         out=pcap_dump_open(tmp,dfile);
101         if(out==NULL){
102                 dbgprintf(0,"Error opening output file\n");
103                 exit(1);
104         }
105
106         /*process packets*/
107         u_char *user=(u_char*)out;
108         pcap_loop(in, -1, handle_packet, user); 
109         
110         /*close files*/
111         pcap_close(in);
112         pcap_close(tmp);
113         pcap_dump_close(out);
114 return 0;
115 }
116
117
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)
120 {
121         int                                     link_type;
122         struct const_packet     old;
123
124         /*Determine the link type for this packet*/
125         link_type=pcap_datalink(in);
126
127         /*Setup packet struct*/
128         old.h=h;
129         old.length=h->caplen;
130         old.data=bytes;
131         old.private=user;
132         
133         /*do all the fancy conversions*/
134         if(!do_encap(link_type, &old)){
135                 return;
136         }
137 return;
138 }
139
140 /*de-encapsulate packet*/
141 int decap_packet(const struct const_packet* old)
142 {
143         struct pcap_pkthdr h;
144
145         if(!old || !old->data || !old->h){
146                 dbgprintf(0,"Error: decap_packet() given bad data!\n");
147                 return 0;
148         }
149
150         h.ts=old->h->ts;
151         h.caplen=old->length;
152         h.len=old->length;
153
154         pcap_dump((u_char*)old->private, &h, old->data);
155 return 0;
156 }
157
158 void version()
159 {
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");
165         exit(0);
166 }
167
168 /*Usage information for program*/
169 void usage()
170 {
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");
175         exit(0);
176 }
177
178 /*Debug Printf*/
179 void dbgprintf(int level, const char *fmt, ...)
180 {
181     va_list args;
182     if(debug>=level){
183         va_start(args, fmt);
184         vfprintf(stderr, fmt, args);
185         va_end(args);
186     }
187 }