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