Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
*
*/
package org.etsi.its.pcapdump;
import java.io.File;
import java.io.IOException;
import java.net.DatagramPacket;
import java.util.Date;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.etsi.common.ByteHelper;
import org.etsi.its.pcapdump.CmdLineParser.IllegalOptionValueException;
import org.etsi.its.pcapdump.CmdLineParser.UnknownOptionException;
/**
* @author yann
*
*/
public class pcapdump implements ILayer {
/**
* Module version
*/
public static final String Version = "1.0.0.0";
/**
* Logger instance
*/
private final static Logger _logger = Logger.getLogger("org.etsi.its.pcapdump");
private static String NicAddr = "00FF6BADBFC2";
private static String FileName = "itsdump.pcapng";
/**
* Debug mode. Default: false
*/
private static Level Debug = Level.OFF;
/**
* ITS Ehter type
*/
private static short FrameType = (short) 0x8947;
/**
* Default ctor
*/
public pcapdump() {
// Nothing to do
}
@Override
public void dispose() {
PcapMultiplexer.getInstance().unregister(this);
}
/**
* @param args
*/
public static void main(String[] args) {
// Process command line arguments
processCommandLine(args);
// Set traces
_logger.addHandler(new ConsoleHandler());
_logger.setLevel(Debug);
// Check if the file exist, never remove or scratch files
File file = new File(FileName);
if (file.exists()) {
File newFile = new File(FileName.replace(".", String.format("_%d.", System.currentTimeMillis())));
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
file.renameTo(newFile);
}
ILayer layer = new pcapdump();
PcapMultiplexer.getInstance(NicAddr);
PcapMultiplexer.getInstance().register(layer, FrameType);
boolean bContinue = true;
while (bContinue) {
try {
// Check keyboard
if (System.in.available() > 0) {
int token = System.in.read();
switch (token) {
case 'h': // Help
// No break;
case '?': // Help
cmdeLineUsage();
break;
case 'q':
bContinue = false;
_logger.info("Terminate application...");
break;
} // End of 'switch' statement
}
// Wait
java.lang.Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} // End of 'while' statement
layer.dispose();
System.err.println("Terminated");
System.exit(0);
}
/**
* Parse the command line and set options
* @param args Command line arguments
*/
private static void processCommandLine(String[] args) {
// Sanity checks
if (args.length == 0) {
return;
}
CmdLineParser parser = new CmdLineParser();
CmdLineParser.Option debug = parser.addBooleanOption("Debug");
CmdLineParser.Option fileName = parser.addStringOption("FileName");
CmdLineParser.Option frameType = parser.addStringOption("FrameType");
CmdLineParser.Option NICAddr = parser.addStringOption("NicAddr");
try {
parser.parse(args);
} catch (IllegalOptionValueException e) {
e.printStackTrace();
printUsage();
System.exit(2);
} catch (UnknownOptionException e) {
e.printStackTrace();
printUsage();
System.exit(3);
}
Object value;
if ((value = parser.getOptionValue(debug, false)) != null) {
Debug = (((Boolean)value).booleanValue() == true) ? Level.ALL : Level.OFF;
}
if ((value = parser.getOptionValue(fileName, "")) != null) {
if (!((String)value).isEmpty()) {
FileName = (String)value;
}
}
if ((value = parser.getOptionValue(frameType, "")) != null) {
if (!((String)value).isEmpty()) {
FrameType = (short)Integer.parseInt((String)value, 16);
}
}
if ((value = parser.getOptionValue(NICAddr, "")) != null) {
if (!((String)value).isEmpty()) {
NicAddr = (String)value;
}
}
} // End of method processCommandLine
/**
* Display application usage
*/
private static void printUsage() {
System.err.println("Usage: [--Debug (default:false)]\n" +
" [--FileName=filename \"Output file name \"]\n" +
" [--FrameType=0x8947 \"Ethernet prototype\" (0x8947)]\n" +
" \n"
);
}
private static void cmdeLineUsage() {
System.err.println("Command Line Usage: \n" +
// TODO
" [q to exit]\n" +
" \n"
);
}
@Override
public String getFileName() {
return FileName;
}
}