-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathSipClientMain.cpp
More file actions
237 lines (191 loc) · 7 KB
/
SipClientMain.cpp
File metadata and controls
237 lines (191 loc) · 7 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
/*
* 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 "SipClient.h"
#include "SipClientSetup.h"
#include "Log.h"
#include "SipUtility.h"
#include "RtpThread.h"
#include "MemoryDebug.h"
extern std::string gstrInviteId;
/**
* @ingroup SipClient
* @brief SIP 클라이언트
* @param argc
* @param argv
* @returns 0 을 리턴한다.
*/
int main( int argc, char * argv[] )
{
if( argc != 2 )
{
printf( "[Usage] %s {xml file}\n", argv[0] );
return 0;
}
char * pszFileName = argv[1];
if( gclsSetupFile.Read( pszFileName ) == false )
{
printf( "Setup file error\n" );
return 0;
}
#ifdef WIN32
#ifdef _DEBUG
_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF | _CRTDBG_CHECK_ALWAYS_DF );
CLog::SetDirectory( "c:\\temp\\sipclient" );
CLog::SetLevel( LOG_NETWORK | LOG_DEBUG | LOG_INFO );
#endif
#endif
CSipUserAgent clsUserAgent;
CSipServerInfo clsServerInfo;
CSipStackSetup clsSetup;
CSipClient clsSipClient;
clsServerInfo.m_strIp = gclsSetupFile.m_strSipServerIp;
clsServerInfo.m_strDomain = gclsSetupFile.m_strSipDomain;
clsServerInfo.m_strUserId = gclsSetupFile.m_strSipUserId;
clsServerInfo.m_strPassWord = gclsSetupFile.m_strSipPassWord;
clsServerInfo.m_eTransport = gclsSetupFile.m_eSipTransport;
clsServerInfo.m_iPort = gclsSetupFile.m_iSipServerPort;
clsServerInfo.m_iLoginTimeout = 600;
//clsServerInfo.m_iNatTimeout = 10;
// 삼성 070 서비스 로그인시 User-Agent 헤더에 특수한 문자열이 포함되지 않으면 480 응답이 수신된다.
// 아래와 같이 Acrobits 으로 User-Agent 헤더가 시작하면 정상적으로 401 응답을 수신한다.
//clsSetup.m_strUserAgent = "Acrobits";
// Expires 헤더에 300 을 입력하고 싶으면 아래와 같이 설정하면 된다.
// clsServerInfo.m_iLoginTimeout = 300;
clsSetup.m_iLocalUdpPort = gclsSetupFile.m_iUdpPort;
clsSetup.m_strLocalIp = gclsSetupFile.m_strLocalIp;
if( gclsSetupFile.m_eSipTransport == E_SIP_TCP )
{
clsSetup.m_iLocalTcpPort = gclsSetupFile.m_iUdpPort;
clsSetup.m_iTcpCallBackThreadCount = 1;
}
else if( gclsSetupFile.m_eSipTransport == E_SIP_TLS )
{
clsSetup.m_iLocalTlsPort = gclsSetupFile.m_iUdpPort;
clsSetup.m_strCertFile = gclsSetupFile.m_strPemFile;
}
// Via 헤더 및 Contact 헤더에 로컬 수신 포트 번호를 설정하고 싶으면 아래와 같이 설정하면 된다.
// clsSetup.m_bUseContactListenPort = true;
// UDP 수신 쓰레드의 기본 개수는 1개이다. 이를 수정하려면 CSipStackSetup.m_iUdpThreadCount 를 수정하면 된다.
clsUserAgent.InsertRegisterInfo( clsServerInfo );
if( clsUserAgent.Start( clsSetup, &clsSipClient ) == false )
{
printf( "sip stack start error\n" );
return 0;
}
if( gclsRtpThread.Create() == false )
{
printf( "rtp thread create error\n" );
return 0;
}
char szCommand[1024];
int iLen;
memset( szCommand, 0, sizeof(szCommand) );
while( fgets( szCommand, sizeof(szCommand), stdin ) )
{
if( szCommand[0] == 'Q' || szCommand[0] == 'q' ) break;
iLen = (int)strlen(szCommand);
if( iLen >= 2 && szCommand[iLen-2] == '\r' )
{
szCommand[iLen-2] = '\0';
}
else if( iLen >= 1 && szCommand[iLen-1] == '\n' )
{
szCommand[iLen-1] = '\0';
}
if( szCommand[0] == 'C' || szCommand[0] == 'c' )
{
CSipCallRtp clsRtp;
CSipCallRoute clsRoute;
clsRtp.m_strIp = clsSetup.m_strLocalIp;
clsRtp.m_iPort = gclsRtpThread.m_iPort;
clsRtp.m_iCodec = 0;
clsRoute.m_strDestIp = gclsSetupFile.m_strSipServerIp;
clsRoute.m_iDestPort = gclsSetupFile.m_iSipServerPort;
clsRoute.m_eTransport = gclsSetupFile.m_eSipTransport;
clsUserAgent.StartCall( gclsSetupFile.m_strSipUserId.c_str(), szCommand + 2, &clsRtp, &clsRoute, gstrInviteId );
}
else if( szCommand[0] == 'e' || szCommand[0] == 's' )
{
clsUserAgent.StopCall( gstrInviteId.c_str() );
gclsRtpThread.Stop( );
gstrInviteId.clear();
}
else if( szCommand[0] == 'a' )
{
CSipCallRtp clsRtp;
clsRtp.m_strIp = clsSetup.m_strLocalIp;
clsRtp.m_iPort = gclsRtpThread.m_iPort;
clsRtp.m_iCodec = 0;
clsUserAgent.AcceptCall( gstrInviteId.c_str(), &clsRtp );
gclsRtpThread.Start( clsSipClient.m_clsDestRtp.m_strIp.c_str(), clsSipClient.m_clsDestRtp.m_iPort );
}
else if( szCommand[0] == 'm' )
{
CSipCallRoute clsRoute;
clsRoute.m_strDestIp = gclsSetupFile.m_strSipServerIp;
clsRoute.m_iDestPort = gclsSetupFile.m_iSipServerPort;
clsRoute.m_eTransport = gclsSetupFile.m_eSipTransport;
clsUserAgent.SendSms( gclsSetupFile.m_strSipUserId.c_str(), szCommand + 2, "hello", &clsRoute );
}
else if( szCommand[0] == 'i' )
{
CMonitorString strBuf;
clsUserAgent.m_clsSipStack.GetString( strBuf );
printf( "%s", strBuf.GetString() );
}
else if( szCommand[0] == 't' )
{
// OPTION 메시지 전송 예제
CSipMessage * pclsMessage = new CSipMessage();
if( pclsMessage )
{
char szTag[SIP_TAG_MAX_SIZE], szCallIdName[SIP_CALL_ID_NAME_MAX_SIZE];
SipMakeTag( szTag, sizeof(szTag) );
SipMakeCallIdName( szCallIdName, sizeof(szCallIdName) );
// Call-ID 를 저장한다.
pclsMessage->m_clsCallId.m_strName = szCallIdName;
pclsMessage->m_clsCallId.m_strHost = clsUserAgent.m_clsSipStack.m_clsSetup.m_strLocalIp;
pclsMessage->m_eTransport = E_SIP_UDP;
// SIP method 를 저장한다.
pclsMessage->m_strSipMethod = SIP_METHOD_OPTIONS;
// Request Uri 를 저장한다.
pclsMessage->m_clsReqUri.Set( SIP_PROTOCOL, "1000", "192.168.0.1", 5060 );
// CSeq 를 저장한다.
pclsMessage->m_clsCSeq.Set( 1, SIP_METHOD_OPTIONS );
// From 헤더를 저장한다.
pclsMessage->m_clsFrom.m_clsUri.Set( SIP_PROTOCOL, "2000", "192.168.0.200", 5060 );
pclsMessage->m_clsFrom.InsertParam( SIP_TAG, szTag );
// To 헤더를 저장한다.
pclsMessage->m_clsTo.m_clsUri.Set( SIP_PROTOCOL, "1000", "192.168.0.1", 5060 );
// SIP 메시지를 전송할 대상 IP 주소와 포트 번호 및 프로토콜을 저장한다.
pclsMessage->AddRoute( "192.168.0.1", 5060, E_SIP_UDP );
clsUserAgent.m_clsSipStack.SendSipMessage( pclsMessage );
}
}
memset( szCommand, 0, sizeof(szCommand) );
}
if( gstrInviteId.empty() == false )
{
clsUserAgent.StopCall( gstrInviteId.c_str() );
}
gclsRtpThread.Stop( );
sleep(2);
clsUserAgent.Stop();
clsUserAgent.Final();
return 0;
}