2 * Routines for DICOM dissection
3 * Copyright 2003, Rich Coe <Richard.Coe@med.ge.com>
5 * DICOM communication protocol
6 * http://medical.nema.org/dicom/2003.html
7 * DICOM Part 8: Network Communication Support for Message Exchange
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.)
20 * Ethereal - Network traffic analyzer
21 * By Gerald Combs <gerald@ethereal.com>
22 * Copyright 1998 Gerald Combs
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.
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.
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.
44 * This is my first pass at a Ethereal dissector to display
45 * DICOM (Digital Imaging and Communications in Medicine) packets.
47 * - It currently displays most of the DICOM packets.
49 * - I've used it to debug Query/Retrieve, Storage, and Echo protocols.
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.
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.
62 * - The 'value to string' routines should probably be hash lookups.
65 * - Fixed the heuristic code -- sometimes a conversation already exists
66 * - Fixed the dissect code to display all the tags in the pdu
69 * - fix memory leak when Assoc packet is processed repeatedly in ethereal
71 * - removed unused partial packet flag
73 * - added better support for DICOM VR
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
79 * - remove my goofy attempt at trying to get access to the fragmented packets
80 * (anyone have an idea on how to fix this ???)
82 * - process all the data in the Assoc packet even if display is off
84 * - limit display of data in Assoc packet to defined size of the data even
85 * if reported size is larger
87 * - show the last tag in a packet as [incomplete] if we don't have all the data
89 * - added framework for reporting DICOM async negotiation (not finished)
90 * (I'm not aware of an implementation which currently supports this)
92 * - still need to fix display of continuation packets
104 #include <epan/packet.h>
105 #include <epan/emem.h>
106 #include <epan/strutil.h>
107 #include <epan/conversation.h>
109 #include "packet-tcp.h"
111 /* Initialize the protocol and registered fields */
112 static int proto_dcm = -1;
113 static int hf_dcm_pdu = -1,
115 hf_dcm_pdu_type = -1,
117 hf_dcm_pdi_name = -1,
118 hf_dcm_pdi_syntax = -1,
121 hf_dcm_pdu_maxlen = -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;
130 /* Initialize the subtree pointers */
131 static gint ett_dcm = -1, ett_assoc = -1, ett_dcm_data = -1;
133 static const value_string dcm_pdu_ids[] = {
134 { 1, "ASSOC Request" },
135 { 2, "ASSOC Accept" },
136 { 3, "ASSOC Reject" },
138 { 5, "RELEASE Request" },
139 { 6, "RELEASE Response" },
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" },
160 struct dcmItem *next, *prev;
162 guint8 id; /* 0x20 Presentation Context */
163 const guint8 *abs; /* 0x30 Abstract syntax */
164 char *xfer; /* 0x40 Transfer 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 */
171 typedef struct dcmItem dcmItem_t;
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_ }; */
181 guint8 orig[1+AEEND], targ[1+AEEND], resp[1+AEEND], source, result, reason;
183 typedef struct dcmState dcmState_t;
194 #define DCM_TSTAT 6 /* call dcm_rsp2str() on TINT2 */
197 #define DCM_SQ 9 /* sequence */
198 #define DCM_OTH 10 /* other */
200 typedef struct dcmTag dcmTag_t;
202 static GHashTable *dcm_tagTable = NULL;
204 dcmItem_t * lookupCtx(dcmState_t *dd, guint8 ctx);
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" },
278 if (NULL == dcm_tagTable) {
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));
292 if (NULL == (ds = (dcmState_t *) g_malloc(sizeof(dcmState_t)))) {
296 ds->tlen = ds->rlen = 0;
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;
306 dcm_pdu2str(guint8 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;
330 dcm_result2str(guint8 result)
334 case 1: s = "Reject Permanent"; break;
335 case 2: s = "Reject Transient"; break;
342 dcm_source2str(guint8 source)
346 case 1: s = "User"; break;
347 case 2: s = "Provider (ACSE)"; break;
348 case 3: s = "Provider (Presentation)"; break;
355 dcm_reason2str(guint8 source, guint8 reason)
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;
364 } else if (2 == source) switch (reason) {
365 case 1: s = "No reason"; break;
366 case 2: s = "protocol unsupported"; break;
368 } else if (3 == source) switch (reason) {
369 case 1: s = "temporary congestion"; break;
370 case 2: s = "local limit exceeded"; break;
377 dcm_abort2str(guint8 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;
393 dcm_PCresult2str(guint8 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;
408 dcm_flags2str(guint8 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 */
422 dcm_cmd2str(guint16 us)
425 /* there should be a better way to do this */
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;
456 dcm_rsp2str(guint16 us)
460 case 0x0000: s = "Success"; break;
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;
470 if (0xC000 == (0xF000 & us)) s = "Failed: Unable to Process";
475 dcm_setSyntax(dcmItem_t *di, char *name)
477 if (NULL == di) return;
478 if (di->xfer != NULL)
479 g_free(di->xfer); /* free prev allocated xfer */
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
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 */
503 dcm_tag2str(guint16 grp, guint16 elm, guint8 syntax, tvbuff_t *tvb, int offset, guint32 len, int vr, int tr)
505 static char buf[512+1]; /* bad form ??? */
513 static dcmTag_t utag = { 0, 0, "(unknown)" };
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);
523 tag = (grp << 16) | elm;
524 if (NULL == (dtag = g_hash_table_lookup(dcm_tagTable, GUINT_TO_POINTER(tag))))
527 DISSECTOR_ASSERT(sizeof(buf) > strlen(dtag->desc));
528 strcpy(buf, dtag->desc);
529 pl = sizeof(buf) - strlen(buf);
530 p = buf + strlen(buf);
532 vval = tvb_format_text(tvb, vr, 2);
542 switch (tr > 0 ? tr : dtag->dtype) {
544 default: /* try ascii */
546 vval = tvb_format_text(tvb, offset, len);
547 vval_len = strlen(vval);
549 strncpy(p, vval, pl - 6);
553 strncpy(p, vval, vval_len);
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);
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);
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);
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);
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));
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));
596 case DCM_SQ: /* Sequence */
597 case DCM_OTH: /* Other BYTE, WORD, ... */
598 case DCM_TRET: /* Retired */
605 dcm_get_pdu_len(tvbuff_t *tvb, int offset)
609 len = tvb_get_ntohl(tvb, 2 + offset);
610 return len + 6; /* add in fixed header part */
614 dissect_dcm_assoc(dcmState_t *dcm_data, proto_item *ti, tvbuff_t *tvb, int offset)
616 proto_tree *dcm_tree = NULL;
617 dcmItem_t *di = NULL;
618 guint8 id, *name, result;
624 dcm_tree = proto_item_add_subtree(ti, ett_assoc);
625 while (-1 < offset && offset < (int) dcm_data->clen) {
628 id = tvb_get_guint8(tvb, offset);
629 len = tvb_get_ntohs(tvb, 2 + offset);
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));
635 case 0x10: /* App context */
637 proto_tree_add_item(dcm_tree, hf_dcm_pdi_name, tvb, offset, len > 65 ? 65 : len, FALSE);
640 case 0x30: /* Abstract syntax */
642 proto_tree_add_item(dcm_tree, hf_dcm_pdi_syntax, tvb, offset, len > 65 ? 65 : len, FALSE);
645 case 0x40: /* Transfer syntax */
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);
655 case 0x20: /* Presentation context */
656 id = tvb_get_guint8(tvb, offset);
657 di = lookupCtx(dcm_data, id);
659 di = se_alloc(sizeof(struct dcmItem));
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;
670 dcm_data->first = dcm_data->last = di;
673 proto_tree_add_item(dcm_tree, hf_dcm_pctxt, tvb, offset, 1, FALSE);
676 case 0x21: /* Presentation context reply */
677 id = tvb_get_guint8(tvb, offset);
678 result = tvb_get_guint8(tvb, 2 + offset);
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));
687 di = lookupCtx(dcm_data, id);
692 case 0x50: /* User Info */
694 case 0x51: /* Max length */
695 mlen = tvb_get_ntohl(tvb, offset);
697 proto_tree_add_item(dcm_tree, hf_dcm_pdu_maxlen, tvb, offset, 4, FALSE);
702 proto_tree_add_item(dcm_tree, hf_dcm_impl, tvb, offset, len > 65 ? 65 : len, FALSE);
705 case 0x55: /* version */
707 proto_tree_add_item(dcm_tree, hf_dcm_vers, tvb, offset, len > 17 ? 17 : len, FALSE);
710 case 0x53: /* async negotion */
722 lookupCtx(dcmState_t *dd, guint8 ctx)
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 };
733 return di ? di : &dunk;
740 - (1+) presentation data value (PDV) items
742 10 1 Presentation Context ID (odd ints 1 - 255)
745 0x01 if set, contains Message Command info, else Message Data
746 0x02 if set, contains last fragment
756 dissect_dcm_data(dcmState_t *dcm_data, proto_item *ti, tvbuff_t *tvb)
758 int len, offset, toffset, state, vr, tr;
759 proto_tree *dcm_tree;
761 guint8 ctx, syntax = DCM_UNK;
762 guint16 grp = 0, elm = 0;
763 guint32 tlen = 0, nlen;
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);
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.
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" :
783 if (DCM_UNK == di->syntax)
785 len = offset = toffset = 11;
788 while (len + nlen <= dcm_data->tlen && len + nlen <= dcm_data->clen) {
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); */
800 else if (DCM_UNK == di->syntax) {
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 */
811 } break; /* don't fall through -- check length */
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 */
820 grp = tvb_get_ntohs(tvb, offset);
821 elm = tvb_get_ntohs(tvb, offset+2);
826 if (0xfffe == grp) state = D_LEN4;
829 } break; /* don't fall through -- check length */
833 V = tvb_get_guint8(tvb, offset); offset++;
834 R = tvb_get_guint8(tvb, offset); offset++;
836 /* 4byte lengths OB, OW, OF, SQ, UN, UT */
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))) {
843 offset += 2; /* skip 00 (2 bytes) */
846 } else if ('F' == V && 'L' == R) {
848 } else if ('F' == V && 'D' == R) {
850 } else if (('S' == V && 'L' == R) || ('U' == V && 'L' == R)) {
852 } else if (('S' == V && 'S' == R) || ('U' == V && 'S' == R)) {
854 } else if ('A' == V && 'T' == R) {
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)))
870 } break; /* don't fall through -- check length */
872 if (DCM_ILE & syntax) /* is it LE */
873 tlen = tvb_get_letohs(tvb, offset);
875 tlen = tvb_get_ntohs(tvb, offset);
882 if (DCM_ILE & syntax) /* is it LE */
883 tlen = tvb_get_letohl(tvb, offset);
885 tlen = tvb_get_ntohl(tvb, offset);
890 } break; /* don't fall through -- check length */
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));
901 /* } else if (0xfffe == grp) { */ /* need to make a sub-tree here */
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));
917 if (D_VALUE == state) {
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));
929 Originator src:srcport dest:destport
930 Acceptor src:srcport dest:destport
932 conn = lookup(src:srcport, dest:destport)
934 look at data payload of packet
935 if no-data return false;
936 if 01 == *p && *p+10 ... *p+42 <= [ 0x20 .. printable ]
940 static void dissect_dcm_pdu(tvbuff_t *tvb,packet_info *pinfo,proto_tree *tree);
942 /* Code to actually dissect the packets */
944 dissect_dcm(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
946 conversation_t *conv;
950 dcmState_t *dcm_data = NULL;
952 conv = find_conversation(pinfo->fd->num, &pinfo->src, &pinfo->dst,
953 pinfo->ptype, pinfo->srcport, pinfo->destport, 0);
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);
959 conv = conversation_new(pinfo->fd->num, &pinfo->src, &pinfo->dst, pinfo->ptype,
960 pinfo->srcport, pinfo->destport, 0);
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
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;
975 len = 6 + tvb_get_ntohl(tvb, 2);
977 dcm_data->valid = FALSE; /* packet is > decl len */
980 conversation_add_proto_data(conv, proto_dcm, dcm_data);
983 if (FALSE == dcm_data->valid)
986 if (check_col(pinfo->cinfo, COL_PROTOCOL))
987 col_clear(pinfo->cinfo, COL_PROTOCOL);
989 tcp_dissect_pdus(tvb, pinfo, tree, 1, 6, dcm_get_pdu_len, dissect_dcm_pdu);
995 dissect_dcm_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
998 dcmState_t *dcm_data;
999 proto_tree *dcm_tree;
1000 conversation_t *conv;
1004 if (NULL == (conv = find_conversation(pinfo->fd->num, &pinfo->src, &pinfo->dst,
1005 pinfo->ptype, pinfo->srcport, pinfo->destport, 0)))
1008 dcm_data = conversation_get_proto_data(conv, proto_dcm);
1010 if (check_col(pinfo->cinfo, COL_PROTOCOL))
1011 col_set_str(pinfo->cinfo, COL_PROTOCOL, "DCM");
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.
1019 if (check_col(pinfo->cinfo, COL_INFO))
1020 col_clear(pinfo->cinfo, COL_INFO);
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);
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);
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);
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));
1057 buf = g_malloc(128);
1058 strcpy(buf, "DCM Data");
1060 case 5: /* RELEASE Request */
1061 buf = g_malloc(128);
1062 strcpy(buf, "DCM RELEASE Request");
1065 case 6: /* RELEASE Response */
1066 buf = g_malloc(128);
1067 strcpy(buf, "DCM RELEASE Response");
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) : "");
1081 buf = g_malloc(128);
1082 strcpy(buf, "DCM Continuation");
1083 offset = -1; /* cannot continue parsing */
1086 if (check_col(pinfo->cinfo, COL_INFO))
1087 col_set_str(pinfo->cinfo, COL_INFO, buf);
1089 /* In the interest of speed, if "tree" is NULL, don't do any work not
1090 necessary to generate protocol tree items. */
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);
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 */
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);
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);
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);
1123 /* If this protocol has a sub-dissector call it here, see section 1.8 */
1127 /* Register the protocol with Ethereal */
1129 /* this format is require because a script is used to build the C function
1130 that calls all the protocol registration.
1134 proto_register_dcm(void)
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 } },
1171 { &hf_dcm_FIELDABBREV, { "FIELDNAME", "dcm.FIELDABBREV",
1172 FIELDTYPE, FIELDBASE, FIELDCONVERT, BITMASK, "FIELDDESCR", HFILL } },
1176 /* Setup protocol subtree array */
1177 static gint *ett[] = {
1182 /* Register the protocol name and description */
1183 proto_dcm = proto_register_protocol("DICOM", "dicom", "dcm");
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));
1189 register_init_routine(&dcm_init);
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.
1198 proto_reg_handoff_dcm(void)
1200 dissector_handle_t dcm_handle;
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);
1213 6 2 protocol version (0x0 0x1)
1218 74 - presentation data value items
1223 2 protocol version (0x0 0x1)
1225 16 dest aetitle (not checked)
1226 16 src aetitle (not checked)
1228 - presentation data value items
1234 1 result (1 reject perm, 2 reject transient)
1235 1 source (1 service user, 2 service provider, 3 service profider)
1239 2 application context name not supported
1240 3 calling aetitle not recognized
1241 7 called aetitle not recognized
1244 2 protocol version not supported
1246 1 temporary congestion
1247 2 local limit exceeded
1252 - (1+) presentation data value (PDV) items
1254 10 1 Presentation Context ID (odd ints 1 - 255)
1257 0x01 if set, contains Message Command info, else Message Data
1258 0x02 if set, contains last fragment
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)
1280 10 Application Context
1285 20 Presentation Context
1288 1 Presentation context id
1290 - (1) abstract and (1+) transfer syntax sub-items
1292 21 Presentation Context (Reply)
1295 1 ID (odd int's 1-255)
1297 1 result (0 accept, 1 user-reject, 2 no-reason, 3 abstract not supported, 4- transfer syntax not supported)