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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
|
//***************************************************************************
// Group VDR/GraphTFT
// File tcpchannel.c
// Date 31.10.06
// This code is distributed under the terms and conditions of the
// GNU GENERAL PUBLIC LICENSE. See the file COPYING for details.
// (c) 2006-2013 Jörg Wendel
//--------------------------------------------------------------------------
// Class TcpChannel
//***************************************************************************
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <netdb.h>
#include <errno.h>
#include <string.h>
#include "tcpchannel.h"
#include "common.h"
//***************************************************************************
// Object
//***************************************************************************
TcpChannel::TcpChannel(int aTimeout, int aHandle)
{
handle = aHandle;
timeout = aTimeout;
localAddr = 0;
port = 0;
nTtlSent = 0;
nTtlReceived = 0;
lookAheadChar = false;
lookAhead = 0;
}
TcpChannel::~TcpChannel()
{
close();
}
//***************************************************************************
// Open -> Start Listener
//***************************************************************************
int TcpChannel::open(unsigned short aPort, const char* aLocalHost)
{
struct sockaddr_in localSockAddr;
struct hostent* hostInfo;
int value = 1;
int aHandle;
// clear
memset((char*)&localSockAddr, 0, sizeof(localSockAddr));
// init
localSockAddr.sin_family = AF_INET;
// resolve local host
if (aLocalHost && *aLocalHost)
{
// search alias
if ((hostInfo = ::gethostbyname(aLocalHost)))
memcpy((char*)&localAddr, hostInfo->h_addr, hostInfo->h_length);
else if ((unsigned int)(localAddr = inet_addr(aLocalHost)) == INADDR_NONE)
{
tell(1, "unknown hostname '%s'", aLocalHost);
return fail;
}
// set local endpoint
memcpy(&localSockAddr.sin_addr, &localAddr, sizeof(struct in_addr));
}
// Server-Socket
localSockAddr.sin_port = htons(aPort);
// open socket
if ((aHandle = ::socket(PF_INET, SOCK_STREAM, 0)) < 0)
{
tell(1, "Error: ");
return fail;
}
// set socket non-blocking
if (fcntl(aHandle, F_SETFL, O_NONBLOCK) < 0)
tell(1, "Error: Setting socket options failed, errno (%d)", errno);
setsockopt(aHandle, SOL_SOCKET, SO_REUSEADDR,
(char*)&value, sizeof(value));
// bind address to socket
if (::bind(aHandle, (struct sockaddr*)&localSockAddr, sizeof(localSockAddr)) < 0)
{
::close(aHandle);
tell(1, "Error: Bind failed, errno (%d)", errno);
return fail;
}
if (::listen(aHandle, 5) < 0)
{
::close(aHandle);
return fail;
}
// save
handle = aHandle;
port = aPort;
return success;
}
//***************************************************************************
// Close
//***************************************************************************
int TcpChannel::close()
{
if (handle)
{
::close(handle);
handle = 0;
}
return success;
}
//***************************************************************************
// Listen
//***************************************************************************
int TcpChannel::listen(TcpChannel*& child)
{
struct sockaddr_in remote;
struct timeval tv;
fd_set readFD;
int aHandle, num, len;
child = 0;
tv.tv_sec = 0;
tv.tv_usec = 1;
len = sizeof(remote);
// clear and set file-descriptor
FD_ZERO(&readFD);
FD_SET(handle, &readFD);
// call select to look for request
if ((num = ::select(handle+1, &readFD,(fd_set*)0,(fd_set*)0, &tv)) < 0)
return checkErrno();
if (!FD_ISSET(handle, &readFD))
return wrnNoConnectIndication;
// accept client
if ((aHandle = ::accept(handle, (struct sockaddr*)&remote, (socklen_t*)&len)) < 0)
{
tell(1, "Error: Accept failed, errno was %d - '%s'", errno, strerror(errno));
return errAcceptFailed;
}
// set none blocking, event for the new connection
if (fcntl(aHandle, F_SETFL, O_NONBLOCK) < 0)
return fail;
// create new tcp channel
child = new TcpChannel(timeout, aHandle);
return success;
}
//***************************************************************************
// Write to client
//***************************************************************************
int TcpChannel::write(int command, const char* buf, int bufLen)
{
struct timeval wait;
int result, nfds;
fd_set writeFD;
int nSent = 0;
Header header;
if (!handle)
return fail;
cMutexLock lock(&_mutex);
if (buf && !bufLen)
bufLen = strlen(buf);
tell(3, "Writing (%ld) header bytes, command (%d), size (%d)",
sizeof(Header), command, bufLen);
header.command = htonl(command);
header.size = htonl(bufLen);
result = ::write(handle, &header, sizeof(Header));
if (result != sizeof(Header))
return errIOError;
if (!buf)
return success;
tell(3, "Writing (%uld) kb now", bufLen/1024);
do
{
result = ::write(handle, buf + nSent, bufLen - nSent);
if (result < 0)
{
if (errno != EWOULDBLOCK)
return checkErrno();
// time-out for select
wait.tv_sec = timeout;
wait.tv_usec = 1;
// clear and set file-descriptors
FD_ZERO(&writeFD);
FD_SET(handle, &writeFD);
// look event
if ((nfds = ::select(handle+1, 0, &writeFD, 0, &wait)) < 0)
{
// Error: Select failed
return checkErrno();
}
// no event occured -> timeout
if (nfds == 0)
return wrnTimeout;
}
else
{
nSent += result;
}
} while (nSent < bufLen);
// increase send counter
nTtlSent += nSent;
return success;
}
//***************************************************************************
// Look
//***************************************************************************
int TcpChannel::look(int aTimeout)
{
struct timeval tv;
fd_set readFD, writeFD;
int n;
if (!handle)
return fail;
// time-out for select
tv.tv_sec = aTimeout;
tv.tv_usec = 1;
// clear and set file-descriptors
FD_ZERO(&readFD);
FD_ZERO(&writeFD);
FD_SET(handle, &readFD);
FD_SET(handle, &writeFD);
// look event
n = ::select(handle+1, &readFD, (aTimeout ? 0 : &writeFD) , 0, &tv);
if (n < 0)
return checkErrno();
// // check write ok
// if (!FD_ISSET(handle, &writeFD))
// return wrnChannelBlocked;
// check read-event
if (!FD_ISSET(handle, &readFD))
return wrnNoEventPending;
// check first-char
if (::read(handle, &lookAheadChar, 1) == 0)
return errConnectionClosed;
// look ahead char received
lookAhead = true;
return success;
}
//***************************************************************************
// Read
//***************************************************************************
int TcpChannel::read(char* buf, int bufLen)
{
int nfds, result;
fd_set readFD;
int nReceived;
struct timeval wait;
if (!handle)
return fail;
memset(buf, 0, bufLen);
nReceived = 0;
if (lookAhead)
{
*(buf) = lookAheadChar;
lookAhead = false;
nReceived++;
}
while (nReceived < bufLen)
{
result = ::read(handle, buf + nReceived, bufLen - nReceived);
if (result < 0)
{
if (errno != EWOULDBLOCK)
return checkErrno();
// time-out for select
wait.tv_sec = timeout;
wait.tv_usec = 1;
// clear and set file-descriptors
FD_ZERO(&readFD);
FD_SET(handle, &readFD);
// look event
if ((nfds = ::select(handle+1, &readFD, 0, 0, &wait)) < 0)
return checkErrno();
// no event occured -> timeout
if (nfds == 0)
return wrnTimeout;
}
else if (result == 0)
{
// connection closed -> eof received
return errConnectionClosed;
}
else
{
// inc read char count
nReceived += result;
}
}
nTtlReceived += nReceived;
tell(3, "Debug: Received %d bytes\n", nReceived);
return success;
}
//***************************************************************************
// Check Errno
//***************************************************************************
int TcpChannel::checkErrno()
{
switch (errno)
{
case EINTR: return wrnSysInterrupt;
case EBADF: return errInvalidEndpoint;
case EWOULDBLOCK: return wrnNoDataAvaileble;
case ECONNRESET: return errConnectionClosed;
default: return errIOError;
}
}
|