summaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-zabbix.c
blob: 070c245a8bd10069f2ef75b2d706752f95aabc57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
/* packet-zabbix.c
 * Routines for Zabbix protocol dissection
 * Copyright 2023, Markku Leiniö <markku.leinio@gmail.com>
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

/*
 * Zabbix protocol specifications can be found in Zabbix documentation:
 * https://www.zabbix.com/documentation/current/en/manual/appendix/protocols
 */

#include "config.h"

#include <epan/conversation.h>
#include <epan/expert.h>
#include <epan/packet.h>
#include <epan/prefs.h>
#include <wsutil/inet_addr.h>
#include <wsutil/nstime.h>
#include <wsutil/wsjson.h>
#include "packet-tcp.h"

void proto_register_zabbix(void);
void proto_reg_handoff_zabbix(void);

static dissector_handle_t zabbix_handle;

/* Desegmentation of Zabbix protocol over TCP */
static gboolean zabbix_desegment = true;

/* Initialize the protocol and registered fields */
static int proto_zabbix = -1;
static int hf_zabbix_header = -1;
static int hf_zabbix_flags = -1;
static int hf_zabbix_flag_zabbix_communications = -1;
static int hf_zabbix_flag_compressed = -1;
static int hf_zabbix_flag_largepacket = -1;
static int hf_zabbix_flag_reserved = -1;
static int hf_zabbix_length = -1;
static int hf_zabbix_reserved = -1;
static int hf_zabbix_uncompressed_length = -1;
static int hf_zabbix_large_length = -1;
static int hf_zabbix_large_reserved = -1;
static int hf_zabbix_large_uncompressed_length = -1;
static int hf_zabbix_data = -1;
static int hf_zabbix_time = -1;
static int hf_zabbix_agent = -1;
static int hf_zabbix_agent_config = -1;
static int hf_zabbix_agent_data = -1;
static int hf_zabbix_agent_passive = -1;
static int hf_zabbix_agent_name = -1;
static int hf_zabbix_agent_hb = -1;
static int hf_zabbix_agent_hb_freq = -1;
static int hf_zabbix_agent_hostmetadata = -1;
static int hf_zabbix_agent_hostinterface = -1;
static int hf_zabbix_agent_listenipv4 = -1;
static int hf_zabbix_agent_listenipv6 = -1;
static int hf_zabbix_agent_listenport = -1;
static int hf_zabbix_proxy = -1;
static int hf_zabbix_proxy_hb = -1;
static int hf_zabbix_proxy_name = -1;
static int hf_zabbix_proxy_data = -1;
static int hf_zabbix_proxy_config = -1;
static int hf_zabbix_proxy_fullsync = -1;
static int hf_zabbix_proxy_incr_config = -1;
static int hf_zabbix_proxy_no_config_change = -1;
static int hf_zabbix_sender = -1;
static int hf_zabbix_sender_name = -1;
static int hf_zabbix_request = -1;
static int hf_zabbix_response = -1;
static int hf_zabbix_success = -1;
static int hf_zabbix_failed = -1;
static int hf_zabbix_config_revision = -1;
static int hf_zabbix_session = -1;
static int hf_zabbix_version = -1;

/* Initialize the subtree pointers */
static int ett_zabbix = -1;

/* Initialize expert fields */
static expert_field ei_zabbix_packet_too_large = EI_INIT;
static expert_field ei_zabbix_json_error = EI_INIT;

/* Other dissector-specifics */
static range_t *zabbix_port_range;

static const guint8 ZABBIX_HDR_SIGNATURE[] = "ZBXD";
static const char ZABBIX_UNKNOWN[] = "<unknown>";

typedef struct _zabbix_conv_info_t {
    guint32 req_framenum;
    nstime_t req_timestamp;
    uint16_t oper_flags;         /* ZABBIX_T_XXX macros below */
    const guint8 *host_name;
} zabbix_conv_info_t;

#define ZABBIX_HDR_MIN_LEN          13              /* When not large packet */
#define ZABBIX_HDR_MAX_LEN          21              /* When large packet */
#define ZABBIX_MAX_LENGTH_ALLOWED   1024*1024*1024  /* 1 GB */
#define ZABBIX_TCP_PORTS            "10050,10051"   /* IANA registered ports */

#define ZABBIX_FLAG_ZABBIX_COMMUNICATIONS   0x01
#define ZABBIX_FLAG_COMPRESSED              0x02
#define ZABBIX_FLAG_LARGEPACKET             0x04
#define ZABBIX_FLAG_RESERVED                0xf8

/* Response flags are not saved in the conversations */
#define ZABBIX_RESPONSE_SUCCESS     0x01
#define ZABBIX_RESPONSE_FAILED      0x02
#define ZABBIX_RESPONSE_FULLSYNC    0x04
#define ZABBIX_RESPONSE_INCREMENTAL 0x08
#define ZABBIX_RESPONSE_NOCHANGE    0x10

/* Flags for saving and comparing operation types,
 * max 16 bits as defined in zabbix_conv_info_t above */
#define ZABBIX_T_REQUEST            0x00000001   /* Not set for heartbeats */
#define ZABBIX_T_RESPONSE           0x00000002
#define ZABBIX_T_ACTIVE             0x00000004
#define ZABBIX_T_PASSIVE            0x00000008
#define ZABBIX_T_AGENT              0x00000010
#define ZABBIX_T_PROXY              0x00000020
#define ZABBIX_T_SENDER             0x00000040
#define ZABBIX_T_CONFIG             0x00000080
#define ZABBIX_T_DATA               0x00000100
#define ZABBIX_T_HEARTBEAT          0x00000200

#define ADD_ZABBIX_T_FLAGS(flags)       (zabbix_info->oper_flags |= (flags))
#define CLEAR_ZABBIX_T_FLAGS(flags)     (zabbix_info->oper_flags &= (0xffff-(flags)))
#define IS_ZABBIX_T_FLAGS(flags)        ((zabbix_info->oper_flags & (flags)) == (flags))

#define CONV_IS_ZABBIX_REQUEST(zabbix_info,pinfo)           ((zabbix_info)->req_framenum == (pinfo)->fd->num)
#define CONV_IS_ZABBIX_RESPONSE(zabbix_info,pinfo)          ((zabbix_info)->req_framenum != (pinfo)->fd->num)


static zabbix_conv_info_t*
zabbix_find_conversation_and_get_conv_data(packet_info *pinfo)
{
    conversation_t *conversation;
    zabbix_conv_info_t *zabbix_info = NULL;

    conversation = find_conversation_pinfo(pinfo, 0);
    if (conversation) {
        zabbix_info = (zabbix_conv_info_t *)conversation_get_proto_data(conversation, proto_zabbix);
    } else {
        conversation = conversation_new(pinfo->num, &pinfo->src, &pinfo->dst,
            conversation_pt_to_conversation_type(pinfo->ptype),
            pinfo->srcport, pinfo->destport, 0);
    }
    if (!zabbix_info) {
        /* New conversation, or there was no Zabbix data yet in the existing conv */
        zabbix_info = wmem_alloc(wmem_file_scope(), sizeof(zabbix_conv_info_t));
        if (value_is_in_range(zabbix_port_range, pinfo->destport)) {
            /* Let's assume this is the first Zabbix packet (request) */
            zabbix_info->req_framenum = pinfo->fd->num;
            zabbix_info->req_timestamp = pinfo->abs_ts;
        }
        else {
            /* For any reason we didn't have Zabbix data yet but this is not
             * the first packet for the connection, so don't save it as a request
             */
            zabbix_info->req_framenum = 0;
            nstime_set_unset(&zabbix_info->req_timestamp);
            /* For some reason this produces "syntax error: '{'" when compiling:
            zabbix_info->req_timestamp = NSTIME_INIT_UNSET;
            */
        }
        zabbix_info->oper_flags = 0;
        zabbix_info->host_name = NULL;
        conversation_add_proto_data(conversation, proto_zabbix, (void *)zabbix_info);
    }
    return zabbix_info;
}

