aboutsummaryrefslogtreecommitdiffstats
path: root/packet-sctp.c
blob: 2d9a3612ae9f2f0a350fdb4a07536a71e3ab6257 (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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
/* packet-sctp.c
 * Routines for Stream Control Transmission Protocol dissection
 * Copyright 2000, Michael Txen <Michael.Tuexen@icn.siemens.de>
 *
 * $Id: packet-sctp.c,v 1.23 2001/12/08 06:41:42 guy Exp $
 *
 * Ethereal - Network traffic analyzer
 * By Gerald Combs <gerald@ethereal.com>
 * Copyright 1998 Gerald Combs
 *
 * Copied from README.developer
 * 
 * This program 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 2
 * of the License, or (at your option) any later version.
 * 
 * This program 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 this program; 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 <stdio.h>
#include <stdlib.h>


#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif

#ifdef HAVE_NETINET_IN_H
# include <netinet/in.h>
#endif

#include <string.h>
#include <glib.h>

#ifdef NEED_SNPRINTF_H
# include "snprintf.h"
#endif

#include "packet.h"
#include "ipproto.h"

/* Initialize the protocol and registered fields */
static int proto_sctp = -1;
static int hf_sctp_source_port      = -1;
static int hf_sctp_destination_port = -1;
static int hf_sctp_verification_tag = -1;
static int hf_sctp_checksum         = -1;

static int hf_sctp_chunk_type       = -1;
static int hf_sctp_chunk_flags      = -1;
static int hf_sctp_chunk_length     = -1;

static int hf_sctp_init_chunk_initiate_tag   = -1;
static int hf_sctp_init_chunk_adv_rec_window_credit = -1;
static int hf_sctp_init_chunk_number_of_outbound_streams = -1;
static int hf_sctp_init_chunk_number_of_inbound_streams  = -1;
static int hf_sctp_init_chunk_initial_tsn    = -1;

static int hf_sctp_cumulative_tsn_ack = -1;

static int hf_sctp_data_chunk_tsn = -1;
static int hf_sctp_data_chunk_stream_id = -1;
static int hf_sctp_data_chunk_stream_seq_number = -1;
static int hf_sctp_data_chunk_payload_proto_id = -1;

static int hf_sctp_data_chunk_e_bit = -1;
static int hf_sctp_data_chunk_b_bit = -1;
static int hf_sctp_data_chunk_u_bit = -1;

static int hf_sctp_sack_chunk_cumulative_tsn_ack = -1;
static int hf_sctp_sack_chunk_adv_rec_window_credit = -1;
static int hf_sctp_sack_chunk_number_of_gap_blocks = -1;
static int hf_sctp_sack_chunk_number_of_dup_tsns = -1;
static int hf_sctp_sack_chunk_gap_block_start = -1;
static int hf_sctp_sack_chunk_gap_block_end = -1;
static int hf_sctp_sack_chunk_duplicate_tsn = -1;

static int hf_sctp_shutdown_chunk_cumulative_tsn_ack = -1;

static int hf_sctp_cwr_chunk_lowest_tsn = -1;

static int hf_sctp_ecne_chunk_lowest_tsn = -1;

static int hf_sctp_shutdown_complete_chunk_t_bit = -1;

static int hf_sctp_chunk_parameter_type = -1;
static int hf_sctp_chunk_parameter_length = -1;
static int hf_sctp_parameter_ipv4_address = -1;
static int hf_sctp_parameter_ipv6_address = -1;
static int hf_sctp_parameter_cookie_preservative_increment = -1;
static int hf_sctp_parameter_hostname_hostname = -1;
static int hf_sctp_supported_address_types_parameter = -1;

static int hf_sctp_cause_code = -1;
static int hf_sctp_cause_length = -1;
static int hf_sctp_cause_stream_identifier = -1;

static int hf_sctp_cause_number_of_missing_parameters = -1;
static int hf_sctp_cause_missing_parameter_type = -1;

static int hf_sctp_cause_measure_of_staleness = -1;

static int hf_sctp_cause_tsn = -1;

static dissector_table_t sctp_port_dissector_table;
static dissector_table_t sctp_ppi_dissector_table;

/* Initialize the subtree pointers */
static gint ett_sctp = -1;
static gint ett_sctp_chunk = -1;
static gint ett_sctp_chunk_parameter = -1;
static gint ett_sctp_chunk_cause = -1;
static gint ett_sctp_data_chunk_flags = -1;
static gint ett_sctp_sack_chunk_gap_block = -1;
static gint ett_sctp_supported_address_types_parameter = -1;
static gint ett_sctp_unrecognized_parameter_parameter = -1;

#define SCTP_DATA_CHUNK_ID               0
#define SCTP_INIT_CHUNK_ID               1
#define SCTP_INIT_ACK_CHUNK_ID           2
#define SCTP_SACK_CHUNK_ID               3
#define SCTP_HEARTBEAT_CHUNK_ID          4
#define SCTP_HEARTBEAT_ACK_CHUNK_ID      5
#define SCTP_ABORT_CHUNK_ID              6
#define SCTP_SHUTDOWN_CHUNK_ID           7
#define SCTP_SHUTDOWN_ACK_CHUNK_ID       8
#define SCTP_ERROR_CHUNK_ID              9
#define SCTP_COOKIE_ECHO_CHUNK_ID       10
#define SCTP_COOKIE_ACK_CHUNK_ID        11
#define SCTP_ECNE_CHUNK_ID              12
#define SCTP_CWR_CHUNK_ID               13
#define SCTP_SHUTDOWN_COMPLETE_CHUNK_ID 14
#define SCTP_IETF_EXT                  255

static const value_string sctp_chunk_type_values[] = {
  { SCTP_DATA_CHUNK_ID,              "DATA" },
  { SCTP_INIT_CHUNK_ID,              "INIT" },
  { SCTP_INIT_ACK_CHUNK_ID,          "INIT_ACK" },
  { SCTP_SACK_CHUNK_ID,              "SACK" },
  { SCTP_HEARTBEAT_CHUNK_ID,         "HEARTBEAT" },
  { SCTP_HEARTBEAT_ACK_CHUNK_ID,     "HEARTBEAT_ACK" },
  { SCTP_ABORT_CHUNK_ID,             "ABORT" },
  { SCTP_SHUTDOWN_CHUNK_ID,          "SHUTDOWN" },
  { SCTP_SHUTDOWN_ACK_CHUNK_ID,      "SHUTDOWN_ACK" },
  { SCTP_ERROR_CHUNK_ID,             "ERROR" },
  { SCTP_COOKIE_ECHO_CHUNK_ID,       "COOKIE_ECHO" },
  { SCTP_COOKIE_ACK_CHUNK_ID,        "COOKIE_ACK" },
  { SCTP_ECNE_CHUNK_ID,              "ECNE" },
  { SCTP_CWR_CHUNK_ID,               "CWR" },
  { SCTP_SHUTDOWN_COMPLETE_CHUNK_ID, "SHUTDOWN_COMPLETE" },
  { SCTP_IETF_EXT,                   "IETF_EXTENSION" },
  { 0,                               NULL } };

#define HEARTBEAT_INFO_PARAMETER_ID          0x0001
#define IPV4ADDRESS_PARAMETER_ID             0x0005
#define IPV6ADDRESS_PARAMETER_ID             0x0006
#define STATE_COOKIE_PARAMETER_ID            0x0007
#define UNREC_PARA_PARAMETER_ID              0x0008
#define COOKIE_PRESERVATIVE_PARAMETER_ID     0x0009
#define HOSTNAME_ADDRESS_PARAMETER_ID        0x000b
#define SUPPORTED_ADDRESS_TYPES_PARAMETER_ID 0x000c
#define ECN_PARAMETER_ID                     0x8000

static const value_string sctp_parameter_identifier_values[] = {
  { HEARTBEAT_INFO_PARAMETER_ID,          "Heartbeat info" },
  { IPV4ADDRESS_PARAMETER_ID,             "IPv4 address" },
  { IPV6ADDRESS_PARAMETER_ID,             "IPv6 address" },
  { STATE_COOKIE_PARAMETER_ID,            "State cookie" },
  { UNREC_PARA_PARAMETER_ID,              "Unrecognized parameters" },
  { COOKIE_PRESERVATIVE_PARAMETER_ID,     "Cookie preservative" },
  { HOSTNAME_ADDRESS_PARAMETER_ID,        "Hostname address" },
  { SUPPORTED_ADDRESS_TYPES_PARAMETER_ID, "Supported address types" },
  { ECN_PARAMETER_ID,                     "ECN" },
  { 0,                                    NULL } };

#define PARAMETER_TYPE_LENGTH            2
#define PARAMETER_LENGTH_LENGTH          2
#define PARAMETER_HEADER_LENGTH          (PARAMETER_TYPE_LENGTH + PARAMETER_LENGTH_LENGTH)

#define PARAMETER_HEADER_OFFSET          0
#define PARAMETER_TYPE_OFFSET            PARAMETER_HEADER_OFFSET
#define PARAMETER_LENGTH_OFFSET          (PARAMETER_TYPE_OFFSET + PARAMETER_TYPE_LENGTH)
#define PARAMETER_VALUE_OFFSET           (PARAMETER_LENGTH_OFFSET + PARAMETER_LENGTH_LENGTH)

#define HEARTBEAT_INFO_PARAMETER_INFO_OFFSET PARAMETER_VALUE_OFFSET
#define HEARTBEAT_INFO_PARAMETER_HEADER_LENGTH PARAMETER_HEADER_LENGTH

#define IPV4_ADDRESS_LENGTH              4
#define IPV6_ADDRESS_LENGTH              16

#define STATE_COOKIE_PARAMETER_HEADER_LENGTH   PARAMETER_HEADER_LENGTH
#define STATE_COOKIE_PARAMETER_COOKIE_OFFSET   PARAMETER_VALUE_OFFSET

#define COOKIE_PRESERVATIVE_PARAMETER_INCR_OFFSET PARAMETER_VALUE_OFFSET
#define COOKIE_PRESERVATIVE_PARAMETER_INCR_LENGTH 4
#define SUPPORTED_ADDRESS_TYPE_PARAMETER_ADDRESS_TYPE_LENGTH 2

#define CAUSE_CODE_LENGTH            2
#define CAUSE_LENGTH_LENGTH          2
#define CAUSE_HEADER_LENGTH          (CAUSE_CODE_LENGTH + CAUSE_LENGTH_LENGTH)

#define CAUSE_HEADER_OFFSET          0
#define CAUSE_CODE_OFFSET            CAUSE_HEADER_OFFSET
#define CAUSE_LENGTH_OFFSET          (CAUSE_CODE_OFFSET + CAUSE_CODE_LENGTH)
#define CAUSE_INFO_OFFSET            (CAUSE_LENGTH_OFFSET + CAUSE_LENGTH_LENGTH)

#define CAUSE_STREAM_IDENTIFIER_LENGTH 2
#define CAUSE_RESERVED_LENGTH 2
#define CAUSE_STREAM_IDENTIFIER_OFFSET CAUSE_INFO_OFFSET
#define CAUSE_RESERVED_OFFSET          (CAUSE_STREAM_IDENTIFIER_OFFSET + CAUSE_STREAM_IDENTIFIER_LENGTH)

#define CAUSE_NUMBER_OF_MISSING_PARAMETERS_LENGTH 4
#define CAUSE_MISSING_PARAMETER_TYPE_LENGTH       2

#define CAUSE_NUMBER_OF_MISSING_PARAMETERS_OFFSET CAUSE_INFO_OFFSET
#define CAUSE_FIRST_MISSING_PARAMETER_TYPE_OFFSET (CAUSE_NUMBER_OF_MISSING_PARAMETERS_OFFSET + \
                                                   CAUSE_NUMBER_OF_MISSING_PARAMETERS_LENGTH )

#define CAUSE_MEASURE_OF_STALENESS_LENGTH 4
#define CAUSE_MEASURE_OF_STALENESS_OFFSET CAUSE_INFO_OFFSET

#define CAUSE_TSN_LENGTH 4
#define CAUSE_TSN_OFFSET CAUSE_INFO_OFFSET

#define INVALID_STREAM_IDENTIFIER              0x01
#define MISSING_MANDATORY_PARAMETERS           0x02
#define STALE_COOKIE_ERROR                     0x03
#define OUT_OF_RESOURCE                        0x04
#define UNRESOLVABLE_ADDRESS                   0x05
#define UNRECOGNIZED_CHUNK_TYPE                0x06
#define INVALID_MANDATORY_PARAMETER            0x07
#define UNRECOGNIZED_PARAMETERS                0x08
#define NO_USER_DATA                           0x09
#define COOKIE_RECEIVED_WHILE_SHUTTING_DOWN    0x0a

