aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-ms-mms.c
blob: 9287e4341da96deef3a3434174f64f3a5c30297d (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
/* packet-ms-mms.c
 *
 * Routines for MicroSoft MMS (Microsoft Media Server) message dissection
 *
 * See
 *
 *    http://msdn.microsoft.com/en-us/library/cc234711.aspx
 *
 * for the [MS-MMSP] specification.
 *
 * Copyright 2005
 * Written by Martin Mathieson
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

/* Information sources:
 * sdp.ppona.com
 */

#include "config.h"

#include <stdio.h>

#include <epan/packet.h>
#include <epan/conversation.h>
#include <epan/strutil.h>

static dissector_handle_t msmms_handle;
static gint               proto_msmms                      = -1;


/* Command fields */
static gint   hf_msmms_command                             = -1;
static gint   hf_msmms_command_common_header               = -1;
/* static gint   hf_msmms_command_version                     = -1; */
static gint   hf_msmms_command_signature                   = -1;
static gint   hf_msmms_command_length                      = -1;
static gint   hf_msmms_command_protocol_type               = -1;
static gint   hf_msmms_command_length_remaining            = -1;
static gint   hf_msmms_command_sequence_number             = -1;
static gint   hf_msmms_command_timestamp                   = -1;
static gint   hf_msmms_command_length_remaining2           = -1;
static gint   hf_msmms_command_to_client_id                = -1;
static gint   hf_msmms_command_to_server_id                = -1;
static gint   hf_msmms_command_direction                   = -1;

static gint   hf_msmms_command_prefix1                     = -1;
static gint   hf_msmms_command_prefix1_error               = -1;
static gint   hf_msmms_command_prefix1_command_level       = -1;
static gint   hf_msmms_command_prefix2                     = -1;

static gint   hf_msmms_command_client_transport_info       = -1;
static gint   hf_msmms_command_client_player_info          = -1;

static gint   hf_msmms_command_server_version              = -1;
static gint   hf_msmms_command_tool_version                = -1;
static gint   hf_msmms_command_update_url                  = -1;
static gint   hf_msmms_command_password_type               = -1;

static gint   hf_msmms_command_server_version_length       = -1;
static gint   hf_msmms_command_tool_version_length         = -1;
static gint   hf_msmms_command_update_url_length           = -1;
static gint   hf_msmms_command_password_type_length        = -1;

static gint   hf_msmms_command_number_of_words             = -1;
static gint   hf_msmms_command_client_id                   = -1;
static gint   hf_msmms_command_server_file                 = -1;

static gint   hf_msmms_command_result_flags                = -1;

static gint   hf_msmms_command_broadcast_indexing          = -1;
static gint   hf_msmms_command_broadcast_liveness          = -1;

static gint   hf_msmms_command_recorded_media_length       = -1;
static gint   hf_msmms_command_media_packet_length         = -1;

static gint   hf_msmms_command_strange_string              = -1;

static gint   hf_msmms_command_stream_structure_count      = -1;
static gint   hf_msmms_stream_selection_flags              = -1;
static gint   hf_msmms_stream_selection_stream_id          = -1;
static gint   hf_msmms_stream_selection_action             = -1;

static gint   hf_msmms_command_header_packet_id_type       = -1;


/* Data fields */
static gint   hf_msmms_data                                = -1;
static gint   hf_msmms_data_sequence_number                = -1;
static gint   hf_msmms_data_packet_id_type                 = -1;
static gint   hf_msmms_data_udp_sequence                   = -1;
static gint   hf_msmms_data_tcp_flags                      = -1;
static gint   hf_msmms_data_packet_length                  = -1;

static gint   hf_msmms_data_header_id                      = -1;
static gint   hf_msmms_data_client_id                      = -1;
static gint   hf_msmms_data_command_id                     = -1;
static gint   hf_msmms_data_packet_to_resend               = -1;

static gint   hf_msmms_data_timing_pair                    = -1;
static gint   hf_msmms_data_timing_pair_seqno              = -1;
static gint   hf_msmms_data_timing_pair_flags              = -1;
static gint   hf_msmms_data_timing_pair_id                 = -1;
static gint   hf_msmms_data_timing_pair_flag               = -1;
static gint   hf_msmms_data_timing_pair_packet_length      = -1;

static gint   hf_msmms_data_unparsed                       = -1;


/* Subtrees */
static gint   ett_msmms_command                            = -1;
static gint   ett_msmms_command_common_header              = -1;
static gint   ett_msmms_data                               = -1;
static gint   ett_msmms_data_timing_packet_pair            = -1;

#define MSMMS_PORT 1755


/* Known command IDs */
#define SERVER_COMMAND_CONNECT_INFO                0x01
#define SERVER_COMMAND_TRANSPORT_INFO              0x02
#define SERVER_COMMAND_PROTOCOL_SELECTION_ERROR    0x03
#define SERVER_COMMAND_REQUEST_SERVER_FILE         0x05
#define SERVER_COMMAND_START_SENDING_FROM          0x07
#define SERVER_COMMAND_STOP_BUTTON_PRESSED         0x09
#define SERVER_COMMAND_CANCEL_PROTOCOL             0x0d
#define SERVER_COMMAND_HEADER_REQUEST              0x15
#define SERVER_COMMAND_TIMING_TEST_DATA_REQUEST    0x18
#define SERVER_COMMAND_AUTHENTICATION_RESPONSE     0x1a
#define SERVER_COMMAND_NETWORK_TIMER_TEST_RESPONSE 0x1b
#define SERVER_COMMAND_ACTIVATE_FF_RW_BUTTONS      0x28
#define SERVER_COMMAND_HAVE_STOPPED_PLAYING        0x30
#define SERVER_COMMAND_LOCAL_COMPUTER_DETAILS      0x32
#define SERVER_COMMAND_MEDIA_STREAM_MBR_SELECTOR   0x33

static const value_string to_server_command_vals[] =
{
    { SERVER_COMMAND_CONNECT_INFO,                "Connect info" },
    { SERVER_COMMAND_TRANSPORT_INFO,              "Transport info" },
    { SERVER_COMMAND_PROTOCOL_SELECTION_ERROR,    "Protocol selection error" },
    { SERVER_COMMAND_REQUEST_SERVER_FILE,         "Request server file"  },
    { SERVER_COMMAND_START_SENDING_FROM,          "Start sending from:" },
    { SERVER_COMMAND_STOP_BUTTON_PRESSED,         "Stop button pressed" },
    { SERVER_COMMAND_CANCEL_PROTOCOL,             "Cancel protocol" },
    { SERVER_COMMAND_HEADER_REQUEST,              "Header request" },
    { SERVER_COMMAND_TIMING_TEST_DATA_REQUEST,    "Timing test data request" },
    { SERVER_COMMAND_AUTHENTICATION_RESPONSE,     "Authentication response" },
    { SERVER_COMMAND_NETWORK_TIMER_TEST_RESPONSE, "Network timer test response" },
    { SERVER_COMMAND_ACTIVATE_FF_RW_BUTTONS,      "Activate FF/Rewind buttons" },
    { SERVER_COMMAND_HAVE_STOPPED_PLAYING,        "Have stopped playing" },
    { SERVER_COMMAND_LOCAL_COMPUTER_DETAILS,      "Local computer details" },
    { SERVER_COMMAND_MEDIA_STREAM_MBR_SELECTOR,   "Media Stream MBR selector" },
    { 0,      NULL }
};


#define CLIENT_COMMAND_SERVER_INFO                0x01
#define CLIENT_COMMAND_TRANSPORT_INFO_ACK         0x02
#define CLIENT_COMMAND_PROTOCOL_SELECTION_ERROR   0x03
#define CLIENT_COMMAND_SENDING_MEDIA_FILE_NOW     0x05
#define CLIENT_COMMAND_MEDIA_DETAILS              0x06
#define CLIENT_COMMAND_FF_RW                      0x0a
#define CLIENT_COMMAND_SENDING_HEADER_RESPONSE    0x11
#define CLIENT_COMMAND_TIMING_TEST_DATA_RESPONSE  0x15
#define CLIENT_COMMAND_TIMING_TEST_DATA_REQUEST   0x18
#define CLIENT_COMMAND_AUTHENTICATION_CHALLENGE   0x1a
#define CLIENT_COMMAND_NETWORK_TIMER_TEST         0x1b
#define CLIENT_COMMAND_END_OF_MEDIA_STREAM        0x1e
#define CLIENT_COMMAND_MEDIA_CHANGING_INDICATOR   0x20
#define CLIENT_COMMAND_STREAM_SELECTION_INDICATOR 0x21

static const value_string to_client_command_vals[] =
{
    { CLIENT_COMMAND_SERVER_INFO,                "Server info" },
    { CLIENT_COMMAND_TRANSPORT_INFO_ACK,         "Transport info ack" },
    { CLIENT_COMMAND_PROTOCOL_SELECTION_ERROR,   "Protocol selection error" },
    { CLIENT_COMMAND_SENDING_MEDIA_FILE_NOW,     "Sending media file now"  },
    { CLIENT_COMMAND_MEDIA_DETAILS,              "Media details"  },
    { CLIENT_COMMAND_FF_RW,                      "FF/Rewind" },
    { CLIENT_COMMAND_SENDING_HEADER_RESPONSE,    "Sending header response" },
    { CLIENT_COMMAND_TIMING_TEST_DATA_RESPONSE,  "Timing test data response" },
    { CLIENT_COMMAND_TIMING_TEST_DATA_REQUEST,   "Timing test data request" },
    { CLIENT_COMMAND_AUTHENTICATION_CHALLENGE,   "Authentication challenge" },
    { CLIENT_COMMAND_NETWORK_TIMER_TEST,         "Network timer test" },
    { CLIENT_COMMAND_END_OF_MEDIA_STREAM,        "End of media stream" },
    { CLIENT_COMMAND_MEDIA_CHANGING_INDICATOR,   "Media changing indicator" },
    { CLIENT_COMMAND_STREAM_SELECTION_INDICATOR, "Stream selection indicator" },
    { 0,      NULL }
};

/* Command direction */

#define  TO_SERVER      0x0003
#define  TO_CLIENT      0x0004

static const value_string command_direction_vals[] =
{
    { TO_SERVER,  "To Server"},
    { TO_CLIENT,  "To Client"},
    { 0, NULL }
};

static const value_string tcp_flags_vals[] =
{
    { 0x00,     "Middle of packet series" },
    { 0x04,     "First packet of a packet series" },
    { 0x08,     "Last packet of a packet series" },
    { 0x0C,     "There is only one packet in this series"  },
    { 0x10,     "UDP packet pair timing packet" },
    { 0,        NULL }
};

static const value_string media_result_flags_vals[] =
{
    { 0x01,  "Media file name was accepted (no auth)"},
    { 0x02,  "Authentication for this media was accepted (BASIC auth)"},
    { 0x03,  "Authentication accepted (NTLM auth)"},
    { 0, NULL }
};

static const value_string broadcast_indexing_vals[] =
{
    { 0x00,  "No indexed seeking (live or no video streams)"},
    { 0x80,  "Indexed seeking (video streams available)"},
    { 0, NULL }
};

static const value_string broadcast_liveness_vals[] =
{
    { 0x01,  "Pre-recorded broadcast"},
    { 0x02,  "Live broadcast"},
    { 0x42,  "Presentation which includes a script command"},
    { 0, NULL }
};

static const value_string stream_selection_action_vals[] =
{
    { 0x00,  "Stream at full frame rate"},
    { 0x01,  "Only stream key frames"},
    { 0x02,  "No stream, switch it off"},
    { 0, NULL }
};


/* Server to client error codes */
static const value_string server_to_client_error_vals[] =
{
    { 0x00000000,  "OK"},
    { 0xC00D001A,  "File was not found"},
    { 0xC00D000E,  "The network is busy"},
    { 0xC00D000F,  "Too many connection sessions to server exist, cannot connect"},
    { 0xC00D0029,  "The network has failed - connection was lost"},
    { 0xC00D0034,  "There is no more data in the stream (UDP)"},
    { 0x80070005,  "You do not have access to the location or file"},
    { 0xC00D0013,  "There was no timely response from the server"},
    { 0x80070057,  "A parameter in the location is incorrect"},
    { 0x8000FFFF,  "File failed to open"},
    { 0, NULL }
};




/*************************/
/* Function declarations */
void proto_register_msmms(void);
void proto_reg_handoff_msmms_command(void);

static gint dissect_msmms_command(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
static gint dissect_msmms_data(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
static gint dissect_msmms_data_udp_command(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);

static void dissect_client_transport_info(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
                                          guint offset, guint length_remaining);
static void dissect_server_info(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
                                guint offset);
static void dissect_client_player_info(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
                                       guint offset, guint length_remaining);
static void dissect_start_sending_from_info(tvbuff_t *tvb, proto_tree *tree, guint offset);
static void dissect_cancel_info(tvbuff_t *tvb, proto_tree *tree, guint offset);
static void dissect_timing_test_request(tvbuff_t *tvb, proto_tree *tree, guint offset);
static void dissect_timing_test_response(tvbuff_t *tvb, proto_tree *tree, guint offset);
static void dissect_request_server_file(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
                                        guint offset, guint length_remaining);
static void dissect_media_details(tvbuff_t *tvb, proto_tree *tree, guint offset);
static void dissect_header_response(tvbuff_t *tvb, proto_tree *tree, guint offset);
static void dissect_network_timer_test_response(tvbuff_t *tvb, proto_tree *tree, guint offset);
static void dissect_transport_info_response(tvbuff_t *tvb, proto_tree *tree, guint offset,
                                            guint length_remaining);
static void dissect_media_stream_mbr_selector(tvbuff_t *tvb, proto_tree *tree, guint offset);
static void dissect_header_request(tvbuff_t *tvb, proto_tree *tree, guint offset);
static void dissect_stop_button_pressed(tvbuff_t *tvb, proto_tree *tree, guint offset);

static void msmms_data_add_address(packet_info *pinfo, address *addr, endpoint_type et, int port);



/****************************/
/* Main dissection function */
/****************************/
static gint dissect_msmms_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
{
    /* Work out what type of packet this is and dissect it as such */

    /* Just don't dissect if can't even read command signature */
    if (tvb_captured_length(tvb) < 8) {
        return 0;
    }

    /* Command */
    if (tvb_get_letohl(tvb, 4) == 0xb00bface)
    {
        return dissect_msmms_command(tvb, pinfo, tree);
    }
    else
    /* UDP data command */
    if ((pinfo->ptype == PT_UDP) && (pinfo->destport == MSMMS_PORT))
    {
        return dissect_msmms_data_udp_command(tvb, pinfo, tree);
    }
    else
    /* Assume data (don't consider frames from client->server data...) */
    if (pinfo->destport != MSMMS_PORT)
    {
        return dissect_msmms_data(tvb, pinfo, tree);
    }

    /* It was none of the above so assume not really an MMS packet */
    return 0;
}


/*************************************/
/* Dissection of different PDU types */
/*************************************/

/* Dissect command packet */
static gint dissect_msmms_command(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
    gint        offset                    = 0;
    proto_item *ti;
    proto_tree *msmms_tree;
    proto_tree *msmms_common_command_tree;
    guint32     sequence_number;
    guint16     command_id;
    guint16     command_dir;
    gint32      length_of_command;
    guint32     length_remaining;

    /******************************/
    /* Check for available length */

    /* Need to have enough bytes for length field */
    if (tvb_reported_length_remaining(tvb, offset) < 12)
    {
        pinfo->desegment_offset = 0;  /* Start at beginning next time */
        pinfo->desegment_len = DESEGMENT_ONE_MORE_SEGMENT;     /* Need one more byte to try again */
        return -1;
    }

    /* Read length field and see if we're short */
    length_of_command = tvb_get_letohl(tvb, offset+8);
    if (tvb_reported_length_remaining(tvb, 16) < length_of_command)
    {
        pinfo->desegment_offset = 0; /* Start at beginning next time */
        pinfo->desegment_len = DESEGMENT_ONE_MORE_SEGMENT;   /* Need one more byte to try again */
        return -1;
    }


    /* Set columns */
    col_set_str(pinfo->cinfo, COL_PROTOCOL, "MSMMS");
    col_set_str(pinfo->cinfo, COL_INFO, "Command: ");

    /* Add hidden filter for "msmms.command" */
    ti = proto_tree_add_item(tree, hf_msmms_command, tvb, 0, 0, ENC_ASCII|ENC_NA);
    PROTO_ITEM_SET_HIDDEN(ti);

    /* Create MSMMS control protocol tree */
    ti = proto_tree_add_item(tree, proto_msmms, tvb, offset, -1, ENC_NA);
    msmms_tree = proto_item_add_subtree(ti, ett_msmms_command);


    /* Read command ID and direction now so can give common command header a
       descriptive label */
    command_id = tvb_get_letohs(tvb, 36);
    command_dir = tvb_get_letohs(tvb, 36+2);


    /*************************/
    /* Common command header */

    /* Add a tree for common header */
    ti =  proto_tree_add_string_format(msmms_tree, hf_msmms_command_common_header, tvb, offset, -1,
            "",
            "%s (to %s)",
            (command_dir == TO_SERVER) ?
            val_to_str_const(command_id, to_server_command_vals, "Unknown") :
            val_to_str_const(command_id, to_client_command_vals, "Unknown"),
            (command_dir == TO_SERVER) ? "server" : "client");
    msmms_common_command_tree = proto_item_add_subtree(ti, ett_msmms_command_common_header);

    /* Format of 1st 4 bytes unknown.  May be version... */
    offset += 4;

    /* Signature (already verified by main dissection function) */
    proto_tree_add_item(msmms_common_command_tree, hf_msmms_command_signature, tvb, offset, 4, ENC_LITTLE_ENDIAN);
    offset += 4;

    /* Length of command */
    proto_tree_add_item(msmms_common_command_tree, hf_msmms_command_length, tvb, offset, 4, ENC_LITTLE_ENDIAN);
    offset += 4;

    /* Protocol name.  Must be "MMS"... */
    if (strncmp((char*)tvb_get_string_enc(wmem_packet_scope(), tvb, offset, 3, ENC_ASCII), "MMS", 3) != 0)
    {
        return offset;
    }
    proto_tree_add_item(msmms_common_command_tree, hf_msmms_command_protocol_type, tvb, offset, 4, ENC_ASCII|ENC_NA);
    offset += 4;

    /* Remaining length in multiples of 8 bytes */
    proto_tree_add_item(msmms_common_command_tree, hf_msmms_command_length_remaining, tvb, offset, 4, ENC_LITTLE_ENDIAN);
    offset += 4;

    /* Sequence number */
    sequence_number = tvb_get_letohl(tvb, offset);
    proto_tree_add_item(msmms_common_command_tree, hf_msmms_command_sequence_number, tvb, offset, 4, ENC_LITTLE_ENDIAN);
    offset += 4;

    /* Timestamp */
    proto_tree_add_item(msmms_common_command_tree, hf_msmms_command_timestamp, tvb, offset, 8, ENC_LITTLE_ENDIAN);
    offset += 8;

    /* Another length remaining field... */
    length_remaining = tvb_get_letohl(tvb, offset);
    proto_tree_add_item(msmms_common_command_tree, hf_msmms_command_length_remaining2, tvb, offset, 4, ENC_LITTLE_ENDIAN);
    offset += 4;

    /* Command depends up on direction */
    proto_tree_add_item(msmms_common_command_tree,
                        (command_dir == TO_SERVER) ?
                            hf_msmms_command_to_server_id :
                            hf_msmms_command_to_client_id,
                        tvb, offset, 2, ENC_LITTLE_ENDIAN);
    offset += 2;

    /* Direction */
    proto_tree_add_item(msmms_common_command_tree, hf_msmms_command_direction, tvb, offset, 2, ENC_LITTLE_ENDIAN);
    offset += 2;

    /* This is the end of the common command header */
    proto_item_set_len(msmms_common_command_tree, offset);


    /* Show summary in info column */
    col_append_fstr(pinfo->cinfo, COL_INFO,
                    "seq=%03u: %s %s",
                    sequence_number,
                    (command_dir == TO_SERVER) ? "-->" : "<--",
                    (command_dir == TO_SERVER) ?
                        val_to_str_const(command_id, to_server_command_vals, "Unknown") :
                        val_to_str_const(command_id, to_client_command_vals, "Unknown"));

    /* Adjust length_remaining for command-specific details */
    length_remaining = (length_remaining*8) - 8;

    /* Now parse any command-specific params */
    if (command_dir == TO_SERVER)
    {
        /* Commands to server */
        switch (command_id)
        {
            case SERVER_COMMAND_TRANSPORT_INFO:
                dissect_client_transport_info(tvb, pinfo, msmms_tree,
                                              offset, length_remaining);
                break;
            case SERVER_COMMAND_CONNECT_INFO:
                dissect_client_player_info(tvb, pinfo, msmms_tree,
                                           offset, length_remaining);
                break;
            case SERVER_COMMAND_START_SENDING_FROM:
                dissect_start_sending_from_info(tvb, msmms_tree, offset);
                break;
            case SERVER_COMMAND_CANCEL_PROTOCOL:
                dissect_cancel_info(tvb, msmms_tree, offset);
                break;
            case SERVER_COMMAND_TIMING_TEST_DATA_REQUEST:
                dissect_timing_test_request(tvb, tree, offset);
                break;
            case SERVER_COMMAND_REQUEST_SERVER_FILE:
                dissect_request_server_file(tvb, pinfo, tree, offset, length_remaining);
                break;
            case SERVER_COMMAND_NETWORK_TIMER_TEST_RESPONSE:
                dissect_network_timer_test_response(tvb, tree, offset);
                break;
            case SERVER_COMMAND_MEDIA_STREAM_MBR_SELECTOR:
                dissect_media_stream_mbr_selector(tvb, tree, offset);
                break;
            case SERVER_COMMAND_HEADER_REQUEST:
                dissect_header_request(tvb, tree, offset);
                break;
            case SERVER_COMMAND_STOP_BUTTON_PRESSED:
                dissect_stop_button_pressed(tvb, tree, offset);
                break;

            /* TODO: other commands */

            default:
                break;
        }
    }
    else
    {
        /* Commands to client */
        switch (command_id)
        {
            case CLIENT_COMMAND_SERVER_INFO:
                dissect_server_info(tvb, pinfo, msmms_tree, offset);
                break;
            case CLIENT_COMMAND_TIMING_TEST_DATA_RESPONSE:
                dissect_timing_test_response(tvb, tree, offset);
                break;
            case CLIENT_COMMAND_MEDIA_DETAILS:
                dissect_media_details(tvb, tree, offset);
                break;
            case CLIENT_COMMAND_SENDING_HEADER_RESPONSE:
                dissect_header_response(tvb, tree, offset);
                break;
            case CLIENT_COMMAND_TRANSPORT_INFO_ACK:
                dissect_transport_info_response(tvb, tree, offset, length_remaining);
                break;

            /* TODO: other commands: */
            default:
                break;
        }
    }

    /* Got to the end so assume it belongs to this protocol */
    return length_of_command + 12;
}


/* Parse the only known UDP command (0x01) */
static gint dissect_msmms_data_udp_command(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
    proto_item *ti;
    proto_tree *msmms_tree;
    gint        offset     = 0;

    /* Set protocol column */
    col_set_str(pinfo->cinfo, COL_PROTOCOL, "MSMMS");

    /* Create MSMMS data protocol tree */
    ti = proto_tree_add_item(tree, proto_msmms, tvb, offset, -1, ENC_NA);
    msmms_tree = proto_item_add_subtree(ti, ett_msmms_data);

    /* Header ID */
    proto_tree_add_item(msmms_tree, hf_msmms_data_header_id, tvb, offset, 4, ENC_LITTLE_ENDIAN);
    offset += 4;

    /* Client ID */
    proto_tree_add_item(msmms_tree, hf_msmms_data_client_id, tvb, offset, 4, ENC_LITTLE_ENDIAN);
    offset += 4;

    /* Command ID */
    proto_tree_add_item(msmms_tree, hf_msmms_data_command_id, tvb, offset, 2, ENC_LITTLE_ENDIAN);
    offset += 4;

    col_set_str(pinfo->cinfo, COL_INFO, "Request to resend packet(s):");

    /* Show list of packets to resend */
    while (tvb_reported_length_remaining(tvb, offset) >= 4)
    {
        guint32 packet_number = tvb_get_letohl(tvb, offset);
        proto_tree_add_item(msmms_tree, hf_msmms_data_packet_to_resend, tvb, offset, 4, ENC_LITTLE_ENDIAN);
        offset += 4;

        col_append_fstr(pinfo->cinfo, COL_INFO, " %u", packet_number);
    }

    /* Report that whole of UDP packet was dissected */
    return tvb_reported_length_remaining(tvb, 0);
}


/* Dissect a data packet */
static gint dissect_msmms_data(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
    gint        offset = 0;
    proto_item  *ti;
    proto_tree  *msmms_tree;
    proto_tree  *msmms_data_timing_pair = NULL;
    guint32     sequence_number;
    guint16     packet_length;
    guint16     packet_length_found;
    guint8      value = 0;

    /* How many bytes do we need? */
    packet_length = tvb_get_letohs(tvb, 6);

    /* How many bytes have we got? */
    packet_length_found = tvb_reported_length_remaining(tvb, 0);

    /* Reject frame reported not to reach length field */
    if (packet_length < 8)
    {
        return 0;
    }

    /* Stop and ask for more bytes if necessary */
    if (packet_length_found < packet_length)
    {
        pinfo->desegment_offset = 0;  /* Start from beginning again next time */
        pinfo->desegment_len = DESEGMENT_ONE_MORE_SEGMENT;     /* Try again with even one more byte */
        return -1;
    }


    /* Will reject packet if is TCP and has invalid flag */
    if (pinfo->ptype == PT_TCP)
    {
        /* Flag value is in 5th byte */
        value = tvb_get_guint8(tvb, 5);
        /* Reject packet if not a recognised packet type */
        if (try_val_to_str(value, tcp_flags_vals) == NULL)
        {
            return 0;
        }
    }

    col_set_str(pinfo->cinfo, COL_PROTOCOL, "MSMMS");

    /* Add hidden filter for "msmms.data" */
    ti = proto_tree_add_item(tree, hf_msmms_data, tvb, 0, 0, ENC_NA);
    PROTO_ITEM_SET_HIDDEN(ti);

    /* Create MSMMS data protocol tree */
    ti = proto_tree_add_item(tree, proto_msmms, tvb, offset, -1, ENC_NA);
    msmms_tree = proto_item_add_subtree(ti, ett_msmms_data);

    /* Sequence number */
    sequence_number = tvb_get_letohl(tvb, offset);
    proto_tree_add_item(msmms_tree, hf_msmms_data_sequence_number, tvb, offset, 4, ENC_LITTLE_ENDIAN);
    offset += 4;

    /* Packet ID type */
    proto_tree_add_item(msmms_tree, hf_msmms_data_packet_id_type, tvb, offset, 1, ENC_LITTLE_ENDIAN);
    offset++;

    /* Next byte depends upon whether UDP or TCP */
    if (pinfo->ptype == PT_UDP)
    {
        /* UDP */
        proto_tree_add_item(msmms_tree, hf_msmms_data_udp_sequence, tvb,
                            offset, 1, ENC_LITTLE_ENDIAN);
    }
    else
    {
        /* TCP */
        proto_tree_add_item(msmms_tree, hf_msmms_data_tcp_flags, tvb,
                            offset, 1, ENC_LITTLE_ENDIAN);
    }
    offset++;

    /* Packet Length */
    packet_length = tvb_get_letohs(tvb, offset);
    proto_tree_add_item(msmms_tree, hf_msmms_data_packet_length, tvb, offset, 2, ENC_LITTLE_ENDIAN);
    offset += 2;

    /* Parse UDP Timing packet pair headers if present */
    if (value == 0x01)
    {
        ti =  proto_tree_add_string(msmms_tree, hf_msmms_data_timing_pair, tvb, offset, 8, "");
        msmms_data_timing_pair = proto_item_add_subtree(ti, ett_msmms_data_timing_packet_pair);

        proto_tree_add_item(msmms_data_timing_pair, hf_msmms_data_timing_pair_seqno, tvb, offset, 1, ENC_LITTLE_ENDIAN);
        offset++;
        proto_tree_add_item(msmms_data_timing_pair, hf_msmms_data_timing_pair_flags, tvb, offset, 3, ENC_LITTLE_ENDIAN);
        offset += 3;
        proto_tree_add_item(msmms_data_timing_pair, hf_msmms_data_timing_pair_id, tvb, offset, 1, ENC_LITTLE_ENDIAN);
        offset++;
        proto_tree_add_item(msmms_data_timing_pair, hf_msmms_data_timing_pair_flag, tvb, offset, 1, ENC_LITTLE_ENDIAN);
        offset++;
        proto_tree_add_item(msmms_data_timing_pair, hf_msmms_data_timing_pair_packet_length, tvb, offset, 2, ENC_LITTLE_ENDIAN);
        offset += 2;
    }

    /* Everything else is marked as unparsed data */
    proto_tree_add_item(msmms_tree, hf_msmms_data_unparsed, tvb, offset, packet_length-offset, ENC_NA);
    offset = packet_length;

    /* Show summary in info column */
    col_add_fstr(pinfo->cinfo, COL_INFO, "Data: seq=%05u, len=%05u",
                 sequence_number, packet_length);

    /* Whole of packet length has been dissected now */
    return offset;
}



/***************************************/
/* Dissect command-specific parameters */
/***************************************/

/* Transport information (address, port, etc) */
static void dissect_client_transport_info(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
                                          guint offset, guint length_remaining)
{
    char    *transport_info;
    guint   ipaddr[4];
    char    protocol[3+1] = "";
    guint   port;
    int     fields_matched;

    /* Flags */
    proto_tree_add_item(tree, hf_msmms_command_prefix1, tvb, offset, 4, ENC_LITTLE_ENDIAN);
    offset += 4;
    proto_tree_add_item(tree, hf_msmms_command_prefix2, tvb, offset, 4, ENC_LITTLE_ENDIAN);
    offset += 4;

    /* These 12 bytes are not understood */
    offset += 4;
    offset += 4;
    offset += 4;

    /* Extract and show the string in tree and info column */
    transport_info = tvb_get_string_enc(wmem_packet_scope(), tvb, offset, length_remaining - 20, ENC_UTF_16|ENC_LITTLE_ENDIAN);

    proto_tree_add_string_format(tree, hf_msmms_command_client_transport_info, tvb,
                                 offset, length_remaining-20,
                                 transport_info, "Transport: (%s)", transport_info);

    col_append_fstr(pinfo->cinfo, COL_INFO, " (%s)",
                    format_text(wmem_packet_scope(), (guchar*)transport_info, length_remaining - 20));


    /* Try to extract details from this string */
    fields_matched = sscanf(transport_info, "%*c%*c%u.%u.%u.%u%*c%3s%*c%u",
                            &ipaddr[0], &ipaddr[1], &ipaddr[2], &ipaddr[3],
                            protocol, &port);

    /* Use this information to set up a conversation for the data stream */
    if (fields_matched == 6)
    {
        endpoint_type et = ENDPOINT_NONE;

        /* Work out the port type */
        if (strncmp(protocol, "UDP", 3) == 0)
        {
            et = ENDPOINT_UDP;
        }
        else
        if (strncmp(protocol, "TCP", 3) == 0)
        {
            et = ENDPOINT_TCP;
        }

        /* Set the dissector for indicated conversation */
        if (et != ENDPOINT_NONE)
        {
            guint8 octets[4];
            address addr;
            octets[0] = ipaddr[0];
            octets[1] = ipaddr[1];
            octets[2] = ipaddr[2];
            octets[3] = ipaddr[3];
            addr.type = AT_IPv4;
            addr.len = 4;
            addr.data = octets;
            msmms_data_add_address(pinfo, &addr, et, port);
        }
    }
}

/* Dissect server data */
static void dissect_server_info(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
                                guint offset)
{
    guint32       server_version_length;
    guint32       tool_version_length;
    guint32       download_update_player_length;
    guint32       password_encryption_type_length;
    const guint8 *server_version;

    /* ErrorCode */
    proto_tree_add_item(tree, hf_msmms_command_prefix1_error, tvb, offset, 4, ENC_LITTLE_ENDIAN);
    offset += 4;
    proto_tree_add_item(tree, hf_msmms_command_prefix2, tvb, offset, 4, ENC_LITTLE_ENDIAN);
    offset += 4;

    /* Next 8 words are not understood */
    offset += 4;
    offset += 4;
    offset += 4;
    offset += 4;
    offset += 4;
    offset += 4;
    offset += 4;
    offset += 4;


    /* Length of server version */
    server_version_length = tvb_get_letohl(tvb, offset);
    proto_tree_add_item(tree, hf_msmms_command_server_version_length, tvb, offset, 4, ENC_LITTLE_ENDIAN);
    offset += 4;

    /* Length of tool version */
    tool_version_length = tvb_get_letohl(tvb, offset);
    proto_tree_add_item(tree, hf_msmms_command_tool_version_length, tvb, offset, 4, ENC_LITTLE_ENDIAN);
    offset += 4;

    /* Length of download update player URL */
    download_update_player_length = tvb_get_letohl(tvb, offset);
    proto_tree_add_item(tree, hf_msmms_command_update_url_length, tvb, offset, 4, ENC_LITTLE_ENDIAN);
    offset += 4;

    /* Length of password encryption type */
    password_encryption_type_length = tvb_get_letohl(tvb, offset);
    proto_tree_add_item(tree, hf_msmms_command_password_type_length, tvb, offset, 4, ENC_LITTLE_ENDIAN);
    offset += 4;

    /* Server version string. */
    if (server_version_length > 1)
    {
        /* Server version string */
        proto_tree_add_item_ret_string(tree, hf_msmms_command_server_version, tvb,
                            offset, server_version_length*2,
                            ENC_UTF_16|ENC_LITTLE_ENDIAN, wmem_packet_scope(), &server_version);

        col_append_fstr(pinfo->cinfo, COL_INFO, " (version='%s')",
                    format_text(wmem_packet_scope(), (const guchar*)server_version, strlen(server_version)));
    }
    offset += (server_version_length*2);


    /* Tool version string. */
    if (tool_version_length > 1)
    {
        proto_tree_add_item(tree, hf_msmms_command_tool_version, tvb,
                            offset, tool_version_length*2,
                            ENC_UTF_16|ENC_LITTLE_ENDIAN);
    }
    offset += (tool_version_length*2);

    /* Download update player url string. */
    if (download_update_player_length > 1)
    {
        proto_tree_add_item(tree, hf_msmms_command_update_url, tvb,
                            offset, download_update_player_length*2,
                            ENC_UTF_16|ENC_LITTLE_ENDIAN);
    }
    offset += (download_update_player_length*2);

    /* Password encryption type string. */
    if (password_encryption_type_length > 1)
    {
        proto_tree_add_item(tree, hf_msmms_command_password_type, tvb,
                            offset, password_encryption_type_length*2,
                            ENC_UTF_16|ENC_LITTLE_ENDIAN);
    }
/*    offset += (password_encryption_type_length*2); */
}

/* Player (client) information */
static void dissect_client_player_info(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
                                       guint offset, guint length_remaining)
{
    const guint8 *player_info;

    /* Flags */
    proto_tree_add_item(tree, hf_msmms_command_prefix1, tvb, offset, 4, ENC_LITTLE_ENDIAN);
    offset += 4;
    proto_tree_add_item(tree, hf_msmms_command_prefix2, tvb, offset, 4, ENC_LITTLE_ENDIAN);
    offset += 4;

    /* These 4 bytes are not understood */
    offset += 4;

    /* Extract and show the string in tree and info column */
    proto_tree_add_item_ret_string(tree, hf_msmms_command_client_player_info, tvb,
                        offset, length_remaining-12,
                        ENC_UTF_16|ENC_LITTLE_ENDIAN, wmem_packet_scope(), &player_info);

    col_append_fstr(pinfo->cinfo, COL_INFO, " (%s)",
                    format_text(wmem_packet_scope(), (const guchar*)player_info, strlen(player_info)));
}

/* Dissect info about where client wants to start playing from */
static void dissect_start_sending_from_info(tvbuff_t *tvb, proto_tree *tree, guint offset)
{
    /* Command Level */
    proto_tree_add_item(tree, hf_msmms_command_prefix1_command_level, tvb, offset, 4, ENC_LITTLE_ENDIAN);
    offset += 4;
    proto_tree_add_item(tree, hf_msmms_command_prefix2, tvb, offset, 4, ENC_LITTLE_ENDIAN);

    /* 40 bytes follow the prefixes... */
}

/* Dissect cancel parameters */
static void dissect_cancel_info(tvbuff_t *tvb, proto_tree *tree, guint offset)
{
    /* Command Level */
    proto_tree_add_item(tree, hf_msmms_command_prefix1_command_level, tvb, offset, 4, ENC_LITTLE_ENDIAN);
    offset += 4;
    proto_tree_add_item(tree, hf_msmms_command_prefix2, tvb, offset, 4, ENC_LITTLE_ENDIAN);
}

/* Dissect timing test data request */
static void dissect_timing_test_request(tvbuff_t *tvb, proto_tree *tree, guint offset)
{
    /* Flags */
    proto_tree_add_item(tree, hf_msmms_command_prefix1, tvb, offset, 4, ENC_LITTLE_ENDIAN);
    offset += 4;
    proto_tree_add_item(tree, hf_msmms_command_prefix2, tvb, offset, 4, ENC_LITTLE_ENDIAN);
}

/* Dissect timing test data response */
static void dissect_timing_test_response(tvbuff_t *tvb, proto_tree *tree, guint offset)
{
    /* ErrorCode */
    proto_tree_add_item(tree, hf_msmms_command_prefix1_error, tvb, offset, 4, ENC_LITTLE_ENDIAN);
    offset += 4;
    /* Flags */
    proto_tree_add_item(tree, hf_msmms_command_prefix2, tvb, offset, 4, ENC_LITTLE_ENDIAN);
    offset += 4;

    /* Number of 4 byte fields in structure */
    proto_tree_add_item(tree, hf_msmms_command_number_of_words, tvb, offset, 4, ENC_LITTLE_ENDIAN);
    offset += 4;
    offset += 4;
    offset += 4;

    /* Client ID */
    proto_tree_add_item(tree, hf_msmms_command_client_id, tvb, offset, 4, ENC_LITTLE_ENDIAN);

    /* 20 more bytes... */
}

/* Dissect request for server file */
static void dissect_request_server_file(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
                                        guint offset, guint length_remaining)
{
    const guint8 *server_file;

    /* Command Level */
    proto_tree_add_item(tree, hf_msmms_command_prefix1_command_level, tvb, offset, 4, ENC_LITTLE_ENDIAN);
    offset += 4;
    proto_tree_add_item(tree, hf_msmms_command_prefix2, tvb, offset, 4, ENC_LITTLE_ENDIAN);
    offset += 4;

    offset += 4;
    offset += 4;

    /* File path on server */
    proto_tree_add_item_ret_string(tree, hf_msmms_command_server_file, tvb,
                        offset, length_remaining-16,
                        ENC_UTF_16|ENC_LITTLE_ENDIAN, wmem_packet_scope(), &server_file);

    col_append_fstr(pinfo->cinfo, COL_INFO, " (%s)",
                    format_text(wmem_packet_scope(), (const guchar*)server_file, strlen(server_file)));
}

/* Dissect media details from server */
static void dissect_media_details(tvbuff_t *tvb, proto_tree *tree, guint offset)
{
    /* ErrorCode */
    proto_tree_add_item(tree, hf_msmms_command_prefix1_error, tvb, offset, 4, ENC_LITTLE_ENDIAN);
    offset += 4;
    proto_tree_add_item(tree, hf_msmms_command_prefix2, tvb, offset, 4, ENC_LITTLE_ENDIAN);
    offset += 4;

    /* Result flags */
    proto_tree_add_item(tree, hf_msmms_command_result_flags, tvb, offset, 4, ENC_LITTLE_ENDIAN);
    offset += 4;

    offset += 4;
    offset += 4;

    /* Broadcast flags. */
    proto_tree_add_item(tree, hf_msmms_command_broadcast_indexing, tvb, offset+2, 1, ENC_LITTLE_ENDIAN);
    proto_tree_add_item(tree, hf_msmms_command_broadcast_liveness, tvb, offset+3, 1, ENC_LITTLE_ENDIAN);
    offset += 4;

    /* These 8 bytes may be a time field... */
    offset += 4;
    offset += 4;

    /* Media length in seconds */
    proto_tree_add_item(tree, hf_msmms_command_recorded_media_length, tvb, offset, 4, ENC_LITTLE_ENDIAN);
    offset += 4;

    offset += 4;
    offset += 4;
    offset += 4;
    offset += 4;

    /* Packet length in bytes */
    proto_tree_add_item(tree, hf_msmms_command_media_packet_length, tvb, offset, 4, ENC_LITTLE_ENDIAN);
}

/* Dissect header response */
static void dissect_header_response(tvbuff_t *tvb, proto_tree *tree, guint offset)
{
    /* ErrorCode */
    proto_tree_add_item(tree, hf_msmms_command_prefix1_error, tvb, offset, 4, ENC_LITTLE_ENDIAN);
    offset += 4;
    /* Packet ID type */
    proto_tree_add_item(tree, hf_msmms_command_prefix2, tvb, offset, 4, ENC_LITTLE_ENDIAN);

    /* 8 more bytes */
}

/* Dissect network timer test response */
static void dissect_network_timer_test_response(tvbuff_t *tvb, proto_tree *tree, guint offset)
{
    /* Command Level */
    proto_tree_add_item(tree, hf_msmms_command_prefix1_command_level, tvb, offset, 4, ENC_LITTLE_ENDIAN);
    offset += 4;
    proto_tree_add_item(tree, hf_msmms_command_prefix2, tvb, offset, 4, ENC_LITTLE_ENDIAN);
}

/* Dissect transport info response */
static void dissect_transport_info_response(tvbuff_t *tvb, proto_tree *tree,
                                            guint offset, guint length_remaining)
{
    /* Command Level */
    proto_tree_add_item(tree, hf_msmms_command_prefix1_command_level, tvb, offset, 4, ENC_LITTLE_ENDIAN);
    offset += 4;
    proto_tree_add_item(tree, hf_msmms_command_prefix2, tvb, offset, 4, ENC_LITTLE_ENDIAN);
    offset += 4;

    /* Length */
    proto_tree_add_item(tree, hf_msmms_command_number_of_words, tvb, offset, 4, ENC_LITTLE_ENDIAN);
    offset += 4;

    /* Read this strange string */
    proto_tree_add_item(tree, hf_msmms_command_strange_string, tvb,
                        offset, length_remaining-12,
                        ENC_UTF_16|ENC_LITTLE_ENDIAN);
}

/* Media stream MBR selector */
static void dissect_media_stream_mbr_selector(tvbuff_t *tvb, proto_tree *tree, guint offset)
{
    /* Stream structure count (always 1) */
    proto_tree_add_item(tree, hf_msmms_command_stream_structure_count, tvb, offset, 4, ENC_LITTLE_ENDIAN);
    offset += 4;

    /* Stream selection structure */
    proto_tree_add_item(tree, hf_msmms_stream_selection_flags, tvb, offset, 2, ENC_LITTLE_ENDIAN);
    offset += 2;
    proto_tree_add_item(tree, hf_msmms_stream_selection_stream_id, tvb, offset, 2, ENC_LITTLE_ENDIAN);
    offset += 2;
    proto_tree_add_item(tree, hf_msmms_stream_selection_action, tvb, offset, 2, ENC_LITTLE_ENDIAN);
}

/* Dissect header request */
static void dissect_header_request(tvbuff_t *tvb, proto_tree *tree, guint offset)
{
    gint n;

    /* Command Level */
    proto_tree_add_item(tree, hf_msmms_command_prefix1_command_level, tvb, offset, 4, ENC_LITTLE_ENDIAN);
    offset += 4;
    proto_tree_add_item(tree, hf_msmms_command_prefix2, tvb, offset, 4, ENC_LITTLE_ENDIAN);
    offset += 4;

    /* Skip 8 unknown words */
    for (n=0; n < 8; n++)
    {
        offset += 4;
    }

    /* Header packet ID type */
    proto_tree_add_item(tree, hf_msmms_command_header_packet_id_type, tvb, offset, 4, ENC_LITTLE_ENDIAN);
}

/* Dissect stop button pressed */
static void dissect_stop_button_pressed(tvbuff_t *tvb, proto_tree *tree, guint offset)
{
    /* Command Level */
    proto_tree_add_item(tree, hf_msmms_command_prefix1_command_level, tvb, offset, 4, ENC_LITTLE_ENDIAN);
    offset += 4;
    proto_tree_add_item(tree, hf_msmms_command_prefix2, tvb, offset, 4, ENC_LITTLE_ENDIAN);
}


/********************************************************/
/* Helper function to set up an MS-MMS data conversation */
/********************************************************/
static void msmms_data_add_address(packet_info *pinfo, address *addr, endpoint_type et, int port)
{
    address         null_addr;
    conversation_t *p_conv;

    /* If this isn't the first time this packet has been processed,
     * we've already done this work, so we don't need to do it
     * again. */
    if (pinfo->fd->visited)
    {
        return;
    }

    clear_address(&null_addr);

    /* Check if the ip address and port combination is not
     * already registered as a conversation. */
    p_conv = find_conversation(pinfo->num, addr, &null_addr, et, port, 0,
                               NO_ADDR_B | NO_PORT_B);

    /* If not, create a new conversation. */
    if (!p_conv)
    {
        p_conv = conversation_new(pinfo->num, addr, &null_addr, et,
                                  (guint32)port, 0, NO_ADDR2 | NO_PORT2);
    }

    /* Set dissector */
    conversation_set_dissector(p_conv, msmms_handle);
}



/*************************/
/* Register protocol     */
/*************************/

void proto_register_msmms(void)
{
    static hf_register_info hf[] =
    {

        /* Command fields */
        {
            &hf_msmms_command,
            {
                "Command",
                "msmms.command",
                FT_STRING,
                BASE_NONE,
                NULL,
                0x0,
                "MSMMS command hidden filter", HFILL
            }
        },
        {
            &hf_msmms_command_common_header,
            {
                "Command common header",
                "msmms.command.common-header",
                FT_STRING,
                BASE_NONE,
                NULL,
                0x0,
                "MSMMS command common header", HFILL
            }
        },

#if 0
        {
            &hf_msmms_command_version,
            {
                "Version",
                "msmms.command.version",
                FT_UINT32,
                BASE_DEC,
                NULL,
                0x0,
                NULL, HFILL
            }
        },
#endif
        {
            &hf_msmms_command_signature,
            {
                "Command signature",
                "msmms.command.signature",
                FT_UINT32,
                BASE_HEX,
                NULL,
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_command_length,
            {
                "Command length",
                "msmms.command.length",
                FT_UINT32,
                BASE_DEC,
                NULL,
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_command_protocol_type,
            {
                "Protocol type",
                "msmms.command.protocol-type",
                FT_STRING,
                BASE_NONE,
                NULL,
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_command_length_remaining,
            {
                "Length until end (8-byte blocks)",
                "msmms.command.length-remaining",
                FT_UINT32,
                BASE_DEC,
                NULL,
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_command_sequence_number,
            {
                "Sequence number",
                "msmms.command.sequence-number",
                FT_UINT32,
                BASE_DEC,
                NULL,
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_command_timestamp,
            {
                "Time stamp (s)",
                "msmms.command.timestamp",
                FT_DOUBLE,
                BASE_NONE,
                NULL,
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_command_length_remaining2,
            {
                "Length until end (8-byte blocks)",
                "msmms.command.length-remaining2",
                FT_UINT32,
                BASE_DEC,
                NULL,
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_command_to_server_id,
            {
                "Command",
                "msmms.command.to-server-id",
                FT_UINT16,
                BASE_HEX,
                VALS(to_server_command_vals),
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_command_to_client_id,
            {
                "Command",
                "msmms.command.to-client-id",
                FT_UINT16,
                BASE_HEX,
                VALS(to_client_command_vals),
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_command_direction,
            {
                "Command direction",
                "msmms.command.direction",
                FT_UINT16,
                BASE_HEX,
                VALS(command_direction_vals),
                0x0,
                NULL, HFILL
            }
        },

        {
            &hf_msmms_command_prefix1,
            {
                "Prefix 1",
                "msmms.command.prefix1",
                FT_UINT32,
                BASE_HEX,
                NULL,
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_command_prefix1_error,
            {
                "Prefix 1 ErrorCode",
                "msmms.command.prefix1-error-code",
                FT_UINT32,
                BASE_HEX,
                VALS(server_to_client_error_vals),
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_command_prefix1_command_level,
            {
                "Prefix 1 Command Level",
                "msmms.command.prefix1-command-level",
                FT_UINT32,
                BASE_DEC,
                NULL,
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_command_prefix2,
            {
                "Prefix 2",
                "msmms.command.prefix2",
                FT_UINT32,
                BASE_HEX,
                NULL,
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_command_client_transport_info,
            {
                "Client transport info",
                "msmms.command.client-transport-info",
                FT_STRING,
                BASE_NONE,
                NULL,
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_command_client_player_info,
            {
                "Player info",
                "msmms.command.player-info",
                FT_STRING,
                BASE_NONE,
                NULL,
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_command_server_version_length,
            {
                "Server Version Length",
                "msmms.command.server-version-length",
                FT_UINT32,
                BASE_DEC,
                NULL,
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_command_tool_version_length,
            {
                "Tool Version Length",
                "msmms.command.tool-version-length",
                FT_UINT32,
                BASE_DEC,
                NULL,
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_command_update_url_length,
            {
                "Download update URL length",
                "msmms.command.download-update-player-url-length",
                FT_UINT32,
                BASE_DEC,
                NULL,
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_command_password_type_length,
            {
                "Password encryption type length",
                "msmms.command.password-encryption-type-length",
                FT_UINT32,
                BASE_DEC,
                NULL,
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_command_server_version,
            {
                "Server version",
                "msmms.command.server-version",
                FT_STRING,
                BASE_NONE,
                NULL,
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_command_tool_version,
            {
                "Tool version",
                "msmms.command.tool-version",
                FT_STRING,
                BASE_NONE,
                NULL,
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_command_update_url,
            {
                "Download update player URL",
                "msmms.command.download-update-player-url",
                FT_STRING,
                BASE_NONE,
                NULL,
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_command_password_type,
            {
                "Password encryption type",
                "msmms.command.password-encryption-type",
                FT_STRING,
                BASE_NONE,
                NULL,
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_command_number_of_words,
            {
                "Number of 4 byte fields in structure",
                "msmms.data.words-in-structure",
                FT_UINT32,
                BASE_DEC,
                NULL,
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_command_client_id,
            {
                "Client ID",
                "msmms.data.client-id",
                FT_UINT32,
                BASE_DEC,
                NULL,
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_command_server_file,
            {
                "Server file",
                "msmms.command.server-file",
                FT_STRING,
                BASE_NONE,
                NULL,
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_command_result_flags,
            {
                "Result flags",
                "msmms.command.result-flags",
                FT_UINT32,
                BASE_HEX,
                VALS(media_result_flags_vals),
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_command_broadcast_indexing,
            {
                "Broadcast indexing",
                "msmms.command.broadcast-indexing",
                FT_UINT8,
                BASE_HEX,
                VALS(broadcast_indexing_vals),
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_command_broadcast_liveness,
            {
                "Broadcast liveness",
                "msmms.command.broadcast-liveness",
                FT_UINT8,
                BASE_HEX,
                VALS(broadcast_liveness_vals),
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_command_recorded_media_length,
            {
                "Pre-recorded media length (seconds)",
                "msmms.data.prerecorded-media-length",
                FT_UINT32,
                BASE_DEC,
                NULL,
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_command_media_packet_length,
            {
                "Media packet length (bytes)",
                "msmms.data.media-packet-length",
                FT_UINT32,
                BASE_DEC,
                NULL,
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_command_strange_string,
            {
                "Strange string",
                "msmms.command.strange-string",
                FT_STRING,
                BASE_NONE,
                NULL,
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_command_stream_structure_count,
            {
                "Stream structure count",
                "msmms.data.stream-structure-count",
                FT_UINT32,
                BASE_DEC,
                NULL,
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_stream_selection_flags,
            {
                "Stream selection flags",
                "msmms.data.stream-selection-flags",
                FT_UINT16,
                BASE_HEX,
                NULL,
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_stream_selection_stream_id,
            {
                "Stream id",
                "msmms.data.selection-stream-id",
                FT_UINT16,
                BASE_DEC,
                NULL,
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_stream_selection_action,
            {
                "Action",
                "msmms.data.selection-stream-action",
                FT_UINT16,
                BASE_DEC,
                VALS(stream_selection_action_vals),
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_command_header_packet_id_type,
            {
                "Header packet ID type",
                "msmms.data.header-packet-id-type",
                FT_UINT32,
                BASE_HEX,
                NULL,
                0x0,
                NULL, HFILL
            }
        },


        /* Data fields */
        {
            &hf_msmms_data,
            {
                "Data",
                "msmms.data",
                FT_NONE,
                BASE_NONE,
                NULL,
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_data_sequence_number,
            {
                "Sequence number",
                "msmms.data.sequence",
                FT_UINT32,
                BASE_DEC,
                NULL,
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_data_packet_id_type,
            {
                "Packet ID type",
                "msmms.data.packet-id-type",
                FT_UINT8,
                BASE_HEX,
                NULL,
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_data_packet_length,
            {
                "Packet length",
                "msmms.data.packet-length",
                FT_UINT16,
                BASE_DEC,
                NULL,
                0x0,
                NULL, HFILL
            }
        },

        {
            &hf_msmms_data_header_id,
            {
                "Header ID",
                "msmms.data.header-id",
                FT_UINT32,
                BASE_HEX,
                NULL,
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_data_client_id,
            {
                "Client ID",
                "msmms.data.client-id",
                FT_UINT32,
                BASE_HEX,
                NULL,
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_data_command_id,
            {
                "Command ID",
                "msmms.data.command-id",
                FT_UINT16,
                BASE_DEC,
                NULL,
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_data_packet_to_resend,
            {
                "Packet to resend",
                "msmms.data.packet-to-resend",
                FT_UINT32,
                BASE_DEC,
                NULL,
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_data_udp_sequence,
            {
                "UDP Sequence",
                "msmms.data.udp-sequence",
                FT_UINT8,
                BASE_DEC,
                NULL,
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_data_tcp_flags,
            {
                "TCP flags",
                "msmms.data.tcp-flags",
                FT_UINT8,
                BASE_HEX,
                VALS(tcp_flags_vals),
                0x0,
                NULL, HFILL
            }
        },

        {
            &hf_msmms_data_timing_pair,
            {
                "Data timing pair",
                "msmms.data.timing-pair",
                FT_NONE,
                BASE_NONE,
                NULL,
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_data_timing_pair_seqno,
            {
                "Sequence number",
                "msmms.data.timing-pair.sequence-number",
                FT_UINT8,
                BASE_DEC,
                NULL,
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_data_timing_pair_flags,
            {
                "Flags",
                "msmms.data.timing-pair.flags",
                FT_UINT24,
                BASE_DEC,
                NULL,
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_data_timing_pair_id,
            {
                "ID",
                "msmms.data.timing-pair.id",
                FT_UINT8,
                BASE_HEX,
                NULL,
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_data_timing_pair_flag,
            {
                "Flag",
                "msmms.data.timing-pair.flag",
                FT_UINT8,
                BASE_HEX,
                NULL,
                0x0,
                NULL, HFILL
            }
        },
        {
            &hf_msmms_data_timing_pair_packet_length,
            {
                "Packet length",
                "msmms.data.timing-pair.packet-length",
                FT_UINT16,
                BASE_HEX,
                NULL,
                0x0,
                NULL, HFILL
            }
        },

        /* Unparsed data */
        {
            &hf_msmms_data_unparsed,
            {
                "Unparsed data",
                "msmms.data.unparsed",
                FT_NONE,
                BASE_NONE,
                NULL,
                0x0,
                NULL, HFILL
            }
        },

    };

    static gint *ett[] =
    {
        &ett_msmms_command,
        &ett_msmms_command_common_header,
        &ett_msmms_data,
        &ett_msmms_data_timing_packet_pair
    };

    /* Register protocol and fields */
    proto_msmms = proto_register_protocol("Microsoft Media Server", "MSMMS", "msmms");
    proto_register_field_array(proto_msmms, hf, array_length(hf));
    proto_register_subtree_array(ett, array_length(ett));
    msmms_handle = register_dissector("msmms", dissect_msmms_pdu, proto_msmms);
}

void proto_reg_handoff_msmms_command(void)
{
    /* Control commands using TCP port */
    dissector_add_uint_with_preference("tcp.port", MSMMS_PORT, msmms_handle);
    /* Data command(s) using UDP port */
    dissector_add_uint_with_preference("udp.port", MSMMS_PORT, msmms_handle);
}

/*
 * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 4
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * vi: set shiftwidth=4 tabstop=8 expandtab:
 * :indentSize=4:tabSize=8:noTabs=true:
 */