aboutsummaryrefslogtreecommitdiffstats
path: root/src/bridge.c
blob: 5a9de23d8d9e57fb164991d1088d1a25ec8158d3 (plain)
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/*
   Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 Her Majesty the
   Queen in Right of Canada (Communications Research Center Canada)
   */
/*
   This file is part of CRC-DabMux.

   CRC-DabMux 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.

   CRC-DabMux 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 CRC-DabMux.  If not, see <http://www.gnu.org/licenses/>.
   */

#include <stdio.h>
#ifdef _WIN32
#  include <winsock2.h>
#else
#  include <netinet/in.h>
#endif // _WIN32
#include <string.h>
#include "bridge.h"
#include "crc.h"

#include <assert.h>
#include "PcDebug.h"

#ifdef _WIN32
#  ifdef _DEBUG
     int bridgeVerbosity = 0;
#  endif
#else
#  ifdef DEBUG
     int bridgeVerbosity = 0;
#  endif
#endif

void printStats(struct bridgeInfo* info, FILE* out)
{
    struct bridgeStats stats = getStats(info);
    fprintf(out, "frames     : %lu\n", stats.frames);
    fprintf(out, " valids    : %lu\n", stats.valids);
    fprintf(out, " invalids  : %lu\n", stats.invalids);
    fprintf(out, " bytes     : %lu\n", stats.bytes);
    fprintf(out, " packets   : %lu\n", stats.packets);
    fprintf(out, " errors    : %lu\n", stats.errors);
    fprintf(out, "  missings : %lu\n", stats.missings);
    fprintf(out, "  dropped  : %lu\n", stats.dropped);
    fprintf(out, "  crc      : %lu\n", stats.crc);
    fprintf(out, "  overruns : %lu\n", stats.overruns);
}


void resetStats(struct bridgeInfo* info)
{
    memset(&info->stats, 0, sizeof(info->stats));
}


struct bridgeStats getStats(struct bridgeInfo* info)
{
    return info->stats;
}


void bridgeInitInfo(struct bridgeInfo* info)
{
    memset(info, 0, sizeof(*info));
    info->transmitted = -8;
};


int writePacket(void* dataIn, int sizeIn, void* dataOut, int sizeOut,
        struct bridgeInfo* info)
{
    static struct bridgeHdr header = { 0 };

    PDEBUG4_VERBOSE(1, bridgeVerbosity, "writePacket\n sizeIn: %i, sizeOut: %i, "
            "offset: %i, transmitted: %i\n",
            sizeIn, sizeOut, info->offset, info->transmitted);

    assert(info->transmitted < sizeIn);

    if ((info->offset == 0) && (sizeIn > 0)) {
        ((unsigned short*)dataOut)[0] = 0xb486;
        info->offset = 2;
    }
    if (sizeIn == 0) {
        memset((unsigned char*)dataOut + info->offset, 0, sizeOut - info->offset);
        info->offset = 0;
        info->transmitted = -8;
        PDEBUG1_VERBOSE(1, bridgeVerbosity, " return %i (sizeIn == 0)\n",
                sizeOut);
        return 0;
    }

