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 : : #if LWIP_SOCKET
8 : :
9 : : #include "SolidSyslogLwipSocketAddress.h"
10 : :
11 : : #include <stdbool.h>
12 : : #include <stddef.h>
13 : : #include <stdint.h>
14 : :
15 : : #include "SolidSyslogError.h"
16 : : #include "SolidSyslogErrorCategory.h"
17 : : #include "SolidSyslogLwipSocketAddressErrors.h"
18 : : #include "SolidSyslogLwipSocketAddressPrivate.h"
19 : : #include "SolidSyslogPoolAllocator.h"
20 : : #include "SolidSyslogPrival.h"
21 : : #include "SolidSyslogTunables.h"
22 : :
23 : : struct SolidSyslogAddress;
24 : :
25 : : static inline struct SolidSyslogAddress* LwipSocketAddress_HandleFromIndex(size_t index);
26 : : static inline size_t LwipSocketAddress_IndexFromHandle(const struct SolidSyslogAddress* base);
27 : : static inline void LwipSocketAddress_CleanupAtIndex(size_t index, void* context);
28 : :
29 : : static bool LwipSocketAddress_InUse[SOLIDSYSLOG_ADDRESS_POOL_SIZE];
30 : : static struct SolidSyslogPoolAllocator LwipSocketAddress_Allocator = {
31 : : LwipSocketAddress_InUse,
32 : : SOLIDSYSLOG_ADDRESS_POOL_SIZE
33 : : };
34 : :
35 : 104 : struct SolidSyslogAddress* SolidSyslogLwipSocketAddress_Create(void)
36 : : {
37 : : /* TU-private fallback returned when the pool is exhausted. Sized as a real
38 : : * SolidSyslogLwipSocketAddress so a Resolver overwrite at the exhausted-fallback
39 : : * call site is bounded - same sockaddr_in storage as any pooled slot. */
40 : : static struct SolidSyslogLwipSocketAddress fallback;
41 : :
42 : 104 : size_t index = SolidSyslogPoolAllocator_AcquireFirstFree(&LwipSocketAddress_Allocator);
43 : 104 : struct SolidSyslogAddress* handle = (struct SolidSyslogAddress*) &fallback;
44 [ + + ]: 104 : if (SolidSyslogPoolAllocator_IndexIsValid(&LwipSocketAddress_Allocator, index) == true)
45 : : {
46 : 101 : handle = LwipSocketAddress_HandleFromIndex(index);
47 : 101 : SolidSyslogLwipSocketAddress_Initialise(handle);
48 : : }
49 : : else
50 : : {
51 : 3 : LwipSocketAddress_Report(
52 : : SOLIDSYSLOG_POOL_EXHAUSTED_SEVERITY,
53 : : SOLIDSYSLOG_CAT_POOL_EXHAUSTED,
54 : : SOLIDSYSLOG_ADDRESS_ERROR_POOL_EXHAUSTED
55 : : );
56 : : }
57 : 104 : return handle;
58 : : }
59 : :
60 : 319 : static inline struct SolidSyslogAddress* LwipSocketAddress_HandleFromIndex(size_t index)
61 : : {
62 : : static struct SolidSyslogLwipSocketAddress pool[SOLIDSYSLOG_ADDRESS_POOL_SIZE];
63 : 319 : return (struct SolidSyslogAddress*) &pool[index];
64 : : }
65 : :
66 : 104 : void SolidSyslogLwipSocketAddress_Destroy(struct SolidSyslogAddress* base)
67 : : {
68 : 104 : size_t index = LwipSocketAddress_IndexFromHandle(base);
69 [ + + + + ]: 206 : bool released = SolidSyslogPoolAllocator_IndexIsValid(&LwipSocketAddress_Allocator, index) &&
70 : 102 : SolidSyslogPoolAllocator_FreeIfInUse(
71 : : &LwipSocketAddress_Allocator,
72 : : index,
73 : : LwipSocketAddress_CleanupAtIndex,
74 : : NULL
75 : : );
76 [ + + ]: 104 : if (!released)
77 : : {
78 : 3 : LwipSocketAddress_Report(
79 : : SOLIDSYSLOG_UNKNOWN_DESTROY_SEVERITY,
80 : : SOLIDSYSLOG_CAT_UNKNOWN_DESTROY,
81 : : SOLIDSYSLOG_ADDRESS_ERROR_UNKNOWN_DESTROY
82 : : );
83 : : }
84 : 104 : }
85 : :
86 : 104 : static inline size_t LwipSocketAddress_IndexFromHandle(const struct SolidSyslogAddress* base)
87 : : {
88 : 104 : size_t result = SOLIDSYSLOG_ADDRESS_POOL_SIZE;
89 [ + + ]: 119 : for (size_t poolIndex = 0U; poolIndex < SOLIDSYSLOG_ADDRESS_POOL_SIZE; poolIndex++)
90 : : {
91 [ + + ]: 117 : if (base == LwipSocketAddress_HandleFromIndex(poolIndex))
92 : : {
93 : 102 : result = poolIndex;
94 : 102 : break;
95 : : }
96 : : }
97 : 104 : return result;
98 : : }
99 : :
100 : 101 : static inline void LwipSocketAddress_CleanupAtIndex(size_t index, void* context)
101 : : {
102 : : (void) context;
103 : 101 : SolidSyslogLwipSocketAddress_Cleanup(LwipSocketAddress_HandleFromIndex(index));
104 : 101 : }
105 : :
106 : : #else
107 : :
108 : : /* ISO C forbids an empty translation unit. */
109 : : typedef int LwipSocketAddressStatic_EmptyTranslationUnit;
110 : :
111 : : #endif /* LWIP_SOCKET */
|