static const value_string sctp_cause_code_values[] = {
  { INVALID_STREAM_IDENTIFIER,           "Invalid stream identifier" },
  { MISSING_MANDATORY_PARAMETERS,        "Missing mandator parameter" },
  { STALE_COOKIE_ERROR,                  "Stale cookie error" },
  { OUT_OF_RESOURCE,                     "Out of resource" },
  { UNRESOLVABLE_ADDRESS,                "Unresolvable address" },
  { UNRECOGNIZED_CHUNK_TYPE,             "Unrecognized chunk type " },
  { INVALID_MANDATORY_PARAMETER,         "Invalid mandatory parameter" },
  { UNRECOGNIZED_PARAMETERS,             "Unrecognized parameters" },
  { NO_USER_DATA,                        "No user data" },
  { COOKIE_RECEIVED_WHILE_SHUTTING_DOWN, "Cookie received while shutting down" },
  { 0,                           NULL } };

#define NOT_SPECIFIED_PROTOCOL_ID  0
#define IUA_PAYLOAD_PROTOCOL_ID    1
#define M2UA_PAYLOAD_PROTOCOL_ID   2
#define M3UA_PAYLOAD_PROTOCOL_ID   3
#define SUA_PAYLOAD_PROTOCOL_ID    4
#define M2PA_PAYLOAD_PROTOCOL_ID   5
#define V5UA_PAYLOAD_PROTOCOL_ID   6

static const value_string sctp_payload_proto_id_values[] = {
  { NOT_SPECIFIED_PROTOCOL_ID,           "not specified" },
  { IUA_PAYLOAD_PROTOCOL_ID,             "IUA" },
  { M2UA_PAYLOAD_PROTOCOL_ID,            "M2UA" },
  { M3UA_PAYLOAD_PROTOCOL_ID,            "M3UA" },
  { SUA_PAYLOAD_PROTOCOL_ID,             "SUA" },
  { M2PA_PAYLOAD_PROTOCOL_ID,            "M2PA" },
  { V5UA_PAYLOAD_PROTOCOL_ID,            "V5UA" },
  { 0,                                   NULL } };

/* The structure of the common header is described by the following constants */
#define SOURCE_PORT_LENGTH      2
#define DESTINATION_PORT_LENGTH 2
#define VERIFICATION_TAG_LENGTH 4
#define CHECKSUM_LENGTH         4
#define COMMON_HEADER_LENGTH    (SOURCE_PORT_LENGTH + \
				 DESTINATION_PORT_LENGTH + \
				 VERIFICATION_TAG_LENGTH + \
				 CHECKSUM_LENGTH)
#define SOURCE_PORT_OFFSET      0
#define DESTINATION_PORT_OFFSET (SOURCE_PORT_OFFSET + SOURCE_PORT_LENGTH)
#define VERIFICATION_TAG_OFFSET (DESTINATION_PORT_OFFSET + DESTINATION_PORT_LENGTH)
#define CHECKSUM_OFFSET         (VERIFICATION_TAG_OFFSET + VERIFICATION_TAG_LENGTH)

/* The structure of the chunk header is described by the following constants */
#define CHUNK_TYPE_LENGTH             1
#define CHUNK_FLAGS_LENGTH            1
#define CHUNK_LENGTH_LENGTH           2
#define CHUNK_HEADER_LENGTH           (CHUNK_TYPE_LENGTH + \
                                       CHUNK_FLAGS_LENGTH + \
                                       CHUNK_LENGTH_LENGTH)
#define CHUNK_HEADER_OFFSET           0
#define CHUNK_TYPE_OFFSET             CHUNK_HEADER_OFFSET
#define CHUNK_FLAGS_OFFSET            (CHUNK_TYPE_OFFSET + CHUNK_TYPE_LENGTH)
#define CHUNK_LENGTH_OFFSET           (CHUNK_FLAGS_OFFSET + CHUNK_FLAGS_LENGTH)
#define CHUNK_VALUE_OFFSET            (CHUNK_LENGTH_OFFSET + CHUNK_LENGTH_LENGTH)

/* The following constants describe the structure of DATA chunks */
#define DATA_CHUNK_TSN_LENGTH         4
#define DATA_CHUNK_STREAM_ID_LENGTH   2
#define DATA_CHUNK_STREAM_SEQ_NUMBER_LENGTH 2
#define DATA_CHUNK_PAYLOAD_PROTOCOL_ID_LENGTH 4

#define DATA_CHUNK_TSN_OFFSET         (CHUNK_VALUE_OFFSET + 0)
#define DATA_CHUNK_STREAM_ID_OFFSET   (DATA_CHUNK_TSN_OFFSET + DATA_CHUNK_TSN_LENGTH)
#define DATA_CHUNK_STREAM_SEQ_NUMBER_OFFSET (DATA_CHUNK_STREAM_ID_OFFSET + \
                                             DATA_CHUNK_STREAM_SEQ_NUMBER_LENGTH)
#define DATA_CHUNK_PAYLOAD_PROTOCOL_ID_OFFSET (DATA_CHUNK_STREAM_SEQ_NUMBER_OFFSET + \
                                               DATA_CHUNK_STREAM_SEQ_NUMBER_LENGTH)
#define DATA_CHUNK_PAYLOAD_OFFSET     (DATA_CHUNK_PAYLOAD_PROTOCOL_ID_OFFSET + \
                                       DATA_CHUNK_PAYLOAD_PROTOCOL_ID_LENGTH)

#define DATA_CHUNK_HEADER_LENGTH      (CHUNK_HEADER_LENGTH + \
                                       DATA_CHUNK_TSN_LENGTH + \
                                       DATA_CHUNK_STREAM_ID_LENGTH + \
                                       DATA_CHUNK_STREAM_SEQ_NUMBER_LENGTH + \
                                       DATA_CHUNK_PAYLOAD_PROTOCOL_ID_LENGTH)

#define SCTP_DATA_CHUNK_E_BIT 0x01
#define SCTP_DATA_CHUNK_B_BIT 0x02
#define SCTP_DATA_CHUNK_U_BIT 0x04

#define INIT_CHUNK_INITIATE_TAG_LENGTH               4
#define INIT_CHUNK_ADV_REC_WINDOW_CREDIT_LENGTH      4
#define INIT_CHUNK_NUMBER_OF_OUTBOUND_STREAMS_LENGTH 2
#define INIT_CHUNK_NUMBER_OF_INBOUND_STREAMS_LENGTH  2
#define INIT_CHUNK_INITIAL_TSN_LENGTH                4

#define INIT_CHUNK_INITIATE_TAG_OFFSET               CHUNK_VALUE_OFFSET
#define INIT_CHUNK_ADV_REC_WINDOW_CREDIT_OFFSET      (INIT_CHUNK_INITIATE_TAG_OFFSET + \
                                                      INIT_CHUNK_INITIATE_TAG_LENGTH )
#define INIT_CHUNK_NUMBER_OF_OUTBOUND_STREAMS_OFFSET (INIT_CHUNK_ADV_REC_WINDOW_CREDIT_OFFSET + \
                                                      INIT_CHUNK_ADV_REC_WINDOW_CREDIT_LENGTH )
#define INIT_CHUNK_NUMBER_OF_INBOUND_STREAMS_OFFSET  (INIT_CHUNK_NUMBER_OF_OUTBOUND_STREAMS_OFFSET + \
                                                      INIT_CHUNK_NUMBER_OF_OUTBOUND_STREAMS_LENGTH )
#define INIT_CHUNK_INITIAL_TSN_OFFSET                (INIT_CHUNK_NUMBER_OF_INBOUND_STREAMS_OFFSET + \
                                                      INIT_CHUNK_NUMBER_OF_INBOUND_STREAMS_LENGTH )
#define INIT_CHUNK_VARIABLE_LENGTH_PARAMETER_OFFSET  (INIT_CHUNK_INITIAL_TSN_OFFSET + \
                                                      INIT_CHUNK_INITIAL_TSN_LENGTH )


#define SACK_CHUNK_CUMULATIVE_TSN_ACK_LENGTH    4
#define SACK_CHUNK_ADV_REC_WINDOW_CREDIT_LENGTH 4
#define SACK_CHUNK_NUMBER_OF_GAP_BLOCKS_LENGTH  2
#define SACK_CHUNK_NUMBER_OF_DUP_TSNS_LENGTH    2
#define SACK_CHUNK_GAP_BLOCK_LENGTH             4
#define SACK_CHUNK_GAP_BLOCK_START_LENGTH       2
#define SACK_CHUNK_GAP_BLOCK_END_LENGTH         2
#define SACK_CHUNK_DUP_TSN_LENGTH               4

#define SACK_CHUNK_CUMULATIVE_TSN_ACK_OFFSET (CHUNK_VALUE_OFFSET + 0)
#define SACK_CHUNK_ADV_REC_WINDOW_CREDIT_OFFSET (SACK_CHUNK_CUMULATIVE_TSN_ACK_OFFSET + \
                                                 SACK_CHUNK_CUMULATIVE_TSN_ACK_LENGTH)
#define SACK_CHUNK_NUMBER_OF_GAP_BLOCKS_OFFSET (SACK_CHUNK_ADV_REC_WINDOW_CREDIT_OFFSET + \
                                                SACK_CHUNK_ADV_REC_WINDOW_CREDIT_LENGTH)
#define SACK_CHUNK_NUMBER_OF_DUP_TSNS_OFFSET (SACK_CHUNK_NUMBER_OF_GAP_BLOCKS_OFFSET + \
                                              SACK_CHUNK_NUMBER_OF_GAP_BLOCKS_LENGTH)
#define SACK_CHUNK_GAP_BLOCK_OFFSET (SACK_CHUNK_NUMBER_OF_DUP_TSNS_OFFSET + \
                                     SACK_CHUNK_NUMBER_OF_DUP_TSNS_LENGTH)

#define HEARTBEAT_CHUNK_INFO_OFFSET CHUNK_VALUE_OFFSET

#define SHUTDOWN_CHUNK_CUMULATIVE_TSN_ACK_OFFSET CHUNK_VALUE_OFFSET
#define SHUTDOWN_CHUNK_CUMULATIVE_TSN_ACK_LENGTH 4
		
#define ABORT_CHUNK_FIRST_ERROR_CAUSE_OFFSET 4     
#define ERROR_CHUNK_FIRST_ERROR_CAUSE_OFFSET 4     

#define COOKIE_ECHO_CHUNK_COOKIE_OFFSET CHUNK_VALUE_OFFSET

#define ECNE_CHUNK_LOWEST_TSN_OFFSET CHUNK_VALUE_OFFSET
#define ECNE_CHUNK_LOWEST_TSN_LENGTH 4

#define CWR_CHUNK_LOWEST_TSN_OFFSET CHUNK_VALUE_OFFSET
#define CWR_CHUNK_LOWEST_TSN_LENGTH 4
		     
#define SCTP_SHUTDOWN_COMPLETE_CHUNK_T_BIT 0x01

static const true_false_string sctp_data_chunk_e_bit_value = {
  "Last segment",
  "Not the last segment"
};

static const true_false_string sctp_data_chunk_b_bit_value = {
  "First segment",
  "Subsequent segment"
};

static const true_false_string sctp_data_chunk_u_bit_value = {
  "Unordered delivery",
  "Ordered deliviery"
};

static const true_false_string sctp_shutdown_complete_chunk_t_bit_value = {
  "No TCB destroyed",
  "TCB destroyed"
};

/* adler32.c -- compute the Adler-32 checksum of a data stream
 * Copyright (C) 1995-1996 Mark Adler
 * For conditions of distribution and use, see copyright notice in zlib.h
 * available, e.g. from  http://www.cdrom.com/pub/infozip/zlib/
 *
 * It was modified for the use in this dissector.
 */

#define BASE 65521L /* largest prime smaller than 65536      */
#define NMAX 5540   /* NMAX is the largest n - 12 such that  */
		    /* 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */

#define DO1(buf,i)  {s1 += buf[i]; s2 += s1;}
#define DO2(buf,i)  DO1(buf,i); DO1(buf,i+1);
#define DO4(buf,i)  DO2(buf,i); DO2(buf,i+2);
#define DO8(buf,i)  DO4(buf,i); DO4(buf,i+4);
#define DO16(buf)   DO8(buf,0); DO8(buf,8);

