Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cd4f5d09cee55e3e71f374333c5e0660700e0e84 (plain) (blame)
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/*******************************************************************************
 * Copyright (c) 2007, 2013 Wind River Systems, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v1.0 which accompany this distribution.
 * The Eclipse Public License is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 * You may elect to redistribute this code under either of these licenses.
 *
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/

/*
 * This module implements tunneling of TCF messages to another target on behalf of a client
 * This service intended to be used when a client has no direct access to a target.
 */

#include <tcf/config.h>
#include <assert.h>
#include <string.h>
#include <tcf/framework/json.h>
#include <tcf/framework/proxy.h>
#include <tcf/framework/protocol.h>
#include <tcf/framework/trace.h>
#include <tcf/framework/errors.h>
#include <tcf/framework/exceptions.h>
#include <tcf/framework/myalloc.h>

typedef struct Proxy {
    Channel * c;
    Protocol * proto;
    int other;
    int instance;
} Proxy;

typedef struct RedirectInfo {
    Channel * host;
    char token[256];
} RedirectInfo;

static ChannelRedirectionListener redirection_listeners[16];
static int redirection_listeners_cnt = 0;

static ProxyLogFilterListener proxy_log_filter_listener;

static void proxy_update(Channel * c1, Channel * c2);

static void proxy_connecting(Channel * c) {
    int i;
    Proxy * target = (Proxy *)c->client_data;
    Proxy * host = target + target->other;

    assert(c == target->c);
    assert(target->other == -1);
    assert(c->state == ChannelStateStarted);
    assert(host->c->state == ChannelStateHelloReceived);

    for (i = 0; i < redirection_listeners_cnt; i++) {
        redirection_listeners[i](host->c, target->c);
    }

    target->c->disable_zero_copy = !host->c->out.supports_zero_copy;
    send_hello_message(target->c);

    trace(LOG_PROXY, "Proxy waiting Hello from target");
}

static void command_redirect_done (Channel * c, void * client_data, int error) {
    RedirectInfo * info = (RedirectInfo *)client_data;

    if (!is_channel_closed(info->host)) {
        int err = error;

        if (err == 0) {
            proxy_update (info->host, c);
        }

        write_stringz(&info->host->out, "R");
        write_stringz(&info->host->out, info->token);
        write_errno(&info->host->out, err);
        write_stream(&info->host->out, MARKER_EOM);
    }

    channel_unlock(info->host);
    loc_free (info);
}

static void read_peer_attr(InputStream * inp, const char * name, void * x) {
    peer_server_addprop((PeerServer *)x, loc_strdup(name), json_read_alloc_string(inp));
}

static void command_locator_redirect(char * token, Channel * c, void * args) {
    char id[256];
    PeerServer * ps = NULL;
    Channel * target = (Channel *)args;
    RedirectInfo * info = (RedirectInfo *)loc_alloc_zero(sizeof(RedirectInfo));

    if (peek_stream(&c->inp) == '{') {
        ps = peer_server_alloc();
        json_read_struct(&c->inp, read_peer_attr, ps);
    }
    else {
        json_read_string(&c->inp, id, sizeof(id));
    }

    json_test_char(&c->inp, MARKER_EOA);
    json_test_char(&c->inp, MARKER_EOM);

    channel_lock(c);
    info->host = c;
    strlcpy(info->token, token, sizeof(info->token));

    /* Send the redirect command to the next TCF entity */

    if (ps != NULL) {
        send_redirect_command_by_props(target, ps, command_redirect_done, info);
    }
    else {
        send_redirect_command_by_id(target, id, command_redirect_done, info);
    }

    if (ps != NULL) peer_server_free(ps);
}

static void proxy_connected(Channel * c) {
    int i;
    Proxy * target = (Proxy *)c->client_data;
    Proxy * host = target + target->other;

    assert(target->c == c);
    if (target->other == 1) {
        /* We get here after sending hello to host */
        return;
    }
    assert(c->state == ChannelStateConnected);
    assert(host->c->state == ChannelStateHelloReceived);

    host->c->disable_zero_copy = !target->c->out.supports_zero_copy;

    trace(LOG_PROXY, "Proxy connected, target services:");
    for (i = 0; i < target->c->peer_service_cnt; i++) {
        char * nm = target->c->peer_service_list[i];
        trace(LOG_PROXY, "    %s", nm);
        if (strcmp(nm, "ZeroCopy") == 0) continue;
        protocol_get_service(host->proto, nm);
    }

    /*
     * Intercept the Locator.redirect command to update the local list of
     * services with the ones from the next TCF entity (agent), and send a
     * consolidate list to the previous TCF entity (client). This is
     * required in the case of more than one server between the client and
     * the agent.
     */

    add_command_handler2(host->c->protocol, "Locator", "redirect",
                         command_locator_redirect, target->c);

    for (i = 0; i < redirection_listeners_cnt; i++) {
        redirection_listeners[i](host->c, target->c);
    }

    send_hello_message(host->c);
}

