forked from steve-o/openpgm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
packet_test.c
1165 lines (977 loc) · 31.5 KB
/
packet_test.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
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* vim:ts=8:sts=8:sw=4:noai:noexpandtab
*
* PGM packet formats, RFC 3208.
*
* Copyright (c) 2006-2011 Miru Limited.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <ctype.h>
#include <stdio.h>
#ifndef _WIN32
# include <sys/socket.h>
# include <netdb.h>
# include <netinet/in.h>
# include <arpa/inet.h>
#endif
#include <impl/i18n.h>
#include <impl/framework.h>
#include <impl/packet_test.h>
//#define PACKET_DEBUG
static bool pgm_print_spm (const struct pgm_header* const, const void*, const size_t);
static bool pgm_print_poll (const struct pgm_header* const, const void*, const size_t);
static bool pgm_print_polr (const struct pgm_header* const, const void*, const size_t);
static bool pgm_print_odata (const struct pgm_header* const, const void*, const size_t);
static bool pgm_print_rdata (const struct pgm_header* const, const void*, const size_t);
static bool pgm_print_nak (const struct pgm_header* const, const void*, const size_t);
static bool pgm_print_nnak (const struct pgm_header* const, const void*, const size_t);
static bool pgm_print_ncf (const struct pgm_header* const, const void*, const size_t);
static bool pgm_print_spmr (const struct pgm_header* const, const void*, const size_t);
static bool pgm_print_ack (const struct pgm_header* const, const void*, const size_t);
static ssize_t pgm_print_options (const void*, size_t);
PGM_GNUC_INTERNAL
bool
pgm_print_packet (
const void* data,
size_t len
)
{
/* pre-conditions */
pgm_assert (NULL != data);
pgm_assert (len > 0);
/* minimum size should be IP header plus PGM header */
if (len < (sizeof(struct pgm_ip) + sizeof(struct pgm_header)))
{
printf ("Packet size too small: %" PRIzu " bytes, expecting at least %" PRIzu " bytes.\n",
len, sizeof(struct pgm_ip) + sizeof(struct pgm_header));
return FALSE;
}
/* decode IP header */
const struct pgm_ip* ip = (const struct pgm_ip*)data;
if (ip->ip_v != 4) /* IP version, 4 or 6 */
{
puts ("not IP4 packet :/"); /* v6 not currently handled */
return FALSE;
}
printf ("IP ");
const size_t ip_header_length = ip->ip_hl * 4; /* IP header length in 32bit octets */
if (ip_header_length < sizeof(struct pgm_ip))
{
puts ("bad IP header length :(");
return FALSE;
}
size_t packet_length = pgm_ntohs(ip->ip_len); /* total packet length */
/* ip_len can equal packet_length - ip_header_length in FreeBSD/NetBSD
* Stevens/Fenner/Rudolph, Unix Network Programming Vol.1, p.739
*
* RFC3828 allows partial packets such that len < packet_length with UDP lite
*/
if (len == packet_length + ip_header_length) {
packet_length += ip_header_length;
}
if (len < packet_length) { /* redundant: often handled in kernel */
puts ("truncated IP packet");
return FALSE;
}
/* TCP Segmentation Offload (TSO) might have zero length here */
if (packet_length < ip_header_length) {
puts ("bad length :(");
return FALSE;
}
const uint16_t offset = pgm_ntohs(ip->ip_off);
/* 3 bits routing priority, 4 bits type of service: delay, throughput, reliability, cost */
printf ("(tos 0x%x", (int)ip->ip_tos);
switch (ip->ip_tos & 0x3)
{
case 1: printf (",ECT(1)"); break;
case 2: printf (",ECT(0)"); break;
case 3: printf (",CE"); break;
default: break;
}
/* time to live */
if (ip->ip_ttl >= 1) printf (", ttl %u", ip->ip_ttl);
/* fragmentation */
#define IP_RDF 0x8000
#define IP_DF 0x4000
#define IP_MF 0x2000
#define IP_OFFMASK 0x1fff
printf (", id %u, offset %u, flags [%s%s]",
pgm_ntohs(ip->ip_id),
(offset & 0x1fff) * 8,
((offset & IP_DF) ? "DF" : ""),
((offset & IP_MF) ? "+" : ""));
printf (", length %" PRIzu "", packet_length);
/* IP options */
if ((ip_header_length - sizeof(struct pgm_ip)) > 0) {
printf (", options (");
pgm_ipopt_print((const void*)(ip + 1), ip_header_length - sizeof(struct pgm_ip));
printf (" )");
}
/* packets that fail checksum will generally not be passed upstream except with rfc3828
*/
const uint16_t ip_sum = pgm_inet_checksum(data, (uint16_t)packet_length, 0);
if (ip_sum != 0) {
const uint16_t encoded_ip_sum = pgm_ntohs(ip->ip_sum);
printf (", bad cksum! %i", encoded_ip_sum);
}
printf (") ");
/* fragmentation offset, bit 0: 0, bit 1: do-not-fragment, bit 2: more-fragments */
if ((offset & 0x1fff) != 0) {
puts ("fragmented packet :/");
return FALSE;
}
/* PGM payload, header looks as follows:
*
* 0 1 2 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Source Port | Destination Port |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Type | Options | Checksum |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Global Source ID ... |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | ... Global Source ID | TSDU Length |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Type specific data ...
* +-+-+-+-+-+-+-+-+-+- ...
*/
const struct pgm_header* pgm_header = (const struct pgm_header*)((const char*)data + ip_header_length);
const size_t pgm_length = packet_length - ip_header_length;
if (pgm_length < sizeof(pgm_header)) {
puts ("bad packet size :(");
return FALSE;
}
printf ("%s.%s > ",
pgm_gethostbyaddr((const struct in_addr*)&ip->ip_src), pgm_udpport_string(pgm_header->pgm_sport));
printf ("%s.%s: PGM\n",
pgm_gethostbyaddr((const struct in_addr*)&ip->ip_dst), pgm_udpport_string(pgm_header->pgm_dport));
printf ("type: %s [%i] (version=%i, reserved=%i)\n"
"options: extensions=%s, network-significant=%s, parity packet=%s (variable size=%s)\n"
"global source id: %i.%i.%i.%i.%i.%i\n"
"tsdu length: %i\n",
/* packet type */ /* packet version */ /* reserved = 0x0 */
pgm_type_string(pgm_header->pgm_type & 0xf),
(pgm_header->pgm_type & 0xf), ((pgm_header->pgm_type & 0xc0) >> 6), ((pgm_header->pgm_type & 0x30) >> 4),
/* bit 0 set => one or more option extensions are present */
((pgm_header->pgm_options & (0x1 << 7)) ? "true" : "false"),
/* bit 1 set => one or more options are network-significant */
((pgm_header->pgm_options & (0x1 << 6)) ? "true" : "false"),
/* bit 7 set => parity packet (OPT_PARITY) */
((pgm_header->pgm_options & (0x1 << 0)) ? "true" : "false"),
/* bit 6 set => parity packet for variable packet sizes (OPT_VAR_PKTLEN) */
((pgm_header->pgm_options & (0x1 << 1)) ? "true" : "false"),
pgm_header->pgm_gsi[0], pgm_header->pgm_gsi[1], pgm_header->pgm_gsi[2], pgm_header->pgm_gsi[3], pgm_header->pgm_gsi[4], pgm_header->pgm_gsi[5],
pgm_ntohs(pgm_header->pgm_tsdu_length));
if (pgm_header->pgm_checksum)
{
#if 0
const uint16_t encoded_pgm_sum = pgm_header->pgm_checksum;
/* requires modification of data buffer */
pgm_header->pgm_checksum = 0;
const uint16_t pgm_sum = pgm_csum_fold (pgm_csum_partial((const char*)pgm_header, pgm_length, 0));
if (pgm_sum != encoded_pgm_sum) {
printf ("PGM checksum incorrect, packet %x calculated %x :(\n", encoded_pgm_sum, pgm_sum);
return FALSE;
}
#endif
} else {
puts ("No PGM checksum :O");
}
/* now decode PGM packet types */
const void* pgm_data = pgm_header + 1;
const size_t pgm_data_length = pgm_length - sizeof(pgm_header); /* can equal zero for SPMR's */
bool err = FALSE;
switch (pgm_header->pgm_type) {
case PGM_SPM: err = pgm_print_spm (pgm_header, pgm_data, pgm_data_length); break;
case PGM_POLL: err = pgm_print_poll (pgm_header, pgm_data, pgm_data_length); break;
case PGM_POLR: err = pgm_print_polr (pgm_header, pgm_data, pgm_data_length); break;
case PGM_ODATA: err = pgm_print_odata (pgm_header, pgm_data, pgm_data_length); break;
case PGM_RDATA: err = pgm_print_rdata (pgm_header, pgm_data, pgm_data_length); break;
case PGM_NAK: err = pgm_print_nak (pgm_header, pgm_data, pgm_data_length); break;
case PGM_NNAK: err = pgm_print_nnak (pgm_header, pgm_data, pgm_data_length); break;
case PGM_NCF: err = pgm_print_ncf (pgm_header, pgm_data, pgm_data_length); break;
case PGM_SPMR: err = pgm_print_spmr (pgm_header, pgm_data, pgm_data_length); break;
case PGM_ACK: err = pgm_print_ack (pgm_header, pgm_data, pgm_data_length); break;
default: puts ("unknown packet type :("); break;
}
return err;
}
/* 8.1. Source Path Messages (SPM)
*
* 0 1 2 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | SPM's Sequence Number |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Trailing Edge Sequence Number |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Leading Edge Sequence Number |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | NLA AFI | Reserved |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Path NLA ... |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-...-+-+
* | Option Extensions when present ... |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- ... -+-+-+-+-+-+-+-+-+-+-+-+-+-+
*
* NLA = Network Layer Address
* NLA AFI = NLA Address Family Indicator: rfc 1700 (ADDRESS FAMILY NUMBERS)
* => Path NLA = IP address of last network element
*/
#define PGM_MIN_SPM_SIZE ( sizeof(struct pgm_spm) )
static
bool
pgm_print_spm (
const struct pgm_header* const header,
const void* data,
const size_t len
)
{
/* pre-conditions */
pgm_assert (NULL != header);
pgm_assert (NULL != data);
pgm_assert (len > 0);
printf ("SPM: ");
if (len < PGM_MIN_SPM_SIZE) {
puts ("packet truncated :(");
return FALSE;
}
const struct pgm_spm * spm = (const struct pgm_spm *)data;
const struct pgm_spm6* spm6 = (const struct pgm_spm6*)data;
const uint16_t spm_nla_afi = pgm_ntohs (spm->spm_nla_afi);
printf ("sqn %" PRIu32 " trail %" PRIu32 "lu lead %" PRIu32 "lu nla-afi %u ",
pgm_ntohl(spm->spm_sqn),
pgm_ntohl(spm->spm_trail),
pgm_ntohl(spm->spm_lead),
spm_nla_afi); /* address family indicator */
char s[INET6_ADDRSTRLEN];
const void* pgm_opt;
size_t pgm_opt_len;
switch (spm_nla_afi) {
case AFI_IP:
pgm_inet_ntop (AF_INET, &spm->spm_nla, s, sizeof(s));
pgm_opt = (const uint8_t*)data + sizeof(struct pgm_spm);
pgm_opt_len = len - sizeof(struct pgm_spm);
break;
case AFI_IP6:
if (len < sizeof (struct pgm_spm6)) {
puts ("packet truncated :(");
return FALSE;
}
pgm_inet_ntop (AF_INET6, &spm6->spm6_nla, s, sizeof(s));
pgm_opt = (const uint8_t*)data + sizeof(struct pgm_spm6);
pgm_opt_len = len - sizeof(struct pgm_spm6);
break;
default:
printf ("unsupported afi");
return FALSE;
}
printf ("%s", s);
/* option extensions */
if (header->pgm_options & PGM_OPT_PRESENT &&
pgm_print_options (pgm_opt, pgm_opt_len) < 0 )
{
return FALSE;
}
printf ("\n");
return TRUE;
}
/* 14.7.1. Poll Request
*
* 0 1 2 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | POLL's Sequence Number |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | POLL's Round | POLL's Sub-type |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | NLA AFI | Reserved |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Path NLA ... |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-...-+-+
* | POLL's Back-off Interval |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Random String |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Matching Bit-Mask |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Option Extensions when present ... |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- ... -+-+-+-+-+-+-+-+-+-+-+-+-+-+
*
* Sent to ODATA multicast group with IP Router Alert option.
*/
#define PGM_MIN_POLL_SIZE ( sizeof(struct pgm_poll) )
static
bool
pgm_print_poll (
const struct pgm_header* const header,
const void* data,
const size_t len
)
{
/* pre-conditions */
pgm_assert (NULL != header);
pgm_assert (NULL != data);
pgm_assert (len > 0);
printf ("POLL: ");
if (len < PGM_MIN_POLL_SIZE) {
puts ("packet truncated :(");
return FALSE;
}
const struct pgm_poll * poll4 = (const struct pgm_poll *)data;
const struct pgm_poll6* poll6 = (const struct pgm_poll6*)data;
const uint16_t poll_nla_afi = pgm_ntohs (poll4->poll_nla_afi);
printf ("sqn %" PRIu32 " round %u sub-type %u nla-afi %u ",
pgm_ntohl(poll4->poll_sqn),
pgm_ntohs(poll4->poll_round),
pgm_ntohs(poll4->poll_s_type),
poll_nla_afi); /* address family indicator */
char s[INET6_ADDRSTRLEN];
const void* pgm_opt;
size_t pgm_opt_len;
switch (poll_nla_afi) {
case AFI_IP:
pgm_inet_ntop (AF_INET, &poll4->poll_nla, s, sizeof(s));
pgm_opt = (const uint8_t*)data + sizeof(struct pgm_poll);
pgm_opt_len = len - sizeof(struct pgm_poll);
printf ("%s", s);
/* back-off interval in microseconds */
printf (" bo_ivl %u", poll4->poll_bo_ivl);
/* random string */
printf (" rand [%c%c%c%c]",
isprint (poll4->poll_rand[0]) ? poll4->poll_rand[0] : '.',
isprint (poll4->poll_rand[1]) ? poll4->poll_rand[1] : '.',
isprint (poll4->poll_rand[2]) ? poll4->poll_rand[2] : '.',
isprint (poll4->poll_rand[3]) ? poll4->poll_rand[3] : '.' );
/* matching bit-mask */
printf (" mask 0x%x", poll4->poll_mask);
break;
case AFI_IP6:
if (len < sizeof (struct pgm_poll6)) {
puts ("packet truncated :(");
return FALSE;
}
pgm_inet_ntop (AF_INET6, &poll6->poll6_nla, s, sizeof (s));
pgm_opt = (const uint8_t*)data + sizeof(struct pgm_poll6);
pgm_opt_len = len - sizeof(struct pgm_poll6);
printf ("%s", s);
/* back-off interval in microseconds */
printf (" bo_ivl %u", poll6->poll6_bo_ivl);
/* random string */
printf (" rand [%c%c%c%c]",
isprint (poll6->poll6_rand[0]) ? poll6->poll6_rand[0] : '.',
isprint (poll6->poll6_rand[1]) ? poll6->poll6_rand[1] : '.',
isprint (poll6->poll6_rand[2]) ? poll6->poll6_rand[2] : '.',
isprint (poll6->poll6_rand[3]) ? poll6->poll6_rand[3] : '.' );
/* matching bit-mask */
printf (" mask 0x%x", poll6->poll6_mask);
break;
default:
printf ("unsupported afi");
return FALSE;
}
/* option extensions */
if (header->pgm_options & PGM_OPT_PRESENT &&
pgm_print_options (pgm_opt, pgm_opt_len) < 0 )
{
return FALSE;
}
printf ("\n");
return TRUE;
}
/* 14.7.2. Poll Response
*
* 0 1 2 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | POLR's Sequence Number |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | POLR's Round | reserved |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Option Extensions when present ... |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- ... -+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
static
bool
pgm_print_polr (
const struct pgm_header* const header,
const void* data,
const size_t len
)
{
/* pre-conditions */
pgm_assert (NULL != header);
pgm_assert (NULL != data);
pgm_assert (len > 0);
printf ("POLR: ");
if (len < sizeof(struct pgm_polr)) {
puts ("packet truncated :(");
return FALSE;
}
const struct pgm_polr* polr = (const struct pgm_polr*)data;
printf("sqn %" PRIu32 " round %u",
pgm_ntohl(polr->polr_sqn),
pgm_ntohs(polr->polr_round));
const void* pgm_opt = (const uint8_t*)data + sizeof(struct pgm_polr);
size_t pgm_opt_len = len - sizeof(struct pgm_polr);
/* option extensions */
if (header->pgm_options & PGM_OPT_PRESENT &&
pgm_print_options (pgm_opt, pgm_opt_len) < 0 )
{
return FALSE;
}
printf ("\n");
return TRUE;
}
/* 8.2. Data Packet
*
* 0 1 2 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Data Packet Sequence Number |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Trailing Edge Sequence Number |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Option Extensions when present ... |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- ... -+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Data ...
* +-+-+- ...
*/
static
bool
pgm_print_odata (
const struct pgm_header* const header,
const void* data,
const size_t len
)
{
/* pre-conditions */
pgm_assert (NULL != header);
pgm_assert (NULL != data);
pgm_assert (len > 0);
printf ("ODATA: ");
if (len < sizeof(struct pgm_data)) {
puts ("packet truncated :(");
return FALSE;
}
const struct pgm_data* odata = (const struct pgm_data*)data;
printf ("sqn %" PRIu32 " trail %" PRIu32 " [",
pgm_ntohl(odata->data_sqn),
pgm_ntohl(odata->data_trail));
/* option extensions */
const void* pgm_opt = (const uint8_t*)data + sizeof(struct pgm_data);
size_t pgm_opt_len = len - sizeof(struct pgm_data);
const char* payload = pgm_opt;
if (header->pgm_options & PGM_OPT_PRESENT) {
const ssize_t opt_len = pgm_print_options (pgm_opt, pgm_opt_len);
if (opt_len < 0)
return FALSE;
payload += opt_len;
}
/* data */
const char* end = payload + pgm_ntohs (header->pgm_tsdu_length);
while (payload < end) {
if (isprint (*payload))
putchar (*payload);
else
putchar ('.');
payload++;
}
printf ("]\n");
return TRUE;
}
/* 8.2. Repair Data
*/
static
bool
pgm_print_rdata (
const struct pgm_header* const header,
const void* data,
const size_t len
)
{
/* pre-conditions */
pgm_assert (NULL != header);
pgm_assert (NULL != data);
pgm_assert (len > 0);
printf ("RDATA: ");
if (len < sizeof(struct pgm_data)) {
puts ("packet truncated :(");
return FALSE;
}
const struct pgm_data* rdata = (const struct pgm_data*)data;
printf ("sqn %" PRIu32 " trail %" PRIu32 " [",
pgm_ntohl (rdata->data_sqn),
pgm_ntohl (rdata->data_trail));
/* option extensions */
const void* pgm_opt = (const uint8_t*)data + sizeof(struct pgm_data);
size_t pgm_opt_len = len - sizeof(struct pgm_data);
const char* payload = pgm_opt;
if (header->pgm_options & PGM_OPT_PRESENT) {
const ssize_t opt_len = pgm_print_options (pgm_opt, pgm_opt_len);
if (opt_len < 0)
return FALSE;
payload += opt_len;
}
/* data */
const char* end = payload + pgm_ntohs (header->pgm_tsdu_length);
while (payload < end) {
if (isprint (*payload))
putchar (*payload);
else
putchar ('.');
payload++;
}
printf ("]\n");
return TRUE;
}
/* 8.3. NAK
*
* Technically the AFI of the source and multicast group can be different
* but that would be very wibbly wobbly. One example is using a local DLR
* with a IPv4 address to reduce NAK cost for recovery on wide IPv6
* distribution.
*
* 0 1 2 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Requested Sequence Number |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | NLA AFI | Reserved |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Source NLA ... |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-...-+-+
* | NLA AFI | Reserved |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Multicast Group NLA ... |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-...-+-+
* | Option Extensions when present ...
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- ...
*/
#define PGM_MIN_NAK_SIZE ( sizeof(struct pgm_nak) )
static
bool
pgm_print_nak (
const struct pgm_header* const header,
const void* data,
const size_t len
)
{
/* pre-conditions */
pgm_assert (NULL != header);
pgm_assert (NULL != data);
pgm_assert (len > 0);
printf ("NAK: ");
if (len < PGM_MIN_NAK_SIZE) {
puts ("packet truncated :(");
return FALSE;
}
const struct pgm_nak * nak = (const struct pgm_nak *)data;
const struct pgm_nak6* nak6 = (const struct pgm_nak6*)data;
const uint16_t nak_src_nla_afi = pgm_ntohs (nak->nak_src_nla_afi);
printf ("sqn %" PRIu32 " src ",
pgm_ntohl(nak->nak_sqn));
char s[INET6_ADDRSTRLEN];
const void* pgm_opt;
size_t pgm_opt_len;
/* source nla */
switch (nak_src_nla_afi) {
case AFI_IP: {
const uint16_t nak_grp_nla_afi = pgm_ntohs (nak->nak_grp_nla_afi);
if (nak_src_nla_afi != nak_grp_nla_afi) {
puts ("different source & group afi very wibbly wobbly :(");
return FALSE;
}
pgm_inet_ntop (AF_INET, &nak->nak_src_nla, s, sizeof(s));
pgm_opt = (const uint8_t*)data + sizeof(struct pgm_nak);
pgm_opt_len = len - sizeof(struct pgm_nak);
printf ("%s grp ", s);
pgm_inet_ntop (AF_INET, &nak->nak_grp_nla, s, sizeof(s));
printf ("%s", s);
break;
}
case AFI_IP6: {
if (len < sizeof (struct pgm_nak6)) {
puts ("packet truncated :(");
return FALSE;
}
const uint16_t nak_grp_nla_afi = pgm_ntohs (nak6->nak6_grp_nla_afi);
if (nak_src_nla_afi != nak_grp_nla_afi) {
puts ("different source & group afi very wibbly wobbly :(");
return FALSE;
}
pgm_inet_ntop (AF_INET6, &nak6->nak6_src_nla, s, sizeof(s));
pgm_opt = (const uint8_t*)data + sizeof(struct pgm_nak6);
pgm_opt_len = len - sizeof(struct pgm_nak6);
printf ("%s grp ", s);
pgm_inet_ntop (AF_INET6, &nak6->nak6_grp_nla, s, sizeof(s));
printf ("%s", s);
break;
}
default:
puts ("unsupported afi");
return FALSE;
}
/* option extensions */
if (header->pgm_options & PGM_OPT_PRESENT &&
pgm_print_options (pgm_opt, pgm_opt_len) < 0 )
{
return FALSE;
}
printf ("\n");
return TRUE;
}
/* 8.3. N-NAK
*/
static
bool
pgm_print_nnak (
PGM_GNUC_UNUSED const struct pgm_header* const header,
PGM_GNUC_UNUSED const void* data,
const size_t len
)
{
/* pre-conditions */
pgm_assert (NULL != header);
pgm_assert (NULL != data);
pgm_assert (len > 0);
printf ("N-NAK: ");
if (len < sizeof(struct pgm_nak)) {
puts ("packet truncated :(");
return FALSE;
}
// struct pgm_nak* nnak = (struct pgm_nak*)data;
return TRUE;
}
/* 8.3. NCF
*/
static
bool
pgm_print_ncf (
PGM_GNUC_UNUSED const struct pgm_header* const header,
PGM_GNUC_UNUSED const void* data,
const size_t len
)
{
/* pre-conditions */
pgm_assert (NULL != header);
pgm_assert (NULL != data);
pgm_assert (len > 0);
printf ("NCF: ");
if (len < sizeof(struct pgm_nak)) {
puts ("packet truncated :(");
return FALSE;
}
// struct pgm_nak* ncf = (struct pgm_nak*)data;
return TRUE;
}
/* 13.6. SPM Request
*
* 0 1 2 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Option Extensions when present ...
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- ...
*/
static
bool
pgm_print_spmr (
const struct pgm_header* const header,
const void* data,
const size_t len
)
{
/* pre-conditions */
pgm_assert (NULL != header);
pgm_assert (NULL != data);
pgm_assert (len > 0);
printf ("SPMR: ");
/* option extensions */
if (header->pgm_options & PGM_OPT_PRESENT &&
pgm_print_options (data, len) < 0 )
{
return FALSE;
}
printf ("\n");
return TRUE;
}
/* PGMCC: ACK
*
* 0 1 2 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | RX_MAX |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Received Packet Bitmap |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Option Extensions when present ...
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- ...
*/
static
bool
pgm_print_ack (
const struct pgm_header* const header,
const void* data,
const size_t len
)
{
/* pre-conditions */
pgm_assert (NULL != header);
pgm_assert (NULL != data);
pgm_assert (len > 0);
printf ("ACK: ");
const struct pgm_ack* ack = (const struct pgm_ack*)data;
char bitmap[33];
for (unsigned i = 31; i; i--)
bitmap[i] = (ack->ack_bitmap & (1 << i)) ? '1' : '0';
bitmap[32] = '\0';
printf ("rx_max %" PRIu32 " bitmap [%s] ",
pgm_ntohl(ack->ack_rx_max), bitmap);
/* option extensions */
if (header->pgm_options & PGM_OPT_PRESENT &&
pgm_print_options (data, len) < 0 )
{
return FALSE;
}
printf ("\n");
return TRUE;
}
/* Parse PGM options fields, alters contents of packet.
*
* returns -1 on failure, or total length in octets of the option fields
*/
static
ssize_t
pgm_print_options (
const void* data,
size_t len
)
{
/* pre-conditions */
pgm_assert (NULL != data);
pgm_assert (len > 0);
printf (" OPTIONS:");
if (len < sizeof(struct pgm_opt_length)) {
puts (" packet truncated :(");
return -1;
}
const struct pgm_opt_length* opt_len = (const struct pgm_opt_length*)data;
if (opt_len->opt_length != sizeof(struct pgm_opt_length)) {
printf (" bad opt_length length %u\n", (unsigned)opt_len->opt_length);
return -1;
}
uint16_t opt_total_length = pgm_ntohs (opt_len->opt_total_length);
printf (" total len %u ", opt_total_length);
if (opt_total_length < (sizeof(struct pgm_opt_length) + sizeof(struct pgm_opt_header)) ||
opt_total_length > len)
{
puts ("bad total length");
return -1;
}
/* total length includes opt_length option */
opt_total_length -= sizeof(struct pgm_opt_length);
const struct pgm_opt_header* opt_header = (const struct pgm_opt_header*)(opt_len + 1);
/* iterate through options (max 16) */
unsigned count = 16;
while (opt_total_length && count)
{
if (opt_total_length < sizeof(struct pgm_opt_header) ||
opt_header->opt_length > opt_total_length)
{
puts ("short on option data :o");
return -1;
}
if (opt_header->opt_type & PGM_OPT_END) {
printf ("OPT_END+");
}
switch (opt_header->opt_type & PGM_OPT_MASK) {
case PGM_OPT_FRAGMENT:
printf ("OPT_FRAGMENT ");
break;
case PGM_OPT_NAK_LIST:
printf ("OPT_NAK_LIST ");
break;
case PGM_OPT_JOIN:
printf ("OPT_JOIN ");
break;
case PGM_OPT_REDIRECT:
printf ("OPT_REDIRECT ");
break;
case PGM_OPT_SYN:
printf ("OPT_SYN ");
break;
case PGM_OPT_FIN:
printf ("OPT_FIN ");
break;
case PGM_OPT_RST:
printf ("OPT_RST ");
break;
case PGM_OPT_PARITY_PRM:
printf ("OPT_PARITY_PRM ");
break;
case PGM_OPT_CURR_TGSIZE:
printf ("OPT_CURR_TGSIZE ");
break;
case PGM_OPT_CR:
printf ("OPT_CR ");
break;
case PGM_OPT_CRQST:
printf ("OPT_CRQST ");
break;
case PGM_OPT_PGMCC_DATA:
printf ("OPT_PGMCC_DATA ");
break;