aboutsummaryrefslogtreecommitdiffstats
path: root/packet-sip.c
blob: df27440cbeea2ef3a6309d4f7c26ca531667c45b (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
/* packet-sip.c
 * Routines for the Session Initiation Protocol (SIP) dissection.
 * RFCs 3261-3264
 *
 * TODO: Pay attention to Content-Type: It might not always be SDP.
 *	Content-Type is fixed, mixed/mode is not handled though.
 *       hf_ display filters for headers of SIP extension RFCs:
 *		Done for RFC 3265, RFC 3262
 *		Use hash table for list of headers
 *       Add sip msg body dissection based on Content-Type for:
 *                SDP, MIME, and other types
 *       Align SIP methods with recent Internet Drafts or RFC
 *               (SIP INFO, rfc2976 - done)
 *               (SIP SUBSCRIBE-NOTIFY - done)
 *               (SIP REFER - done)
 *               check for other
 *
 * Copyright 2000, Heikki Vatiainen <hessu@cs.tut.fi>
 * Copyright 2001, Jean-Francois Mule <jfm@cablelabs.com>
 *
 * $Id: packet-sip.c,v 1.64 2004/04/14 04:45:10 etxrab Exp $
 *
 * Ethereal - Network traffic analyzer
 * By Gerald Combs <gerald@ethereal.com>
 * Copyright 1998 Gerald Combs
 *
 * Copied from packet-cops.c
 *
 * 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>
#include <string.h>
#include <ctype.h>

#include "prefs.h"

#include <glib.h>
#include <epan/packet.h>

#include "packet-sip.h"
#include "tap.h"

#define TCP_PORT_SIP 5060
#define UDP_PORT_SIP 5060

static gint sip_tap = -1;

/* Initialize the protocol and registered fields */
static gint proto_sip = -1;
static gint proto_raw_sip = -1;
static gint hf_msg_hdr = -1;
static gint hf_Method = -1;
static gint hf_Request_Line = -1;
static gint hf_Status_Code = -1;
static gint hf_Status_Line = -1;
static gint hf_sip_to_addr = -1;
static gint hf_sip_from_addr = -1;
static gint hf_sip_tag = -1;

/* Initialize the subtree pointers */
static gint ett_sip 		= -1;
static gint ett_sip_reqresp 	= -1;
static gint ett_sip_hdr 	= -1;
static gint ett_raw_text 	= -1;
static gint ett_sip_element 	= -1;
static gint ett_sip_message_body = -1;

/* PUBLISH method added as per http://www.ietf.org/internet-drafts/draft-ietf-sip-publish-01.txt */
static const char *sip_methods[] = {
        "<Invalid method>",      /* Pad so that the real methods start at index 1 */
        "ACK",
        "BYE",
        "CANCEL",
        "DO",
        "INFO",
        "INVITE",
        "MESSAGE",
        "NOTIFY",
        "OPTIONS",
        "PRACK",
        "QAUTH",
        "REFER",
        "REGISTER",
        "SPRACK",
        "SUBSCRIBE",
        "UPDATE",
        "PUBLISH"
};

/* from RFC 3261 */
/* Updated with info from http://www.iana.org/assignments/sip-parameters */
/* (last updated 2003-10-31) */
/* Added two headsers ( Etag and If-Match )from http://www.ietf.org/internet-drafts/draft-ietf-sip-publish-01.txt */
typedef struct {
        char *name;
        char *compact_name;
} sip_header_t;
static const sip_header_t sip_headers[] = {
                { "Unknown-header", 		NULL }, /* Pad so that the real headers start at index 1 */
                { "Accept", 			NULL },
                { "Accept-Encoding", 		NULL },
                { "Accept-Language", 		NULL },
                { "Alert-Info", 		NULL },
                { "Allow", 			NULL },
                { "Allow-Events", 		NULL },
                { "Authentication-Info", 	NULL },
                { "Authorization", 		NULL },
                { "Call-ID", 			"i"  },
                { "Call-Info", 			NULL },
                { "Contact", 			"m"  },
                { "Content-Disposition", 	NULL },
                { "Content-Encoding", 		"e"  },
                { "Content-Language", 		NULL },
                { "Content-Length", 		"l"  },
                { "Content-Type", 		"c"  },
                { "CSeq", 			NULL },
                { "Date", 			NULL },
                { "Error-Info", 		NULL },
                { "Event", 			"o"  },
                { "Expires", 			NULL },
                { "From", 			"f"  },
                { "In-Reply-To", 		NULL },
                { "Max-Forwards", 		NULL },
                { "MIME-Version", 		NULL },
                { "Min-Expires", 		NULL },
                { "Organization", 		NULL },
                { "Priority", 			NULL },
                { "Proxy-Authenticate", 	NULL },
                { "Proxy-Authorization", 	NULL },
                { "Proxy-Require", 		NULL },
                { "RAck", 			NULL },
                { "RSeq", 			NULL },
                { "Record-Route", 		NULL },
                { "Reply-To", 			NULL },
                { "Require", 			NULL },
                { "Retry-After", 		NULL },
                { "Route", 			NULL },
                { "Server", 			NULL },
                { "Subject", 			"s"  },
                { "Subscription-State", 	NULL },
                { "Supported", 			"k" },
                { "Timestamp", 			NULL },
                { "To", 			"t"  },
                { "Unsupported", 		NULL },
                { "User-Agent", 		NULL },
                { "Via", 			"v" },
                { "Warning", 			NULL },
                { "WWW-Authenticate", 		NULL },
                { "P-Access-Network-Info",	NULL },  /*  RFC3455  */
                { "P-Asserted-Identity",        NULL },  /*  RFC3325  */
                { "P-Associated-URI",           NULL },  /*  RFC3455  */
                { "P-Called-Party-ID",          NULL },  /*  RFC3455  */
                { "P-Charging-Function-Addresses",NULL }, /*  RFC3455  */
                { "P-Charging-Vector",          NULL },  /*  RFC3455  */
                { "P-DCS-Trace-Party-ID",       NULL },  /*  RFC3603  */
                { "P-DCS-OSPS",                 NULL },  /*  RFC3603  */
                { "P-DCS-Billing-Info",         NULL },  /*  RFC3603  */
                { "P-DCS-LAES",                 NULL },  /*  RFC3603  */
                { "P-DCS-Redirect",             NULL },  /*  RFC3603  */
                { "P-Media-Authorization",      NULL },  /*  RFC3313  */
                { "P-Preferred-Identity",       NULL },  /*  RFC3325  */
                { "P-Visited-Network-ID",       NULL },  /*  RFC3455  */
                { "Path",                       NULL },  /*  RFC3327  */
                { "Privacy",                    NULL },  /*  RFC3323  */
                { "Reason",                     NULL },  /*  RFC3326  */
                { "Refer-To",					"r"  },  /*  RFC3515  */
                { "Service-Route",				NULL },  /*  RFC3608  */
                { "SIP-ETag",					NULL },  /*  draft-ietf-sip-publish-03  */
                { "SIP-If-Match",				NULL },  /*  draft-ietf-sip-publish-03  */

};


#define POS_ACCEPT 			1
#define POS_ACCEPT_ENCODING		2
#define POS_ACCEPT_LANGUAGE		3
#define POS_ALERT_INFO			4
#define POS_ALLOW			5
#define POS_ALLOW_EVENTS		6
#define POS_AUTHENTICATION_INFO		7
#define POS_AUTHORIZATION		8
#define POS_CALL_ID			9
#define POS_CALL_INFO			10
#define POS_CONTACT			11
#define POS_CONTENT_DISPOSITION		12
#define POS_CONTENT_ENCODING		13
#define POS_CONTENT_LANGUAGE		14
#define POS_CONTENT_LENGTH		15
#define POS_CONTENT_TYPE		16
#define POS_CSEQ			17
#define POS_DATE			18
#define POS_ERROR_INFO			19
#define POS_EVENT			20
#define POS_EXPIRES			21
#define POS_FROM			22
#define POS_IN_REPLY_TO			23
#define POS_MAX_FORWARDS		24
#define POS_MIME_VERSION		25
#define POS_MIN_EXPIRES			26
#define POS_ORGANIZATION		27
#define POS_PRIORITY			28
#define POS_PROXY_AUTHENTICATE		29
#define POS_PROXY_AUTHORIZATION		30
#define POS_PROXY_REQUIRE		31
#define POS_RACK			32
#define POS_RSEQ			33
#define POS_RECORD_ROUTE		34
#define POS_REPLY_TO			35
#define POS_REQUIRE			36
#define POS_RETRY_AFTER			37
#define POS_ROUTE			38
#define POS_SERVER			39
#define POS_SUBJECT			40
#define POS_SUBSCRIPTION_STATE		41
#define POS_SUPPORTED			42
#define POS_TIMESTAMP			43
#define POS_TO				44
#define POS_UNSUPPORTED			45
#define POS_USER_AGENT			46
#define POS_VIA				47
#define POS_WARNING			48
#define POS_WWW_AUTHENTICATE		49

#define POS_P_ACCESS_NETWORK_INFO	50
#define POS_P_ASSERTED_IDENTITY         51
#define POS_P_ASSOCIATED_URI            52
#define POS_P_CALLED_PARTY_ID           53
#define POS_P_CHARGING_FUNCTION_ADDRESSES 54
#define POS_P_CHARGING_VECTOR           55
#define POS_P_DCS_TRACE_PARTY_ID        56
#define POS_P_DCS_OSPS                  57
#define POS_P_DCS_BILLING_INFO          58
#define POS_P_DCS_LAES                  59
#define POS_P_DCS_REDIRECT              60
#define POS_P_MEDIA_AUTHORIZATION       61
#define POS_P_PREFERRED_IDENTITY        62
#define POS_P_VISITED_NETWORK_ID        63
#define POS_PATH                        64
#define POS_PRIVACY                     65
#define POS_REASON                      66
#define POS_REFER_TO                    67
#define POS_SERVICE_ROUTE               68
#define POS_SIP_ETAG					69
#define POS_SIP_IF_MATCH				70

static gint hf_header_array[] = {
		-1, /* "Unknown-header" - Pad so that the real headers start at index 1 */
                -1, /* "Accept" */
                -1, /* "Accept-Encoding" */
                -1, /* "Accept-Language" */
                -1, /* "Alert-Info" */
                -1, /* "Allow" */
		-1, /* "Allow-Events" - RFC 3265 */
                -1, /* "Authentication-Info" */
                -1, /* "Authorization" */
                -1, /* "Call-ID" */
                -1, /* "Call-Info" */
                -1, /* "Contact" */
                -1, /* "Content-Disposition" */
                -1, /* "Content-Encoding" */
                -1, /* "Content-Language" */
                -1, /* "Content-Length" */
                -1, /* "Content-Type" */
                -1, /* "CSeq" */
                -1, /* "Date" */
                -1, /* "Error-Info" */
                -1, /* "Expires" */
		-1, /* "Event" - RFC 3265 */
                -1, /* "From" */
                -1, /* "In-Reply-To" */
                -1, /* "Max-Forwards" */
                -1, /* "MIME-Version" */
                -1, /* "Min-Expires" */
                -1, /* "Organization" */
                -1, /* "Priority" */
                -1, /* "Proxy-Authenticate" */
                -1, /* "Proxy-Authorization" */
                -1, /* "Proxy-Require" */
		-1, /* "RAck" - RFC 3262 */
		-1, /* "RSeq" - RFC 3261 */
                -1, /* "Record-Route" */
                -1, /* "Reply-To" */
                -1, /* "Require" */
                -1, /* "Retry-After" */
                -1, /* "Route" */
                -1, /* "Server" */
                -1, /* "Subject" */
		-1, /* "Subscription-State" - RFC 3265 */
                -1, /* "Supported" */
                -1, /* "Timestamp" */
                -1, /* "To" */
                -1, /* "Unsupported" */
                -1, /* "User-Agent" */
                -1, /* "Via" */
                -1, /* "Warning" */
                -1,  /* "WWW-Authenticate" */
                -1,  /* "P-Access-Network-Info"	-   RFC3455  */
                -1,  /* "P-Asserted-Identity" 	-   RFC3325  */
                -1,  /* "P-Associated-URI" 	-   RFC3455  */
                -1,  /* "P-Called-Party-ID" 	-   RFC3455  */
                -1,  /* "P-Charging-Function-Addresses" -   RFC3455  */
                -1,  /* "P-Charging-Vector" 	-   RFC3455  */
                -1,  /* "P-DCS-Trace-Party-ID" 	-   RFC3603  */
                -1,  /* "P-DCS-OSPS" 		-   RFC3603  */
                -1,  /* "P-DCS-Billing-Info" 	-   RFC3603  */
                -1,  /* "P-DCS-LAES" 		-   RFC3603  */
                -1,  /* "P-DCS-Redirect" 	-   RFC3603  */
                -1,  /* "P-Media-Authorization" 	-   RFC3313  */
                -1,  /* "P-Preferred-Identity" 	-   RFC3325  */
                -1,  /* "P-Visited-Network-ID" 	-   RFC3455  */
                -1,  /* "Path" 			-   RFC3327  */
                -1,  /* "Privacy" 		-   RFC3323  */
                -1,  /* "Reason" 		-   RFC3326  */
                -1,  /* "Refer-To" 		-   RFC3515  */
                -1,  /* "Service-Route" 		-   RFC3608  */
                -1,  /* "ETag"     draft-ietf-sip-publish-01  */
                -1,  /* "If-Match  draft-ietf-sip-publish-01  */

};

/*
 * Type of line.  It's either a SIP Request-Line, a SIP Status-Line, or
 * another type of line.
 */
typedef enum {
	REQUEST_LINE,
	STATUS_LINE,
	OTHER_LINE
} line_type_t;

/* global_sip_raw_text determines whether we are going to display		*/
/* the raw text of the SIP message, much like the MEGACO dissector does.	*/
static gboolean global_sip_raw_text = FALSE;
/* strict_sip_version determines whether the SIP dissector enforces
 * the SIP version to be "SIP/2.0". */
static gboolean strict_sip_version = TRUE;

static gboolean dissect_sip_common(tvbuff_t *tvb, packet_info *pinfo,
    proto_tree *tree, gboolean is_heur);
static line_type_t sip_parse_line(tvbuff_t *tvb, gint linelen,
    guint *token_1_len);
static gboolean sip_is_known_request(tvbuff_t *tvb, int meth_offset,
    guint meth_len, guint *meth_idx);
static gint sip_is_known_sip_header(tvbuff_t *tvb, int offset,
    guint header_len);
static void dfilter_sip_request_line(tvbuff_t *tvb, proto_tree *tree,
    guint meth_len);
static void dfilter_sip_status_line(tvbuff_t *tvb, proto_tree *tree);
static void tvb_raw_text_add(tvbuff_t *tvb, proto_tree *tree);

/* SIP content type and internet media type used by other dissectors
 * are the same.  List of media types from IANA at:
 * http://www.iana.org/assignments/media-types/index.html */
static dissector_table_t media_type_dissector_table;

#define SIP2_HDR "SIP/2.0"
#define SIP2_HDR_LEN (strlen (SIP2_HDR))

/* Store the info needed by the SIP tap for one packet */
static sip_info_value_t *stat_info;

/* Code to actually dissect the packets */
static int
dissect_sip(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
	if (!dissect_sip_common(tvb, pinfo, tree, FALSE))
		return 0;

	return tvb_length(tvb);
}

static void
dissect_sip_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
	dissect_sip_common(tvb, pinfo, tree, TRUE);
}

