forked from ralphlange/procServ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clientFactory.cc
294 lines (263 loc) · 9.41 KB
/
clientFactory.cc
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
// Process server for soft ioc
// David H. Thompson 8/29/2003
// Ralph Lange <[email protected]> 2007-2016
// GNU Public License (GPLv3) applies - see www.gnu.org
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <signal.h>
#include "procServ.h"
#include "processClass.h"
#include "libtelnet.h"
static const telnet_telopt_t my_telopts[] = {
{ TELNET_TELOPT_ECHO, TELNET_WILL, 0 },
{ TELNET_TELOPT_LINEMODE, 0, TELNET_DO },
// { TELNET_TELOPT_NAOCRD, TELNET_WILL, 0 },
{ -1, 0, 0 }
};
class clientItem : public connectionItem
{
public:
clientItem(int port, bool readonly);
~clientItem();
void readFromFd(void);
int Send(const char *buf, int len);
int Send(const char * stamp, int stamp_len,
const char * message, int count);
private:
static void telnet_eh(telnet_t *telnet, telnet_event_t *event, void *user_data);
void processInput(const char *buf, int len);
void writeToFd(const char *buf, int len);
telnet_t *_telnet;
static int _users;
static int _loggers;
static int _status;
};
// service and calls clientFactory when clients are accepted
connectionItem * clientFactory(int socketIn, bool readonly)
{
connectionItem *ci = new clientItem(socketIn, readonly);
PRINTF("Created new client connection (clientItem %p; read%s)\n",
ci, readonly?"only":"/write");
return ci;
}
clientItem::~clientItem()
{
if ( _fd >= 0 ) close( _fd );
if (_telnet) telnet_free(_telnet);
PRINTF("~clientItem()\n");
if (_readonly) _loggers--;
else _users--;
}
// Client item constructor
// This sets KEEPALIVE on the socket and displays the greeting
// Also sets the socket SNDTIMEO
clientItem::clientItem(int socketIn, bool readonly) :
connectionItem(socketIn, readonly)
{
assert(socketIn>=0);
int optval = 1;
int i;
struct tm procServStart_tm; // Time when this procServ started
char procServStart_buf[32]; // Time when this procServ started - as string
struct tm IOCStart_tm; // Time when the current IOC was started
char IOCStart_buf[32]; // Time when the current IOC was started - as string
#define BUFLEN 512
char buf1[BUFLEN], buf2[BUFLEN];
char greeting1[] = "@@@ Welcome to procServ (" PROCSERV_VERSION_STRING ")" NL;
#define GREETLEN 256
char greeting2[GREETLEN] = "";
struct timeval send_timeout;
send_timeout.tv_sec = 10;
send_timeout.tv_usec = 0;
PRINTF("New clientItem %p\n", this);
if ( killChar ) {
snprintf(greeting2, GREETLEN, "@@@ Use %s%c to kill the child, ", CTL_SC(killChar));
} else {
snprintf(greeting2, GREETLEN, "@@@ Kill command disabled, ");
}
snprintf(buf1, BUFLEN, "auto restart is %s, ", autoRestart ? "ON" : "OFF");
if ( toggleRestartChar ) {
snprintf(buf2, BUFLEN, "use %s%c to toggle auto restart" NL, CTL_SC(toggleRestartChar));
} else {
snprintf(buf2, BUFLEN, "auto restart toggle disabled" NL);
}
strncat(greeting2, buf1, GREETLEN-strlen(greeting2)-1);
strncat(greeting2, buf2, GREETLEN-strlen(greeting2)-1);
if (logoutChar) {
snprintf(buf2, BUFLEN, "@@@ Use %s%c to logout from procServ server" NL, CTL_SC(logoutChar));
strncat(greeting2, buf2, GREETLEN-strlen(greeting2)-1);
}
localtime_r( &procServStart, &procServStart_tm );
strftime( procServStart_buf, sizeof(procServStart_buf)-1,
timeFormat, &procServStart_tm );
localtime_r( &IOCStart, &IOCStart_tm );
strftime( IOCStart_buf, sizeof(IOCStart_buf)-1,
timeFormat, &IOCStart_tm );
snprintf(buf1, BUFLEN, "@@@ procServ server started at: %s" NL,
procServStart_buf);
if ( processClass::exists() ) {
snprintf(buf2, BUFLEN, "@@@ Child \"%s\" started at: %s" NL,
childName, IOCStart_buf );
strncat(buf1, buf2, BUFLEN-strlen(buf1)-1);
}
snprintf(buf2, BUFLEN, "@@@ %d user(s) and %d logger(s) connected (plus you)" NL,
_users, _loggers);
setsockopt( socketIn, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(optval) );
setsockopt( socketIn, SOL_SOCKET, SO_SNDTIMEO, &send_timeout, sizeof(send_timeout) );
if ( _readonly ) { // Logging client
_loggers++;
} else { // Regular (user) client
_users++;
write( _fd, greeting1, strlen(greeting1) );
write( _fd, greeting2, strlen(greeting2) );
}
write( _fd, infoMessage1, strlen(infoMessage1) );
write( _fd, infoMessage2, strlen(infoMessage2) );
write( _fd, buf1, strlen(buf1) );
if ( ! _readonly )
write( _fd, buf2, strlen(buf2) );
if ( ! processClass::exists() )
write( _fd, infoMessage3, strlen(infoMessage3) );
_telnet = telnet_init(my_telopts, telnet_eh, 0, this);
for (i = 0; my_telopts[i].telopt >= 0; i++) {
if (my_telopts[i].him > 0) {
telnet_negotiate(_telnet, my_telopts[i].him, my_telopts[i].telopt);
}
if (my_telopts[i].us > 0) {
telnet_negotiate(_telnet, my_telopts[i].us, my_telopts[i].telopt);
}
}
}
// clientItem::readFromFd
// Reads from the FD, forwards to telnet state machine
void clientItem::readFromFd(void)
{
char buf[1600];
int len;
len = read(_fd, buf, sizeof(buf)-1);
if (len == 0) {
PRINTF("clientItem:: Got EOF reading input connection\n");
_markedForDeletion = true;
} else if (len < 0) {
PRINTF("clientItem:: Got error reading input connection: %s\n", strerror(errno));
_markedForDeletion = true;
} else if (!_readonly) {
buf[len] = '\0';
telnet_recv(_telnet, buf, len);
}
}
// clientItem::processInput
// Scans for restart / quit char if in child shut down mode,
// else sends the characters to the other connections
void clientItem::processInput(const char *buf, int len)
{
int i;
if (len > 0) {
// Scan input for commands
for (i = 0; i < len; i++) {
if (false == processClass::exists()) { // We're in child shut down mode
if ((restartChar && buf[i] == restartChar)
|| (killChar && buf[i] == killChar)) {
PRINTF ("Got a restart command\n");
waitForManualStart = false;
processClass::restartOnce();
}
if (quitChar && buf[i] == quitChar) {
PRINTF ("Got a shutdown command\n");
shutdownServer = true;
}
}
if (logoutChar && buf[i] == logoutChar) {
PRINTF ("Got a logout command\n");
_markedForDeletion = true;
}
if (toggleRestartChar && buf[i] == toggleRestartChar) {
autoRestart = ! autoRestart;
char msg[128] = NL;
PRINTF ("Got a toggleAutoRestart command\n");
SendToAll(msg, strlen(msg), NULL);
snprintf(msg, 128, "@@@ Toggled auto restart to %s" NL,
autoRestart ? "ON" : "OFF");
SendToAll(msg, strlen(msg), NULL);
}
if (killChar && buf[i] == killChar) {
PRINTF ("Got a kill command\n");
processFactorySendSignal(killSig);
}
}
SendToAll(buf, len, this);
}
}
// Send characters to telnet state machine
int clientItem::Send(const char * buf, int len)
{
if (!_markedForDeletion) {
_status = 0;
telnet_send(_telnet, buf, len);
}
return _status;
}
// Send characters, printing time stamps at every new line
int clientItem::Send(const char * stamp, int stamp_len,
const char * message, int count)
{
if (isLogger()) {
// Some OSs (Windows) do not support line buffering, so we can get parts of lines,
// hence need to track of when to send timestamp
int i = 0, j = 0;
for (i = 0; i < count; ++i) {
if (!_log_stamp_sent) {
Send(stamp, stamp_len);
_log_stamp_sent = true;
}
if (message[i] == '\n') {
Send(message+j, i-j+1);
j = i + 1;
_log_stamp_sent = false;
}
}
return Send(message+j, count-j); // finish off rest of line with no newline at end
} else {
return Send(message, count);
}
}
// Write characters to client FD
void clientItem::writeToFd(const char * buf, int len)
{
int status = 0;
while (-1 == (status = write(_fd, buf, len)) && errno == EINTR);
if (-1 == status) {
_markedForDeletion = true;
_status = status;
}
}
// Event handler for libtelnet
// this is being called when libtelnet process an input buffer
void clientItem::telnet_eh(telnet_t *telnet, telnet_event_t *event, void *user_data)
{
clientItem *client = (clientItem *)user_data;
switch (event->type) {
case TELNET_EV_DATA:
client->processInput(event->data.buffer, event->data.size);
break;
case TELNET_EV_SEND:
client->writeToFd(event->data.buffer, event->data.size);
break;
case TELNET_EV_ERROR:
fprintf(stderr, "TELNET error: %s", event->error.msg);
break;
default:
break;
}
}
int clientItem::_users;
int clientItem::_loggers;
int clientItem::_status;