gmemchunk -> se_alloc() improvements
[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  * Ethereal - Network traffic analyzer
21  * By Gerald Combs <gerald@ethereal.com>
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 Ethereal 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 ethereal
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     static char buf[512+1];     /* bad form ??? */
506     const guint8 *vval;
507     size_t vval_len;
508     char *p;
509     guint32 tag, val32;
510     guint16 val16;
511     dcmTag_t *dtag;
512     size_t pl;
513     static dcmTag_t utag = { 0, 0, "(unknown)" };
514
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, sizeof(buf), "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(sizeof(buf) > strlen(dtag->desc));
528     strcpy(buf, dtag->desc);
529     pl = sizeof(buf) - strlen(buf);
530     p = buf + strlen(buf);
531     if (vr > 0) {
532         vval = tvb_format_text(tvb, vr, 2);
533         *p++ = ' ';
534         *p++ = '[';
535         strcpy(p, vval);
536         p += strlen(vval);
537         *p++ = ']';
538         *p = 0;
539         pl -= 5;
540     }
541
542     switch (tr > 0 ? tr : dtag->dtype) {
543     case DCM_TSTR:
544     default:            /* try ascii */
545         *p++ = ' ';
546         vval = tvb_format_text(tvb, offset, len);
547         vval_len = strlen(vval);
548         if (vval_len > pl) {
549             strncpy(p, vval, pl - 6);
550             p += pl - 6;
551             strcpy(p, "[...]");
552         } else {
553             strncpy(p, vval, vval_len);
554             p += vval_len;
555             *p = 0;
556         }
557         break;
558     case DCM_TINT2:
559         if (DCM_ILE & syntax) 
560              val16 = tvb_get_letohs(tvb, offset);
561         else val16 = tvb_get_ntohs(tvb, offset);
562         sprintf(p, " 0x%x (%d)", val16, val16);
563         break;
564     case DCM_TINT4:
565         if (DCM_ILE & syntax) 
566              val32 = tvb_get_letohl(tvb, offset); 
567         else val32 = tvb_get_ntohl(tvb, offset); 
568         sprintf(p, " 0x%x (%d)", val32, val32);
569         break;
570     case DCM_TFLT: {
571         gfloat valf;
572         if (DCM_ILE & syntax) 
573              valf = tvb_get_letohieee_float(tvb, offset); 
574         else valf = tvb_get_ntohieee_float(tvb, offset); 
575         sprintf(p, " (%f)", valf);
576         } break;
577     case DCM_TDBL: {
578         gdouble vald;
579         if (DCM_ILE & syntax) 
580              vald = tvb_get_letohieee_double(tvb, offset); 
581         else vald = tvb_get_ntohieee_double(tvb, offset); 
582         sprintf(p, " (%f)", vald);
583         } break;
584     case DCM_TSTAT: /* call dcm_rsp2str() on TINT2 */
585         if (DCM_ILE & syntax) 
586              val16 = tvb_get_letohs(tvb, offset);
587         else val16 = tvb_get_ntohs(tvb, offset);
588         sprintf(p, " 0x%x '%s'", val16, dcm_rsp2str(val16));
589         break;
590     case DCM_TCMD:   /* call dcm_cmd2str() on TINT2 */
591         if (DCM_ILE & syntax) 
592              val16 = tvb_get_letohs(tvb, offset);
593         else val16 = tvb_get_ntohs(tvb, offset);
594         sprintf(p, " 0x%x '%s'", val16, dcm_cmd2str(val16));
595         break;
596     case DCM_SQ:        /* Sequence */
597     case DCM_OTH:       /* Other BYTE, WORD, ... */
598     case DCM_TRET:      /* Retired */
599         break;
600     }
601     return buf;
602 }
603
604 static guint
605 dcm_get_pdu_len(tvbuff_t *tvb, int offset)
606 {
607     guint32 len;
608
609     len = tvb_get_ntohl(tvb, 2 + offset);
610     return len + 6;             /* add in fixed header part */
611 }
612
613 static void 
614 dissect_dcm_assoc(dcmState_t *dcm_data, proto_item *ti, tvbuff_t *tvb, int offset)
615
616     proto_tree *dcm_tree = NULL;
617     dcmItem_t *di = NULL;
618     guint8 id, *name, result;
619     int reply = 0;
620
621     if (!ti)
622         return;
623
624     dcm_tree = proto_item_add_subtree(ti, ett_assoc);
625     while (-1 < offset && offset < (int) dcm_data->clen) {
626         guint16 len;
627         guint32 mlen;
628         id = tvb_get_guint8(tvb, offset);
629         len = tvb_get_ntohs(tvb, 2 + offset);
630         if (ti)
631             proto_tree_add_uint_format(dcm_tree, hf_dcm_pdi, tvb,
632                 offset, 4+len, id, "Item 0x%x (%s)", id, dcm_pdu2str(id));
633         offset += 4;
634         switch (id) {
635         case 0x10:              /* App context */
636             if (ti)
637                 proto_tree_add_item(dcm_tree, hf_dcm_pdi_name, tvb, offset, len > 65 ? 65 : len, FALSE);
638             offset += len;
639             break;
640         case 0x30:              /* Abstract syntax */
641             if (ti)
642                 proto_tree_add_item(dcm_tree, hf_dcm_pdi_syntax, tvb, offset, len > 65 ? 65 : len, FALSE);
643             offset += len;
644             break;
645         case 0x40:              /* Transfer syntax */
646             if (ti)
647                 proto_tree_add_item(dcm_tree, hf_dcm_pdi_syntax, tvb, offset, len > 65 ? 65 : len, FALSE);
648             if (reply && di && di->valid) {
649                 name = tvb_get_ephemeral_string(tvb, offset, len);
650                 dcm_setSyntax(di, name);
651             }
652             reply = 0;
653             offset += len;
654             break;
655         case 0x20:              /* Presentation context */
656             id = tvb_get_guint8(tvb, offset);
657             di = lookupCtx(dcm_data, id);
658             if (!di->valid) {
659                 di = se_alloc(sizeof(struct dcmItem));
660                 di->id = id;
661                 di->valid = 1;
662                 di->xfer = NULL;
663                 di->syntax = DCM_UNK;
664                 di->next = di->prev = NULL;
665                 if (dcm_data->last) {
666                     dcm_data->last->next = di;
667                     di->prev = dcm_data->last;
668                     dcm_data->last = di;
669                 } else 
670                     dcm_data->first = dcm_data->last = di;
671             }
672             if (ti)
673                 proto_tree_add_item(dcm_tree, hf_dcm_pctxt, tvb, offset, 1, FALSE);
674             offset += 4;
675             break;
676         case 0x21:              /* Presentation context reply */
677             id = tvb_get_guint8(tvb, offset);
678             result = tvb_get_guint8(tvb, 2 + offset);
679             if (ti) {
680                 proto_tree_add_item(dcm_tree, hf_dcm_pctxt, tvb, offset, 1, FALSE);
681                 proto_tree_add_uint_format(dcm_tree, hf_dcm_pcres, tvb, 
682                     2 + offset, 1, result, 
683                     "Result 0x%x (%s)", result, dcm_PCresult2str(result));
684             }
685             if (0 == result) {
686                 reply = 1;
687                 di = lookupCtx(dcm_data, id);
688                 offset += 4;
689             } else
690                 offset += len;
691             break;
692         case 0x50:              /* User Info */
693             break;
694         case 0x51:              /* Max length */
695             mlen = tvb_get_ntohl(tvb, offset);
696             if (ti)
697                 proto_tree_add_item(dcm_tree, hf_dcm_pdu_maxlen, tvb, offset, 4, FALSE);
698             offset += len;
699             break;
700         case 0x52:              /* UID */
701             if (ti)
702                 proto_tree_add_item(dcm_tree, hf_dcm_impl, tvb, offset, len > 65 ? 65 : len, FALSE);
703             offset += len;
704             break;
705         case 0x55:              /* version */
706             if (ti)
707                 proto_tree_add_item(dcm_tree, hf_dcm_vers, tvb, offset, len > 17 ? 17 : len, FALSE);
708             offset += len;
709             break;
710         case 0x53:              /* async negotion */
711             /* hf_dcm_async */
712             offset += len;
713             break;
714         default:
715             offset += len;
716             break;
717         }
718     }
719 }
720
721 dcmItem_t *
722 lookupCtx(dcmState_t *dd, guint8 ctx)
723 {
724     dcmItem_t *di = dd->first;
725     static dcmItem_t dunk = { NULL, NULL, 0, -1, 
726         "not found - click on ASSOC Request", 
727         "not found - click on ASSOC Request", DCM_UNK };
728     while (di) {
729         if (ctx == di->id)
730             break;
731         di = di->next;
732     }
733     return di ? di : &dunk;
734 }
735
736 /* 
737 04 P-DATA-TF
738  1  1 reserved
739  2  4 length
740     - (1+) presentation data value (PDV) items
741  6      4 length
742 10      1 Presentation Context ID (odd ints 1 - 255)
743         - PDV
744 11      1 header 
745             0x01 if set, contains Message Command info, else Message Data
746             0x02 if set, contains last fragment
747  */
748 #define D_HEADER 1
749 #define D_TAG    2
750 #define D_VR     3
751 #define D_LEN2   4
752 #define D_LEN4   5
753 #define D_VALUE  6
754
755 static void 
756 dissect_dcm_data(dcmState_t *dcm_data, proto_item *ti, tvbuff_t *tvb)
757 {
758     int len, offset, toffset, state, vr, tr;
759     proto_tree *dcm_tree;
760     dcmItem_t *di;
761     guint8 ctx, syntax = DCM_UNK;
762     guint16 grp = 0, elm = 0;
763     guint32 tlen = 0, nlen;
764
765     dcm_tree = proto_item_add_subtree(ti, ett_dcm_data);
766     proto_tree_add_item(dcm_tree, hf_dcm_data_len, tvb, 6, 4, FALSE);
767     ctx = tvb_get_guint8(tvb, 10);
768     di = lookupCtx(dcm_data, ctx);
769     /*
770      * XXX - telling the user to "click on ASSOC request" is bogus if we
771      * have already identified the ASSOC request and can connect it to
772      * this mnessage; if clicking on a request prior to this one causes
773      * additional state information to be set up that would affect the
774      * dissection of this request, we should set up that state *at the
775      * time we dissect that request*, if possible, and if clicking on it
776      * doesn't change any state, clicking on the request doesn't convey
777      * any additional information.
778      */
779     proto_tree_add_uint_format(dcm_tree, hf_dcm_data_ctx, tvb, 10, 1, 
780         ctx, "Context 0x%x (%s)", ctx,
781         di->xfer == NULL ? "not found - click on ASSOC Request" :
782                            di->xfer);
783     if (DCM_UNK == di->syntax)
784         return;
785     len = offset = toffset = 11;
786     state = D_HEADER;
787     nlen = 1;
788     while (len + nlen <= dcm_data->tlen && len + nlen <= dcm_data->clen) {
789     switch (state) {
790     case D_HEADER: {
791         guint8 flags;
792         flags = tvb_get_guint8(tvb, offset);
793         proto_tree_add_uint_format(dcm_tree, hf_dcm_data_flags, tvb, offset, 1, 
794             flags, "Flags 0x%x (%s)", flags, dcm_flags2str(flags));
795         /* proto_tree_add_item(dcm_tree, hf_dcm_data_flags, tvb, offset, 1, FALSE); */
796         len++;
797         offset++;
798         if (0x1 & flags) 
799             syntax = DCM_ILE;
800         else if (DCM_UNK == di->syntax) {
801             const guint8 *val;
802             tlen = dcm_data->clen - len;
803             val = tvb_get_ptr(tvb, offset, tlen+8);
804             proto_tree_add_bytes_format(dcm_tree, hf_dcm_data_tag, tvb,
805                 offset, tlen, val, "(%04x,%04x) %-8x Unparsed data", 0, 0, tlen);
806             len = dcm_data->clen;      /* ends parsing */
807         } else
808             syntax = di->syntax;
809         state = D_TAG;
810         nlen = 4;
811         } break;                /* don't fall through -- check length */
812     case D_TAG: {
813         vr = tr = 0;
814         if (DCM_ILE & syntax) {
815             grp = tvb_get_letohs(tvb, offset);
816             elm = tvb_get_letohs(tvb, offset+2);
817             state = (DCM_EBE & syntax) ? D_VR : D_LEN4;  /* is Explicit */
818             nlen  = (DCM_EBE & syntax) ? 2 : 4;  /* is Explicit */
819         } else {
820             grp = tvb_get_ntohs(tvb, offset);
821             elm = tvb_get_ntohs(tvb, offset+2);
822             state = D_VR;
823             nlen = 2;
824         }
825         toffset = offset;
826         if (0xfffe == grp) state = D_LEN4;
827         offset += 4;
828         len += 4;
829         } break;                /* don't fall through -- check length */
830     case D_VR:  {
831         guint8 V, R;
832         vr = offset;
833         V = tvb_get_guint8(tvb, offset); offset++;
834         R = tvb_get_guint8(tvb, offset); offset++;
835         len += 2;
836         /* 4byte lengths OB, OW, OF, SQ, UN, UT */
837         state = D_LEN2;
838         nlen = 2;
839         if ((('O' == V) && ('B' == R || 'W' == R || 'F' == R) && (tr = DCM_OTH))
840             || (('U' == V) && ('N' == R || (('T' == R) && (tr = DCM_TSTR))))
841             || ('S' == V && 'Q' == R && (tr = DCM_SQ))) {
842             state = D_LEN4;
843             offset += 2;        /* skip 00 (2 bytes) */
844             len += 2;
845             nlen = 4;
846         } else if ('F' == V && 'L' == R) {
847             tr = DCM_TFLT;
848         } else if ('F' == V && 'D' == R) {
849             tr = DCM_TDBL;
850         } else if (('S' == V && 'L' == R) || ('U' == V && 'L' == R)) {
851             tr = DCM_TINT4;
852         } else if (('S' == V && 'S' == R) || ('U' == V && 'S' == R)) {
853             tr = DCM_TINT2;
854         } else if ('A' == V && 'T' == R) {
855             tr = DCM_OTH;
856         } else
857             tr = DCM_TSTR;
858 /* 
859         else if (('A' == V && ('E' == R || 'S' == R))
860             || ('C' == V && 'S' == R)
861             || ('D' == V && ('A' == R || 'S' == R || 'T' == R))
862             || ('I' == V && 'S' == R)
863             || ('L' == V && ('O' == R || 'T' == R))
864             || ('P' == V && 'N' == R)
865             || ('S' == V && ('H' == R ||| 'T' == R))
866             || ('T' == V && 'M' == R)
867             || ('U' == V && ('I' == R || 'T' == R)))
868             tr = DCM_TSTR;
869  */
870         } break;                /* don't fall through -- check length */
871     case D_LEN2: {
872         if (DCM_ILE & syntax)   /* is it LE */
873             tlen = tvb_get_letohs(tvb, offset); 
874         else
875             tlen = tvb_get_ntohs(tvb, offset); 
876         offset += 2;
877         len += 2;
878         state = D_VALUE;
879         nlen = tlen;
880         } break;
881     case D_LEN4: {
882         if (DCM_ILE & syntax)   /* is it LE */
883             tlen = tvb_get_letohl(tvb, offset); 
884         else
885             tlen = tvb_get_ntohl(tvb, offset); 
886         offset += 4;
887         len += 4;
888         state = D_VALUE;
889         nlen = tlen;
890         } break;                /* don't fall through -- check length */
891     case D_VALUE: {
892         const guint8 *val;
893         int totlen = (offset - toffset);
894         if (0xffffffff == tlen || 0xfffe == grp) {
895             val = tvb_get_ptr(tvb, toffset, totlen);
896             proto_tree_add_bytes_format(dcm_tree, hf_dcm_data_tag, tvb, 
897                 toffset, totlen, val, 
898                 "(%04x,%04x) %-8x %s", grp, elm, tlen, 
899                     dcm_tag2str(grp, elm, syntax, tvb, offset, 0, vr, tr));
900             tlen = 0;
901         /* } else if (0xfffe == grp) { */ /* need to make a sub-tree here */
902         } else {
903             totlen += tlen;
904             val = tvb_get_ptr(tvb, toffset, totlen);
905             proto_tree_add_bytes_format(dcm_tree, hf_dcm_data_tag, tvb, 
906                 toffset, totlen, val, 
907                 "(%04x,%04x) %-8x %s", grp, elm, tlen, 
908                     dcm_tag2str(grp, elm, syntax, tvb, offset, tlen, vr, tr));
909         }
910         len += tlen;
911         offset += tlen;
912         state = D_TAG;
913         nlen = 4;
914         } break;
915     }
916     }
917     if (D_VALUE == state) {
918         const guint8 *val;
919         int totlen = (offset - toffset);
920         val = tvb_get_ptr(tvb, toffset, totlen);
921         proto_tree_add_bytes_format(dcm_tree, hf_dcm_data_tag, tvb,
922             toffset, totlen, val,
923             "(%04x,%04x) %-8x %s [incomplete]", grp, elm, tlen, 
924                 dcm_tag2str(grp, elm, syntax, tvb, offset, tlen, vr, tr));
925     }
926 }
927
928 /* 
929      Originator src:srcport dest:destport
930      Acceptor   src:srcport dest:destport
931
932      conn = lookup(src:srcport, dest:destport) 
933      if (!conn)
934          look at data payload of packet
935          if no-data return false;
936          if 01 == *p && *p+10 ... *p+42 <= [ 0x20 .. printable ]
937             create conn
938  */
939
940 static void dissect_dcm_pdu(tvbuff_t *tvb,packet_info *pinfo,proto_tree *tree);
941
942 /* Code to actually dissect the packets */
943 static gboolean
944 dissect_dcm(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
945 {
946     conversation_t *conv;
947     guint8 pdu;
948     guint16 vers;
949     guint32 len, tlen;
950     dcmState_t *dcm_data = NULL;
951
952     conv = find_conversation(pinfo->fd->num, &pinfo->src, &pinfo->dst,
953         pinfo->ptype, pinfo->srcport, pinfo->destport, 0);
954
955     if (NULL != conv)   /* conversation exists */
956                         /* do we have any data for this conversation ? */
957         dcm_data = conversation_get_proto_data(conv, proto_dcm);
958     else
959         conv = conversation_new(pinfo->fd->num, &pinfo->src, &pinfo->dst, pinfo->ptype,
960             pinfo->srcport, pinfo->destport, 0);
961
962     if (NULL == dcm_data) {
963         /* No conversation found.
964          * only look for the first packet of a DICOM conversation.
965          * if we don't get the first packet, we cannot decode the rest
966          * of the session.
967          */
968         if (NULL == (dcm_data = mkds()))
969             return FALSE;       /* internal error */
970         if (10 > (tlen = tvb_reported_length(tvb))     /* not long enough */
971             || 1 != (pdu = tvb_get_guint8(tvb, 0))     /* look for the start */
972             || 1 != (vers = tvb_get_ntohs(tvb, 6)))    /* not version 1 */
973             dcm_data->valid = FALSE;            
974         else {
975             len = 6 + tvb_get_ntohl(tvb, 2);
976             if (len < tlen)
977                 dcm_data->valid = FALSE;        /* packet is > decl len */
978         }
979
980         conversation_add_proto_data(conv, proto_dcm, dcm_data);
981     }
982
983     if (FALSE == dcm_data->valid)
984         return FALSE;
985
986     if (check_col(pinfo->cinfo, COL_PROTOCOL)) 
987         col_clear(pinfo->cinfo, COL_PROTOCOL);
988
989     tcp_dissect_pdus(tvb, pinfo, tree, 1, 6, dcm_get_pdu_len, dissect_dcm_pdu);
990
991     return TRUE;
992 }
993
994 static void
995 dissect_dcm_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
996 {
997     proto_item *ti;
998     dcmState_t *dcm_data;
999     proto_tree *dcm_tree;
1000     conversation_t *conv;
1001     char *buf;
1002     int offset = 0;
1003
1004     if (NULL == (conv = find_conversation(pinfo->fd->num, &pinfo->src, &pinfo->dst,
1005         pinfo->ptype, pinfo->srcport, pinfo->destport, 0)))
1006         return;  /* OOPS */
1007
1008     dcm_data = conversation_get_proto_data(conv, proto_dcm);
1009
1010     if (check_col(pinfo->cinfo, COL_PROTOCOL)) 
1011         col_set_str(pinfo->cinfo, COL_PROTOCOL, "DCM");
1012     
1013 /* This field shows up as the "Info" column in the display; you should make
1014    it, if possible, summarize what's in the packet, so that a user looking
1015    at the list of packets can tell what type of packet it is. See section 1.5
1016    for more information.
1017    */
1018
1019     if (check_col(pinfo->cinfo, COL_INFO)) 
1020         col_clear(pinfo->cinfo, COL_INFO);
1021
1022     dcm_data->pdu = tvb_get_guint8(tvb, 0);
1023     dcm_data->tlen = tvb_get_ntohl(tvb, 2) + 6;
1024     dcm_data->clen = tvb_reported_length(tvb);
1025
1026     switch (dcm_data->pdu) {
1027     case 1:                                     /* ASSOC Request */
1028         tvb_memcpy(tvb, dcm_data->orig, 10, 16);
1029         tvb_memcpy(tvb, dcm_data->targ, 26, 16);
1030         dcm_data->orig[AEEND] = dcm_data->targ[AEEND] = 0;
1031         buf = g_malloc(128);
1032         g_snprintf(buf, 128, "DCM ASSOC Request %s <-- %s",
1033             dcm_data->orig, dcm_data->targ);
1034         offset = 74;
1035         break;
1036     case 2:                             /* ASSOC Accept */
1037         tvb_memcpy(tvb, dcm_data->resp, 26, 16);
1038         buf = g_malloc(128);
1039         g_snprintf(buf, 128, "DCM ASSOC Accept %s <-- %s (%s)",
1040             dcm_data->orig, dcm_data->targ, dcm_data->resp);
1041         offset = 74; 
1042         break;
1043     case 3:                                     /* ASSOC Reject */
1044         dcm_data->result = tvb_get_guint8(tvb, 7);
1045         dcm_data->source = tvb_get_guint8(tvb, 8);
1046         dcm_data->reason = tvb_get_guint8(tvb, 9);
1047         buf = g_malloc(128);
1048         g_snprintf(buf, 128, "DCM ASSOC Reject %s <-- %s %s %s %s",
1049             dcm_data->orig, dcm_data->targ,
1050             dcm_result2str(dcm_data->result),
1051             dcm_source2str(dcm_data->source),
1052             dcm_reason2str(dcm_data->source, dcm_data->reason));
1053         offset = 10;
1054         break;
1055     case 4:                                     /* DATA */
1056         offset = 6; 
1057         buf = g_malloc(128);
1058         strcpy(buf, "DCM Data");
1059         break;
1060     case 5:                                     /* RELEASE Request */
1061         buf = g_malloc(128);
1062         strcpy(buf, "DCM RELEASE Request");
1063         offset = 6; 
1064         break;
1065     case 6:                                     /* RELEASE Response */
1066         buf = g_malloc(128);
1067         strcpy(buf, "DCM RELEASE Response");
1068         offset = 6; 
1069         break;
1070     case 7:                                     /* ABORT */
1071         dcm_data->source = tvb_get_guint8(tvb, 8);
1072         dcm_data->reason = tvb_get_guint8(tvb, 9);
1073         buf = g_malloc(128);
1074         g_snprintf(buf, 128, "DCM ABORT %s <-- %s %s %s", 
1075             dcm_data->orig, dcm_data->targ,
1076             (dcm_data->source == 1) ? "USER" :
1077                 (dcm_data->source == 2) ? "PROVIDER" : "",
1078             dcm_data->source == 1 ? dcm_abort2str(dcm_data->reason) : "");
1079         break;
1080     default:
1081         buf = g_malloc(128);
1082         strcpy(buf, "DCM Continuation");
1083         offset = -1;                            /* cannot continue parsing */
1084         break;
1085     }
1086     if (check_col(pinfo->cinfo, COL_INFO)) 
1087         col_set_str(pinfo->cinfo, COL_INFO, buf);
1088
1089 /* In the interest of speed, if "tree" is NULL, don't do any work not
1090    necessary to generate protocol tree items. */
1091     if (tree) {
1092     proto_item *tf;
1093     ti = proto_tree_add_item(tree, proto_dcm, tvb, 0, -1, FALSE);
1094     dcm_tree = proto_item_add_subtree(ti, ett_dcm);
1095     proto_tree_add_uint_format(dcm_tree, hf_dcm_pdu, tvb, 0, dcm_data->tlen, 
1096         dcm_data->pdu, "PDU 0x%x (%s)", dcm_data->pdu, 
1097         dcm_pdu2str(dcm_data->pdu));
1098     proto_tree_add_item(dcm_tree, hf_dcm_pdu_len, tvb, 2, 4, FALSE);
1099
1100     switch (dcm_data->pdu) {
1101     case 1:                                     /* ASSOC Request */
1102     case 2:                                     /* ASSOC Accept */
1103     case 3:                                     /* ASSOC Reject */
1104     case 5:                                     /* RELEASE Request */
1105     case 6:                                     /* RELEASE Response */
1106     case 7:                                     /* ABORT */
1107         tf = proto_tree_add_string(dcm_tree, hf_dcm_pdu_type, tvb, 0, dcm_data->tlen, buf);
1108         dissect_dcm_assoc(dcm_data, tf, tvb, offset);
1109         break;
1110     case 4:                                     /* DATA */
1111         tf = proto_tree_add_string(dcm_tree, hf_dcm_pdu_type, tvb, 0, dcm_data->tlen, buf);
1112         dissect_dcm_data(dcm_data, tf, tvb);
1113         break;
1114     default:
1115         break;
1116     }
1117
1118 /* Continue adding tree items to process the packet here */
1119     } else if (1 == dcm_data->pdu || 2 == dcm_data->pdu) {
1120         dissect_dcm_assoc(dcm_data, NULL, tvb, offset);
1121     }
1122
1123 /* If this protocol has a sub-dissector call it here, see section 1.8 */
1124 }
1125
1126
1127 /* Register the protocol with Ethereal */
1128
1129 /* this format is require because a script is used to build the C function
1130    that calls all the protocol registration.
1131 */
1132
1133 void
1134 proto_register_dcm(void)
1135 {                 
1136 /* Setup list of header fields  See Section 1.6.1 for details*/
1137 static hf_register_info hf[] = {
1138     { &hf_dcm_pdu, { "PDU", "dcm.pdu",
1139         FT_UINT8, BASE_HEX, VALS(dcm_pdu_ids), 0, "", HFILL } },
1140     { &hf_dcm_pdu_len, { "PDU LENGTH", "dcm.pdu_len",
1141         FT_UINT32, BASE_HEX, NULL, 0, "", HFILL } },
1142     { &hf_dcm_pdu_type, { "PDU Detail", "dcm.pdu_detail",
1143         FT_STRING, BASE_NONE, NULL, 0, "", HFILL } },
1144     { &hf_dcm_pdi, { "Item", "dcm.pdu.pdi",
1145         FT_UINT8, BASE_HEX, VALS(dcm_pdi_ids), 0, "", HFILL } },
1146     { &hf_dcm_pdi_name, { "Application Context", "dcm.pdi.name",
1147         FT_STRING, BASE_NONE, NULL, 0, "", HFILL } },
1148     { &hf_dcm_pdi_syntax, { "Abstract Syntax", "dcm.pdi.syntax",
1149         FT_STRING, BASE_NONE, NULL, 0, "", HFILL } },
1150     { &hf_dcm_pctxt, { "Presentation Context", "dcm.pdi.ctxt",
1151         FT_UINT8, BASE_HEX, NULL, 0, "", HFILL } },
1152     { &hf_dcm_pcres, { "Presentation Context result", "dcm.pdi.result",
1153         FT_UINT8, BASE_HEX, VALS(dcm_pdi_ids), 0, "", HFILL } },
1154     { &hf_dcm_pdu_maxlen, { "MAX PDU LENGTH", "dcm.max_pdu_len",
1155         FT_UINT32, BASE_DEC, NULL, 0, "", HFILL } },
1156     { &hf_dcm_impl, { "Implementation", "dcm.pdi.impl",
1157         FT_STRING, BASE_NONE, NULL, 0, "", HFILL } },
1158     { &hf_dcm_vers, { "Version", "dcm.pdi.version",
1159         FT_STRING, BASE_NONE, NULL, 0, "", HFILL } },
1160     { &hf_dcm_async, { "Asynch", "dcm.pdi.async",
1161         FT_STRING, BASE_NONE, NULL, 0, "", HFILL } },
1162     { &hf_dcm_data_len, { "DATA LENGTH", "dcm.data.len",
1163         FT_UINT32, BASE_HEX, NULL, 0, "", HFILL } },
1164     { &hf_dcm_data_ctx, { "Data Context", "dcm.data.ctx",
1165         FT_UINT8, BASE_HEX, NULL, 0, "", HFILL } },
1166     { &hf_dcm_data_flags, { "Flags", "dcm.data.flags",
1167         FT_UINT8, BASE_HEX, NULL, 0, "", HFILL } },
1168     { &hf_dcm_data_tag, { "Tag", "dcm.data.tag",
1169         FT_BYTES, BASE_HEX, NULL, 0, "", HFILL } },
1170 /*
1171     { &hf_dcm_FIELDABBREV, { "FIELDNAME", "dcm.FIELDABBREV",
1172         FIELDTYPE, FIELDBASE, FIELDCONVERT, BITMASK, "FIELDDESCR", HFILL } },
1173  */
1174     };
1175
1176 /* Setup protocol subtree array */
1177     static gint *ett[] = {
1178             &ett_dcm,
1179             &ett_assoc,
1180             &ett_dcm_data
1181     };
1182 /* Register the protocol name and description */
1183     proto_dcm = proto_register_protocol("DICOM", "dicom", "dcm");
1184
1185 /* Required function calls to register the header fields and subtrees used */
1186     proto_register_field_array(proto_dcm, hf, array_length(hf));
1187     proto_register_subtree_array(ett, array_length(ett));
1188
1189     register_init_routine(&dcm_init);
1190 }
1191
1192
1193 /* If this dissector uses sub-dissector registration add a registration routine.
1194    This format is required because a script is used to find these routines and
1195    create the code that calls these routines.
1196 */
1197 void
1198 proto_reg_handoff_dcm(void)
1199 {
1200     dissector_handle_t dcm_handle;
1201
1202     heur_dissector_add("tcp", dissect_dcm, proto_dcm);
1203     dcm_handle = new_create_dissector_handle(dissect_dcm, proto_dcm);
1204     dissector_add("tcp.port", 104, dcm_handle);
1205 }
1206
1207 /* 
1208
1209 PDU's
1210 01 ASSOC-RQ
1211  1    1 reserved
1212  2    4 length
1213  6    2 protocol version (0x0 0x1)
1214  8    2 reserved
1215 10   16 dest aetitle
1216 26   16 src  aetitle
1217 42   32 reserved 
1218 74    - presentation data value items
1219
1220 02 A-ASSOC-AC
1221     1 reserved
1222     4 length
1223     2 protocol version (0x0 0x1)
1224     2 reserved
1225    16 dest aetitle (not checked)
1226    16 src  aetitle (not checked)
1227    32 reserved 
1228     - presentation data value items
1229
1230 03 ASSOC-RJ
1231     1 reserved
1232     4 length (4)
1233     1 reserved
1234     1 result  (1 reject perm, 2 reject transient)
1235     1 source  (1 service user, 2 service provider, 3 service profider)
1236     1 reason
1237         1 == source 
1238             1 no reason given
1239             2 application context name not supported
1240             3 calling aetitle not recognized
1241             7 called aetitle not recognized
1242         2 == source
1243             1 no reason given
1244             2 protocol version not supported
1245         3 == source
1246             1 temporary congestion
1247             2 local limit exceeded
1248
1249 04 P-DATA-TF
1250  1  1 reserved
1251  2  4 length
1252     - (1+) presentation data value (PDV) items
1253  6      4 length
1254 10      1 Presentation Context ID (odd ints 1 - 255)
1255         - PDV
1256 11      1 header 
1257             0x01 if set, contains Message Command info, else Message Data
1258             0x02 if set, contains last fragment
1259
1260 05 A-RELEASE-RQ
1261     1 reserved
1262     4 length (4)
1263     4 reserved
1264
1265 06 A-RELEASE-RP
1266     1 reserved
1267     4 length (4)
1268     4 reserved
1269
1270 07 A-ABORT
1271     1  reserved
1272     4  length (4)
1273     2  reserved
1274     1  source  (0 = user, 1 = provider)
1275     1  reason  if 1 == source (0 not spec, 1 unrecognized, 2 unexpected 4 unrecognized param, 5 unexpected param, 6 invalid param)
1276
1277
1278
1279 ITEM's
1280 10 Application Context
1281     1 reserved
1282     2 length
1283     - name
1284
1285 20 Presentation Context
1286     1 reserved
1287     2 length
1288     1 Presentation context id
1289     3 reserved
1290     - (1) abstract and (1+) transfer syntax sub-items
1291
1292 21 Presentation Context (Reply)
1293     1 reserved
1294     2 length
1295     1 ID (odd int's 1-255)
1296     1 reserved
1297     1 result (0 accept, 1 user-reject, 2 no-reason, 3 abstract not supported, 4- transfer syntax not supported)
1298     1 reserved
1299     - (1) type 40
1300
1301 30 Abstract syntax 
1302     1 reserved
1303     2 length
1304     - name (<= 64)
1305
1306 40 Transfer syntax
1307     1 reserved
1308     2 length
1309     - name (<= 64)
1310
1311 50 user information
1312     1 reserved
1313     2 length
1314     - user data
1315
1316 51 max length
1317     1 reserved
1318     2 length (4)
1319     4 max PDU lengths
1320  */