-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathswitch_core_port_allocator.c
262 lines (216 loc) · 6.86 KB
/
switch_core_port_allocator.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2005-2014, Anthony Minessale II <anthm@freeswitch.org>
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
*
* The Initial Developer of the Original Code is
* Anthony Minessale II <anthm@freeswitch.org>
* Portions created by the Initial Developer are Copyright (C)
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Anthony Minessale II <anthm@freeswitch.org>
* Michael Jerris <mike@jerris.com>
* Paul D. Tinsley <pdt at jackhammer.org>
*
*
* switch_core_port_allocator.c -- Main Core Library (port allocator)
*
*/
#include <switch.h>
#include "private/switch_core_pvt.h"
struct switch_core_port_allocator {
char *ip;
switch_port_t start;
switch_port_t end;
switch_port_t next;
int8_t *track;
uint32_t track_len;
uint32_t track_used;
switch_port_flag_t flags;
switch_mutex_t *mutex;
switch_memory_pool_t *pool;
};
SWITCH_DECLARE(switch_status_t) switch_core_port_allocator_new(const char *ip, switch_port_t start,
switch_port_t end, switch_port_flag_t flags, switch_core_port_allocator_t **new_allocator)
{
switch_status_t status;
switch_memory_pool_t *pool;
switch_core_port_allocator_t *alloc;
int even, odd;
if ((status = switch_core_new_memory_pool(&pool)) != SWITCH_STATUS_SUCCESS) {
return status;
}
if (!(alloc = switch_core_alloc(pool, sizeof(*alloc)))) {
switch_core_destroy_memory_pool(&pool);
return SWITCH_STATUS_MEMERR;
}
alloc->flags = flags;
alloc->ip = switch_core_strdup(pool, ip);
even = switch_test_flag(alloc, SPF_EVEN);
odd = switch_test_flag(alloc, SPF_ODD);
alloc->flags |= runtime.port_alloc_flags;
if (!(even && odd)) {
if (even) {
if ((start % 2) != 0) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Rounding odd start port %d to %d\n", start, start + 1);
start++;
}
if ((end % 2) != 0) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Rounding odd end port %d to %d\n", end, end - 1);
end--;
}
} else if (odd) {
if ((start % 2) == 0) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Rounding even start port %d to %d\n", start, start + 1);
start++;
}
if ((end % 2) == 0) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Rounding even end port %d to %d\n", end, end - 1);
end--;
}
}
}
alloc->track_len = (end - start) + 2;
if (!(even && odd)) {
alloc->track_len /= 2;
}
alloc->track = switch_core_alloc(pool, (alloc->track_len + 2) * sizeof(switch_byte_t));
alloc->start = start;
alloc->next = start;
alloc->end = end;
switch_mutex_init(&alloc->mutex, SWITCH_MUTEX_NESTED, pool);
alloc->pool = pool;
*new_allocator = alloc;
return SWITCH_STATUS_SUCCESS;
}
static switch_bool_t test_port(switch_core_port_allocator_t *alloc, int type, switch_port_t port)
{
switch_memory_pool_t *pool = NULL;
switch_sockaddr_t *local_addr = NULL;
switch_socket_t *sock = NULL;
switch_bool_t r = SWITCH_FALSE;
if (switch_core_new_memory_pool(&pool) != SWITCH_STATUS_SUCCESS) {
return SWITCH_FALSE;
}
if (switch_sockaddr_new(&local_addr, alloc->ip, port, pool) == SWITCH_STATUS_SUCCESS) {
if (switch_socket_create(&sock, switch_sockaddr_get_family(local_addr), type, 0, pool) == SWITCH_STATUS_SUCCESS) {
if (switch_socket_bind(sock, local_addr) == SWITCH_STATUS_SUCCESS) {
r = SWITCH_TRUE;
}
switch_socket_close(sock);
}
}
switch_core_destroy_memory_pool(&pool);
return r;
}
SWITCH_DECLARE(switch_status_t) switch_core_port_allocator_request_port(switch_core_port_allocator_t *alloc, switch_port_t *port_ptr)
{
switch_port_t port = 0;
switch_status_t status = SWITCH_STATUS_FALSE;
int even = switch_test_flag(alloc, SPF_EVEN);
int odd = switch_test_flag(alloc, SPF_ODD);
switch_mutex_lock(alloc->mutex);
srand((unsigned) ((unsigned) (intptr_t) port_ptr + (unsigned) (intptr_t) switch_thread_self() + switch_micro_time_now()));
while (alloc->track_len && alloc->track_used < alloc->track_len) {
uint32_t index;
uint32_t tries = 0;
/* randomly pick a port */
index = switch_rand() % alloc->track_len;
/* if it is used walk up the list to find a free one */
while (alloc->track[index] && tries < alloc->track_len) {
tries++;
if (alloc->track[index] < 0) {
alloc->track[index]++;
}
if (++index >= alloc->track_len) {
index = 0;
}
}
if (tries < alloc->track_len) {
switch_bool_t r = SWITCH_TRUE;
if ((even && odd)) {
port = (switch_port_t) (index + alloc->start);
} else {
port = (switch_port_t) (index + (alloc->start / 2));
port *= 2;
}
if ((alloc->flags & SPF_ROBUST_UDP)) {
r = test_port(alloc, SOCK_DGRAM, port);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "UDP port robustness check for port %d %s\n", port, r ? "pass" : "fail");
}
if ((alloc->flags & SPF_ROBUST_TCP)) {
r = test_port(alloc, SOCK_STREAM, port);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "TCP port robustness check for port %d %s\n", port, r ? "pass" : "fail");
}
if (r) {
alloc->track[index] = 1;
alloc->track_used++;
status = SWITCH_STATUS_SUCCESS;
goto end;
} else {
alloc->track[index] = -4;
}
}
}
end:
switch_mutex_unlock(alloc->mutex);
if (status == SWITCH_STATUS_SUCCESS) {
*port_ptr = port;
} else {
*port_ptr = 0;
}
return status;
}
SWITCH_DECLARE(switch_status_t) switch_core_port_allocator_free_port(switch_core_port_allocator_t *alloc, switch_port_t port)
{
switch_status_t status = SWITCH_STATUS_FALSE;
int even = switch_test_flag(alloc, SPF_EVEN);
int odd = switch_test_flag(alloc, SPF_ODD);
int index;
if (port < alloc->start) {
return SWITCH_STATUS_GENERR;
}
index = port - alloc->start;
if (!(even && odd)) {
index /= 2;
}
switch_mutex_lock(alloc->mutex);
if (alloc->track[index] > 0) {
alloc->track[index] = -4;
alloc->track_used--;
status = SWITCH_STATUS_SUCCESS;
}
switch_mutex_unlock(alloc->mutex);
return status;
}
SWITCH_DECLARE(void) switch_core_port_allocator_destroy(switch_core_port_allocator_t **alloc)
{
switch_memory_pool_t *pool = (*alloc)->pool;
switch_core_destroy_memory_pool(&pool);
*alloc = NULL;
}
/* For Emacs:
* Local Variables:
* mode:c
* indent-tabs-mode:t
* tab-width:4
* c-basic-offset:4
* End:
* For VIM:
* vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
*/