print-zeromq.c revision 251127
10SN/A/*
210346SN/A * This file implements decoding of ZeroMQ network protocol(s).
30SN/A *
40SN/A *
50SN/A * Copyright (c) 2013 The TCPDUMP project
60SN/A * All rights reserved.
72362SN/A *
80SN/A * Redistribution and use in source and binary forms, with or without
92362SN/A * modification, are permitted provided that the following conditions
100SN/A * are met:
110SN/A * 1. Redistributions of source code must retain the above copyright
120SN/A *    notice, this list of conditions and the following disclaimer.
130SN/A * 2. Redistributions in binary form must reproduce the above copyright
140SN/A *    notice, this list of conditions and the following disclaimer in the
150SN/A *    documentation and/or other materials provided with the distribution.
160SN/A *
170SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
180SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
190SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
200SN/A * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
212362SN/A * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
222362SN/A * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
232362SN/A * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
240SN/A * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
250SN/A * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
260SN/A * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
270SN/A * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
280SN/A * POSSIBILITY OF SUCH DAMAGE.
290SN/A */
300SN/A
310SN/A#ifdef HAVE_CONFIG_H
320SN/A#include "config.h"
330SN/A#endif
340SN/A
350SN/A#include <tcpdump-stdinc.h>
360SN/A
370SN/A#include <stdio.h>
380SN/A
390SN/A#include "interface.h"
400SN/A#include "extract.h"
410SN/A
420SN/A/* Maximum number of ZMTP/1.0 frame body bytes (without the flags) to dump in
430SN/A * hex and ASCII under a single "-v" flag.
440SN/A */
450SN/A#define VBYTES 128
460SN/A
470SN/A/*
480SN/A * Below is an excerpt from the "13/ZMTP" specification:
490SN/A *
500SN/A * A ZMTP message consists of 1 or more frames.
510SN/A *
520SN/A * A ZMTP frame consists of a length, followed by a flags field and a frame
530SN/A * body of (length - 1) octets. Note: the length includes the flags field, so
540SN/A * an empty frame has a length of 1.
550SN/A *
560SN/A * For frames with a length of 1 to 254 octets, the length SHOULD BE encoded
570SN/A * as a single octet. The minimum valid length of a frame is 1 octet, thus a
580SN/A * length of 0 is invalid and such frames SHOULD be discarded silently.
590SN/A *
600SN/A * For frames with lengths of 255 and greater, the length SHALL BE encoded as
610SN/A * a single octet with the value 255, followed by the length encoded as a
620SN/A * 64-bit unsigned integer in network byte order. For frames with lengths of
630SN/A * 1 to 254 octets this encoding MAY be also used.
640SN/A *
650SN/A * The flags field consists of a single octet containing various control
660SN/A * flags. Bit 0 is the least significant bit.
670SN/A *
680SN/A * - Bit 0 (MORE): More frames to follow. A value of 0 indicates that there
690SN/A *   are no more frames to follow. A value of 1 indicates that more frames
700SN/A *   will follow. On messages consisting of a single frame the MORE flag MUST
710SN/A *   be 0.
720SN/A *
7312195Savstepan * - Bits 1-7: Reserved. Bits 1-7 are reserved for future use and SHOULD be
740SN/A *   zero.
750SN/A */
760SN/A
770SN/Astatic const u_char *
780SN/Azmtp1_print_frame(const u_char *cp, const u_char *ep) {
790SN/A	u_int64_t body_len_declared, body_len_captured, header_len;
800SN/A	u_int8_t flags;
810SN/A
820SN/A	printf("\n\t");
830SN/A	TCHECK2(*cp, 1); /* length/0xFF */
840SN/A
850SN/A	if (cp[0] != 0xFF) {
860SN/A		header_len = 1; /* length */
870SN/A		body_len_declared = cp[0];
880SN/A		if (body_len_declared == 0)
890SN/A			return cp + header_len; /* skip to next frame */
900SN/A		printf(" frame flags+body  (8-bit) length %"PRIu8"", cp[0]);
910SN/A		TCHECK2(*cp, header_len + 1); /* length, flags */
920SN/A		flags = cp[1];
930SN/A	} else {
940SN/A		header_len = 1 + 8; /* 0xFF, length */
950SN/A		printf(" frame flags+body (64-bit) length");
960SN/A		TCHECK2(*cp, header_len); /* 0xFF, length */
970SN/A		body_len_declared = EXTRACT_64BITS(cp + 1);
980SN/A		if (body_len_declared == 0)
990SN/A			return cp + header_len; /* skip to next frame */
1000SN/A		printf(" %"PRIu64"", body_len_declared);
1010SN/A		TCHECK2(*cp, header_len + 1); /* 0xFF, length, flags */
1020SN/A		flags = cp[9];
1030SN/A	}
1040SN/A
1050SN/A	body_len_captured = ep - cp - header_len;
1060SN/A	if (body_len_declared > body_len_captured)
1070SN/A		printf(" (%"PRIu64" captured)", body_len_captured);
1080SN/A	printf(", flags 0x%02"PRIx8"", flags);
1090SN/A
1100SN/A	if (vflag) {
11110346SN/A		u_int64_t body_len_printed = MIN(body_len_captured, body_len_declared);
11210346SN/A
11310346SN/A		printf(" (%s|%s|%s|%s|%s|%s|%s|%s)",
1140SN/A			flags & 0x80 ? "MBZ" : "-",
1150SN/A			flags & 0x40 ? "MBZ" : "-",
1160SN/A			flags & 0x20 ? "MBZ" : "-",
1170SN/A			flags & 0x10 ? "MBZ" : "-",
1180SN/A			flags & 0x08 ? "MBZ" : "-",
1190SN/A			flags & 0x04 ? "MBZ" : "-",
1200SN/A			flags & 0x02 ? "MBZ" : "-",
1210SN/A			flags & 0x01 ? "MORE" : "-");
1220SN/A
1230SN/A		if (vflag == 1)
1240SN/A			body_len_printed = MIN(VBYTES + 1, body_len_printed);
1250SN/A		if (body_len_printed > 1) {
1260SN/A			printf(", first %"PRIu64" byte(s) of body:", body_len_printed - 1);
1270SN/A			hex_and_ascii_print("\n\t ", cp + header_len + 1, body_len_printed - 1);
1280SN/A			printf("\n");
1290SN/A		}
1300SN/A	}
1310SN/A
1320SN/A	TCHECK2(*cp, header_len + body_len_declared); /* Next frame within the buffer ? */
1330SN/A	return cp + header_len + body_len_declared;
1340SN/A
1350SN/Atrunc:
1360SN/A	printf(" [|zmtp1]");
1370SN/A	return ep;
1380SN/A}
1390SN/A
1400SN/Avoid
1410SN/Azmtp1_print(const u_char *cp, u_int len) {
1420SN/A	const u_char *ep = MIN(snapend, cp + len);
1430SN/A
1440SN/A	printf(": ZMTP/1.0");
1450SN/A	while (cp < ep)
1460SN/A		cp = zmtp1_print_frame(cp, ep);
1470SN/A}
1480SN/A
1490SN/A