static gboolean
dissect_sip_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
	return dissect_sip_common(tvb, pinfo, tree, FALSE);
}

static gboolean
dissect_sip_common(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
    gboolean dissect_other_as_continuation)
{
        int offset;
        gint next_offset, linelen;
	line_type_t line_type;
        tvbuff_t *next_tvb;
        gboolean is_known_request;
	gboolean found_match = FALSE;
        char *descr;
        guint token_1_len;
	guint current_method_idx = 0;
        proto_item *ts = NULL, *ti = NULL, *th = NULL, *sip_element_item = NULL;
        proto_tree *sip_tree = NULL, *reqresp_tree = NULL , *hdr_tree = NULL, *sip_element_tree = NULL, *message_body_tree = NULL;
	guchar contacts = 0, contact_is_star = 0, expires_is_0 = 0;
	char csec_method[16] = "";
	char *media_type_str = NULL;
	char *media_type_str_lower_case = NULL;
	char *content_type_parameter_str = NULL;

	/* Initialise stat info for passing to tap */
	stat_info = g_malloc(sizeof(sip_info_value_t));
	stat_info->response_code = 0;
	stat_info->request_method = NULL;

        /*
         * Note that "tvb_find_line_end()" will return a value that
         * is not longer than what's in the buffer, so the
         * "tvb_get_ptr()" calls below won't throw exceptions.
         *
         * Note that "tvb_strneql()" doesn't throw exceptions, so
         * "sip_parse_line()" won't throw an exception.
         */
        offset = 0;
        linelen = tvb_find_line_end(tvb, 0, -1, &next_offset, FALSE);
        line_type = sip_parse_line(tvb, linelen, &token_1_len);
        if (line_type == OTHER_LINE) {
        	/*
        	 * This is neither a SIP request nor response.
        	 */
                if (!dissect_other_as_continuation) {
                        /*
                         * We were asked to reject this.
                         */
                	return FALSE;
                }

                /*
                 * Just dissect it as a continuation.
                 */
        }

        if (check_col(pinfo->cinfo, COL_PROTOCOL))
                col_set_str(pinfo->cinfo, COL_PROTOCOL, "SIP");

        switch (line_type) {

        case REQUEST_LINE:
                is_known_request = sip_is_known_request(tvb, 0, token_1_len, &current_method_idx);
                descr = is_known_request ? "Request" : "Unknown request";
                if (check_col(pinfo->cinfo, COL_INFO)) {
                        col_add_fstr(pinfo->cinfo, COL_INFO, "%s: %s",
                             descr,
                             tvb_format_text(tvb, 0, linelen - SIP2_HDR_LEN - 1));
                }
		break;

        case STATUS_LINE:
                descr = "Status";
                if (check_col(pinfo->cinfo, COL_INFO)) {
                        col_add_fstr(pinfo->cinfo, COL_INFO, "Status: %s",
                             tvb_format_text(tvb, SIP2_HDR_LEN + 1, linelen - SIP2_HDR_LEN - 1));
                }
		break;

	case OTHER_LINE:
	default: /* Squelch compiler complaints */
	        descr = "Continuation";
                if (check_col(pinfo->cinfo, COL_INFO))
                        col_set_str(pinfo->cinfo, COL_INFO, "Continuation");
                break;
        }

        if (tree) {
                ts = proto_tree_add_item(tree, proto_sip, tvb, 0, -1, FALSE);
                sip_tree = proto_item_add_subtree(ts, ett_sip);
	}

	switch (line_type) {

	case REQUEST_LINE:
		if (sip_tree) {
	        	ti = proto_tree_add_string(sip_tree, hf_Request_Line, tvb, 0, linelen,
	                              tvb_format_text(tvb, 0, linelen));
	        	reqresp_tree = proto_item_add_subtree(ti, ett_sip_reqresp);
	        }
	        dfilter_sip_request_line(tvb, reqresp_tree, token_1_len);
	        break;

	case STATUS_LINE:
		if (sip_tree) {
	        	ti = proto_tree_add_string(sip_tree, hf_Status_Line, tvb, 0, linelen,
	       	                      tvb_format_text(tvb, 0, linelen));
	        	reqresp_tree = proto_item_add_subtree(ti, ett_sip_reqresp);
	        }
	        dfilter_sip_status_line(tvb, reqresp_tree);
	        break;

	case OTHER_LINE:
		if (sip_tree) {
	        	ti = proto_tree_add_text(sip_tree, tvb, 0, next_offset,
		                         "%s line: %s", descr,
	                                 tvb_format_text(tvb, 0, linelen));
	        	reqresp_tree = proto_item_add_subtree(ti, ett_sip_reqresp);
	        	proto_tree_add_text(sip_tree, tvb, 0, -1,
	            		"Continuation data");
	        }
	        return TRUE;
	}

	offset = next_offset;
	if (sip_tree) {
		th = proto_tree_add_item(sip_tree, hf_msg_hdr, tvb, offset, -1, FALSE);
		hdr_tree = proto_item_add_subtree(th, ett_sip_hdr);
	}

        /*
         * Process the headers - if we're not building a protocol tree,
         * we just do this to find the blank line separating the
         * headers from the message body.
         */
        next_offset = offset;
        while (tvb_reported_length_remaining(tvb, offset) > 0) {
                gint line_end_offset;
                gint colon_offset;
                gint semi_colon_offset;
                gint parameter_offset;
		gint content_type_len, content_type_parameter_str_len;
                gint header_len;
                gint hf_index;
                gint value_offset,tag_offset;
                guchar c;
		size_t value_len;
                char *value;

                linelen = tvb_find_line_end(tvb, offset, -1, &next_offset,
                    FALSE);
                if (linelen == 0) {
                        /*
                         * This is a blank line separating the
                         * message header from the message body.
                         */
                        break;
                }
                line_end_offset = offset + linelen;
		colon_offset = tvb_find_guint8(tvb, offset, linelen, ':');
		if (colon_offset == -1) {
			/*
			 * Malformed header - no colon after the name.
			 */
			if(hdr_tree) {
				proto_tree_add_text(hdr_tree, tvb, offset,
				    next_offset - offset, "%s",
				    tvb_format_text(tvb, offset, linelen));
			}
		} else {
			header_len = colon_offset - offset;
			hf_index = sip_is_known_sip_header(tvb,
			    offset, header_len);

			if (hf_index == -1) {
				if(hdr_tree) {
					proto_tree_add_text(hdr_tree, tvb,
					    offset, next_offset - offset, "%s",
					    tvb_format_text(tvb, offset, linelen));
				}
			} else {
				/*
				 * Skip whitespace after the colon.
				 */
				value_offset = colon_offset + 1;
				while (value_offset < line_end_offset
				    && ((c = tvb_get_guint8(tvb,
					    value_offset)) == ' '
				      || c == '\t'))
					value_offset++;
				/*
				 * Fetch the value.
				 */
				value_len = line_end_offset - value_offset;
				value = tvb_get_string(tvb, value_offset,
				    value_len);

				/*
				 * Add it to the protocol tree,
				 * but display the line as is.
				 */
				switch ( hf_index ) {

				case POS_TO :
					if(hdr_tree) {
						sip_element_item = proto_tree_add_string_format(hdr_tree,
						    hf_header_array[hf_index], tvb,
						    offset, next_offset - offset,
						    value, "%s",
						    tvb_format_text(tvb, offset, linelen));
						sip_element_tree = proto_item_add_subtree( sip_element_item,
							 ett_sip_element);
					}
					tag_offset = tvb_find_guint8(tvb, offset,linelen, ';');
					if ( tag_offset != -1){
						tag_offset = tag_offset + 1;
						c = tvb_get_guint8(tvb,tag_offset);
						if ( c == 't' ){/* tag found */
							if(sip_element_tree) {
								proto_tree_add_string(sip_element_tree,
								    hf_sip_to_addr, tvb,
								    value_offset, (tag_offset - value_offset - 1),
				    				    tvb_format_text(tvb, value_offset,
								    ( tag_offset - value_offset - 1)));
							}
							tag_offset = tvb_find_guint8(tvb, tag_offset,linelen, '=') + 1;
							if(sip_element_tree) {
								proto_tree_add_string(sip_element_tree,
								    hf_sip_tag, tvb,
								    tag_offset, (line_end_offset - tag_offset),
				    				    tvb_format_text(tvb, tag_offset, (line_end_offset - tag_offset)));
				    			}

						}
						else {
							if(sip_element_tree) {
								proto_tree_add_string_format(sip_element_tree,
								    hf_sip_to_addr, tvb,
								    offset, line_end_offset - offset,
								    value, "%s",
								    tvb_format_text(tvb, offset, linelen));
							}
						}/* if c= t */
					} /* if tag offset */
					break;

				case POS_FROM :
					if(hdr_tree) {
						sip_element_item = proto_tree_add_string_format(hdr_tree,
						    hf_header_array[hf_index], tvb,
						    offset, next_offset - offset,
						    value, "%s",
						    tvb_format_text(tvb, offset, linelen));
						sip_element_tree = proto_item_add_subtree( sip_element_item, ett_sip_element);
					}
					tag_offset = tvb_find_guint8(tvb, offset,linelen, ';');
					if ( tag_offset != -1){
						tag_offset = tag_offset + 1;
						c = tvb_get_guint8(tvb,tag_offset);
						if ( c == 't' ){/* tag found */
							if(sip_element_tree) {
								proto_tree_add_string(sip_element_tree,
								    hf_sip_from_addr, tvb,
								    value_offset, (tag_offset - value_offset - 1),
								    tvb_format_text(tvb, value_offset, ( tag_offset - value_offset - 1)));
							}
							tag_offset = tvb_find_guint8(tvb, offset,linelen, '=') + 1;
							if(sip_element_tree) {
								proto_tree_add_string(sip_element_tree,
								    hf_sip_tag, tvb,
								    tag_offset, (line_end_offset - tag_offset),
								    tvb_format_text(tvb, tag_offset, (line_end_offset - tag_offset)));
							}
						}
						else {
							tag_offset = tvb_find_guint8(tvb, tag_offset,linelen, ';');
							if ( tag_offset != -1){
								tag_offset = tag_offset + 1;
								c = tvb_get_guint8(tvb,tag_offset);
								if ( c == 't' ){/* tag found */
									if(sip_element_tree) {
										proto_tree_add_string(sip_element_tree,
							 			    hf_sip_from_addr, tvb,
							   			    value_offset, (tag_offset - value_offset - 1),
				    						    tvb_format_text(tvb, value_offset, ( tag_offset - value_offset - 1)));
									}
									tag_offset = tvb_find_guint8(tvb, tag_offset,linelen, '=') + 1;
									if(sip_element_tree) {
										proto_tree_add_string(sip_element_tree,
										    hf_sip_tag, tvb,
										    tag_offset, (line_end_offset - tag_offset),
										    tvb_format_text(tvb, tag_offset, (line_end_offset - tag_offset)));
									}
								}
							}
						}/* if c= t */
					} /* if tag offset */
					break;

				case POS_CSEQ :
					/* Extract method name from value */
					for (value_offset = 0; value_offset < (gint)strlen(value); value_offset++)
					{
						if (isalpha((guchar)value[value_offset]))
						{
							strcpy(csec_method,value+value_offset);
							break;
						}
					}
					/* Add 'CSeq' string item to tree */
					if(hdr_tree) {
						proto_tree_add_string_format(hdr_tree,
						    hf_header_array[hf_index], tvb,
						    offset, next_offset - offset,
						    value, "%s",
						    tvb_format_text(tvb, offset, linelen));
					}
					break;

				case POS_EXPIRES :
					if (strcmp(value, "0") == 0)
					{
						expires_is_0 = 1;
					}
					/* Add 'Expires' string item to tree */
					if(hdr_tree) {
						proto_tree_add_string_format(hdr_tree,
						    hf_header_array[hf_index], tvb,
						    offset, next_offset - offset,
						    value, "%s",
						    tvb_format_text(tvb, offset, linelen));
					}
					break;

				/*
				 * Content-Type is the same as Internet
				 * media type used by other dissectors,
				 * appropriate dissector found by
				 * lookup in "media_type" dissector table.
				 */
				case POS_CONTENT_TYPE :
					if(hdr_tree) {
						proto_tree_add_string_format(hdr_tree,
						    hf_header_array[hf_index], tvb,
						    offset, next_offset - offset,
						    value, "%s",
						    tvb_format_text(tvb, offset, linelen));
					}
					content_type_len = value_len;
					semi_colon_offset = tvb_find_guint8(tvb, value_offset,linelen, ';');
					if ( semi_colon_offset != -1) {
						parameter_offset = semi_colon_offset +1;
						/*
						 * Skip whitespace after the semicolon.
						 */
						while (parameter_offset < line_end_offset
					    && ((c = tvb_get_guint8(tvb,
						    parameter_offset)) == ' '
					      || c == '\t'))
						parameter_offset++;
						content_type_len = semi_colon_offset - value_offset;
						content_type_parameter_str_len = line_end_offset - parameter_offset;
						content_type_parameter_str = tvb_get_string(tvb, parameter_offset,
								content_type_parameter_str_len);
					}
					media_type_str = tvb_get_string(tvb, value_offset, content_type_len);
#if GLIB_MAJOR_VERSION < 2
					media_type_str_lower_case = g_strdup(media_type_str);
					g_strdown(media_type_str_lower_case);
#else
					media_type_str_lower_case = g_ascii_strdown(media_type_str, -1);
#endif
					g_free(media_type_str);
					break;

				case POS_CONTACT :
					contacts++;
					if (strcmp(value, "*") == 0)
					{
						contact_is_star = 1;
					}
					/* Fall through to default case to add string to tree */

				default :
					if(hdr_tree) {
						proto_tree_add_string_format(hdr_tree,
						    hf_header_array[hf_index], tvb,
						    offset, next_offset - offset,
						    value, "%s",
						    tvb_format_text(tvb, offset, linelen));
					}
				break;
				}/* end switch */
				g_free(value);
			}/*if HF_index */
		}/* if colon_offset */
                offset = next_offset;
        }/* End while */

        if (tvb_offset_exists(tvb, next_offset)) {

                /*
                 * There's a message body starting at "next_offset".
                 * Set the length of the header item.
                 */
                proto_item_set_end(th, tvb, next_offset);
                next_tvb = tvb_new_subset(tvb, next_offset, -1, -1);
                if(sip_tree) {
               		ti = proto_tree_add_text(sip_tree, next_tvb, 0, -1,
                	                         "Message body");
               		message_body_tree = proto_item_add_subtree(ti, ett_sip_message_body);
		}

		/* give the content type parameters to sub dissectors */

		if ( media_type_str_lower_case != NULL ) {
			void *save_private_data = pinfo->private_data;
			pinfo->private_data = content_type_parameter_str;
			found_match = dissector_try_string(media_type_dissector_table,
							   media_type_str_lower_case,
							   next_tvb, pinfo,
							   message_body_tree);
			g_free(media_type_str_lower_case);
			g_free(content_type_parameter_str);
			pinfo->private_data = save_private_data;
			/* If no match dump as text */
		}
		if ( found_match != TRUE )
		{
			offset = 0;
	        	while (tvb_offset_exists(next_tvb, offset)) {
	                	tvb_find_line_end(next_tvb, offset, -1, &next_offset, FALSE);
	                	linelen = next_offset - offset;
	                	if(message_body_tree) {
	                		proto_tree_add_text(message_body_tree, next_tvb, offset, linelen,
	                   			"%s", tvb_format_text(next_tvb, offset, linelen));
	                   	}
 	        		offset = next_offset;
 	 		}/* end while */
		}
        }


	/* Add to info column interesting things learned from header fields. */
	if (check_col(pinfo->cinfo, COL_INFO))
	{
		/* Registration requests */
		if (strcmp(sip_methods[current_method_idx], "REGISTER") == 0)
		{
			if (contact_is_star && expires_is_0)
			{
				col_append_str(pinfo->cinfo, COL_INFO, "    (remove all bindings)");
			}
			else
			if (!contacts)
			{
				col_append_str(pinfo->cinfo, COL_INFO, "    (fetch bindings)");
			}
		}

		/* Registration responses */
		if (line_type == STATUS_LINE && (strcmp(csec_method, "REGISTER") == 0))
		{
			col_append_fstr(pinfo->cinfo, COL_INFO, "    (%d bindings)", contacts);
		}
	}

        if (global_sip_raw_text)
                tvb_raw_text_add(tvb, tree);

	/* Report this packet to the tap */
	tap_queue_packet(sip_tap, pinfo, stat_info);

        return TRUE;
}

