-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathSipUtility.cpp
More file actions
305 lines (258 loc) · 6.85 KB
/
SipUtility.cpp
File metadata and controls
305 lines (258 loc) · 6.85 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
/*
* 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 "SipParserDefine.h"
#include "SipUtility.h"
#include "SipMutex.h"
#include "SipMd5.h"
#include "TimeUtility.h"
#include <string>
#include <time.h>
#include <stdlib.h>
#include "Random.h"
#include "MemoryDebug.h"
static CSipMutex gclsMutex;
static int giTag;
static int giBranch;
static int giCallId;
static std::string gstrSystemId;
static char garrChar[64];
/**
* @ingroup SipParser
* @brief 다른 시스템과 구분되는 현재 시스템의 유일한 값을 설정한다.
* @param pszId 현재 시스템의 유일한 값
*/
void SipSetSystemId( const char * pszId )
{
gstrSystemId = pszId;
}
/**
* @ingroup SipParser
* @brief byte 로 구성된 변수를 알파벳으로 구성된 문자열로 변환하기 위한 매핑 테이블을 생성한다.
*/
static void InitRandomString()
{
struct timeval sttTime;
gettimeofday( &sttTime, NULL );
int i, iPos = 0;
for( i = 0; i < 26; ++i )
{
garrChar[iPos++] = 'a' + i;
garrChar[iPos++] = 'A' + i;
}
for( i = 0; i < 10; ++i )
{
garrChar[iPos++] = '0' + i;
}
garrChar[iPos++] = '_';
garrChar[iPos++] = '-';
}
/**
* @ingroup SipParser
* @brief SIP From/To tag 에 사용할 문자열을 작성한다.
* @param pszTag SIP From/To tag 에 사용할 문자열을 저장할 변수
* @param iTagSize pszTag 변수의 크기
*/
void SipMakeTag( char * pszTag, int iTagSize )
{
int iTag;
gclsMutex.acquire();
if( giTag <= 0 || giTag > 2000000000 )
{
giTag = RandomGet();
}
else
{
++giTag;
}
iTag = giTag;
gclsMutex.release();
snprintf( pszTag, iTagSize, "%d", iTag );
}
/**
* @ingroup SipParser
* @brief SIP Via branch 에 사용할 문자열을 작성한다.
* @param pszBranch SIP Via branch 에 사용할 문자열을 저장할 변수
* @param iBranchSize pszBranch 변수의 크기
*/
void SipMakeBranch( char * pszBranch, int iBranchSize )
{
int iBranch;
memset( pszBranch, 0, iBranchSize );
gclsMutex.acquire();
if( giBranch <= 0 || giBranch > 2000000000 )
{
giBranch = RandomGet();
}
else
{
++giBranch;
}
iBranch = giBranch;
gclsMutex.release();
int iLen = 0;
if( gstrSystemId.empty() )
{
iLen = snprintf( pszBranch, iBranchSize, "%sWCSS%x", VIA_PREFIX, iBranch );
}
else
{
iLen = snprintf( pszBranch, iBranchSize, "%sWCSS%s%x", VIA_PREFIX, gstrSystemId.c_str(), iBranch );
}
if( iBranchSize > ( iLen + 6 ) )
{
struct timeval sttTime;
char szValue[4];
gettimeofday( &sttTime, NULL );
memcpy( szValue, &sttTime.tv_sec, 2 );
memcpy( szValue + 2, &sttTime.tv_usec, 2 );
SipMakePrintString( ( unsigned char *)szValue, 4, pszBranch + iLen, iBranchSize - iLen );
}
}
/**
* @ingroup SipParser
* @brief SIP Call-ID 에 사용할 name 문자열을 작성한다.
* @param pszCallId SIP Call-ID 에 사용할 name 문자열을 저장할 변수
* @param iCallIdSize pszCallId 변수의 크기
*/
void SipMakeCallIdName( char * pszCallId, int iCallIdSize )
{
int iCallId;
memset( pszCallId, 0, iCallIdSize );
gclsMutex.acquire();
if( giCallId <= 0 || giCallId > 2000000000 )
{
giCallId = RandomGet();
}
else
{
++giCallId;
}
iCallId = giCallId;
gclsMutex.release();
int iLen = 0;
if( gstrSystemId.empty() )
{
iLen = snprintf( pszCallId, iCallIdSize, "WCSS%x", iCallId );
}
else
{
iLen = snprintf( pszCallId, iCallIdSize, "WCSS%s%x", gstrSystemId.c_str(), iCallId );
}
if( iCallIdSize > ( iLen + 6 ) )
{
struct timeval sttTime;
char szValue[4];
gettimeofday( &sttTime, NULL );
memcpy( szValue, &sttTime.tv_sec, 2 );
memcpy( szValue + 2, &sttTime.tv_usec, 2 );
SipMakePrintString( ( unsigned char *)szValue, 4, pszCallId + iLen, iCallIdSize - iLen );
}
}
/**
* @ingroup SipParser
* @brief 입력 문자열을 출력할 수 있는 64개의 문자들로 변환하여서 출력 문자열에 저장한다.
* @param pszInput 입력 문자열
* @param iInputSize 입력 문자열 크기
* @param pszOutput 출력 문자열
* @param iOutputSize 출력 문자열 크기
*/
bool SipMakePrintString( const unsigned char * pszInput, int iInputSize, char * pszOutput, int iOutputSize )
{
if( garrChar[0] == '\0' )
{
InitRandomString();
}
char cType = 0;
unsigned char cValue = 0, cNextValue = 0;
int iOutputLen = 0;
--iOutputSize;
for( int i = 0; i < iInputSize; ++i )
{
if( iOutputLen >= iOutputSize ) return false;
switch( cType )
{
case 0:
cValue = pszInput[i] >> 2;
cNextValue = pszInput[i] & 0x03;
pszOutput[iOutputLen++] = garrChar[ cValue ];
++cType;
break;
case 1:
cValue = cNextValue << 4 | pszInput[i] >> 4;
cNextValue = pszInput[i] & 0x0F;
pszOutput[iOutputLen++] = garrChar[ cValue ];
++cType;
break;
case 2:
cValue = cNextValue << 2 | pszInput[i] >> 6;
cNextValue = pszInput[i] & 0x3F;
pszOutput[iOutputLen++] = garrChar[ cValue ];
if( iOutputLen >= iOutputSize ) return false;
pszOutput[iOutputLen++] = garrChar[ cNextValue ];
cType = 0;
break;
}
}
pszOutput[iOutputLen] = '\0';
return true;
}
/**
* @ingroup SipParser
* @brief 평문을 MD5 문자열로 변환한다.
* @param pszInput 평문
* @param szResult MD5 문자열 저장 변수
*/
void SipMd5String21( char * pszInput, char szResult[22] )
{
unsigned char szDigest[16];
SipMd5Byte( pszInput, szDigest );
SipMakePrintString( szDigest, 16, szResult, 22 );
}
/**
* @ingroup SipParser
* @brief [] 로 둘러쌓인 IPv6 주소에서 [] 를 제거한다.
* @param strHost IP 주소
*/
void SipIpv6Parse( std::string & strHost )
{
int iLen = (int)strHost.length();
if( iLen > 0 )
{
const char * pszHost = strHost.c_str();
if( pszHost[0] == '[' && pszHost[iLen-1] == ']' )
{
strHost.erase( iLen-1, 1 );
strHost.erase( 0, 1 );
}
}
}
int SipIpv6Print( std::string & strHost, char * pszText, int iTextSize, int iLen )
{
int n, iHostLen = (int)strHost.length();
const char * pszHost = strHost.c_str();
if( iHostLen > 2 && pszHost[0] != '[' && strstr( pszHost, ":" ) )
{
// IPv6
n = snprintf( pszText + iLen, iTextSize - iLen, "[%s]", pszHost );
}
else
{
n = snprintf( pszText + iLen, iTextSize - iLen, "%s", pszHost );
}
return n;
}