1 /******************************************************************************
4 Lantiq Deutschland GmbH
6 For licensing information, see the file 'LICENSE' in the root folder of
9 ******************************************************************************/
20 #include "ParamInfo.h"
22 #include "aux_utils.h"
34 #define MTLK_LOGGER_NETWORK 147 /* Corresponds to wtap 45 - WTAP_ENCAP_USER0 */
35 #define MTLK_LOGGER_SNAPLEN 65535
37 #define SCD_FILES_DELIM ","
39 static const pcap_hdr_t log_pcap_hdr
=
69 #define LOCAL_DBG_TRACE(x) DBG_TRACE("logcnv: " << x)
71 #define LOCAL_DBG_TRACE(x)
74 static const ParamInfo
paramShowHelp(CCmdLine::ParamName("h", "help"),
76 static const ParamInfo
paramDebugLevel(CCmdLine::ParamName("d", "debug"),
77 "Application debug level (for debugging purposes only)",
79 static const ParamInfo
paramStringFiles(CCmdLine::ParamName("s", "str-file"),
80 "Compilation extracted string-file (.scd)",
81 "file1[,file2[,...]]");
82 static const ParamInfo
paramPCapOut(CCmdLine::ParamName("p", "pcap"),
83 "Output in PCAP format");
84 static const ParamInfo
paramInFile(CCmdLine::ParamName("i", "input"),
85 "Log file to read (default: stdin)",
87 static const ParamInfo
paramOutFile(CCmdLine::ParamName("o", "output"),
88 "Output file (default: stdout)",
91 #define stream_enable_exceptions(s) (s).exceptions(ios::badbit | ios::failbit)
96 static const ParamInfo
* AllParams
[] = {¶mShowHelp
,
103 CHelpScreen HelpScreen
;
105 cerr
<< "Metalink Log Convertor Utility v." MTLK_SOURCE_VERSION
107 << "Usage:" << endl
<< "\tlogcnv ";
109 for (size_t i
= 0 ; i
< ARRAY_SIZE(AllParams
); i
++) {
110 HelpScreen
.AddParam(*AllParams
[i
]);
113 cerr
<< HelpScreen
.GetHelp();
117 WriteString (string
& str
, ostream
&out_s
)
119 uint16 size
= str
.length() + 1;
120 uint16 sz_h
= HOST_TO_NET16(size
);
121 out_s
.write((const char *)&sz_h
, sizeof(sz_h
));
122 out_s
.write(str
.c_str(), size
);
126 ProcessLog (istream
&in_s
, ostream
&out_s
, vector
<string
> &scd_files
, bool pcap_out
)
130 pcaprec_hdr_t pcaprec_hdr
;
131 mtlklog_hdr_t mtlklog_hdr
;
135 for (vector
<string
>::iterator it
= scd_files
.begin(); it
!= scd_files
.end(); ++it
) {
139 /* Put the PCAP file header */
141 out_s
.write((const char *)&log_pcap_hdr
, sizeof(log_pcap_hdr
));
144 while (!in_s
.eof()) {
148 if (log_evt
.HasData()) {
150 out_s
<< "[" << setw(10) << setfill('0') << log_evt
.GetTS() << setfill(' ') << setw(0) << "] " << log_evt
.GetMsgString(fmt_db
) << "'" << endl
;
153 string src
= log_evt
.GetSrcString(fmt_db
);
154 string dst
= log_evt
.GetDstString(fmt_db
);
155 string msg
= log_evt
.GetMsgString(fmt_db
);
157 pcaprec_hdr
.ts_sec
= log_evt
.GetTS()/1000;
158 pcaprec_hdr
.ts_usec
= (log_evt
.GetTS()%1000) * 1000;
159 pcaprec_hdr
.incl_len
=
160 pcaprec_hdr
.orig_len
= sizeof(mtlklog_hdr_t
) +
161 sizeof(uint16
) + src
.length() + 1 +
162 sizeof(uint16
) + dst
.length() + 1 +
163 sizeof(uint16
) + msg
.length() + 1;
164 out_s
.write((const char *)&pcaprec_hdr
, sizeof(pcaprec_hdr_t
));
165 mtlklog_hdr
.oid
= log_evt
.GetOID();
166 mtlklog_hdr
.gid
= log_evt
.GetGID();
167 mtlklog_hdr
.fid
= HOST_TO_NET16(log_evt
.GetFID());
168 mtlklog_hdr
.lid
= HOST_TO_NET16(log_evt
.GetLID());
169 mtlklog_hdr
.wlanif
= log_evt
.GetWLANIF();
170 out_s
.write((const char *)&mtlklog_hdr
, sizeof(mtlklog_hdr
));
171 WriteString(src
, out_s
);
172 WriteString(dst
, out_s
);
173 WriteString(msg
, out_s
);
182 MAIN_KNOWN_ERROR
= -1,
183 MAIN_UNKNOWN_ERROR
= -2
187 int __cdecl
main(int argc
, char* argv
[])
189 int main(int argc
, char* argv
[])
193 CCmdLine
cmdLine(argc
, argv
);
194 string strStringFiles
= cmdLine
.getParamValue(paramStringFiles
);
196 if (cmdLine
.isCmdLineParam(paramShowHelp
)) {
197 LOCAL_DBG_TRACE("Entering help mode...");
201 vector
<string
> scd_files
;
202 CStrTokenizer
tok(strStringFiles
);
203 for(CStrTokenizer::iterator t
= tok
.begin(SCD_FILES_DELIM
); t
; ++t
) {
204 LOCAL_DBG_TRACE("\tTokenizer returned path \"" << t
.get() << "\"");
209 scd_files
.push_back(t
.get());
212 if (scd_files
.empty()) {
214 throw logic_error("At least one string-file (.scd) must be specified!");
217 istream
*in_s
= &cin
;
218 ostream
*out_s
= &cout
;
222 bool pcap_out
= cmdLine
.isCmdLineParam(paramPCapOut
);
224 fName
= cmdLine
.getParamValue(paramInFile
);
225 if (!fName
.empty()) {
226 stream_enable_exceptions(in_f
);
227 in_f
.open(fName
.c_str(), ios::in
| ios::binary
);
231 stream_enable_exceptions(cin
);
233 /* Without _setmode() Windows replaces end-of-lines and breaks the data */
234 if (_setmode(_fileno(stdin
), _O_BINARY
) == -1) {
235 throw logic_error("Can't set STDIN to BINARY mode!");
240 fName
= cmdLine
.getParamValue(paramOutFile
);
241 if (!fName
.empty()) {
242 stream_enable_exceptions(out_f
);
243 out_f
.open(fName
.c_str(), pcap_out
?(ios::out
| ios::binary
):ios::out
);
247 stream_enable_exceptions(cout
);
249 /* Without _setmode() Windows replaces end-of-lines and breaks the data */
250 if (_setmode(_fileno(stdout
), _O_BINARY
) == -1) {
251 throw logic_error("Can't set STDOUT to BINARY mode!");
256 ProcessLog(*in_s
, *out_s
, scd_files
, pcap_out
);
258 catch (const exception
& ex
) {
259 cerr
<< "Error occurred:" << endl
<< "\t"
260 << ex
.what() << endl
;
261 return MAIN_KNOWN_ERROR
;
264 cerr
<< "Unknown fatal error occurred." << endl
;
265 return MAIN_UNKNOWN_ERROR
;