aboutsummaryrefslogtreecommitdiffstats
path: root/test/packetdrill/packet_to_string.c
blob: 1fd90b29393ee5570586d5884c9959b617825de2 (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
/*
 * Copyright 2013 Google Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */
/*
 * Author: ncardwell@google.com (Neal Cardwell)
 *
 * Implementation for generating human-readable representations of IP
 * packets.
 */

#include "packet_to_string.h"

#include <stdlib.h>
#include "socket.h"
#include "tcp_options_to_string.h"

static void endpoints_to_string(FILE *s, const struct packet *packet)
{
	char src_string[ADDR_STR_LEN];
	char dst_string[ADDR_STR_LEN];
	struct tuple tuple;

	get_packet_tuple(packet, &tuple);

	fprintf(s, "%s:%u > %s:%u",
		ip_to_string(&tuple.src.ip, src_string), ntohs(tuple.src.port),
		ip_to_string(&tuple.dst.ip, dst_string), ntohs(tuple.dst.port));
}

static void packet_buffer_to_string(FILE *s, struct packet *packet)
{
	char *hex = NULL;
	hex_dump(packet->buffer, packet_end(packet) - packet->buffer, &hex);
	fputc('\n', s);
	fprintf(s, "%s", hex);
	free(hex);
}

static int ipv4_header_to_string(FILE *s, struct packet *packet, int layer,
				 enum dump_format_t format, char **error)
{
	char src_string[ADDR_STR_LEN];
	char dst_string[ADDR_STR_LEN];
	struct ip_address src_ip, dst_ip;
	const struct ipv4 *ipv4 = packet->headers[layer].h.ipv4;

	ip_from_ipv4(&ipv4->src_ip, &src_ip);
	ip_from_ipv4(&ipv4->dst_ip, &dst_ip);

	fprintf(s, "ipv4 %s > %s: ",
		ip_to_string(&src_ip, src_string),
		ip_to_string(&dst_ip, dst_string));

	return STATUS_OK;
}

static int ipv6_header_to_string(FILE *s, struct packet *packet, int layer,
				 enum dump_format_t format, char **error)
{
	char src_string[ADDR_STR_LEN];
	char dst_string[ADDR_STR_LEN];
	struct ip_address src_ip, dst_ip;
	const struct ipv6 *ipv6 = packet->headers[layer].h.ipv6;

	ip_from_ipv6(&ipv6->src_ip, &src_ip);
	ip_from_ipv6(&ipv6->dst_ip, &dst_ip);

	fprintf(s, "ipv6 %s > %s: ",
		ip_to_string(&src_ip, src_string),
		ip_to_string(&dst_ip, dst_string));

	return STATUS_OK;
}

static int gre_header_to_string(FILE *s, struct packet *packet, int layer,
				enum dump_format_t format, char **error)
{
	const struct gre *gre = packet->headers[layer].h.gre;
	int i = 0;

	fprintf(s, "gre flags 0x%x proto 0x%04x",
		ntohs(gre->flags),
		ntohs(gre->proto));

	if (gre->has_checksum || gre->has_routing) {
		fprintf(s, " sum 0x%x off 0x%x",
			ntohs(gre->be16[0]),
			ntohs(gre->be16[1]));
		i++;
	}

	if (gre->has_key) {
		fprintf(s, " key 0x%x", ntohl(gre->be32[i]));
		i++;
	}

	if (gre->has_seq) {
		fprintf(s, " seq 0x%x", ntohl(gre->be32[i]));
		i++;
	}

	fprintf(s, ": ");
	return STATUS_OK;
}

static int mpls_header_to_string(FILE *s, struct packet *packet, int layer,
				 enum dump_format_t format, char **error)
{
	struct header *header = &packet->headers[layer];
	int num_entries = header->header_bytes / sizeof(struct mpls);
	int i = 0;

	fprintf(s, "mpls");

	for (i = 0; i < num_entries; ++i) {
		const struct mpls *mpls = header->h.mpls + i;

		fprintf(s, " (label %u, tc %u,%s ttl %u)",
			mpls_entry_label(mpls),
			mpls_entry_tc(mpls),
			mpls_entry_stack(mpls) ? " [S]," : "",
			mpls_entry_ttl(mpls));
	}

	fprintf(s, ": ");
	return STATUS_OK;
}

/* Print a string representation of the TCP packet:
 *  direction opt_ip_info flags seq ack window tcp_options
 */
static int tcp_packet_to_string(FILE *s, struct packet *packet,
				enum dump_format_t format, char **error)
{
	int result = STATUS_OK;       /* return value */

	if ((format == DUMP_FULL) || (format == DUMP_VERBOSE)) {
		endpoints_to_string(s, packet);
		fputc(' ', s);
	}


	/* We print flags in the same order as tcpdump 4.1.1. */
	if (packet->tcp->fin)
		fputc('F', s);
	if (packet->tcp->syn)
		fputc('S', s);
	if (packet->tcp->rst)
		fputc('R', s);
	if (packet->tcp->psh)
		fputc('P', s);
	if (packet->tcp->ack)
		fputc('.', s);
	if (packet->tcp->urg)
		fputc('U', s);
	if (packet->tcp->ece)
		fputc('E', s);   /* ECN *E*cho sent (ECN) */
	if (packet->tcp->cwr)
		fputc('W', s);   /* Congestion *W*indow reduced (ECN) */

	fprintf(s, " %u:%u(%u) ",
		ntohl(packet->tcp->seq),
		ntohl(packet->tcp->seq) + packet_payload_len(packet),
		packet_payload_len(packet));

	if (packet->tcp->ack)
		fprintf(s, "ack %u ", ntohl(packet->tcp->ack_seq));

	if (!(packet->flags & FLAG_WIN_NOCHECK))
		fprintf(s, "win %u ", ntohs(packet->tcp->window));

	if (packet_tcp_options_len(packet) > 0) {
		char *tcp_options = NULL;
		if (tcp_options_to_string(packet, &tcp_options, error))
			result = STATUS_ERR;
		else
			fprintf(s, "<%s>", tcp_options);
		free(tcp_options);
	}

	if (format == DUMP_VERBOSE)
		packet_buffer_to_string(s, packet);

	return result;
}

static int udp_packet_to_string(FILE *s, struct packet *packet,
				enum dump_format_t format, char **error)
{
	int result = STATUS_OK;       /* return value */

	if ((format == DUMP_FULL) || (format == DUMP_VERBOSE)) {
		endpoints_to_string(s, packet);
		fputc(' ', s);
	}

	fprintf(s, "udp (%u)", packet_payload_len(packet));

	if (format == DUMP_VERBOSE)
		packet_buffer_to_string(s, packet);

	return result;
}

static int icmpv4_packet_to_string(FILE *s, struct packet *packet,
				   enum dump_format_t format, char **error)
{
	fprintf(s, "icmpv4");
	/* TODO(ncardwell): print type, code; use tables from icmp_packet.c */
	return STATUS_OK;
}

static int icmpv6_packet_to_string(FILE *s, struct packet *packet,
				   enum dump_format_t format, char **error)
{
	fprintf(s, "icmpv6");
	/* TODO(ncardwell): print type, code; use tables from icmp_packet.c */
	return STATUS_OK;
}

typedef int (*header_to_string_func)(FILE *s, struct packet *packet, int layer,
				     enum dump_format_t format, char **error);

static int encap_header_to_string(FILE *s, struct packet *packet, int layer,
				  enum dump_format_t format, char **error)
{
	header_to_string_func printers[HEADER_NUM_TYPES] = {
		[HEADER_IPV4]	= ipv4_header_to_string,
		[HEADER_IPV6]	= ipv6_header_to_string,
		[HEADER_GRE]	= gre_header_to_string,
		[HEADER_MPLS]	= mpls_header_to_string,
	};
	header_to_string_func printer = NULL;
	enum header_t type = packet->headers[layer].type;

	assert(type > HEADER_NONE);
	assert(type < HEADER_NUM_TYPES);
	printer = printers[type];
	assert(printer != NULL);
	return printer(s, packet, layer, format, error);
}


int packet_to_string(struct packet *packet,
		     enum dump_format_t format,
		     char **ascii_string, char **error)
{
	assert(packet != NULL);
	int result = STATUS_ERR;       /* return value */
	size_t size = 0;
	FILE *s = open_memstream(ascii_string, &size);  /* output string */
	int i;
	int header_count = packet_header_count(packet);

	/* Print any encapsulation headers preceding layer 3 and 4 headers. */
	for (i = 0; i < header_count - 2; ++i) {
		if (packet->headers[i].type == HEADER_NONE)
			break;
		if (encap_header_to_string(s, packet, i, format, error))
			goto out;
	}

	if ((packet->ipv4 == NULL) && (packet->ipv6 == NULL)) {
		fprintf(s, "[NO IP HEADER]");
	} else {
		if (packet->tcp != NULL) {
			if (tcp_packet_to_string(s, packet, format, error))
				goto out;
		} else if (packet->udp != NULL) {
			if (udp_packet_to_string(s, packet, format, error))
				goto out;
		} else if (packet->icmpv4 != NULL) {
			if (icmpv4_packet_to_string(s, packet, format, error))
				goto out;
		} else if (packet->icmpv6 != NULL) {
			if (icmpv6_packet_to_string(s, packet, format, error))
				goto out;
		} else {
			fprintf(s, "[NO TCP OR ICMP HEADER]");
		}
	}

	result = STATUS_OK;

out:
	fclose(s);
	return result;
}