static unsigned int
sctp_adler_32(const unsigned char* buf,
	      unsigned int len)
{
    unsigned int s1 = 1L;
    unsigned int s2 = 0L;
    int k;
    
    /* handle the first 8 bytes of the datagram */
    DO8(buf,0);
    buf += SOURCE_PORT_LENGTH + 
           DESTINATION_PORT_LENGTH + 
           VERIFICATION_TAG_LENGTH;
    
    /* handle four 0 bytes as checksum */
    s2  += CHECKSUM_LENGTH * s1;
    buf += CHECKSUM_LENGTH;

    /* now we have 12 bytes handled */
    len -= COMMON_HEADER_LENGTH;

    /* handle the rest of the datagram */
    while (len > 0) {
        k = len < NMAX ? len : NMAX;
        len -= k;
        while (k >= 16) {
            DO16(buf);
            buf += 16;
            k -= 16;
        }
        if (k != 0) do {
            s1 += *buf++;
            s2 += s1;
        } while (--k);
        s1 %= BASE;
        s2 %= BASE;
    }
    return (s2 << 16) | s1;
}

static char 
*sctp_checksum_state(tvbuff_t *tvb, guint orig_checksum)
{
  guint length;
  
  length = tvb_length(tvb);
  if (orig_checksum == sctp_adler_32(tvb_get_ptr(tvb, 0, length), length))
    return "correct";
  else
    return "incorrect";
}

static guint 
nr_of_padding_bytes (guint length)
{
  guint remainder;

  remainder = length % 4;

  if (remainder == 0)
    return 0;
  else
    return 4 - remainder;
}

/* 
 * TLV parameter stuff for INIT and INIT-ACK chunks
 */

static void
dissect_parameter(tvbuff_t *, proto_tree *);

static gboolean
dissect_sctp_chunk(tvbuff_t *, packet_info *, proto_tree *, proto_tree *);

static void 
dissect_tlv_parameter_list(tvbuff_t *parameter_list_tvb, proto_tree *tree)
{
  guint offset, length, padding_length, total_length;
  tvbuff_t *parameter_tvb;

  offset = 0;
  while(tvb_length_remaining(parameter_list_tvb, offset)) {
    length         = tvb_get_ntohs(parameter_list_tvb, offset + PARAMETER_LENGTH_OFFSET);
    padding_length = nr_of_padding_bytes(length);
    total_length   = length + padding_length;
    /* create a tvb for the chunk including the padding bytes */
    parameter_tvb    = tvb_new_subset(parameter_list_tvb, offset, total_length, total_length);
    dissect_parameter(parameter_tvb, tree); 
    /* get rid of the handled parameter */
    offset += total_length;
  }
}

static void
dissect_heartbeat_info_parameter(tvbuff_t *parameter_tvb, proto_tree *parameter_tree, proto_item *parameter_item)
{
  guint16 length, heartbeat_info_length;
  
  length = tvb_get_ntohs(parameter_tvb, PARAMETER_LENGTH_OFFSET);
  
  heartbeat_info_length = length - HEARTBEAT_INFO_PARAMETER_HEADER_LENGTH;

  proto_tree_add_text(parameter_tree, parameter_tvb, HEARTBEAT_INFO_PARAMETER_INFO_OFFSET, heartbeat_info_length,
		      "Heartbeat info (%u byte%s)",
		      heartbeat_info_length, plurality(heartbeat_info_length, "", "s"));

  proto_item_set_text(parameter_item, "Heartbeat info parameter with %u byte%s of info",
		      heartbeat_info_length, plurality(heartbeat_info_length, "", "s"));
}

static void
dissect_ipv4_parameter(tvbuff_t *parameter_tvb, proto_tree *parameter_tree, proto_item *parameter_item)
{
  guint32 ipv4_address;

  tvb_memcpy(parameter_tvb, (guint8 *)&ipv4_address, PARAMETER_VALUE_OFFSET, IPV4_ADDRESS_LENGTH); 
  proto_tree_add_ipv4(parameter_tree, hf_sctp_parameter_ipv4_address,
		      parameter_tvb, PARAMETER_VALUE_OFFSET, IPV4_ADDRESS_LENGTH,
		      ipv4_address);  
  proto_item_set_text(parameter_item, "IPV4 address parameter");
}

static void
dissect_ipv6_parameter(tvbuff_t *parameter_tvb, proto_tree *parameter_tree, proto_item *parameter_item)
{
  proto_tree_add_ipv6(parameter_tree, hf_sctp_parameter_ipv6_address,
		      parameter_tvb, PARAMETER_VALUE_OFFSET, IPV6_ADDRESS_LENGTH,
		      tvb_get_ptr(parameter_tvb, PARAMETER_VALUE_OFFSET, IPV6_ADDRESS_LENGTH));
  
  proto_item_set_text(parameter_item, "IPV6 address parameter");
}

static void
dissect_state_cookie_parameter(tvbuff_t *parameter_tvb, proto_tree *parameter_tree, proto_item *parameter_item)
{
  guint16 length, state_cookie_length;
  
  length = tvb_get_ntohs(parameter_tvb, PARAMETER_LENGTH_OFFSET);
  
  state_cookie_length = length - STATE_COOKIE_PARAMETER_HEADER_LENGTH;

  proto_tree_add_text(parameter_tree, parameter_tvb, STATE_COOKIE_PARAMETER_COOKIE_OFFSET, state_cookie_length,
		      "State cookie (%u byte%s)",
		      state_cookie_length, plurality(state_cookie_length, "", "s"));

  proto_item_set_text(parameter_item, "State Cookie Parameter with %u byte%s cookie",
		      state_cookie_length, plurality(state_cookie_length, "", "s"));
}

static void
dissect_unrecognized_parameters_parameter(tvbuff_t *parameter_tvb, proto_tree *parameter_tree, proto_item *parameter_item)
{
  guint16 length, padding_length, parameter_value_length;
  tvbuff_t *unrecognized_parameters_tvb;

  length = tvb_get_ntohs(parameter_tvb, PARAMETER_LENGTH_OFFSET);
  padding_length = nr_of_padding_bytes(length);

  parameter_value_length = length - PARAMETER_HEADER_LENGTH + padding_length;

  unrecognized_parameters_tvb = tvb_new_subset(parameter_tvb, PARAMETER_VALUE_OFFSET, 
					       parameter_value_length, parameter_value_length);
  dissect_tlv_parameter_list(unrecognized_parameters_tvb, parameter_tree);
   
  proto_item_set_text(parameter_item, "Unrecognized parameter of type");
}

static void
dissect_cookie_preservative_parameter(tvbuff_t *parameter_tvb, proto_tree *parameter_tree, proto_item *parameter_item)
{
  guint32 increment;

  increment =  tvb_get_ntohl(parameter_tvb, COOKIE_PRESERVATIVE_PARAMETER_INCR_OFFSET);
  
  proto_tree_add_uint(parameter_tree, hf_sctp_parameter_cookie_preservative_increment, parameter_tvb, 
		      COOKIE_PRESERVATIVE_PARAMETER_INCR_OFFSET, 
		      COOKIE_PRESERVATIVE_PARAMETER_INCR_LENGTH,
		      increment);
  
  proto_item_set_text(parameter_item, "Cookie preservative parameter requesting for a %u msec increment",
		      increment);
}

static void
dissect_hostname_parameter(tvbuff_t *parameter_tvb, proto_tree *parameter_tree, proto_item *parameter_item)
{
  guint16  length, hostname_length;
  char *hostname;

  length = tvb_get_ntohs(parameter_tvb, PARAMETER_LENGTH_OFFSET);
  
  hostname_length = length - PARAMETER_HEADER_LENGTH;
  hostname = (char *)tvb_get_ptr(parameter_tvb, PARAMETER_VALUE_OFFSET, hostname_length);
  proto_tree_add_string(parameter_tree, hf_sctp_parameter_hostname_hostname, parameter_tvb,
			PARAMETER_VALUE_OFFSET, hostname_length,
			hostname);

  proto_item_set_text(parameter_item, "Hostname parameter");
}

static void
dissect_supported_address_types_parameter(tvbuff_t *parameter_tvb, proto_tree *parameter_tree, proto_item *parameter_item)
{
  guint16 length, address_type, number_of_address_types, address_type_number, list_of_address_types_length ;
  guint offset;
  proto_item *address_list_item;
  proto_tree *address_list_tree;

  length = tvb_get_ntohs(parameter_tvb, PARAMETER_LENGTH_OFFSET);
  list_of_address_types_length = length - PARAMETER_HEADER_LENGTH;
  number_of_address_types = list_of_address_types_length / SUPPORTED_ADDRESS_TYPE_PARAMETER_ADDRESS_TYPE_LENGTH;

  address_list_item = proto_tree_add_text(parameter_tree, parameter_tvb, PARAMETER_VALUE_OFFSET, list_of_address_types_length,
					  "Supported Address Types (%u address type%s)",
					  number_of_address_types, plurality(number_of_address_types, "", "s"));
  address_list_tree = proto_item_add_subtree(address_list_item, ett_sctp_supported_address_types_parameter);
 
  offset = PARAMETER_VALUE_OFFSET;
  for(address_type_number = 1; address_type_number <= number_of_address_types; address_type_number++) {
    address_type = tvb_get_ntohs(parameter_tvb, offset);
    proto_tree_add_uint_format(address_list_tree, hf_sctp_supported_address_types_parameter,
			       parameter_tvb, offset, SUPPORTED_ADDRESS_TYPE_PARAMETER_ADDRESS_TYPE_LENGTH,
			       address_type, "Supported address type: 0x%04x (%s)",
			       address_type, val_to_str(address_type, sctp_parameter_identifier_values, "unknown"));
    offset += SUPPORTED_ADDRESS_TYPE_PARAMETER_ADDRESS_TYPE_LENGTH;
  };

  proto_item_set_text(parameter_item, "Supported address types parameter reporting %u address type%s",
		      number_of_address_types, plurality(number_of_address_types, "", "s"));
}

static void
dissect_ecn_parameter(tvbuff_t *parameter_tvb, proto_tree *parameter_tree, proto_item *parameter_item)
{
   proto_item_set_text(parameter_item, "ECN parameter");
}

static void
dissect_unknown_parameter(tvbuff_t *parameter_tvb, proto_tree *parameter_tree, proto_item *parameter_item)
{
  guint16 type, length, parameter_value_length;
  
  type   = tvb_get_ntohs(parameter_tvb, PARAMETER_TYPE_OFFSET);
  length = tvb_get_ntohs(parameter_tvb, PARAMETER_LENGTH_OFFSET);
  
  parameter_value_length = length - PARAMETER_HEADER_LENGTH;

  proto_tree_add_text(parameter_tree, parameter_tvb, PARAMETER_VALUE_OFFSET, parameter_value_length,
		      "Parameter value (%u byte%s)",
		      parameter_value_length, plurality(parameter_value_length, "", "s"));

  proto_item_set_text(parameter_item, "Parameter of type %u and %u byte%s value",
		      type, parameter_value_length, plurality(parameter_value_length, "", "s"));
}

