Fix about 100 simple to fix warnings from gcc 4.0 in epan/dissectors
[obnox/wireshark/wip.git] / epan / dissectors / packet-dcm.c
1 /* packet-dcm.c
2  * Routines for DICOM dissection
3  * Copyright 2003, Rich Coe <Richard.Coe@med.ge.com>
4  *
5  * DICOM communication protocol
6  * http://medical.nema.org/dicom/2003.html
7  *   DICOM Part 8: Network Communication Support for Message Exchange
8  *
9  * (NOTE: you need to turn on 'Allow subdissector to desegment TCP streams'
10  *        in Preferences/Protocols/TCP Option menu, in order to view
11  *        DICOM packets correctly.  
12  *        Also, you might have to turn off tcp.check_checksum if tcp
13  *        detects that the checksum is bad - for example, if you're
14  *        capturing on a network interface that does TCP checksum
15  *        offloading and you're capturing outgoing packets.
16  *        This should probably be documented somewhere besides here.)
17  *
18  * $Id$
19  *
20  * Wireshark - Network traffic analyzer
21  * By Gerald Combs <gerald@wireshark.org>
22  * Copyright 1998 Gerald Combs
23  *
24  * This program is free software; you can redistribute it and/or
25  * modify it under the terms of the GNU General Public License
26  * as published by the Free Software Foundation; either version 2
27  * of the License, or (at your option) any later version.
28  * 
29  * This program is distributed in the hope that it will be useful,
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32  * GNU General Public License for more details.
33  * 
34  * You should have received a copy of the GNU General Public License
35  * along with this program; if not, write to the Free Software
36  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
37  */
38
39 #ifdef HAVE_CONFIG_H
40 # include "config.h"
41 #endif
42
43 /* Notes:
44  * This is my first pass at a Wireshark dissector to display
45  * DICOM (Digital Imaging and Communications in Medicine) packets. 
46  * 
47  * - It currently displays most of the DICOM packets.
48  * 
49  * - I've used it to debug Query/Retrieve, Storage, and Echo protocols.
50  *
51  * - Not all DICOM tags are currently displayed symbolically.  
52  *   Unknown tags are displayed as '(unknown)'
53  *   More known tags might be added in the future.
54  *   If the tag data contains a string, it will be displayed.
55  *   Even if the tag contains Explicit VR, it is not currently used to
56  *   symbolically display the data.  Consider this a future enhancement.
57  *
58  * - If the DATA PDU has the 'more' bit set, subsequent packets will
59  *   not currently display.  Finding out how much 'more' data is coming
60  *   currently requires parsing the entire packet.
61  * 
62  * - The 'value to string' routines should probably be hash lookups.
63  *
64  * 9 Nov 2004
65  * - Fixed the heuristic code -- sometimes a conversation already exists
66  * - Fixed the dissect code to display all the tags in the pdu
67  *
68  * 28 Apr 2005
69  * - fix memory leak when Assoc packet is processed repeatedly in wireshark
70  *
71  * - removed unused partial packet flag
72  *
73  * - added better support for DICOM VR
74  *      - sequences
75  *      - report actual VR in packet display, if supplied by xfer syntax 
76  *      - show that we are not displaying entire tag string with '[...]',
77  *        some tags can hold up to 2^32-1 chars
78  *
79  * - remove my goofy attempt at trying to get access to the fragmented packets
80  *   (anyone have an idea on how to fix this ???)
81  *
82  * - process all the data in the Assoc packet even if display is off
83  *
84  * - limit display of data in Assoc packet to defined size of the data even
85  *   if reported size is larger
86  *
87  * - show the last tag in a packet as [incomplete] if we don't have all the data
88  *
89  * - added framework for reporting DICOM async negotiation (not finished)
90  *   (I'm not aware of an implementation which currently supports this)
91  *
92  * - still need to fix display of continuation packets
93  */
94
95 #include <stdio.h>
96 #include <stdlib.h>
97 #include <string.h>
98 #include <ctype.h>
99
100 #include <glib.h>
101
102 #include "isprint.h"
103
104 #include <epan/packet.h>
105 #include <epan/emem.h>
106 #include <epan/strutil.h>
107 #include <epan/conversation.h>
108
109 #include "packet-tcp.h"
110
111 /* Initialize the protocol and registered fields */
112 static int proto_dcm = -1;
113 static int hf_dcm_pdu = -1,
114     hf_dcm_pdu_len = -1,
115     hf_dcm_pdu_type = -1,
116     hf_dcm_pdi = -1,
117     hf_dcm_pdi_name = -1,
118     hf_dcm_pdi_syntax = -1, 
119     hf_dcm_pctxt = -1,
120     hf_dcm_pcres = -1,
121     hf_dcm_pdu_maxlen = -1,
122     hf_dcm_impl = -1,
123     hf_dcm_vers = -1,
124     hf_dcm_async = -1,
125     hf_dcm_data_len = -1,
126     hf_dcm_data_ctx = -1,
127     hf_dcm_data_flags = -1,
128     hf_dcm_data_tag = -1;
129
130 /* Initialize the subtree pointers */
131 static gint ett_dcm = -1, ett_assoc = -1, ett_dcm_data = -1;
132
133 static const value_string dcm_pdu_ids[] = {
134     { 1, "ASSOC Request" },
135     { 2, "ASSOC Accept" },
136     { 3, "ASSOC Reject" },
137     { 4, "Data" },
138     { 5, "RELEASE Request" },
139     { 6, "RELEASE Response" },
140     { 7, "ABORT" },
141     { 0, NULL }
142 };
143
144 static const value_string dcm_pdi_ids[] = {
145     { 0x10, "Application Context" },
146     { 0x20, "Presentation Context" },
147     { 0x21, "Presentation Context Reply" },
148     { 0x30, "Abstract syntax" },
149     { 0x40, "Transfer syntax" },
150     { 0x50, "User Info" },
151     { 0x51, "Max Length" },
152     { 0, NULL }
153 };
154
155 struct dcmContext {
156     guint8 id;
157     guint8 result;
158 };
159 struct dcmItem {
160     struct dcmItem *next, *prev;
161     int valid;
162     guint8 id;          /* 0x20 Presentation Context */
163     const guint8 *abs;  /* 0x30 Abstract syntax */
164     char *xfer;         /* 0x40 Transfer syntax */
165     guint8 syntax;
166 #define DCM_ILE  0x01           /* implicit, little endian */
167 #define DCM_EBE  0x02           /* explicit, big endian */
168 #define DCM_ELE  0x03           /* explicit, little endian */
169 #define DCM_UNK  0xf0
170 };
171 typedef struct dcmItem dcmItem_t;
172
173 struct dcmState {
174     dcmItem_t *first, *last;
175     guint8 pdu;         /* protocol data unit */
176     guint32 tlen, clen, rlen;    /* length: total, current, remaining */
177     int coff;           /* current offset */
178     int valid;          /* this conversation is a dicom conversation */
179     /* enum { DCM_NONE, DCM_ASSOC, DCM_ }; */
180 #define AEEND 16
181     guint8 orig[1+AEEND], targ[1+AEEND], resp[1+AEEND], source, result, reason;
182 };
183 typedef struct dcmState dcmState_t;
184
185 struct dcmTag {
186     int tag;
187     int dtype;
188     const char *desc;
189 #define DCM_TSTR  1
190 #define DCM_TINT2 2
191 #define DCM_TINT4 3
192 #define DCM_TFLT  4
193 #define DCM_TDBL  5
194 #define DCM_TSTAT 6  /* call dcm_rsp2str() on TINT2 */
195 #define DCM_TRET  7
196 #define DCM_TCMD  8
197 #define DCM_SQ    9     /* sequence */
198 #define DCM_OTH   10    /* other */
199 };
200 typedef struct dcmTag dcmTag_t;
201
202 static GHashTable *dcm_tagTable = NULL;
203
204 dcmItem_t * lookupCtx(dcmState_t *dd, guint8 ctx);
205
206 static dcmTag_t tagData[] = {
207     {  0x1,    DCM_TRET,  "(Ret) Length to End" },
208     {  0x2,    DCM_TSTR,  "Affected Class" },
209     {  0x3,    DCM_TSTR,  "Requested Class" },
210     {  0x0010, DCM_TRET,  "(Ret) Recognition Code" },
211     {  0x0100, DCM_TCMD,  "Command Field" },
212     {  0x0110, DCM_TINT2, "Message ID" },
213     {  0x0120, DCM_TINT2, "Resp Message ID" },
214     {  0x0200, DCM_TRET,  "(Ret) Initiator" },
215     {  0x0300, DCM_TRET,  "(Ret) Reciever" },
216     {  0x0400, DCM_TRET,  "(Ret) Find Location" },
217     {  0x0600, DCM_TSTR,  "Dest AE" },
218     {  0x0700, DCM_TINT2, "Priority" },
219     {  0x0800, DCM_TINT2, "Data Set (0x0101 means no data set present)" },
220     {  0x0850, DCM_TRET,  "(Ret) Num Matches" },
221     {  0x0860, DCM_TRET,  "(Ret) Resp Seq Num" },
222     {  0x0900, DCM_TSTAT, "Status" },
223     {  0x0901, DCM_TSTR,  "Offending elm(s)" },
224     {  0x0902, DCM_TSTR,  "Error Comment" },
225     {  0x0903, DCM_TINT2, "Error Id" },
226     {  0x1000, DCM_TSTR,  "Affected Instance UID" },
227     {  0x1001, DCM_TSTR,  "Requested Instance UID" },
228     {  0x1002, DCM_TINT2, "Event Type Id" },
229     {  0x1005, DCM_TSTR,  "Attr Id List" },
230     {  0x1008, DCM_TINT2, "Action Type Id" },
231     {  0x1020, DCM_TINT2, "Num Remaining Ops" },
232     {  0x1021, DCM_TINT2, "Num Completed Ops" },
233     {  0x1022, DCM_TINT2, "Num Failed Ops" },
234     {  0x1023, DCM_TINT2, "Num Warning Ops" },
235     {  0x1030, DCM_TSTR,  "Move Orig AE" },
236     {  0x1031, DCM_TINT2, "Move Orig Id" },
237     {  0x4000, DCM_TRET,  "(Ret) DIALOG Recv'r" },
238     {  0x4010, DCM_TRET,  "(Ret) Terminal Type" },
239     {  0x5010, DCM_TRET,  "(Ret) Msg Set ID" },
240     {  0x5020, DCM_TRET,  "(Ret) End Msg ID" },
241     {  0x5110, DCM_TRET,  "(Ret) Display Fmt" },
242     {  0x5120, DCM_TRET,  "(Ret) Page Position ID" },
243     {  0x5130, DCM_TRET,  "(Ret) Text Fmt ID" },
244     {  0x5140, DCM_TRET,  "(Ret) Nor/Rev" },
245     {  0x5150, DCM_TRET,  "(Ret) Add Gray Scale" },
246     {  0x5160, DCM_TRET,  "(Ret) Borders" },
247     {  0x5170, DCM_TRET,  "(Ret) Copies" },
248     {  0x5180, DCM_TRET,  "(Ret) Mag Type" },
249     {  0x5190, DCM_TRET,  "(Ret) Erase" },
250     {  0x51a0, DCM_TRET,  "(Ret) Print" },
251     {  0x080018, DCM_TSTR, "Image UID" },
252     {  0x080020, DCM_TSTR, "Study Date" },
253     {  0x080030, DCM_TSTR, "Study Time" },
254     {  0x080050, DCM_TSTR, "Acc Num" },
255     {  0x080052, DCM_TSTR, "Q/R Level" },
256     {  0x080054, DCM_TSTR, "Retrieve AE" },
257     {  0x080060, DCM_TSTR, "Modality" },
258     {  0x080070, DCM_TSTR, "Manuf" },
259     {  0x081030, DCM_TSTR, "Study Desc" },
260     {  0x08103e, DCM_TSTR, "Series Desc" },
261     {  0x100010, DCM_TSTR, "Patient Name" },
262     {  0x100020, DCM_TSTR, "Patient Id" },
263     {  0x20000d, DCM_TSTR, "Study UID" },
264     {  0x20000e, DCM_TSTR, "Series UID" },
265     {  0x200010, DCM_TSTR, "Study Num" },
266     {  0x200011, DCM_TSTR, "Series Num" },
267     {  0x200012, DCM_TSTR, "Acq Num" },
268     {  0x200013, DCM_TSTR, "Image Num" },
269     {  0x7fe00010, DCM_OTH, "Pixels" },
270     {  0xfffee000, DCM_TRET, "Item Begin" },
271     {  0xfffee00d, DCM_TRET, "Item End" },
272     {  0xfffee0dd, DCM_TRET, "Sequence End" },
273 };
274
275 static void
276 dcm_init(void)
277 {
278     if (NULL == dcm_tagTable) {
279         unsigned int i;
280         dcm_tagTable = g_hash_table_new(NULL, NULL);
281         for (i = 0; i < sizeof(tagData) / sizeof(dcmTag_t); i++) 
282             g_hash_table_insert(dcm_tagTable, GINT_TO_POINTER(tagData[i].tag),
283                 (gpointer) (tagData+i));
284     }
285 }
286
287 static dcmState_t *
288 mkds(void)
289 {
290     dcmState_t *ds;
291
292     if (NULL == (ds = (dcmState_t *) g_malloc(sizeof(dcmState_t)))) {
293         return NULL;
294     }
295     ds->pdu = 0;
296     ds->tlen = ds->rlen = 0;
297     ds->valid = TRUE;
298     memset(ds->orig, 0, sizeof(ds->orig));
299     memset(ds->targ, 0, sizeof(ds->targ));
300     memset(ds->resp, 0, sizeof(ds->resp));
301     ds->first = ds->last = NULL;
302     return ds;
303 }
304
305 static const char *
306 dcm_pdu2str(guint8 item)
307 {
308     const char *s = "";
309     switch (item) {
310     case 1: s = "ASSOC Request"; break;
311     case 2: s = "ASSOC Accept"; break;
312     case 3: s = "ASSOC Reject"; break;
313     case 4: s = "Data"; break;
314     case 5: s = "RELEASE Request"; break;
315     case 6: s = "RELEASE Response"; break;
316     case 7: s = "ABORT"; break;
317     case 0x10: s = "Application Context"; break;
318     case 0x20: s = "Presentation Context"; break;
319     case 0x21: s = "Presentation Context Reply"; break;
320     case 0x30: s = "Abstract syntax"; break;
321     case 0x40: s = "Transfer syntax"; break;
322     case 0x50: s = "User Info"; break;
323     case 0x51: s = "Max Length"; break;
324     default: break;
325     }
326     return s;
327 }
328
329 static const char *
330 dcm_result2str(guint8 result)
331 {
332     const char *s = "";
333     switch (result) {
334     case 1:  s = "Reject Permanent"; break;
335     case 2:  s = "Reject Transient"; break;
336     default: break;
337     }
338     return s;
339 }
340
341 static const char *
342 dcm_source2str(guint8 source)
343 {
344     const char *s = "";
345     switch (source) {
346     case 1:  s = "User"; break;
347     case 2:  s = "Provider (ACSE)"; break;
348     case 3:  s = "Provider (Presentation)"; break;
349     default: break;
350     }
351     return s;
352 }
353
354 static const char * 
355 dcm_reason2str(guint8 source, guint8 reason)
356 {
357     const char *s = "";
358     if (1 == source) switch (reason) {
359         case 1:  s = "No reason"; break;
360         case 2:  s = "App Name not supported"; break;
361         case 3:  s = "calling AET not recognized"; break;
362         case 7:  s = "called AET not recognized"; break;
363         default: break;
364     } else if (2 == source) switch (reason) {
365         case 1:  s = "No reason"; break;
366         case 2:  s = "protocol unsupported"; break;
367         default: break;
368     } else if (3 == source) switch (reason) {
369         case 1:  s = "temporary congestion"; break;
370         case 2:  s = "local limit exceeded"; break;
371         default: break;
372     }
373     return s;
374 }
375
376 static const char *
377 dcm_abort2str(guint8 reason)
378 {
379     const char *s = "";
380     switch (reason) {
381     case 0:  s = "not specified"; break;
382     case 1:  s = "unrecognized"; break;
383     case 2:  s = "unexpected"; break;
384     case 4:  s = "unrecognized parameter"; break;
385     case 5:  s = "unexpected parameter"; break;
386     case 6:  s = "invalid parameter"; break;
387     default: break;
388     }
389     return s;
390 }
391
392 static const char *
393 dcm_PCresult2str(guint8 result)
394 {
395     const char *s = "";
396     switch (result) {
397     case 0:  s = "accept"; break;
398     case 1:  s = "user-reject"; break;
399     case 2:  s = "no-reason"; break;
400     case 3:  s = "abstract syntax unsupported"; break;
401     case 4:  s = "transfer syntax unsupported"; break;
402     default: break;
403     }
404     return s;
405 }
406
407 static const char *
408 dcm_flags2str(guint8 flags)
409 {
410     const char *s = "";
411     switch (flags) {
412     case 0:  s = "Data,    more Fragments"; break;      /* 00 */
413     case 1:  s = "Command, more Fragments"; break;      /* 01 */
414     case 2:  s = "Data,    last Fragment";  break;      /* 10 */
415     case 3:  s = "Command, last Fragment";  break;      /* 11 */
416     default: break;
417     }
418     return s;
419 }
420
421 static const char *
422 dcm_cmd2str(guint16 us)
423 {
424     const char *s = "";
425     /* there should be a better way to do this */
426     switch (us) {
427     case 0x0001:  s = "C-STORE-RQ"; break;
428     case 0x8001:  s = "C-STORE-RSP"; break;
429     case 0x0010:  s = "C-GET-RQ"; break;
430     case 0x8010:  s = "C-GET-RSP"; break;
431     case 0x0020:  s = "C-FIND-RQ"; break;
432     case 0x8020:  s = "C-FIND-RSP"; break;
433     case 0x0021:  s = "C-MOVE-RQ"; break;
434     case 0x8021:  s = "C-MOVE-RSP"; break;
435     case 0x0030:  s = "C-ECHO-RQ"; break;
436     case 0x8030:  s = "C-ECHO-RSP"; break;
437     case 0x0100:  s = "N-EVENT-REPORT-RQ"; break;
438     case 0x8100:  s = "N-EVENT-REPORT-RSP"; break;
439     case 0x0110:  s = "N-GET-RQ"; break;
440     case 0x8110:  s = "N-GET-RSP"; break;
441     case 0x0120:  s = "N-SET-RQ"; break;
442     case 0x8120:  s = "N-SET-RSP"; break;
443     case 0x0130:  s = "N-ACTION-RQ"; break;
444     case 0x8130:  s = "N-ACTION-RSP"; break;
445     case 0x0140:  s = "N-CREATE-RQ"; break;
446     case 0x8140:  s = "N-CREATE-RSP"; break;
447     case 0x0150:  s = "N-DELETE-RQ"; break;
448     case 0x8150:  s = "N-DELETE-RSP"; break;
449     case 0x0fff:  s = "C-CANCEL-RQ"; break;
450     default: break;
451     }
452     return s;
453 }
454
455 static const char *
456 dcm_rsp2str(guint16 us)
457 {
458     const char *s = "";
459     switch (us) {
460     case 0x0000:  s = "Success"; break;
461     case 0xa701:
462     case 0xa702:  s = "Refused: Out of Resources"; break;
463     case 0xa801:  s = "Refused: Move Destination unknown"; break;
464     case 0xa900:  s = "Failed:  Id does not match Class"; break;
465     case 0xb000:  s = "Warning: operations complete -- One or more Failures"; break;
466     case 0xfe00:  s = "Cancel:  operations terminated by Cancel"; break;
467     case 0xff00:  s = "Pending: operations are continuing"; break;
468     default: break;
469     }
470     if (0xC000 == (0xF000 & us))  s = "Failed:  Unable to Process"; 
471     return s;
472 }
473
474 static void
475 dcm_setSyntax(dcmItem_t *di, char *name)
476 {
477     if (NULL == di) return;
478     if (di->xfer != NULL)
479         g_free(di->xfer);       /* free prev allocated xfer */
480     di->syntax = 0;
481     di->xfer = g_strdup(name);
482     if (0 == *name) return;
483     /* this would be faster to skip the common parts, and have a FSA to 
484      * find the syntax.
485      * Absent of coding that, this is in descending order of probability */
486     if (0 == strcmp(name, "1.2.840.10008.1.2"))
487         di->syntax = DCM_ILE;    /* implicit little endian */
488     else if (0 == strcmp(name, "1.2.840.10008.1.2.1"))
489         di->syntax = DCM_ELE;    /* explicit little endian */
490     else if (0 == strcmp(name, "1.2.840.10008.1.2.2"))
491         di->syntax = DCM_EBE;    /* explicit big endian */
492     else if (0 == strcmp(name, "1.2.840.113619.5.2"))
493         di->syntax = DCM_ILE;    /* implicit little endian, big endian pixels */
494     else if (0 == strcmp(name, "1.2.840.10008.1.2.4.70"))
495         di->syntax = DCM_ELE;    /* explicit little endian, jpeg */
496     else if (0 == strncmp(name, "1.2.840.10008.1.2.4", 18))
497         di->syntax = DCM_ELE;    /* explicit little endian, jpeg */
498     else if (0 == strcmp(name, "1.2.840.10008.1.2.1.99"))
499         di->syntax = DCM_ELE;    /* explicit little endian, deflated */
500 }
501
502 static char *
503 dcm_tag2str(guint16 grp, guint16 elm, guint8 syntax, tvbuff_t *tvb, int offset, guint32 len, int vr, int tr)
504 {
505     char *buf;
506     const guint8 *vval;
507     char *p;
508     guint32 tag, val32;
509     guint16 val16;
510     dcmTag_t *dtag;
511     static dcmTag_t utag = { 0, 0, "(unknown)" };
512
513 #define MAX_BUF_LEN 1024
514     buf=ep_alloc(MAX_BUF_LEN);
515     *buf = 0;
516     if (0 == elm) {
517         if (DCM_ILE & syntax) 
518              val32 = tvb_get_letohl(tvb, offset); 
519         else val32 = tvb_get_ntohl(tvb, offset); 
520         g_snprintf(buf, MAX_BUF_LEN, "Group Length 0x%x (%d)", val32, val32);
521         return buf;
522     }
523     tag = (grp << 16) | elm;
524     if (NULL == (dtag = g_hash_table_lookup(dcm_tagTable, GUINT_TO_POINTER(tag))))
525         dtag = &utag;
526
527     DISSECTOR_ASSERT(MAX_BUF_LEN > strlen(dtag->desc));
528     p=buf;
529     p+=MIN(MAX_BUF_LEN-(p-buf),
530            g_snprintf(p, MAX_BUF_LEN-(p-buf), "%s", dtag->desc));
531     if (vr > 0) {
532         vval = (guint8*)tvb_format_text(tvb, vr, 2);
533         p+=MIN(MAX_BUF_LEN-(p-buf),
534                g_snprintf(p, MAX_BUF_LEN-(p-buf), " [%s]", vval));
535     }
536
537     switch (tr > 0 ? tr : dtag->dtype) {
538     case DCM_TSTR:
539     default:            /* try ascii */
540         vval = (guint8*)tvb_format_text(tvb, offset, len);
541         p+=MIN(MAX_BUF_LEN-(p-buf),
542                g_snprintf(p, MAX_BUF_LEN-(p-buf), " %s", vval));
543         break;
544     case DCM_TINT2:
545         if (DCM_ILE & syntax) 
546              val16 = tvb_get_letohs(tvb, offset);
547         else val16 = tvb_get_ntohs(tvb, offset);
548         p+=MIN(MAX_BUF_LEN-(p-buf),
549                g_snprintf(p, MAX_BUF_LEN-(p-buf), " 0x%x (%d)", val16, val16));
550         break;
551     case DCM_TINT4:
552         if (DCM_ILE & syntax) 
553              val32 = tvb_get_letohl(tvb, offset); 
554         else val32 = tvb_get_ntohl(tvb, offset); 
555         p+=MIN(MAX_BUF_LEN-(p-buf),
556                g_snprintf(p, MAX_BUF_LEN-(p-buf), " 0x%x (%d)", val32, val32));
557         break;
558     case DCM_TFLT: {
559         gfloat valf;
560         if (DCM_ILE & syntax) 
561              valf = tvb_get_letohieee_float(tvb, offset); 
562         else valf = tvb_get_ntohieee_float(tvb, offset); 
563         p+=MIN(MAX_BUF_LEN-(p-buf),
564                g_snprintf(p, MAX_BUF_LEN-(p-buf), " (%f)", valf));
565         } break;
566     case DCM_TDBL: {
567         gdouble vald;
568         if (DCM_ILE & syntax) 
569              vald = tvb_get_letohieee_double(tvb, offset); 
570         else vald = tvb_get_ntohieee_double(tvb, offset); 
571         p+=MIN(MAX_BUF_LEN-(p-buf),
572                g_snprintf(p, MAX_BUF_LEN-(p-buf), " (%f)", vald));
573         } break;
574     case DCM_TSTAT: /* call dcm_rsp2str() on TINT2 */
575         if (DCM_ILE & syntax) 
576              val16 = tvb_get_letohs(tvb, offset);
577         else val16 = tvb_get_ntohs(tvb, offset);
578         p+=MIN(MAX_BUF_LEN-(p-buf),
579                g_snprintf(p, MAX_BUF_LEN-(p-buf), " 0x%x '%s'", val16, dcm_rsp2str(val16)));
580         break;
581     case DCM_TCMD:   /* call dcm_cmd2str() on TINT2 */
582         if (DCM_ILE & syntax) 
583              val16 = tvb_get_letohs(tvb, offset);
584         else val16 = tvb_get_ntohs(tvb, offset);
585         p+=MIN(MAX_BUF_LEN-(p-buf),
586                g_snprintf(p, MAX_BUF_LEN-(p-buf), " 0x%x '%s'", val16, dcm_cmd2str(val16)));
587         break;
588     case DCM_SQ:        /* Sequence */
589     case DCM_OTH:       /* Other BYTE, WORD, ... */
590     case DCM_TRET:      /* Retired */
591         break;
592     }
593     return buf;
594 }
595
596 static guint
597 dcm_get_pdu_len(packet_info *pinfo _U_, tvbuff_t *tvb, int offset)
598 {
599     guint32 len;
600
601     len = tvb_get_ntohl(tvb, 2 + offset);
602     return len + 6;             /* add in fixed header part */
603 }
604
605 static void 
606 dissect_dcm_assoc(dcmState_t *dcm_data, proto_item *ti, tvbuff_t *tvb, int offset)
607
608     proto_tree *dcm_tree = NULL;
609     dcmItem_t *di = NULL;
610     guint8 id, *name, result;
611     int reply = 0;
612
613     if (!ti)
614         return;
615
616     dcm_tree = proto_item_add_subtree(ti, ett_assoc);
617     while (-1 < offset && offset < (int) dcm_data->clen) {
618         guint16 len;
619         guint32 mlen;
620         id = tvb_get_guint8(tvb, offset);
621         len = tvb_get_ntohs(tvb, 2 + offset);
622         if (ti)
623             proto_tree_add_uint_format(dcm_tree, hf_dcm_pdi, tvb,
624                 offset, 4+len, id, "Item 0x%x (%s)", id, dcm_pdu2str(id));
625         offset += 4;
626         switch (id) {
627         case 0x10:              /* App context */
628             if (ti)
629                 proto_tree_add_item(dcm_tree, hf_dcm_pdi_name, tvb, offset, len > 65 ? 65 : len, FALSE);
630             offset += len;
631             break;
632         case 0x30:              /* Abstract syntax */
633             if (ti)
634                 proto_tree_add_item(dcm_tree, hf_dcm_pdi_syntax, tvb, offset, len > 65 ? 65 : len, FALSE);
635             offset += len;
636             break;
637         case 0x40:              /* Transfer syntax */
638             if (ti)
639                 proto_tree_add_item(dcm_tree, hf_dcm_pdi_syntax, tvb, offset, len > 65 ? 65 : len, FALSE);
640             if (reply && di && di->valid) {
641                 name = tvb_get_ephemeral_string(tvb, offset, len);
642                 dcm_setSyntax(di, (char*)name);
643             }
644             reply = 0;
645             offset += len;
646             break;
647         case 0x20:              /* Presentation context */
648             id = tvb_get_guint8(tvb, offset);
649             di = lookupCtx(dcm_data, id);
650             if (!di->valid) {
651                 di = se_alloc(sizeof(struct dcmItem));
652                 di->id = id;
653                 di->valid = 1;
654                 di->xfer = NULL;
655                 di->syntax = DCM_UNK;
656                 di->next = di->prev = NULL;
657                 if (dcm_data->last) {
658                     dcm_data->last->next = di;
659                     di->prev = dcm_data->last;
660                     dcm_data->last = di;
661                 } else 
662                     dcm_data->first = dcm_data->last = di;
663             }
664             if (ti)
665                 proto_tree_add_item(dcm_tree, hf_dcm_pctxt, tvb, offset, 1, FALSE);
666             offset += 4;
667             break;
668         case 0x21:              /* Presentation context reply */
669             id = tvb_get_guint8(tvb, offset);
670             result = tvb_get_guint8(tvb, 2 + offset);
671             if (ti) {
672                 proto_tree_add_item(dcm_tree, hf_dcm_pctxt, tvb, offset, 1, FALSE);
673                 proto_tree_add_uint_format(dcm_tree, hf_dcm_pcres, tvb, 
674                     2 + offset, 1, result, 
675                     "Result 0x%x (%s)", result, dcm_PCresult2str(result));
676             }
677             if (0 == result) {
678                 reply = 1;
679                 di = lookupCtx(dcm_data, id);
680                 offset += 4;
681             } else
682                 offset += len;
683             break;
684         case 0x50:              /* User Info */
685             break;
686         case 0x51:              /* Max length */
687             mlen = tvb_get_ntohl(tvb, offset);
688             if (ti)
689                 proto_tree_add_item(dcm_tree, hf_dcm_pdu_maxlen, tvb, offset, 4, FALSE);
690             offset += len;
691             break;
692         case 0x52:              /* UID */
693             if (ti)
694                 proto_tree_add_item(dcm_tree, hf_dcm_impl, tvb, offset, len > 65 ? 65 : len, FALSE);
695             offset += len;
696             break;
697         case 0x55:              /* version */
698             if (ti)
699                 proto_tree_add_item(dcm_tree, hf_dcm_vers, tvb, offset, len > 17 ? 17 : len, FALSE);
700             offset += len;
701             break;
702         case 0x53:              /* async negotion */
703             /* hf_dcm_async */
704             offset += len;
705             break;
706         default:
707             offset += len;
708             break;
709         }
710     }
711 }
712
713 dcmItem_t *
714 lookupCtx(dcmState_t *dd, guint8 ctx)
715 {
716     dcmItem_t *di = dd->first;
717     static dcmItem_t dunk = { NULL, NULL, 0, -1, 
718         "not found - click on ASSOC Request", 
719         "not found - click on ASSOC Request", DCM_UNK };
720     while (di) {
721         if (ctx == di->id)
722             break;
723         di = di->next;
724     }
725     return di ? di : &dunk;
726 }
727
728 /* 
729 04 P-DATA-TF
730  1  1 reserved
731  2  4 length
732     - (1+) presentation data value (PDV) items
733  6      4 length
734 10      1 Presentation Context ID (odd ints 1 - 255)
735         - PDV
736 11      1 header 
737             0x01 if set, contains Message Command info, else Message Data
738             0x02 if set, contains last fragment
739  */
740 #define D_HEADER 1
741 #define D_TAG    2
742 #define D_VR     3
743 #define D_LEN2   4
744 #define D_LEN4   5
745 #define D_VALUE  6
746
747 static void 
748 dissect_dcm_data(dcmState_t *dcm_data, proto_item *ti, tvbuff_t *tvb)
749 {
750     int len, offset, toffset, state, vr, tr;
751     proto_tree *dcm_tree;
752     dcmItem_t *di;
753     guint8 ctx, syntax = DCM_UNK;
754     guint16 grp = 0, elm = 0;
755     guint32 tlen = 0, nlen;
756
757     dcm_tree = proto_item_add_subtree(ti, ett_dcm_data);
758     proto_tree_add_item(dcm_tree, hf_dcm_data_len, tvb, 6, 4, FALSE);
759     ctx = tvb_get_guint8(tvb, 10);
760     di = lookupCtx(dcm_data, ctx);
761     /*
762      * XXX - telling the user to "click on ASSOC request" is bogus if we
763      * have already identified the ASSOC request and can connect it to
764      * this mnessage; if clicking on a request prior to this one causes
765      * additional state information to be set up that would affect the
766      * dissection of this request, we should set up that state *at the
767      * time we dissect that request*, if possible, and if clicking on it
768      * doesn't change any state, clicking on the request doesn't convey
769      * any additional information.
770      */
771     proto_tree_add_uint_format(dcm_tree, hf_dcm_data_ctx, tvb, 10, 1, 
772         ctx, "Context 0x%x (%s)", ctx,
773         di->xfer == NULL ? "not found - click on ASSOC Request" :
774                            di->xfer);
775     if (DCM_UNK == di->syntax)
776         return;
777     len = offset = toffset = 11;
778     state = D_HEADER;
779     nlen = 1;
780     while (len + nlen <= dcm_data->tlen && len + nlen <= dcm_data->clen) {
781     switch (state) {
782     case D_HEADER: {
783         guint8 flags;
784         flags = tvb_get_guint8(tvb, offset);
785         proto_tree_add_uint_format(dcm_tree, hf_dcm_data_flags, tvb, offset, 1, 
786             flags, "Flags 0x%x (%s)", flags, dcm_flags2str(flags));
787         /* proto_tree_add_item(dcm_tree, hf_dcm_data_flags, tvb, offset, 1, FALSE); */
788         len++;
789         offset++;
790         if (0x1 & flags) 
791             syntax = DCM_ILE;
792         else if (DCM_UNK == di->syntax) {
793             const guint8 *val;
794             tlen = dcm_data->clen - len;
795             val = tvb_get_ptr(tvb, offset, tlen+8);
796             proto_tree_add_bytes_format(dcm_tree, hf_dcm_data_tag, tvb,
797                 offset, tlen, val, "(%04x,%04x) %-8x Unparsed data", 0, 0, tlen);
798             len = dcm_data->clen;      /* ends parsing */
799         } else
800             syntax = di->syntax;
801         state = D_TAG;
802         nlen = 4;
803         } break;                /* don't fall through -- check length */
804     case D_TAG: {
805         vr = tr = 0;
806         if (DCM_ILE & syntax) {
807             grp = tvb_get_letohs(tvb, offset);
808             elm = tvb_get_letohs(tvb, offset+2);
809             state = (DCM_EBE & syntax) ? D_VR : D_LEN4;  /* is Explicit */
810             nlen  = (DCM_EBE & syntax) ? 2 : 4;  /* is Explicit */
811         } else {
812             grp = tvb_get_ntohs(tvb, offset);
813             elm = tvb_get_ntohs(tvb, offset+2);
814             state = D_VR;
815             nlen = 2;
816         }
817         toffset = offset;
818         if (0xfffe == grp) state = D_LEN4;
819         offset += 4;
820         len += 4;
821         } break;                /* don't fall through -- check length */
822     case D_VR:  {
823         guint8 V, R;
824         vr = offset;
825         V = tvb_get_guint8(tvb, offset); offset++;
826         R = tvb_get_guint8(tvb, offset); offset++;
827         len += 2;
828         /* 4byte lengths OB, OW, OF, SQ, UN, UT */
829         state = D_LEN2;
830         nlen = 2;
831         if ((('O' == V) && ('B' == R || 'W' == R || 'F' == R) && (tr = DCM_OTH))
832             || (('U' == V) && ('N' == R || (('T' == R) && (tr = DCM_TSTR))))
833             || ('S' == V && 'Q' == R && (tr = DCM_SQ))) {
834             state = D_LEN4;
835             offset += 2;        /* skip 00 (2 bytes) */
836             len += 2;
837             nlen = 4;
838         } else if ('F' == V && 'L' == R) {
839             tr = DCM_TFLT;
840         } else if ('F' == V && 'D' == R) {
841             tr = DCM_TDBL;
842         } else if (('S' == V && 'L' == R) || ('U' == V && 'L' == R)) {
843             tr = DCM_TINT4;
844         } else if (('S' == V && 'S' == R) || ('U' == V && 'S' == R)) {
845             tr = DCM_TINT2;
846         } else if ('A' == V && 'T' == R) {
847             tr = DCM_OTH;
848         } else
849             tr = DCM_TSTR;
850 /* 
851         else if (('A' == V && ('E' == R || 'S' == R))
852             || ('C' == V && 'S' == R)
853             || ('D' == V && ('A' == R || 'S' == R || 'T' == R))
854             || ('I' == V && 'S' == R)
855             || ('L' == V && ('O' == R || 'T' == R))
856             || ('P' == V && 'N' == R)
857             || ('S' == V && ('H' == R ||| 'T' == R))
858             || ('T' == V && 'M' == R)
859             || ('U' == V && ('I' == R || 'T' == R)))
860             tr = DCM_TSTR;
861  */
862         } break;                /* don't fall through -- check length */
863     case D_LEN2: {
864         if (DCM_ILE & syntax)   /* is it LE */
865             tlen = tvb_get_letohs(tvb, offset); 
866         else
867             tlen = tvb_get_ntohs(tvb, offset); 
868         offset += 2;
869         len += 2;
870         state = D_VALUE;
871         nlen = tlen;
872         } break;
873     case D_LEN4: {
874         if (DCM_ILE & syntax)   /* is it LE */
875             tlen = tvb_get_letohl(tvb, offset); 
876         else
877             tlen = tvb_get_ntohl(tvb, offset); 
878         offset += 4;
879         len += 4;
880         state = D_VALUE;
881         nlen = tlen;
882         } break;                /* don't fall through -- check length */
883     case D_VALUE: {
884         const guint8 *val;
885         int totlen = (offset - toffset);
886         if (0xffffffff == tlen || 0xfffe == grp) {
887             val = tvb_get_ptr(tvb, toffset, totlen);
888             proto_tree_add_bytes_format(dcm_tree, hf_dcm_data_tag, tvb, 
889                 toffset, totlen, val, 
890                 "(%04x,%04x) %-8x %s", grp, elm, tlen, 
891                     dcm_tag2str(grp, elm, syntax, tvb, offset, 0, vr, tr));
892             tlen = 0;
893         /* } else if (0xfffe == grp) { */ /* need to make a sub-tree here */
894         } else {
895             totlen += tlen;
896             val = tvb_get_ptr(tvb, toffset, totlen);
897             proto_tree_add_bytes_format(dcm_tree, hf_dcm_data_tag, tvb, 
898                 toffset, totlen, val, 
899                 "(%04x,%04x) %-8x %s", grp, elm, tlen, 
900                     dcm_tag2str(grp, elm, syntax, tvb, offset, tlen, vr, tr));
901         }
902         len += tlen;
903         offset += tlen;
904         state = D_TAG;
905         nlen = 4;
906         } break;
907     }
908     }
909     if (D_VALUE == state) {
910         const guint8 *val;
911         int totlen = (offset - toffset);
912         val = tvb_get_ptr(tvb, toffset, totlen);
913         proto_tree_add_bytes_format(dcm_tree, hf_dcm_data_tag, tvb,
914             toffset, totlen, val,
915             "(%04x,%04x) %-8x %s [incomplete]", grp, elm, tlen, 
916                 dcm_tag2str(grp, elm, syntax, tvb, offset, tlen, vr, tr));
917     }
918 }
919
920 /* 
921      Originator src:srcport dest:destport
922      Acceptor   src:srcport dest:destport
923
924      conn = lookup(src:srcport, dest:destport) 
925      if (!conn)
926          look at data payload of packet
927          if no-data return false;
928          if 01 == *p && *p+10 ... *p+42 <= [ 0x20 .. printable ]
929             create conn
930  */
931
932 static void dissect_dcm_pdu(tvbuff_t *tvb,packet_info *pinfo,proto_tree *tree);
933
934 /* Code to actually dissect the packets */
935 static gboolean
936 dissect_dcm(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
937 {
938     conversation_t *conv;
939     guint8 pdu;
940     guint16 vers;
941     guint32 len, tlen;
942     dcmState_t *dcm_data = NULL;
943
944     conv = find_conversation(pinfo->fd->num, &pinfo->src, &pinfo->dst,
945         pinfo->ptype, pinfo->srcport, pinfo->destport, 0);
946
947     if (NULL != conv)   /* conversation exists */
948                         /* do we have any data for this conversation ? */
949         dcm_data = conversation_get_proto_data(conv, proto_dcm);
950     else
951         conv = conversation_new(pinfo->fd->num, &pinfo->src, &pinfo->dst, pinfo->ptype,
952             pinfo->srcport, pinfo->destport, 0);
953
954     if (NULL == dcm_data) {
955         /* No conversation found.
956          * only look for the first packet of a DICOM conversation.
957          * if we don't get the first packet, we cannot decode the rest
958          * of the session.
959          */
960         if (NULL == (dcm_data = mkds()))
961             return FALSE;       /* internal error */
962         if (10 > (tlen = tvb_reported_length(tvb))     /* not long enough */
963             || 1 != (pdu = tvb_get_guint8(tvb, 0))     /* look for the start */
964             || 1 != (vers = tvb_get_ntohs(tvb, 6)))    /* not version 1 */
965             dcm_data->valid = FALSE;            
966         else {
967             len = 6 + tvb_get_ntohl(tvb, 2);
968             if (len < tlen)
969                 dcm_data->valid = FALSE;        /* packet is > decl len */
970         }
971
972         conversation_add_proto_data(conv, proto_dcm, dcm_data);
973     }
974
975     if (FALSE == dcm_data->valid)
976         return FALSE;
977
978     if (check_col(pinfo->cinfo, COL_PROTOCOL)) 
979         col_clear(pinfo->cinfo, COL_PROTOCOL);
980
981     tcp_dissect_pdus(tvb, pinfo, tree, 1, 6, dcm_get_pdu_len, dissect_dcm_pdu);
982
983     return TRUE;
984 }
985
986 static void
987 dissect_dcm_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
988 {
989     proto_item *ti;
990     dcmState_t *dcm_data;
991     proto_tree *dcm_tree;
992     conversation_t *conv;
993     char *buf=NULL;
994     int offset = 0;
995
996     if (NULL == (conv = find_conversation(pinfo->fd->num, &pinfo->src, &pinfo->dst,
997         pinfo->ptype, pinfo->srcport, pinfo->destport, 0)))
998         return;  /* OOPS */
999
1000     dcm_data = conversation_get_proto_data(conv, proto_dcm);
1001
1002     if (check_col(pinfo->cinfo, COL_PROTOCOL)) 
1003         col_set_str(pinfo->cinfo, COL_PROTOCOL, "DCM");
1004     
1005 /* This field shows up as the "Info" column in the display; you should make
1006    it, if possible, summarize what's in the packet, so that a user looking
1007    at the list of packets can tell what type of packet it is. See section 1.5
1008    for more information.
1009    */
1010
1011     if (check_col(pinfo->cinfo, COL_INFO)) 
1012         col_clear(pinfo->cinfo, COL_INFO);
1013
1014     dcm_data->pdu = tvb_get_guint8(tvb, 0);
1015     dcm_data->tlen = tvb_get_ntohl(tvb, 2) + 6;
1016     dcm_data->clen = tvb_reported_length(tvb);
1017
1018     switch (dcm_data->pdu) {
1019     case 1:                                     /* ASSOC Request */
1020         tvb_memcpy(tvb, dcm_data->orig, 10, 16);
1021         tvb_memcpy(tvb, dcm_data->targ, 26, 16);
1022         dcm_data->orig[AEEND] = dcm_data->targ[AEEND] = 0;
1023         buf = ep_alloc(128);
1024         g_snprintf(buf, 128, "DCM ASSOC Request %s <-- %s",
1025             dcm_data->orig, dcm_data->targ);
1026         offset = 74;
1027         break;
1028     case 2:                             /* ASSOC Accept */
1029         tvb_memcpy(tvb, dcm_data->resp, 26, 16);
1030         buf = ep_alloc(128);
1031         g_snprintf(buf, 128, "DCM ASSOC Accept %s <-- %s (%s)",
1032             dcm_data->orig, dcm_data->targ, dcm_data->resp);
1033         offset = 74; 
1034         break;
1035     case 3:                                     /* ASSOC Reject */
1036         dcm_data->result = tvb_get_guint8(tvb, 7);
1037         dcm_data->source = tvb_get_guint8(tvb, 8);
1038         dcm_data->reason = tvb_get_guint8(tvb, 9);
1039         buf = ep_alloc(128);
1040         g_snprintf(buf, 128, "DCM ASSOC Reject %s <-- %s %s %s %s",
1041             dcm_data->orig, dcm_data->targ,
1042             dcm_result2str(dcm_data->result),
1043             dcm_source2str(dcm_data->source),
1044             dcm_reason2str(dcm_data->source, dcm_data->reason));
1045         offset = 10;
1046         break;
1047     case 4:                                     /* DATA */
1048         offset = 6; 
1049         buf="DCM Data";
1050         break;
1051     case 5:                                     /* RELEASE Request */
1052         buf="DCM RELEASE Request";
1053         offset = 6; 
1054         break;
1055     case 6:                                     /* RELEASE Response */
1056         buf="DCM RELEASE Response";
1057         offset = 6; 
1058         break;
1059     case 7:                                     /* ABORT */
1060         dcm_data->source = tvb_get_guint8(tvb, 8);
1061         dcm_data->reason = tvb_get_guint8(tvb, 9);
1062         buf = ep_alloc(128);
1063         g_snprintf(buf, 128, "DCM ABORT %s <-- %s %s %s", 
1064             dcm_data->orig, dcm_data->targ,
1065             (dcm_data->source == 1) ? "USER" :
1066                 (dcm_data->source == 2) ? "PROVIDER" : "",
1067             dcm_data->source == 1 ? dcm_abort2str(dcm_data->reason) : "");
1068         break;
1069     default:
1070         buf="DCM Continuation";
1071         offset = -1;                            /* cannot continue parsing */
1072         break;
1073     }
1074     if (check_col(pinfo->cinfo, COL_INFO)) 
1075         col_set_str(pinfo->cinfo, COL_INFO, buf);
1076
1077 /* In the interest of speed, if "tree" is NULL, don't do any work not
1078    necessary to generate protocol tree items. */
1079     if (tree) {
1080     proto_item *tf;
1081     ti = proto_tree_add_item(tree, proto_dcm, tvb, 0, -1, FALSE);
1082     dcm_tree = proto_item_add_subtree(ti, ett_dcm);
1083     proto_tree_add_uint_format(dcm_tree, hf_dcm_pdu, tvb, 0, dcm_data->tlen, 
1084         dcm_data->pdu, "PDU 0x%x (%s)", dcm_data->pdu, 
1085         dcm_pdu2str(dcm_data->pdu));
1086     proto_tree_add_item(dcm_tree, hf_dcm_pdu_len, tvb, 2, 4, FALSE);
1087
1088     switch (dcm_data->pdu) {
1089     case 1:                                     /* ASSOC Request */
1090     case 2:                                     /* ASSOC Accept */
1091     case 3:                                     /* ASSOC Reject */
1092     case 5:                                     /* RELEASE Request */
1093     case 6:                                     /* RELEASE Response */
1094     case 7:                                     /* ABORT */
1095         tf = proto_tree_add_string(dcm_tree, hf_dcm_pdu_type, tvb, 0, dcm_data->tlen, buf);
1096         dissect_dcm_assoc(dcm_data, tf, tvb, offset);
1097         break;
1098     case 4:                                     /* DATA */
1099         tf = proto_tree_add_string(dcm_tree, hf_dcm_pdu_type, tvb, 0, dcm_data->tlen, buf);
1100         dissect_dcm_data(dcm_data, tf, tvb);
1101         break;
1102     default:
1103         break;
1104     }
1105
1106 /* Continue adding tree items to process the packet here */
1107     } else if (1 == dcm_data->pdu || 2 == dcm_data->pdu) {
1108         dissect_dcm_assoc(dcm_data, NULL, tvb, offset);
1109     }
1110
1111 /* If this protocol has a sub-dissector call it here, see section 1.8 */
1112 }
1113
1114
1115 /* Register the protocol with Wireshark */
1116
1117 /* this format is require because a script is used to build the C function
1118    that calls all the protocol registration.
1119 */
1120
1121 void
1122 proto_register_dcm(void)
1123 {                 
1124 /* Setup list of header fields  See Section 1.6.1 for details*/
1125 static hf_register_info hf[] = {
1126     { &hf_dcm_pdu, { "PDU", "dcm.pdu",
1127         FT_UINT8, BASE_HEX, VALS(dcm_pdu_ids), 0, "", HFILL } },
1128     { &hf_dcm_pdu_len, { "PDU LENGTH", "dcm.pdu_len",
1129         FT_UINT32, BASE_HEX, NULL, 0, "", HFILL } },
1130     { &hf_dcm_pdu_type, { "PDU Detail", "dcm.pdu_detail",
1131         FT_STRING, BASE_NONE, NULL, 0, "", HFILL } },
1132     { &hf_dcm_pdi, { "Item", "dcm.pdu.pdi",
1133         FT_UINT8, BASE_HEX, VALS(dcm_pdi_ids), 0, "", HFILL } },
1134     { &hf_dcm_pdi_name, { "Application Context", "dcm.pdi.name",
1135         FT_STRING, BASE_NONE, NULL, 0, "", HFILL } },
1136     { &hf_dcm_pdi_syntax, { "Abstract Syntax", "dcm.pdi.syntax",
1137         FT_STRING, BASE_NONE, NULL, 0, "", HFILL } },
1138     { &hf_dcm_pctxt, { "Presentation Context", "dcm.pdi.ctxt",
1139         FT_UINT8, BASE_HEX, NULL, 0, "", HFILL } },
1140     { &hf_dcm_pcres, { "Presentation Context result", "dcm.pdi.result",
1141         FT_UINT8, BASE_HEX, VALS(dcm_pdi_ids), 0, "", HFILL } },
1142     { &hf_dcm_pdu_maxlen, { "MAX PDU LENGTH", "dcm.max_pdu_len",
1143         FT_UINT32, BASE_DEC, NULL, 0, "", HFILL } },
1144     { &hf_dcm_impl, { "Implementation", "dcm.pdi.impl",
1145         FT_STRING, BASE_NONE, NULL, 0, "", HFILL } },
1146     { &hf_dcm_vers, { "Version", "dcm.pdi.version",
1147         FT_STRING, BASE_NONE, NULL, 0, "", HFILL } },
1148     { &hf_dcm_async, { "Asynch", "dcm.pdi.async",
1149         FT_STRING, BASE_NONE, NULL, 0, "", HFILL } },
1150     { &hf_dcm_data_len, { "DATA LENGTH", "dcm.data.len",
1151         FT_UINT32, BASE_HEX, NULL, 0, "", HFILL } },
1152     { &hf_dcm_data_ctx, { "Data Context", "dcm.data.ctx",
1153         FT_UINT8, BASE_HEX, NULL, 0, "", HFILL } },
1154     { &hf_dcm_data_flags, { "Flags", "dcm.data.flags",
1155         FT_UINT8, BASE_HEX, NULL, 0, "", HFILL } },
1156     { &hf_dcm_data_tag, { "Tag", "dcm.data.tag",
1157         FT_BYTES, BASE_HEX, NULL, 0, "", HFILL } },
1158 /*
1159     { &hf_dcm_FIELDABBREV, { "FIELDNAME", "dcm.FIELDABBREV",
1160         FIELDTYPE, FIELDBASE, FIELDCONVERT, BITMASK, "FIELDDESCR", HFILL } },
1161  */
1162     };
1163
1164 /* Setup protocol subtree array */
1165     static gint *ett[] = {
1166             &ett_dcm,
1167             &ett_assoc,
1168             &ett_dcm_data
1169     };
1170 /* Register the protocol name and description */
1171     proto_dcm = proto_register_protocol("DICOM", "dicom", "dcm");
1172
1173 /* Required function calls to register the header fields and subtrees used */
1174     proto_register_field_array(proto_dcm, hf, array_length(hf));
1175     proto_register_subtree_array(ett, array_length(ett));
1176
1177     register_init_routine(&dcm_init);
1178 }
1179
1180
1181 /* If this dissector uses sub-dissector registration add a registration routine.
1182    This format is required because a script is used to find these routines and
1183    create the code that calls these routines.
1184 */
1185 void
1186 proto_reg_handoff_dcm(void)
1187 {
1188     dissector_handle_t dcm_handle;
1189
1190     heur_dissector_add("tcp", dissect_dcm, proto_dcm);
1191     dcm_handle = new_create_dissector_handle(dissect_dcm, proto_dcm);
1192     dissector_add("tcp.port", 104, dcm_handle);
1193 }
1194
1195 /* 
1196
1197 PDU's
1198 01 ASSOC-RQ
1199  1    1 reserved
1200  2    4 length
1201  6    2 protocol version (0x0 0x1)
1202  8    2 reserved
1203 10   16 dest aetitle
1204 26   16 src  aetitle
1205 42   32 reserved 
1206 74    - presentation data value items
1207
1208 02 A-ASSOC-AC
1209     1 reserved
1210     4 length
1211     2 protocol version (0x0 0x1)
1212     2 reserved
1213    16 dest aetitle (not checked)
1214    16 src  aetitle (not checked)
1215    32 reserved 
1216     - presentation data value items
1217
1218 03 ASSOC-RJ
1219     1 reserved
1220     4 length (4)
1221     1 reserved
1222     1 result  (1 reject perm, 2 reject transient)
1223     1 source  (1 service user, 2 service provider, 3 service profider)
1224     1 reason
1225         1 == source 
1226             1 no reason given
1227             2 application context name not supported
1228             3 calling aetitle not recognized
1229             7 called aetitle not recognized
1230         2 == source
1231             1 no reason given
1232             2 protocol version not supported
1233         3 == source
1234             1 temporary congestion
1235             2 local limit exceeded
1236
1237 04 P-DATA-TF
1238  1  1 reserved
1239  2  4 length
1240     - (1+) presentation data value (PDV) items
1241  6      4 length
1242 10      1 Presentation Context ID (odd ints 1 - 255)
1243         - PDV
1244 11      1 header 
1245             0x01 if set, contains Message Command info, else Message Data
1246             0x02 if set, contains last fragment
1247
1248 05 A-RELEASE-RQ
1249     1 reserved
1250     4 length (4)
1251     4 reserved
1252
1253 06 A-RELEASE-RP
1254     1 reserved
1255     4 length (4)
1256     4 reserved
1257
1258 07 A-ABORT
1259     1  reserved
1260     4  length (4)
1261     2  reserved
1262     1  source  (0 = user, 1 = provider)
1263     1  reason  if 1 == source (0 not spec, 1 unrecognized, 2 unexpected 4 unrecognized param, 5 unexpected param, 6 invalid param)
1264
1265
1266
1267 ITEM's
1268 10 Application Context
1269     1 reserved
1270     2 length
1271     - name
1272
1273 20 Presentation Context
1274     1 reserved
1275     2 length
1276     1 Presentation context id
1277     3 reserved
1278     - (1) abstract and (1+) transfer syntax sub-items
1279
1280 21 Presentation Context (Reply)
1281     1 reserved
1282     2 length
1283     1 ID (odd int's 1-255)
1284     1 reserved
1285     1 result (0 accept, 1 user-reject, 2 no-reason, 3 abstract not supported, 4- transfer syntax not supported)
1286     1 reserved
1287     - (1) type 40
1288
1289 30 Abstract syntax 
1290     1 reserved
1291     2 length
1292     - name (<= 64)
1293
1294 40 Transfer syntax
1295     1 reserved
1296     2 length
1297     - name (<= 64)
1298
1299 50 user information
1300     1 reserved
1301     2 length
1302     - user data
1303
1304 51 max length
1305     1 reserved
1306     2 length (4)
1307     4 max PDU lengths
1308  */