Branch data Line data Source code
1 : : /* SPDX-FileCopyrightText: Copyright 2026 Cozens Software Solutions Limited
2 : : * SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0 OR LicenseRef-PolyForm-Internal-Use-1.0.0 OR LicenseRef-COSOSO-Commercial
3 : : */
4 : :
5 : : #include "SolidSyslogUdpSender.h"
6 : :
7 : : #include <stdbool.h>
8 : : #include <stddef.h>
9 : : #include <stdint.h>
10 : :
11 : : #include "SolidSyslogError.h"
12 : : #include "SolidSyslogErrorCategory.h"
13 : : #include "SolidSyslogNullSender.h"
14 : : #include "SolidSyslogPoolAllocator.h"
15 : : #include "SolidSyslogPrival.h"
16 : : #include "SolidSyslogTunables.h"
17 : : #include "SolidSyslogUdpSenderErrors.h"
18 : : #include "SolidSyslogUdpSenderPrivate.h"
19 : :
20 : : struct SolidSyslogSender;
21 : :
22 : : static bool UdpSender_IsValidConfig(const struct SolidSyslogUdpSenderConfig* config);
23 : : static inline size_t UdpSender_IndexFromHandle(const struct SolidSyslogSender* base);
24 : : static inline void UdpSender_CleanupAtIndex(size_t index, void* context);
25 : :
26 : : static bool UdpSender_InUse[SOLIDSYSLOG_UDP_SENDER_POOL_SIZE];
27 : : static struct SolidSyslogUdpSender UdpSender_Pool[SOLIDSYSLOG_UDP_SENDER_POOL_SIZE];
28 : : static struct SolidSyslogPoolAllocator UdpSender_Allocator = {UdpSender_InUse, SOLIDSYSLOG_UDP_SENDER_POOL_SIZE};
29 : :
30 : 138 : struct SolidSyslogSender* SolidSyslogUdpSender_Create(const struct SolidSyslogUdpSenderConfig* config)
31 : : {
32 : 138 : struct SolidSyslogSender* result = SolidSyslogNullSender_Get();
33 [ + + ]: 138 : if (UdpSender_IsValidConfig(config))
34 : : {
35 : 131 : size_t index = SolidSyslogPoolAllocator_AcquireFirstFree(&UdpSender_Allocator);
36 [ + + ]: 131 : if (SolidSyslogPoolAllocator_IndexIsValid(&UdpSender_Allocator, index))
37 : : {
38 : 129 : SolidSyslogUdpSender_Initialise(&UdpSender_Pool[index].Base, config);
39 : 129 : result = &UdpSender_Pool[index].Base;
40 : : }
41 : : else
42 : : {
43 : 2 : UdpSender_Report(
44 : : SOLIDSYSLOG_POOL_EXHAUSTED_SEVERITY,
45 : : SOLIDSYSLOG_CAT_POOL_EXHAUSTED,
46 : : SOLIDSYSLOG_UDP_SENDER_ERROR_POOL_EXHAUSTED
47 : : );
48 : : }
49 : : }
50 : 138 : return result;
51 : : }
52 : :
53 : 138 : static bool UdpSender_IsValidConfig(const struct SolidSyslogUdpSenderConfig* config)
54 : : {
55 : 138 : bool valid = false;
56 [ + + ]: 138 : if (config == NULL)
57 : : {
58 : 3 : UdpSender_Report(
59 : : SOLIDSYSLOG_BAD_CONFIG_FATAL_SEVERITY,
60 : : SOLIDSYSLOG_CAT_BAD_CONFIG,
61 : : SOLIDSYSLOG_UDP_SENDER_ERROR_NULL_CONFIG
62 : : );
63 : : }
64 [ + + ]: 135 : else if (config->Resolver == NULL)
65 : : {
66 : 1 : UdpSender_Report(
67 : : SOLIDSYSLOG_BAD_CONFIG_FATAL_SEVERITY,
68 : : SOLIDSYSLOG_CAT_BAD_CONFIG,
69 : : SOLIDSYSLOG_UDP_SENDER_ERROR_NULL_RESOLVER
70 : : );
71 : : }
72 [ + + ]: 134 : else if (config->Datagram == NULL)
73 : : {
74 : 1 : UdpSender_Report(
75 : : SOLIDSYSLOG_BAD_CONFIG_FATAL_SEVERITY,
76 : : SOLIDSYSLOG_CAT_BAD_CONFIG,
77 : : SOLIDSYSLOG_UDP_SENDER_ERROR_NULL_DATAGRAM
78 : : );
79 : : }
80 [ + + ]: 133 : else if (config->Address == NULL)
81 : : {
82 : 1 : UdpSender_Report(
83 : : SOLIDSYSLOG_BAD_CONFIG_FATAL_SEVERITY,
84 : : SOLIDSYSLOG_CAT_BAD_CONFIG,
85 : : SOLIDSYSLOG_UDP_SENDER_ERROR_NULL_ADDRESS
86 : : );
87 : : }
88 [ + + ]: 132 : else if (config->Endpoint == NULL)
89 : : {
90 : 1 : UdpSender_Report(
91 : : SOLIDSYSLOG_BAD_CONFIG_FATAL_SEVERITY,
92 : : SOLIDSYSLOG_CAT_BAD_CONFIG,
93 : : SOLIDSYSLOG_UDP_SENDER_ERROR_NULL_ENDPOINT
94 : : );
95 : : }
96 : : else
97 : : {
98 : 131 : valid = true;
99 : : }
100 : 138 : return valid;
101 : : }
102 : :
103 : 140 : void SolidSyslogUdpSender_Destroy(struct SolidSyslogSender* base)
104 : : {
105 : 140 : size_t index = UdpSender_IndexFromHandle(base);
106 [ + + + + ]: 270 : bool released = SolidSyslogPoolAllocator_IndexIsValid(&UdpSender_Allocator, index) &&
107 : 130 : SolidSyslogPoolAllocator_FreeIfInUse(&UdpSender_Allocator, index, UdpSender_CleanupAtIndex, NULL);
108 [ + + ]: 140 : if (!released)
109 : : {
110 : 11 : UdpSender_Report(
111 : : SOLIDSYSLOG_UNKNOWN_DESTROY_SEVERITY,
112 : : SOLIDSYSLOG_CAT_UNKNOWN_DESTROY,
113 : : SOLIDSYSLOG_UDP_SENDER_ERROR_UNKNOWN_DESTROY
114 : : );
115 : : }
116 : 140 : }
117 : :
118 : 140 : static inline size_t UdpSender_IndexFromHandle(const struct SolidSyslogSender* base)
119 : : {
120 : 140 : size_t result = SOLIDSYSLOG_UDP_SENDER_POOL_SIZE;
121 [ + + ]: 150 : for (size_t poolIndex = 0U; poolIndex < SOLIDSYSLOG_UDP_SENDER_POOL_SIZE; poolIndex++)
122 : : {
123 [ + + ]: 140 : if (base == &UdpSender_Pool[poolIndex].Base)
124 : : {
125 : 130 : result = poolIndex;
126 : 130 : break;
127 : : }
128 : : }
129 : 140 : return result;
130 : : }
131 : :
132 : 129 : static inline void UdpSender_CleanupAtIndex(size_t index, void* context)
133 : : {
134 : : (void) context;
135 : 129 : SolidSyslogUdpSender_Cleanup(&UdpSender_Pool[index].Base);
136 : 129 : }
|