    while (info->offset < sizeOut) {
        switch (info->transmitted) {
        case (-8):
            ((unsigned char*)dataOut)[info->offset++] = 0xcb;
            ++info->transmitted;
            break;
        case (-7):
            ((unsigned char*)dataOut)[info->offset++] = 0x28;
            ++info->transmitted;
            break;
        case (-6):
            header.size = htons((unsigned short)sizeIn);
            header.crc = htons((unsigned short)(crc16(0xffff, &header, 4) ^ 0xffff));
            ((unsigned char*)dataOut)[info->offset++] = ((char*)&header)[0];
            ++info->transmitted;
            break;
        case (-5):
            ((unsigned char*)dataOut)[info->offset++] = ((char*)&header)[1];
            ++info->transmitted;
            break;
        case (-4):
            ((unsigned char*)dataOut)[info->offset++] = ((char*)&header)[2];
            ++info->transmitted;
            break;
        case (-3):
            ((unsigned char*)dataOut)[info->offset++] = ((char*)&header)[3];
            ++info->transmitted;
            break;
        case (-2):
            ((unsigned char*)dataOut)[info->offset++] = ((char*)&header)[4];
            ++info->transmitted;
            break;
        case (-1):
            ((unsigned char*)dataOut)[info->offset++] = ((char*)&header)[5];
            ++info->transmitted;
            header.seqNb = htons((unsigned short)(ntohs(header.seqNb) + 1));
            break;
        default:
            ((unsigned char*)dataOut)[info->offset++] =
                ((unsigned char*)dataIn)[info->transmitted++];
            if (info->transmitted == sizeIn) {
                PDEBUG2_VERBOSE(1, bridgeVerbosity,
                        " Packet done, %i bytes at offset %i\n",
                        info->transmitted, info->offset);
                PDEBUG1_VERBOSE(1, bridgeVerbosity,
                        " return %i (sizeIn == transmitted)\n", info->offset);
                info->transmitted = -8;
                return info->offset;
            }
        }
    }

    PDEBUG1_VERBOSE(1, bridgeVerbosity, " return %i (offset >= sizeOut)\n",
            info->offset);
    info->offset = 0;
    return 0;
}


int getPacket(void* dataIn, int sizeIn, void* dataOut, int sizeOut,
        struct bridgeInfo* info, char async)
{
    unsigned char* in = (unsigned char*)dataIn;
    unsigned char* out = (unsigned char*)dataOut;
    unsigned char ch;
    unsigned short crc;
    unsigned short diff;

    PDEBUG3_VERBOSE(1, bridgeVerbosity,
            "getPacket\n pos\t%i\n state\t%i\n received\t%i\n",
            info->pos, info->state, info->received);

    if (info->pos == 0) {
        ++info->stats.frames;
        if (((unsigned short*)dataIn)[0] != 0xb486) {
            if (((unsigned short*)dataIn)[0] != 0) {
                ++info->stats.invalids;
                printf("WARNING: processing frame with invalid magic "
                        "number!\n");
            } else {
                PDEBUG0_VERBOSE(1, bridgeVerbosity,
                        "getPacket: not a valid frame\n");
                return 0;
            }
        } else {
            PDEBUG0_VERBOSE(2, bridgeVerbosity, "Valid frame\n");
            info->pos += 2;
            ++info->stats.valids;
        }
        info->stats.bytes += sizeIn;
    }
    while (info->pos < sizeIn) {
        ch = in[info->pos++];
        switch (info->state) {
        case 0:  // sync search
            info->sync <<= 8;
            info->sync |= ch;
            if (info->sync == 0xcb28) {
                PDEBUG0_VERBOSE(2, bridgeVerbosity, "Sync found\n");
                ++info->stats.packets;
                info->received = 0;
                info->state = 1;
            }
            if (info->sync == 0) {		// Padding
                info->pos = 0;
                return 0;
            }
            break;
        case 1:  // header search
            ((char*)&info->header)[info->received++] = ch;
            if (info->received == sizeof(struct bridgeHdr)) {
                PDEBUG0_VERBOSE(2, bridgeVerbosity, "Header found\n");
                out = (unsigned char*)dataOut;
                info->received = 0;
                info->state = 2;
                crc = crc16(0xffff, &info->header, 4);
                crc ^= 0xffff;
                info->header.size = ntohs(info->header.size);
                info->header.seqNb = ntohs(info->header.seqNb);
                info->header.crc = ntohs(info->header.crc);
                PDEBUG4_VERBOSE(2, bridgeVerbosity,
                        " size\t%i\n seq\t%i\n crc\t0x%.4x (0x%.4x)\n",
                        info->header.size, info->header.seqNb,
                        info->header.crc, crc);
                if (crc != info->header.crc) {
                    PDEBUG0_VERBOSE(2, bridgeVerbosity, "CRC error\n");
                    ++info->stats.errors;
                    ++info->stats.crc;
                    info->state = 0;
                    if (info->pos < sizeof(struct bridgeHdr) + 2 + 2) {
                        info->pos = 2;
                    }
                } else {
                    if (!info->initSeq) {
                        info->lastSeq = info->header.seqNb;
                        info->initSeq = 1;
                    } else {
                        if (info->header.seqNb > info->lastSeq) {
                            diff = (info->header.seqNb - info->lastSeq) - 1;
                        } else {
                            diff = ((short)info->lastSeq -
                                    (short)info->header.seqNb) - 1;
                        }
                        info->stats.errors += diff;
                        info->stats.missings += diff;
                        info->lastSeq = info->header.seqNb;
                    }
                }
            }
            break;
        case 2:  // data
            out[info->received++] = ch;
            if (info->received == info->header.size) {
                PDEBUG0_VERBOSE(2, bridgeVerbosity, "data found\n");
                info->state = 0;
                return info->received;
            }
            if (info->received == sizeOut) {
                PDEBUG1_VERBOSE(1, bridgeVerbosity, "To much data: %i\n",
                        info->received);
                ++info->stats.errors;
                ++info->stats.overruns;
                info->sync = 0;
                info->state = 0;
                return -1;
            }
            break;
        case 3:  // Padding or sync
            if (ch == 0) {			// Padding
                info->pos = 0;
                return 0;
            }
            if (ch != 0xcb) {		// error
                info->sync = ch;
                info->state = 0;
            } else {
                info->state = 4;
            }
            break;
        case 4: // Low byte sync
            if (ch != 28) {			// error
                info->sync <<= 8;
                info->sync |= ch;
                info->state = 0;
            } else {
                info->state = 2;
            }
            break;
        }
    }
    info->pos = 0;
    return 0;
}


