-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathIpFragmentMap.cpp
More file actions
361 lines (308 loc) · 8.42 KB
/
IpFragmentMap.cpp
File metadata and controls
361 lines (308 loc) · 8.42 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
* Copyright (C) 2012 Yee Young Han <websearch@naver.com> (http://blog.naver.com/websearch)
*
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "IpFragmentMap.h"
#include "TimeUtility.h"
#include "Log.h"
#include "MemoryDebug.h"
CIpFragmentMap gclsIpFragmentMap;
CIpPacket::CIpPacket() : m_pszPacket(NULL), m_iPacketLen(0)
{
}
CIpPacket::~CIpPacket()
{
if( m_pszPacket )
{
free( m_pszPacket );
m_pszPacket = NULL;
}
}
CIpFragmentData::CIpFragmentData() : m_bMoreFragment(false), m_sFragmentOffset(0), m_pszIpBody(NULL), m_iIpBodyLen(0)
{
}
CIpFragmentData::~CIpFragmentData()
{
if( m_pszIpBody )
{
free( m_pszIpBody );
m_pszIpBody = NULL;
}
}
CIpFragmentInfo::CIpFragmentInfo() : m_iTime(NULL), m_bRecvFragmentEnd(false)
{
}
CIpFragmentInfo::~CIpFragmentInfo()
{
IP_FRAGMENT_DATA_LIST::iterator itList;
for( itList = m_clsList.begin(); itList != m_clsList.end(); ++itList )
{
delete (*itList);
}
m_clsList.clear();
}
/**
* @ingroup SipCallDump
* @brief fragment 된 패킷을 저장한다.
* @param psttIp4Header IPv4 헤더
* @param pszIpBody IP body
* @param iIpBodyLen IP body len
* @param bEnd [out] 모든 fragment 패킷이 수신되었으면 true 가 저장된다.
* @returns 성공하면 true 를 리턴하고 그렇지 않으면 false 를 리턴한다.
*/
bool CIpFragmentInfo::Insert( Ip4Header * psttIp4Header, char * pszIpBody, int iIpBodyLen, bool & bEnd )
{
uint16_t sFlags = ntohs(psttIp4Header->flags_fo);
CIpFragmentData * pclsData = new CIpFragmentData();
if( pclsData == NULL )
{
CLog::Print( LOG_ERROR, "%s new error", __FUNCTION__ );
return false;
}
pclsData->m_pszIpBody = (u_char *)malloc( iIpBodyLen );
if( pclsData->m_pszIpBody == NULL )
{
CLog::Print( LOG_ERROR, "%s malloc error", __FUNCTION__ );
delete pclsData;
return false;
}
memcpy( pclsData->m_pszIpBody, pszIpBody, iIpBodyLen );
pclsData->m_iIpBodyLen = iIpBodyLen;
if( sFlags & 0x2000 )
{
pclsData->m_bMoreFragment = true;
}
else
{
m_bRecvFragmentEnd = true;
}
pclsData->m_sFragmentOffset = ( sFlags & 0x1FFF ) * 8;
if( pclsData->m_sFragmentOffset == 0 )
{
memcpy( &m_sttIpHeader, psttIp4Header, 20 );
}
if( m_clsList.empty() )
{
m_clsList.push_back( pclsData );
}
else
{
IP_FRAGMENT_DATA_LIST::iterator itList;
bool bFound = false;
for( itList = m_clsList.begin(); itList != m_clsList.end(); ++itList )
{
if( pclsData->m_sFragmentOffset == (*itList)->m_sFragmentOffset )
{
// 동일한 Fragment Offset 이면 저장하지 않는다.
delete pclsData;
bFound = true;
break;
}
else if( pclsData->m_sFragmentOffset < (*itList)->m_sFragmentOffset )
{
m_clsList.insert( itList, pclsData );
bFound = true;
break;
}
}
if( bFound == false )
{
m_clsList.push_back( pclsData );
}
}
time( &m_iTime );
if( m_bRecvFragmentEnd )
{
IP_FRAGMENT_DATA_LIST::iterator itList;
int iWantOffset = 0;
bool bError = false;
for( itList = m_clsList.begin(); itList != m_clsList.end(); ++itList )
{
if( iWantOffset != (*itList)->m_sFragmentOffset )
{
bError = true;
break;
}
iWantOffset += (*itList)->m_iIpBodyLen;
}
if( bError == false )
{
bEnd = true;
}
}
return true;
}
/**
* @ingroup SipCallDump
* @brief fragment 된 패킷들을 합쳐서 하나의 IP 패킷으로 생성한다.
* @param pclsPacket [out] 하나의 IP 패킷 저장 객체
* @returns 성공하면 true 를 리턴하고 그렇지 않으면 false 를 리턴한다.
*/
bool CIpFragmentInfo::GetPacket( CIpPacket * pclsPacket )
{
IP_FRAGMENT_DATA_LIST::iterator itList;
int iIpBodyLen = 0;
for( itList = m_clsList.begin(); itList != m_clsList.end(); ++itList )
{
iIpBodyLen += (*itList)->m_iIpBodyLen;
}
pclsPacket->m_pszPacket = (u_char *)malloc( sizeof(m_sttIpHeader) + iIpBodyLen );
if( pclsPacket->m_pszPacket == NULL )
{
CLog::Print( LOG_ERROR, "%s malloc error", __FUNCTION__ );
return false;
}
memcpy( pclsPacket->m_pszPacket, &m_sttIpHeader, 20 );
pclsPacket->m_iPacketLen = 20;
for( itList = m_clsList.begin(); itList != m_clsList.end(); ++itList )
{
memcpy( pclsPacket->m_pszPacket + pclsPacket->m_iPacketLen, (*itList)->m_pszIpBody, (*itList)->m_iIpBodyLen );
pclsPacket->m_iPacketLen += (*itList)->m_iIpBodyLen;
}
Ip4Header * psttIpHeader = (Ip4Header *)pclsPacket->m_pszPacket;
psttIpHeader->flags_fo = 0;
psttIpHeader->tlen = htons( pclsPacket->m_iPacketLen );
return true;
}
CIpFragmentMap::CIpFragmentMap()
{
}
CIpFragmentMap::~CIpFragmentMap()
{
}
/**
* @ingroup SipCallDump
* @brief 자료구조에 fragment 된 패킷을 저장한다.
* @param psttIp4Header IPv4 헤더
* @param pszIpBody IP body
* @param iIpBodyLen IP body len
* @param bEnd [out] 모든 fragment 패킷이 수신되었으면 true 가 저장된다.
* @returns 성공하면 true 를 리턴하고 그렇지 않으면 false 를 리턴한다.
*/
bool CIpFragmentMap::Insert( Ip4Header * psttIp4Header, char * pszIpBody, int iIpBodyLen, bool & bEnd )
{
if( psttIp4Header->flags_fo == 0 ) return false;
std::string strKey;
IP_FRAGMENT_MAP::iterator itMap;
bEnd = false;
GetKey( psttIp4Header, strKey );
m_clsMutex.acquire();
itMap = m_clsMap.find( strKey );
if( itMap == m_clsMap.end() )
{
CIpFragmentInfo * pclsInfo = new CIpFragmentInfo();
if( pclsInfo == NULL )
{
CLog::Print( LOG_ERROR, "%s new error", __FUNCTION__ );
}
else
{
pclsInfo->Insert( psttIp4Header, pszIpBody, iIpBodyLen, bEnd );
m_clsMap.insert( IP_FRAGMENT_MAP::value_type( strKey, pclsInfo ) );
}
}
else
{
itMap->second->Insert( psttIp4Header, pszIpBody, iIpBodyLen, bEnd );
}
m_clsMutex.release();
return true;
}
/**
* @ingroup SipCallDump
* @brief 자료구조에서 fragment 패킷 관련 정보를 삭제한다.
* @param psttIp4Header IPv4 헤더
* @param pclsPacket [out] 삭제한 fragment 패킷들을 합쳐서 하나의 IP 패킷으로 저장된 객체
* @returns 성공하면 true 를 리턴하고 그렇지 않으면 false 를 리턴한다.
*/
bool CIpFragmentMap::Delete( Ip4Header * psttIp4Header, CIpPacket * pclsPacket )
{
bool bRes = false;
std::string strKey;
IP_FRAGMENT_MAP::iterator itMap;
GetKey( psttIp4Header, strKey );
m_clsMutex.acquire();
itMap = m_clsMap.find( strKey );
if( itMap != m_clsMap.end() )
{
if( pclsPacket )
{
bRes = itMap->second->GetPacket( pclsPacket );
}
else
{
bRes = true;
}
delete itMap->second;
m_clsMap.erase( itMap );
}
m_clsMutex.release();
return bRes;
}
/**
* @ingroup SipCallDump
* @brief 60초 이상 지난 fragment 패킷들을 삭제한다.
*/
void CIpFragmentMap::DeleteTimeout()
{
IP_FRAGMENT_MAP::iterator itMap, itNext;
time_t iTime;
time( &iTime );
iTime -= 60;
m_clsMutex.acquire();
for( itMap = m_clsMap.begin(); itMap != m_clsMap.end(); ++itMap )
{
LOOP_START:
if( iTime > itMap->second->m_iTime )
{
itNext = itMap;
++itNext;
delete itMap->second;
m_clsMap.erase( itMap );
if( itNext == m_clsMap.end() ) break;
itMap = itNext;
goto LOOP_START;
}
}
m_clsMutex.release();
}
/**
* @ingroup SipCallDump
* @brief 모든 fragment 패킷들을 삭제한다.
*/
void CIpFragmentMap::DeleteAll()
{
IP_FRAGMENT_MAP::iterator itMap, itNext;
m_clsMutex.acquire();
for( itMap = m_clsMap.begin(); itMap != m_clsMap.end(); ++itMap )
{
delete itMap->second;
}
m_clsMap.clear();
m_clsMutex.release();
}
/**
* @ingroup SipCallDump
* @brief 키를 생성한다.
* @param psttIp4Header IPv4 헤더
* @param strKey 키 저장 변수
*/
void CIpFragmentMap::GetKey( Ip4Header * psttIp4Header, std::string & strKey )
{
char szKey[32];
snprintf( szKey, sizeof(szKey), "%X_%X_%X", psttIp4Header->saddr, psttIp4Header->daddr, psttIp4Header->identification );
strKey = szKey;
}