/* Display filter for SIP Request-Line */
static void
dfilter_sip_request_line(tvbuff_t *tvb, proto_tree *tree, guint meth_len)
{
	char	*string;

        /*
         * We know we have the entire method; otherwise, "sip_parse_line()"
         * would have returned OTHER_LINE.
         */
        string = tvb_get_string(tvb, 0, meth_len);
        if (tree) {
        	proto_tree_add_string(tree, hf_Method, tvb, 0, meth_len, string);
	}
	/* Copy request method for telling tap */
	stat_info->request_method = g_malloc(meth_len+1);
	strncpy(stat_info->request_method, string, meth_len+1);

        /* String no longer needed */
        g_free(string);
}

/* Display filter for SIP Status-Line */
static void
dfilter_sip_status_line(tvbuff_t *tvb, proto_tree *tree)
{
	char string[3+1];

        /*
         * We know we have the entire status code; otherwise,
         * "sip_parse_line()" would have returned OTHER_LINE.
         * We also know that we have a version string followed by a
         * space at the beginning of the line, for the same reason.
         */
        tvb_memcpy(tvb, (guint8 *)string, SIP2_HDR_LEN + 1, 3);
        string[3] = '\0';
        if (tree) {
        	proto_tree_add_string(tree, hf_Status_Code, tvb, SIP2_HDR_LEN + 1,
        	    3, string);
	}
        /* Add response code for sending to tap */
        stat_info->response_code = atoi(string);
}

