aboutsummaryrefslogtreecommitdiff
path: root/src/lib/daemon_ip_limit.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/daemon_ip_limit.c')
-rw-r--r--src/lib/daemon_ip_limit.c120
1 files changed, 60 insertions, 60 deletions
diff --git a/src/lib/daemon_ip_limit.c b/src/lib/daemon_ip_limit.c
index 4a131c92..21550265 100644
--- a/src/lib/daemon_ip_limit.c
+++ b/src/lib/daemon_ip_limit.c
@@ -89,7 +89,7 @@ get_master (struct MHD_Daemon *daemon)
89static void 89static void
90MHD_ip_count_lock (struct MHD_Daemon *daemon) 90MHD_ip_count_lock (struct MHD_Daemon *daemon)
91{ 91{
92 MHD_mutex_lock_chk_(&daemon->per_ip_connection_mutex); 92 MHD_mutex_lock_chk_ (&daemon->per_ip_connection_mutex);
93} 93}
94 94
95 95
@@ -101,7 +101,7 @@ MHD_ip_count_lock (struct MHD_Daemon *daemon)
101static void 101static void
102MHD_ip_count_unlock (struct MHD_Daemon *daemon) 102MHD_ip_count_unlock (struct MHD_Daemon *daemon)
103{ 103{
104 MHD_mutex_unlock_chk_(&daemon->per_ip_connection_mutex); 104 MHD_mutex_unlock_chk_ (&daemon->per_ip_connection_mutex);
105} 105}
106 106
107 107
@@ -135,37 +135,37 @@ MHD_ip_addr_compare (const void *a1,
135 */ 135 */
136static int 136static int
137MHD_ip_addr_to_key (const struct sockaddr *addr, 137MHD_ip_addr_to_key (const struct sockaddr *addr,
138 socklen_t addrlen, 138 socklen_t addrlen,
139 struct MHD_IPCount *key) 139 struct MHD_IPCount *key)
140{ 140{
141 memset(key, 141 memset (key,
142 0, 142 0,
143 sizeof(*key)); 143 sizeof(*key));
144 144
145 /* IPv4 addresses */ 145 /* IPv4 addresses */
146 if (sizeof (struct sockaddr_in) == addrlen) 146 if (sizeof (struct sockaddr_in) == addrlen)
147 { 147 {
148 const struct sockaddr_in *addr4 = (const struct sockaddr_in*) addr; 148 const struct sockaddr_in *addr4 = (const struct sockaddr_in*) addr;
149 149
150 key->family = AF_INET; 150 key->family = AF_INET;
151 memcpy (&key->addr.ipv4, 151 memcpy (&key->addr.ipv4,
152 &addr4->sin_addr, 152 &addr4->sin_addr,
153 sizeof(addr4->sin_addr)); 153 sizeof(addr4->sin_addr));
154 return MHD_YES; 154 return MHD_YES;
155 } 155 }
156 156
157#if HAVE_INET6 157#if HAVE_INET6
158 /* IPv6 addresses */ 158 /* IPv6 addresses */
159 if (sizeof (struct sockaddr_in6) == addrlen) 159 if (sizeof (struct sockaddr_in6) == addrlen)
160 { 160 {
161 const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6*) addr; 161 const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6*) addr;
162 162
163 key->family = AF_INET6; 163 key->family = AF_INET6;
164 memcpy (&key->addr.ipv6, 164 memcpy (&key->addr.ipv6,
165 &addr6->sin6_addr, 165 &addr6->sin6_addr,
166 sizeof(addr6->sin6_addr)); 166 sizeof(addr6->sin6_addr));
167 return MHD_YES; 167 return MHD_YES;
168 } 168 }
169#endif 169#endif
170 170
171 /* Some other address */ 171 /* Some other address */
@@ -186,8 +186,8 @@ MHD_ip_addr_to_key (const struct sockaddr *addr,
186 */ 186 */
187int 187int
188MHD_ip_limit_add (struct MHD_Daemon *daemon, 188MHD_ip_limit_add (struct MHD_Daemon *daemon,
189 const struct sockaddr *addr, 189 const struct sockaddr *addr,
190 socklen_t addrlen) 190 socklen_t addrlen)
191{ 191{
192 struct MHD_IPCount *key; 192 struct MHD_IPCount *key;
193 void **nodep; 193 void **nodep;
@@ -206,31 +206,31 @@ MHD_ip_limit_add (struct MHD_Daemon *daemon,
206 if (MHD_NO == MHD_ip_addr_to_key (addr, 206 if (MHD_NO == MHD_ip_addr_to_key (addr,
207 addrlen, 207 addrlen,
208 key)) 208 key))
209 { 209 {
210 /* Allow unhandled address types through */ 210 /* Allow unhandled address types through */
211 free (key); 211 free (key);
212 return MHD_YES; 212 return MHD_YES;
213 } 213 }
214 MHD_ip_count_lock (daemon); 214 MHD_ip_count_lock (daemon);
215 215
216 /* Search for the IP address */ 216 /* Search for the IP address */
217 if (NULL == (nodep = tsearch (key, 217 if (NULL == (nodep = tsearch (key,
218 &daemon->per_ip_connection_count, 218 &daemon->per_ip_connection_count,
219 &MHD_ip_addr_compare))) 219 &MHD_ip_addr_compare)))
220 { 220 {
221#ifdef HAVE_MESSAGES 221#ifdef HAVE_MESSAGES
222 MHD_DLOG (daemon, 222 MHD_DLOG (daemon,
223 MHD_SC_IP_COUNTER_FAILURE, 223 MHD_SC_IP_COUNTER_FAILURE,
224 _("Failed to add IP connection count node\n")); 224 _ ("Failed to add IP connection count node\n"));
225#endif 225#endif
226 MHD_ip_count_unlock (daemon); 226 MHD_ip_count_unlock (daemon);
227 free (key); 227 free (key);
228 return MHD_NO; 228 return MHD_NO;
229 } 229 }
230 node = *nodep; 230 node = *nodep;
231 /* If we got an existing node back, free the one we created */ 231 /* If we got an existing node back, free the one we created */
232 if (node != key) 232 if (node != key)
233 free(key); 233 free (key);
234 key = (struct MHD_IPCount *) node; 234 key = (struct MHD_IPCount *) node;
235 /* Test if there is room for another connection; if so, 235 /* Test if there is room for another connection; if so,
236 * increment count */ 236 * increment count */
@@ -253,8 +253,8 @@ MHD_ip_limit_add (struct MHD_Daemon *daemon,
253 */ 253 */
254void 254void
255MHD_ip_limit_del (struct MHD_Daemon *daemon, 255MHD_ip_limit_del (struct MHD_Daemon *daemon,
256 const struct sockaddr *addr, 256 const struct sockaddr *addr,
257 socklen_t addrlen) 257 socklen_t addrlen)
258{ 258{
259 struct MHD_IPCount search_key; 259 struct MHD_IPCount search_key;
260 struct MHD_IPCount *found_key; 260 struct MHD_IPCount *found_key;
@@ -274,27 +274,27 @@ MHD_ip_limit_del (struct MHD_Daemon *daemon,
274 274
275 /* Search for the IP address */ 275 /* Search for the IP address */
276 if (NULL == (nodep = tfind (&search_key, 276 if (NULL == (nodep = tfind (&search_key,
277 &daemon->per_ip_connection_count, 277 &daemon->per_ip_connection_count,
278 &MHD_ip_addr_compare))) 278 &MHD_ip_addr_compare)))
279 { 279 {
280 /* Something's wrong if we couldn't find an IP address 280 /* Something's wrong if we couldn't find an IP address
281 * that was previously added */ 281 * that was previously added */
282 MHD_PANIC (_("Failed to find previously-added IP address\n")); 282 MHD_PANIC (_ ("Failed to find previously-added IP address\n"));
283 } 283 }
284 found_key = (struct MHD_IPCount *) *nodep; 284 found_key = (struct MHD_IPCount *) *nodep;
285 /* Validate existing count for IP address */ 285 /* Validate existing count for IP address */
286 if (0 == found_key->count) 286 if (0 == found_key->count)
287 { 287 {
288 MHD_PANIC (_("Previously-added IP address had counter of zero\n")); 288 MHD_PANIC (_ ("Previously-added IP address had counter of zero\n"));
289 } 289 }
290 /* Remove the node entirely if count reduces to 0 */ 290 /* Remove the node entirely if count reduces to 0 */
291 if (0 == --found_key->count) 291 if (0 == --found_key->count)
292 { 292 {
293 tdelete (found_key, 293 tdelete (found_key,
294 &daemon->per_ip_connection_count, 294 &daemon->per_ip_connection_count,
295 &MHD_ip_addr_compare); 295 &MHD_ip_addr_compare);
296 free (found_key); 296 free (found_key);
297 } 297 }
298 298
299 MHD_ip_count_unlock (daemon); 299 MHD_ip_count_unlock (daemon);
300} 300}