static void
zabbix_add_expert_info_if_too_large(packet_info *pinfo, proto_tree *tree_item,
    uint64_t length, bool *is_too_large)
{
    if (length > ZABBIX_MAX_LENGTH_ALLOWED) {
        expert_add_info(pinfo, tree_item, &ei_zabbix_packet_too_large);
        *is_too_large = true;
    }
    return;
}

static int
dissect_zabbix_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
{
    int offset = 0;
    int agent_hb_freq = 0;
    unsigned oper_response = 0;
    proto_item *ti;
    proto_item *pi;
    proto_tree *temp_ti;
    proto_tree *zabbix_tree = NULL;
    uint8_t flags;
    uint16_t agent_listenport = 0;
    uint64_t length;
    uint64_t uncompressed_length;
    uint64_t datalen;
    int64_t config_revision = -1;
    bool is_compressed;
    bool is_large_packet;
    bool is_too_large = false;
    char *json_str;
    jsmntok_t *data_array = NULL;
    jsmntok_t *data_object = NULL;
    const char *agent_name = NULL;
    const char *agent_hostmetadata = NULL;
    const char *agent_hostinterface = NULL;
    const char *agent_listenip = NULL;
    const char *proxy_name = NULL;
    const char *sender_name = NULL;
    const char *session = NULL;
    const char *request_type = NULL;
    const char *response_status = NULL;
    const char *version = NULL;
    gdouble temp_double;
    tvbuff_t *next_tvb;
    zabbix_conv_info_t *zabbix_info;
    static int* const flagbits[] = {
        &hf_zabbix_flag_reserved,
        &hf_zabbix_flag_largepacket,
        &hf_zabbix_flag_compressed,
        &hf_zabbix_flag_zabbix_communications,
        NULL
    };

    /* Make entries in Protocol column and Info column on summary display */
    col_set_str(pinfo->cinfo, COL_PROTOCOL, "Zabbix");
    col_clear(pinfo->cinfo, COL_INFO);

    if ((tvb_reported_length(tvb) < ZABBIX_HDR_MIN_LEN) ||
        (tvb_memeql(tvb, offset, ZABBIX_HDR_SIGNATURE, 4) == -1)) {
        /* Encrypted or not Zabbix at all */
        return 0;
    }
    flags = tvb_get_guint8(tvb, offset+4);
    if (!(flags & ZABBIX_FLAG_ZABBIX_COMMUNICATIONS)) {
        return 0;
    }

    zabbix_info = zabbix_find_conversation_and_get_conv_data(pinfo);

    is_compressed = (flags & ZABBIX_FLAG_COMPRESSED) > 0;
    is_large_packet = (flags & ZABBIX_FLAG_LARGEPACKET) > 0;

    /* create display subtree for the protocol */
    ti = proto_tree_add_item(tree, proto_zabbix, tvb, 0, -1, ENC_NA);
    zabbix_tree = proto_item_add_subtree(ti, ett_zabbix);
    proto_tree_add_item(zabbix_tree, hf_zabbix_header, tvb, offset, 4, ENC_UTF_8);
    offset += 4;
    proto_tree_add_bitmask(zabbix_tree, tvb, offset, hf_zabbix_flags, ett_zabbix, flagbits, ENC_BIG_ENDIAN);
    offset += 1;
    if (is_large_packet) {
        /* 8-byte values */
        temp_ti = proto_tree_add_item_ret_uint64(zabbix_tree,
            hf_zabbix_large_length, tvb, offset, 8, ENC_LITTLE_ENDIAN, &length);
        zabbix_add_expert_info_if_too_large(pinfo, temp_ti, length, &is_too_large);
        offset += 8;
        if (is_compressed) {
            temp_ti = proto_tree_add_item_ret_uint64(zabbix_tree,
                hf_zabbix_large_uncompressed_length, tvb, offset, 8, ENC_LITTLE_ENDIAN, &uncompressed_length);
            zabbix_add_expert_info_if_too_large(pinfo, temp_ti, uncompressed_length, &is_too_large);
        } else {
            proto_tree_add_item(zabbix_tree, hf_zabbix_large_reserved, tvb, offset, 8, ENC_LITTLE_ENDIAN);
        }
        offset += 8;
    } else {
        /* 4-byte values */
        uint32_t temp_uint32;
        temp_ti = proto_tree_add_item_ret_uint(zabbix_tree,
            hf_zabbix_length, tvb, offset, 4, ENC_LITTLE_ENDIAN, &temp_uint32);
        length = (uint64_t)temp_uint32;
        zabbix_add_expert_info_if_too_large(pinfo, temp_ti, length, &is_too_large);
        offset += 4;
        if (is_compressed) {
            temp_ti = proto_tree_add_item_ret_uint(zabbix_tree,
                hf_zabbix_uncompressed_length, tvb, offset, 4, ENC_LITTLE_ENDIAN, &temp_uint32);
            uncompressed_length = (uint64_t)temp_uint32;
            zabbix_add_expert_info_if_too_large(pinfo, temp_ti, uncompressed_length, &is_too_large);
        } else {
            proto_tree_add_item(zabbix_tree, hf_zabbix_reserved, tvb, offset, 4, ENC_LITTLE_ENDIAN);
        }
        offset += 4;
    }
    if (is_too_large) {
        /* Set next_tvb for response time calculation to work later */
        next_tvb = tvb_new_subset_remaining(tvb, offset);
        /* ... but don't do any content-based inspection, just skip to the end */
        goto final_outputs;
    } else if (is_compressed) {
        next_tvb = tvb_uncompress(tvb, offset, tvb_reported_length_remaining(tvb, offset));
        if (next_tvb) {
            tvb_set_child_real_data_tvbuff(tvb, next_tvb);
            add_new_data_source(pinfo, next_tvb, "Uncompressed data");
            datalen = uncompressed_length;
        } else {
            /* Handle uncompressed */
            next_tvb = tvb_new_subset_remaining(tvb, offset);
            datalen = length;
        }
    } else {
        next_tvb = tvb_new_subset_remaining(tvb, offset);
        datalen = length;
    }

    /* Use only next_tvb and datalen for data extraction from here on! */
    offset = 0;

    /* Rewrite the default texts in the protocol tree and initialize request/response flags */
    if (CONV_IS_ZABBIX_REQUEST(zabbix_info, pinfo)) {
        proto_item_set_text(ti, "Zabbix Protocol request");
        ADD_ZABBIX_T_FLAGS(ZABBIX_T_REQUEST);
        CLEAR_ZABBIX_T_FLAGS(ZABBIX_T_RESPONSE);
    }
    else if (CONV_IS_ZABBIX_RESPONSE(zabbix_info, pinfo)) {
        proto_item_set_text(ti, "Zabbix Protocol response");
        ADD_ZABBIX_T_FLAGS(ZABBIX_T_RESPONSE);
        CLEAR_ZABBIX_T_FLAGS(ZABBIX_T_REQUEST);
    }

    /*
     * Note that json_str is modified when using json_get_xxx() functions below!
     * So don't use it to anything else (make a wmem_strdup() if needed)
     */
    json_str = tvb_get_string_enc(pinfo->pool, next_tvb, offset, (int)datalen, ENC_UTF_8);
    if (CONV_IS_ZABBIX_REQUEST(zabbix_info, pinfo) && !json_validate(json_str, datalen)) {
        /* The only non-JSON Zabbix request is passive agent, update the conversation data */
        ADD_ZABBIX_T_FLAGS(ZABBIX_T_AGENT | ZABBIX_T_PASSIVE);
    }
    if (IS_ZABBIX_T_FLAGS(ZABBIX_T_AGENT | ZABBIX_T_PASSIVE)) {
        if (CONV_IS_ZABBIX_REQUEST(zabbix_info, pinfo)) {
            proto_item_set_text(ti, "Zabbix Passive agent request");
            col_add_fstr(pinfo->cinfo, COL_INFO, "Zabbix Passive agent request");
        } else if (CONV_IS_ZABBIX_RESPONSE(zabbix_info, pinfo)) {
            proto_item_set_text(ti, "Zabbix Passive agent response");
            col_add_fstr(pinfo->cinfo, COL_INFO, "Zabbix Passive agent response");
        }
        /* Don't do content-based searches for passive agents */
        goto show_agent_outputs;
    }
    /* Parse JSON, first get the token count */
    int token_count = json_parse(json_str, NULL, 0);
    if (token_count <= 0) {
        temp_ti = proto_tree_add_item(zabbix_tree, hf_zabbix_data, next_tvb, 0, (int)datalen, ENC_UTF_8);
        expert_add_info_format(pinfo, temp_ti, &ei_zabbix_json_error, "Error in initial JSON parse");
        goto final_outputs;
    }
    jsmntok_t *tokens = wmem_alloc_array(pinfo->pool, jsmntok_t, token_count);
    int ret = json_parse(json_str, tokens, token_count);
    if (ret <= 0) {
        temp_ti = proto_tree_add_item(zabbix_tree, hf_zabbix_data, next_tvb, 0, (int)datalen, ENC_UTF_8);
        expert_add_info_format(pinfo, temp_ti, &ei_zabbix_json_error, "Error parsing JSON tokens");
        goto final_outputs;
    }

    /*
     * Now we have JSON tokens analyzed, let's do all the logic to populate the fields.
     * Also set Zabbix tree item and Info column texts, Len= and ports will be added later below.
     */

    /* First populate common fields */
    version = json_get_string(json_str, tokens, "version");
    session = json_get_string(json_str, tokens, "session");
    if (json_get_double(json_str, tokens, "config_revision", &temp_double)) {
        config_revision = (int64_t)temp_double;
    }
    request_type = json_get_string(json_str, tokens, "request");
    response_status = json_get_string(json_str, tokens, "response");
    data_array = json_get_array(json_str, tokens, "data");
    data_object = json_get_object(json_str, tokens, "data");
    if (request_type) {
        if (strcmp(request_type, "active checks") == 0) {
            /* Active agent requesting configs */
            ADD_ZABBIX_T_FLAGS(ZABBIX_T_AGENT | ZABBIX_T_CONFIG | ZABBIX_T_ACTIVE);
            agent_name = json_get_string(json_str, tokens, "host");
            zabbix_info->host_name = wmem_strdup(wmem_file_scope(), agent_name);
            proto_item_set_text(ti,
                "Zabbix Request for active checks for \"%s\"", (agent_name ? agent_name : ZABBIX_UNKNOWN));
            col_add_fstr(pinfo->cinfo, COL_INFO,
                "Zabbix Request for active checks for \"%s\"", (agent_name ? agent_name : ZABBIX_UNKNOWN));
            agent_hostmetadata = json_get_string(json_str, tokens, "host_metadata");
            agent_hostinterface = json_get_string(json_str, tokens, "interface");
            agent_listenip = json_get_string(json_str, tokens, "ip");
            if (json_get_double(json_str, tokens, "port", &temp_double)) {
                agent_listenport = (uint16_t)temp_double;
            }
        }
        else if (strcmp(request_type, "agent data") == 0) {
            /* Active agent sending data */
            ADD_ZABBIX_T_FLAGS(ZABBIX_T_AGENT | ZABBIX_T_DATA | ZABBIX_T_ACTIVE);
            /* Zabbix Agent 2 has Host in the top level */
            agent_name = json_get_string(json_str, tokens, "host");
            if (!agent_name) {
                /* For Zabbix Agent try parsing agent name inside data array */
                jsmntok_t *tok = json_get_array(json_str, tokens, "data");
                if (tok && json_get_array_len(tok) > 0) {
                    jsmntok_t *datatok = json_get_array_index(tok, 0);
                    agent_name = json_get_string(json_str, datatok, "host");
                }
            }
            zabbix_info->host_name = wmem_strdup(wmem_file_scope(), agent_name);
            proto_item_set_text(ti,
                "Zabbix Send agent data from \"%s\"", (agent_name ? agent_name : ZABBIX_UNKNOWN));
            col_add_fstr(pinfo->cinfo, COL_INFO,
                "Zabbix Send agent data from \"%s\"", (agent_name ? agent_name : ZABBIX_UNKNOWN));
        }
        else if (strcmp(request_type, "active check heartbeat") == 0) {
            /* Active agent sending heartbeat */
            /* Clear the request flag first */
            zabbix_info->oper_flags = 0;
            ADD_ZABBIX_T_FLAGS(ZABBIX_T_AGENT | ZABBIX_T_HEARTBEAT | ZABBIX_T_ACTIVE);
            agent_name = json_get_string(json_str, tokens, "host");
            if (json_get_double(json_str, tokens, "heartbeat_freq", &temp_double)) {
                agent_hb_freq = (int)temp_double;
            }
            proto_item_set_text(ti,
                "Zabbix Agent heartbeat from \"%s\"", (agent_name ? agent_name : ZABBIX_UNKNOWN));
            col_add_fstr(pinfo->cinfo, COL_INFO,
                "Zabbix Agent heartbeat from \"%s\"", (agent_name ? agent_name : ZABBIX_UNKNOWN));
        }
        else if (strcmp(request_type, "sender data") == 0) {
            /* Sender/trapper */
            ADD_ZABBIX_T_FLAGS(ZABBIX_T_SENDER);
            /* Try to get the sender name from the first data array item */
            jsmntok_t *tok = json_get_array(json_str, tokens, "data");
            if (tok && json_get_array_len(tok) > 0) {
                jsmntok_t *datatok = json_get_array_index(tok, 0);
                sender_name = json_get_string(json_str, datatok, "host");
            }
            zabbix_info->host_name = wmem_strdup(wmem_file_scope(), sender_name);
            proto_item_set_text(ti,
                "Zabbix Sender data from \"%s\"", (sender_name ? sender_name : ZABBIX_UNKNOWN));
            col_add_fstr(pinfo->cinfo, COL_INFO,
                "Zabbix Sender data from \"%s\"", (sender_name ? sender_name : ZABBIX_UNKNOWN));
        }
        else if ((strcmp(request_type, "proxy data") == 0) ||
                (strcmp(request_type, "host availability") == 0) ||
                (strcmp(request_type, "history data") == 0) ||
                (strcmp(request_type, "discovery data") == 0) ||
                (strcmp(request_type, "auto registration") == 0)) {
            /* Either active or passive proxy; "proxy data" = Zabbix 3.4+,
             * others = Zabbix 3.2 or older */
            proxy_name = json_get_string(json_str, tokens, "host");
            if (token_count == 3) {     /* Only '{"request":"xxx"}' */
                /* This is Zabbix server connecting to passive proxy */
                ADD_ZABBIX_T_FLAGS(ZABBIX_T_PROXY | ZABBIX_T_DATA | ZABBIX_T_PASSIVE);
                proto_item_set_text(ti, "Zabbix Request for passive proxy data");
                col_add_fstr(pinfo->cinfo, COL_INFO, "Zabbix Request for passive proxy data");
            }
            else if (proxy_name) {
                /* This is an active proxy connecting to server */
                ADD_ZABBIX_T_FLAGS(ZABBIX_T_PROXY | ZABBIX_T_DATA | ZABBIX_T_ACTIVE);
                zabbix_info->host_name = wmem_strdup(wmem_file_scope(), proxy_name);
                proto_item_set_text(ti, "Zabbix Proxy data from \"%s\"", proxy_name);
                col_add_fstr(pinfo->cinfo, COL_INFO, "Zabbix Proxy data from \"%s\"", proxy_name);
            }
        }
        else if (strcmp(request_type, "proxy config") == 0) {
            /* Either active or passive proxy */
            proxy_name = json_get_string(json_str, tokens, "host");
            if (token_count == 3) {     /* Only '{"request":"proxy config"}' */
                /* This is Zabbix 6.4+ server connecting to passive proxy */
                ADD_ZABBIX_T_FLAGS(ZABBIX_T_PROXY | ZABBIX_T_CONFIG | ZABBIX_T_PASSIVE);
                proto_item_set_text(ti, "Zabbix Start send proxy config to passive proxy");
                col_add_fstr(pinfo->cinfo, COL_INFO, "Zabbix Start send proxy config to passive proxy");
            }
            else if (proxy_name) {
                /* This is an active proxy connecting to server */
                ADD_ZABBIX_T_FLAGS(ZABBIX_T_PROXY | ZABBIX_T_CONFIG | ZABBIX_T_ACTIVE);
                zabbix_info->host_name = wmem_strdup(wmem_file_scope(), proxy_name);
                proto_item_set_text(ti, "Zabbix Request proxy config for \"%s\"", proxy_name);
                col_add_fstr(pinfo->cinfo, COL_INFO, "Zabbix Request proxy config for \"%s\"", proxy_name);
            }
        }
        else if (strcmp(request_type, "proxy heartbeat") == 0) {
            /* Heartbeat from active proxy, not used in Zabbix 6.4+ */
            ADD_ZABBIX_T_FLAGS(ZABBIX_T_PROXY | ZABBIX_T_HEARTBEAT | ZABBIX_T_ACTIVE);
            proxy_name = json_get_string(json_str, tokens, "host");
            zabbix_info->host_name = wmem_strdup(wmem_file_scope(), proxy_name);
            proto_item_set_text(ti, "Zabbix Proxy heartbeat from \"%s\"", proxy_name);
            col_add_fstr(pinfo->cinfo, COL_INFO, "Zabbix Proxy heartbeat from \"%s\"", proxy_name);
        }
    }
    else if (json_get_object(json_str, tokens, "globalmacro")) {
        /* This is Zabbix server before 6.4 sending configurations to active proxy */
        ADD_ZABBIX_T_FLAGS(ZABBIX_T_PROXY | ZABBIX_T_CONFIG | ZABBIX_T_ACTIVE);
        proxy_name = zabbix_info->host_name;
        proto_item_set_text(ti,
            "Zabbix Response for proxy config for \"%s\"", (proxy_name ? proxy_name : ZABBIX_UNKNOWN));
        col_add_fstr(pinfo->cinfo, COL_INFO,
            "Zabbix Response for proxy config for \"%s\"", (proxy_name ? proxy_name : ZABBIX_UNKNOWN));
    }
    else if (json_get_double(json_str, tokens, "full_sync", &temp_double)) {
        /* This is Zabbix 6.4+ server sending proxy config to active or passive proxy */
        /* Only present when value is 1 */
        ADD_ZABBIX_T_FLAGS(ZABBIX_T_PROXY | ZABBIX_T_CONFIG);
        oper_response |= ZABBIX_RESPONSE_FULLSYNC;
        /* Active/passive flag was set in the earlier packet */
        if (IS_ZABBIX_T_FLAGS(ZABBIX_T_PASSIVE)) {
            /* There is no proxy name anywhere to use */
            proto_item_set_text(ti, "Zabbix Passive proxy config");
            col_add_fstr(pinfo->cinfo, COL_INFO, "Zabbix Passive proxy config");
        }
        else {
            proxy_name = zabbix_info->host_name;
            proto_item_set_text(ti,
                "Zabbix Response for proxy config for \"%s\"", (proxy_name ? proxy_name : ZABBIX_UNKNOWN));
            col_add_fstr(pinfo->cinfo, COL_INFO,
                "Zabbix Response for proxy config for \"%s\"", (proxy_name ? proxy_name : ZABBIX_UNKNOWN));
        }
    }
    else if (response_status) {
        if (strcmp(response_status, "success") == 0) {
            oper_response |= ZABBIX_RESPONSE_SUCCESS;
        }
        else if (strcmp(response_status, "failed") == 0) {
            oper_response |= ZABBIX_RESPONSE_FAILED;
        }
        if (IS_ZABBIX_T_FLAGS(ZABBIX_T_AGENT)) {
            agent_name = zabbix_info->host_name;
            if (IS_ZABBIX_T_FLAGS(ZABBIX_T_CONFIG | ZABBIX_T_ACTIVE)) {
                proto_item_set_text(ti,
                    "Zabbix Response for active checks for \"%s\" (%s)", (agent_name ? agent_name : ZABBIX_UNKNOWN), response_status);
                col_add_fstr(pinfo->cinfo, COL_INFO,
                    "Zabbix Response for active checks for \"%s\" (%s)", (agent_name ? agent_name : ZABBIX_UNKNOWN), response_status);
            }
            else if (IS_ZABBIX_T_FLAGS(ZABBIX_T_DATA | ZABBIX_T_ACTIVE)) {
                proto_item_set_text(ti,
                    "Zabbix Response for agent data for \"%s\" (%s)", (agent_name ? agent_name : ZABBIX_UNKNOWN), response_status);
                col_add_fstr(pinfo->cinfo, COL_INFO,
                    "Zabbix Response for agent data for \"%s\" (%s)", (agent_name ? agent_name : ZABBIX_UNKNOWN), response_status);
            }
        }
        else if (IS_ZABBIX_T_FLAGS(ZABBIX_T_PROXY)) {
            proxy_name = zabbix_info->host_name;
            if (IS_ZABBIX_T_FLAGS(ZABBIX_T_CONFIG | ZABBIX_T_ACTIVE)) {
                proto_item_set_text(ti,
                    "Zabbix Response for active proxy config request for \"%s\" (%s)", (proxy_name ? proxy_name : ZABBIX_UNKNOWN), response_status);
                col_add_fstr(pinfo->cinfo, COL_INFO,
                    "Zabbix Response for active proxy config request for \"%s\" (%s)", (proxy_name ? proxy_name : ZABBIX_UNKNOWN), response_status);
            }
            else if (IS_ZABBIX_T_FLAGS(ZABBIX_T_DATA | ZABBIX_T_ACTIVE)) {
                proto_item_set_text(ti,
                    "Zabbix Response for active proxy data for \"%s\" (%s)", (proxy_name ? proxy_name : ZABBIX_UNKNOWN), response_status);
                col_add_fstr(pinfo->cinfo, COL_INFO,
                    "Zabbix Response for active proxy data for \"%s\" (%s)", (proxy_name ? proxy_name : ZABBIX_UNKNOWN), response_status);
            }
            else if (IS_ZABBIX_T_FLAGS(ZABBIX_T_CONFIG | ZABBIX_T_PASSIVE)) {
                proto_item_set_text(ti,
                    "Zabbix Response for passive proxy config (%s)", response_status);
                col_add_fstr(pinfo->cinfo, COL_INFO,
                    "Zabbix Response for passive proxy config (%s)", response_status);
            }
            else if (IS_ZABBIX_T_FLAGS(ZABBIX_T_DATA | ZABBIX_T_PASSIVE)) {
                proto_item_set_text(ti,
                    "Zabbix Response for passive proxy data (%s)", response_status);
                col_add_fstr(pinfo->cinfo, COL_INFO,
                    "Zabbix Response for passive proxy data (%s)", response_status);
            }
            else if (IS_ZABBIX_T_FLAGS(ZABBIX_T_HEARTBEAT | ZABBIX_T_ACTIVE)) {
                proto_item_set_text(ti,
                    "Zabbix Response for active proxy heartbeat for \"%s\" (%s)", (proxy_name ? proxy_name : ZABBIX_UNKNOWN), response_status);
                col_add_fstr(pinfo->cinfo, COL_INFO,
                    "Zabbix Response for active proxy heartbeat for \"%s\" (%s)", (proxy_name ? proxy_name : ZABBIX_UNKNOWN), response_status);
            }
        }
        else if (IS_ZABBIX_T_FLAGS(ZABBIX_T_SENDER)) {
            sender_name = zabbix_info->host_name;
            proto_item_set_text(ti,
                "Zabbix Response for sender data for \"%s\" (%s)", (sender_name ? sender_name : ZABBIX_UNKNOWN), response_status);
            col_add_fstr(pinfo->cinfo, COL_INFO,
                "Zabbix Response for sender data for \"%s\" (%s)", (sender_name ? sender_name : ZABBIX_UNKNOWN), response_status);
        }
    }
    else if (data_object || data_array) {
        /* No other match above, let's assume this is server sending incremental
         * configuration to a proxy
         */
        ADD_ZABBIX_T_FLAGS(ZABBIX_T_PROXY | ZABBIX_T_CONFIG);
        if (data_object && (data_object->size == 0)) {
            /* Empty data object */
            oper_response |= ZABBIX_RESPONSE_NOCHANGE;
        }
        else if (data_array) {
            /* This was not a "full_sync" but data array exists */
            oper_response |= ZABBIX_RESPONSE_INCREMENTAL;
        }
        if (IS_ZABBIX_T_FLAGS(ZABBIX_T_PASSIVE)) {
            /* There is no proxy name anywhere to use */
            proto_item_set_text(ti, "Zabbix Passive proxy config");
            col_add_fstr(pinfo->cinfo, COL_INFO, "Zabbix Passive proxy config");
        }
        else {
            proxy_name = zabbix_info->host_name;
            proto_item_set_text(ti,
                "Zabbix Response for proxy config for \"%s\"", (proxy_name ? proxy_name : ZABBIX_UNKNOWN));
            col_add_fstr(pinfo->cinfo, COL_INFO,
                "Zabbix Response for proxy config for \"%s\"", (proxy_name ? proxy_name : ZABBIX_UNKNOWN));
        }
    }
    else if (session && version) {
        /* Last guesses: responses from passive proxy */
        if (IS_ZABBIX_T_FLAGS(ZABBIX_T_PROXY | ZABBIX_T_CONFIG | ZABBIX_T_PASSIVE)) {
            proto_item_set_text(ti, "Zabbix Passive proxy response for config push");
            col_add_fstr(pinfo->cinfo, COL_INFO, "Zabbix Passive proxy response for config push");
        }
        else if (IS_ZABBIX_T_FLAGS(ZABBIX_T_PROXY | ZABBIX_T_DATA | ZABBIX_T_PASSIVE)) {
            proto_item_set_text(ti, "Zabbix Passive proxy data response");
            col_add_fstr(pinfo->cinfo, COL_INFO, "Zabbix Passive proxy data response");
        }
    }


    /* Add all relevant fields to the tree */

show_agent_outputs:
    if (IS_ZABBIX_T_FLAGS(ZABBIX_T_AGENT)) {
        temp_ti = proto_tree_add_boolean(zabbix_tree, hf_zabbix_agent, NULL, 0, 0, true);
        proto_item_set_text(temp_ti, "This is an agent connection");
        proto_item_set_generated(temp_ti);
        if (IS_ZABBIX_T_FLAGS(ZABBIX_T_DATA)) {
            temp_ti = proto_tree_add_boolean(zabbix_tree, hf_zabbix_agent_data, NULL, 0, 0, true);
            if (IS_ZABBIX_T_FLAGS(ZABBIX_T_RESPONSE)) {
                /* Set as generated, not seen in data */
                proto_item_set_generated(temp_ti);
            }
        }
        else if (IS_ZABBIX_T_FLAGS(ZABBIX_T_CONFIG)) {
            temp_ti = proto_tree_add_boolean(zabbix_tree, hf_zabbix_agent_config, NULL, 0, 0, true);
            if (IS_ZABBIX_T_FLAGS(ZABBIX_T_RESPONSE)) {
                /* Set as generated, not seen in data */
                proto_item_set_generated(temp_ti);
            }
        }
        else if (IS_ZABBIX_T_FLAGS(ZABBIX_T_HEARTBEAT)) {
            proto_tree_add_boolean(zabbix_tree, hf_zabbix_agent_hb, NULL, 0, 0, true);
        }
        else if (IS_ZABBIX_T_FLAGS(ZABBIX_T_PASSIVE)) {
            temp_ti = proto_tree_add_boolean(zabbix_tree, hf_zabbix_agent_passive, NULL, 0, 0, true);
            proto_item_set_text(temp_ti, "Agent is in passive mode");
            proto_item_set_generated(temp_ti);
        }
    }
    else if (IS_ZABBIX_T_FLAGS(ZABBIX_T_PROXY)) {
        temp_ti = proto_tree_add_boolean(zabbix_tree, hf_zabbix_proxy, NULL, 0, 0, true);
        proto_item_set_text(temp_ti, "This is a proxy connection");
        proto_item_set_generated(temp_ti);
    }
    else if (IS_ZABBIX_T_FLAGS(ZABBIX_T_SENDER)) {
        temp_ti = proto_tree_add_boolean(zabbix_tree, hf_zabbix_sender, NULL, 0, 0, true);
        proto_item_set_text(temp_ti, "This is a sender connection");
        proto_item_set_generated(temp_ti);
    }
    if (oper_response & ZABBIX_RESPONSE_SUCCESS) {
        proto_tree_add_boolean(zabbix_tree, hf_zabbix_success, NULL, 0, 0, true);
    }
    else if (oper_response & ZABBIX_RESPONSE_FAILED) {
        proto_tree_add_boolean(zabbix_tree, hf_zabbix_failed, NULL, 0, 0, true);
    }
    if (IS_ZABBIX_T_FLAGS(ZABBIX_T_AGENT)) {
        if (agent_name) {
            temp_ti = proto_tree_add_string(zabbix_tree, hf_zabbix_agent_name, NULL, 0, 0, agent_name);
            if (IS_ZABBIX_T_FLAGS(ZABBIX_T_RESPONSE)) {
                /* agent_name was populated from the conversation */
                proto_item_set_generated(temp_ti);
                proto_item_append_text(temp_ti, " (from the request)");
            }
        }
        if (agent_hb_freq) {
            proto_tree_add_int(zabbix_tree, hf_zabbix_agent_hb_freq, NULL, 0, 0, agent_hb_freq);
        }
        if (agent_hostmetadata) {
            proto_tree_add_string(zabbix_tree, hf_zabbix_agent_hostmetadata, NULL, 0, 0, agent_hostmetadata);
        }
        if (agent_hostinterface) {
            proto_tree_add_string(zabbix_tree, hf_zabbix_agent_hostinterface, NULL, 0, 0, agent_hostinterface);
        }
        if (agent_listenip) {
            if (strstr(agent_listenip, ":") != NULL) {
                ws_in6_addr addr6;
                if (ws_inet_pton6(agent_listenip, &addr6)) {
                    proto_tree_add_ipv6(zabbix_tree, hf_zabbix_agent_listenipv6, NULL, 0, 0, &addr6);
                }
            }
            else {
                ws_in4_addr addr4;
                if (ws_inet_pton4(agent_listenip, &addr4)) {
                    proto_tree_add_ipv4(zabbix_tree, hf_zabbix_agent_listenipv4, NULL, 0, 0, addr4);
                }
            }
        }
        if (agent_listenport) {
            proto_tree_add_uint(zabbix_tree, hf_zabbix_agent_listenport, NULL, 0, 0, agent_listenport);
        }
    }
    else if (IS_ZABBIX_T_FLAGS(ZABBIX_T_PROXY)) {
        if (proxy_name) {
            temp_ti = proto_tree_add_string(zabbix_tree, hf_zabbix_proxy_name, NULL, 0, 0, proxy_name);
            if (IS_ZABBIX_T_FLAGS(ZABBIX_T_RESPONSE)) {
                /* proxy_name was populated from the conversation */
                proto_item_set_generated(temp_ti);
                proto_item_append_text(temp_ti, " (from the request)");
            }
        }
        if (IS_ZABBIX_T_FLAGS(ZABBIX_T_DATA)) {
            proto_tree_add_boolean(zabbix_tree, hf_zabbix_proxy_data, NULL, 0, 0, true);
        }
        else if (IS_ZABBIX_T_FLAGS(ZABBIX_T_CONFIG)) {
            proto_tree_add_boolean(zabbix_tree, hf_zabbix_proxy_config, NULL, 0, 0, true);
            if (oper_response & ZABBIX_RESPONSE_FULLSYNC) {
                proto_tree_add_boolean(zabbix_tree, hf_zabbix_proxy_fullsync, NULL, 0, 0, true);
            }
            else if (oper_response & ZABBIX_RESPONSE_INCREMENTAL) {
                proto_tree_add_boolean(zabbix_tree, hf_zabbix_proxy_incr_config, NULL, 0, 0, true);
            }
            else if (oper_response & ZABBIX_RESPONSE_NOCHANGE) {
                proto_tree_add_boolean(zabbix_tree, hf_zabbix_proxy_no_config_change, NULL, 0, 0, true);
            }
        }
        else if (IS_ZABBIX_T_FLAGS(ZABBIX_T_HEARTBEAT)) {
            proto_tree_add_boolean(zabbix_tree, hf_zabbix_proxy_hb, NULL, 0, 0, true);
        }
    }
    else if (sender_name) {
        temp_ti = proto_tree_add_string(zabbix_tree, hf_zabbix_sender_name, NULL, 0, 0, sender_name);
        if (IS_ZABBIX_T_FLAGS(ZABBIX_T_RESPONSE)) {
            /* sender_name was populated from the conversation */
            proto_item_set_generated(temp_ti);
            proto_item_append_text(temp_ti, " (from the request)");
        }
    }
    if (version) {
        proto_tree_add_string(zabbix_tree, hf_zabbix_version, NULL, 0, 0, version);
    }
    if (config_revision > -1) {
        proto_tree_add_int64(zabbix_tree, hf_zabbix_config_revision, NULL, 0, 0, config_revision);
    }
    if (session) {
        proto_tree_add_string(zabbix_tree, hf_zabbix_session, NULL, 0, 0, session);
    }
    /* Show also the full JSON (or passive agent request/response) */
    proto_tree_add_item(zabbix_tree, hf_zabbix_data, next_tvb, 0, (int)datalen, ENC_UTF_8);

final_outputs:

    /* These are common for all cases, too large or not */

    /* Check the ZABBIX_T_REQUEST flag (and not CONV_IS_ZABBIX_REQUEST macro) because
     * heartbeats are not marked as requests */
    if (IS_ZABBIX_T_FLAGS(ZABBIX_T_REQUEST)) {
        temp_ti = proto_tree_add_boolean(zabbix_tree, hf_zabbix_request, NULL, 0, 0, true);
        proto_item_set_text(temp_ti, "This is Zabbix request");
    } else if (CONV_IS_ZABBIX_RESPONSE(zabbix_info, pinfo)) {
        temp_ti = proto_tree_add_boolean(zabbix_tree, hf_zabbix_response, NULL, 0, 0, true);
        proto_item_set_text(temp_ti, "This is Zabbix response");
        if (!nstime_is_unset(&zabbix_info->req_timestamp)) {
            nstime_t delta;
            nstime_delta(&delta, &pinfo->abs_ts, &zabbix_info->req_timestamp);
            pi = proto_tree_add_time(zabbix_tree, hf_zabbix_time, next_tvb, 0, 0, &delta);
            proto_item_set_generated(pi);
        }
    }

    /* Add length to the Zabbix tree text */
    proto_item_append_text(ti, ", Len=%u", (unsigned)length);
    /* Add/set Info column texts */
    const gchar *info_text = col_get_text(pinfo->cinfo, COL_INFO);
    if (!info_text || !strlen(info_text)) {
        /* Info column is still empty, set the default text */
        if (CONV_IS_ZABBIX_REQUEST(zabbix_info, pinfo)) {
            col_add_fstr(pinfo->cinfo, COL_INFO, "Zabbix Protocol request, Flags=0x%02x", flags);
        } else if (CONV_IS_ZABBIX_RESPONSE(zabbix_info, pinfo)) {
            col_add_fstr(pinfo->cinfo, COL_INFO, "Zabbix Protocol response, Flags=0x%02x", flags);
        } else {
            col_add_fstr(pinfo->cinfo, COL_INFO, "Zabbix Protocol, Flags=0x%02x", flags);
        }
    }
    col_append_fstr(pinfo->cinfo, COL_INFO, ", Len=%u (", (unsigned)length);
    col_append_ports(pinfo->cinfo, COL_INFO, PT_TCP, pinfo->srcport, pinfo->destport);
    col_append_str(pinfo->cinfo, COL_INFO, ")");

    return tvb_reported_length(tvb);
}