void dump(void* data, int size, FILE* stream)
{
    int i;
    fprintf(stream, "%i bytes\n", size);
    for (i = 0; i < size; ++i) {
        fprintf(stream, " 0x%.2x", ((unsigned char*)data)[i]);
        if (i % 8 == 7)
            fprintf(stream, "\n");
    }
    fprintf(stream, "\n");
}


#ifdef BRIDGE_TEST
#include <stdlib.h>


int test(const unsigned char* data)
{
    unsigned char bridgeSize = data[0];
    unsigned char nbInput = data[1];
    unsigned char nbBridge = 1;
    struct bridgeInfo info;

    int i, j;
    int index = 0;
    int max = 0;
    int nbBytes;

    unsigned char** inputData;
    unsigned char** bridgeData;
    unsigned char* outputData;

    inputData =  malloc(nbInput * 4);
    bridgeData = malloc(nbBridge * 4);
    for (i = 0; i < nbInput; ++i) {
        if (data[i + 2] > 0)
            inputData[i] = malloc(data[i + 2]);
        if (data[i + 2] > max) {
            max = data[i + 2];
        }
        for (j = 0; j < data[i + 2]; ++j) {
            inputData[i][j] = index++;
        }
    }
    bridgeData[0] = malloc(bridgeSize);
    memset(bridgeData[0], 0, bridgeSize);
    outputData = malloc(max);
    bridgeInitInfo(&info);

    // Write packets
    index = 0;
    while (1) {
        if (data[index + 2] == 0) {
            if (++index == nbInput)
                break;
        }
        while ((nbBytes = writePacket(inputData[index], data[index + 2],
                        bridgeData[nbBridge - 1], bridgeSize, &info))
                != 0) {
            if (++index == nbInput) {
                break;
            }
        }
        if (index == nbInput)
            break;
        // TODO check null
        bridgeData = realloc(bridgeData, (++nbBridge) * 4);
        bridgeData[nbBridge - 1] = malloc(bridgeSize);
        memset(bridgeData[nbBridge - 1], 0, bridgeSize);
    }
//    if (nbBytes != bridgeSize) {
        writePacket(NULL, 0, bridgeData[nbBridge - 1], bridgeSize, &info);
//    }

    // read packets
    index = 0;
    for (i = 0; i < nbBridge; ++i) {
        while ((nbBytes = getPacket(bridgeData[i], bridgeSize, outputData, max,
                        &info, 0)) != 0) {
            while (data[index + 2] == 0) {
                ++index;
            }
            if (nbBytes != data[index + 2]) {
                printf("FAILED\n");
                printf("Invalid size at bridge %i, data %i: %i != %i\n",
                        i, index, nbBytes, data[index + 2]);
                for (i = 0; i < nbInput; ++i) {
                    printf("Input %i: ", i);
                    dump(inputData[i], data[i + 2], stdout);
                }
                for (i = 0; i < nbBridge; ++i) {
                    printf("Bridge %i: ", i);
                    dump(bridgeData[i], bridgeSize, stdout);
                }
                printf("Output %i: ", index);
                dump(outputData, nbBytes, stdout);
                return -1;
            }
            if (memcmp(outputData, inputData[index], data[index + 2]) != 0) {
                printf("FAILED\n");
                printf("output != input\n");
                for (i = 0; i < nbInput; ++i) {
                    printf("Input %i: ", i);
                    dump(inputData[i], data[i + 2], stdout);
                }
                for (i = 0; i < nbBridge; ++i) {
                    printf("Bridge %i: ", i);
                    dump(bridgeData[i], bridgeSize, stdout);
                }
                printf("Output %i: ", index);
                dump(outputData, nbBytes, stdout);
            }
            ++index;
        }
    }

    printf("SUCCESS\n");

    for (i = 0; i < nbInput; ++i) {
        if (data[i + 2] > 0)
            free(inputData[i]);
    }
    free(inputData);
    free(outputData);
    for (i = 0; i < nbBridge; ++i) {
        free(bridgeData[i]);
    }
    free(bridgeData);

    return -1;
}


