-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathctx.c
399 lines (315 loc) · 9.63 KB
/
ctx.c
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
/* @(#) client/server context implementation
*
* Copyright 2008-2011 Pavel V. Cherenkov ([email protected])
*
* This file is part of udpxy.
*
* udpxy 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.
*
* udpxy 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 udpxy. If not, see <http://www.gnu.org/licenses/>.
*/
#include "osdef.h" /* os-specific definitions */
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <netinet/in.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include "ctx.h"
#include "udpxy.h"
#include "util.h"
#include "mtrace.h"
extern FILE* g_flog;
extern const char IPv4_ALL[];
/* initialize server context data
*/
int
init_server_ctx( struct server_ctx* ctx,
const size_t max,
const char* laddr, uint16_t lport,
const char* mifc_addr )
{
int flags = -1;
assert( lport && mifc_addr && ctx && max );
ctx->lsockfd = 0;
(void) strncpy( ctx->listen_addr, (laddr ? laddr : IPv4_ALL),
IPADDR_STR_SIZE );
ctx->listen_addr[ IPADDR_STR_SIZE - 1 ] = '\0';
ctx->listen_port = lport;
(void) strncpy( ctx->mcast_ifc_addr, mifc_addr, IPADDR_STR_SIZE );
ctx->mcast_ifc_addr[ IPADDR_STR_SIZE - 1 ] = '\0';
ctx->cl = calloc(max, sizeof(struct client_ctx));
if( NULL == ctx->cl ) {
mperror( g_flog, errno, "%s: client_t - calloc", __func__ );
return ERR_INTERNAL;
}
(void) memset( ctx->cl, 0, max * sizeof(struct client_ctx) );
ctx->clfree = ctx->clmax = max;
(void) memset( &ctx->rq, 0, sizeof(ctx->rq) );
if( 0 != pipe(ctx->cpipe) ) {
mperror( g_flog, errno, "%s: pipe", __func__ );
return ERR_INTERNAL;
}
/* make reading end of pipe non-blocking (we don't want to
* block on pipe read on the server side)
*/
if( -1 == (flags = fcntl( ctx->cpipe[0], F_GETFL )) ||
-1 == fcntl( ctx->cpipe[0], F_SETFL, flags | O_NONBLOCK ) ) {
mperror( g_flog, errno, "%s: fcntl", __func__ );
return ERR_INTERNAL;
}
return 0;
}
void
free_server_ctx( struct server_ctx* ctx )
{
int i = -1;
assert(ctx);
free( ctx->cl );
for( i = 0; i < 2; ++i ) {
if( ctx->cpipe[i] < 0 ) continue;
if( 0 != close( ctx->cpipe[i] ) ) {
mperror( g_flog, errno,
"%s: close(pipe)", __func__ );
}
}
(void) memset( ctx, 0, sizeof(struct server_ctx) );
return;
}
/* find index of the first client with the given pid
*/
int
find_client( const struct server_ctx* ctx, pid_t pid )
{
int i = -1;
assert( ctx && (pid >= 0) );
for( i = 0; (size_t)i < ctx->clmax; ++i ) {
if( ctx->cl[ i ].pid == pid )
return i;
}
return -1;
}
/* populate connection's source info in client context
*/
static int
get_src_info( struct client_ctx* cl, int sockfd )
{
struct stat st;
struct sockaddr_in addr;
a_socklen_t len = 0;
int rc = 0;
assert( cl );
if( -1 == (rc = fstat( sockfd, &st )) ) {
mperror( g_flog, errno, "%s: fstat", __func__ );
return ERR_INTERNAL;
}
if( S_ISREG( st.st_mode ) ) {
(void) strncpy( cl->src_addr, "File", sizeof(cl->src_addr) );
cl->src_addr[ sizeof(cl->src_addr) - 1 ] = '\0';
cl->src_port = 0;
}
else if( S_ISSOCK( st.st_mode ) ) {
len = sizeof(addr);
rc = getpeername( sockfd, (struct sockaddr*)&addr, &len );
if( 0 == rc ) {
(void)strncpy( cl->src_addr, inet_ntoa(addr.sin_addr),
sizeof(cl->src_addr) - 1 );
cl->src_addr[ sizeof(cl->src_addr) - 1 ] = '\0';
cl->src_port = ntohs(addr.sin_port);
}
else {
mperror( g_flog, errno, "%s: getpeername", __func__ );
return ERR_INTERNAL;
}
} /* S_ISSOCK */
return rc;
}
/* add client to server context
*/
int
add_client( struct server_ctx* ctx,
pid_t cpid, const char* maddr, uint16_t mport,
int sockfd )
{
struct client_ctx* client = NULL;
int index = -1;
int rc = 0;
assert( ctx && maddr && mport );
index = find_client( ctx, 0 );
if( -1 == index )
return ERR_INTERNAL;
client = &(ctx->cl[ index ]);
client->pid = cpid;
(void) strncpy( client->mcast_addr, maddr, IPADDR_STR_SIZE );
client->mcast_addr[ IPADDR_STR_SIZE - 1 ] = '\0';
client->mcast_port = mport;
if (ctx->rq.tail[0])
(void) strcpy( client->tail, ctx->rq.tail );
rc = get_src_info( client, sockfd );
if( 0 != rc ) {
client->pid = 0;
return ERR_INTERNAL;
}
/* init sender id: 0 indicates no stats */
client->tstat.sender_id = 0;
ctx->clfree--;
if( g_flog ) {
(void)tmfprintf( g_flog, "Added client: pid=[%d], maddr=[%s], mport=[%d], "
"saddr=[%s], sport=[%d]\n",
(int)cpid, maddr, (int)mport, client->src_addr, client->src_port );
}
return 0;
}
/* delete client from server context
*/
int
delete_client( struct server_ctx* ctx, pid_t cpid )
{
struct client_ctx* client = NULL;
int index = -1;
assert( ctx && (cpid > 0) );
index = find_client( ctx, cpid );
if( -1 == index ) {
return ERR_INTERNAL;
}
client = &(ctx->cl[ index ]);
client->pid = 0;
ctx->clfree++;
if( g_flog ) {
TRACE( (void)tmfprintf( g_flog, "Deleted client: pid=[%d]\n", cpid) );
}
return 0;
}
/* init traffic relay statistics
*/
void
tpstat_init( struct tps_data* d, int setpid )
{
assert( d );
if( setpid )
d->pid = getpid();
d->tm_from = time(NULL);
d->niter = 0;
d->nbytes = 0;
return;
}
/* send statistics update to server (if it's time)
*/
void
tpstat_update( struct server_ctx* ctx,
struct tps_data* d, ssize_t nbytes )
{
static const double MAX_NOUPDATE_ITER = 1000.0;
static const double MAX_SEC = 10.0;
struct tput_stat ts;
int writefd = -1, rc = 0;
const int DO_NOT_SET_PID = 0;
ssize_t nwr = -1;
double nsec;
assert( ctx && d );
d->nbytes += nbytes;
nsec = difftime( time(NULL), d->tm_from );
d->niter++;
if( !((d->niter >= MAX_NOUPDATE_ITER) || (nsec >= MAX_SEC)) )
return;
/* attempt to send update to server */
d->niter = 0;
ts.sender_id = d->pid;
ts.nbytes = d->nbytes;
ts.nsec = nsec;
writefd = ctx->cpipe[1];
do {
nwr = write( writefd, &ts, sizeof(ts) );
if( nwr <= 0 ) {
if( (EINTR != errno) && (EAGAIN != errno) ) {
mperror( g_flog, errno, "%s: write", __func__ );
rc = -1;
break;
}
/* if it's an interrupt or pipe full - ignore */
TRACE( (void)tmfprintf( g_flog, "%s - write error [%s] - ignored\n",
__func__, strerror(errno)) );
break;
}
if( sizeof(ts) != (size_t)nwr ) {
(void)tmfprintf( g_flog, "%s - wrote [%d] bytes to pipe, "
"expected [%u]\n",
__func__, nwr, (int)sizeof(ts) );
rc = -1;
break;
}
/*
TRACE( (void)tmfprintf( g_flog,
"Sent TSTAT={ sender=[%ld], bytes=[%f], seconds=[%f] }\n",
(long)ts.sender_id, ts.nbytes, ts.nsec) );
*/
} while(0);
if( 0 == rc ) {
tpstat_init( d, DO_NOT_SET_PID );
}
return;
}
/* read client statistics data and update the context
*/
int
tpstat_read( struct server_ctx* ctx )
{
int cindex = -1;
int readfd = -1;
ssize_t nread = 0;
struct tput_stat ts;
struct client_ctx* client = NULL;
assert( ctx );
readfd = ctx->cpipe[0];
assert( readfd > 0 );
(void)memset( &ts, 0, sizeof(ts) );
nread = read( readfd, &ts, sizeof(ts) );
if( nread <= 0 ) {
if( (EINTR != errno) && (EAGAIN != errno) ) {
mperror( g_flog, errno, "%s: read", __func__ );
return ERR_INTERNAL;
}
/* if it's an interrupt or no data available - ignore */
TRACE( (void)tmfprintf( g_flog, "%s - read error [%s] - ingored\n",
__func__, strerror(errno)) );
return 0;
}
if( sizeof(ts) != (size_t)nread ) {
(void)tmfprintf( g_flog, "%s - read [%d] bytes from pipe, expected [%u]\n",
__func__, nread, (int)sizeof(ts) );
return ERR_INTERNAL;
}
TRACE( (void)tmfprintf( g_flog,
"Received TSTAT={ sender=[%ld], bytes=[%f], seconds=[%f] }\n",
(long)ts.sender_id, ts.nbytes, ts.nsec) );
cindex = find_client( ctx, (pid_t)ts.sender_id );
if( -1 == cindex ) {
(void)tmfprintf( g_flog, "%s - cannot find client [%ld]\n",
__func__, (long)ts.sender_id );
/* ignore invalid client id's */
return 0;
}
client = &(ctx->cl[ cindex ]);
client->tstat = ts;
TRACE( (void)tmfprintf( g_flog, "Updated context for pid=[%d]; "
"[%.1f] Kb/sec\n", client->pid, (ts.nbytes / 1024) / ts.nsec ) );
return 0;
}
/* __EOF__ */