/* Determine PDU length of Zabbix protocol */
static unsigned
get_zabbix_pdu_len(packet_info *pinfo _U_, tvbuff_t *tvb, int offset, void *data _U_)
{
    uint8_t flags;
    uint64_t length;

    flags = tvb_get_guint8(tvb, offset+4);
    if (flags & ZABBIX_FLAG_LARGEPACKET) {
        /* 8-byte length field
         * Note that ZABBIX_HDR_MIN_LEN check (in dissect_zabbix()) is still enough
         * due to the header structure (there are reserved bytes)
         */
        length = tvb_get_guint64(tvb, offset+5, ENC_LITTLE_ENDIAN) + ZABBIX_HDR_MAX_LEN;
    } else {
        /* 4-byte length */
        length = tvb_get_guint32(tvb, offset+5, ENC_LITTLE_ENDIAN) + ZABBIX_HDR_MIN_LEN;
    }
    return (unsigned)length;
}

/* The main dissecting routine */
static int
dissect_zabbix(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
{
    uint8_t flags;

    if (tvb_captured_length(tvb) < ZABBIX_HDR_MIN_LEN) {
        /* Not enough data */
        return 0;
    }
    if (tvb_memeql(tvb, 0, ZABBIX_HDR_SIGNATURE, 4)) {
        /* Encrypted or not Zabbix at all */
        return 0;
    }
    flags = tvb_get_guint8(tvb, 4);
    if (!(flags & ZABBIX_FLAG_ZABBIX_COMMUNICATIONS)) {
        return 0;
    }
    /* This is unencrypted Zabbix protocol, continue with dissecting it */
    tcp_dissect_pdus(tvb, pinfo, tree, zabbix_desegment, ZABBIX_HDR_MIN_LEN,
        get_zabbix_pdu_len, dissect_zabbix_pdu, data);
    return tvb_reported_length(tvb);
}

/* Register the protocol with Wireshark */

void
proto_register_zabbix(void)
{
    static hf_register_info hf[] = {
        { &hf_zabbix_header,
            { "Header", "zabbix.header",
            FT_STRING, BASE_NONE, NULL, 0,
            NULL, HFILL }
        },
        { &hf_zabbix_flags,
            { "Flags", "zabbix.flags",
            FT_UINT8, BASE_HEX, NULL, 0,
            NULL, HFILL }
        },
        { &hf_zabbix_flag_zabbix_communications,
            { "Zabbix communications protocol", "zabbix.flags.zabbix",
            FT_BOOLEAN, 8, TFS(&tfs_yes_no), ZABBIX_FLAG_ZABBIX_COMMUNICATIONS,
            NULL, HFILL }
        },
        { &hf_zabbix_flag_compressed,
            { "Compressed", "zabbix.flags.compressed",
            FT_BOOLEAN, 8, TFS(&tfs_yes_no), ZABBIX_FLAG_COMPRESSED,
            NULL, HFILL }
        },
        { &hf_zabbix_flag_largepacket,
            { "Large packet", "zabbix.flags.large_packet",
            FT_BOOLEAN, 8, TFS(&tfs_yes_no), ZABBIX_FLAG_LARGEPACKET,
            NULL, HFILL }
        },
        { &hf_zabbix_flag_reserved,
            { "Reserved bits", "zabbix.flags.reserved",
            FT_UINT8, BASE_DEC, NULL, ZABBIX_FLAG_RESERVED,
            NULL, HFILL }
        },
        { &hf_zabbix_length,
            { "Length", "zabbix.len",
            FT_UINT32, BASE_DEC, NULL, 0,
            NULL, HFILL }
        },
        { &hf_zabbix_reserved,
            { "Reserved", "zabbix.reserved",
            FT_UINT32, BASE_DEC, NULL, 0,
            NULL, HFILL }
        },
        { &hf_zabbix_uncompressed_length,
            { "Uncompressed length", "zabbix.uncompressed_len",
            FT_UINT32, BASE_DEC, NULL, 0,
            NULL, HFILL }
        },
        { &hf_zabbix_large_length,
            { "Large length", "zabbix.large.len",
            FT_UINT64, BASE_DEC, NULL, 0,
            NULL, HFILL }
        },
        { &hf_zabbix_large_reserved,
            { "Large reserved", "zabbix.large.reserved",
            FT_UINT64, BASE_DEC, NULL, 0,
            NULL, HFILL }
        },
        { &hf_zabbix_large_uncompressed_length,
            { "Large uncompressed length", "zabbix.large.uncompressed_len",
            FT_UINT64, BASE_DEC, NULL, 0,
            NULL, HFILL }
        },
        { &hf_zabbix_data,
            { "Data", "zabbix.data",
            FT_STRING, BASE_NONE, NULL, 0,
            NULL, HFILL }
        },
        { &hf_zabbix_time,
            { "Response time", "zabbix.time",
            FT_RELATIVE_TIME, BASE_NONE, NULL, 0,
            NULL, HFILL }
        },
        { &hf_zabbix_request,
            { "Zabbix protocol request", "zabbix.request",
            FT_BOOLEAN, BASE_NONE, NULL, 0,
            NULL, HFILL }
        },
        { &hf_zabbix_response,
            { "Zabbix protocol response", "zabbix.response",
            FT_BOOLEAN, BASE_NONE, NULL, 0,
            NULL, HFILL }
        },
        { &hf_zabbix_success,
            { "Success", "zabbix.success",
            FT_BOOLEAN, BASE_NONE, TFS(&tfs_yes_no), 0,
            NULL, HFILL }
        },
        { &hf_zabbix_failed,
            { "Failed", "zabbix.failed",
            FT_BOOLEAN, BASE_NONE, TFS(&tfs_yes_no), 0,
            NULL, HFILL }
        },
        { &hf_zabbix_agent,
            { "Zabbix agent connection", "zabbix.agent",
            FT_BOOLEAN, BASE_NONE, NULL, 0,
            NULL, HFILL }
        },
        { &hf_zabbix_agent_config,
            { "Zabbix agent config", "zabbix.agent.config",
            FT_BOOLEAN, BASE_NONE, TFS(&tfs_yes_no), 0,
            NULL, HFILL }
        },
        { &hf_zabbix_agent_data,
            { "Zabbix agent data", "zabbix.agent.data",
            FT_BOOLEAN, BASE_NONE, TFS(&tfs_yes_no), 0,
            NULL, HFILL }
        },
        { &hf_zabbix_agent_passive,
            { "Passive agent", "zabbix.agent.passive",
            FT_BOOLEAN, BASE_NONE, TFS(&tfs_yes_no), 0,
            NULL, HFILL }
        },
        { &hf_zabbix_agent_name,
            { "Agent name", "zabbix.agent.name",
            FT_STRING, BASE_NONE, NULL, 0,
            NULL, HFILL }
        },
        { &hf_zabbix_agent_hb,
            { "Agent heartbeat", "zabbix.agent.heartbeat",
            FT_BOOLEAN, BASE_NONE, TFS(&tfs_yes_no), 0,
            NULL, HFILL }
        },
        { &hf_zabbix_agent_hb_freq,
            { "Agent heartbeat frequency", "zabbix.agent.heartbeat_freq",
            FT_INT32, BASE_DEC, NULL, 0,
            NULL, HFILL }
        },
        { &hf_zabbix_agent_hostmetadata,
            { "Agent host metadata", "zabbix.agent.host_metadata",
            FT_STRING, BASE_NONE, NULL, 0,
            NULL, HFILL }
        },
        { &hf_zabbix_agent_hostinterface,
            { "Agent host interface", "zabbix.agent.host_interface",
            FT_STRING, BASE_NONE, NULL, 0,
            NULL, HFILL }
        },
        { &hf_zabbix_agent_listenipv4,
            { "Agent listen IPv4", "zabbix.agent.listen_ipv4",
            FT_IPv4, BASE_NONE, NULL, 0,
            NULL, HFILL }
        },
        { &hf_zabbix_agent_listenipv6,
            { "Agent listen IPv6", "zabbix.agent.listen_ipv6",
            FT_IPv6, BASE_NONE, NULL, 0,
            NULL, HFILL }
        },
        { &hf_zabbix_agent_listenport,
            { "Agent listen port", "zabbix.agent.listen_port",
            FT_UINT16, BASE_DEC, NULL, 0,
            NULL, HFILL }
        },
        { &hf_zabbix_proxy,
            { "Proxy connection", "zabbix.proxy",
            FT_BOOLEAN, BASE_NONE, TFS(&tfs_yes_no), 0,
            NULL, HFILL }
        },
        { &hf_zabbix_proxy_name,
            { "Proxy name", "zabbix.proxy.name",
            FT_STRING, BASE_NONE, NULL, 0,
            NULL, HFILL }
        },
        { &hf_zabbix_proxy_hb,
            { "Proxy heartbeat", "zabbix.proxy.heartbeat",
            FT_BOOLEAN, BASE_NONE, TFS(&tfs_yes_no), 0,
            NULL, HFILL }
        },
        { &hf_zabbix_proxy_data,
            { "Proxy data", "zabbix.proxy.data",
            FT_BOOLEAN, BASE_NONE, TFS(&tfs_yes_no), 0,
            NULL, HFILL }
        },
        { &hf_zabbix_proxy_config,
            { "Proxy config", "zabbix.proxy.config",
            FT_BOOLEAN, BASE_NONE, TFS(&tfs_yes_no), 0,
            NULL, HFILL }
        },
        { &hf_zabbix_proxy_fullsync,
            { "Proxy config full sync", "zabbix.proxy.full_sync",
            FT_BOOLEAN, BASE_NONE, TFS(&tfs_yes_no), 0,
            NULL, HFILL }
        },
        { &hf_zabbix_proxy_incr_config,
            { "Proxy incremental config", "zabbix.proxy.incremental_config",
            FT_BOOLEAN, BASE_NONE, TFS(&tfs_yes_no), 0,
            NULL, HFILL }
        },
        { &hf_zabbix_proxy_no_config_change,
            { "Proxy no config changes", "zabbix.proxy.no_config_changes",
            FT_BOOLEAN, BASE_NONE, TFS(&tfs_yes_no), 0,
            NULL, HFILL }
        },
        { &hf_zabbix_sender,
            { "Sender connection", "zabbix.sender",
            FT_BOOLEAN, BASE_NONE, TFS(&tfs_yes_no), 0,
            NULL, HFILL }
        },
        { &hf_zabbix_sender_name,
            { "Sender name", "zabbix.sender.name",
            FT_STRING, BASE_NONE, NULL, 0,
            NULL, HFILL }
        },
        { &hf_zabbix_version,
            { "Version", "zabbix.version",
            FT_STRING, BASE_NONE, NULL, 0,
            NULL, HFILL }
        },
        { &hf_zabbix_session,
            { "Session", "zabbix.session",
            FT_STRING, BASE_NONE, NULL, 0,
            NULL, HFILL }
        },
        { &hf_zabbix_config_revision,
            { "Config revision", "zabbix.config_revision",
            FT_INT64, BASE_DEC, NULL, 0,
            NULL, HFILL }
        },
    };

    static ei_register_info ei[] = {
        {
            &ei_zabbix_packet_too_large,
                { "zabbix.packet_too_large", PI_UNDECODED, PI_WARN,
                "Packet is too large for detailed dissection", EXPFILL }
        },
        {
            &ei_zabbix_json_error,
                { "zabbix.json_error", PI_PROTOCOL, PI_ERROR,
                "Cannot parse JSON", EXPFILL }
        },
    };

    /* Setup protocol subtree array */
    static gint *ett[] = {
        &ett_zabbix,
    };

    module_t *zabbix_module;
    expert_module_t *expert_zabbix;

    /* Register the protocol name and description */
    proto_zabbix = proto_register_protocol("Zabbix Protocol", "Zabbix", "zabbix");

    /* Required function calls to register the header fields and subtrees used */
    proto_register_field_array(proto_zabbix, hf, array_length(hf));
    proto_register_subtree_array(ett, array_length(ett));

    zabbix_module = prefs_register_protocol(proto_zabbix, NULL);

    prefs_register_bool_preference(zabbix_module, "desegment",
        "Reassemble Zabbix messages spanning multiple TCP segments",
        "Whether the Zabbix protocol dissector should reassemble messages spanning multiple TCP segments."
        " To use this option, you must also enable \"Allow subdissectors to reassemble TCP streams\" in the TCP protocol settings.",
        &zabbix_desegment);

    zabbix_handle = register_dissector("zabbix", dissect_zabbix, proto_zabbix);

    expert_zabbix = expert_register_protocol(proto_zabbix);
    expert_register_field_array(expert_zabbix, ei, array_length(ei));
}

void
proto_reg_handoff_zabbix(void)
{
    dissector_add_uint_range_with_preference("tcp.port", ZABBIX_TCP_PORTS, zabbix_handle);
    zabbix_port_range = prefs_get_range_value("Zabbix", "tcp.port");
    dissector_add_uint_range("tls.port", zabbix_port_range, zabbix_handle);
}