static void
dissect_parameter(tvbuff_t *parameter_tvb, proto_tree *chunk_tree)
{
  guint16 type, length, padding_length, total_length;
  proto_item *parameter_item;
  proto_tree *parameter_tree;

  type           = tvb_get_ntohs(parameter_tvb, PARAMETER_TYPE_OFFSET);
  length         = tvb_get_ntohs(parameter_tvb, PARAMETER_LENGTH_OFFSET);
  padding_length = nr_of_padding_bytes(length);
  total_length   = length + padding_length;
   
  parameter_item = proto_tree_add_text(chunk_tree, parameter_tvb,
	PARAMETER_HEADER_OFFSET, total_length, "%s parameter",
	val_to_str(type, sctp_parameter_identifier_values, "Unknown"));
  parameter_tree = proto_item_add_subtree(parameter_item, ett_sctp_chunk_parameter);
 
  proto_tree_add_uint(parameter_tree, hf_sctp_chunk_parameter_type, 
		      parameter_tvb, PARAMETER_TYPE_OFFSET, PARAMETER_TYPE_LENGTH,
		      type);
  proto_tree_add_uint(parameter_tree, hf_sctp_chunk_parameter_length, 
		      parameter_tvb, PARAMETER_LENGTH_OFFSET, PARAMETER_LENGTH_LENGTH,
		      length);
 
  switch(type) {
  case HEARTBEAT_INFO_PARAMETER_ID:
    dissect_heartbeat_info_parameter(parameter_tvb, parameter_tree, parameter_item);
    break;
  case IPV4ADDRESS_PARAMETER_ID:
    dissect_ipv4_parameter(parameter_tvb, parameter_tree, parameter_item);
    break;
  case IPV6ADDRESS_PARAMETER_ID:
    dissect_ipv6_parameter(parameter_tvb, parameter_tree, parameter_item);
    break;
  case STATE_COOKIE_PARAMETER_ID:
    dissect_state_cookie_parameter(parameter_tvb, parameter_tree, parameter_item);
    break;
  case UNREC_PARA_PARAMETER_ID:
    dissect_unrecognized_parameters_parameter(parameter_tvb, parameter_tree, parameter_item);
    break;
  case COOKIE_PRESERVATIVE_PARAMETER_ID:
    dissect_cookie_preservative_parameter(parameter_tvb, parameter_tree, parameter_item);
    break;
  case HOSTNAME_ADDRESS_PARAMETER_ID:
    dissect_hostname_parameter(parameter_tvb, parameter_tree, parameter_item);
    break;
  case SUPPORTED_ADDRESS_TYPES_PARAMETER_ID:
    dissect_supported_address_types_parameter(parameter_tvb, parameter_tree, parameter_item);
    break;
  case ECN_PARAMETER_ID:
    dissect_ecn_parameter(parameter_tvb, parameter_tree, parameter_item);
    break;
  default:
    dissect_unknown_parameter(parameter_tvb, parameter_tree, parameter_item);
    break;
  };
  if ((padding_length > 0) && (type != UNREC_PARA_PARAMETER_ID))
    proto_tree_add_text(parameter_tree, parameter_tvb, PARAMETER_HEADER_OFFSET + length, padding_length,
			"Padding: %u byte%s",
			padding_length, plurality(padding_length, "", "s"));
}

/*
 * Code to handle error causes for ABORT and ERROR chunks
 */
static void
dissect_invalid_stream_identifier_cause(tvbuff_t *cause_tvb, proto_tree *cause_tree, proto_item *cause_item)
{
  guint16 stream_identifier;

  stream_identifier   = tvb_get_ntohs(cause_tvb, CAUSE_STREAM_IDENTIFIER_OFFSET);
  proto_tree_add_uint(cause_tree, hf_sctp_cause_stream_identifier, 
		      cause_tvb, CAUSE_STREAM_IDENTIFIER_OFFSET, CAUSE_STREAM_IDENTIFIER_LENGTH,
		      stream_identifier);
  proto_tree_add_text(cause_tree, cause_tvb, CAUSE_RESERVED_OFFSET, CAUSE_RESERVED_LENGTH,
		      "Reserved (2 bytes)");

  proto_item_set_text(cause_item, "Error cause reporting invalid stream identifier %u",
		      stream_identifier);
}

static void
dissect_missing_mandatory_parameters_cause(tvbuff_t *cause_tvb, proto_tree *cause_tree, proto_item *cause_item)
{
  guint32 number_of_missing_parameters, missing_parameter_number;
  guint16 parameter_type;
  guint   offset;

  number_of_missing_parameters = tvb_get_ntohl(cause_tvb, CAUSE_NUMBER_OF_MISSING_PARAMETERS_OFFSET);
  proto_tree_add_uint(cause_tree, hf_sctp_cause_number_of_missing_parameters, 
		      cause_tvb, CAUSE_NUMBER_OF_MISSING_PARAMETERS_OFFSET, CAUSE_NUMBER_OF_MISSING_PARAMETERS_LENGTH,
		      number_of_missing_parameters);
  offset = CAUSE_FIRST_MISSING_PARAMETER_TYPE_OFFSET;
  for(missing_parameter_number = 1; missing_parameter_number <= number_of_missing_parameters; missing_parameter_number++) {
    parameter_type = tvb_get_ntohs(cause_tvb, offset);
    proto_tree_add_uint(cause_tree, hf_sctp_cause_missing_parameter_type,
			cause_tvb, offset, CAUSE_MISSING_PARAMETER_TYPE_LENGTH,
			parameter_type);
    offset +=  CAUSE_MISSING_PARAMETER_TYPE_LENGTH;
  };

  proto_item_set_text(cause_item, "Error cause reporting %u missing mandatory parameter%s",
		      number_of_missing_parameters, plurality(number_of_missing_parameters, "", "s") );
}

static void
dissect_stale_cookie_error_cause(tvbuff_t *cause_tvb, proto_tree *cause_tree, proto_item *cause_item)
{
  guint32 measure_of_staleness;

  measure_of_staleness =  tvb_get_ntohl(cause_tvb, CAUSE_MEASURE_OF_STALENESS_OFFSET);
  
  proto_tree_add_uint(cause_tree, hf_sctp_cause_measure_of_staleness, cause_tvb, 
		      CAUSE_MEASURE_OF_STALENESS_OFFSET, 
		      CAUSE_MEASURE_OF_STALENESS_LENGTH,
		      measure_of_staleness);
  
  proto_item_set_text(cause_item, "Error cause reporting a measure of staleness of %u usec",
		      measure_of_staleness);
}

static void
dissect_out_of_resource_cause(tvbuff_t *cause_tvb, proto_tree *cause_tree, proto_item *cause_item)
{
  proto_item_set_text(cause_item, "Error cause reporting lack of resources");
}

static void
dissect_unresolvable_address_cause(tvbuff_t *cause_tvb, proto_tree *cause_tree, proto_item *cause_item)
{
  guint16 code, length, parameter_length, parameter_type;
  tvbuff_t *parameter_tvb;

  code   = tvb_get_ntohs(cause_tvb, CAUSE_CODE_OFFSET);
  length = tvb_get_ntohs(cause_tvb, CAUSE_LENGTH_OFFSET);
  
  parameter_length = length - CAUSE_HEADER_LENGTH;
  parameter_tvb = tvb_new_subset(cause_tvb, CAUSE_INFO_OFFSET, 
				 parameter_length, parameter_length);

  dissect_parameter(parameter_tvb, cause_tree);
  parameter_type = tvb_get_ntohs(parameter_tvb, PARAMETER_TYPE_OFFSET);
 
  proto_item_set_text(cause_item, "Error cause reporting unresolvable address of type 0x%04x (%s)",
		      parameter_type, val_to_str(parameter_type, sctp_parameter_identifier_values, "unknown") );
}

static void
dissect_unrecognized_chunk_type_cause(tvbuff_t *cause_tvb,  packet_info *pinfo, 
				      proto_tree *cause_tree, proto_item *cause_item)
{
  guint16 length, chunk_length;
  guint8 unrecognized_type;
  tvbuff_t *unrecognized_chunk_tvb;

  length = tvb_get_ntohs(cause_tvb, CAUSE_LENGTH_OFFSET);
  
  chunk_length = length - CAUSE_HEADER_LENGTH;

  unrecognized_chunk_tvb = tvb_new_subset(cause_tvb, CAUSE_INFO_OFFSET, 
					  chunk_length, chunk_length);
  dissect_sctp_chunk(unrecognized_chunk_tvb, pinfo, cause_tree,cause_tree);

  unrecognized_type   = tvb_get_guint8(unrecognized_chunk_tvb, CHUNK_TYPE_OFFSET);
 
  proto_item_set_text(cause_item, "Error cause reporting unrecognized chunk of type %u (%s)",
		      unrecognized_type,
		      val_to_str(unrecognized_type, sctp_chunk_type_values, "unknown"));
}

static void
dissect_invalid_mandatory_parameter_cause(tvbuff_t *cause_tvb, proto_tree *cause_tree, proto_item *cause_item)
{
  proto_item_set_text(cause_item, "Error cause reporting an invalid mandatory parameter");
}

static void
dissect_unrecognized_parameters_cause(tvbuff_t *cause_tvb, proto_tree *cause_tree, proto_item *cause_item)
{
  guint16 length, padding_length, cause_info_length;
  tvbuff_t *unrecognized_parameters_tvb;

  length = tvb_get_ntohs(cause_tvb, CAUSE_LENGTH_OFFSET);
  padding_length = nr_of_padding_bytes(length);

  cause_info_length = length - CAUSE_HEADER_LENGTH + padding_length;

  unrecognized_parameters_tvb = tvb_new_subset(cause_tvb, CAUSE_INFO_OFFSET, 
					       cause_info_length, cause_info_length);
  dissect_tlv_parameter_list(unrecognized_parameters_tvb, cause_tree);
 
  proto_item_set_text(cause_item, "Error cause reporting unrecognized parameters");
}

static void
dissect_no_user_data_cause(tvbuff_t *cause_tvb, proto_tree *cause_tree, proto_item *cause_item)
{
  guint32 tsn;

  tsn = tvb_get_ntohl(cause_tvb, CAUSE_TSN_OFFSET);
  proto_tree_add_uint(cause_tree, hf_sctp_cause_tsn, cause_tvb, 
		      CAUSE_TSN_OFFSET, 
		      CAUSE_TSN_LENGTH,
		      tsn);

  proto_item_set_text(cause_item, "Error cause reporting data chunk with TSN %u contains no data",
		      tsn);
}

static void
dissect_cookie_received_while_shutting_down_cause(tvbuff_t *cause_tvb, proto_tree *cause_tree, proto_item *cause_item)
{
  proto_item_set_text(cause_item, "Error cause reporting cookie reception while shutting down");
}

static void
dissect_unknown_cause(tvbuff_t *cause_tvb, proto_tree *cause_tree, proto_item *cause_item)
{
  guint16 code, length, cause_info_length;

  code   = tvb_get_ntohs(cause_tvb, CAUSE_CODE_OFFSET);
  length = tvb_get_ntohs(cause_tvb, CAUSE_LENGTH_OFFSET);
  
  cause_info_length = length - CAUSE_HEADER_LENGTH;

  proto_tree_add_text(cause_tree, cause_tvb, CAUSE_INFO_OFFSET, cause_info_length,
		      "Cause specific information (%u byte%s)",
		      cause_info_length, plurality(cause_info_length, "", "s"));

  proto_item_set_text(cause_item, "Error cause with code %u and %u byte%s information",
		      code, cause_info_length, plurality(cause_info_length, "", "s"));
}

static void
dissect_error_cause(tvbuff_t *cause_tvb, packet_info *pinfo, proto_tree *chunk_tree)
{
  guint16 code, length, padding_length, total_length;
  proto_item *cause_item;
  proto_tree *cause_tree;

  code           = tvb_get_ntohs(cause_tvb, CAUSE_CODE_OFFSET);
  length         = tvb_get_ntohs(cause_tvb, CAUSE_LENGTH_OFFSET);
  padding_length = nr_of_padding_bytes(length);
  total_length   = length + padding_length;

  cause_item = proto_tree_add_text(chunk_tree, cause_tvb,
				   CAUSE_HEADER_OFFSET, total_length,
				   "BAD ERROR CAUSE");
  cause_tree = proto_item_add_subtree(cause_item, ett_sctp_chunk_cause);
 
  proto_tree_add_uint(cause_tree, hf_sctp_cause_code, 
		      cause_tvb, CAUSE_CODE_OFFSET, CAUSE_CODE_LENGTH,
		      code);
  proto_tree_add_uint(cause_tree, hf_sctp_cause_length, 
		      cause_tvb, CAUSE_LENGTH_OFFSET, CAUSE_LENGTH_LENGTH,
		      length);
 
  switch(code) {
  case INVALID_STREAM_IDENTIFIER:
    dissect_invalid_stream_identifier_cause(cause_tvb, cause_tree, cause_item);
    break;
  case MISSING_MANDATORY_PARAMETERS:
    dissect_missing_mandatory_parameters_cause(cause_tvb, cause_tree, cause_item);
    break;
  case STALE_COOKIE_ERROR:
    dissect_stale_cookie_error_cause(cause_tvb, cause_tree, cause_item);
    break;
  case OUT_OF_RESOURCE:
    dissect_out_of_resource_cause(cause_tvb, cause_tree, cause_item);
    break;
  case UNRESOLVABLE_ADDRESS:
    dissect_unresolvable_address_cause(cause_tvb, cause_tree, cause_item);
    break;
  case UNRECOGNIZED_CHUNK_TYPE:
    dissect_unrecognized_chunk_type_cause(cause_tvb, pinfo, cause_tree, cause_item);
    break;
  case INVALID_MANDATORY_PARAMETER:
    dissect_invalid_mandatory_parameter_cause(cause_tvb, cause_tree, cause_item);
    break;
  case UNRECOGNIZED_PARAMETERS:
    dissect_unrecognized_parameters_cause(cause_tvb, cause_tree, cause_item);
    break;
  case NO_USER_DATA:
    dissect_no_user_data_cause(cause_tvb, cause_tree, cause_item);
    break;
  case COOKIE_RECEIVED_WHILE_SHUTTING_DOWN:
    dissect_cookie_received_while_shutting_down_cause(cause_tvb, cause_tree, cause_item);
    break;
  default:
    dissect_unknown_cause(cause_tvb, cause_tree, cause_item);
    break;
  };
  if ((padding_length > 0) && (code != UNRECOGNIZED_PARAMETERS))
    proto_tree_add_text(cause_tree, cause_tvb, CAUSE_HEADER_OFFSET + length, padding_length,
			"Padding: %u byte%s",
			padding_length, plurality(padding_length, "", "s"));
}

