1 /*
2 * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
3 * Copyright (C) 2005-2015, Anthony Minessale II <anthm@freeswitch.org>
4 *
5 * Version: MPL 1.1
6 *
7 * The contents of this file are subject to the Mozilla Public License Version
8 * 1.1 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/F
11 *
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 * for the specific language governing rights and limitations under the
15 * License.
16 *
17 * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
18 *
19 * The Initial Developer of the Original Code is
20 * Michael Jerris <mike@jerris.com>
21 * Portions created by the Initial Developer are Copyright (C)
22 * the Initial Developer. All Rights Reserved.
23 *
24 * Contributor(s):
25 *
26 * Michael Jerris <mike@jerris.com>
27 * Eliot Gable <egable@gmail.com>
28 * William King <william.king@quentustech.com>
29 *
30 * switch_apr.c -- apr wrappers and extensions
31 *
32 */
33
34 #include <switch.h>
35 #ifndef WIN32
36 #include <switch_private.h>
37 #endif
38 #include "private/switch_core_pvt.h"
39
40 /* apr headers*/
41 #include <apr.h>
42 #include <apr_atomic.h>
43 #include <apr_pools.h>
44 #include <apr_hash.h>
45 #include <apr_network_io.h>
46 #include <apr_errno.h>
47 #include <apr_thread_proc.h>
48 #include <apr_portable.h>
49 #include <apr_thread_mutex.h>
50 #include <apr_thread_cond.h>
51 #include <apr_thread_rwlock.h>
52 #include <apr_file_io.h>
53 #include <apr_poll.h>
54 #include <apr_strings.h>
55 #define APR_WANT_STDIO
56 #define APR_WANT_STRFUNC
57 #include <apr_want.h>
58 #include <apr_file_info.h>
59 #include <apr_fnmatch.h>
60 #include <apr_tables.h>
61
62 #ifdef WIN32
63 #include "apr_arch_networkio.h"
64 /* Missing socket symbols */
65 #ifndef SOL_TCP
66 #define SOL_TCP IPPROTO_TCP
67 #endif
68 #endif
69
70 /* apr_vformatter_buff_t definition*/
71 #include <apr_lib.h>
72
73 /* apr-util headers */
74 #include <apr_queue.h>
75 #include <apr_uuid.h>
76 #if (defined(HAVE_LIBMD5) || defined(HAVE_LIBMD) || defined(HAVE_MD5INIT))
77 #include <md5.h>
78 #elif defined(HAVE_LIBCRYPTO)
79 #include <openssl/md5.h>
80 #else
81 #include <apr_md5.h>
82 #endif
83
84 /* apr stubs */
85
switch_status_is_timeup(int status)86 SWITCH_DECLARE(int) switch_status_is_timeup(int status)
87 {
88 return APR_STATUS_IS_TIMEUP(status);
89 }
90
91 /* Memory Pools */
92
switch_thread_self(void)93 SWITCH_DECLARE(switch_thread_id_t) switch_thread_self(void)
94 {
95 #ifndef WIN32
96 return apr_os_thread_current();
97 #else
98 return (switch_thread_id_t) (GetCurrentThreadId());
99 #endif
100 }
101
switch_thread_equal(switch_thread_id_t tid1,switch_thread_id_t tid2)102 SWITCH_DECLARE(int) switch_thread_equal(switch_thread_id_t tid1, switch_thread_id_t tid2)
103 {
104 #ifdef WIN32
105 return (tid1 == tid2);
106 #else
107 return apr_os_thread_equal(tid1, tid2);
108 #endif
109
110 }
111
switch_ci_hashfunc_default(const char * char_key,switch_ssize_t * klen)112 SWITCH_DECLARE(unsigned int) switch_ci_hashfunc_default(const char *char_key, switch_ssize_t *klen)
113 {
114 unsigned int hash = 0;
115 const unsigned char *key = (const unsigned char *) char_key;
116 const unsigned char *p;
117 apr_ssize_t i;
118
119 if (*klen == APR_HASH_KEY_STRING) {
120 for (p = key; *p; p++) {
121 hash = hash * 33 + tolower(*p);
122 }
123 *klen = p - key;
124 } else {
125 for (p = key, i = *klen; i; i--, p++) {
126 hash = hash * 33 + tolower(*p);
127 }
128 }
129
130 return hash;
131 }
132
133
switch_hashfunc_default(const char * key,switch_ssize_t * klen)134 SWITCH_DECLARE(unsigned int) switch_hashfunc_default(const char *key, switch_ssize_t *klen)
135 {
136 return apr_hashfunc_default(key, klen);
137 }
138
139 /* string functions */
140
switch_strftime(char * s,switch_size_t * retsize,switch_size_t max,const char * format,switch_time_exp_t * tm)141 SWITCH_DECLARE(switch_status_t) switch_strftime(char *s, switch_size_t *retsize, switch_size_t max, const char *format, switch_time_exp_t *tm)
142 {
143 const char *p = format;
144
145 if (!p)
146 return SWITCH_STATUS_FALSE;
147
148 while (*p) {
149 if (*p == '%') {
150 switch (*(++p)) {
151 case 'C':
152 case 'D':
153 case 'r':
154 case 'R':
155 case 'T':
156 case 'e':
157 case 'a':
158 case 'A':
159 case 'b':
160 case 'B':
161 case 'c':
162 case 'd':
163 case 'H':
164 case 'I':
165 case 'j':
166 case 'm':
167 case 'M':
168 case 'p':
169 case 'S':
170 case 'U':
171 case 'w':
172 case 'W':
173 case 'x':
174 case 'X':
175 case 'y':
176 case 'Y':
177 case 'z':
178 case 'Z':
179 case '%':
180 p++;
181 continue;
182 case '\0':
183 default:
184 return SWITCH_STATUS_FALSE;
185 }
186 }
187 p++;
188 }
189
190 return apr_strftime(s, retsize, max, format, (apr_time_exp_t *) tm);
191 }
192
switch_strftime_nocheck(char * s,switch_size_t * retsize,switch_size_t max,const char * format,switch_time_exp_t * tm)193 SWITCH_DECLARE(switch_status_t) switch_strftime_nocheck(char *s, switch_size_t *retsize, switch_size_t max, const char *format, switch_time_exp_t *tm)
194 {
195 return apr_strftime(s, retsize, max, format, (apr_time_exp_t *) tm);
196 }
197
switch_snprintf(char * buf,switch_size_t len,const char * format,...)198 SWITCH_DECLARE(int) switch_snprintf(char *buf, switch_size_t len, const char *format, ...)
199 {
200 va_list ap;
201 int ret;
202 va_start(ap, format);
203 ret = apr_vsnprintf(buf, len, format, ap);
204 va_end(ap);
205 return ret;
206 }
207
switch_vsnprintf(char * buf,switch_size_t len,const char * format,va_list ap)208 SWITCH_DECLARE(int) switch_vsnprintf(char *buf, switch_size_t len, const char *format, va_list ap)
209 {
210 return apr_vsnprintf(buf, len, format, ap);
211 }
212
switch_copy_string(char * dst,const char * src,switch_size_t dst_size)213 SWITCH_DECLARE(char *) switch_copy_string(char *dst, const char *src, switch_size_t dst_size)
214 {
215 if (!dst)
216 return NULL;
217 if (!src) {
218 *dst = '\0';
219 return dst;
220 }
221 return apr_cpystrn(dst, src, dst_size);
222 }
223
224 /* thread read write lock functions */
225
switch_thread_rwlock_create(switch_thread_rwlock_t ** rwlock,switch_memory_pool_t * pool)226 SWITCH_DECLARE(switch_status_t) switch_thread_rwlock_create(switch_thread_rwlock_t ** rwlock, switch_memory_pool_t *pool)
227 {
228 return apr_thread_rwlock_create(rwlock, pool);
229 }
230
switch_thread_rwlock_destroy(switch_thread_rwlock_t * rwlock)231 SWITCH_DECLARE(switch_status_t) switch_thread_rwlock_destroy(switch_thread_rwlock_t *rwlock)
232 {
233 return apr_thread_rwlock_destroy(rwlock);
234 }
235
switch_thread_rwlock_pool_get(switch_thread_rwlock_t * rwlock)236 SWITCH_DECLARE(switch_memory_pool_t *) switch_thread_rwlock_pool_get(switch_thread_rwlock_t *rwlock)
237 {
238 return apr_thread_rwlock_pool_get(rwlock);
239 }
240
switch_thread_rwlock_rdlock(switch_thread_rwlock_t * rwlock)241 SWITCH_DECLARE(switch_status_t) switch_thread_rwlock_rdlock(switch_thread_rwlock_t *rwlock)
242 {
243 return apr_thread_rwlock_rdlock(rwlock);
244 }
245
switch_thread_rwlock_tryrdlock(switch_thread_rwlock_t * rwlock)246 SWITCH_DECLARE(switch_status_t) switch_thread_rwlock_tryrdlock(switch_thread_rwlock_t *rwlock)
247 {
248 return apr_thread_rwlock_tryrdlock(rwlock);
249 }
250
switch_thread_rwlock_wrlock(switch_thread_rwlock_t * rwlock)251 SWITCH_DECLARE(switch_status_t) switch_thread_rwlock_wrlock(switch_thread_rwlock_t *rwlock)
252 {
253 return apr_thread_rwlock_wrlock(rwlock);
254 }
255
switch_thread_rwlock_trywrlock(switch_thread_rwlock_t * rwlock)256 SWITCH_DECLARE(switch_status_t) switch_thread_rwlock_trywrlock(switch_thread_rwlock_t *rwlock)
257 {
258 return apr_thread_rwlock_trywrlock(rwlock);
259 }
260
switch_thread_rwlock_trywrlock_timeout(switch_thread_rwlock_t * rwlock,int timeout)261 SWITCH_DECLARE(switch_status_t) switch_thread_rwlock_trywrlock_timeout(switch_thread_rwlock_t *rwlock, int timeout)
262 {
263 int sanity = timeout * 2;
264
265 while (sanity) {
266 if (switch_thread_rwlock_trywrlock(rwlock) == SWITCH_STATUS_SUCCESS) {
267 return SWITCH_STATUS_SUCCESS;
268 }
269 sanity--;
270 switch_yield(500000);
271 }
272
273 return SWITCH_STATUS_FALSE;
274 }
275
276
switch_thread_rwlock_unlock(switch_thread_rwlock_t * rwlock)277 SWITCH_DECLARE(switch_status_t) switch_thread_rwlock_unlock(switch_thread_rwlock_t *rwlock)
278 {
279 return apr_thread_rwlock_unlock(rwlock);
280 }
281
282 /* thread mutex functions */
283
switch_mutex_init(switch_mutex_t ** lock,unsigned int flags,switch_memory_pool_t * pool)284 SWITCH_DECLARE(switch_status_t) switch_mutex_init(switch_mutex_t ** lock, unsigned int flags, switch_memory_pool_t *pool)
285 {
286 #ifdef WIN32
287 /* Old version of APR misunderstands mutexes. On Windows, mutexes are cross-process.
288 APR has no reason to not use critical sections instead of mutexes. */
289 if (flags == SWITCH_MUTEX_NESTED) flags = SWITCH_MUTEX_DEFAULT;
290 #endif
291 return apr_thread_mutex_create(lock, flags, pool);
292 }
293
switch_mutex_destroy(switch_mutex_t * lock)294 SWITCH_DECLARE(switch_status_t) switch_mutex_destroy(switch_mutex_t *lock)
295 {
296 return apr_thread_mutex_destroy(lock);
297 }
298
switch_mutex_lock(switch_mutex_t * lock)299 SWITCH_DECLARE(switch_status_t) switch_mutex_lock(switch_mutex_t *lock)
300 {
301 return apr_thread_mutex_lock(lock);
302 }
303
switch_mutex_unlock(switch_mutex_t * lock)304 SWITCH_DECLARE(switch_status_t) switch_mutex_unlock(switch_mutex_t *lock)
305 {
306 return apr_thread_mutex_unlock(lock);
307 }
308
switch_mutex_trylock(switch_mutex_t * lock)309 SWITCH_DECLARE(switch_status_t) switch_mutex_trylock(switch_mutex_t *lock)
310 {
311 return apr_thread_mutex_trylock(lock);
312 }
313
314 /* time function stubs */
315
switch_time_now(void)316 SWITCH_DECLARE(switch_time_t) switch_time_now(void)
317 {
318 #if defined(HAVE_CLOCK_GETTIME) && defined(SWITCH_USE_CLOCK_FUNCS)
319 struct timespec ts;
320 clock_gettime(CLOCK_REALTIME, &ts);
321 return ts.tv_sec * APR_USEC_PER_SEC + (ts.tv_nsec / 1000);
322 #else
323 return (switch_time_t) apr_time_now();
324 #endif
325 }
326
switch_time_exp_gmt_get(switch_time_t * result,switch_time_exp_t * input)327 SWITCH_DECLARE(switch_status_t) switch_time_exp_gmt_get(switch_time_t *result, switch_time_exp_t *input)
328 {
329 return apr_time_exp_gmt_get((apr_time_t *) result, (apr_time_exp_t *) input);
330 }
331
switch_time_exp_get(switch_time_t * result,switch_time_exp_t * input)332 SWITCH_DECLARE(switch_status_t) switch_time_exp_get(switch_time_t *result, switch_time_exp_t *input)
333 {
334 return apr_time_exp_get((apr_time_t *) result, (apr_time_exp_t *) input);
335 }
336
switch_time_exp_lt(switch_time_exp_t * result,switch_time_t input)337 SWITCH_DECLARE(switch_status_t) switch_time_exp_lt(switch_time_exp_t *result, switch_time_t input)
338 {
339 return apr_time_exp_lt((apr_time_exp_t *) result, input);
340 }
341
switch_time_exp_tz(switch_time_exp_t * result,switch_time_t input,switch_int32_t offs)342 SWITCH_DECLARE(switch_status_t) switch_time_exp_tz(switch_time_exp_t *result, switch_time_t input, switch_int32_t offs)
343 {
344 return apr_time_exp_tz((apr_time_exp_t *) result, input, (apr_int32_t) offs);
345 }
346
switch_time_exp_gmt(switch_time_exp_t * result,switch_time_t input)347 SWITCH_DECLARE(switch_status_t) switch_time_exp_gmt(switch_time_exp_t *result, switch_time_t input)
348 {
349 return apr_time_exp_gmt((apr_time_exp_t *) result, input);
350 }
351
switch_rfc822_date(char * date_str,switch_time_t t)352 SWITCH_DECLARE(switch_status_t) switch_rfc822_date(char *date_str, switch_time_t t)
353 {
354 return apr_rfc822_date(date_str, t);
355 }
356
switch_time_make(switch_time_t sec,int32_t usec)357 SWITCH_DECLARE(switch_time_t) switch_time_make(switch_time_t sec, int32_t usec)
358 {
359 return ((switch_time_t) (sec) * APR_USEC_PER_SEC + (switch_time_t) (usec));
360 }
361
362 /* Thread condition locks */
363
switch_thread_cond_create(switch_thread_cond_t ** cond,switch_memory_pool_t * pool)364 SWITCH_DECLARE(switch_status_t) switch_thread_cond_create(switch_thread_cond_t ** cond, switch_memory_pool_t *pool)
365 {
366 return apr_thread_cond_create(cond, pool);
367 }
368
switch_thread_cond_wait(switch_thread_cond_t * cond,switch_mutex_t * mutex)369 SWITCH_DECLARE(switch_status_t) switch_thread_cond_wait(switch_thread_cond_t *cond, switch_mutex_t *mutex)
370 {
371 return apr_thread_cond_wait(cond, mutex);
372 }
373
switch_thread_cond_timedwait(switch_thread_cond_t * cond,switch_mutex_t * mutex,switch_interval_time_t timeout)374 SWITCH_DECLARE(switch_status_t) switch_thread_cond_timedwait(switch_thread_cond_t *cond, switch_mutex_t *mutex, switch_interval_time_t timeout)
375 {
376 apr_status_t st = apr_thread_cond_timedwait(cond, mutex, timeout);
377
378 if (st == APR_TIMEUP) {
379 st = SWITCH_STATUS_TIMEOUT;
380 }
381
382 return st;
383 }
384
switch_thread_cond_signal(switch_thread_cond_t * cond)385 SWITCH_DECLARE(switch_status_t) switch_thread_cond_signal(switch_thread_cond_t *cond)
386 {
387 return apr_thread_cond_signal(cond);
388 }
389
switch_thread_cond_broadcast(switch_thread_cond_t * cond)390 SWITCH_DECLARE(switch_status_t) switch_thread_cond_broadcast(switch_thread_cond_t *cond)
391 {
392 return apr_thread_cond_broadcast(cond);
393 }
394
switch_thread_cond_destroy(switch_thread_cond_t * cond)395 SWITCH_DECLARE(switch_status_t) switch_thread_cond_destroy(switch_thread_cond_t *cond)
396 {
397 return apr_thread_cond_destroy(cond);
398 }
399
400 /* file i/o stubs */
401
switch_file_open(switch_file_t ** newf,const char * fname,int32_t flag,switch_fileperms_t perm,switch_memory_pool_t * pool)402 SWITCH_DECLARE(switch_status_t) switch_file_open(switch_file_t ** newf, const char *fname, int32_t flag, switch_fileperms_t perm,
403 switch_memory_pool_t *pool)
404 {
405 return apr_file_open(newf, fname, flag, perm, pool);
406 }
407
switch_file_seek(switch_file_t * thefile,switch_seek_where_t where,int64_t * offset)408 SWITCH_DECLARE(switch_status_t) switch_file_seek(switch_file_t *thefile, switch_seek_where_t where, int64_t *offset)
409 {
410 apr_status_t rv;
411 apr_off_t off = (apr_off_t) (*offset);
412 rv = apr_file_seek(thefile, where, &off);
413 *offset = (int64_t) off;
414 return rv;
415 }
416
switch_file_copy(const char * from_path,const char * to_path,switch_fileperms_t perms,switch_memory_pool_t * pool)417 SWITCH_DECLARE(switch_status_t) switch_file_copy(const char *from_path, const char *to_path, switch_fileperms_t perms, switch_memory_pool_t *pool)
418 {
419 return apr_file_copy(from_path, to_path, perms, pool);
420 }
421
422
switch_file_close(switch_file_t * thefile)423 SWITCH_DECLARE(switch_status_t) switch_file_close(switch_file_t *thefile)
424 {
425 return apr_file_close(thefile);
426 }
427
switch_file_trunc(switch_file_t * thefile,int64_t offset)428 SWITCH_DECLARE(switch_status_t) switch_file_trunc(switch_file_t *thefile, int64_t offset)
429 {
430 return apr_file_trunc(thefile, offset);
431 }
432
switch_file_lock(switch_file_t * thefile,int type)433 SWITCH_DECLARE(switch_status_t) switch_file_lock(switch_file_t *thefile, int type)
434 {
435 return apr_file_lock(thefile, type);
436 }
437
switch_file_rename(const char * from_path,const char * to_path,switch_memory_pool_t * pool)438 SWITCH_DECLARE(switch_status_t) switch_file_rename(const char *from_path, const char *to_path, switch_memory_pool_t *pool)
439 {
440 return apr_file_rename(from_path, to_path, pool);
441 }
442
switch_file_remove(const char * path,switch_memory_pool_t * pool)443 SWITCH_DECLARE(switch_status_t) switch_file_remove(const char *path, switch_memory_pool_t *pool)
444 {
445 return apr_file_remove(path, pool);
446 }
447
switch_file_read(switch_file_t * thefile,void * buf,switch_size_t * nbytes)448 SWITCH_DECLARE(switch_status_t) switch_file_read(switch_file_t *thefile, void *buf, switch_size_t *nbytes)
449 {
450 return apr_file_read(thefile, buf, nbytes);
451 }
452
switch_file_write(switch_file_t * thefile,const void * buf,switch_size_t * nbytes)453 SWITCH_DECLARE(switch_status_t) switch_file_write(switch_file_t *thefile, const void *buf, switch_size_t *nbytes)
454 {
455 return apr_file_write(thefile, buf, nbytes);
456 }
457
switch_file_printf(switch_file_t * thefile,const char * format,...)458 SWITCH_DECLARE(int) switch_file_printf(switch_file_t *thefile, const char *format, ...)
459 {
460 va_list ap;
461 int ret;
462 char *data;
463
464 va_start(ap, format);
465
466 if ((ret = switch_vasprintf(&data, format, ap)) != -1) {
467 switch_size_t bytes = strlen(data);
468 switch_file_write(thefile, data, &bytes);
469 free(data);
470 }
471
472 va_end(ap);
473
474 return ret;
475 }
476
switch_file_mktemp(switch_file_t ** thefile,char * templ,int32_t flags,switch_memory_pool_t * pool)477 SWITCH_DECLARE(switch_status_t) switch_file_mktemp(switch_file_t ** thefile, char *templ, int32_t flags, switch_memory_pool_t *pool)
478 {
479 return apr_file_mktemp(thefile, templ, flags, pool);
480 }
481
switch_file_get_size(switch_file_t * thefile)482 SWITCH_DECLARE(switch_size_t) switch_file_get_size(switch_file_t *thefile)
483 {
484 struct apr_finfo_t finfo;
485 return apr_file_info_get(&finfo, APR_FINFO_SIZE, thefile) == SWITCH_STATUS_SUCCESS ? (switch_size_t) finfo.size : 0;
486 }
487
switch_directory_exists(const char * dirname,switch_memory_pool_t * pool)488 SWITCH_DECLARE(switch_status_t) switch_directory_exists(const char *dirname, switch_memory_pool_t *pool)
489 {
490 apr_dir_t *dir_handle;
491 switch_memory_pool_t *our_pool = NULL;
492 switch_status_t status;
493
494 if (!pool) {
495 switch_core_new_memory_pool(&our_pool);
496 pool = our_pool;
497 }
498
499 if ((status = apr_dir_open(&dir_handle, dirname, pool)) == APR_SUCCESS) {
500 apr_dir_close(dir_handle);
501 }
502
503 if (our_pool) {
504 switch_core_destroy_memory_pool(&our_pool);
505 }
506
507 return status;
508 }
509
switch_file_exists(const char * filename,switch_memory_pool_t * pool)510 SWITCH_DECLARE(switch_status_t) switch_file_exists(const char *filename, switch_memory_pool_t *pool)
511 {
512 int32_t wanted = APR_FINFO_TYPE;
513 switch_memory_pool_t *our_pool = NULL;
514 switch_status_t status = SWITCH_STATUS_FALSE;
515 apr_finfo_t info = { 0 };
516
517 if (zstr(filename)) {
518 return status;
519 }
520
521 if (!pool) {
522 switch_core_new_memory_pool(&our_pool);
523 }
524
525 apr_stat(&info, filename, wanted, pool ? pool : our_pool);
526 if (info.filetype != APR_NOFILE) {
527 status = SWITCH_STATUS_SUCCESS;
528 }
529
530 if (our_pool) {
531 switch_core_destroy_memory_pool(&our_pool);
532 }
533
534 return status;
535 }
536
switch_dir_make(const char * path,switch_fileperms_t perm,switch_memory_pool_t * pool)537 SWITCH_DECLARE(switch_status_t) switch_dir_make(const char *path, switch_fileperms_t perm, switch_memory_pool_t *pool)
538 {
539 return apr_dir_make(path, perm, pool);
540 }
541
switch_dir_make_recursive(const char * path,switch_fileperms_t perm,switch_memory_pool_t * pool)542 SWITCH_DECLARE(switch_status_t) switch_dir_make_recursive(const char *path, switch_fileperms_t perm, switch_memory_pool_t *pool)
543 {
544 return apr_dir_make_recursive(path, perm, pool);
545 }
546
547 struct switch_dir {
548 apr_dir_t *dir_handle;
549 apr_finfo_t finfo;
550 };
551
switch_dir_open(switch_dir_t ** new_dir,const char * dirname,switch_memory_pool_t * pool)552 SWITCH_DECLARE(switch_status_t) switch_dir_open(switch_dir_t ** new_dir, const char *dirname, switch_memory_pool_t *pool)
553 {
554 switch_status_t status;
555 switch_dir_t *dir = malloc(sizeof(*dir));
556
557 if (!dir) {
558 *new_dir = NULL;
559 return SWITCH_STATUS_FALSE;
560 }
561
562 memset(dir, 0, sizeof(*dir));
563 if ((status = apr_dir_open(&(dir->dir_handle), dirname, pool)) == APR_SUCCESS) {
564 *new_dir = dir;
565 } else {
566 free(dir);
567 *new_dir = NULL;
568 }
569
570 return status;
571 }
572
switch_dir_close(switch_dir_t * thedir)573 SWITCH_DECLARE(switch_status_t) switch_dir_close(switch_dir_t *thedir)
574 {
575 switch_status_t status = apr_dir_close(thedir->dir_handle);
576
577 free(thedir);
578 return status;
579 }
580
switch_dir_count(switch_dir_t * thedir)581 SWITCH_DECLARE(uint32_t) switch_dir_count(switch_dir_t *thedir)
582 {
583 const char *name;
584 apr_int32_t finfo_flags = APR_FINFO_DIRENT | APR_FINFO_TYPE | APR_FINFO_NAME;
585 uint32_t count = 0;
586
587 apr_dir_rewind(thedir->dir_handle);
588
589 while (apr_dir_read(&(thedir->finfo), finfo_flags, thedir->dir_handle) == SWITCH_STATUS_SUCCESS) {
590
591 if (thedir->finfo.filetype != APR_REG && thedir->finfo.filetype != APR_LNK) {
592 continue;
593 }
594
595 if (!(name = thedir->finfo.fname)) {
596 name = thedir->finfo.name;
597 }
598
599 if (name) {
600 count++;
601 }
602 }
603
604 apr_dir_rewind(thedir->dir_handle);
605
606 return count;
607 }
608
switch_dir_next_file(switch_dir_t * thedir,char * buf,switch_size_t len)609 SWITCH_DECLARE(const char *) switch_dir_next_file(switch_dir_t *thedir, char *buf, switch_size_t len)
610 {
611 const char *fname = NULL;
612 apr_int32_t finfo_flags = APR_FINFO_DIRENT | APR_FINFO_TYPE | APR_FINFO_NAME;
613 const char *name;
614
615 while (apr_dir_read(&(thedir->finfo), finfo_flags, thedir->dir_handle) == SWITCH_STATUS_SUCCESS) {
616
617 if (thedir->finfo.filetype != APR_REG && thedir->finfo.filetype != APR_LNK) {
618 continue;
619 }
620
621 if (!(name = thedir->finfo.fname)) {
622 name = thedir->finfo.name;
623 }
624
625 if (name) {
626 switch_copy_string(buf, name, len);
627 fname = buf;
628 break;
629 } else {
630 continue;
631 }
632 }
633 return fname;
634 }
635
636 /* thread stubs */
637
638 #ifndef WIN32
639 struct apr_threadattr_t {
640 apr_pool_t *pool;
641 pthread_attr_t attr;
642 int priority;
643 };
644 #else
645 /* this needs to be revisited when apr for windows supports thread priority settings */
646 /* search for WIN32 in this file */
647 struct apr_threadattr_t {
648 apr_pool_t *pool;
649 apr_int32_t detach;
650 apr_size_t stacksize;
651 int priority;
652 };
653 #endif
654
655
switch_threadattr_create(switch_threadattr_t ** new_attr,switch_memory_pool_t * pool)656 SWITCH_DECLARE(switch_status_t) switch_threadattr_create(switch_threadattr_t ** new_attr, switch_memory_pool_t *pool)
657 {
658 switch_status_t status;
659
660 if ((status = apr_threadattr_create(new_attr, pool)) == SWITCH_STATUS_SUCCESS) {
661
662 (*new_attr)->priority = SWITCH_PRI_LOW;
663
664 }
665
666 return status;
667 }
668
switch_threadattr_detach_set(switch_threadattr_t * attr,int32_t on)669 SWITCH_DECLARE(switch_status_t) switch_threadattr_detach_set(switch_threadattr_t *attr, int32_t on)
670 {
671 return apr_threadattr_detach_set(attr, on);
672 }
673
switch_threadattr_stacksize_set(switch_threadattr_t * attr,switch_size_t stacksize)674 SWITCH_DECLARE(switch_status_t) switch_threadattr_stacksize_set(switch_threadattr_t *attr, switch_size_t stacksize)
675 {
676 return apr_threadattr_stacksize_set(attr, stacksize);
677 }
678
switch_threadattr_priority_set(switch_threadattr_t * attr,switch_thread_priority_t priority)679 SWITCH_DECLARE(switch_status_t) switch_threadattr_priority_set(switch_threadattr_t *attr, switch_thread_priority_t priority)
680 {
681
682 attr->priority = priority;
683
684 return SWITCH_STATUS_SUCCESS;
685 }
686
687 static char TT_KEY[] = "1";
688
switch_thread_create(switch_thread_t ** new_thread,switch_threadattr_t * attr,switch_thread_start_t func,void * data,switch_memory_pool_t * cont)689 SWITCH_DECLARE(switch_status_t) switch_thread_create(switch_thread_t ** new_thread, switch_threadattr_t *attr,
690 switch_thread_start_t func, void *data, switch_memory_pool_t *cont)
691 {
692 switch_core_memory_pool_set_data(cont, "_in_thread", TT_KEY);
693 return apr_thread_create(new_thread, attr, func, data, cont);
694 }
695
switch_interval_time_from_timeval(struct timeval * tvp)696 SWITCH_DECLARE(switch_interval_time_t) switch_interval_time_from_timeval(struct timeval *tvp)
697 {
698 return ((switch_interval_time_t)tvp->tv_sec * 1000000) + tvp->tv_usec / 1000;
699 }
700
701 /* socket stubs */
702
switch_os_sock_get(switch_os_socket_t * thesock,switch_socket_t * sock)703 SWITCH_DECLARE(switch_status_t) switch_os_sock_get(switch_os_socket_t *thesock, switch_socket_t *sock)
704 {
705 return apr_os_sock_get(thesock, sock);
706 }
707
switch_os_sock_put(switch_socket_t ** sock,switch_os_socket_t * thesock,switch_memory_pool_t * pool)708 SWITCH_DECLARE(switch_status_t) switch_os_sock_put(switch_socket_t **sock, switch_os_socket_t *thesock, switch_memory_pool_t *pool)
709 {
710 return apr_os_sock_put(sock, thesock, pool);
711 }
712
switch_socket_addr_get(switch_sockaddr_t ** sa,switch_bool_t remote,switch_socket_t * sock)713 SWITCH_DECLARE(switch_status_t) switch_socket_addr_get(switch_sockaddr_t ** sa, switch_bool_t remote, switch_socket_t *sock)
714 {
715 return apr_socket_addr_get(sa, (apr_interface_e) remote, sock);
716 }
717
switch_socket_create(switch_socket_t ** new_sock,int family,int type,int protocol,switch_memory_pool_t * pool)718 SWITCH_DECLARE(switch_status_t) switch_socket_create(switch_socket_t ** new_sock, int family, int type, int protocol, switch_memory_pool_t *pool)
719 {
720 return apr_socket_create(new_sock, family, type, protocol, pool);
721 }
722
switch_socket_shutdown(switch_socket_t * sock,switch_shutdown_how_e how)723 SWITCH_DECLARE(switch_status_t) switch_socket_shutdown(switch_socket_t *sock, switch_shutdown_how_e how)
724 {
725 return apr_socket_shutdown(sock, (apr_shutdown_how_e) how);
726 }
727
switch_socket_close(switch_socket_t * sock)728 SWITCH_DECLARE(switch_status_t) switch_socket_close(switch_socket_t *sock)
729 {
730 return apr_socket_close(sock);
731 }
732
switch_socket_bind(switch_socket_t * sock,switch_sockaddr_t * sa)733 SWITCH_DECLARE(switch_status_t) switch_socket_bind(switch_socket_t *sock, switch_sockaddr_t *sa)
734 {
735 return apr_socket_bind(sock, sa);
736 }
737
switch_socket_listen(switch_socket_t * sock,int32_t backlog)738 SWITCH_DECLARE(switch_status_t) switch_socket_listen(switch_socket_t *sock, int32_t backlog)
739 {
740 return apr_socket_listen(sock, backlog);
741 }
742
switch_socket_accept(switch_socket_t ** new_sock,switch_socket_t * sock,switch_memory_pool_t * pool)743 SWITCH_DECLARE(switch_status_t) switch_socket_accept(switch_socket_t ** new_sock, switch_socket_t *sock, switch_memory_pool_t *pool)
744 {
745 return apr_socket_accept(new_sock, sock, pool);
746 }
747
switch_socket_connect(switch_socket_t * sock,switch_sockaddr_t * sa)748 SWITCH_DECLARE(switch_status_t) switch_socket_connect(switch_socket_t *sock, switch_sockaddr_t *sa)
749 {
750 return apr_socket_connect(sock, sa);
751 }
752
switch_socket_send(switch_socket_t * sock,const char * buf,switch_size_t * len)753 SWITCH_DECLARE(switch_status_t) switch_socket_send(switch_socket_t *sock, const char *buf, switch_size_t *len)
754 {
755 int status = SWITCH_STATUS_SUCCESS;
756 switch_size_t req = *len, wrote = 0, need = *len;
757 int to_count = 0;
758
759 while ((wrote < req && status == SWITCH_STATUS_SUCCESS) || (need == 0 && status == SWITCH_STATUS_BREAK) || status == 730035 || status == 35) {
760 need = req - wrote;
761 status = apr_socket_send(sock, buf + wrote, &need);
762 if (status == SWITCH_STATUS_BREAK || status == 730035 || status == 35) {
763 if (++to_count > 60000) {
764 status = SWITCH_STATUS_FALSE;
765 break;
766 }
767 switch_yield(10000);
768 } else {
769 to_count = 0;
770 }
771 wrote += need;
772 }
773
774 *len = wrote;
775 return (switch_status_t)status;
776 }
777
switch_socket_send_nonblock(switch_socket_t * sock,const char * buf,switch_size_t * len)778 SWITCH_DECLARE(switch_status_t) switch_socket_send_nonblock(switch_socket_t *sock, const char *buf, switch_size_t *len)
779 {
780 if (!sock || !buf || !len) {
781 return SWITCH_STATUS_GENERR;
782 }
783
784 return apr_socket_send(sock, buf, len);
785 }
786
switch_socket_sendto(switch_socket_t * sock,switch_sockaddr_t * where,int32_t flags,const char * buf,switch_size_t * len)787 SWITCH_DECLARE(switch_status_t) switch_socket_sendto(switch_socket_t *sock, switch_sockaddr_t *where, int32_t flags, const char *buf,
788 switch_size_t *len)
789 {
790 if (!where || !buf || !len || !*len) {
791 return SWITCH_STATUS_GENERR;
792 }
793 return apr_socket_sendto(sock, where, flags, buf, len);
794 }
795
switch_socket_recv(switch_socket_t * sock,char * buf,switch_size_t * len)796 SWITCH_DECLARE(switch_status_t) switch_socket_recv(switch_socket_t *sock, char *buf, switch_size_t *len)
797 {
798 int r;
799
800 r = apr_socket_recv(sock, buf, len);
801
802 if (r == 35 || r == 730035) {
803 r = SWITCH_STATUS_BREAK;
804 }
805
806 return (switch_status_t)r;
807 }
808
switch_sockaddr_create(switch_sockaddr_t ** sa,switch_memory_pool_t * pool)809 SWITCH_DECLARE(switch_status_t) switch_sockaddr_create(switch_sockaddr_t **sa, switch_memory_pool_t *pool)
810 {
811 switch_sockaddr_t *new_sa;
812 unsigned short family = APR_INET;
813
814 new_sa = apr_pcalloc(pool, sizeof(apr_sockaddr_t));
815 switch_assert(new_sa);
816 new_sa->pool = pool;
817 memset(new_sa, 0, sizeof(*new_sa));
818
819 new_sa->family = family;
820 new_sa->sa.sin.sin_family = family;
821
822 new_sa->salen = sizeof(struct sockaddr_in);
823 new_sa->addr_str_len = 16;
824 new_sa->ipaddr_ptr = &(new_sa->sa.sin.sin_addr);
825 new_sa->ipaddr_len = sizeof(struct in_addr);
826
827 *sa = new_sa;
828 return SWITCH_STATUS_SUCCESS;
829 }
830
switch_sockaddr_info_get(switch_sockaddr_t ** sa,const char * hostname,int32_t family,switch_port_t port,int32_t flags,switch_memory_pool_t * pool)831 SWITCH_DECLARE(switch_status_t) switch_sockaddr_info_get(switch_sockaddr_t ** sa, const char *hostname, int32_t family,
832 switch_port_t port, int32_t flags, switch_memory_pool_t *pool)
833 {
834 return apr_sockaddr_info_get(sa, hostname, family, port, flags, pool);
835 }
836
switch_socket_opt_set(switch_socket_t * sock,int32_t opt,int32_t on)837 SWITCH_DECLARE(switch_status_t) switch_socket_opt_set(switch_socket_t *sock, int32_t opt, int32_t on)
838 {
839 if (opt == SWITCH_SO_TCP_KEEPIDLE) {
840 #if defined(TCP_KEEPIDLE)
841 int r = -10;
842 r = setsockopt(sock->socketdes, SOL_TCP, TCP_KEEPIDLE, (void *)&on, sizeof(on));
843 return r ? SWITCH_STATUS_FALSE : SWITCH_STATUS_SUCCESS;
844 #else
845 return SWITCH_STATUS_NOTIMPL;
846 #endif
847 }
848
849 if (opt == SWITCH_SO_TCP_KEEPINTVL) {
850 #if defined(TCP_KEEPINTVL)
851 int r = -10;
852 r = setsockopt(sock->socketdes, SOL_TCP, TCP_KEEPINTVL, (void *)&on, sizeof(on));
853 return r ? SWITCH_STATUS_FALSE : SWITCH_STATUS_SUCCESS;
854 #else
855 return SWITCH_STATUS_NOTIMPL;
856 #endif
857 }
858
859 return apr_socket_opt_set(sock, opt, on);
860 }
861
switch_socket_timeout_get(switch_socket_t * sock,switch_interval_time_t * t)862 SWITCH_DECLARE(switch_status_t) switch_socket_timeout_get(switch_socket_t *sock, switch_interval_time_t *t)
863 {
864 apr_interval_time_t at = 0;
865 switch_status_t status = apr_socket_timeout_get(sock, &at);
866 *t = at;
867
868 return status;
869 }
870
switch_socket_timeout_set(switch_socket_t * sock,switch_interval_time_t t)871 SWITCH_DECLARE(switch_status_t) switch_socket_timeout_set(switch_socket_t *sock, switch_interval_time_t t)
872 {
873 return apr_socket_timeout_set(sock, t);
874 }
875
switch_sockaddr_ip_get(char ** addr,switch_sockaddr_t * sa)876 SWITCH_DECLARE(switch_status_t) switch_sockaddr_ip_get(char **addr, switch_sockaddr_t *sa)
877 {
878 return apr_sockaddr_ip_get(addr, sa);
879 }
880
switch_sockaddr_equal(const switch_sockaddr_t * sa1,const switch_sockaddr_t * sa2)881 SWITCH_DECLARE(int) switch_sockaddr_equal(const switch_sockaddr_t *sa1, const switch_sockaddr_t *sa2)
882 {
883 return apr_sockaddr_equal(sa1, sa2);
884 }
885
switch_mcast_join(switch_socket_t * sock,switch_sockaddr_t * join,switch_sockaddr_t * iface,switch_sockaddr_t * source)886 SWITCH_DECLARE(switch_status_t) switch_mcast_join(switch_socket_t *sock, switch_sockaddr_t *join, switch_sockaddr_t *iface, switch_sockaddr_t *source)
887 {
888 return apr_mcast_join(sock, join, iface, source);
889 }
890
switch_mcast_hops(switch_socket_t * sock,uint8_t ttl)891 SWITCH_DECLARE(switch_status_t) switch_mcast_hops(switch_socket_t *sock, uint8_t ttl)
892 {
893 return apr_mcast_hops(sock, ttl);
894 }
895
switch_mcast_loopback(switch_socket_t * sock,uint8_t opt)896 SWITCH_DECLARE(switch_status_t) switch_mcast_loopback(switch_socket_t *sock, uint8_t opt)
897 {
898 return apr_mcast_loopback(sock, opt);
899 }
900
switch_mcast_interface(switch_socket_t * sock,switch_sockaddr_t * iface)901 SWITCH_DECLARE(switch_status_t) switch_mcast_interface(switch_socket_t *sock, switch_sockaddr_t *iface)
902 {
903 return apr_mcast_interface(sock, iface);
904 }
905
906
907 /* socket functions */
908
switch_get_addr(char * buf,switch_size_t len,switch_sockaddr_t * in)909 SWITCH_DECLARE(const char *) switch_get_addr(char *buf, switch_size_t len, switch_sockaddr_t *in)
910 {
911 if (!in) {
912 return SWITCH_BLANK_STRING;
913 }
914
915 memset(buf, 0, len);
916
917 if (in->family == AF_INET) {
918 get_addr(buf, len, (struct sockaddr *) &in->sa, in->salen);
919 return buf;
920 }
921
922 get_addr6(buf, len, (struct sockaddr_in6 *) &in->sa, in->salen);
923 return buf;
924 }
925
switch_socket_fd_get(switch_socket_t * sock)926 SWITCH_DECLARE(int) switch_socket_fd_get(switch_socket_t *sock)
927 {
928 return apr_socket_fd_get(sock);
929 }
930
switch_sockaddr_get_port(switch_sockaddr_t * sa)931 SWITCH_DECLARE(uint16_t) switch_sockaddr_get_port(switch_sockaddr_t *sa)
932 {
933 return sa->port;
934 }
935
switch_sockaddr_get_family(switch_sockaddr_t * sa)936 SWITCH_DECLARE(int32_t) switch_sockaddr_get_family(switch_sockaddr_t *sa)
937 {
938 return sa->family;
939 }
940
switch_getnameinfo(char ** hostname,switch_sockaddr_t * sa,int32_t flags)941 SWITCH_DECLARE(switch_status_t) switch_getnameinfo(char **hostname, switch_sockaddr_t *sa, int32_t flags)
942 {
943 return apr_getnameinfo(hostname, sa, flags);
944 }
945
switch_socket_atmark(switch_socket_t * sock,int * atmark)946 SWITCH_DECLARE(switch_status_t) switch_socket_atmark(switch_socket_t *sock, int *atmark)
947 {
948 return apr_socket_atmark(sock, atmark);
949 }
950
switch_socket_recvfrom(switch_sockaddr_t * from,switch_socket_t * sock,int32_t flags,char * buf,size_t * len)951 SWITCH_DECLARE(switch_status_t) switch_socket_recvfrom(switch_sockaddr_t *from, switch_socket_t *sock, int32_t flags, char *buf, size_t *len)
952 {
953 int r = SWITCH_STATUS_GENERR;
954
955 if (from && sock && (r = apr_socket_recvfrom(from, sock, flags, buf, len)) == APR_SUCCESS) {
956 from->port = ntohs(from->sa.sin.sin_port);
957 /* from->ipaddr_ptr = &(from->sa.sin.sin_addr);
958 * from->ipaddr_ptr = inet_ntoa(from->sa.sin.sin_addr);
959 */
960 }
961
962 if (r == 35 || r == 730035) {
963 r = SWITCH_STATUS_BREAK;
964 }
965
966 return (switch_status_t)r;
967 }
968
969 /* poll stubs */
970
switch_pollset_create(switch_pollset_t ** pollset,uint32_t size,switch_memory_pool_t * pool,uint32_t flags)971 SWITCH_DECLARE(switch_status_t) switch_pollset_create(switch_pollset_t ** pollset, uint32_t size, switch_memory_pool_t *pool, uint32_t flags)
972 {
973 return apr_pollset_create(pollset, size, pool, flags);
974 }
975
switch_pollset_add(switch_pollset_t * pollset,const switch_pollfd_t * descriptor)976 SWITCH_DECLARE(switch_status_t) switch_pollset_add(switch_pollset_t *pollset, const switch_pollfd_t *descriptor)
977 {
978 if (!pollset || !descriptor) {
979 return SWITCH_STATUS_FALSE;
980 }
981
982 return apr_pollset_add((apr_pollset_t *) pollset, (const apr_pollfd_t *) descriptor);
983 }
984
switch_pollset_remove(switch_pollset_t * pollset,const switch_pollfd_t * descriptor)985 SWITCH_DECLARE(switch_status_t) switch_pollset_remove(switch_pollset_t *pollset, const switch_pollfd_t *descriptor)
986 {
987 if (!pollset || !descriptor) {
988 return SWITCH_STATUS_FALSE;
989 }
990
991 return apr_pollset_remove((apr_pollset_t *) pollset, (const apr_pollfd_t *) descriptor);
992 }
993
switch_socket_create_pollfd(switch_pollfd_t ** pollfd,switch_socket_t * sock,int16_t flags,void * client_data,switch_memory_pool_t * pool)994 SWITCH_DECLARE(switch_status_t) switch_socket_create_pollfd(switch_pollfd_t **pollfd, switch_socket_t *sock, int16_t flags, void *client_data, switch_memory_pool_t *pool)
995 {
996 if (!pollfd || !sock) {
997 return SWITCH_STATUS_FALSE;
998 }
999
1000 if ((*pollfd = (switch_pollfd_t*)apr_palloc(pool, sizeof(switch_pollfd_t))) == 0) {
1001 return SWITCH_STATUS_MEMERR;
1002 }
1003
1004 memset(*pollfd, 0, sizeof(switch_pollfd_t));
1005
1006 (*pollfd)->desc_type = (switch_pollset_type_t) APR_POLL_SOCKET;
1007 (*pollfd)->reqevents = flags;
1008 (*pollfd)->desc.s = sock;
1009 (*pollfd)->client_data = client_data;
1010
1011 return SWITCH_STATUS_SUCCESS;
1012 }
1013
1014
switch_pollset_poll(switch_pollset_t * pollset,switch_interval_time_t timeout,int32_t * num,const switch_pollfd_t ** descriptors)1015 SWITCH_DECLARE(switch_status_t) switch_pollset_poll(switch_pollset_t *pollset, switch_interval_time_t timeout, int32_t *num, const switch_pollfd_t **descriptors)
1016 {
1017 apr_status_t st = SWITCH_STATUS_FALSE;
1018
1019 if (pollset) {
1020 st = apr_pollset_poll((apr_pollset_t *) pollset, timeout, num, (const apr_pollfd_t **) descriptors);
1021
1022 if (st == APR_TIMEUP) {
1023 st = SWITCH_STATUS_TIMEOUT;
1024 }
1025 }
1026
1027 return st;
1028 }
1029
switch_poll(switch_pollfd_t * aprset,int32_t numsock,int32_t * nsds,switch_interval_time_t timeout)1030 SWITCH_DECLARE(switch_status_t) switch_poll(switch_pollfd_t *aprset, int32_t numsock, int32_t *nsds, switch_interval_time_t timeout)
1031 {
1032 apr_status_t st = SWITCH_STATUS_FALSE;
1033
1034 if (aprset) {
1035 st = apr_poll((apr_pollfd_t *) aprset, numsock, nsds, timeout);
1036
1037 if (numsock == 1 && ((aprset[0].rtnevents & APR_POLLERR) || (aprset[0].rtnevents & APR_POLLHUP) || (aprset[0].rtnevents & APR_POLLNVAL))) {
1038 st = SWITCH_STATUS_GENERR;
1039 } else if (st == APR_TIMEUP) {
1040 st = SWITCH_STATUS_TIMEOUT;
1041 }
1042 }
1043
1044 return st;
1045 }
1046
switch_socket_create_pollset(switch_pollfd_t ** poll,switch_socket_t * sock,int16_t flags,switch_memory_pool_t * pool)1047 SWITCH_DECLARE(switch_status_t) switch_socket_create_pollset(switch_pollfd_t ** poll, switch_socket_t *sock, int16_t flags, switch_memory_pool_t *pool)
1048 {
1049 switch_pollset_t *pollset;
1050
1051 if (switch_pollset_create(&pollset, 1, pool, 0) != SWITCH_STATUS_SUCCESS) {
1052 return SWITCH_STATUS_GENERR;
1053 }
1054
1055 if (switch_socket_create_pollfd(poll, sock, flags, sock, pool) != SWITCH_STATUS_SUCCESS) {
1056 return SWITCH_STATUS_GENERR;
1057 }
1058
1059 if (switch_pollset_add(pollset, *poll) != SWITCH_STATUS_SUCCESS) {
1060 return SWITCH_STATUS_GENERR;
1061 }
1062
1063 return SWITCH_STATUS_SUCCESS;
1064 }
1065
1066 /* apr-util stubs */
1067
1068 /* UUID Handling (apr-util) */
1069
switch_uuid_format(char * buffer,const switch_uuid_t * uuid)1070 SWITCH_DECLARE(void) switch_uuid_format(char *buffer, const switch_uuid_t *uuid)
1071 {
1072 #ifndef WIN32
1073 apr_uuid_format(buffer, (const apr_uuid_t *) uuid);
1074 #else
1075 RPC_CSTR buf;
1076 UuidToString((const UUID *) uuid, &buf);
1077 strcpy(buffer, (const char *) buf);
1078 RpcStringFree(&buf);
1079 #endif
1080 }
1081
switch_uuid_get(switch_uuid_t * uuid)1082 SWITCH_DECLARE(void) switch_uuid_get(switch_uuid_t *uuid)
1083 {
1084 switch_mutex_lock(runtime.uuid_mutex);
1085 #ifndef WIN32
1086 apr_uuid_get((apr_uuid_t *) uuid);
1087 #else
1088 UuidCreate((UUID *) uuid);
1089 #endif
1090 switch_mutex_unlock(runtime.uuid_mutex);
1091 }
1092
switch_uuid_parse(switch_uuid_t * uuid,const char * uuid_str)1093 SWITCH_DECLARE(switch_status_t) switch_uuid_parse(switch_uuid_t *uuid, const char *uuid_str)
1094 {
1095 #ifndef WIN32
1096 return apr_uuid_parse((apr_uuid_t *) uuid, uuid_str);
1097 #else
1098 return UuidFromString((RPC_CSTR) uuid_str, (UUID *) uuid);
1099 #endif
1100 }
1101
switch_md5(unsigned char digest[SWITCH_MD5_DIGESTSIZE],const void * input,switch_size_t inputLen)1102 SWITCH_DECLARE(switch_status_t) switch_md5(unsigned char digest[SWITCH_MD5_DIGESTSIZE], const void *input, switch_size_t inputLen)
1103 {
1104 #if (defined(HAVE_LIBMD5) || defined(HAVE_LIBMD) || defined(HAVE_MD5INIT))
1105 MD5_CTX md5_context;
1106
1107 MD5Init(&md5_context);
1108 MD5Update(&md5_context, input, inputLen);
1109 MD5Final(digest, &md5_context);
1110
1111 return SWITCH_STATUS_SUCCESS;
1112 #elif defined(HAVE_LIBCRYPTO)
1113 MD5_CTX md5_context;
1114
1115 MD5_Init(&md5_context);
1116 MD5_Update(&md5_context, input, inputLen);
1117 MD5_Final(digest, &md5_context);
1118
1119 return SWITCH_STATUS_SUCCESS;
1120 #else
1121 return apr_md5(digest, input, inputLen);
1122 #endif
1123 }
1124
switch_md5_string(char digest_str[SWITCH_MD5_DIGEST_STRING_SIZE],const void * input,switch_size_t inputLen)1125 SWITCH_DECLARE(switch_status_t) switch_md5_string(char digest_str[SWITCH_MD5_DIGEST_STRING_SIZE], const void *input, switch_size_t inputLen)
1126 {
1127 unsigned char digest[SWITCH_MD5_DIGESTSIZE];
1128 switch_status_t status = switch_md5(digest, input, inputLen);
1129 short i, x;
1130 uint8_t b;
1131
1132 digest_str[SWITCH_MD5_DIGEST_STRING_SIZE - 1] = '\0';
1133
1134 for (x = i = 0; x < SWITCH_MD5_DIGESTSIZE; x++) {
1135 b = (digest[x] >> 4) & 15;
1136 digest_str[i++] = b + (b > 9 ? 'a' - 10 : '0');
1137 b = digest[x] & 15;
1138 digest_str[i++] = b + (b > 9 ? 'a' - 10 : '0');
1139 }
1140 digest_str[i] = '\0';
1141
1142 return status;
1143 }
1144
1145 /* FIFO queues (apr-util) */
1146
switch_queue_create(switch_queue_t ** queue,unsigned int queue_capacity,switch_memory_pool_t * pool)1147 SWITCH_DECLARE(switch_status_t) switch_queue_create(switch_queue_t ** queue, unsigned int queue_capacity, switch_memory_pool_t *pool)
1148 {
1149 return apr_queue_create(queue, queue_capacity, pool);
1150 }
1151
switch_queue_size(switch_queue_t * queue)1152 SWITCH_DECLARE(unsigned int) switch_queue_size(switch_queue_t *queue)
1153 {
1154 return apr_queue_size(queue);
1155 }
1156
switch_queue_pop(switch_queue_t * queue,void ** data)1157 SWITCH_DECLARE(switch_status_t) switch_queue_pop(switch_queue_t *queue, void **data)
1158 {
1159 return apr_queue_pop(queue, data);
1160 }
1161
switch_queue_pop_timeout(switch_queue_t * queue,void ** data,switch_interval_time_t timeout)1162 SWITCH_DECLARE(switch_status_t) switch_queue_pop_timeout(switch_queue_t *queue, void **data, switch_interval_time_t timeout)
1163 {
1164 return apr_queue_pop_timeout(queue, data, timeout);
1165 }
1166
switch_queue_push(switch_queue_t * queue,void * data)1167 SWITCH_DECLARE(switch_status_t) switch_queue_push(switch_queue_t *queue, void *data)
1168 {
1169 apr_status_t s;
1170
1171 do {
1172 s = apr_queue_push(queue, data);
1173 } while (s == APR_EINTR);
1174
1175 return s;
1176 }
1177
switch_queue_trypop(switch_queue_t * queue,void ** data)1178 SWITCH_DECLARE(switch_status_t) switch_queue_trypop(switch_queue_t *queue, void **data)
1179 {
1180 return apr_queue_trypop(queue, data);
1181 }
1182
switch_queue_interrupt_all(switch_queue_t * queue)1183 SWITCH_DECLARE(switch_status_t) switch_queue_interrupt_all(switch_queue_t *queue)
1184 {
1185 return apr_queue_interrupt_all(queue);
1186 }
1187
switch_queue_term(switch_queue_t * queue)1188 SWITCH_DECLARE(switch_status_t) switch_queue_term(switch_queue_t *queue)
1189 {
1190 return apr_queue_term(queue);
1191 }
1192
switch_queue_trypush(switch_queue_t * queue,void * data)1193 SWITCH_DECLARE(switch_status_t) switch_queue_trypush(switch_queue_t *queue, void *data)
1194 {
1195 apr_status_t s;
1196
1197 do {
1198 s = apr_queue_trypush(queue, data);
1199 } while (s == APR_EINTR);
1200
1201 return s;
1202 }
1203
switch_vasprintf(char ** ret,const char * fmt,va_list ap)1204 SWITCH_DECLARE(int) switch_vasprintf(char **ret, const char *fmt, va_list ap)
1205 {
1206 #ifdef HAVE_VASPRINTF
1207 return vasprintf(ret, fmt, ap);
1208 #else
1209 char *buf;
1210 int len;
1211 size_t buflen;
1212 va_list ap2;
1213 char *tmp = NULL;
1214
1215 #ifdef _MSC_VER
1216 #if _MSC_VER >= 1500
1217 /* hack for incorrect assumption in msvc header files for code analysis */
1218 __analysis_assume(tmp);
1219 #endif
1220 ap2 = ap;
1221 #else
1222 va_copy(ap2, ap);
1223 #endif
1224
1225 len = vsnprintf(tmp, 0, fmt, ap2);
1226
1227 if (len > 0 && (buf = malloc((buflen = (size_t) (len + 1)))) != NULL) {
1228 len = vsnprintf(buf, buflen, fmt, ap);
1229 *ret = buf;
1230 } else {
1231 *ret = NULL;
1232 len = -1;
1233 }
1234
1235 va_end(ap2);
1236 return len;
1237 #endif
1238 }
1239
switch_match_glob(const char * pattern,switch_array_header_t ** result,switch_memory_pool_t * pool)1240 SWITCH_DECLARE(switch_status_t) switch_match_glob(const char *pattern, switch_array_header_t ** result, switch_memory_pool_t *pool)
1241 {
1242 return apr_match_glob(pattern, (apr_array_header_t **) result, pool);
1243 }
1244
1245 /**
1246 * Create an anonymous pipe.
1247 * @param in The file descriptor to use as input to the pipe.
1248 * @param out The file descriptor to use as output from the pipe.
1249 * @param pool The pool to operate on.
1250 */
switch_file_pipe_create(switch_file_t ** in,switch_file_t ** out,switch_memory_pool_t * pool)1251 SWITCH_DECLARE(switch_status_t) switch_file_pipe_create(switch_file_t ** in, switch_file_t ** out, switch_memory_pool_t *pool)
1252 {
1253 return apr_file_pipe_create((apr_file_t **) in, (apr_file_t **) out, pool);
1254 }
1255
1256 /**
1257 * Get the timeout value for a pipe or manipulate the blocking state.
1258 * @param thepipe The pipe we are getting a timeout for.
1259 * @param timeout The current timeout value in microseconds.
1260 */
switch_file_pipe_timeout_get(switch_file_t * thepipe,switch_interval_time_t * timeout)1261 SWITCH_DECLARE(switch_status_t) switch_file_pipe_timeout_get(switch_file_t *thepipe, switch_interval_time_t *timeout)
1262 {
1263 return apr_file_pipe_timeout_get((apr_file_t *) thepipe, (apr_interval_time_t *) timeout);
1264 }
1265
1266 /**
1267 * Set the timeout value for a pipe or manipulate the blocking state.
1268 * @param thepipe The pipe we are setting a timeout on.
1269 * @param timeout The timeout value in microseconds. Values < 0 mean wait
1270 * forever, 0 means do not wait at all.
1271 */
switch_file_pipe_timeout_set(switch_file_t * thepipe,switch_interval_time_t timeout)1272 SWITCH_DECLARE(switch_status_t) switch_file_pipe_timeout_set(switch_file_t *thepipe, switch_interval_time_t timeout)
1273 {
1274 return apr_file_pipe_timeout_set((apr_file_t *) thepipe, (apr_interval_time_t) timeout);
1275 }
1276
1277
1278 /**
1279 * stop the current thread
1280 * @param thd The thread to stop
1281 * @param retval The return value to pass back to any thread that cares
1282 */
switch_thread_exit(switch_thread_t * thd,switch_status_t retval)1283 SWITCH_DECLARE(switch_status_t) switch_thread_exit(switch_thread_t *thd, switch_status_t retval)
1284 {
1285 return apr_thread_exit((apr_thread_t *) thd, retval);
1286 }
1287
1288 /**
1289 * block until the desired thread stops executing.
1290 * @param retval The return value from the dead thread.
1291 * @param thd The thread to join
1292 */
switch_thread_join(switch_status_t * retval,switch_thread_t * thd)1293 SWITCH_DECLARE(switch_status_t) switch_thread_join(switch_status_t *retval, switch_thread_t *thd)
1294 {
1295 if ( !thd ) {
1296 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "ERROR: Attempting to join thread that does not exist\n");
1297 return SWITCH_STATUS_FALSE;
1298 }
1299
1300 return apr_thread_join((apr_status_t *) retval, (apr_thread_t *) thd);
1301 }
1302
1303
switch_atomic_init(switch_memory_pool_t * pool)1304 SWITCH_DECLARE(switch_status_t) switch_atomic_init(switch_memory_pool_t *pool)
1305 {
1306 return apr_atomic_init((apr_pool_t *) pool);
1307 }
1308
switch_atomic_read(volatile switch_atomic_t * mem)1309 SWITCH_DECLARE(uint32_t) switch_atomic_read(volatile switch_atomic_t *mem)
1310 {
1311 #ifdef apr_atomic_t
1312 return apr_atomic_read((apr_atomic_t *)mem);
1313 #else
1314 return apr_atomic_read32((apr_uint32_t *)mem);
1315 #endif
1316 }
1317
switch_atomic_set(volatile switch_atomic_t * mem,uint32_t val)1318 SWITCH_DECLARE(void) switch_atomic_set(volatile switch_atomic_t *mem, uint32_t val)
1319 {
1320 #ifdef apr_atomic_t
1321 apr_atomic_set((apr_atomic_t *)mem, val);
1322 #else
1323 apr_atomic_set32((apr_uint32_t *)mem, val);
1324 #endif
1325 }
1326
switch_atomic_add(volatile switch_atomic_t * mem,uint32_t val)1327 SWITCH_DECLARE(void) switch_atomic_add(volatile switch_atomic_t *mem, uint32_t val)
1328 {
1329 #ifdef apr_atomic_t
1330 apr_atomic_add((apr_atomic_t *)mem, val);
1331 #else
1332 apr_atomic_add32((apr_uint32_t *)mem, val);
1333 #endif
1334 }
1335
switch_atomic_inc(volatile switch_atomic_t * mem)1336 SWITCH_DECLARE(void) switch_atomic_inc(volatile switch_atomic_t *mem)
1337 {
1338 #ifdef apr_atomic_t
1339 apr_atomic_inc((apr_atomic_t *)mem);
1340 #else
1341 apr_atomic_inc32((apr_uint32_t *)mem);
1342 #endif
1343 }
1344
switch_atomic_dec(volatile switch_atomic_t * mem)1345 SWITCH_DECLARE(int) switch_atomic_dec(volatile switch_atomic_t *mem)
1346 {
1347 #ifdef apr_atomic_t
1348 return apr_atomic_dec((apr_atomic_t *)mem);
1349 #else
1350 return apr_atomic_dec32((apr_uint32_t *)mem);
1351 #endif
1352 }
1353
switch_strerror(switch_status_t statcode,char * buf,switch_size_t bufsize)1354 SWITCH_DECLARE(char *) switch_strerror(switch_status_t statcode, char *buf, switch_size_t bufsize)
1355 {
1356 return apr_strerror(statcode, buf, bufsize);
1357 }
1358
1359 /* For Emacs:
1360 * Local Variables:
1361 * mode:c
1362 * indent-tabs-mode:t
1363 * tab-width:4
1364 * c-basic-offset:4
1365 * End:
1366 * For VIM:
1367 * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
1368 */
1369