[comment]: # translation:outdated

[comment]: # ({fff80fa1-9153def3})
# 4 Header

[comment]: # ({/fff80fa1-9153def3})

[comment]: # ({new-faa3c87b})
#### Overview

The header is present in all request and response messages between Zabbix components. 
It is required to determine the message length, if it is compressed or not, if it is a large packet or not.

Zabbix communications protocol has 1GB packet size limit per connection. The limit of 1GB 
is applied to both the received packet data length and the uncompressed data length.

When sending configuration to Zabbix proxy, the packet size limit is increased to 4GB to allow 
syncing large configurations. When data length before compression exceeds 4GB, Zabbix server automatically 
starts using the large packet format (0x04 flag) which increases the packet size limit to 16GB.

Note that while a large packet format can be used for sending any data, currently only the Zabbix proxy 
configuration syncer can handle packets that are larger than 1GB.

[comment]: # ({/new-faa3c87b})

[comment]: # ({new-409c40cc})

#### Structure

The header consists of four fields. All numbers in the header are formatted as little-endian.

|Field|Size|Size<br>(large packet)|Description|
|--|-|-|------|
|`<PROTOCOL>`|4|4|`"ZBXD"` or `5A 42 58 44`|
|`<FLAGS>`|1|1|Protocol flags:<br>`0x01` - Zabbix communications protocol<br>`0x02` - compression<br>`0x04` - large packet|
|`<DATALEN>`|4|8|Data length.|
|`<RESERVED>`|4|8|When compression is used (`0x02` flag) - the length of uncompressed data<br>When compression is not used - `00 00 00 00`|

[comment]: # ({/new-409c40cc})

[comment]: # ({8698de59-8698de59})
#### Implementation

Here are code snippets showing how to add Zabbix protocol header to the
`data` you *want* to send in order to obtain `packet` you *should* send
to Zabbix so it is interpreted correctly.

|Language|Code|
|--------|----|
|bash|`printf -v LENGTH '%016x' "${#DATA}"PACK=""for (( i=14; i>=0; i-=2 )); do PACK="$PACK\\x${LENGTH:$i:2}"; doneprintf "ZBXD\1$PACK%s" "$DATA"`{.bash}|
|Java|`byte[] header = new byte[] {'Z', 'B', 'X', 'D', '\1',(byte)(data.length & 0xFF),(byte)((data.length >> 8) & 0xFF),(byte)((data.length >> 16) & 0xFF),(byte)((data.length >> 24) & 0xFF),'\0', '\0', '\0', '\0'};|
|<|byte[] packet = new byte[header.length + data.length];System.arraycopy(header, 0, packet, 0, header.length);System.arraycopy(data, 0, packet, header.length, data.length);`{.Java}|
|PHP|`$packet = "ZBXD\1" . pack('P', strlen($data)) . $data;`{.PHP}or`$packet = "ZBXD\1" . pack('V', strlen($data)) . "\0\0\0\0" . $data;`{.PHP}|
|Perl|`my $packet = "ZBXD\1" . pack('<Q', length($data)) . $data;`{.Perl}or`my $packet = "ZBXD\1" . pack('V', length($data)) . "\0\0\0\0" . $data;`{.Perl}|
|Python|`packet = "ZBXD\1" + struct.pack('<Q', len(data)) + data`{.Python}|

[comment]: # ({/8698de59-8698de59})