/*
 * Code to actually dissect the packets 
*/

static gboolean
dissect_payload(tvbuff_t *payload_tvb, packet_info *pinfo, proto_tree *tree,
		proto_tree *chunk_tree, guint32 ppi, guint16 payload_length, guint16 padding_length)
{
  /* do lookup with the subdissector table */
  if (dissector_try_port (sctp_ppi_dissector_table, ppi,  payload_tvb, pinfo, tree) ||
      dissector_try_port(sctp_port_dissector_table, pinfo->srcport,  payload_tvb, pinfo, tree) ||
      dissector_try_port(sctp_port_dissector_table, pinfo->destport, payload_tvb, pinfo, tree)){
    return TRUE;
  }
  else {
    if (check_col(pinfo->fd, COL_INFO))
      col_append_str(pinfo->fd, COL_INFO, "DATA ");
    proto_tree_add_text(chunk_tree, payload_tvb, 0, payload_length,
			"Payload (%u byte%s)",
			payload_length, plurality(payload_length, "", "s")); 
    if (padding_length > 0)
      proto_tree_add_text(chunk_tree, payload_tvb, payload_length, padding_length,
			  "Padding: %u byte%s",
			  padding_length, plurality(padding_length, "", "s"));
    return FALSE;
  }
}

static gboolean
dissect_data_chunk(tvbuff_t *chunk_tvb, packet_info *pinfo, proto_tree *tree,
		   proto_tree *chunk_tree, proto_item *chunk_item, proto_item *flags_item)
{ 
  guint8  flags;
  guint16 length, total_payload_length, payload_length, padding_length, stream_id, stream_seq_number;
  guint32 tsn, payload_proto_id;
  proto_tree *flag_tree;
  tvbuff_t *payload_tvb;
   
  length            = tvb_get_ntohs(chunk_tvb, CHUNK_LENGTH_OFFSET);
  payload_length       = length - DATA_CHUNK_HEADER_LENGTH;
  padding_length       = nr_of_padding_bytes(length);
  total_payload_length = payload_length + padding_length;
  payload_tvb          = tvb_new_subset(chunk_tvb, DATA_CHUNK_PAYLOAD_OFFSET,
					  total_payload_length, total_payload_length);
  payload_proto_id     = tvb_get_ntohl(chunk_tvb, DATA_CHUNK_PAYLOAD_PROTOCOL_ID_OFFSET);

  if (chunk_tree) {
    flags             = tvb_get_guint8(chunk_tvb, CHUNK_FLAGS_OFFSET);
     
    flag_tree = proto_item_add_subtree(flags_item, ett_sctp_data_chunk_flags);
    proto_tree_add_boolean(flag_tree, hf_sctp_data_chunk_e_bit, chunk_tvb,
			   CHUNK_FLAGS_OFFSET, CHUNK_FLAGS_LENGTH, flags);
    proto_tree_add_boolean(flag_tree, hf_sctp_data_chunk_b_bit, chunk_tvb,
			   CHUNK_FLAGS_OFFSET, CHUNK_FLAGS_LENGTH, flags);
    proto_tree_add_boolean(flag_tree, hf_sctp_data_chunk_u_bit, chunk_tvb,
			   CHUNK_FLAGS_OFFSET, CHUNK_FLAGS_LENGTH, flags);
    
    tsn               = tvb_get_ntohl(chunk_tvb, DATA_CHUNK_TSN_OFFSET);
    stream_id         = tvb_get_ntohs(chunk_tvb, DATA_CHUNK_STREAM_ID_OFFSET);
    stream_seq_number = tvb_get_ntohs(chunk_tvb, DATA_CHUNK_STREAM_SEQ_NUMBER_OFFSET);
         
    proto_tree_add_uint(chunk_tree, hf_sctp_data_chunk_tsn, 
			chunk_tvb,
			DATA_CHUNK_TSN_OFFSET, DATA_CHUNK_TSN_LENGTH,
			tsn);
    proto_tree_add_uint(chunk_tree, hf_sctp_data_chunk_stream_id, 
			chunk_tvb, 
			DATA_CHUNK_STREAM_ID_OFFSET, DATA_CHUNK_STREAM_ID_LENGTH,
			stream_id);
    proto_tree_add_uint(chunk_tree, hf_sctp_data_chunk_stream_seq_number, 
			chunk_tvb, 
			DATA_CHUNK_STREAM_SEQ_NUMBER_OFFSET, DATA_CHUNK_STREAM_SEQ_NUMBER_LENGTH,
			stream_seq_number);
    proto_tree_add_uint(chunk_tree, hf_sctp_data_chunk_payload_proto_id, 
			chunk_tvb, DATA_CHUNK_PAYLOAD_PROTOCOL_ID_OFFSET, DATA_CHUNK_PAYLOAD_PROTOCOL_ID_LENGTH,
			payload_proto_id);
    proto_item_set_text(chunk_item, "DATA chunk with TSN %u (%u:%u) containing %u byte%s of payload",
			tsn, stream_id, stream_seq_number, 
			payload_length, plurality(payload_length, "", "s"));
  };   
  return dissect_payload(payload_tvb, pinfo, tree, chunk_tree, payload_proto_id, payload_length, padding_length); 
}

static void
dissect_init_chunk(tvbuff_t *chunk_tvb,  packet_info *pinfo, proto_tree *tree,
		   proto_tree *chunk_tree, proto_item *chunk_item, proto_item *flags_item)
{ 
  guint32 initiate_tag, adv_rec_window_credit, initial_tsn;
  guint16 number_of_inbound_streams, number_of_outbound_streams;
  guint8  type;
  tvbuff_t *parameter_list_tvb;

  type                       = tvb_get_guint8(chunk_tvb, CHUNK_TYPE_OFFSET);
  
  if (check_col(pinfo->fd, COL_INFO)) {
    if (type == SCTP_INIT_CHUNK_ID) {
      col_append_str(pinfo->fd, COL_INFO, "INIT ");
    } else {
      col_append_str(pinfo->fd, COL_INFO, "INIT_ACK ");
    };
  };
  
  if (chunk_tree) {
    initiate_tag               = tvb_get_ntohl(chunk_tvb, INIT_CHUNK_INITIATE_TAG_OFFSET);
    adv_rec_window_credit      = tvb_get_ntohl(chunk_tvb, INIT_CHUNK_ADV_REC_WINDOW_CREDIT_OFFSET);
    number_of_inbound_streams  = tvb_get_ntohs(chunk_tvb, INIT_CHUNK_NUMBER_OF_INBOUND_STREAMS_OFFSET);
    number_of_outbound_streams = tvb_get_ntohs(chunk_tvb, INIT_CHUNK_NUMBER_OF_OUTBOUND_STREAMS_OFFSET);
    initial_tsn                = tvb_get_ntohl(chunk_tvb, INIT_CHUNK_INITIAL_TSN_OFFSET);
    
    /* handle fixed parameters */
    proto_tree_add_uint(chunk_tree, hf_sctp_init_chunk_initiate_tag, 
			chunk_tvb,
			INIT_CHUNK_INITIATE_TAG_OFFSET, INIT_CHUNK_INITIATE_TAG_LENGTH,
			initiate_tag);
    proto_tree_add_uint(chunk_tree, hf_sctp_init_chunk_adv_rec_window_credit, 
			chunk_tvb,
			INIT_CHUNK_ADV_REC_WINDOW_CREDIT_OFFSET, INIT_CHUNK_ADV_REC_WINDOW_CREDIT_LENGTH,
			adv_rec_window_credit);
    proto_tree_add_uint(chunk_tree, hf_sctp_init_chunk_number_of_outbound_streams, 
			chunk_tvb,
			INIT_CHUNK_NUMBER_OF_OUTBOUND_STREAMS_OFFSET, INIT_CHUNK_NUMBER_OF_OUTBOUND_STREAMS_LENGTH,
			number_of_outbound_streams);
    proto_tree_add_uint(chunk_tree, hf_sctp_init_chunk_number_of_inbound_streams, 
			chunk_tvb,
			INIT_CHUNK_NUMBER_OF_INBOUND_STREAMS_OFFSET, INIT_CHUNK_NUMBER_OF_INBOUND_STREAMS_LENGTH,
			number_of_inbound_streams);
    proto_tree_add_uint(chunk_tree, hf_sctp_init_chunk_initial_tsn, 
			chunk_tvb,
			INIT_CHUNK_INITIAL_TSN_OFFSET, INIT_CHUNK_INITIAL_TSN_LENGTH,
			initial_tsn);
    
    /* handle variable paramters */
    parameter_list_tvb = tvb_new_subset(chunk_tvb, INIT_CHUNK_VARIABLE_LENGTH_PARAMETER_OFFSET, -1, -1);
    dissect_tlv_parameter_list(parameter_list_tvb, chunk_tree);
    
    proto_item_set_text(chunk_item, 
			"%s chunk requesting for %u outbound stream%s and accepting up to %u inbound stream%s",
			val_to_str(type, sctp_chunk_type_values, "unknown"),
			number_of_outbound_streams, plurality(number_of_outbound_streams, "", "s"),
			number_of_inbound_streams, plurality(number_of_inbound_streams, "", "s"));
  }
} 

static void
dissect_init_ack_chunk(tvbuff_t *chunk_tvb, packet_info *pinfo, proto_tree *tree,
		       proto_tree *chunk_tree, proto_item *chunk_item, proto_item *flags_item)
{ 
  dissect_init_chunk(chunk_tvb, pinfo, tree, chunk_tree, chunk_item, flags_item);
} 

