mdns_addr_tailq.h revision 1.1 1 /*
2 * Copyright (c) 2021 Apple Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef MDNS_ADDR_TAIL_QUEUE_H
18 #define MDNS_ADDR_TAIL_QUEUE_H
19
20 //======================================================================================================================
21 // MARK: - Headers
22
23 #include "nullability.h" // For NULLABLE and NONNULL.
24 #include "bsd_queue.h" // For STAILQ.
25 #include <stdlib.h>
26 #include <stdbool.h> // For bool.
27 #include "mDNSEmbeddedAPI.h" // For mDNSAddr, mDNSIPPort and mStatus.
28 #if defined(POSIX_BUILD)
29 #include "DebugServices.h"
30 #endif // defined(POSIX_BUILD)
31
32 //======================================================================================================================
33 // MARK: - Structures
34
35 typedef struct mdns_addr_with_port mdns_addr_with_port_t;
36 struct mdns_addr_with_port {
37 STAILQ_ENTRY(mdns_addr_with_port) __entries; // The entry used by STAILQ to form the linked list.
38 mDNSAddr address; // IPv4 or IPv6 address.
39 mDNSIPPort port; // The port number that is associated with the address above.
40 };
41
42 typedef struct mdns_addr_tailq mdns_addr_tailq_t;
43 // The tail queue struct mdns_addr_tailq that contains struct mdns_addr_with_port.
44 STAILQ_HEAD(mdns_addr_tailq, mdns_addr_with_port);
45
46 //======================================================================================================================
47 // MARK: - Function Declarations
48
49 /*!
50 * @brief
51 * Creates a tail queue that contains IP address and port number.
52 *
53 * @result
54 * A pointer to the tail queue or NULL if the system was out of memory.
55 */
56 mdns_addr_tailq_t * NULLABLE
57 mdns_addr_tailq_create(void);
58
59 /*!
60 * @brief
61 * Disposes a tail queue that is created by <code>mdns_addr_tailq_create()</code>.
62 *
63 * @param me
64 * The tail queue that will be disposed.
65 *
66 * @discussion
67 * Use <code>MDNS_DISPOSE_MDNS_ADDR_TAILQ()</code> to dispose a tail queue safely.
68 */
69 void
70 mdns_addr_tailq_dispose(mdns_addr_tailq_t * NONNULL me);
71 #define MDNS_DISPOSE_MDNS_ADDR_TAILQ(obj) _MDNS_STRICT_DISPOSE_TEMPLATE(obj, mdns_addr_tailq_dispose)
72
73 /*!
74 * @brief
75 * Checks if the current tail queue is empty.
76 *
77 * @param me
78 * The tail queue to be checked.
79 *
80 * @result
81 * True if the tail queue does not contain any element. Otherwise, false.
82 */
83 bool
84 mdns_addr_tailq_empty(const mdns_addr_tailq_t * NONNULL me);
85
86 /*!
87 * @brief
88 * Adds an IP address with its corresponding port number into the front of the tail queue.
89 *
90 * @param me
91 * The tail queue where the address and port number to be added into.
92 *
93 * @param address
94 * The IP address to be added.
95 *
96 * @param port
97 * The corresponding port number of the added address to be added.
98 *
99 * @result
100 * The added address with its port number in the front of the tail queue, or NULL if the system was out of memory.
101 */
102 const mdns_addr_with_port_t * NULLABLE
103 mdns_addr_tailq_add_front(mdns_addr_tailq_t * NONNULL me, const mDNSAddr * NONNULL address, mDNSIPPort port);
104
105 /*!
106 * @brief
107 * Adds an IP address with its corresponding port number into the back of the tail queue.
108 *
109 * @param me
110 * The tail queue where the address and port number to be added into.
111 *
112 * @param address
113 * The IP address to be added.
114 *
115 * @param port
116 * The corresponding port number of the added address to be added.
117 *
118 * @result
119 * The added address with its port number in the back of the tail queue, or NULL if the system was out of memory.
120 */
121 const mdns_addr_with_port_t * NULLABLE
122 mdns_addr_tailq_add_back(mdns_addr_tailq_t * NONNULL me, const mDNSAddr * NONNULL address, mDNSIPPort port);
123
124 /*!
125 * @brief
126 * Gets the pointer to mdns_addr_with_port_t (which contains the IP address and port number added before) in the front of the tail queue.
127 *
128 * @param me
129 * The tail queue where the mdns_addr_with_port_t to be get from.
130 *
131 * @result
132 * The pointer to the mdns_addr_with_port_t in the front of the tail queue, or NULL if the tail queue is empty.
133 */
134 const mdns_addr_with_port_t * NULLABLE
135 mdns_addr_tailq_get_front(const mdns_addr_tailq_t * NONNULL me);
136
137 /*!
138 * @brief
139 * Removes the first IP address and its port number in the front of the tail queue.
140 *
141 * @param me
142 * The tail queue where the IP address and its port number to be removed.
143 */
144 void
145 mdns_addr_tailq_remove_front(mdns_addr_tailq_t * NONNULL me);
146
147 /*!
148 * @brief
149 * Removes mdns_addr_with_port_t object in the tail queue with the IP address and port number specified in the parameters.
150 *
151 * @param me
152 * The tail queue where the IP address and its port number to be removed.
153 *
154 * @param address
155 * The IP address to be matched.
156 *
157 * @param port
158 * The corresponding port number of the IP address to be matched.
159 *
160 * @result
161 * True if the mdns_addr_with_port_t object is found and removed from the tail queue, for the IP address and port number specified in the parameters.
162 * False if such mdns_addr_with_port_t object does not exist in the tail queue.
163 */
164 bool
165 mdns_addr_tailq_remove(mdns_addr_tailq_t * NONNULL me, const mDNSAddr * NONNULL address, mDNSIPPort port);
166
167 /*!
168 * @brief
169 * Checks if two mdns_addr_with_port_t object are equal in value.
170 *
171 * @param addr_1
172 * mdns_addr_with_port_t object to be compared.
173 *
174 * @param addr_2
175 * mdns_addr_with_port_t object to be compared.
176 *
177 * @result
178 * True if two mdns_addr_with_port_t are equal in value, otherwise, false.
179 */
180 bool
181 mdns_addr_with_port_equal(const mdns_addr_with_port_t * NONNULL addr_1, const mdns_addr_with_port_t * NONNULL addr_2);
182
183 #endif // MDNS_ADDR_TAIL_QUEUE_H
184