/* From section 4.1 of RFC 2543:
 *
 * Request-Line  =  Method SP Request-URI SP SIP-Version CRLF
 *
 * From section 5.1 of RFC 2543:
 *
 * Status-Line  =  SIP-version SP Status-Code SP Reason-Phrase CRLF
 *
 * From section 7.1 of RFC 3261:
 *
 * Unlike HTTP, SIP treats the version number as a literal string.
 * In practice, this should make no difference.
 */
static line_type_t
sip_parse_line(tvbuff_t *tvb, gint linelen, guint *token_1_lenp)
{
	gint space_offset;
	guint token_1_len;
	gint token_2_start;
	guint token_2_len;
	gint token_3_start;
	guint token_3_len;
	gint colon_pos;

	space_offset = tvb_find_guint8(tvb, 0, -1, ' ');
	if (space_offset <= 0) {
		/*
		 * Either there's no space in the line (which means
		 * the line is empty or doesn't have a token followed
		 * by a space; neither is valid for a request or status), or
		 * the first character in the line is a space (meaning
		 * the method is empty, which isn't valid for a request,
		 * or the SIP version is empty, which isn't valid for a
		 * status).
		 */
		return OTHER_LINE;
	}
	token_1_len = space_offset;
	token_2_start = space_offset + 1;
	space_offset = tvb_find_guint8(tvb, token_2_start, -1, ' ');
	if (space_offset == -1) {
		/*
		 * There's no space after the second token, so we don't
		 * have a third token.
		 */
		return OTHER_LINE;
	}
	token_2_len = space_offset - token_2_start;
	token_3_start = space_offset + 1;
	token_3_len = linelen - token_3_start;

	*token_1_lenp = token_1_len;

	/*
	 * Is the first token a version string?
	 */
	if ( (strict_sip_version && (
		token_1_len == SIP2_HDR_LEN
		&& tvb_strneql(tvb, 0, SIP2_HDR, SIP2_HDR_LEN) == 0)
	) || (! strict_sip_version && (
		tvb_strneql(tvb, 0, "SIP/", 4) == 0)
	)) {
		/*
		 * Yes, so this is either a Status-Line or something
		 * else other than a Request-Line.  To be a Status-Line,
		 * the second token must be a 3-digit number.
		 */
		if (token_2_len != 3) {
			/*
			 * We don't have 3-character status code.
			 */
			return OTHER_LINE;
		}
		if (!isdigit(tvb_get_guint8(tvb, token_2_start)) ||
		    !isdigit(tvb_get_guint8(tvb, token_2_start + 1)) ||
		    !isdigit(tvb_get_guint8(tvb, token_2_start + 2))) {
			/*
			 * 3 characters yes, 3 digits no.
			 */
			return OTHER_LINE;
		}
		return STATUS_LINE;
	} else {
		/*
		 * No, so this is either a Request-Line or something
		 * other than a Status-Line.  To be a Request-Line, the
		 * second token must be a URI and the third token must
		 * be a version string.
		 */
		if (token_2_len < 3) {
			/*
			 * We don't have a URI consisting of at least 3
			 * characters.
			 */
			return OTHER_LINE;
		}
		colon_pos = tvb_find_guint8(tvb, token_2_start + 1, -1, ':');
		if (colon_pos == -1) {
			/*
			 * There is no colon after the method, so the URI
			 * doesn't have a colon in it, so it's not valid.
			 */
			return OTHER_LINE;
		}
		if (colon_pos >= token_3_start) {
			/*
			 * The colon is in the version string, not the URI.
			 */
			return OTHER_LINE;
		}
		/* XXX - Check for a proper URI prefix? */
		if ( (strict_sip_version && (
			token_3_len != SIP2_HDR_LEN
			|| tvb_strneql(tvb, token_3_start, SIP2_HDR, SIP2_HDR_LEN) == -1)
		) || (! strict_sip_version && (
			tvb_strneql(tvb, token_3_start, "SIP/", 4) == -1)
		)) {
			/*
			 * The version string isn't an SIP version 2.0 version
			 * string.
			 */
			return OTHER_LINE;
		}
		return REQUEST_LINE;
	}
}

