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