int main(int argc, char* argv[])
{
    int i;
    // test: bridgesize, nbinput [, input1, input2, ... ]
    const unsigned char complete[] = { 32, 1, 16 };
    const unsigned char split[] = { 32, 1, 48 };
    const unsigned char twice[] = {32, 2, 8, 4 };
    const unsigned char secondSplit[] = { 32, 2, 16, 16 };
    const unsigned char headerSplit[][4] = {
        { 32, 2, 23, 16 },
        { 32, 2, 22, 16 },
        { 32, 2, 21, 16 },
        { 32, 2, 20, 16 },
        { 32, 2, 19, 16 },
        { 32, 2, 18, 16 },
        { 32, 2, 17, 16 }
    };
    const unsigned char two[] = { 32, 3, 16, 0, 16 };
    const unsigned char doubleSplit[] = { 32, 2, 32, 32 };
    const unsigned char full[] = { 32, 2, 24, 12 };
    const unsigned char empty[] = { 32, 3, 0, 0, 5 };

#ifdef _WIN32
  #ifdef _DEBUG
    bridgeVerbosity = argc - 1;
  #endif // DEBUG
#else
  #ifdef DEBUG
    bridgeVerbosity = argc - 1;
  #endif // DEBUG
#endif // _WIN32

    printf("Complete:     ");
    test(complete);
    //	printStats(stdout);
    fflush(stdout);

    printf("split:        ");
    test(split);
    //	printStats(stdout);
    fflush(stdout);

    printf("twice:        ");
    test(twice);
    //	printStats(stdout);
    fflush(stdout);

    printf("second split: ");
    test(secondSplit);
    //	printStats(stdout);
    fflush(stdout);

    for (i = 0; i < sizeof(headerSplit) / sizeof(headerSplit[0]); ++i) {
        printf("headerSplit%i: ", i);
        test(headerSplit[i]);
        //	printStats(stdout);
        fflush(stdout);
    }

    printf("two:          ");
    test(two);
    //	printStats(stdout);
    fflush(stdout);

    printf("doubleSplit:  ");
    test(doubleSplit);
    //	printStats(stdout);
    fflush(stdout);

    printf("full:         ");
    test(full);
    //	printStats(stdout);
    fflush(stdout);

    printf("empty:        ");
    test(empty);
    //	printStats(stdout);
    fflush(stdout);

    return 0;
}

#endif // BRIDGE_TEST