-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtsharkdecoder.cpp
More file actions
156 lines (132 loc) · 4.2 KB
/
tsharkdecoder.cpp
File metadata and controls
156 lines (132 loc) · 4.2 KB
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
74
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
/* This file is part of 3GPP Decoder project.
* Copyright (C) 2015 Prashant Panigrahi prashant@3glteinfo.com
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "tsharkdecoder.h"
#include <string>
using namespace std;
TSharkDecoder::TSharkDecoder()
{
}
TSharkDecoder::~TSharkDecoder()
{
}
/* Decoding starts here
*/
void TSharkDecoder::startDecoder(QString strEncodedData, QString strProtocol)
{
QString strData;
strData = preformatData(strEncodedData);
formatFiletext2pcap(strData);
callText2pacp();
callTshark(strProtocol);
cleanOutput();
}
/* Make the incoming packets in HEX format.
* The packets are encoded in this format:
* ab - ab
* a - 0a
* abcd - ab cd
*/
QString TSharkDecoder::preformatData(QString strEncodedData){
QString strReturnData;
QStringList strListData;
strListData = strEncodedData.split(" ");
for(int i = 0; i < strListData.length(); i++ )
{
if(strListData[i].length() == 2)
{
strReturnData.append(" ");
strReturnData.append(strListData[i]);
}
else if(strListData[i].length() == 1)
{
strReturnData.append(" 0");
strReturnData.append(strListData[i]);
}
else if(strListData[i].length() > 2)
{
for(int j = 0; j < strListData[i].length(); j = j+2)
{
QStringRef strTempData = strListData[i].midRef(j, 2);
strReturnData.append(" ");
strReturnData.append(strTempData);
}
}
}
return strReturnData;
}
/*Create a Textfile which text2pcap can understand.
* 0000 are added to the beginning of the HEX data
* to make it text2pacp type
*/
void TSharkDecoder::formatFiletext2pcap(QString strData)
{
QFile textFile("textdata.txt");
if (textFile.open(QIODevice::ReadWrite)) {
QTextStream stream(&textFile);
stream << "0000";
stream <<strData;
qDebug() << strData;
}
}
/* Make a PCAP file from the textdata.
* Function calls text2pack.exe from wireshark folder.
*/
void TSharkDecoder::callText2pacp()
{
QString command= "text2pcap -q -l 147 textdata.txt decode_temp.pcap";
system(qPrintable(command));
qDebug() << command;
}
/* Call Tshark to decode the PCAP file.
* RRC messages are decoded directly
* NAS messages embeded are also parsed by Tshark.
*/
void TSharkDecoder::callTshark(QString strProtocol)
{
//tshark -o "uat:user_dlts:\"User 0 (DLT=147)\",\"LTE-RRC.BCCH.BCH\",\"0\",\"\",\"0\",\"\"" -r decode_temp.pcap -V > decode_output_temp.txt
//tshark -o \"uat:user_dlts:\\\"User 0 (DLT=147)\\\",\\\"
QString command;
//command = command.append(strTsharkPath);
command = command.append("tshark -o \"uat:user_dlts:\\\"User 0 (DLT=147)\\\",\\\"");
command = command.append(strProtocol);
command = command.append("\\\",\\\"0\\\",\\\"\\\",\\\"0\\\",\\\"\\\"\" -r decode_temp.pcap\ -V > decode_output_temp.txt");
qDebug() << command;
system(qPrintable(command));
system("rm textdata.txt decode_temp.pcap");
}
/* After decode the first 15 lines are useless data for us.
* These lines can be removed for a clean presentation
*/
void TSharkDecoder::cleanOutput()
{
QFile f("decode_output_temp.txt");
if(f.open(QIODevice::ReadWrite | QIODevice::Text))
{
QString s;
QTextStream t(&f);
int i = 0;
while(!t.atEnd())
{
QString line = t.readLine();
if(i>=15)
s.append(line + "\n");
i++;
}
f.resize(0);
t << s;
f.close();
}
}