static void
dissect_sack_chunk(tvbuff_t *chunk_tvb, packet_info *pinfo, proto_tree *tree,
		   proto_tree *chunk_tree, proto_item *chunk_item, proto_item *flags_item)
{ 
  guint32 cumulative_tsn_ack, adv_rec_window_credit, dup_tsn;
  guint16 number_of_gap_blocks, number_of_dup_tsns;
  guint16 gap_block_number, dup_tsn_number, start, end;
  gint gap_block_offset, dup_tsn_offset;
  proto_item *block_item;
  proto_tree *block_tree;
  
  if (check_col(pinfo->fd, COL_INFO))
    col_append_str(pinfo->fd, COL_INFO, "SACK ");

  if (chunk_tree) {
    cumulative_tsn_ack    = tvb_get_ntohl(chunk_tvb, SACK_CHUNK_CUMULATIVE_TSN_ACK_OFFSET);
    adv_rec_window_credit = tvb_get_ntohl(chunk_tvb, SACK_CHUNK_ADV_REC_WINDOW_CREDIT_OFFSET);
    number_of_gap_blocks  = tvb_get_ntohs(chunk_tvb, SACK_CHUNK_NUMBER_OF_GAP_BLOCKS_OFFSET);
    number_of_dup_tsns    = tvb_get_ntohs(chunk_tvb, SACK_CHUNK_NUMBER_OF_DUP_TSNS_OFFSET);
    
    proto_tree_add_uint(chunk_tree, hf_sctp_sack_chunk_cumulative_tsn_ack, 
			chunk_tvb,
			SACK_CHUNK_CUMULATIVE_TSN_ACK_OFFSET, SACK_CHUNK_CUMULATIVE_TSN_ACK_LENGTH,
			cumulative_tsn_ack);
    proto_tree_add_uint(chunk_tree, hf_sctp_sack_chunk_adv_rec_window_credit, 
			chunk_tvb,
			SACK_CHUNK_ADV_REC_WINDOW_CREDIT_OFFSET, SACK_CHUNK_ADV_REC_WINDOW_CREDIT_LENGTH,
			adv_rec_window_credit);
    proto_tree_add_uint(chunk_tree, hf_sctp_sack_chunk_number_of_gap_blocks, 
			chunk_tvb,
			SACK_CHUNK_NUMBER_OF_GAP_BLOCKS_OFFSET, SACK_CHUNK_NUMBER_OF_GAP_BLOCKS_LENGTH,
			number_of_gap_blocks);
    proto_tree_add_uint(chunk_tree, hf_sctp_sack_chunk_number_of_dup_tsns, 
			chunk_tvb,
			SACK_CHUNK_NUMBER_OF_DUP_TSNS_OFFSET, SACK_CHUNK_NUMBER_OF_DUP_TSNS_LENGTH,
			number_of_dup_tsns);
    
    /* handle the gap acknowledgement blocks */
    gap_block_offset = SACK_CHUNK_GAP_BLOCK_OFFSET;
    for(gap_block_number = 1; gap_block_number <= number_of_gap_blocks; gap_block_number++) {
      start = tvb_get_ntohs(chunk_tvb, gap_block_offset);
      end   = tvb_get_ntohs(chunk_tvb, gap_block_offset + SACK_CHUNK_GAP_BLOCK_START_LENGTH);
      block_item = proto_tree_add_text(chunk_tree, chunk_tvb,
				       gap_block_offset, SACK_CHUNK_GAP_BLOCK_LENGTH,
				       "Gap Acknowledgement for %u TSN%s",
				       1 + end - start, plurality(1 + end - start, "", "s"));
      block_tree = proto_item_add_subtree(block_item, ett_sctp_sack_chunk_gap_block);
      proto_tree_add_uint(block_tree, hf_sctp_sack_chunk_gap_block_start, 
			  chunk_tvb,
			  gap_block_offset, SACK_CHUNK_GAP_BLOCK_START_LENGTH,
			  start);
      proto_tree_add_uint(block_tree, hf_sctp_sack_chunk_gap_block_end, 
			  chunk_tvb,
			  gap_block_offset + SACK_CHUNK_GAP_BLOCK_START_LENGTH,
			  SACK_CHUNK_GAP_BLOCK_END_LENGTH,
			  end);
      gap_block_offset += SACK_CHUNK_GAP_BLOCK_LENGTH;
    };
    
    /* handle the duplicate TSNs */
    dup_tsn_offset = SACK_CHUNK_GAP_BLOCK_OFFSET + number_of_gap_blocks * SACK_CHUNK_GAP_BLOCK_LENGTH;
    for(dup_tsn_number = 1; dup_tsn_number <= number_of_dup_tsns; dup_tsn_number++) {
      dup_tsn = tvb_get_ntohl(chunk_tvb, dup_tsn_offset);
      proto_tree_add_uint(chunk_tree, hf_sctp_sack_chunk_duplicate_tsn, 
			  chunk_tvb,
			  dup_tsn, SACK_CHUNK_DUP_TSN_LENGTH,
			  dup_tsn);
      dup_tsn_offset += SACK_CHUNK_DUP_TSN_LENGTH;
    }
    
    proto_item_set_text(chunk_item, 
			"SACK chunk acknowledging TSN %u and reporting %u gap%s and %u duplicate TSN%s",
			cumulative_tsn_ack,
			number_of_gap_blocks, plurality(number_of_gap_blocks, "", "s"),
			number_of_dup_tsns, plurality(number_of_dup_tsns, "", "s"));
  } 
}

static void
dissect_heartbeat_chunk(tvbuff_t *chunk_tvb, packet_info *pinfo, proto_tree *tree,
			proto_tree *chunk_tree, proto_item *chunk_item, proto_item *flags_item)
{
  tvbuff_t   *parameter_tvb;
  guint chunk_length, info_length, padding_length, total_length;
    
  if (check_col(pinfo->fd, COL_INFO))
    col_append_str(pinfo->fd, COL_INFO, "HEARTBEAT ");

  if (chunk_tree) {
    chunk_length   = tvb_get_ntohs(chunk_tvb,  CHUNK_LENGTH_OFFSET);
    info_length    = chunk_length - CHUNK_HEADER_LENGTH;
    padding_length = nr_of_padding_bytes(info_length);
    total_length   = info_length + padding_length;
    parameter_tvb  = tvb_new_subset(chunk_tvb, HEARTBEAT_CHUNK_INFO_OFFSET, total_length, total_length);
    
    dissect_parameter(parameter_tvb, chunk_tree);
    
    proto_item_set_text(chunk_item, "HEARTBEAT chunk");
  }
}
 
static void
dissect_heartbeat_ack_chunk(tvbuff_t *chunk_tvb, packet_info *pinfo, proto_tree *tree,
			    proto_tree *chunk_tree, proto_item *chunk_item, proto_item *flags_item)
{  
  tvbuff_t   *parameter_tvb;
  guint chunk_length, info_length, padding_length, total_length;
  
  if (check_col(pinfo->fd, COL_INFO))
    col_append_str(pinfo->fd, COL_INFO, "HEARTBEAT_ACK ");

  if (chunk_tree) {
    chunk_length   = tvb_get_ntohs(chunk_tvb,  CHUNK_LENGTH_OFFSET);
    info_length    = chunk_length - CHUNK_HEADER_LENGTH;
    padding_length = nr_of_padding_bytes(info_length);
    total_length   = info_length + padding_length;

    parameter_tvb  = tvb_new_subset(chunk_tvb, HEARTBEAT_CHUNK_INFO_OFFSET, total_length, total_length);
    
    dissect_parameter(parameter_tvb, chunk_tree);
    
    proto_item_set_text(chunk_item, "HEARTBEAT ACK chunk");
  } 
}

static void
dissect_abort_chunk(tvbuff_t *chunk_tvb, packet_info *pinfo, proto_tree *tree,
		    proto_tree *chunk_tree, proto_item *chunk_item, proto_item *flags_item)
{
  guint offset, number_of_causes;
  guint16 length, padding_length, total_length;
  tvbuff_t *cause_tvb;
  
  if (check_col(pinfo->fd, COL_INFO))
    col_append_str(pinfo->fd, COL_INFO, "ABORT ");

  if (chunk_tree) {
    number_of_causes = 0;
    offset = ABORT_CHUNK_FIRST_ERROR_CAUSE_OFFSET;
    while(tvb_length_remaining(chunk_tvb, offset)) {
    length         = tvb_get_ntohs(chunk_tvb, offset + CAUSE_LENGTH_OFFSET);
    padding_length = nr_of_padding_bytes(length);
    total_length   = length + padding_length;
    /* create a tvb for the chunk including the padding bytes */
    cause_tvb      = tvb_new_subset(chunk_tvb, offset, total_length, total_length);
    dissect_error_cause(cause_tvb, pinfo, chunk_tree); 
    /* get rid of the handled parameter */
    offset += total_length;
    number_of_causes++;
    };
    
    proto_item_set_text(chunk_item, "Abort chunk with %u cause%s",
			number_of_causes, plurality(number_of_causes, "", "s"));
  } 
}

static void
dissect_shutdown_chunk(tvbuff_t *chunk_tvb, packet_info *pinfo, proto_tree *tree,
		       proto_tree *chunk_tree, proto_item *chunk_item, proto_item *flags_item)
{ 
  guint32 cumulative_tsn_ack;
 
  if (check_col(pinfo->fd, COL_INFO))
    col_append_str(pinfo->fd, COL_INFO, "SHUTDOWN ");

  if (chunk_tree) {
    cumulative_tsn_ack = tvb_get_ntohl(chunk_tvb, SHUTDOWN_CHUNK_CUMULATIVE_TSN_ACK_OFFSET);
    proto_tree_add_uint(chunk_tree, hf_sctp_shutdown_chunk_cumulative_tsn_ack, 
			chunk_tvb,
			SHUTDOWN_CHUNK_CUMULATIVE_TSN_ACK_OFFSET,
			SHUTDOWN_CHUNK_CUMULATIVE_TSN_ACK_LENGTH,
			cumulative_tsn_ack);
    
    proto_item_set_text(chunk_item, "SHUTDOWN chunk acknowledging up to TSN %u",
			cumulative_tsn_ack);
  } 
}

static void
dissect_shutdown_ack_chunk(tvbuff_t *chunk_tvb, packet_info *pinfo, proto_tree *tree,
			   proto_tree *chunk_tree, proto_item *chunk_item, proto_item *flags_item)
{ 
  if (check_col(pinfo->fd, COL_INFO))
    col_append_str(pinfo->fd, COL_INFO, "SHUTDOWN_ACK ");

  if (chunk_tree) {
    proto_item_set_text(chunk_item, "SHUTDOWN ACK chunk");
  } 
}

static void
dissect_error_chunk(tvbuff_t *chunk_tvb, packet_info *pinfo, proto_tree *tree,
		    proto_tree *chunk_tree, proto_item *chunk_item, proto_item *flags_item)
{ 
  guint offset, number_of_causes;
  guint16 length, padding_length, total_length;
  tvbuff_t *cause_tvb;
  
  if (check_col(pinfo->fd, COL_INFO))
    col_append_str(pinfo->fd, COL_INFO, "ERROR ");

  if (chunk_tree) {
    number_of_causes = 0;
    offset = ERROR_CHUNK_FIRST_ERROR_CAUSE_OFFSET;
    do {
      length         = tvb_get_ntohs(chunk_tvb, offset + CAUSE_LENGTH_OFFSET);
      padding_length = nr_of_padding_bytes(length);
      total_length   = length + padding_length;
      /* create a tvb for the chunk including the padding bytes */
      cause_tvb      = tvb_new_subset(chunk_tvb, offset, total_length, total_length);
      dissect_error_cause(cause_tvb, pinfo, chunk_tree); 
      /* get rid of the handled parameter */
      offset += total_length;
      number_of_causes++;
    } while(tvb_length_remaining(chunk_tvb, offset));
    
    proto_item_set_text(chunk_item, "Error chunk with %u cause%s",
			number_of_causes, plurality(number_of_causes, "", "s"));
  } 
}

static void
dissect_cookie_echo_chunk(tvbuff_t *chunk_tvb, packet_info *pinfo, proto_tree *tree,
			  proto_tree *chunk_tree, proto_item *chunk_item, proto_item *flags_item)
{ 
  guint length, cookie_length, padding_length;

  length         = tvb_get_ntohs(chunk_tvb, CHUNK_LENGTH_OFFSET);
  padding_length = nr_of_padding_bytes(length);
  cookie_length  = length - CHUNK_HEADER_LENGTH;
 
  if (check_col(pinfo->fd, COL_INFO))
    col_append_str(pinfo->fd, COL_INFO, "COOKIE_ECHO ");

  if (chunk_tree) {  
    proto_tree_add_text(chunk_tree, chunk_tvb, COOKIE_ECHO_CHUNK_COOKIE_OFFSET, cookie_length,
			"Cookie (%u byte%s)",
			cookie_length, plurality(cookie_length, "", "s"));
    proto_item_set_text(chunk_item, "COOKIE ECHO chunk containing a cookie of %u byte%s",
			cookie_length, plurality(cookie_length, "", "s"));
    
    if (padding_length > 0)
      proto_tree_add_text(chunk_tree, chunk_tvb, CHUNK_HEADER_OFFSET + length, padding_length,
			  "Padding: %u byte%s",
			  padding_length, plurality(padding_length, "", "s"));
  } 
}

static void
dissect_cookie_ack_chunk(tvbuff_t *chunk_tvb, packet_info *pinfo, proto_tree *tree,
			 proto_tree *chunk_tree, proto_item *chunk_item, proto_item *flags_item)
{  
  if (check_col(pinfo->fd, COL_INFO))
    col_append_str(pinfo->fd, COL_INFO, "COOKIE_ACK ");

  if (chunk_tree) {
    proto_item_set_text(chunk_item, "COOKIE ACK chunk");
  } 
}

static void
dissect_ecne_chunk(tvbuff_t *chunk_tvb, packet_info *pinfo, proto_tree *tree,
		   proto_tree *chunk_tree, proto_item *chunk_item, proto_item *flags_item)
{ 
  guint32 lowest_tsn;
 
  if (check_col(pinfo->fd, COL_INFO))
    col_append_str(pinfo->fd, COL_INFO, "ECNE ");

  if (chunk_tree) {
    lowest_tsn = tvb_get_ntohl(chunk_tvb, ECNE_CHUNK_LOWEST_TSN_OFFSET);
    proto_tree_add_uint(chunk_tree, hf_sctp_ecne_chunk_lowest_tsn, 
			chunk_tvb,
			ECNE_CHUNK_LOWEST_TSN_OFFSET, ECNE_CHUNK_LOWEST_TSN_LENGTH,
			lowest_tsn);
    
    proto_item_set_text(chunk_item, "ECNE chunk");
  } 
}