static void proxy_disconnected(Channel * c) {
    Proxy * proxy = (Proxy *)c->client_data;

    assert(c == proxy->c);
    if (proxy[proxy->other].c->state == ChannelStateDisconnected) {
        trace(LOG_PROXY, "Proxy disconnected");
        if (proxy->other == -1) proxy--;
        broadcast_group_free(c->bcg);
        assert(proxy[0].c->bcg == NULL);
        assert(proxy[1].c->bcg == NULL);
        proxy[0].c->client_data = NULL;
        proxy[1].c->client_data = NULL;
        protocol_release(proxy[0].proto);
        protocol_release(proxy[1].proto);
        channel_unlock(proxy[0].c);
        channel_unlock(proxy[1].c);
        loc_free(proxy);
    }
    else {
        channel_close(proxy[proxy->other].c);
    }
}

#if ENABLE_Trace

static char log_buf[1024];
static size_t log_pos = 0;

static void log_chr(int c) {
    if (log_pos + 2 < sizeof log_buf) log_buf[log_pos++] = (char)c;
}

static void log_str(const char * s) {
    char c;
    while ((c = *s++) != '\0') {
        if (log_pos + 2 < sizeof log_buf) log_buf[log_pos++] = c;
    }
}

static void log_byte_func(int i) {
    if (i >= ' ' && i < 127) {
        /* Printable ASCII  */
        log_chr(i);
    }
    else if (i == 0) {
        log_chr(' ');
    }
    else if (i > 0) {
        char buf[16];
        snprintf(buf, sizeof buf, "\\x%02x", i);
        log_str(buf);
    }
    else if (i == MARKER_EOM) {
        log_str("<eom>");
    }
    else if (i == MARKER_EOS) {
        log_str("<eom>");
    }
    else {
        log_str("<?>");
    }
}

#define log_byte(b) { if (log_mode & LOG_TCFLOG) log_byte_func(b); }

static int log_start(Proxy * proxy, char ** argv, int argc) {
    int i;
    log_pos = 0;
    if (log_mode & LOG_TCFLOG) {
        if (proxy_log_filter_listener &&
            proxy_log_filter_listener(proxy->c, proxy[proxy->other].c, argc, argv))
            return 1;
        log_str(proxy->other > 0 ? "---> " : "<--- ");
        for (i = 0; i < argc; i++) {
            log_str(argv[i]);
            log_chr(' ');
        }
    }
    return 0;
}

static void log_flush(Proxy * proxy) {
    if (log_mode & LOG_TCFLOG) {
        log_chr(0);
        trace(LOG_TCFLOG, "%d: %s", proxy->instance, log_buf);
    }
}

#else

#define log_start(a, b, c) 0
#define log_byte(a) do {} while(0)
#define log_flush(a) do {} while(0)

#endif

static void proxy_default_message_handler(Channel * c, char ** argv, int argc) {
    /* TODO: if proxy is connected to itself, it can deadlock when retransmitting a long message */
    Proxy * proxy = (Proxy *)c->client_data;
    Channel * otherc = proxy[proxy->other].c;
    InputStream * inp = &c->inp;
    OutputStream * out = &otherc->out;
    int i = 0;
    int filtered = 0;

    assert(c == proxy->c);
    assert(argc > 0 && strlen(argv[0]) == 1);
    if (proxy[proxy->other].c->state == ChannelStateDisconnected) return;

    if (argv[0][0] == 'C') {
        write_stringz(out, argv[0]);
        /* Prefix token with 'R'emote to distinguish from locally generated commands */
        write_stream(out, 'R');
        i = 1;
    }
    else if (argv[0][0] == 'R' || argv[0][0] == 'P' || argv[0][0] == 'N') {
        if (argv[1][0] != 'R') {
            trace(LOG_ALWAYS, "Reply with unexpected token: %s", argv[1]);
            exception(ERR_PROTOCOL);
        }
        argv[1]++;
    }

    while (i < argc) write_stringz(out, argv[i++]);

    filtered = log_start(proxy, argv, argc);

    /* Copy body of message */
    do {
        if (out->supports_zero_copy &&
#if ENABLE_Trace
               (log_mode & LOG_TCFLOG) == 0 &&
#endif
                inp->end - inp->cur >= 0x100) {
            write_block_stream(out, (char *)inp->cur, inp->end - inp->cur);
            inp->cur = inp->end;
        }
        else {
            i = read_stream(inp);
            if (!filtered) log_byte(i);
            write_stream(out, i);
        }
    }
    while (i != MARKER_EOM && i != MARKER_EOS);
    if (!filtered) log_flush(proxy);
}

