]> sjero.net Git - strip6in4/blob - strip6in4.c
e15122036e76e368e59a6c6bb49d8a9e1e2b742b
[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
48         /*parse commandline options*/
49         if(argc > 9){
50                 usage();
51         }
52
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*/
58                                 sfile=argv[i];
59                         }else{
60                                 if(dfile==NULL){
61                                         dfile=argv[i]; /*assign second non-dash argument to the output file*/
62                                 }else{
63                                         usage();
64                                 }
65                         }
66                 }else{
67                         if(argv[i][1]=='V' && strlen(argv[i])==2){ /* -V */
68                                 version();
69                         }else if(argv[i][1]=='h' && strlen(argv[i])==2){ /*-h*/
70                                 usage();
71                         }else if(argv[i][1]=='v' && strlen(argv[i])==2){ /*-v*/
72                                 debug++;
73                         }else{
74                                 usage();
75                         }
76                 }
77         }
78         
79         if(sfile==NULL || dfile==NULL){
80                 usage();
81         }
82
83         /*all options validated*/
84
85         if(debug){
86                 dbgprintf(1,"Input file: %s\n", sfile);
87                 dbgprintf(1,"Output file: %s\n", dfile);
88         }
89
90         /*attempt to open input file*/
91         in=pcap_open_offline(dfile, erbuffer);
92         if(in==NULL){
93                 dbgprintf(0,"Error opening input file\n");
94                 exit(1);
95         }
96
97         /*attempt to open output file*/
98         out=pcap_dump_open(in,dfile);
99         if(out==NULL){
100                 dbgprintf(0,"Error opening output file\n");
101                 exit(1);
102         }
103
104         /*process packets*/
105         u_char *user=(u_char*)out;
106         pcap_loop(in, -1, handle_packet, user); 
107         
108         /*close files*/
109         pcap_close(in);
110         pcap_dump_close(out);
111 return 0;
112 }
113
114
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)
117 {
118         int                                     link_type;
119         struct const_packet     old;
120
121         /*Determine the link type for this packet*/
122         link_type=pcap_datalink(in);
123
124         /*Setup packet struct*/
125         old.h=h;
126         old.length=h->caplen;
127         old.data=bytes;
128         old.private=user;
129         
130         /*do all the fancy conversions*/
131         if(!do_encap(link_type, &old)){
132                 return;
133         }
134 return;
135 }
136
137 /*de-encapsulate packet*/
138 int decap_packet(const struct const_packet* old)
139 {
140         struct pcap_pkthdr h;
141
142         if(!old || !old->data || !old->h){
143                 dbgprintf(0,"Error: decap_packet() given bad data!\n");
144                 return 0;
145         }
146
147         h.ts=old->h->ts;
148         h.caplen=old->length;
149         h.len=old->length;
150
151         pcap_dump((u_char*)old->private, &h, old->data);
152 return 0;
153 }
154
155 void version()
156 {
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");
162         exit(0);
163 }
164
165 /*Usage information for program*/
166 void usage()
167 {
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");
172         exit(0);
173 }
174
175 /*Debug Printf*/
176 void dbgprintf(int level, const char *fmt, ...)
177 {
178     va_list args;
179     if(debug>=level){
180         va_start(args, fmt);
181         vfprintf(stderr, fmt, args);
182         va_end(args);
183     }
184 }