-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathTcpSessionList.cpp
More file actions
267 lines (222 loc) · 6.51 KB
/
TcpSessionList.cpp
File metadata and controls
267 lines (222 loc) · 6.51 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
/*
* 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 "SipStackDefine.h"
#include "TcpSessionList.h"
#include "SipStack.h"
#include "Log.h"
#include <time.h>
#include "MemoryDebug.h"
CTcpSessionListInfo::CTcpSessionListInfo() : m_iPort(0)
#ifdef USE_TLS
, m_psttSsl(NULL)
#endif
, m_iConnectTime(0), m_iRecvTime(0), m_bUseTimeout(true)
{
}
/**
* @ingroup SipStack
* @brief 세션 정보를 초기화시킨다.
*/
void CTcpSessionListInfo::Clear()
{
m_strIp.clear();
m_iPort = 0;
m_clsSipBuf.Clear();
m_iConnectTime = 0;
m_iRecvTime = 0;
}
CTcpSessionList::CTcpSessionList( CSipStack * pclsSipStack, ESipTransport eTransport ) : m_psttPollFd(NULL), m_iPollFdMax(0), m_iPoolFdCount(0)
, m_pclsSipStack( pclsSipStack), m_eTransport( eTransport )
{
}
CTcpSessionList::~CTcpSessionList(void)
{
if( m_psttPollFd ) free( m_psttPollFd );
}
/**
* @ingroup SipStack
* @brief TCP 세션 정보를 초기화시킨다.
* @param iPollFdMax TCP 세션 개수
* @returns 성공하면 true 를 리턴하고 실패하면 false 를 리턴한다.
*/
bool CTcpSessionList::Init( int iPollFdMax )
{
m_iPollFdMax = iPollFdMax;
m_psttPollFd = ( struct pollfd * )malloc( sizeof(struct pollfd) * m_iPollFdMax );
if( m_psttPollFd == NULL )
{
return false;
}
for( int i = 0; i < m_iPollFdMax; ++i )
{
TcpSetPollIn( m_psttPollFd[i], INVALID_SOCKET );
}
for( int i = 0; i < m_iPollFdMax; ++i )
{
CTcpSessionListInfo clsEntry;
m_clsList.push_back( clsEntry );
}
return true;
}
/**
* @ingroup SipStack
* @brief TCP 소켓을 추가한다.
* @param hSocket TCP 소켓
* @returns 성공하면 true 를 리턴하고 실패하면 false 를 리턴한다.
*/
bool CTcpSessionList::Insert( Socket hSocket )
{
if( m_iPoolFdCount >= m_iPollFdMax ) return false;
TcpSetPollIn( m_psttPollFd[m_iPoolFdCount], hSocket );
++m_iPoolFdCount;
return true;
}
/**
* @ingroup SipStack
* @brief TCP 세션 정보를 추가한다.
* @param clsTcpComm TCP 세션 정보 저장 객체
* @param psttSsl SSL 세션 정보 저장 구조체 - SSL 세션인 경우만 저장한다.
* @returns 성공하면 true 를 리턴하고 실패하면 false 를 리턴한다.
*/
bool CTcpSessionList::Insert( CTcpComm & clsTcpComm, SSL * psttSsl )
{
time_t iTime;
time( &iTime );
if( m_iPoolFdCount >= m_iPollFdMax )
{
for( int i = 1; i < m_iPoolFdCount; ++i )
{
if( m_psttPollFd[i].fd == INVALID_SOCKET )
{
TcpSetPollIn( m_psttPollFd[i], clsTcpComm.m_hSocket );
m_clsList[i].m_strIp = clsTcpComm.m_szIp;
m_clsList[i].m_iPort = clsTcpComm.m_iPort;
m_clsList[i].m_iConnectTime = iTime;
m_clsList[i].m_iRecvTime = iTime;
#ifdef USE_TLS
m_clsList[i].m_psttSsl = psttSsl;
#endif
return true;
}
}
return false;
}
TcpSetPollIn( m_psttPollFd[m_iPoolFdCount], clsTcpComm.m_hSocket );
m_clsList[m_iPoolFdCount].m_strIp = clsTcpComm.m_szIp;
m_clsList[m_iPoolFdCount].m_iPort = clsTcpComm.m_iPort;
m_clsList[m_iPoolFdCount].m_iConnectTime = iTime;
m_clsList[m_iPoolFdCount].m_iRecvTime = iTime;
#ifdef USE_TLS
m_clsList[m_iPoolFdCount].m_psttSsl = psttSsl;
#endif
++m_iPoolFdCount;
return true;
}
/**
* @ingroup SipStack
* @brief TCP 세션 정보를 삭제한다.
* @param iIndex TCP 세션 인덱스
* @param pclsEntry TCP 쓰레드 정보 객체
* @returns 성공하면 true 를 리턴하고 실패하면 false 를 리턴한다.
*/
bool CTcpSessionList::Delete( int iIndex, CThreadListEntry * pclsEntry )
{
if( iIndex >= m_iPoolFdCount || iIndex < 0 ) return false;
m_pclsSipStack->TcpSessionEnd( m_clsList[iIndex].m_strIp.c_str(), m_clsList[iIndex].m_iPort, m_eTransport );
#ifdef USE_TLS
if( m_clsList[iIndex].m_psttSsl )
{
SSLClose( m_clsList[iIndex].m_psttSsl );
m_clsList[iIndex].m_psttSsl = NULL;
}
#endif
closesocket( m_psttPollFd[iIndex].fd );
m_psttPollFd[iIndex].fd = INVALID_SOCKET;
m_psttPollFd[iIndex].events = 0;
m_psttPollFd[iIndex].revents = 0;
m_clsList[iIndex].Clear();
if( iIndex == (m_iPoolFdCount - 1) )
{
for( int i = iIndex; i >= 0; --i )
{
if( m_psttPollFd[i].fd != INVALID_SOCKET ) break;
m_iPoolFdCount = i;
}
}
pclsEntry->DecreaseSocketCount();
return true;
}
/**
* @ingroup SipStack
* @brief 모든 TCP 세션 정보를 삭제한다.
* @param pclsEntry TCP 쓰레드 정보 객체
*/
void CTcpSessionList::DeleteAll( CThreadListEntry * pclsEntry )
{
for( int i = 0; i < m_iPoolFdCount; ++i )
{
#ifdef USE_TLS
if( m_clsList[i].m_psttSsl )
{
SSLClose( m_clsList[i].m_psttSsl );
m_clsList[i].m_psttSsl = NULL;
}
#endif
if( m_psttPollFd[i].fd != INVALID_SOCKET )
{
closesocket( m_psttPollFd[i].fd );
m_psttPollFd[i].fd = INVALID_SOCKET;
m_psttPollFd[i].events = 0;
m_psttPollFd[i].revents = 0;
m_clsList[i].Clear();
pclsEntry->DecreaseSocketCount();
}
}
}
/**
* @ingroup SipStack
* @brief TCP 수신 timeout 이 발생한 TCP 세션을 종료시킨다.
* @param iTimeout TCP 수신 timeout 시간 (초단위)
* @param pclsEntry TCP 쓰레드 정보
*/
void CTcpSessionList::DeleteTimeout( int iTimeout, CThreadListEntry * pclsEntry )
{
time_t iTime;
time( &iTime );
iTime -= iTimeout;
for( int i = 1; i < m_iPoolFdCount; ++i )
{
if( m_clsList[i].m_bUseTimeout == false ) continue;
if( m_clsList[i].m_iRecvTime == 0 ) continue;
if( m_clsList[i].m_iRecvTime < iTime )
{
CLog::Print( LOG_DEBUG, "%s (%s,%d) timeout", __FUNCTION__, m_clsList[i].m_strIp.c_str(), m_clsList[i].m_iPort );
if( m_eTransport == E_SIP_TCP )
{
m_pclsSipStack->m_clsTcpSocketMap.Delete( m_clsList[i].m_strIp.c_str(), m_clsList[i].m_iPort );
}
#ifdef USE_TLS
else if( m_eTransport == E_SIP_TLS )
{
m_pclsSipStack->m_clsTlsSocketMap.Delete( m_clsList[i].m_strIp.c_str(), m_clsList[i].m_iPort );
}
#endif
Delete( i, pclsEntry );
}
}
}