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 "lwip/opt.h"
6 : :
7 : : /* This component requires lwIP built with DNS. */
8 : : #if LWIP_DNS
9 : :
10 : : #include "SolidSyslogLwipRawDnsResolver.h"
11 : :
12 : : #include <stdbool.h>
13 : : #include <stddef.h>
14 : : #include <stdint.h>
15 : :
16 : : #include "SolidSyslogError.h"
17 : : #include "SolidSyslogErrorCategory.h"
18 : : #include "SolidSyslogLwipRawDnsResolverErrors.h"
19 : : #include "SolidSyslogLwipRawDnsResolverPrivate.h"
20 : : #include "SolidSyslogNullResolver.h"
21 : : #include "SolidSyslogPoolAllocator.h"
22 : : #include "SolidSyslogPrival.h"
23 : : #include "SolidSyslogTunables.h"
24 : :
25 : : struct SolidSyslogResolver;
26 : :
27 : : static inline bool LwipRawDnsResolver_IsValidConfig(const struct SolidSyslogLwipRawDnsResolverConfig* config);
28 : : static inline size_t LwipRawDnsResolver_IndexFromHandle(const struct SolidSyslogResolver* base);
29 : : static inline void LwipRawDnsResolver_CleanupAtIndex(size_t index, void* context);
30 : :
31 : : static bool LwipRawDnsResolver_InUse[SOLIDSYSLOG_RESOLVER_POOL_SIZE];
32 : : static struct SolidSyslogLwipRawDnsResolver LwipRawDnsResolver_Pool[SOLIDSYSLOG_RESOLVER_POOL_SIZE];
33 : : static struct SolidSyslogPoolAllocator LwipRawDnsResolver_Allocator = {
34 : : LwipRawDnsResolver_InUse,
35 : : SOLIDSYSLOG_RESOLVER_POOL_SIZE
36 : : };
37 : :
38 : 46 : struct SolidSyslogResolver* SolidSyslogLwipRawDnsResolver_Create(
39 : : const struct SolidSyslogLwipRawDnsResolverConfig* config
40 : : )
41 : : {
42 : 46 : struct SolidSyslogResolver* handle = SolidSyslogNullResolver_Get();
43 [ + + ]: 46 : if (LwipRawDnsResolver_IsValidConfig(config))
44 : : {
45 : 42 : size_t index = SolidSyslogPoolAllocator_AcquireFirstFree(&LwipRawDnsResolver_Allocator);
46 [ + + ]: 42 : if (SolidSyslogPoolAllocator_IndexIsValid(&LwipRawDnsResolver_Allocator, index) == true)
47 : : {
48 : 38 : SolidSyslogLwipRawDnsResolver_Initialise(&LwipRawDnsResolver_Pool[index].Base, config);
49 : 38 : handle = &LwipRawDnsResolver_Pool[index].Base;
50 : : }
51 : : else
52 : : {
53 : 4 : LwipRawDnsResolver_Report(
54 : : SOLIDSYSLOG_POOL_EXHAUSTED_SEVERITY,
55 : : SOLIDSYSLOG_CAT_POOL_EXHAUSTED,
56 : : SOLIDSYSLOG_RESOLVER_ERROR_POOL_EXHAUSTED
57 : : );
58 : : }
59 : : }
60 : 46 : return handle;
61 : : }
62 : :
63 : 47 : void SolidSyslogLwipRawDnsResolver_Destroy(struct SolidSyslogResolver* base)
64 : : {
65 : 47 : size_t index = LwipRawDnsResolver_IndexFromHandle(base);
66 [ + + + + ]: 86 : bool released = SolidSyslogPoolAllocator_IndexIsValid(&LwipRawDnsResolver_Allocator, index) &&
67 : 39 : SolidSyslogPoolAllocator_FreeIfInUse(
68 : : &LwipRawDnsResolver_Allocator,
69 : : index,
70 : : LwipRawDnsResolver_CleanupAtIndex,
71 : : NULL
72 : : );
73 [ + + ]: 47 : if (!released)
74 : : {
75 : 9 : LwipRawDnsResolver_Report(
76 : : SOLIDSYSLOG_UNKNOWN_DESTROY_SEVERITY,
77 : : SOLIDSYSLOG_CAT_UNKNOWN_DESTROY,
78 : : SOLIDSYSLOG_RESOLVER_ERROR_UNKNOWN_DESTROY
79 : : );
80 : : }
81 : 47 : }
82 : :
83 : 46 : static inline bool LwipRawDnsResolver_IsValidConfig(const struct SolidSyslogLwipRawDnsResolverConfig* config)
84 : : {
85 : 46 : bool valid = false;
86 [ + + ]: 46 : if (config == NULL)
87 : : {
88 : 2 : LwipRawDnsResolver_Report(
89 : : SOLIDSYSLOG_BAD_CONFIG_FATAL_SEVERITY,
90 : : SOLIDSYSLOG_CAT_BAD_CONFIG,
91 : : SOLIDSYSLOG_RESOLVER_ERROR_NULL_CONFIG
92 : : );
93 : : }
94 [ + + ]: 44 : else if (config->Sleep == NULL)
95 : : {
96 : 2 : LwipRawDnsResolver_Report(
97 : : SOLIDSYSLOG_BAD_CONFIG_FATAL_SEVERITY,
98 : : SOLIDSYSLOG_CAT_BAD_CONFIG,
99 : : SOLIDSYSLOG_RESOLVER_ERROR_NULL_SLEEP
100 : : );
101 : : }
102 : : else
103 : : {
104 : 42 : valid = true;
105 : : }
106 : 46 : return valid;
107 : : }
108 : :
109 : 47 : static inline size_t LwipRawDnsResolver_IndexFromHandle(const struct SolidSyslogResolver* base)
110 : : {
111 : 47 : size_t result = SOLIDSYSLOG_RESOLVER_POOL_SIZE;
112 [ + + ]: 55 : for (size_t poolIndex = 0U; poolIndex < SOLIDSYSLOG_RESOLVER_POOL_SIZE; poolIndex++)
113 : : {
114 [ + + ]: 47 : if (base == &LwipRawDnsResolver_Pool[poolIndex].Base)
115 : : {
116 : 39 : result = poolIndex;
117 : 39 : break;
118 : : }
119 : : }
120 : 47 : return result;
121 : : }
122 : :
123 : 38 : static inline void LwipRawDnsResolver_CleanupAtIndex(size_t index, void* context)
124 : : {
125 : : (void) context;
126 : 38 : SolidSyslogLwipRawDnsResolver_Cleanup(&LwipRawDnsResolver_Pool[index].Base);
127 : 38 : }
128 : :
129 : : #else
130 : :
131 : : /* ISO C forbids an empty translation unit. */
132 : : typedef int LwipRawDnsResolverStatic_EmptyTranslationUnit;
133 : :
134 : : #endif /* LWIP_DNS */
|