34e98664768bd4c27d7015c5f26e6302b9539eec
[metze/wireshark/wip.git] / epan / dissectors / packet-zvt.c
1 /* packet-zvt.c
2  * Routines for ZVT dissection
3  * Copyright 2014-2015, Martin Kaiser <martin@kaiser.cx>
4  *
5  * Wireshark - Network traffic analyzer
6  * By Gerald Combs <gerald@wireshark.org>
7  * Copyright 1998 Gerald Combs
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 /* ZVT is a manufacturer-independent protocol between payment terminals and
25  * electronic cash-register systems / vending machines
26  *
27  * the specifications are available from http://www.zvt-kassenschnittstelle.de
28  *
29  * ZVT defines a "serial transport protocol" and a "TCP/IP transport
30  * protocol"
31  *
32  * ZVT can sit on top of USB, either the serial or the TCP/IP protocol
33  * can be used in this case - this is not supported for now
34  *
35  * a dump of ZVT data can be converted to pcap, using a user-defined DLT
36  * we register the dissector by name and try to auto-detect the serial
37  * or TCP/IP protocol
38  *
39  * finally, ZVT can run on top of TCP, the default port is 20007, only
40  * the TCP/IP protocol can be used here
41  */
42
43
44 #include "config.h"
45
46 #include <epan/packet.h>
47 #include <epan/prefs.h>
48 #include <epan/addr_resolv.h>
49 #include <epan/expert.h>
50
51 /* special characters of the serial transport protocol */
52 #define STX 0x02
53 #define ETX 0x03
54 #define ACK 0x06
55 #define DLE 0x10
56 #define NAK 0x15
57
58 /* an APDU needs at least a 2-byte control-field and one byte length */
59 #define ZVT_APDU_MIN_LEN 3
60
61
62 static GHashTable *apdu_table = NULL;
63
64 static wmem_tree_t *transactions = NULL;
65
66 typedef struct _zvt_transaction_t {
67     guint32 rqst_frame;
68     guint32 resp_frame;
69     guint16 ctrl;
70 } zvt_transaction_t;
71
72 typedef enum _zvt_direction_t {
73     DIRECTION_UNKNOWN,
74     DIRECTION_ECR_TO_PT,
75     DIRECTION_PT_TO_ECR
76 } zvt_direction_t;
77
78 /* source/destination address field */
79 #define ADDR_ECR "ECR"
80 #define ADDR_PT  "PT"
81
82 #define CCRC_POS 0x80
83 #define CCRC_NEG 0x84
84
85 /* "don't care" value for min_len_field */
86 #define LEN_FIELD_ANY G_MAXUINT32
87
88 typedef struct _apdu_info_t {
89     guint16          ctrl;
90     guint32          min_len_field;
91     zvt_direction_t  direction;
92     void (*dissect_payload)(tvbuff_t *, gint, guint16,
93             packet_info *, proto_tree *, zvt_transaction_t *);
94 } apdu_info_t;
95
96 /* control code 0 is not defined in the specification */
97 #define ZVT_CTRL_NONE      0x0000
98
99 #define CTRL_STATUS        0x040F
100 #define CTRL_INT_STATUS    0x04FF
101 #define CTRL_REGISTRATION  0x0600
102 #define CTRL_AUTHORISATION 0x0601
103 #define CTRL_COMPLETION    0x060F
104 #define CTRL_ABORT         0x061E
105 #define CTRL_END_OF_DAY    0x0650
106 #define CTRL_DIAG          0x0670
107 #define CTRL_INIT          0x0693
108 #define CTRL_PRINT_LINE    0x06D1
109
110 static void dissect_zvt_reg(tvbuff_t *tvb, gint offset, guint16 len,
111         packet_info *pinfo, proto_tree *tree, zvt_transaction_t *zvt_trans);
112 static void dissect_zvt_bitmap_apdu(tvbuff_t *tvb, gint offset, guint16 len,
113         packet_info *pinfo, proto_tree *tree, zvt_transaction_t *zvt_trans);
114
115 static const apdu_info_t apdu_info[] = {
116     { CTRL_STATUS,        0, DIRECTION_PT_TO_ECR, NULL },
117     { CTRL_INT_STATUS,    0, DIRECTION_PT_TO_ECR, NULL },
118     { CTRL_REGISTRATION,  4, DIRECTION_ECR_TO_PT, dissect_zvt_reg },
119     /* authorisation has at least a 0x04 tag and 6 bytes for the amount */
120     { CTRL_AUTHORISATION, 7, DIRECTION_ECR_TO_PT, dissect_zvt_bitmap_apdu },
121     { CTRL_COMPLETION,    0, DIRECTION_PT_TO_ECR, dissect_zvt_bitmap_apdu },
122     { CTRL_ABORT,         0, DIRECTION_PT_TO_ECR, NULL },
123     { CTRL_END_OF_DAY,    0, DIRECTION_ECR_TO_PT, NULL },
124     { CTRL_DIAG,          0,  DIRECTION_ECR_TO_PT, NULL },
125     { CTRL_INIT,          0, DIRECTION_ECR_TO_PT, NULL },
126     { CTRL_PRINT_LINE,    0, DIRECTION_PT_TO_ECR, NULL }
127 };
128
129 void proto_register_zvt(void);
130 void proto_reg_handoff_zvt(void);
131
132 /* the specification mentions tcp port 20007
133    this port is not officially registered with IANA */
134 static guint pref_zvt_tcp_port = 0;
135
136 static int proto_zvt = -1;
137
138 static int ett_zvt = -1;
139 static int ett_zvt_apdu = -1;
140 static int ett_zvt_tlv_dat_obj = -1;
141
142 static int hf_zvt_resp_in = -1;
143 static int hf_zvt_resp_to = -1;
144 static int hf_zvt_serial_char = -1;
145 static int hf_zvt_crc = -1;
146 static int hf_zvt_ctrl = -1;
147 static int hf_zvt_ccrc = -1;
148 static int hf_zvt_aprc = -1;
149 static int hf_zvt_len = -1;
150 static int hf_zvt_data = -1;
151 static int hf_zvt_reg_pwd = -1;
152 static int hf_zvt_reg_cfg = -1;
153 static int hf_zvt_cc = -1;
154 static int hf_zvt_reg_svc_byte = -1;
155 static int hf_zvt_bitmap = -1;
156 static int hf_zvt_tlv_tag = -1;
157 static int hf_zvt_tlv_len = -1;
158
159 static expert_field ei_invalid_apdu_len = EI_INIT;
160
161 static const value_string serial_char[] = {
162     { STX, "Start of text (STX)" },
163     { ETX, "End of text (ETX)" },
164     { ACK, "Acknowledged (ACK)" },
165     { DLE, "Data line escape (DLE)" },
166     { NAK, "Not acknowledged (NAK)" },
167     { 0, NULL }
168 };
169 static value_string_ext serial_char_ext = VALUE_STRING_EXT_INIT(serial_char);
170
171
172 static const value_string ctrl_field[] = {
173     { CTRL_STATUS, "Status Information" },
174     { CTRL_INT_STATUS, "Intermediate Status Information" },
175     { CTRL_REGISTRATION, "Registration" },
176     { CTRL_AUTHORISATION, "Authorisation" },
177     { CTRL_COMPLETION, "Completion" },
178     { CTRL_ABORT, "Abort" },
179     { CTRL_END_OF_DAY, "End Of Day" },
180     { CTRL_DIAG, "Diagnosis" },
181     { CTRL_INIT, "Initialisation" },
182     { CTRL_PRINT_LINE, "Print Line" },
183     { 0x06D3, "Print Text Block" },
184     { 0, NULL }
185 };
186 static value_string_ext ctrl_field_ext = VALUE_STRING_EXT_INIT(ctrl_field);
187
188 #define BMP_TIMEOUT       0x01
189 #define BMP_MAX_STAT_INFO 0x02
190 #define BMP_AMOUNT        0x04
191 #define BMP_PUMP_NR       0x05
192 #define BMP_TLV_CONTAINER 0x06
193 #define BMP_EXP_DATE      0x0E
194 #define BMP_PAYMENT_TYPE  0x19
195 #define BMP_CARD_NUM      0x22
196 #define BMP_T2_DAT        0x23
197 #define BMP_T3_DAT        0x24
198 #define BMP_T1_DAT        0x2D
199 #define BMP_CVV_CVC       0x3A
200 #define BMP_ADD_DATA      0x3C
201 #define BMP_CC            0x49
202
203 static const value_string bitmap[] = {
204     { BMP_TIMEOUT,       "Timeout" },
205     { BMP_MAX_STAT_INFO, "max. status info" },
206     { BMP_AMOUNT,        "Amount" },
207     { BMP_PUMP_NR,       "Pump number" },
208     { BMP_TLV_CONTAINER, "TLV container" },
209     { BMP_EXP_DATE,      "Exipry date" },
210     { BMP_PAYMENT_TYPE,  "Payment type" },
211     { BMP_CARD_NUM,      "Card number" },
212     { BMP_T2_DAT,        "Track 2 data" },
213     { BMP_T3_DAT,        "Track 3 data" },
214     { BMP_T1_DAT,        "Track 1 data" },
215     { BMP_CVV_CVC,       "CVV / CVC" },
216     { BMP_ADD_DATA,      "Additional data" },
217     { BMP_CC,            "Currency code (CC)" },
218     { 0, NULL }
219 };
220 static value_string_ext bitmap_ext = VALUE_STRING_EXT_INIT(bitmap);
221
222 static const value_string tlv_tags[] = {
223     { 0, NULL }
224 };
225 static value_string_ext tlv_tags_ext = VALUE_STRING_EXT_INIT(tlv_tags);
226
227
228 static gint
229 dissect_zvt_tlv_tag(tvbuff_t *tvb, gint offset,
230         packet_info *pinfo _U_, proto_tree *tree, guint32 *tag)
231 {
232     guint8 tag_byte;
233
234     tag_byte = tvb_get_guint8(tvb, offset);
235     if ((tag_byte & 0x1F) == 0x1F) {
236         /* XXX - handle multi-byte tags */
237         return -1;
238     }
239
240     proto_tree_add_uint_format(tree, hf_zvt_tlv_tag,
241             tvb, offset, 1, tag_byte, "Tag: 0x%x", tag_byte);
242
243     if (tag)
244         *tag = tag_byte;
245     return 1;
246 }
247
248
249 static gint
250 dissect_zvt_tlv_container(tvbuff_t *tvb, gint offset,
251         packet_info *pinfo, proto_tree *tree)
252 {
253     gint        offset_start;
254     proto_item *dat_obj_it;
255     proto_tree *dat_obj_tree;
256     gint        tag_len;
257     guint32     tag;
258     guint8      data_len_bytes = 1;
259     guint16     data_len;
260
261     offset_start = offset;
262
263     while (tvb_captured_length_remaining(tvb, offset) > 0) {
264         dat_obj_tree = proto_tree_add_subtree(tree,
265             tvb, offset, -1, ett_zvt_tlv_dat_obj, &dat_obj_it,
266             "TLV data object");
267
268         tag_len = dissect_zvt_tlv_tag(tvb, offset, pinfo, dat_obj_tree, &tag);
269         if (tag_len <= 0)
270             return offset - offset_start;
271         offset += tag_len;
272
273         data_len = tvb_get_guint8(tvb, offset);
274         if (data_len & 0x80) {
275             if ((data_len & 0x03) == 1) {
276                 data_len_bytes++;
277                 data_len = tvb_get_guint8(tvb, offset+1);
278             }
279             else if ((data_len & 0x03) == 2) {
280                 data_len_bytes += 2;
281                 data_len = tvb_get_ntohs(tvb, offset+1);
282             }
283             else {
284                 /* XXX - expert info, exit */
285             }
286         }
287         proto_tree_add_uint(dat_obj_tree, hf_zvt_tlv_len,
288                 tvb, offset, data_len_bytes, data_len);
289         offset += data_len_bytes;
290
291         /* XXX - dissect the data-element */
292         offset += data_len;
293
294         proto_item_set_len(dat_obj_it, tag_len + data_len_bytes + data_len);
295     }
296
297     return offset - offset_start;
298 }
299
300
301 /* dissect one "bitmap", i.e BMP and the corresponding data */
302 static gint
303 dissect_zvt_bitmap(tvbuff_t *tvb, gint offset,
304         packet_info *pinfo, proto_tree *tree)
305 {
306     gint    offset_start;
307     guint8  bmp;
308     gint    ret;
309
310     offset_start = offset;
311
312     bmp = tvb_get_guint8(tvb, offset);
313     if (try_val_to_str(bmp, bitmap) == NULL)
314         return -1;
315
316     proto_tree_add_item(tree, hf_zvt_bitmap, tvb, offset, 1, ENC_BIG_ENDIAN);
317     offset++;
318
319     switch (bmp) {
320         case BMP_TIMEOUT:
321             offset++;
322             break;
323         case BMP_MAX_STAT_INFO:
324             offset++;
325             break;
326         case BMP_AMOUNT:
327             offset += 6;
328             break;
329         case BMP_PUMP_NR:
330             offset++;
331             break;
332         case BMP_EXP_DATE:
333             offset += 2;
334             break;
335         case BMP_PAYMENT_TYPE:
336             offset++;
337             break;
338         case BMP_CVV_CVC:
339             offset += 2;
340             break;
341         case BMP_CC:
342             offset += 2;
343             break;
344         case BMP_TLV_CONTAINER:
345             ret = dissect_zvt_tlv_container(tvb, offset, pinfo, tree);
346             if (ret<0)
347                 return -1;
348
349             offset += ret;
350             break;
351
352         case BMP_CARD_NUM:
353         case BMP_T2_DAT:
354         case BMP_T3_DAT:
355         case BMP_T1_DAT:
356         case BMP_ADD_DATA:
357             /* the bitmaps are not TLV but only TV, there's no length field
358                the tags listed above have variable length
359                -> if we see one of those tags, we have to stop the
360                dissection and report an error to the caller */
361             return -1;
362
363         default:
364             g_assert_not_reached();
365             break;
366     };
367
368     return offset - offset_start;
369 }
370
371 static void
372 dissect_zvt_reg(tvbuff_t *tvb, gint offset, guint16 len _U_,
373         packet_info *pinfo, proto_tree *tree, zvt_transaction_t *zvt_trans)
374 {
375     proto_tree_add_item(tree, hf_zvt_reg_pwd, tvb, offset, 3, ENC_NA);
376     offset += 3;
377
378     proto_tree_add_item(tree, hf_zvt_reg_cfg,
379             tvb, offset, 1, ENC_BIG_ENDIAN);
380     offset++;
381
382     /* check for the optional part CC|0x03|service byte */
383     if (tvb_captured_length_remaining(tvb, offset)>=4 &&
384             tvb_get_guint8(tvb, offset+2)==0x03) {
385
386         proto_tree_add_item(tree, hf_zvt_cc,
387             tvb, offset, 2, ENC_BIG_ENDIAN);
388         offset += 2;
389
390         offset++; /* 0x03 */
391
392         proto_tree_add_item(tree, hf_zvt_reg_svc_byte,
393             tvb, offset, 1, ENC_BIG_ENDIAN);
394         offset++;
395     }
396
397     /* it's ok if the remaining len is 0 */
398     dissect_zvt_bitmap_apdu(tvb, offset,
399             tvb_captured_length_remaining(tvb, offset),
400             pinfo, tree, zvt_trans);
401 }
402
403
404 /* dissect an APDU that contains a sequence of bitmaps */
405 static void
406 dissect_zvt_bitmap_apdu(tvbuff_t *tvb, gint offset, guint16 len,
407         packet_info *pinfo _U_, proto_tree *tree, zvt_transaction_t *zvt_trans _U_)
408 {
409     gint offset_start, ret;
410
411     offset_start = offset;
412
413     while (offset - offset_start < len) {
414         ret = dissect_zvt_bitmap(tvb, offset, pinfo, tree);
415         if (ret <=0)
416             break;
417         offset += ret;
418     }
419 }
420
421
422 static void
423 zvt_set_addresses(packet_info *pinfo, zvt_transaction_t *zvt_trans)
424 {
425     apdu_info_t     *ai;
426     zvt_direction_t  dir = DIRECTION_UNKNOWN;
427
428     if (!zvt_trans)
429         return;
430
431     ai = (apdu_info_t *)g_hash_table_lookup(
432             apdu_table, GUINT_TO_POINTER((guint)zvt_trans->ctrl));
433     if (!ai)
434         return;
435
436     if (zvt_trans->rqst_frame == PINFO_FD_NUM(pinfo)) {
437         dir = ai->direction;
438     }
439     else if (zvt_trans->resp_frame == PINFO_FD_NUM(pinfo)) {
440         if (ai->direction == DIRECTION_ECR_TO_PT)
441             dir = DIRECTION_PT_TO_ECR;
442         else
443             dir = DIRECTION_ECR_TO_PT;
444     }
445
446     if (dir  == DIRECTION_ECR_TO_PT) {
447         SET_ADDRESS(&pinfo->src, AT_STRINGZ,
448                 (int)strlen(ADDR_ECR)+1, ADDR_ECR);
449         SET_ADDRESS(&pinfo->dst, AT_STRINGZ,
450                 (int)strlen(ADDR_PT)+1, ADDR_PT);
451     }
452     else if (dir  == DIRECTION_PT_TO_ECR) {
453         SET_ADDRESS(&pinfo->src, AT_STRINGZ,
454                 (int)strlen(ADDR_PT)+1, ADDR_PT);
455         SET_ADDRESS(&pinfo->dst, AT_STRINGZ,
456                 (int)strlen(ADDR_ECR)+1, ADDR_ECR);
457     }
458 }
459
460
461 /* dissect a ZVT APDU
462    return -1 if we don't have a complete APDU, 0 if the packet is no ZVT APDU
463    or the length of the ZVT APDU if all goes well */
464 static int
465 dissect_zvt_apdu(tvbuff_t *tvb, gint offset, packet_info *pinfo, proto_tree *tree)
466 {
467     gint               offset_start;
468     guint8             len_bytes = 1; /* number of bytes for the len field */
469     guint16            ctrl = ZVT_CTRL_NONE;
470     guint16            len;
471     guint8             byte;
472     proto_item        *apdu_it;
473     proto_tree        *apdu_tree;
474     apdu_info_t       *ai;
475     zvt_transaction_t *zvt_trans = NULL;
476     proto_item        *it;
477
478     offset_start = offset;
479
480     if (tvb_captured_length_remaining(tvb, offset) < ZVT_APDU_MIN_LEN)
481         return -1;
482
483     len = tvb_get_guint8(tvb, offset+2);
484     if (len == 0xFF) {
485         len_bytes = 3;
486         len = tvb_get_ntohs(tvb, offset+3);
487     }
488
489     /* ZVT_APDU_MIN_LEN already includes one length byte */
490     if (tvb_captured_length_remaining(tvb, offset) <
491             ZVT_APDU_MIN_LEN + (len_bytes-1) + len) {
492         return -1;
493     }
494
495     apdu_tree = proto_tree_add_subtree(tree,
496             tvb, offset, -1, ett_zvt_apdu, &apdu_it, "ZVT APDU");
497
498     byte = tvb_get_guint8(tvb, offset);
499     if (byte == CCRC_POS || byte == CCRC_NEG) {
500         proto_tree_add_item(apdu_tree, hf_zvt_ccrc, tvb, offset, 1, ENC_BIG_ENDIAN);
501         col_append_sep_fstr(pinfo->cinfo, COL_INFO, NULL, "%s",
502                 byte == CCRC_POS ? "Positive completion" : "Negative completion");
503         offset++;
504         proto_tree_add_item(apdu_tree, hf_zvt_aprc, tvb, offset, 1, ENC_BIG_ENDIAN);
505         offset++;
506
507         /* XXX - can this ever be NULL? */
508         if (transactions) {
509             zvt_trans = (zvt_transaction_t *)wmem_tree_lookup32_le(
510                     transactions, PINFO_FD_NUM(pinfo));
511            if (zvt_trans && zvt_trans->resp_frame==0) {
512                /* there's a pending request, this packet is the response */
513                zvt_trans->resp_frame = PINFO_FD_NUM(pinfo);
514            }
515
516            if (zvt_trans && zvt_trans->resp_frame == PINFO_FD_NUM(pinfo)) {
517                it = proto_tree_add_uint(apdu_tree, hf_zvt_resp_to,
518                        NULL, 0, 0, zvt_trans->rqst_frame);
519                PROTO_ITEM_SET_GENERATED(it);
520            }
521         }
522
523     }
524     else {
525         ctrl = tvb_get_ntohs(tvb, offset);
526         proto_tree_add_item(apdu_tree, hf_zvt_ctrl, tvb, offset, 2, ENC_BIG_ENDIAN);
527         col_append_sep_fstr(pinfo->cinfo, COL_INFO, NULL, "%s",
528                 val_to_str_const(ctrl, ctrl_field, "Unknown 0x%x"));
529         offset += 2;
530
531         if (PINFO_FD_VISITED(pinfo)) {
532             zvt_trans = (zvt_transaction_t *)wmem_tree_lookup32(
533                     transactions, PINFO_FD_NUM(pinfo));
534             if (zvt_trans && zvt_trans->rqst_frame==PINFO_FD_NUM(pinfo) &&
535                     zvt_trans->resp_frame!=0) {
536                it = proto_tree_add_uint(apdu_tree, hf_zvt_resp_in,
537                        NULL, 0, 0, zvt_trans->resp_frame);
538                PROTO_ITEM_SET_GENERATED(it);
539             }
540         }
541         else {
542             /* XXX - can this ever be NULL? */
543             if (transactions) {
544                 zvt_trans = wmem_new(wmem_file_scope(), zvt_transaction_t);
545                 zvt_trans->rqst_frame = PINFO_FD_NUM(pinfo);
546                 zvt_trans->resp_frame = 0;
547                 zvt_trans->ctrl = ctrl;
548                 wmem_tree_insert32(transactions,
549                         zvt_trans->rqst_frame, (void *)zvt_trans);
550             }
551         }
552     }
553
554     ai = (apdu_info_t *)g_hash_table_lookup(
555             apdu_table, GUINT_TO_POINTER((guint)ctrl));
556
557     it = proto_tree_add_uint(apdu_tree, hf_zvt_len, tvb, offset, len_bytes, len);
558     if (ai && ai->min_len_field!=LEN_FIELD_ANY && len<ai->min_len_field) {
559         expert_add_info_format(pinfo, it, &ei_invalid_apdu_len,
560                 "The APDU length is too short. The minimum length is %d",
561                 ai->min_len_field);
562     }
563     offset += len_bytes;
564
565     zvt_set_addresses(pinfo, zvt_trans);
566
567     if (len > 0) {
568         if (ai && ai->dissect_payload)
569             ai->dissect_payload(tvb, offset, len, pinfo, apdu_tree, zvt_trans);
570         else
571             proto_tree_add_item(apdu_tree, hf_zvt_data,
572                     tvb, offset, len, ENC_NA);
573     }
574     offset += len;
575
576     proto_item_set_len(apdu_it, offset - offset_start);
577     return offset - offset_start;
578 }
579
580
581 static gint
582 dissect_zvt_serial(tvbuff_t *tvb, gint offset, packet_info *pinfo _U_, proto_tree *tree)
583 {
584     gint  offset_start;
585     int   apdu_len;
586
587     offset_start = offset;
588
589     if (tvb_reported_length_remaining(tvb, offset) == 1) {
590         proto_tree_add_item(tree, hf_zvt_serial_char,
591                 tvb, offset, 1, ENC_BIG_ENDIAN);
592         offset++; /* ACK or NAK byte */
593         return offset - offset_start;
594     }
595
596     proto_tree_add_item(tree, hf_zvt_serial_char,
597             tvb, offset, 1, ENC_BIG_ENDIAN);
598     offset ++; /* DLE byte */
599     proto_tree_add_item(tree, hf_zvt_serial_char,
600             tvb, offset, 1, ENC_BIG_ENDIAN);
601     offset ++; /* STX byte */
602
603     apdu_len = dissect_zvt_apdu(tvb, offset, pinfo, tree);
604     if (apdu_len < 0)
605         return apdu_len;
606
607     offset += apdu_len;
608
609     proto_tree_add_item(tree, hf_zvt_serial_char,
610             tvb, offset, 1, ENC_BIG_ENDIAN);
611     offset ++; /* DLE byte */
612     proto_tree_add_item(tree, hf_zvt_serial_char,
613             tvb, offset, 1, ENC_BIG_ENDIAN);
614     offset ++; /* ETX byte */
615
616     /* the CRC is little endian, the other fields are big endian */
617     proto_tree_add_item(tree, hf_zvt_crc,
618             tvb, offset, 2, ENC_LITTLE_ENDIAN);
619     offset += 2; /* CRC bytes */
620
621     return offset - offset_start;
622 }
623
624
625 static gboolean
626 valid_ctrl_field(tvbuff_t *tvb, gint offset)
627 {
628     if (tvb_get_guint8(tvb, offset) == 0x80 ||
629         tvb_get_guint8(tvb, offset) == 0x84 ||
630         try_val_to_str_ext(tvb_get_ntohs(tvb, offset), &ctrl_field_ext)) {
631             return TRUE;
632     }
633
634     return FALSE;
635 }
636
637
638 static int
639 dissect_zvt(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
640 {
641     gint        zvt_len = 0;
642     proto_item *zvt_ti;
643     proto_tree *zvt_tree;
644     gboolean    is_serial; /* serial or TCP/IP protocol? */
645
646     if (tvb_captured_length(tvb) == 1 &&
647             (tvb_get_guint8(tvb, 0) == ACK ||
648              tvb_get_guint8(tvb, 0) == NAK)) {
649         is_serial = TRUE;
650     }
651     else if (tvb_captured_length(tvb) >= 2 &&
652             tvb_get_guint8(tvb, 0) == DLE &&
653             tvb_get_guint8(tvb, 1) == STX) {
654         is_serial = TRUE;
655     }
656     else if (tvb_captured_length(tvb) >= ZVT_APDU_MIN_LEN &&
657             valid_ctrl_field(tvb, 0)) {
658         is_serial = FALSE;
659     }
660     else
661         return 0;
662
663     col_set_str(pinfo->cinfo, COL_PROTOCOL, "ZVT");
664     col_clear(pinfo->cinfo, COL_INFO);
665     zvt_ti = proto_tree_add_protocol_format(tree, proto_zvt,
666             tvb, 0, -1,
667             "ZVT Kassenschnittstelle: %s", is_serial ?
668             "Serial Transport Protocol" : "Transport Protocol TCP/IP");
669     zvt_tree = proto_item_add_subtree(zvt_ti, ett_zvt);
670
671     if (is_serial)
672         zvt_len = dissect_zvt_serial(tvb, 0, pinfo, zvt_tree);
673     else
674         zvt_len = dissect_zvt_apdu(tvb, 0, pinfo, zvt_tree);
675
676     /* zvt_len < 0 means that we have an incomplete APDU
677        we can't do any reassembly here, so let's consume all bytes */
678     if (zvt_len < 0)
679         zvt_len = tvb_captured_length(tvb);
680
681     proto_item_set_len(zvt_ti, zvt_len);
682     return zvt_len;
683 }
684
685
686 static int
687 dissect_zvt_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
688 {
689     gint        offset = 0, zvt_len = 0, ret;
690     proto_item *zvt_ti;
691     proto_tree *zvt_tree;
692
693     if (tvb_captured_length(tvb) < ZVT_APDU_MIN_LEN) {
694         if (pinfo->can_desegment) {
695             pinfo->desegment_offset = offset;
696             pinfo->desegment_len = DESEGMENT_ONE_MORE_SEGMENT;
697         }
698         return zvt_len;
699     }
700
701     if (!valid_ctrl_field(tvb, 0))
702         return 0; /* reject the packet */
703
704     col_set_str(pinfo->cinfo, COL_PROTOCOL, "ZVT");
705     col_clear(pinfo->cinfo, COL_INFO);
706     zvt_ti = proto_tree_add_protocol_format(tree, proto_zvt,
707             tvb, 0, -1,
708             "ZVT Kassenschnittstelle: Transport Protocol TCP/IP");
709     zvt_tree = proto_item_add_subtree(zvt_ti, ett_zvt);
710
711     while (tvb_captured_length_remaining(tvb, offset) > 0) {
712         ret = dissect_zvt_apdu(tvb, offset, pinfo, zvt_tree);
713         if (ret == 0) {
714             /* not a valid APDU
715                mark the bytes that we consumed and exit, give
716                other dissectors a chance to try the remaining
717                bytes */
718             break;
719         }
720         else if (ret < 0) {
721             /* not enough data - ask the TCP layer for more */
722
723             if (pinfo->can_desegment) {
724                 pinfo->desegment_offset = offset;
725                 pinfo->desegment_len = DESEGMENT_ONE_MORE_SEGMENT;
726             }
727             break;
728         }
729         else {
730             offset += ret;
731             zvt_len += ret;
732         }
733     }
734
735     proto_item_set_len(zvt_ti, zvt_len);
736     return zvt_len;
737 }
738
739
740 void
741 proto_register_zvt(void)
742 {
743     guint     i;
744     module_t *zvt_module;
745     expert_module_t* expert_zvt;
746
747     static gint *ett[] = {
748         &ett_zvt,
749         &ett_zvt_apdu,
750         &ett_zvt_tlv_dat_obj
751     };
752     static hf_register_info hf[] = {
753         { &hf_zvt_resp_in,
754             { "Response In", "zvt.resp_in",
755                 FT_FRAMENUM, BASE_NONE, NULL, 0x0, NULL, HFILL } },
756         { &hf_zvt_resp_to,
757             { "Response To", "zvt.resp_to",
758                 FT_FRAMENUM, BASE_NONE, NULL, 0x0, NULL, HFILL } },
759          { &hf_zvt_serial_char,
760             { "Serial character", "zvt.serial_char", FT_UINT8,
761                 BASE_HEX|BASE_EXT_STRING, &serial_char_ext, 0, NULL, HFILL } },
762         { &hf_zvt_crc,
763             { "CRC", "zvt.crc", FT_UINT16,
764                 BASE_HEX, NULL, 0, NULL, HFILL } },
765         { &hf_zvt_ctrl,
766             { "Control-field", "zvt.control_field", FT_UINT16,
767                 BASE_HEX|BASE_EXT_STRING, &ctrl_field_ext, 0, NULL, HFILL } },
768         { &hf_zvt_ccrc,
769             { "CCRC", "zvt.ccrc",
770                 FT_UINT8, BASE_HEX, NULL, 0, NULL, HFILL } },
771         { &hf_zvt_aprc,
772             { "APRC", "zvt.aprc",
773                 FT_UINT8, BASE_HEX, NULL, 0, NULL, HFILL } },
774         { &hf_zvt_len,
775             { "Length-field", "zvt.length_field",
776                 FT_UINT16, BASE_DEC, NULL, 0, NULL, HFILL } },
777         { &hf_zvt_data,
778           { "APDU data", "zvt.data",
779             FT_BYTES, BASE_NONE, NULL, 0, NULL, HFILL } },
780         { &hf_zvt_reg_pwd,
781             { "Password", "zvt.reg.password",
782                 FT_BYTES, BASE_NONE, NULL, 0, NULL, HFILL } },
783         { &hf_zvt_reg_cfg,
784             { "Config byte", "zvt.reg.config_byte",
785                 FT_UINT8, BASE_HEX, NULL, 0, NULL, HFILL } },
786         /* we don't call the filter zvt.reg.cc, the currency code
787            appears in several apdus */
788         { &hf_zvt_cc,
789             { "Currency Code (CC)", "zvt.cc",
790                 FT_UINT16, BASE_HEX, NULL, 0, NULL, HFILL } },
791         { &hf_zvt_reg_svc_byte,
792             { "Service byte", "zvt.reg.service_byte",
793                 FT_UINT8, BASE_HEX, NULL, 0, NULL, HFILL } },
794         { &hf_zvt_bitmap,
795             { "Bitmap", "zvt.bitmap", FT_UINT8,
796                 BASE_HEX|BASE_EXT_STRING, &bitmap_ext, 0, NULL, HFILL } },
797         { &hf_zvt_tlv_tag,
798             { "Tag", "zvt.tlv.tag", FT_UINT32,
799                 BASE_HEX|BASE_EXT_STRING, &tlv_tags_ext, 0, NULL, HFILL } },
800         { &hf_zvt_tlv_len,
801             { "Length", "zvt.tlv.len",
802                 FT_UINT8, BASE_DEC, NULL, 0, NULL, HFILL } }
803     };
804
805     static ei_register_info ei[] = {
806         { &ei_invalid_apdu_len,
807             { "zvt.apdu_len.invalid", PI_PROTOCOL, PI_WARN,
808                 "The APDU length is too short. The minimum length is %d",
809                 EXPFILL }}
810     };
811
812     apdu_table = g_hash_table_new(g_direct_hash, g_direct_equal);
813     for(i=0; i<array_length(apdu_info); i++) {
814         g_hash_table_insert(apdu_table,
815                             GUINT_TO_POINTER((guint)apdu_info[i].ctrl),
816                             (const gpointer)(&apdu_info[i]));
817     }
818
819     proto_zvt = proto_register_protocol(
820             "ZVT Kassenschnittstelle", "ZVT", "zvt");
821     proto_register_field_array(proto_zvt, hf, array_length(hf));
822     proto_register_subtree_array(ett, array_length(ett));
823     expert_zvt = expert_register_protocol(proto_zvt);
824     expert_register_field_array(expert_zvt, ei, array_length(ei));
825
826     zvt_module = prefs_register_protocol(proto_zvt, proto_reg_handoff_zvt);
827     prefs_register_uint_preference(zvt_module, "tcp.port",
828                    "ZVT TCP Port",
829                    "Set the TCP port for ZVT messages (port 20007 according to the spec)",
830                    10,
831                    &pref_zvt_tcp_port);
832
833     transactions = wmem_tree_new_autoreset(wmem_epan_scope(), wmem_file_scope());
834 }
835
836
837 void
838 proto_reg_handoff_zvt(void)
839 {
840     static gboolean            registered_dissector = FALSE;
841     static int                 zvt_tcp_port;
842     static dissector_handle_t  zvt_tcp_handle;
843
844     if (!registered_dissector) {
845         /* register by name to allow mapping to a user DLT */
846         new_register_dissector("zvt", dissect_zvt, proto_zvt);
847
848         zvt_tcp_handle = new_create_dissector_handle(dissect_zvt_tcp, proto_zvt);
849
850         registered_dissector = TRUE;
851     }
852     else
853         dissector_delete_uint("tcp.port", zvt_tcp_port, zvt_tcp_handle);
854
855     zvt_tcp_port = pref_zvt_tcp_port;
856     dissector_add_uint("tcp.port", zvt_tcp_port, zvt_tcp_handle);
857 }
858
859
860 /*
861  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
862  *
863  * Local variables:
864  * c-basic-offset: 4
865  * tab-width: 8
866  * indent-tabs-mode: nil
867  * End:
868  *
869  * vi: set shiftwidth=4 tabstop=8 expandtab:
870  * :indentSize=4:tabSize=8:noTabs=true:
871  */