Repository navigation
Expand file tree
/
Copy pathtelnet_server.cpp
More file actions
217 lines (173 loc) · 5.22 KB
/
Copy pathtelnet_server.cpp
File metadata and controls
217 lines (173 loc) · 5.22 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
/*
This source file is part of KBEngine
For the latest info, see http://www.kbengine.org/
Copyright (c) 2008-2012 KBEngine.
KBEngine is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
KBEngine 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with KBEngine. If not, see <http://www.gnu.org/licenses/>.
*/
#include "telnet_server.hpp"
#include "telnet_handler.hpp"
#include "network/bundle.hpp"
#include "network/endpoint.hpp"
#ifndef CODE_INLINE
#include "telnet_server.ipp"
#endif
namespace KBEngine {
//-------------------------------------------------------------------------------------
TelnetServer::TelnetServer(Mercury::EventDispatcher* pDispatcher, Mercury::NetworkInterface* networkInterface):
handlers_(),
listener_(),
pDispatcher_(pDispatcher),
passwd_(),
deflayer_(TelnetHandler::TELNET_STATE_ROOT),
pNetworkInterface_(networkInterface),
port_(0)
{
}
//-------------------------------------------------------------------------------------
TelnetServer::~TelnetServer(void)
{
}
//-------------------------------------------------------------------------------------
bool TelnetServer::start(std::string passwd, std::string deflayer, u_int16_t port, u_int32_t ip)
{
listener_.socket(SOCK_STREAM);
listener_.setnonblocking(true);
passwd_ = passwd;
if(deflayer == "python")
deflayer_ = TelnetHandler::TELNET_STATE_PYTHON;
int tryn = 0;
while(true)
{
port_ = port;
if (listener_.bind(htons(port), htonl(ip)) == -1)
{
port++;
tryn++;
if(tryn > 65535)
{
tryn = 0;
break;
}
continue;
}
else
{
tryn = 1;
break;
}
};
if(tryn == 0)
{
if (listener_.bind(htons(0), ip) == -1)
{
ERROR_MSG(boost::format("TelnetServer::start:: bind port(%1%) is failed! ip=%2%\n") %
0 % ip);
return false;
}
}
if(listener_.listen(1) == -1)
{
ERROR_MSG(boost::format("TelnetServer::start:: listen is failed! addr=%1%\n") %
listener_.c_str());
return false;
}
if(!pDispatcher_->registerFileDescriptor(listener_, this))
{
ERROR_MSG(boost::format("TelnetServer::start:: registerFileDescriptor is failed! addr=%1%\n") %
listener_.c_str());
return false;
}
INFO_MSG(boost::format("TelnetServer server is running on port %1%\n") % port);
return true;
}
//-------------------------------------------------------------------------------------
void TelnetServer::onTelnetHandlerClosed(int fd, TelnetHandler* pTelnetHandler)
{
INFO_MSG(boost::format("TelnetServer::onTelnetHandlerClosed: del handler(%1%)!\n") %
pTelnetHandler->pEndPoint()->c_str());
KBE_ASSERT(fd == (*pTelnetHandler->pEndPoint()));
pDispatcher_->deregisterFileDescriptor(fd);
handlers_.erase(fd);
}
//-------------------------------------------------------------------------------------
bool TelnetServer::stop()
{
pDispatcher_->deregisterFileDescriptor(listener_);
listener_.close();
listener_.detach();
return true;
}
//-------------------------------------------------------------------------------------
void TelnetServer::closeHandler(int fd, TelnetHandler* pTelnetHandler)
{
TelnetHandlers::iterator iter = handlers_.find(fd);
if(iter == handlers_.end() || iter->second.get() != pTelnetHandler)
{
ERROR_MSG(boost::format("TelnetServer::closeHandler: not found fd(%1%)!\n") % fd);
return;
}
pDispatcher_->deregisterFileDescriptor(fd);
handlers_.erase(iter);
#ifdef unix
::close(fd);
#elif defined(PLAYSTATION3)
::socketclose(fd);
#else
::closesocket(fd);
#endif
}
//-------------------------------------------------------------------------------------
int TelnetServer::handleInputNotification(int fd)
{
KBE_ASSERT(listener_ == fd);
int tickcount = 0;
while(tickcount ++ < 1024)
{
Mercury::EndPoint* pNewEndPoint = listener_.accept();
if(pNewEndPoint == NULL){
if(tickcount == 1)
{
WARNING_MSG(boost::format("TelnetServer::handleInputNotification: accept endpoint(%1%) %2%!\n") %
fd % kbe_strerror());
}
break;
}
else
{
TelnetHandler* pTelnetHandler = new TelnetHandler(pNewEndPoint, this, pNetworkInterface_, passwd_.size() > 0 ?
TelnetHandler::TELNET_STATE_PASSWD : (TelnetHandler::TELNET_STATE)this->deflayer());
if(!pDispatcher_->registerFileDescriptor((*pNewEndPoint), pTelnetHandler))
{
ERROR_MSG(boost::format("TelnetServer::start:: registerFileDescriptor(pTelnetHandler) is failed! addr=%1%\n") %
pNewEndPoint->c_str());
delete pTelnetHandler;
continue;
}
INFO_MSG(boost::format("TelnetServer::handleInputNotification: new handler(%1%)!\n") %
pNewEndPoint->c_str());
handlers_[(*pNewEndPoint)].reset(pTelnetHandler);
std::string s;
if(passwd_.size() > 0)
{
s = "password:";
}
else
{
s = pTelnetHandler->getWelcome();
}
pNewEndPoint->send(s.c_str(), s.size());
}
}
return 0;
}
//-------------------------------------------------------------------------------------
}