xref: /dflybsd-src/contrib/tcpdump/print-zeromq.c (revision 59c07fbdf8168fa08c76c515186d561b5a92690c)
1411677aeSAaron LI /*
2411677aeSAaron LI  * Copyright (c) 2013 The TCPDUMP project
3411677aeSAaron LI  * All rights reserved.
4411677aeSAaron LI  *
5411677aeSAaron LI  * Redistribution and use in source and binary forms, with or without
6411677aeSAaron LI  * modification, are permitted provided that the following conditions
7411677aeSAaron LI  * are met:
8411677aeSAaron LI  * 1. Redistributions of source code must retain the above copyright
9411677aeSAaron LI  *    notice, this list of conditions and the following disclaimer.
10411677aeSAaron LI  * 2. Redistributions in binary form must reproduce the above copyright
11411677aeSAaron LI  *    notice, this list of conditions and the following disclaimer in the
12411677aeSAaron LI  *    documentation and/or other materials provided with the distribution.
13411677aeSAaron LI  *
14411677aeSAaron LI  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15411677aeSAaron LI  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16411677aeSAaron LI  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
17411677aeSAaron LI  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
18411677aeSAaron LI  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19411677aeSAaron LI  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20411677aeSAaron LI  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21411677aeSAaron LI  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22411677aeSAaron LI  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23411677aeSAaron LI  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24411677aeSAaron LI  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25411677aeSAaron LI  * POSSIBILITY OF SUCH DAMAGE.
26411677aeSAaron LI  */
27411677aeSAaron LI 
28411677aeSAaron LI /* \summary: ZeroMQ Message Transport Protocol (ZMTP) printer */
29411677aeSAaron LI 
30411677aeSAaron LI #ifdef HAVE_CONFIG_H
31*ed775ee7SAntonio Huete Jimenez #include <config.h>
32411677aeSAaron LI #endif
33411677aeSAaron LI 
34*ed775ee7SAntonio Huete Jimenez #include "netdissect-stdinc.h"
35411677aeSAaron LI 
36411677aeSAaron LI #include "netdissect.h"
37411677aeSAaron LI #include "extract.h"
38411677aeSAaron LI 
39411677aeSAaron LI 
40411677aeSAaron LI /* Maximum number of ZMTP/1.0 frame body bytes (without the flags) to dump in
41411677aeSAaron LI  * hex and ASCII under a single "-v" flag.
42411677aeSAaron LI  */
43411677aeSAaron LI #define VBYTES 128
44411677aeSAaron LI 
45411677aeSAaron LI /*
46411677aeSAaron LI  * Below is an excerpt from the "13/ZMTP" specification:
47411677aeSAaron LI  *
48411677aeSAaron LI  * A ZMTP message consists of 1 or more frames.
49411677aeSAaron LI  *
50411677aeSAaron LI  * A ZMTP frame consists of a length, followed by a flags field and a frame
51411677aeSAaron LI  * body of (length - 1) octets. Note: the length includes the flags field, so
52411677aeSAaron LI  * an empty frame has a length of 1.
53411677aeSAaron LI  *
54411677aeSAaron LI  * For frames with a length of 1 to 254 octets, the length SHOULD BE encoded
55411677aeSAaron LI  * as a single octet. The minimum valid length of a frame is 1 octet, thus a
56411677aeSAaron LI  * length of 0 is invalid and such frames SHOULD be discarded silently.
57411677aeSAaron LI  *
58411677aeSAaron LI  * For frames with lengths of 255 and greater, the length SHALL BE encoded as
59411677aeSAaron LI  * a single octet with the value 255, followed by the length encoded as a
60411677aeSAaron LI  * 64-bit unsigned integer in network byte order. For frames with lengths of
61411677aeSAaron LI  * 1 to 254 octets this encoding MAY be also used.
62411677aeSAaron LI  *
63411677aeSAaron LI  * The flags field consists of a single octet containing various control
64411677aeSAaron LI  * flags. Bit 0 is the least significant bit.
65411677aeSAaron LI  *
66411677aeSAaron LI  * - Bit 0 (MORE): More frames to follow. A value of 0 indicates that there
67411677aeSAaron LI  *   are no more frames to follow. A value of 1 indicates that more frames
68411677aeSAaron LI  *   will follow. On messages consisting of a single frame the MORE flag MUST
69411677aeSAaron LI  *   be 0.
70411677aeSAaron LI  *
71411677aeSAaron LI  * - Bits 1-7: Reserved. Bits 1-7 are reserved for future use and SHOULD be
72411677aeSAaron LI  *   zero.
73411677aeSAaron LI  */
74411677aeSAaron LI 
75411677aeSAaron LI static const u_char *
zmtp1_print_frame(netdissect_options * ndo,const u_char * cp,const u_char * ep)76411677aeSAaron LI zmtp1_print_frame(netdissect_options *ndo, const u_char *cp, const u_char *ep)
77411677aeSAaron LI {
78411677aeSAaron LI 	uint64_t body_len_declared, body_len_captured, header_len;
79411677aeSAaron LI 	uint8_t flags;
80411677aeSAaron LI 
81*ed775ee7SAntonio Huete Jimenez 	ND_PRINT("\n\t");
82411677aeSAaron LI 
83*ed775ee7SAntonio Huete Jimenez 	if (GET_U_1(cp) != 0xFF) {	/* length/0xFF */
84411677aeSAaron LI 		header_len = 1; /* length */
85*ed775ee7SAntonio Huete Jimenez 		body_len_declared = GET_U_1(cp);
86*ed775ee7SAntonio Huete Jimenez 		ND_PRINT(" frame flags+body  (8-bit) length %" PRIu64, body_len_declared);
87411677aeSAaron LI 	} else {
88411677aeSAaron LI 		header_len = 1 + 8; /* 0xFF, length */
89*ed775ee7SAntonio Huete Jimenez 		ND_PRINT(" frame flags+body (64-bit) length");
90*ed775ee7SAntonio Huete Jimenez 		ND_TCHECK_LEN(cp, header_len); /* 0xFF, length */
91*ed775ee7SAntonio Huete Jimenez 		body_len_declared = GET_BE_U_8(cp + 1);
92*ed775ee7SAntonio Huete Jimenez 		ND_PRINT(" %" PRIu64, body_len_declared);
93411677aeSAaron LI 	}
94411677aeSAaron LI 	if (body_len_declared == 0)
95411677aeSAaron LI 		return cp + header_len; /* skip to the next frame */
96*ed775ee7SAntonio Huete Jimenez 	ND_TCHECK_LEN(cp, header_len + 1); /* ..., flags */
97*ed775ee7SAntonio Huete Jimenez 	flags = GET_U_1(cp + header_len);
98411677aeSAaron LI 
99411677aeSAaron LI 	body_len_captured = ep - cp - header_len;
100411677aeSAaron LI 	if (body_len_declared > body_len_captured)
101*ed775ee7SAntonio Huete Jimenez 		ND_PRINT(" (%" PRIu64 " captured)", body_len_captured);
102*ed775ee7SAntonio Huete Jimenez 	ND_PRINT(", flags 0x%02x", flags);
103411677aeSAaron LI 
104411677aeSAaron LI 	if (ndo->ndo_vflag) {
105*ed775ee7SAntonio Huete Jimenez 		uint64_t body_len_printed = ND_MIN(body_len_captured, body_len_declared);
106411677aeSAaron LI 
107*ed775ee7SAntonio Huete Jimenez 		ND_PRINT(" (%s|%s|%s|%s|%s|%s|%s|%s)",
108411677aeSAaron LI 			flags & 0x80 ? "MBZ" : "-",
109411677aeSAaron LI 			flags & 0x40 ? "MBZ" : "-",
110411677aeSAaron LI 			flags & 0x20 ? "MBZ" : "-",
111411677aeSAaron LI 			flags & 0x10 ? "MBZ" : "-",
112411677aeSAaron LI 			flags & 0x08 ? "MBZ" : "-",
113411677aeSAaron LI 			flags & 0x04 ? "MBZ" : "-",
114411677aeSAaron LI 			flags & 0x02 ? "MBZ" : "-",
115*ed775ee7SAntonio Huete Jimenez 			flags & 0x01 ? "MORE" : "-");
116411677aeSAaron LI 
117411677aeSAaron LI 		if (ndo->ndo_vflag == 1)
118*ed775ee7SAntonio Huete Jimenez 			body_len_printed = ND_MIN(VBYTES + 1, body_len_printed);
119411677aeSAaron LI 		if (body_len_printed > 1) {
120*ed775ee7SAntonio Huete Jimenez 			ND_PRINT(", first %" PRIu64 " byte(s) of body:", body_len_printed - 1);
121411677aeSAaron LI 			hex_and_ascii_print(ndo, "\n\t ", cp + header_len + 1, body_len_printed - 1);
122411677aeSAaron LI 		}
123411677aeSAaron LI 	}
124411677aeSAaron LI 
125411677aeSAaron LI 	/*
126411677aeSAaron LI 	 * Do not advance cp by the sum of header_len and body_len_declared
127*ed775ee7SAntonio Huete Jimenez 	 * before each offset has successfully passed ND_TCHECK_LEN() as the
128411677aeSAaron LI 	 * sum can roll over (9 + 0xfffffffffffffff7 = 0) and cause an
129411677aeSAaron LI 	 * infinite loop.
130411677aeSAaron LI 	 */
131411677aeSAaron LI 	cp += header_len;
132*ed775ee7SAntonio Huete Jimenez 	ND_TCHECK_LEN(cp, body_len_declared); /* Next frame within the buffer ? */
133411677aeSAaron LI 	return cp + body_len_declared;
134411677aeSAaron LI 
135411677aeSAaron LI trunc:
136*ed775ee7SAntonio Huete Jimenez 	nd_trunc_longjmp(ndo);
137411677aeSAaron LI }
138411677aeSAaron LI 
139411677aeSAaron LI void
zmtp1_print(netdissect_options * ndo,const u_char * cp,u_int len)140411677aeSAaron LI zmtp1_print(netdissect_options *ndo, const u_char *cp, u_int len)
141411677aeSAaron LI {
142*ed775ee7SAntonio Huete Jimenez 	const u_char *ep = ND_MIN(ndo->ndo_snapend, cp + len);
143411677aeSAaron LI 
144*ed775ee7SAntonio Huete Jimenez 	ndo->ndo_protocol = "zmtp1";
145*ed775ee7SAntonio Huete Jimenez 	ND_PRINT(": ZMTP/1.0");
146411677aeSAaron LI 	while (cp < ep)
147411677aeSAaron LI 		cp = zmtp1_print_frame(ndo, cp, ep);
148411677aeSAaron LI }
149411677aeSAaron LI 
150411677aeSAaron LI /* The functions below decode a ZeroMQ datagram, supposedly stored in the "Data"
151411677aeSAaron LI  * field of an ODATA/RDATA [E]PGM packet. An excerpt from zmq_pgm(7) man page
152411677aeSAaron LI  * follows.
153411677aeSAaron LI  *
154411677aeSAaron LI  * In order for late joining consumers to be able to identify message
155411677aeSAaron LI  * boundaries, each PGM datagram payload starts with a 16-bit unsigned integer
156411677aeSAaron LI  * in network byte order specifying either the offset of the first message frame
157411677aeSAaron LI  * in the datagram or containing the value 0xFFFF if the datagram contains
158411677aeSAaron LI  * solely an intermediate part of a larger message.
159411677aeSAaron LI  *
160411677aeSAaron LI  * Note that offset specifies where the first message begins rather than the
161411677aeSAaron LI  * first message part. Thus, if there are trailing message parts at the
162411677aeSAaron LI  * beginning of the packet the offset ignores them and points to first initial
163411677aeSAaron LI  * message part in the packet.
164411677aeSAaron LI  */
165411677aeSAaron LI 
166411677aeSAaron LI static const u_char *
zmtp1_print_intermediate_part(netdissect_options * ndo,const u_char * cp,const u_int len)167411677aeSAaron LI zmtp1_print_intermediate_part(netdissect_options *ndo, const u_char *cp, const u_int len)
168411677aeSAaron LI {
169411677aeSAaron LI 	u_int frame_offset;
170*ed775ee7SAntonio Huete Jimenez 	u_int remaining_len;
171411677aeSAaron LI 
172*ed775ee7SAntonio Huete Jimenez 	frame_offset = GET_BE_U_2(cp);
173*ed775ee7SAntonio Huete Jimenez 	ND_PRINT("\n\t frame offset 0x%04x", frame_offset);
174411677aeSAaron LI 	cp += 2;
175*ed775ee7SAntonio Huete Jimenez 	remaining_len = ND_BYTES_AVAILABLE_AFTER(cp); /* without the frame length */
176411677aeSAaron LI 
177411677aeSAaron LI 	if (frame_offset == 0xFFFF)
178411677aeSAaron LI 		frame_offset = len - 2; /* always within the declared length */
179411677aeSAaron LI 	else if (2 + frame_offset > len) {
180*ed775ee7SAntonio Huete Jimenez 		ND_PRINT(" (exceeds datagram declared length)");
181411677aeSAaron LI 		goto trunc;
182411677aeSAaron LI 	}
183411677aeSAaron LI 
184411677aeSAaron LI 	/* offset within declared length of the datagram */
185411677aeSAaron LI 	if (frame_offset) {
186*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("\n\t frame intermediate part, %u bytes", frame_offset);
187411677aeSAaron LI 		if (frame_offset > remaining_len)
188*ed775ee7SAntonio Huete Jimenez 			ND_PRINT(" (%u captured)", remaining_len);
189411677aeSAaron LI 		if (ndo->ndo_vflag) {
190*ed775ee7SAntonio Huete Jimenez 			u_int len_printed = ND_MIN(frame_offset, remaining_len);
191411677aeSAaron LI 
192411677aeSAaron LI 			if (ndo->ndo_vflag == 1)
193*ed775ee7SAntonio Huete Jimenez 				len_printed = ND_MIN(VBYTES, len_printed);
194411677aeSAaron LI 			if (len_printed > 1) {
195*ed775ee7SAntonio Huete Jimenez 				ND_PRINT(", first %u byte(s):", len_printed);
196411677aeSAaron LI 				hex_and_ascii_print(ndo, "\n\t ", cp, len_printed);
197411677aeSAaron LI 			}
198411677aeSAaron LI 		}
199411677aeSAaron LI 	}
200411677aeSAaron LI 	return cp + frame_offset;
201411677aeSAaron LI 
202411677aeSAaron LI trunc:
203*ed775ee7SAntonio Huete Jimenez 	nd_trunc_longjmp(ndo);
204411677aeSAaron LI }
205411677aeSAaron LI 
206411677aeSAaron LI void
zmtp1_datagram_print(netdissect_options * ndo,const u_char * cp,const u_int len)207*ed775ee7SAntonio Huete Jimenez zmtp1_datagram_print(netdissect_options *ndo, const u_char *cp, const u_int len)
208411677aeSAaron LI {
209*ed775ee7SAntonio Huete Jimenez 	const u_char *ep = ND_MIN(ndo->ndo_snapend, cp + len);
210411677aeSAaron LI 
211*ed775ee7SAntonio Huete Jimenez 	ndo->ndo_protocol = "zmtp1";
212411677aeSAaron LI 	cp = zmtp1_print_intermediate_part(ndo, cp, len);
213411677aeSAaron LI 	while (cp < ep)
214411677aeSAaron LI 		cp = zmtp1_print_frame(ndo, cp, ep);
215411677aeSAaron LI }
216