static void
dissect_cwr_chunk(tvbuff_t *chunk_tvb, packet_info *pinfo, proto_tree *tree,
		  proto_tree *chunk_tree, proto_item *chunk_item, proto_item *flags_item)
{ 
  guint32 lowest_tsn;
  
  if (check_col(pinfo->fd, COL_INFO))
    col_append_str(pinfo->fd, COL_INFO, "CWR ");

  if (chunk_tree) {
    lowest_tsn = tvb_get_ntohl(chunk_tvb, CWR_CHUNK_LOWEST_TSN_OFFSET);
    proto_tree_add_uint(chunk_tree, hf_sctp_cwr_chunk_lowest_tsn, 
			chunk_tvb,
			CWR_CHUNK_LOWEST_TSN_OFFSET, CWR_CHUNK_LOWEST_TSN_LENGTH,
			lowest_tsn);
    
    proto_item_set_text(chunk_item, "CWR chunk");
  } 
}

static void
dissect_shutdown_complete_chunk(tvbuff_t *chunk_tvb, packet_info *pinfo, proto_tree *tree,
				proto_tree *chunk_tree, proto_item *chunk_item, proto_item *flags_item)
{ 
  guint8  flags;
  guint16 length;
  proto_tree *flag_tree;
 
  if (check_col(pinfo->fd, COL_INFO))
    col_append_str(pinfo->fd, COL_INFO, "SHUTDOWN_COMPLETE ");

  if (chunk_tree) {
    flags             = tvb_get_guint8(chunk_tvb, CHUNK_FLAGS_OFFSET);
    length            = tvb_get_ntohs(chunk_tvb, CHUNK_LENGTH_OFFSET);
    
    flag_tree = proto_item_add_subtree(flags_item, ett_sctp_data_chunk_flags);
    proto_tree_add_boolean(flag_tree, hf_sctp_shutdown_complete_chunk_t_bit, chunk_tvb,
			   CHUNK_FLAGS_OFFSET, CHUNK_FLAGS_LENGTH, flags);
    
    proto_item_set_text(chunk_item, "SHUTDOWN COMPLETE chunk");
  } 
}

static void
dissect_unknown_chunk(tvbuff_t *chunk_tvb, packet_info *pinfo, proto_tree *tree,
		      proto_tree *chunk_tree, proto_item *chunk_item, proto_item *flags_item)
{ 
  guint length, chunk_value_length, padding_length;
  guint8 type;
  
  if (check_col(pinfo->fd, COL_INFO))
    col_append_str(pinfo->fd, COL_INFO, "UNKNOWN ");

  if (chunk_tree) {
    length         = tvb_get_ntohs(chunk_tvb, CHUNK_LENGTH_OFFSET);
    padding_length = nr_of_padding_bytes(length);
    type           = tvb_get_guint8(chunk_tvb, CHUNK_TYPE_OFFSET);
    
    chunk_value_length = length - CHUNK_HEADER_LENGTH;
    
    proto_tree_add_text(chunk_tree, chunk_tvb, CHUNK_VALUE_OFFSET, chunk_value_length,
			"Chunk value (%u byte%s)",
			chunk_value_length, plurality(chunk_value_length, "", "s"));
    
    if (padding_length > 0)
      proto_tree_add_text(chunk_tree, chunk_tvb, CHUNK_HEADER_OFFSET + length, padding_length,
			  "Padding: %u byte%s",
			  padding_length, plurality(padding_length, "", "s"));
    
    proto_item_set_text(chunk_item, "Chunk of type %u and %u byte%s value",
			type, chunk_value_length, plurality(chunk_value_length, "", "s"));
  }
} 


static gboolean
dissect_sctp_chunk(tvbuff_t *chunk_tvb, packet_info *pinfo, proto_tree *tree, proto_tree *sctp_tree)
{  
  guint8 type, flags;
  guint16 length;
  gboolean result;
  proto_item *flags_item;
  proto_item *chunk_item;
  proto_tree *chunk_tree;

  result = FALSE;
  
  /* first extract the chunk header */
  type   = tvb_get_guint8(chunk_tvb, CHUNK_TYPE_OFFSET);
  flags  = tvb_get_guint8(chunk_tvb, CHUNK_FLAGS_OFFSET);
  length = tvb_get_ntohs(chunk_tvb, CHUNK_LENGTH_OFFSET);
 
  if (tree) {
    /* create proto_tree stuff */
    chunk_item   = proto_tree_add_text(sctp_tree, chunk_tvb,
				       CHUNK_HEADER_OFFSET, tvb_length(chunk_tvb), "Incomplete chunk");
    chunk_tree   = proto_item_add_subtree(chunk_item, ett_sctp_chunk);
    
    /* then insert the chunk header components into the protocol tree */
    proto_tree_add_uint(chunk_tree, hf_sctp_chunk_type, 
			chunk_tvb, CHUNK_TYPE_OFFSET, CHUNK_TYPE_LENGTH,
			type);
    flags_item = proto_tree_add_uint(chunk_tree, hf_sctp_chunk_flags, 
				     chunk_tvb, CHUNK_FLAGS_OFFSET, CHUNK_FLAGS_LENGTH,
				     flags);
    proto_tree_add_uint(chunk_tree, hf_sctp_chunk_length, 
			chunk_tvb, CHUNK_LENGTH_OFFSET, CHUNK_LENGTH_LENGTH,
			length);
  } else {
    chunk_tree = NULL;
    chunk_item = NULL;
    flags_item = NULL;
  };
  
  /* now dissect the chunk value */

  switch(type) {
  case SCTP_DATA_CHUNK_ID:
    result = dissect_data_chunk(chunk_tvb, pinfo, tree, chunk_tree, chunk_item, flags_item);
    break;
  case SCTP_INIT_CHUNK_ID:
    dissect_init_chunk(chunk_tvb, pinfo, tree, chunk_tree, chunk_item, flags_item);
    break;
  case SCTP_INIT_ACK_CHUNK_ID:
    dissect_init_ack_chunk(chunk_tvb, pinfo, tree, chunk_tree, chunk_item, flags_item);
    break;
  case SCTP_SACK_CHUNK_ID:
    dissect_sack_chunk(chunk_tvb, pinfo, tree, chunk_tree, chunk_item, flags_item);
    break; 
  case SCTP_HEARTBEAT_CHUNK_ID:
    dissect_heartbeat_chunk(chunk_tvb, pinfo, tree, chunk_tree, chunk_item, flags_item);
    break;
  case SCTP_HEARTBEAT_ACK_CHUNK_ID:
    dissect_heartbeat_ack_chunk(chunk_tvb, pinfo, tree, chunk_tree, chunk_item, flags_item);
    break;
  case SCTP_ABORT_CHUNK_ID:
    dissect_abort_chunk(chunk_tvb, pinfo, tree, chunk_tree, chunk_item, flags_item);
    break;
  case SCTP_SHUTDOWN_CHUNK_ID:
    dissect_shutdown_chunk(chunk_tvb, pinfo, tree, chunk_tree, chunk_item, flags_item);
    break;
  case SCTP_SHUTDOWN_ACK_CHUNK_ID:
    dissect_shutdown_ack_chunk(chunk_tvb, pinfo, tree, chunk_tree, chunk_item, flags_item);
    break;
  case SCTP_ERROR_CHUNK_ID:
    dissect_error_chunk(chunk_tvb, pinfo, tree, chunk_tree, chunk_item, flags_item);
    break;
  case SCTP_COOKIE_ECHO_CHUNK_ID:
    dissect_cookie_echo_chunk(chunk_tvb, pinfo, tree, chunk_tree, chunk_item, flags_item);
    break;
  case SCTP_COOKIE_ACK_CHUNK_ID:
    dissect_cookie_ack_chunk(chunk_tvb, pinfo, tree, chunk_tree, chunk_item, flags_item);
    break;
  case SCTP_ECNE_CHUNK_ID:
    dissect_ecne_chunk(chunk_tvb, pinfo, tree, chunk_tree, chunk_item, flags_item);
    break;
  case SCTP_CWR_CHUNK_ID:
    dissect_cwr_chunk(chunk_tvb, pinfo, tree, chunk_tree, chunk_item, flags_item);
    break;
  case SCTP_SHUTDOWN_COMPLETE_CHUNK_ID:
    dissect_shutdown_complete_chunk(chunk_tvb, pinfo, tree, chunk_tree, chunk_item, flags_item);
      break;
  default:
    dissect_unknown_chunk(chunk_tvb, pinfo, tree, chunk_tree, chunk_item, flags_item);
    break;
  };
  return result;
}

static void
dissect_sctp_chunks(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
		    proto_item *sctp_item, proto_tree *sctp_tree)
{ 
  tvbuff_t *chunk_tvb;
  guint16 length, padding_length, total_length;
  gint last_offset, offset;
  gboolean sctp_item_length_set;

  /* the common header of the datagram is already handled */
  last_offset = 0;
  offset = COMMON_HEADER_LENGTH;
  sctp_item_length_set = FALSE;

  while(tvb_length_remaining(tvb, offset) > 0) {
    /* extract the chunk length and compute number of padding bytes */
    length         = tvb_get_ntohs(tvb, offset + CHUNK_LENGTH_OFFSET);
    padding_length = nr_of_padding_bytes(length);
    total_length   = length + padding_length;
    /* create a tvb for the chunk including the padding bytes */
    chunk_tvb    = tvb_new_subset(tvb, offset, total_length, total_length);
    /* call dissect_sctp_chunk for a actual work */
    if (dissect_sctp_chunk(chunk_tvb, pinfo, tree, sctp_tree) && (tree)) {
      proto_item_set_len(sctp_item, offset - last_offset + DATA_CHUNK_HEADER_LENGTH);
      sctp_item_length_set = TRUE;
      offset += total_length;
      last_offset = offset;
      if (tvb_length_remaining(tvb, offset) > 0) {
	sctp_item = proto_tree_add_item(tree, proto_sctp, tvb, offset, 0, FALSE);
	sctp_tree = proto_item_add_subtree(sctp_item, ett_sctp);
	sctp_item_length_set = FALSE;
      }
    } else {
    /* get rid of the dissected chunk */
    offset += total_length;
    }
  };
  if (!sctp_item_length_set && (tree)) { 
    proto_item_set_len(sctp_item, offset - last_offset);
  };
}

/* dissect_sctp handles the common header of a SCTP datagram.
 * For the handling of the chunks dissect_sctp_chunks is called.
 */

static void
dissect_sctp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
  guint16 source_port, destination_port;
  guint32 verification_tag, checksum;
  proto_item *sctp_item;
  proto_tree *sctp_tree;

  /* Extract the common header */
  source_port      = tvb_get_ntohs(tvb, SOURCE_PORT_OFFSET);
  destination_port = tvb_get_ntohs(tvb, DESTINATION_PORT_OFFSET);
  verification_tag = tvb_get_ntohl(tvb, VERIFICATION_TAG_OFFSET);
  checksum         = tvb_get_ntohl(tvb, CHECKSUM_OFFSET);

  /* update pi structure */
  pinfo->ptype    = PT_SCTP;
  pinfo->srcport  = source_port;
  pinfo->destport = destination_port;

  /* make entry in the Protocol column on summary display */
  if (check_col(pinfo->fd, COL_PROTOCOL)) 
    col_set_str(pinfo->fd, COL_PROTOCOL, "SCTP");

  /* Clear entries in Info column on summary display */
  if (check_col(pinfo->fd, COL_INFO))
    col_add_str(pinfo->fd, COL_INFO, "");
  
  /* In the interest of speed, if "tree" is NULL, don't do any work not
     necessary to generate protocol tree items. */
  if (tree) {
    /* create the sctp protocol tree */
    sctp_item = proto_tree_add_item(tree, proto_sctp, tvb, 0, 0, FALSE);
    sctp_tree = proto_item_add_subtree(sctp_item, ett_sctp);

    /* add the components of the common header to the protocol tree */
    proto_tree_add_uint(sctp_tree, hf_sctp_source_port, 
			tvb, SOURCE_PORT_OFFSET, SOURCE_PORT_LENGTH,
			source_port);
    proto_tree_add_uint(sctp_tree, hf_sctp_destination_port,
			tvb, DESTINATION_PORT_OFFSET, DESTINATION_PORT_LENGTH,
			destination_port);
    proto_tree_add_uint(sctp_tree, hf_sctp_verification_tag,
			tvb, VERIFICATION_TAG_OFFSET, VERIFICATION_TAG_LENGTH,
			verification_tag);
    proto_tree_add_uint_format(sctp_tree, hf_sctp_checksum,
			       tvb, CHECKSUM_OFFSET, CHECKSUM_LENGTH, checksum,
			       "Adler-32 checksum: 0x%08x (%s)",
			       checksum, sctp_checksum_state(tvb, checksum));
  } else {
    sctp_tree = NULL;
    sctp_item = NULL;
  };
  /* add all chunks of the sctp datagram to the protocol tree */
  dissect_sctp_chunks(tvb, pinfo, tree, sctp_item, sctp_tree);
}