static void proxy_update(Channel * c1, Channel * c2) {
    Proxy * proxy;
    int ix;

    /* c1 is host */
    assert (c1->state == ChannelStateConnected);
    /* c2 is target */
    assert (c2->state == ChannelStateHelloSent);

    /* Check that both channels form a proxy */

    assert(proxy_get_host_channel(c1) == c1);
    assert(proxy_get_target_channel(c1) == c2);
    assert(proxy_get_host_channel(c2) == c1);
    assert(proxy_get_target_channel(c2) == c2);

    proxy = (Proxy *)c1->client_data;
    if (proxy->other == -1) proxy--;

    /* Create new protocols for the channels */

    /* Host */
    proxy[0].proto = protocol_alloc();

    /* Target */
    proxy[1].proto = protocol_alloc();

    trace(LOG_PROXY, "Proxy updated, host services:");
    for (ix = 0; ix < c1->peer_service_cnt; ix++) {
        char * nm = c1->peer_service_list[ix];
        trace(LOG_PROXY, "    %s", nm);
        if (strcmp(nm, "ZeroCopy") == 0) continue;
        protocol_get_service(proxy[1].proto, nm);
    }
    /*
     * Update the state of the host channel to react correctly on the new Hello
     * message from the target, sending a new Hello event to the host
     * with all the services of the target plus the ones of this
     * TCF entity.
     */

    c1->state = ChannelStateHelloReceived;

    /*
     * Notify close of the host channel; a notification about a new opening is
     * sent when the Hello event is received from the target.
     */

    notify_channel_closed(c1);

    protocol_release(c1->protocol);
    protocol_release(c2->protocol);

    c1->protocol = proxy[0].proto;
    set_default_message_handler(proxy[0].proto, proxy_default_message_handler);
    c2->protocol = proxy[1].proto;
    set_default_message_handler(proxy[1].proto, proxy_default_message_handler);
}

void proxy_create(Channel * c1, Channel * c2) {
    TCFBroadcastGroup * bcg = broadcast_group_alloc();
    Proxy * proxy = (Proxy *)loc_alloc_zero(2 * sizeof *proxy);
    int i;

    static int instance;

    assert(c1->state == ChannelStateRedirectReceived);
    assert(c2->state == ChannelStateStartWait);

    /* Host */
    channel_lock(c1);
    proxy[0].c = c1;
    proxy[0].proto = protocol_alloc();
    proxy[0].other = 1;
    proxy[0].instance = instance;

    /* Target */
    channel_lock(c2);
    proxy[1].c = c2;
    proxy[1].proto = protocol_alloc();
    proxy[1].other = -1;
    proxy[1].instance = instance++;

    trace(LOG_PROXY, "Proxy created, host services:");
    for (i = 0; i < c1->peer_service_cnt; i++) {
        char * nm = c1->peer_service_list[i];
        trace(LOG_PROXY, "    %s", nm);
        if (strcmp(nm, "ZeroCopy") == 0) continue;
        protocol_get_service(proxy[1].proto, nm);
    }
    c1->state = ChannelStateHelloReceived;
    notify_channel_closed(c1);
    protocol_release(c1->protocol);
    c1->client_data = NULL;
    assert(c2->protocol == NULL);

    c1->connecting = proxy_connecting;
    c1->connected = proxy_connected;
    c1->disconnected = proxy_disconnected;
    c1->client_data = proxy;
    c1->protocol = proxy[0].proto;
    set_default_message_handler(proxy[0].proto, proxy_default_message_handler);

    c2->connecting = proxy_connecting;
    c2->connected = proxy_connected;
    c2->disconnected = proxy_disconnected;
    c2->client_data = proxy + 1;
    c2->protocol = proxy[1].proto;
    set_default_message_handler(proxy[1].proto, proxy_default_message_handler);

    channel_set_broadcast_group(c1, bcg);
    channel_set_broadcast_group(c2, bcg);
    channel_start(c2);
}

Channel * proxy_get_host_channel(Channel * c) {
    Proxy * proxy = (Proxy *)c->client_data;

    if (c->connecting != proxy_connecting || proxy == NULL || c != proxy->c) return NULL;
    if (proxy->other == -1) proxy--;
    return proxy[0].c;
}

Channel * proxy_get_target_channel(Channel * c) {
    Proxy * proxy = (Proxy *)c->client_data;

    if (c->connecting != proxy_connecting || proxy == NULL || c != proxy->c) return NULL;
    if (proxy->other == -1) proxy--;
    return proxy[1].c;
}

void add_channel_redirection_listener(ChannelRedirectionListener listener) {
    assert(redirection_listeners_cnt < (int)(sizeof(redirection_listeners) / sizeof(ChannelRedirectionListener)));
    redirection_listeners[redirection_listeners_cnt++] = listener;
}

ProxyLogFilterListener set_proxy_log_filter_listener(ProxyLogFilterListener listener) {
    ProxyLogFilterListener old = proxy_log_filter_listener;
    proxy_log_filter_listener = listener;
    return old;
}

Back to the top