/src/openssl/ssl/quic/qlog.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2023-2026 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | #include <stdbool.h> |
11 | | #include <stdio.h> |
12 | | #include "internal/qlog.h" |
13 | | #include "internal/json_enc.h" |
14 | | #include "internal/common.h" |
15 | | #include "internal/cryptlib.h" |
16 | | #include "internal/to_hex.h" |
17 | | #include "crypto/ctype.h" |
18 | | |
19 | 0 | #define BITS_PER_WORD (sizeof(size_t) * 8) |
20 | | #define NUM_ENABLED_W ((QLOG_EVENT_TYPE_NUM + BITS_PER_WORD - 1) / BITS_PER_WORD) |
21 | | |
22 | | static ossl_unused ossl_inline int bit_get(const size_t *p, uint32_t bit_no) |
23 | 0 | { |
24 | 0 | return p[bit_no / BITS_PER_WORD] & (((size_t)1) << (bit_no % BITS_PER_WORD)); |
25 | 0 | } |
26 | | |
27 | | static ossl_unused ossl_inline void bit_set(size_t *p, uint32_t bit_no, int enable) |
28 | 0 | { |
29 | 0 | size_t mask = (((size_t)1) << (bit_no % BITS_PER_WORD)); |
30 | |
|
31 | 0 | if (enable) |
32 | 0 | p[bit_no / BITS_PER_WORD] |= mask; |
33 | 0 | else |
34 | 0 | p[bit_no / BITS_PER_WORD] &= ~mask; |
35 | 0 | } |
36 | | |
37 | | struct qlog_st { |
38 | | QLOG_TRACE_INFO info; |
39 | | |
40 | | BIO *bio; |
41 | | size_t enabled[NUM_ENABLED_W]; |
42 | | uint32_t event_type; |
43 | | const char *event_cat, *event_name, *event_combined_name; |
44 | | OSSL_TIME event_time, prev_event_time; |
45 | | OSSL_JSON_ENC json; |
46 | | int header_done, first_event_done; |
47 | | }; |
48 | | |
49 | | static OSSL_TIME default_now(void *arg) |
50 | 0 | { |
51 | 0 | return ossl_time_now(); |
52 | 0 | } |
53 | | |
54 | | /* |
55 | | * Construction |
56 | | * ============ |
57 | | */ |
58 | | QLOG *ossl_qlog_new(const QLOG_TRACE_INFO *info) |
59 | 0 | { |
60 | 0 | QLOG *qlog = OPENSSL_zalloc(sizeof(QLOG)); |
61 | |
|
62 | 0 | if (qlog == NULL) |
63 | 0 | return NULL; |
64 | | |
65 | 0 | qlog->info.odcid = info->odcid; |
66 | 0 | qlog->info.is_server = info->is_server; |
67 | 0 | qlog->info.now_cb = info->now_cb; |
68 | 0 | qlog->info.now_cb_arg = info->now_cb_arg; |
69 | 0 | qlog->info.override_process_id = info->override_process_id; |
70 | |
|
71 | 0 | if (info->title != NULL |
72 | 0 | && (qlog->info.title = OPENSSL_strdup(info->title)) == NULL) |
73 | 0 | goto err; |
74 | | |
75 | 0 | if (info->description != NULL |
76 | 0 | && (qlog->info.description = OPENSSL_strdup(info->description)) == NULL) |
77 | 0 | goto err; |
78 | | |
79 | 0 | if (info->group_id != NULL |
80 | 0 | && (qlog->info.group_id = OPENSSL_strdup(info->group_id)) == NULL) |
81 | 0 | goto err; |
82 | | |
83 | 0 | if (info->override_impl_name != NULL |
84 | 0 | && (qlog->info.override_impl_name |
85 | 0 | = OPENSSL_strdup(info->override_impl_name)) |
86 | 0 | == NULL) |
87 | 0 | goto err; |
88 | | |
89 | 0 | if (!ossl_json_init(&qlog->json, NULL, |
90 | 0 | OSSL_JSON_FLAG_IJSON | OSSL_JSON_FLAG_SEQ)) |
91 | 0 | goto err; |
92 | | |
93 | 0 | if (qlog->info.now_cb == NULL) |
94 | 0 | qlog->info.now_cb = default_now; |
95 | |
|
96 | 0 | return qlog; |
97 | | |
98 | 0 | err: |
99 | 0 | if (qlog != NULL) { |
100 | 0 | OPENSSL_free((char *)qlog->info.title); |
101 | 0 | OPENSSL_free((char *)qlog->info.description); |
102 | 0 | OPENSSL_free((char *)qlog->info.group_id); |
103 | 0 | OPENSSL_free((char *)qlog->info.override_impl_name); |
104 | 0 | OPENSSL_free(qlog); |
105 | 0 | } |
106 | 0 | return NULL; |
107 | 0 | } |
108 | | |
109 | | static const char *ossl_dirsep_string(const char *path) |
110 | 0 | { |
111 | 0 | if (ossl_ends_with_dirsep(path)) |
112 | 0 | return ""; |
113 | | |
114 | | #if defined(_WIN32) |
115 | | return "\\"; |
116 | | #elif defined(__VMS) |
117 | | return ":"; |
118 | | #else |
119 | 0 | return "/"; |
120 | 0 | #endif /* defined(_WIN32) */ |
121 | 0 | } |
122 | | |
123 | | QLOG *ossl_qlog_new_from_env(const QLOG_TRACE_INFO *info) |
124 | 0 | { |
125 | 0 | QLOG *qlog = NULL; |
126 | 0 | const char *qlogdir = ossl_safe_getenv("QLOGDIR"); |
127 | 0 | const char *qfilter = ossl_safe_getenv("OSSL_QFILTER"); |
128 | 0 | char cid_hex[QUIC_MAX_CONN_ID_LEN * 2 + 1], *filename = NULL; |
129 | 0 | size_t i, hexlen = 0; |
130 | |
|
131 | 0 | if (info == NULL || qlogdir == NULL || qlogdir[0] == '\0') |
132 | 0 | return NULL; |
133 | | |
134 | 0 | if (!ossl_assert(info->odcid.id_len <= QUIC_MAX_CONN_ID_LEN)) { |
135 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR); |
136 | 0 | return NULL; |
137 | 0 | } |
138 | | |
139 | 0 | for (i = 0; i < info->odcid.id_len; ++i) |
140 | 0 | hexlen += ossl_to_lowerhex(cid_hex + hexlen, info->odcid.id[i]); |
141 | 0 | cid_hex[hexlen] = '\0'; |
142 | |
|
143 | 0 | if (ossl_asprintf(&filename, "%s%s%s_%s.sqlog", qlogdir, |
144 | 0 | ossl_dirsep_string(qlogdir), cid_hex, |
145 | 0 | info->is_server ? "server" : "client") |
146 | 0 | < 0) |
147 | 0 | return NULL; |
148 | | |
149 | 0 | qlog = ossl_qlog_new(info); |
150 | 0 | if (qlog == NULL) |
151 | 0 | goto err; |
152 | | |
153 | 0 | if (!ossl_qlog_set_sink_filename(qlog, filename)) |
154 | 0 | goto err; |
155 | | |
156 | 0 | if (qfilter == NULL || qfilter[0] == '\0') |
157 | 0 | qfilter = "*"; |
158 | |
|
159 | 0 | if (!ossl_qlog_set_filter(qlog, qfilter)) |
160 | 0 | goto err; |
161 | | |
162 | 0 | OPENSSL_free(filename); |
163 | 0 | return qlog; |
164 | | |
165 | 0 | err: |
166 | 0 | OPENSSL_free(filename); |
167 | 0 | ossl_qlog_free(qlog); |
168 | 0 | return NULL; |
169 | 0 | } |
170 | | |
171 | | void ossl_qlog_free(QLOG *qlog) |
172 | 0 | { |
173 | 0 | if (qlog == NULL) |
174 | 0 | return; |
175 | | |
176 | 0 | ossl_json_flush_cleanup(&qlog->json); |
177 | 0 | BIO_free_all(qlog->bio); |
178 | 0 | OPENSSL_free((char *)qlog->info.title); |
179 | 0 | OPENSSL_free((char *)qlog->info.description); |
180 | 0 | OPENSSL_free((char *)qlog->info.group_id); |
181 | 0 | OPENSSL_free((char *)qlog->info.override_impl_name); |
182 | 0 | OPENSSL_free(qlog); |
183 | 0 | } |
184 | | |
185 | | /* |
186 | | * Configuration |
187 | | * ============= |
188 | | */ |
189 | | int ossl_qlog_set_sink_bio(QLOG *qlog, BIO *bio) |
190 | 0 | { |
191 | 0 | if (qlog == NULL) |
192 | 0 | return 0; |
193 | | |
194 | 0 | ossl_qlog_flush(qlog); /* best effort */ |
195 | 0 | BIO_free_all(qlog->bio); |
196 | 0 | qlog->bio = bio; |
197 | 0 | ossl_json_set0_sink(&qlog->json, bio); |
198 | 0 | return 1; |
199 | 0 | } |
200 | | |
201 | | #ifndef OPENSSL_NO_STDIO |
202 | | |
203 | | int ossl_qlog_set_sink_file(QLOG *qlog, FILE *f, int close_flag) |
204 | 0 | { |
205 | 0 | BIO *bio; |
206 | |
|
207 | 0 | if (qlog == NULL) |
208 | 0 | return 0; |
209 | | |
210 | 0 | bio = BIO_new_fp(f, BIO_CLOSE); |
211 | 0 | if (bio == NULL) |
212 | 0 | return 0; |
213 | | |
214 | 0 | if (!ossl_qlog_set_sink_bio(qlog, bio)) { |
215 | 0 | BIO_free_all(bio); |
216 | 0 | return 0; |
217 | 0 | } |
218 | | |
219 | 0 | return 1; |
220 | 0 | } |
221 | | |
222 | | #endif |
223 | | |
224 | | int ossl_qlog_set_sink_filename(QLOG *qlog, const char *filename) |
225 | 0 | { |
226 | 0 | BIO *bio; |
227 | |
|
228 | 0 | if (qlog == NULL) |
229 | 0 | return 0; |
230 | | |
231 | | /* |
232 | | * We supply our own text encoding as JSON requires UTF-8, so disable any |
233 | | * OS-specific processing here. |
234 | | */ |
235 | 0 | bio = BIO_new_file(filename, "wb"); |
236 | 0 | if (bio == NULL) |
237 | 0 | return 0; |
238 | | |
239 | 0 | if (!ossl_qlog_set_sink_bio(qlog, bio)) { |
240 | 0 | BIO_free_all(bio); |
241 | 0 | return 0; |
242 | 0 | } |
243 | | |
244 | 0 | return 1; |
245 | 0 | } |
246 | | |
247 | | int ossl_qlog_flush(QLOG *qlog) |
248 | 0 | { |
249 | 0 | if (qlog == NULL) |
250 | 0 | return 1; |
251 | | |
252 | 0 | return ossl_json_flush(&qlog->json); |
253 | 0 | } |
254 | | |
255 | | int ossl_qlog_set_event_type_enabled(QLOG *qlog, uint32_t event_type, |
256 | | int enabled) |
257 | 0 | { |
258 | 0 | if (qlog == NULL || event_type >= QLOG_EVENT_TYPE_NUM) |
259 | 0 | return 0; |
260 | | |
261 | 0 | bit_set(qlog->enabled, event_type, enabled); |
262 | 0 | return 1; |
263 | 0 | } |
264 | | |
265 | | int ossl_qlog_enabled(QLOG *qlog, uint32_t event_type) |
266 | 0 | { |
267 | 0 | if (qlog == NULL) |
268 | 0 | return 0; |
269 | | |
270 | 0 | return bit_get(qlog->enabled, event_type) != 0; |
271 | 0 | } |
272 | | |
273 | | /* |
274 | | * Event Lifecycle |
275 | | * =============== |
276 | | */ |
277 | | static void write_str_once(QLOG *qlog, const char *key, char **p) |
278 | 0 | { |
279 | 0 | if (*p == NULL) |
280 | 0 | return; |
281 | | |
282 | 0 | ossl_json_key(&qlog->json, key); |
283 | 0 | ossl_json_str(&qlog->json, *p); |
284 | |
|
285 | 0 | OPENSSL_free(*p); |
286 | 0 | *p = NULL; |
287 | 0 | } |
288 | | |
289 | | static void qlog_event_seq_header(QLOG *qlog) |
290 | 0 | { |
291 | 0 | if (qlog->header_done) |
292 | 0 | return; |
293 | | |
294 | 0 | ossl_json_object_begin(&qlog->json); |
295 | 0 | { |
296 | 0 | ossl_json_key(&qlog->json, "qlog_version"); |
297 | 0 | ossl_json_str(&qlog->json, "0.3"); |
298 | |
|
299 | 0 | ossl_json_key(&qlog->json, "qlog_format"); |
300 | 0 | ossl_json_str(&qlog->json, "JSON-SEQ"); |
301 | |
|
302 | 0 | write_str_once(qlog, "title", (char **)&qlog->info.title); |
303 | 0 | write_str_once(qlog, "description", (char **)&qlog->info.description); |
304 | |
|
305 | 0 | ossl_json_key(&qlog->json, "trace"); |
306 | 0 | ossl_json_object_begin(&qlog->json); |
307 | 0 | { |
308 | 0 | ossl_json_key(&qlog->json, "common_fields"); |
309 | 0 | ossl_json_object_begin(&qlog->json); |
310 | 0 | { |
311 | 0 | ossl_json_key(&qlog->json, "time_format"); |
312 | 0 | ossl_json_str(&qlog->json, "delta"); |
313 | |
|
314 | 0 | ossl_json_key(&qlog->json, "protocol_type"); |
315 | 0 | ossl_json_array_begin(&qlog->json); |
316 | 0 | { |
317 | 0 | ossl_json_str(&qlog->json, "QUIC"); |
318 | 0 | } /* protocol_type */ |
319 | 0 | ossl_json_array_end(&qlog->json); |
320 | |
|
321 | 0 | write_str_once(qlog, "group_id", (char **)&qlog->info.group_id); |
322 | |
|
323 | 0 | ossl_json_key(&qlog->json, "system_info"); |
324 | 0 | ossl_json_object_begin(&qlog->json); |
325 | 0 | { |
326 | 0 | if (qlog->info.override_process_id != 0) { |
327 | 0 | ossl_json_key(&qlog->json, "process_id"); |
328 | 0 | ossl_json_u64(&qlog->json, qlog->info.override_process_id); |
329 | 0 | } else { |
330 | 0 | #if defined(OPENSSL_SYS_UNIX) |
331 | 0 | ossl_json_key(&qlog->json, "process_id"); |
332 | 0 | ossl_json_u64(&qlog->json, (uint64_t)getpid()); |
333 | | #elif defined(OPENSSL_SYS_WINDOWS) |
334 | | ossl_json_key(&qlog->json, "process_id"); |
335 | | ossl_json_u64(&qlog->json, (uint64_t)GetCurrentProcessId()); |
336 | | #endif |
337 | 0 | } |
338 | 0 | } /* system_info */ |
339 | 0 | ossl_json_object_end(&qlog->json); |
340 | 0 | } /* common_fields */ |
341 | 0 | ossl_json_object_end(&qlog->json); |
342 | |
|
343 | 0 | ossl_json_key(&qlog->json, "vantage_point"); |
344 | 0 | ossl_json_object_begin(&qlog->json); |
345 | 0 | { |
346 | 0 | char buf[128]; |
347 | 0 | const char *p = buf; |
348 | |
|
349 | 0 | if (qlog->info.override_impl_name != NULL) { |
350 | 0 | p = qlog->info.override_impl_name; |
351 | 0 | } else { |
352 | 0 | snprintf(buf, sizeof(buf), "OpenSSL/%s (%s)", |
353 | 0 | OpenSSL_version(OPENSSL_FULL_VERSION_STRING), |
354 | 0 | OpenSSL_version(OPENSSL_PLATFORM) + 10); |
355 | 0 | } |
356 | |
|
357 | 0 | ossl_json_key(&qlog->json, "type"); |
358 | 0 | ossl_json_str(&qlog->json, |
359 | 0 | qlog->info.is_server ? "server" : "client"); |
360 | |
|
361 | 0 | ossl_json_key(&qlog->json, "name"); |
362 | 0 | ossl_json_str(&qlog->json, p); |
363 | 0 | } /* vantage_point */ |
364 | 0 | ossl_json_object_end(&qlog->json); |
365 | 0 | } /* trace */ |
366 | 0 | ossl_json_object_end(&qlog->json); |
367 | 0 | } |
368 | 0 | ossl_json_object_end(&qlog->json); |
369 | |
|
370 | 0 | qlog->header_done = 1; |
371 | 0 | } |
372 | | |
373 | | static void qlog_event_prologue(QLOG *qlog) |
374 | 0 | { |
375 | 0 | qlog_event_seq_header(qlog); |
376 | |
|
377 | 0 | ossl_json_object_begin(&qlog->json); |
378 | |
|
379 | 0 | ossl_json_key(&qlog->json, "name"); |
380 | 0 | ossl_json_str(&qlog->json, qlog->event_combined_name); |
381 | |
|
382 | 0 | ossl_json_key(&qlog->json, "data"); |
383 | 0 | ossl_json_object_begin(&qlog->json); |
384 | 0 | } |
385 | | |
386 | | static void qlog_event_epilogue(QLOG *qlog) |
387 | 0 | { |
388 | 0 | ossl_json_object_end(&qlog->json); |
389 | |
|
390 | 0 | ossl_json_key(&qlog->json, "time"); |
391 | 0 | if (!qlog->first_event_done) { |
392 | 0 | ossl_json_u64(&qlog->json, ossl_time2ms(qlog->event_time)); |
393 | 0 | qlog->prev_event_time = qlog->event_time; |
394 | 0 | qlog->first_event_done = 1; |
395 | 0 | } else { |
396 | 0 | OSSL_TIME delta = ossl_time_subtract(qlog->event_time, |
397 | 0 | qlog->prev_event_time); |
398 | |
|
399 | 0 | ossl_json_u64(&qlog->json, ossl_time2ms(delta)); |
400 | 0 | qlog->prev_event_time = qlog->event_time; |
401 | 0 | } |
402 | |
|
403 | 0 | ossl_json_object_end(&qlog->json); |
404 | 0 | } |
405 | | |
406 | | int ossl_qlog_event_try_begin(QLOG *qlog, |
407 | | uint32_t event_type, |
408 | | const char *event_cat, |
409 | | const char *event_name, |
410 | | const char *event_combined_name) |
411 | 0 | { |
412 | 0 | if (qlog == NULL) |
413 | 0 | return 0; |
414 | | |
415 | 0 | if (!ossl_assert(qlog->event_type == QLOG_EVENT_TYPE_NONE) |
416 | 0 | || !ossl_qlog_enabled(qlog, event_type)) |
417 | 0 | return 0; |
418 | | |
419 | 0 | qlog->event_type = event_type; |
420 | 0 | qlog->event_cat = event_cat; |
421 | 0 | qlog->event_name = event_name; |
422 | 0 | qlog->event_combined_name = event_combined_name; |
423 | 0 | qlog->event_time = qlog->info.now_cb(qlog->info.now_cb_arg); |
424 | |
|
425 | 0 | qlog_event_prologue(qlog); |
426 | 0 | return 1; |
427 | 0 | } |
428 | | |
429 | | void ossl_qlog_event_end(QLOG *qlog) |
430 | 0 | { |
431 | 0 | if (!ossl_assert(qlog != NULL && qlog->event_type != QLOG_EVENT_TYPE_NONE)) |
432 | 0 | return; |
433 | | |
434 | 0 | qlog_event_epilogue(qlog); |
435 | 0 | qlog->event_type = QLOG_EVENT_TYPE_NONE; |
436 | 0 | } |
437 | | |
438 | | /* |
439 | | * Field Generators |
440 | | * ================ |
441 | | */ |
442 | | void ossl_qlog_group_begin(QLOG *qlog, const char *name) |
443 | 0 | { |
444 | 0 | if (name != NULL) |
445 | 0 | ossl_json_key(&qlog->json, name); |
446 | |
|
447 | 0 | ossl_json_object_begin(&qlog->json); |
448 | 0 | } |
449 | | |
450 | | void ossl_qlog_group_end(QLOG *qlog) |
451 | 0 | { |
452 | 0 | ossl_json_object_end(&qlog->json); |
453 | 0 | } |
454 | | |
455 | | void ossl_qlog_array_begin(QLOG *qlog, const char *name) |
456 | 0 | { |
457 | 0 | if (name != NULL) |
458 | 0 | ossl_json_key(&qlog->json, name); |
459 | |
|
460 | 0 | ossl_json_array_begin(&qlog->json); |
461 | 0 | } |
462 | | |
463 | | void ossl_qlog_array_end(QLOG *qlog) |
464 | 0 | { |
465 | 0 | ossl_json_array_end(&qlog->json); |
466 | 0 | } |
467 | | |
468 | | void ossl_qlog_override_time(QLOG *qlog, OSSL_TIME event_time) |
469 | 0 | { |
470 | 0 | qlog->event_time = event_time; |
471 | 0 | } |
472 | | |
473 | | void ossl_qlog_str(QLOG *qlog, const char *name, const char *value) |
474 | 0 | { |
475 | 0 | if (name != NULL) |
476 | 0 | ossl_json_key(&qlog->json, name); |
477 | |
|
478 | 0 | ossl_json_str(&qlog->json, value); |
479 | 0 | } |
480 | | |
481 | | void ossl_qlog_str_len(QLOG *qlog, const char *name, |
482 | | const char *value, size_t value_len) |
483 | 0 | { |
484 | 0 | if (name != NULL) |
485 | 0 | ossl_json_key(&qlog->json, name); |
486 | |
|
487 | 0 | ossl_json_str_len(&qlog->json, value, value_len); |
488 | 0 | } |
489 | | |
490 | | void ossl_qlog_u64(QLOG *qlog, const char *name, uint64_t value) |
491 | 0 | { |
492 | 0 | if (name != NULL) |
493 | 0 | ossl_json_key(&qlog->json, name); |
494 | |
|
495 | 0 | ossl_json_u64(&qlog->json, value); |
496 | 0 | } |
497 | | |
498 | | void ossl_qlog_i64(QLOG *qlog, const char *name, int64_t value) |
499 | 0 | { |
500 | 0 | if (name != NULL) |
501 | 0 | ossl_json_key(&qlog->json, name); |
502 | |
|
503 | 0 | ossl_json_i64(&qlog->json, value); |
504 | 0 | } |
505 | | |
506 | | void ossl_qlog_bool(QLOG *qlog, const char *name, bool value) |
507 | 0 | { |
508 | 0 | if (name != NULL) |
509 | 0 | ossl_json_key(&qlog->json, name); |
510 | |
|
511 | 0 | ossl_json_bool(&qlog->json, value); |
512 | 0 | } |
513 | | |
514 | | void ossl_qlog_bin(QLOG *qlog, const char *name, |
515 | | const void *value, size_t value_len) |
516 | 0 | { |
517 | 0 | if (name != NULL) |
518 | 0 | ossl_json_key(&qlog->json, name); |
519 | |
|
520 | 0 | ossl_json_str_hex(&qlog->json, value, value_len); |
521 | 0 | } |
522 | | |
523 | | /* |
524 | | * Filter Parsing |
525 | | * ============== |
526 | | */ |
527 | | struct lexer { |
528 | | const char *p, *term_end, *end; |
529 | | }; |
530 | | |
531 | | static ossl_inline int is_term_sep_ws(char c) |
532 | 0 | { |
533 | 0 | return c == ' ' || c == '\r' || c == '\n' || c == '\t'; |
534 | 0 | } |
535 | | |
536 | | static ossl_inline int is_name_char(char c) |
537 | 0 | { |
538 | 0 | return ossl_isalpha(c) || ossl_isdigit(c) || c == '_' || c == '-'; |
539 | 0 | } |
540 | | |
541 | | static int lex_init(struct lexer *lex, const char *in, size_t in_len) |
542 | 0 | { |
543 | 0 | if (in == NULL) |
544 | 0 | return 0; |
545 | | |
546 | 0 | lex->p = in; |
547 | 0 | lex->term_end = in; |
548 | 0 | lex->end = in + in_len; |
549 | 0 | return 1; |
550 | 0 | } |
551 | | |
552 | | static int lex_do(struct lexer *lex) |
553 | 0 | { |
554 | 0 | const char *p = lex->term_end, *end = lex->end, *term_end; |
555 | |
|
556 | 0 | for (; is_term_sep_ws(*p) && p < end; ++p) |
557 | 0 | ; |
558 | |
|
559 | 0 | if (p == end) { |
560 | 0 | lex->p = end; |
561 | 0 | lex->term_end = end; |
562 | 0 | return 0; |
563 | 0 | } |
564 | | |
565 | 0 | for (term_end = p; !is_term_sep_ws(*term_end) && term_end < end; ++term_end) |
566 | 0 | ; |
567 | |
|
568 | 0 | lex->p = p; |
569 | 0 | lex->term_end = term_end; |
570 | 0 | return 1; |
571 | 0 | } |
572 | | |
573 | | static int lex_eot(struct lexer *lex) |
574 | 0 | { |
575 | 0 | return lex->p == lex->term_end; |
576 | 0 | } |
577 | | |
578 | | static int lex_peek_char(struct lexer *lex) |
579 | 0 | { |
580 | 0 | return lex_eot(lex) ? -1 : *lex->p; |
581 | 0 | } |
582 | | |
583 | | static int lex_skip_char(struct lexer *lex) |
584 | 0 | { |
585 | 0 | if (lex_eot(lex)) |
586 | 0 | return 0; |
587 | | |
588 | 0 | ++lex->p; |
589 | 0 | return 1; |
590 | 0 | } |
591 | | |
592 | | static int lex_match(struct lexer *lex, const char *s, size_t s_len) |
593 | 0 | { |
594 | 0 | if ((size_t)(lex->term_end - lex->p) != s_len) |
595 | 0 | return 0; |
596 | | |
597 | 0 | if (memcmp(lex->p, s, s_len)) |
598 | 0 | return 0; |
599 | | |
600 | 0 | return 1; |
601 | 0 | } |
602 | | |
603 | | static void lex_get_rest(struct lexer *lex, const char **str, size_t *str_l) |
604 | 0 | { |
605 | 0 | *str = lex->p; |
606 | 0 | *str_l = lex->term_end - lex->p; |
607 | 0 | } |
608 | | |
609 | | static int lex_extract_to(struct lexer *lex, char c, |
610 | | const char **str, size_t *str_l) |
611 | 0 | { |
612 | 0 | const char *p = lex->p, *term_end = lex->term_end, *s; |
613 | |
|
614 | 0 | for (s = p; s < term_end && *s != c; ++s) |
615 | 0 | ; |
616 | 0 | if (s == term_end) |
617 | 0 | return 0; |
618 | | |
619 | 0 | *str = p; |
620 | 0 | *str_l = s - p; |
621 | 0 | lex->p = ++s; |
622 | 0 | return 1; |
623 | 0 | } |
624 | | |
625 | | static int ossl_unused filter_match_event(const char *cat, size_t cat_l, |
626 | | const char *event, size_t event_l, |
627 | | const char *expect_cat, |
628 | | const char *expect_event) |
629 | 0 | { |
630 | 0 | size_t expect_cat_l = strlen(expect_cat); |
631 | 0 | size_t expect_event_l = strlen(expect_event); |
632 | |
|
633 | 0 | if ((cat != NULL && cat_l != expect_cat_l) |
634 | 0 | || (event != NULL && event_l != expect_event_l) |
635 | 0 | || (cat != NULL && memcmp(cat, expect_cat, expect_cat_l)) |
636 | 0 | || (event != NULL && memcmp(event, expect_event, expect_event_l))) |
637 | 0 | return 0; |
638 | | |
639 | 0 | return 1; |
640 | 0 | } |
641 | | |
642 | | /* |
643 | | * enabled: event enablement bitmask Array of size NUM_ENABLED_W. |
644 | | * add: 1 to enable an event, 0 to disable. |
645 | | * cat: Category name/length. Not necessarily zero terminated. |
646 | | * NULL to match any. |
647 | | * event: Event name/length. Not necessarily zero terminated. |
648 | | * NULL to match any. |
649 | | */ |
650 | | static void filter_apply(size_t *enabled, int add, |
651 | | const char *cat, size_t cat_l, |
652 | | const char *event, size_t event_l) |
653 | 0 | { |
654 | | /* clang-format off */ |
655 | | /* Find events which match the given filters. */ |
656 | 0 | #define QLOG_EVENT(e_cat, e_name) \ |
657 | 0 | if (filter_match_event(cat, cat_l, event, event_l, #e_cat, #e_name)) \ |
658 | 0 | bit_set(enabled, QLOG_EVENT_TYPE_##e_cat##_##e_name, add); |
659 | 0 | #include "internal/qlog_events.inc" |
660 | 0 | #undef QLOG_EVENT |
661 | | /* clang-format on */ |
662 | 0 | } |
663 | | |
664 | | static int lex_fail(struct lexer *lex, const char *msg) |
665 | 0 | { |
666 | | /* |
667 | | * TODO(QLOG FUTURE): Determine how to print log messages about bad filter |
668 | | * strings |
669 | | */ |
670 | 0 | lex->p = lex->term_end = lex->end; |
671 | 0 | return 0; |
672 | 0 | } |
673 | | |
674 | | static int validate_name(const char **p, size_t *l) |
675 | 0 | { |
676 | 0 | const char *p_ = *p; |
677 | 0 | size_t i, l_ = *l; |
678 | |
|
679 | 0 | if (l_ == 1 && *p_ == '*') { |
680 | 0 | *p = NULL; |
681 | 0 | *l = 0; |
682 | 0 | return 1; |
683 | 0 | } |
684 | | |
685 | 0 | if (l_ == 0) |
686 | 0 | return 0; |
687 | | |
688 | 0 | for (i = 0; i < l_; ++i) |
689 | 0 | if (!is_name_char(p_[i])) |
690 | 0 | return 0; |
691 | | |
692 | 0 | return 1; |
693 | 0 | } |
694 | | |
695 | | int ossl_qlog_set_filter(QLOG *qlog, const char *filter) |
696 | 0 | { |
697 | 0 | struct lexer lex = { 0 }; |
698 | 0 | char c; |
699 | 0 | const char *cat, *event; |
700 | 0 | size_t cat_l, event_l, enabled[NUM_ENABLED_W]; |
701 | 0 | int add; |
702 | |
|
703 | 0 | memcpy(enabled, qlog->enabled, sizeof(enabled)); |
704 | |
|
705 | 0 | if (!lex_init(&lex, filter, strlen(filter))) |
706 | 0 | return 0; |
707 | | |
708 | 0 | while (lex_do(&lex)) { |
709 | 0 | c = lex_peek_char(&lex); |
710 | 0 | if (c == '+' || c == '-') { |
711 | 0 | add = (c == '+'); |
712 | 0 | lex_skip_char(&lex); |
713 | |
|
714 | 0 | c = lex_peek_char(&lex); |
715 | 0 | if (!is_name_char(c) && c != '*') |
716 | 0 | return lex_fail(&lex, "expected alphanumeric name or '*'" |
717 | 0 | " after +/-"); |
718 | 0 | } else if (!is_name_char(c) && c != '*') { |
719 | 0 | return lex_fail(&lex, "expected +/- or alphanumeric name or '*'"); |
720 | 0 | } else { |
721 | 0 | add = 1; |
722 | 0 | } |
723 | | |
724 | 0 | if (lex_match(&lex, "*", 1)) { |
725 | 0 | filter_apply(enabled, add, NULL, 0, NULL, 0); |
726 | 0 | continue; |
727 | 0 | } |
728 | | |
729 | 0 | if (!lex_extract_to(&lex, ':', &cat, &cat_l)) |
730 | 0 | return lex_fail(&lex, "expected ':' after category name"); |
731 | | |
732 | 0 | lex_get_rest(&lex, &event, &event_l); |
733 | 0 | if (!validate_name(&cat, &cat_l)) |
734 | 0 | return lex_fail(&lex, "expected alphanumeric category name or '*'"); |
735 | 0 | if (!validate_name(&event, &event_l)) |
736 | 0 | return lex_fail(&lex, "expected alphanumeric event name or '*'"); |
737 | | |
738 | 0 | filter_apply(enabled, add, cat, cat_l, event, event_l); |
739 | 0 | } |
740 | | |
741 | 0 | memcpy(qlog->enabled, enabled, sizeof(enabled)); |
742 | 0 | return 1; |
743 | 0 | } |