static gboolean sip_is_known_request(tvbuff_t *tvb, int meth_offset,
    guint meth_len, guint *meth_idx)
{
        guint i;

        for (i = 1; i < array_length(sip_methods); i++) {
                if (meth_len == strlen(sip_methods[i]) &&
                    tvb_strneql(tvb, meth_offset, sip_methods[i], meth_len) == 0)
                {
                     *meth_idx = i;
                     return TRUE;
                }
        }

        return FALSE;
}

/* Returns index of method in sip_headers */
static gint sip_is_known_sip_header(tvbuff_t *tvb, int offset, guint header_len)
{
        guint i;

        for (i = 1; i < array_length(sip_headers); i++) {
                if (header_len == strlen(sip_headers[i].name) &&
                    tvb_strncaseeql(tvb, offset, sip_headers[i].name, header_len) == 0)
                        return i;
                if (sip_headers[i].compact_name != NULL &&
                    header_len == strlen(sip_headers[i].compact_name) &&
                    tvb_strncaseeql(tvb, offset, sip_headers[i].compact_name, header_len) == 0)
                        return i;
        }

        return -1;
}

/*
 * Display the entire message as raw text.
 */
static void
tvb_raw_text_add(tvbuff_t *tvb, proto_tree *tree)
{
        proto_tree *raw_tree = NULL;
        proto_item *ti = NULL;
        int offset, next_offset, linelen;

	if(tree) {
		ti = proto_tree_add_item(tree, proto_raw_sip, tvb, 0, -1, FALSE);
		raw_tree = proto_item_add_subtree(ti, ett_raw_text);
	}

        offset = 0;

        while (tvb_offset_exists(tvb, offset)) {
                tvb_find_line_end(tvb, offset, -1, &next_offset, FALSE);
                linelen = next_offset - offset;
                if(raw_tree) {
			proto_tree_add_text(raw_tree, tvb, offset, linelen,
			    "%s", tvb_format_text(tvb, offset, linelen));
		}
                offset = next_offset;
        }
}

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

        /* Setup list of header fields */
        static hf_register_info hf[] = {

                { &hf_msg_hdr,
                        { "Message Header",           "sip.msg_hdr",
                        FT_NONE, 0, NULL, 0,
                        "Message Header in SIP message", HFILL }
                },
                { &hf_Method,
		       { "Method", 		"sip.Method",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"SIP Method", HFILL }
		},
                { &hf_Request_Line,
                       { "Request-Line",                "sip.Request-Line",
                       FT_STRING, BASE_NONE,NULL,0x0,
                       "SIP Request-Line", HFILL }
                },
                { &hf_Status_Code,
		       { "Status-Code", 		"sip.Status-Code",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"SIP Status Code", HFILL }
		},
		{ &hf_Status_Line,
		       { "Status-Line",                 "sip.Status-Line",
		       FT_STRING, BASE_NONE,NULL,0x0,
                       "SIP Status-Line", HFILL }
                },
                { &hf_sip_to_addr,
		       { "SIP to address", 		"sip.from_addr",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: from addr", HFILL }
		},
                { &hf_sip_from_addr,
		       { "SIP from address", 		"sip.to_addr",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: to addr", HFILL }
		},
                { &hf_sip_tag,
		       { "SIP tag", 		"sip.tag",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: tag", HFILL }
		},
                { &hf_header_array[POS_ACCEPT],
		       { "Accept", 		"sip.Accept",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Accept Header", HFILL }
		},
                { &hf_header_array[POS_ACCEPT_ENCODING],
		       { "Accept-Encoding", 		"sip.Accept-Encoding",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Accept-Encoding Header", HFILL }
		},
                { &hf_header_array[POS_ACCEPT_LANGUAGE],
		       { "Accept-Language", 		"sip.Accept-Language",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Accept-Language Header", HFILL }
		},
                { &hf_header_array[POS_ALERT_INFO],
		       { "Alert-Info", 		"sip.Alert-Info",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Alert-Info Header", HFILL }
		},
                { &hf_header_array[POS_ALLOW],
		       { "Allow", 		"sip.Allow",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Allow Header", HFILL }
		},
                { &hf_header_array[POS_ALLOW_EVENTS],
		       { "Allow-Events", 		"sip.Allow-Events",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3265: Allow-Events Header", HFILL }
		},
                { &hf_header_array[POS_AUTHENTICATION_INFO],
		       { "Authentication-Info", 		"sip.Authentication-Info",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Authentication-Info Header", HFILL }
		},
                { &hf_header_array[POS_AUTHORIZATION],
		       { "Authorization", 		"sip.Authorization",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Authorization Header", HFILL }
		},
                { &hf_header_array[POS_CALL_ID],
		       { "Call-ID", 		"sip.Call-ID",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Call-ID Header", HFILL }
		},
                { &hf_header_array[POS_CALL_INFO],
		       { "Call-Info", 		"sip.Call-Info",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Call-Info Header", HFILL }
		},
                { &hf_header_array[POS_CONTACT],
		       { "Contact", 		"sip.Contact",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Contact Header", HFILL }
		},
                { &hf_header_array[POS_CONTENT_DISPOSITION],
		       { "Content-Disposition", 		"sip.Content-Disposition",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Content-Disposition Header", HFILL }
		},
                { &hf_header_array[POS_CONTENT_ENCODING],
		       { "Content-Encoding", 		"sip.Content-Encoding",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Content-Encoding Header", HFILL }
		},
                { &hf_header_array[POS_CONTENT_LANGUAGE],
		       { "Content-Language", 		"sip.Content-Language",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Content-Language Header", HFILL }
		},
                { &hf_header_array[POS_CONTENT_LENGTH],
		       { "Content-Length", 		"sip.Content-Length",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Content-Length Header", HFILL }
		},
                { &hf_header_array[POS_CONTENT_TYPE],
		       { "Content-Type", 		"sip.Content-Type",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Content-Type Header", HFILL }
		},
                { &hf_header_array[POS_CSEQ],
		       { "CSeq", 		"sip.CSeq",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: CSeq Header", HFILL }
		},
                { &hf_header_array[POS_DATE],
		       { "Date", 		"sip.Date",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Date Header", HFILL }
		},
                { &hf_header_array[POS_ERROR_INFO],
		       { "Error-Info", 		"sip.Error-Info",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Error-Info Header", HFILL }
		},
                { &hf_header_array[POS_EVENT],
		       { "Event", 		"sip.Event",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3265: Event Header", HFILL }
		},
                { &hf_header_array[POS_EXPIRES],
		       { "Expires", 		"sip.Expires",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Expires Header", HFILL }
		},
                { &hf_header_array[POS_FROM],
		       { "From", 		"sip.From",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: From Header", HFILL }
		},
                { &hf_header_array[POS_IN_REPLY_TO],
		       { "In-Reply-To", 		"sip.In-Reply-To",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: In-Reply-To Header", HFILL }
		},
                { &hf_header_array[POS_MAX_FORWARDS],
		       { "Max-Forwards", 		"sip.Max-Forwards",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Max-Forwards Header", HFILL }
		},
                { &hf_header_array[POS_MIME_VERSION],
		       { "MIME-Version", 		"sip.MIME-Version",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: MIME-Version Header", HFILL }
		},
                { &hf_header_array[POS_MIN_EXPIRES],
		       { "Min-Expires", 		"sip.Min-Expires",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Min-Expires Header", HFILL }
		},
                { &hf_header_array[POS_ORGANIZATION],
		       { "Organization", 		"sip.Organization",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Organization Header", HFILL }
		},
                { &hf_header_array[POS_PRIORITY],
		       { "Priority", 		"sip.Priority",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Priority Header", HFILL }
		},
                { &hf_header_array[POS_PROXY_AUTHENTICATE],
		       { "Proxy-Authenticate", 		"sip.Proxy-Authenticate",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Proxy-Authenticate Header", HFILL }
		},
                { &hf_header_array[POS_PROXY_AUTHORIZATION],
		       { "Proxy-Authorization", 		"sip.Proxy-Authorization",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Proxy-Authorization Header", HFILL }
		},
                { &hf_header_array[POS_RACK],
		       { "RAck", 		"sip.RAck",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3262: RAck Header", HFILL }
		},
                { &hf_header_array[POS_RSEQ],
		       { "RSeq", 		"sip.RSeq",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3262: RSeq Header", HFILL }
		},
                { &hf_header_array[POS_PROXY_REQUIRE],
		       { "Proxy-Require", 		"sip.Proxy-Require",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Proxy-Require Header", HFILL }
		},
                { &hf_header_array[POS_RECORD_ROUTE],
		       { "Record-Route", 		"sip.Record-Route",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Record-Route Header", HFILL }
		},
                { &hf_header_array[POS_REPLY_TO],
		       { "Reply-To", 		"sip.Reply-To",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Reply-To Header", HFILL }
		},
                { &hf_header_array[POS_REQUIRE],
		       { "Require", 		"sip.Require",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Require Header", HFILL }
		},
                { &hf_header_array[POS_RETRY_AFTER],
		       { "Retry-After", 		"sip.Retry-After",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Retry-After Header", HFILL }
		},
                { &hf_header_array[POS_ROUTE],
		       { "Route", 		"sip.Route",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Route Header", HFILL }
		},
                { &hf_header_array[POS_SERVER],
		       { "Server", 		"sip.Server",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Server Header", HFILL }
		},
                { &hf_header_array[POS_SUBJECT],
		       { "Subject", 		"sip.Subject",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Subject Header", HFILL }
		},
                { &hf_header_array[POS_SUBSCRIPTION_STATE],
		       { "Subscription-State", 		"sip.Subscription-State",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3265: Subscription-State Header", HFILL }
		},
                { &hf_header_array[POS_SUPPORTED],
		       { "Supported", 		"sip.Supported",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Supported Header", HFILL }
		},
                { &hf_header_array[POS_TIMESTAMP],
		       { "Timestamp", 		"sip.Timestamp",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Timestamp Header", HFILL }
		},
                { &hf_header_array[POS_TO],
		       { "To", 		"sip.To",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: To Header", HFILL }
		},
                { &hf_header_array[POS_UNSUPPORTED],
		       { "Unsupported", 		"sip.Unsupported",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Unsupported Header", HFILL }
		},
                { &hf_header_array[POS_USER_AGENT],
		       { "User-Agent", 		"sip.User-Agent",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: User-Agent Header", HFILL }
		},
                { &hf_header_array[POS_VIA],
		       { "Via", 		"sip.Via",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Via Header", HFILL }
		},
                { &hf_header_array[POS_WARNING],
		       { "Warning", 		"sip.Warning",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Warning Header", HFILL }
		},
                { &hf_header_array[POS_WWW_AUTHENTICATE],
		       { "WWW-Authenticate", 		"sip.WWW-Authenticate",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: WWW-Authenticate Header", HFILL }
		},
		{ &hf_header_array[POS_P_ACCESS_NETWORK_INFO],
		       { "P-Access-Network-Info",	"sip.P-Access-Network-Info",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"P-Access-Network-Info Header", HFILL }
		},

		{ &hf_header_array[POS_P_ASSERTED_IDENTITY],
		       { "P-Asserted-Identity",		"sip.P-Asserted-Identity",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"P-Asserted-Identity Header", HFILL }
		},

		{ &hf_header_array[POS_P_ASSOCIATED_URI],
		       { "P-Associated-URI", 		"sip.P-Associated-URI",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"P-Associated-URI Header", HFILL }
		},

		{ &hf_header_array[POS_P_CALLED_PARTY_ID],
		       { "P-Called-Party-ID", 		"sip.P-Called-Party-ID",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"P-Called-Party-ID Header", HFILL }
		},

		{ &hf_header_array[POS_P_CHARGING_FUNCTION_ADDRESSES],
		       { "P-Charging-Function-Addresses","sip.P-Charging-Function-Addresses",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"P-Charging-Function-Addresses", HFILL }
		},

		{ &hf_header_array[POS_P_CHARGING_VECTOR],
		       { "P-Charging-Vector", 		"sip.P-Charging-Vector",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"P-Charging-Vector Header", HFILL }
		},

		{ &hf_header_array[POS_P_DCS_TRACE_PARTY_ID],
		       { "P-DCS-Trace-Party-ID", 	"sip.P-DCS-Trace-Party-ID",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"P-DCS-Trace-Party-ID Header", HFILL }
		},

		{ &hf_header_array[POS_P_DCS_OSPS],
		       { "P-DCS-OSPS", 			"sip.P-DCS-OSPS",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"P-DCS-OSPS Header", HFILL }
		},

		{ &hf_header_array[POS_P_DCS_BILLING_INFO],
		       { "P-DCS-Billing-Info", 		"sip.P-DCS-Billing-Info",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"P-DCS-Billing-Info Header", HFILL }
		},

		{ &hf_header_array[POS_P_DCS_LAES],
		       { "P-DCS-LAES", 			"sip.P-DCS-LAES",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"P-DCS-LAES Header", HFILL }
		},

		{ &hf_header_array[POS_P_DCS_REDIRECT],
		       { "P-DCS-Redirect", 		"sip.P-DCS-Redirect",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"P-DCS-Redirect Header", HFILL }
		},

		{ &hf_header_array[POS_P_MEDIA_AUTHORIZATION],
		       { "P-Media-Authorization", 	"sip.P-Media-Authorization",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"P-Media-Authorization Header", HFILL }
		},

		{ &hf_header_array[POS_P_PREFERRED_IDENTITY],
		       { "P-Preferred-Identity",  	"sip.P-Preferred-Identity",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"P-Preferred-Identity Header", HFILL }
		},

		{ &hf_header_array[POS_P_VISITED_NETWORK_ID],
		       { "P-Visited-Network-ID", 	"sip.P-Visited-Network-ID",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"P-Visited-Network-ID Header", HFILL }
		},

		{ &hf_header_array[POS_PATH],
		       { "Path", 			"sip.Path",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"Path Header", HFILL }
		},

		{ &hf_header_array[POS_PRIVACY],
		       { "Privacy", 			"sip.Privacy",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"Privacy Header", HFILL }
		},

		{ &hf_header_array[POS_REASON],
		       { "Reason", 			"sip.Reason",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"Reason Header", HFILL }
		},

		{ &hf_header_array[POS_REFER_TO],
		       { "Refer-To", 			"sip.Refer-To",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"Refer-To Header", HFILL }
		},

		{ &hf_header_array[POS_SERVICE_ROUTE],
		       { "Service-Route", 		"sip.Service-Route",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"Service-Route Header", HFILL }
		},

		{ &hf_header_array[POS_SIP_ETAG],
		       { "ETag", 		"sip.ETag",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"SIP-ETag Header", HFILL }
		},
		{ &hf_header_array[POS_SIP_IF_MATCH],
		       { "If_Match", 		"sip.If_Match",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"SIP-If-Match Header", HFILL }
		},
        };

        /* Setup protocol subtree array */
        static gint *ett[] = {
                &ett_sip,
                &ett_sip_reqresp,
                &ett_sip_hdr,
		&ett_sip_element,
		&ett_sip_message_body,
        };
        static gint *ett_raw[] = {
                &ett_raw_text,
        };

	module_t *sip_module;

        /* Register the protocol name and description */
        proto_sip = proto_register_protocol("Session Initiation Protocol",
            "SIP", "sip");
        proto_raw_sip = proto_register_protocol("Session Initiation Protocol (SIP as raw text)",
            "Raw_SIP", "raw_sip");

        /* Required function calls to register the header fields and subtrees used */
        proto_register_field_array(proto_sip, hf, array_length(hf));
        proto_register_subtree_array(ett, array_length(ett));
        proto_register_subtree_array(ett_raw, array_length(ett_raw));

	/* SIP content type and internet media type used by other dissectors are the same */

	media_type_dissector_table = find_dissector_table("media_type");


        sip_module = prefs_register_protocol(proto_sip, NULL);

	prefs_register_bool_preference(sip_module, "display_raw_text",
		"Display raw text for SIP message",
		"Specifies that the raw text of the "
		"SIP message should be displayed "
		"in addition to the dissection tree",
		&global_sip_raw_text);
	prefs_register_bool_preference(sip_module, "strict_sip_version",
		"Enforce strict SIP version check (" SIP2_HDR ")",
		"If enabled, only " SIP2_HDR " traffic will be dissected as SIP. "
		"Disable it to allow SIP traffic with a different version "
		"to be dissected as SIP.",
		&strict_sip_version);

	/* Register for tapping */
	sip_tap = register_tap("sip");
}

void
proto_reg_handoff_sip(void)
{
	dissector_handle_t sip_handle, sip_tcp_handle;

	sip_handle = new_create_dissector_handle(dissect_sip, proto_sip);
	dissector_add("udp.port", UDP_PORT_SIP, sip_handle);
	dissector_add_string("media_type", "message/sip", sip_handle);

	sip_tcp_handle = create_dissector_handle(dissect_sip_tcp, proto_sip);
	dissector_add("tcp.port", TCP_PORT_SIP, sip_tcp_handle);

	heur_dissector_add("udp", dissect_sip_heur, proto_sip);
	heur_dissector_add("tcp", dissect_sip_heur, proto_sip);
	heur_dissector_add("sctp", dissect_sip_heur, proto_sip);
}