/* Register the protocol with Ethereal */
void
proto_register_sctp(void)
{                 

  /* Setup list of header fields */
  static hf_register_info hf[] = {
    { &hf_sctp_source_port,
      { "Source port", "sctp.srcport",
	FT_UINT16, BASE_DEC, NULL, 0x0,          
	"", HFILL }
    },
    { &hf_sctp_destination_port,
      { "Destination port", "sctp.dstport",
	FT_UINT16, BASE_DEC, NULL, 0x0,          
	"", HFILL }
    }, 
    { &hf_sctp_verification_tag,
      { "Verification tag", "sctp.verfication_tag",
	FT_UINT32, BASE_HEX, NULL, 0x0,          
	"", HFILL }
    },
    { &hf_sctp_checksum,
      { "Adler-32 checksum", "sctp.checksum",
	FT_UINT32, BASE_HEX, NULL, 0x0,          
	"", HFILL }
    },
    { &hf_sctp_chunk_type,
      { "Identifier", "sctp.chunk_type",
	FT_UINT8, BASE_DEC, VALS(sctp_chunk_type_values), 0x0,          
	"", HFILL }
    },
    { &hf_sctp_chunk_flags,
      { "Flags", "sctp.chunk_flags",
	FT_UINT8, BASE_BIN, NULL, 0x0,          
	"", HFILL }
    },
    { &hf_sctp_chunk_length,
      { "Length", "sctp.chunk_length",
	FT_UINT16, BASE_DEC, NULL, 0x0,          
	"", HFILL }
    },
    { &hf_sctp_init_chunk_initiate_tag,
      { "Initiate tag", "sctp.init.chunk.initiate.tag",
	FT_UINT32, BASE_HEX, NULL, 0x0,          
	"", HFILL }
    },
    { &hf_sctp_init_chunk_adv_rec_window_credit,
      { "Advertised reciever window credit (a_rwnd)", "sctp.init.chunk.credit",
	FT_UINT32, BASE_DEC, NULL, 0x0,          
	"", HFILL }
    },
    { &hf_sctp_init_chunk_number_of_outbound_streams,
      { "Number of outbound streams", "sctp.init.chunk.nr.out.streams",
	FT_UINT16, BASE_DEC, NULL, 0x0,          
	"", HFILL }
    },
    { &hf_sctp_init_chunk_number_of_inbound_streams,
      { "Number of inbound streams", "sctp.init.chunk.nr.in.streams",
	FT_UINT16, BASE_DEC, NULL, 0x0,          
	"", HFILL }
    },
    {&hf_sctp_init_chunk_initial_tsn,
      { "Initial TSN", "sctp.init.chunk.initial.tsn",
	FT_UINT32, BASE_DEC, NULL, 0x0,          
	"", HFILL }
    }, 
    {&hf_sctp_cumulative_tsn_ack,
     { "Cumulative TSN Ack", "sctp.cumulative.tsn.ack",
	FT_UINT32, BASE_DEC, NULL, 0x0,          
	"", HFILL }
    },
    {&hf_sctp_data_chunk_tsn,
     { "TSN", "sctp.tsn",
       FT_UINT32, BASE_DEC, NULL, 0x0,          
	"", HFILL }
    },
    {&hf_sctp_data_chunk_stream_id,
     { "Stream Identifier", "sctp.stream_id",
	FT_UINT16, BASE_HEX, NULL, 0x0,          
	"", HFILL }
    },
    {&hf_sctp_data_chunk_stream_seq_number,
     { "Stream sequence number", "sctp.stream_seq_number",
       FT_UINT16, BASE_DEC, NULL, 0x0,          
       "", HFILL }
    },
    {&hf_sctp_data_chunk_payload_proto_id,
     { "Payload protocol identifier", "sctp.payload_proto_id",
	FT_UINT32, BASE_DEC, VALS(sctp_payload_proto_id_values), 0x0,          
	"", HFILL }
    },
    {&hf_sctp_data_chunk_e_bit,
     { "E-Bit", "sctp.data.e_bit",
       FT_BOOLEAN, 8, TFS(&sctp_data_chunk_e_bit_value), SCTP_DATA_CHUNK_E_BIT,          
       "", HFILL }
    },
    {&hf_sctp_data_chunk_b_bit,
     { "B-Bit", "sctp.data.b_bit",
       FT_BOOLEAN, 8, TFS(&sctp_data_chunk_b_bit_value), SCTP_DATA_CHUNK_B_BIT,          
       "", HFILL }
    },
    {&hf_sctp_data_chunk_u_bit,
     { "U-Bit", "sctp.data.u.bit",
       FT_BOOLEAN, 8, TFS(&sctp_data_chunk_u_bit_value), SCTP_DATA_CHUNK_U_BIT,          
       "", HFILL }
    },
    {&hf_sctp_sack_chunk_cumulative_tsn_ack,
     { "Cumulative TSN ACK", "sctp.sack.cumulative_tsn_ack",
       FT_UINT32, BASE_DEC, NULL, 0x0,          
       "", HFILL }
    }, 
    {&hf_sctp_sack_chunk_adv_rec_window_credit,
     { "Advertised receiver window credit (a_rwnd)", "sctp.sack.a_rwnd",
       FT_UINT32, BASE_DEC, NULL, 0x0,          
       "", HFILL }
    },
    {&hf_sctp_sack_chunk_number_of_gap_blocks,
     { "Number of gap acknowldgement blocks ", "sctp.sack.number_of_gap_blocks",
       FT_UINT16, BASE_DEC, NULL, 0x0,          
       "", HFILL }
    }, 
    {&hf_sctp_sack_chunk_number_of_dup_tsns,
     { "Number of duplicated TSNs", "sctp.sack.number_of_duplicated_tsns",
       FT_UINT16, BASE_DEC, NULL, 0x0,          
       "", HFILL }
    },
    {&hf_sctp_sack_chunk_gap_block_start,
     { "Start", "sctp.sack.gap_block_start",
       FT_UINT16, BASE_DEC, NULL, 0x0,          
       "", HFILL }
    },
    {&hf_sctp_sack_chunk_gap_block_end,
     { "End", "sctp.sack.gap_block_end",
       FT_UINT16, BASE_DEC, NULL, 0x0,          
       "", HFILL }
    },
    {&hf_sctp_sack_chunk_duplicate_tsn,
     { "Duplicate TSN", "sctp.sack.duplicate.tsn",
       FT_UINT16, BASE_DEC, NULL, 0x0,          
       "", HFILL }
    },  
    {&hf_sctp_shutdown_chunk_cumulative_tsn_ack,
     { "Cumulative TSN Ack", "sctp.shutdown.cumulative_tsn_ack",
	FT_UINT32, BASE_DEC, NULL, 0x0,          
	"", HFILL }
    },
    {&hf_sctp_ecne_chunk_lowest_tsn,
     { "Lowest TSN", "sctp.ecne.lowest_tsn",
       FT_UINT32, BASE_DEC, NULL, 0x0,          
       "", HFILL }
    }, 
    {&hf_sctp_cwr_chunk_lowest_tsn,
     { "Lowest TSN", "sctp.cwr.lowest_tsn",
       FT_UINT32, BASE_DEC, NULL, 0x0,          
       "", HFILL }
    }, 
    {&hf_sctp_shutdown_complete_chunk_t_bit,
     { "E-Bit", "sctp.shutdown_complete.t_bit",
       FT_BOOLEAN, 8, TFS(&sctp_shutdown_complete_chunk_t_bit_value), SCTP_SHUTDOWN_COMPLETE_CHUNK_T_BIT,
       "", HFILL }
    },
    {&hf_sctp_chunk_parameter_type,
     { "Parameter type", "sctp.parameter.type",
       FT_UINT16, BASE_HEX, VALS(sctp_parameter_identifier_values), 0x0,
       "", HFILL }
    },
    {&hf_sctp_chunk_parameter_length,
     { "Parameter length", "sctp.parameter.length",
       FT_UINT16, BASE_DEC, NULL, 0x0,          
       "", HFILL }
    },
    {&hf_sctp_parameter_ipv4_address,
     { "IP Version 4 address", "sctp.parameter.ipv4_address",
       FT_IPv4, BASE_NONE, NULL, 0x0,
       "", HFILL }
    },
    {&hf_sctp_parameter_ipv6_address,
     { "IP Version 6 address", "sctp.parameter.ipv6_address",
       FT_IPv6, BASE_NONE, NULL, 0x0,
       "", HFILL }
    },
    {&hf_sctp_parameter_cookie_preservative_increment,
     { "Suggested Cookie life-span increment (msec)", "sctp.parameter.cookie_preservative_incr",
       FT_UINT32, BASE_DEC, NULL, 0x0,          
       "", HFILL }
    },
    {&hf_sctp_parameter_hostname_hostname,
     { "Hostname", "sctp.parameter.hostname.hostname",
       FT_STRING, BASE_NONE, NULL, 0x0,          
       "", HFILL }
    }, 
    {&hf_sctp_supported_address_types_parameter,
     { "Supported address type", "sctp.parameter.supported_addres_type",
       FT_UINT16, BASE_DEC, NULL, 0x0,          
       "", HFILL }
    }, 
    {&hf_sctp_cause_code,
     { "Cause code", "sctp.cause.code",
       FT_UINT16, BASE_HEX, VALS(sctp_cause_code_values), 0x0,          
       "", HFILL }
    },
    {&hf_sctp_cause_length,
     { "Cause length", "sctp.cause.length",
       FT_UINT16, BASE_DEC, NULL, 0x0,          
       "", HFILL }
    }, 
    {&hf_sctp_cause_stream_identifier,
     { "Stream identifier", "sctp.cause.stream_identifier",
       FT_UINT16, BASE_DEC, NULL, 0x0,          
       "", HFILL }
    },
    {&hf_sctp_cause_number_of_missing_parameters,
     { "Number of missing parameters", "sctp.cause.nr_of_missing_parameters",
       FT_UINT32, BASE_DEC, NULL, 0x0,          
       "", HFILL }
    }, 
    {&hf_sctp_cause_missing_parameter_type,
     { "Missing parameter type", "sctp.cause.missing_parameter_type",
       FT_UINT16, BASE_HEX, VALS(sctp_parameter_identifier_values), 0x0,
       "", HFILL }
    },
    {&hf_sctp_cause_measure_of_staleness,
     { "Measure of staleness in usec", "sctp.cause.measure_of_staleness",
       FT_UINT32, BASE_DEC, NULL, 0x0,          
       "", HFILL }
    },
    {&hf_sctp_cause_tsn,
     { "TSN", "sctp.cause.tsn",
       FT_UINT32, BASE_DEC, NULL, 0x0,          
       "", HFILL }
    },
  };
  
  /* Setup protocol subtree array */
  static gint *ett[] = {
    &ett_sctp,
    &ett_sctp_chunk,
    &ett_sctp_chunk_parameter,
    &ett_sctp_chunk_cause,
    &ett_sctp_data_chunk_flags,
    &ett_sctp_sack_chunk_gap_block,
    &ett_sctp_supported_address_types_parameter,
    &ett_sctp_unrecognized_parameter_parameter
  };
  
  /* Register the protocol name and description */
  proto_sctp = proto_register_protocol("Stream Control Transmission Protocol",
				       "SCTP", "sctp");
  
  /* Required function calls to register the header fields and subtrees used */
  proto_register_field_array(proto_sctp, hf, array_length(hf));
  proto_register_subtree_array(ett, array_length(ett));

  /* subdissector code */
  sctp_port_dissector_table = register_dissector_table("sctp.port",
	"SCTP port", FT_UINT16, BASE_DEC);
  sctp_ppi_dissector_table  = register_dissector_table("sctp.ppi",
        "SCTP payload protocol identifier", FT_UINT32, BASE_HEX);

};

void
proto_reg_handoff_sctp(void)
{
  dissector_handle_t sctp_handle;

  sctp_handle = create_dissector_handle(dissect_sctp, proto_sctp);
  dissector_add("ip.proto", IP_PROTO_SCTP, sctp_handle);
}