1 /* dpa400.c
2  *
3  * Unigraf DisplayPort AUX channel monitor output parser
4  * Copyright 2018, Dirk Eibach, Guntermann & Drunck GmbH <dirk.eibach@gdsys.cc>
5  *
6  * SPDX-License-Identifier: GPL-2.0-or-later
7  */
8 
9 #include "config.h"
10 
11 #include <string.h>
12 
13 #include "wtap-int.h"
14 #include "file_wrappers.h"
15 #include "dpa400.h"
16 
17 enum {
18 	DPA400_DATA = 0x00,
19 	DPA400_DATA_END = 0x01,
20 	DPA400_EVENT = 0x02,
21 	DPA400_START = 0x03,
22 	DPA400_STOP = 0x04,
23 	DPA400_TS_OVERFLOW = 0x84,
24 };
25 
26 struct dpa400_header {
27 	guint8 t0;
28 	guint8 sb0;
29 	guint8 t1;
30 	guint8 sb1;
31 	guint8 t2;
32 	guint8 sb2;
33 };
34 
35 static int dpa400_file_type_subtype = -1;
36 
37 void register_dpa400(void);
38 
dpa400_read_header(FILE_T fh,struct dpa400_header * hdr,int * err,gchar ** err_info)39 static gboolean dpa400_read_header(FILE_T fh, struct dpa400_header *hdr, int *err, gchar **err_info)
40 {
41 	if (!wtap_read_bytes_or_eof(fh, hdr, sizeof(struct dpa400_header), err, err_info))
42 		return FALSE;
43 
44 	if (hdr->sb0 || hdr->sb1 || hdr->sb2) {
45 		*err = WTAP_ERR_BAD_FILE;
46 		*err_info = g_strdup("dpa400: malformed packet header");
47 		return FALSE;
48 	}
49 
50 	return TRUE;
51 }
52 
get_ts(struct dpa400_header * hdr,nstime_t * ts)53 static void get_ts(struct dpa400_header *hdr, nstime_t *ts)
54 {
55 	guint32 val;
56 
57 	val = (hdr->t0 | (hdr->t1 << 8) | ((hdr->t2 & 0x7f) << 16)) << 5;
58 
59 	ts->secs = val / 1000000;
60 	ts->nsecs = (val % 1000000) * 1000;
61 }
62 
get_ts_overflow(nstime_t * ts)63 static void get_ts_overflow(nstime_t *ts)
64 {
65 	guint32 val = 0x7fffff << 5;
66 
67 	ts->secs = val / 1000000;
68 	ts->nsecs = (val % 1000000) * 1000;
69 }
70 
get_from(struct dpa400_header * hdr)71 static guint8 get_from(struct dpa400_header *hdr)
72 {
73 	return hdr->t2 & 0x80;
74 }
75 
dpa400_read_packet(wtap * wth,FILE_T fh,wtap_rec * rec,Buffer * buf,int * err,gchar ** err_info)76 static gboolean dpa400_read_packet(wtap *wth, FILE_T fh, wtap_rec *rec,
77     Buffer *buf, int *err, gchar **err_info)
78 {
79 	guint8 chunk[2];
80 	guint32 ctr = 0;
81 
82 	if (!wth || !rec || !buf)
83 		return FALSE;
84 
85 	if (!wtap_read_bytes_or_eof(fh, chunk, sizeof(chunk), err, err_info))
86 		return FALSE;
87 
88 	if (chunk[1] != 1) {
89 		*err = WTAP_ERR_BAD_FILE;
90 		*err_info = g_strdup("dpa400: malformed packet framing");
91 		return FALSE;
92 	}
93 
94 	ws_buffer_clean(buf);
95 
96 	ws_buffer_append(buf, &chunk[0], 1);
97 	ctr++;
98 
99 	switch (chunk[0]) {
100 	case DPA400_STOP: {
101 		struct dpa400_header hdr;
102 
103 		if (!dpa400_read_header(fh, &hdr, err, err_info))
104 			return FALSE;
105 
106 		get_ts(&hdr, &rec->ts);
107 
108 		rec->rec_type = REC_TYPE_PACKET;
109 		rec->block = wtap_block_create(WTAP_BLOCK_PACKET);
110 		rec->presence_flags = WTAP_HAS_TS;
111 		rec->rec_header.packet_header.caplen = rec->rec_header.packet_header.len = 0;
112 
113 		break;
114 	}
115 
116 	case DPA400_START:
117 	case DPA400_EVENT: {
118 		struct dpa400_header hdr;
119 
120 		if (!dpa400_read_header(fh, &hdr, err, err_info))
121 			return FALSE;
122 
123 		get_ts(&hdr, &rec->ts);
124 
125 		if (!wtap_read_bytes_or_eof(fh, chunk, sizeof(chunk), err, err_info))
126 			return FALSE;
127 
128 		if (chunk[1]) {
129 			*err = WTAP_ERR_BAD_FILE;
130 			*err_info = g_strdup("dpa400: malformed packet");
131 			return FALSE;
132 		}
133 
134 		ws_buffer_append(buf, &chunk[0], 1);
135 		ctr++;
136 
137 		rec->rec_type = REC_TYPE_PACKET;
138 		rec->block = wtap_block_create(WTAP_BLOCK_PACKET);
139 		rec->presence_flags = WTAP_HAS_TS;
140 		rec->rec_header.packet_header.caplen = rec->rec_header.packet_header.len = ctr;
141 
142 		break;
143 	}
144 
145 	case DPA400_DATA: {
146 		struct dpa400_header hdr;
147 		guint8 from_source;
148 
149 		if (!dpa400_read_header(fh, &hdr, err, err_info))
150 			return FALSE;
151 
152 		get_ts(&hdr, &rec->ts);
153 
154 		from_source = !get_from(&hdr);
155 		ws_buffer_append(buf, &from_source, 1);
156 		ctr++;
157 
158 		while (1) {
159 			if (!wtap_read_bytes_or_eof(fh, chunk, sizeof(chunk), err, err_info))
160 				return FALSE;
161 
162 			if (chunk[1])
163 				break;
164 
165 			if (++ctr > WTAP_MAX_PACKET_SIZE_STANDARD) {
166 				*err = WTAP_ERR_BAD_FILE;
167 				*err_info = g_strdup_printf("dpa400: File has data record bigger than maximum of %u",
168 					WTAP_MAX_PACKET_SIZE_STANDARD);
169 				return FALSE;
170 			}
171 
172 			ws_buffer_append(buf, &chunk[0], 1);
173 		}
174 
175 		rec->rec_type = REC_TYPE_PACKET;
176 		rec->block = wtap_block_create(WTAP_BLOCK_PACKET);
177 		rec->presence_flags = WTAP_HAS_TS;
178 		rec->rec_header.packet_header.caplen = rec->rec_header.packet_header.len = ctr;
179 
180 		break;
181 	}
182 
183 	case DPA400_TS_OVERFLOW: {
184 		get_ts_overflow(&rec->ts);
185 
186 		rec->rec_type = REC_TYPE_PACKET;
187 		rec->block = wtap_block_create(WTAP_BLOCK_PACKET);
188 		rec->presence_flags = WTAP_HAS_TS;
189 		rec->rec_header.packet_header.caplen = rec->rec_header.packet_header.len = ctr;
190 
191 		break;
192 	}
193 
194 	default:
195 		*err = WTAP_ERR_BAD_FILE;
196 		*err_info = g_strdup_printf("dpa400: unknown packet type %02x", chunk[0]);
197 		return FALSE;
198 	}
199 
200 	return TRUE;
201 }
202 
dpa400_seek_read(wtap * wth,gint64 seek_off,wtap_rec * rec,Buffer * buf,int * err,gchar ** err_info)203 static gboolean dpa400_seek_read(wtap *wth,gint64 seek_off, wtap_rec *rec, Buffer *buf,
204     int *err, gchar **err_info)
205 {
206 	if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
207 		return FALSE;
208 
209 	return dpa400_read_packet(wth, wth->random_fh, rec, buf, err, err_info);
210 }
211 
dpa400_read(wtap * wth,wtap_rec * rec,Buffer * buf,int * err,gchar ** err_info,gint64 * data_offset)212 static gboolean dpa400_read(wtap *wth, wtap_rec *rec, Buffer *buf,
213     int *err, gchar **err_info, gint64 *data_offset)
214 {
215 	*data_offset = file_tell(wth->fh);
216 
217 	return dpa400_read_packet(wth, wth->fh, rec, buf, err, err_info);
218 }
219 
dpa400_open(wtap * wth,int * err,gchar ** err_info)220 wtap_open_return_val dpa400_open(wtap *wth, int *err, gchar **err_info)
221 {
222 	char magic[4];
223 	const char dpa_magic[] = { 'D', 'B', 'F', 'R' };
224 
225 	/* Read in the number that should be at the start of a "dpa-400" file */
226 	if (!wtap_read_bytes(wth->fh, &magic, sizeof magic, err, err_info)) {
227 		if (*err != WTAP_ERR_SHORT_READ)
228 			return WTAP_OPEN_ERROR;
229 		return WTAP_OPEN_NOT_MINE;
230 	}
231 
232 	if (memcmp(magic, dpa_magic, sizeof(dpa_magic)))
233 		return WTAP_OPEN_NOT_MINE;
234 
235 	wth->file_type_subtype = dpa400_file_type_subtype;
236 	wth->file_encap = WTAP_ENCAP_DPAUXMON;
237 	wth->file_tsprec = WTAP_TSPREC_USEC;
238 	wth->subtype_read = dpa400_read;
239 	wth->subtype_seek_read = dpa400_seek_read;
240 	wth->snapshot_length = 0;
241 
242 	/*
243 	 * Add an IDB; we don't know how many interfaces were
244 	 * involved, so we just say one interface, about which
245 	 * we only know the link-layer type, snapshot length,
246 	 * and time stamp resolution.
247 	 */
248 	wtap_add_generated_idb(wth);
249 
250 	return WTAP_OPEN_MINE;
251 }
252 
253 static const struct supported_block_type dpa400_blocks_supported[] = {
254 	/*
255 	 * We support packet blocks, with no comments or other options.
256 	 */
257 	{ WTAP_BLOCK_PACKET, MULTIPLE_BLOCKS_SUPPORTED, NO_OPTIONS_SUPPORTED }
258 };
259 
260 static const struct file_type_subtype_info dpa400_info = {
261 	"Unigraf DPA-400 capture", "dpa400", "bin", NULL,
262 	FALSE, BLOCKS_SUPPORTED(dpa400_blocks_supported),
263 	NULL, NULL, NULL
264 };
265 
register_dpa400(void)266 void register_dpa400(void)
267 {
268 	dpa400_file_type_subtype = wtap_register_file_type_subtype(&dpa400_info);
269 
270 	/*
271 	 * Register name for backwards compatibility with the
272 	 * wtap_filetypes table in Lua.
273 	 */
274 	wtap_register_backwards_compatibility_lua_name("DPA400",
275 	    dpa400_file_type_subtype);
276 }
277 
278 /*
279  * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
280  *
281  * Local variables:
282  * c-basic-offset: 8
283  * tab-width: 8
284  * indent-tabs-mode: t
285  * End:
286  *
287  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
288  * :indentSize=8:tabSize=8:noTabs=false:
289  */
290