-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathSipServer.cpp
More file actions
184 lines (159 loc) · 5.17 KB
/
SipServer.cpp
File metadata and controls
184 lines (159 loc) · 5.17 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
/*
* 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 "SipServer.h"
#include "SipUtility.h"
#include "UserMap.h"
CSipStack gclsSipStack;
CSipServer gclsSipServer;
CSipServer::CSipServer()
{
}
CSipServer::~CSipServer()
{
}
/**
* @ingroup SimpleSipServer
* @brief SIP 서버를 시작한다.
* @param clsSetup SIP stack 설정 항목을 저장한 객체
* @returns 성공하면 true 를 리턴하고 실패하면 false 를 리턴한다.
*/
bool CSipServer::Start( CSipStackSetup & clsSetup )
{
gclsSipStack.AddCallBack( this );
if( gclsSipStack.Start( clsSetup ) == false ) return false;
return true;
}
/**
* @ingroup SimpleSipServer
* @brief SIP stack 을 종료한다.
*/
void CSipServer::Stop( )
{
gclsSipStack.Stop();
gclsSipStack.Final();
}
/**
* @ingroup SimpleSipServer
* @brief SIP 요청 메시지 수신 이벤트 핸들러
* @param iThreadId 쓰레드 아이디
* @param pclsMessage SIP 요청 메시지
* @returns SIP 요청 메시지를 처리하면 true 를 리턴하고 그렇지 않으면 false 를 리턴한다.
*/
bool CSipServer::RecvRequest( int iThreadId, CSipMessage * pclsMessage )
{
if( pclsMessage->IsMethod( SIP_METHOD_REGISTER ) )
{
// 모든 클라이언트의 로그인을 허용한다.
CSipFrom clsContact;
gclsUserMap.Insert( pclsMessage, &clsContact );
CSipMessage * pclsResponse = pclsMessage->CreateResponseWithToTag( SIP_OK );
if( pclsResponse )
{
pclsResponse->m_clsContactList.push_back( clsContact );
gclsSipStack.SendSipMessage( pclsResponse );
return true;
}
}
else
{
CUserInfo clsUserInfo;
std::string strToId = pclsMessage->m_clsTo.m_clsUri.m_strUser;
if( strToId.length() == 0 )
{
strToId = pclsMessage->m_clsTo.m_strDisplayName;
}
if( gclsUserMap.Select( strToId.c_str(), clsUserInfo ) == false )
{
// TO 사용자가 존재하지 않으면 404 NOT FOUND 로 응답한다.
CSipMessage * pclsResponse = pclsMessage->CreateResponseWithToTag( SIP_NOT_FOUND );
if( pclsResponse )
{
gclsSipStack.SendSipMessage( pclsResponse );
return true;
}
}
else
{
// TO 사용자로 SIP 요청 메시지를 전달한다.
SIP_VIA_LIST::iterator itVia = pclsMessage->m_clsViaList.begin();
if( itVia != pclsMessage->m_clsViaList.end() )
{
const char * pszBranch = itVia->SelectParamValue( SIP_BRANCH );
std::string strBranch;
if( pszBranch )
{
strBranch = pszBranch;
strBranch.append( "_copy" );
}
CSipMessage * pclsRequest = new CSipMessage();
if( pclsRequest )
{
*pclsRequest = *pclsMessage;
pclsRequest->AddVia( gclsSipStack.m_clsSetup.m_strLocalIp.c_str(), gclsSipStack.m_clsSetup.m_iLocalUdpPort, strBranch.c_str() );
pclsRequest->AddRoute( clsUserInfo.m_strIp.c_str(), clsUserInfo.m_iPort );
SIP_FROM_LIST::iterator itContact = pclsRequest->m_clsContactList.begin();
if( itContact != pclsRequest->m_clsContactList.end() )
{
itContact->m_clsUri.m_strHost = gclsSipStack.m_clsSetup.m_strLocalIp;
itContact->m_clsUri.m_iPort = gclsSipStack.m_clsSetup.m_iLocalUdpPort;
}
gclsSipStack.SendSipMessage( pclsRequest );
return true;
}
}
}
}
return false;
}
/**
* @ingroup SimpleSipServer
* @brief SIP 응답 메시지 수신 이벤트 핸들러
* @param iThreadId 쓰레드 아이디
* @param pclsMessage SIP 응답 메시지
* @returns SIP 응답 메시지를 처리하면 true 를 리턴하고 그렇지 않으면 false 를 리턴한다.
*/
bool CSipServer::RecvResponse( int iThreadId, CSipMessage * pclsMessage )
{
CSipMessage * pclsResponse = new CSipMessage();
if( pclsResponse )
{
// SIP 요청 메시지를 전송한 호스트로 전달한다.
*pclsResponse = *pclsMessage;
pclsResponse->m_clsViaList.pop_front();
SIP_FROM_LIST::iterator itContact = pclsResponse->m_clsContactList.begin();
if( itContact != pclsResponse->m_clsContactList.end() )
{
itContact->m_clsUri.m_strHost = gclsSipStack.m_clsSetup.m_strLocalIp;
itContact->m_clsUri.m_iPort = gclsSipStack.m_clsSetup.m_iLocalUdpPort;
}
gclsSipStack.SendSipMessage( pclsResponse );
return true;
}
return false;
}
/**
* @ingroup SimpleSipServer
* @brief SIP 메시지 전송 timeout callback method
* @param iThreadId SIP stack 의 UDP 쓰레드 아이디
* @param pclsMessage 수신된 SIP 응답 메시지
* @returns SIP 응답 메시지를 처리한 경우 true 를 리턴하고 그렇지 않으면 false 를 리턴한다.
*/
bool CSipServer::SendTimeout( int iThreadId, CSipMessage * pclsMessage )
{
return false;
}