Line data Source code
1 : /* SPDX-License-Identifier: GPL-2.0-only */
2 : /*
3 : * Copyright (c) 2023 Meta Platforms, Inc. and affiliates.
4 : */
5 :
6 : #include "core/request.h"
7 :
8 : #include <errno.h>
9 : #include <stddef.h>
10 : #include <stdio.h>
11 : #include <stdlib.h>
12 : #include <string.h>
13 :
14 : #include "core/helper.h"
15 :
16 0 : int bf_request_new(struct bf_request **request, const void *data,
17 : size_t data_len)
18 : {
19 0 : _cleanup_bf_request_ struct bf_request *_request = NULL;
20 :
21 0 : bf_assert(request);
22 0 : bf_assert(!(!!data ^ !!data_len));
23 :
24 0 : _request = calloc(1, sizeof(*_request) + data_len);
25 0 : if (!_request)
26 : return -ENOMEM;
27 :
28 0 : if (data) {
29 0 : memcpy(_request->data, data, data_len);
30 0 : _request->data_len = data_len;
31 : }
32 :
33 0 : *request = TAKE_PTR(_request);
34 :
35 0 : return 0;
36 : }
37 :
38 0 : int bf_request_copy(struct bf_request **dest, const struct bf_request *src)
39 : {
40 0 : _cleanup_bf_request_ struct bf_request *_request = NULL;
41 :
42 0 : bf_assert(dest);
43 0 : bf_assert(src);
44 :
45 0 : _request = bf_memdup(src, bf_request_size(src));
46 0 : if (!_request)
47 : return -ENOMEM;
48 :
49 0 : *dest = TAKE_PTR(_request);
50 :
51 0 : return 0;
52 : }
53 :
54 0 : void bf_request_free(struct bf_request **request)
55 : {
56 0 : free(*request);
57 0 : *request = NULL;
58 0 : }
59 :
60 0 : const char *bf_request_cmd_to_str(enum bf_request_cmd cmd)
61 : {
62 : static const char *cmd_strs[] = {
63 : [BF_REQ_RULESET_FLUSH] = "BF_REQ_RULESET_FLUSH",
64 : [BF_REQ_RULESET_GET] = "BF_REQ_RULESET_GET",
65 : [BF_REQ_RULESET_SET] = "BF_REQ_RULESET_SET",
66 : [BF_REQ_CHAIN_SET] = "BF_REQ_CHAIN_SET",
67 : [BF_REQ_CHAIN_GET] = "BF_REQ_CHAIN_GET",
68 : [BF_REQ_CHAIN_LOAD] = "BF_REQ_CHAIN_LOAD",
69 : [BF_REQ_CHAIN_ATTACH] = "BF_REQ_CHAIN_ATTACH",
70 : [BF_REQ_CHAIN_UPDATE] = "BF_REQ_CHAIN_UPDATE",
71 : [BF_REQ_CHAIN_FLUSH] = "BF_REQ_CHAIN_FLUSH",
72 : [BF_REQ_COUNTERS_SET] = "BF_REQ_COUNTERS_SET",
73 : [BF_REQ_COUNTERS_GET] = "BF_REQ_COUNTERS_GET",
74 : [BF_REQ_CUSTOM] = "BF_REQ_CUSTOM",
75 : };
76 :
77 : static_assert(ARRAY_SIZE(cmd_strs) == _BF_REQ_CMD_MAX,
78 : "missing entries in bf_request_cmd array");
79 :
80 0 : return cmd_strs[cmd];
81 : }
|