2007-03-09 20:44:13 +00:00
|
|
|
/*
|
|
|
|
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
|
|
|
|
* Copyright (C) 2005/2006, Anthony Minessale II <anthmct@yahoo.com>
|
|
|
|
*
|
|
|
|
* 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/F
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* Michael Jerris <mike@jerris.com>
|
|
|
|
* Portions created by the Initial Developer are Copyright (C)
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Michael Jerris <mike@jerris.com>
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* switch_apr.c -- apr wrappers and extensions
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <switch.h>
|
|
|
|
#ifndef WIN32
|
|
|
|
#include <switch_private.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* apr headers*/
|
|
|
|
#include <apr.h>
|
|
|
|
#include <apr_pools.h>
|
|
|
|
#include <apr_hash.h>
|
|
|
|
#include <apr_network_io.h>
|
|
|
|
#include <apr_errno.h>
|
|
|
|
#include <apr_thread_proc.h>
|
|
|
|
#include <apr_portable.h>
|
|
|
|
#include <apr_thread_mutex.h>
|
|
|
|
#include <apr_thread_cond.h>
|
|
|
|
#include <apr_thread_rwlock.h>
|
|
|
|
#include <apr_file_io.h>
|
|
|
|
#include <apr_poll.h>
|
|
|
|
#include <apr_dso.h>
|
|
|
|
#include <apr_strings.h>
|
|
|
|
#define APR_WANT_STDIO
|
|
|
|
#define APR_WANT_STRFUNC
|
|
|
|
#include <apr_want.h>
|
|
|
|
|
|
|
|
/* apr_vformatter_buff_t definition*/
|
|
|
|
#include <apr_lib.h>
|
|
|
|
|
|
|
|
/* apr-util headers */
|
|
|
|
#include <apr_queue.h>
|
|
|
|
#include <apr_uuid.h>
|
|
|
|
|
|
|
|
/* apr stubs */
|
|
|
|
|
|
|
|
/* Memory Pools */
|
|
|
|
|
2007-03-30 00:15:25 +00:00
|
|
|
SWITCH_DECLARE(void) switch_pool_clear(switch_memory_pool_t *p)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
apr_pool_clear(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Hash tables */
|
|
|
|
|
2007-03-30 00:15:25 +00:00
|
|
|
SWITCH_DECLARE(switch_hash_index_t *) switch_hash_first(switch_memory_pool_t *p, switch_hash_t * ht)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_hash_first(p, ht);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_hash_index_t *) switch_hash_next(switch_hash_index_t * ht)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_hash_next(ht);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(void) switch_hash_this(switch_hash_index_t * hi, const void **key, switch_ssize_t *klen, void **val)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
apr_hash_this(hi, key, klen, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(unsigned int) switch_hashfunc_default(const char *key, switch_ssize_t *klen)
|
|
|
|
{
|
|
|
|
return apr_hashfunc_default(key, klen);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_memory_pool_t *) switch_hash_pool_get(switch_hash_t * ht)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_hash_pool_get(ht);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* DSO functions */
|
|
|
|
|
2007-03-30 00:15:25 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_dso_load(switch_dso_handle_t ** res_handle, const char *path, switch_memory_pool_t *ctx)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_dso_load(res_handle, path, ctx);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_dso_unload(switch_dso_handle_t * handle)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_dso_unload(handle);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_dso_sym(switch_dso_handle_sym_t * ressym, switch_dso_handle_t * handle, const char *symname)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_dso_sym(ressym, handle, symname);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(const char *) switch_dso_error(switch_dso_handle_t * dso, char *buf, size_t bufsize)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_dso_error(dso, buf, bufsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* string functions */
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
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)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
2007-03-29 22:31:56 +00:00
|
|
|
return apr_strftime(s, retsize, max, format, (apr_time_exp_t *) tm);
|
2007-03-09 20:44:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(int) switch_snprintf(char *buf, switch_size_t len, const char *format, ...)
|
|
|
|
{
|
2007-03-29 22:31:56 +00:00
|
|
|
va_list ap;
|
2007-03-09 20:44:13 +00:00
|
|
|
int ret;
|
|
|
|
va_start(ap, format);
|
|
|
|
ret = apr_vsnprintf(buf, len, format, ap);
|
2007-03-29 22:31:56 +00:00
|
|
|
va_end(ap);
|
2007-03-09 20:44:13 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(int) switch_vsnprintf(char *buf, switch_size_t len, const char *format, va_list ap)
|
|
|
|
{
|
|
|
|
return apr_vsnprintf(buf, len, format, ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(char *) switch_copy_string(char *dst, const char *src, switch_size_t dst_size)
|
|
|
|
{
|
|
|
|
return apr_cpystrn(dst, src, dst_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* thread read write lock functions */
|
|
|
|
|
2007-03-30 00:15:25 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_thread_rwlock_create(switch_thread_rwlock_t ** rwlock, switch_memory_pool_t *pool)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_thread_rwlock_create(rwlock, pool);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_thread_rwlock_destroy(switch_thread_rwlock_t * rwlock)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_thread_rwlock_destroy(rwlock);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_memory_pool_t *) switch_thread_rwlock_pool_get(switch_thread_rwlock_t * rwlock)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_thread_rwlock_pool_get(rwlock);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_thread_rwlock_rdlock(switch_thread_rwlock_t * rwlock)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_thread_rwlock_rdlock(rwlock);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_thread_rwlock_tryrdlock(switch_thread_rwlock_t * rwlock)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_thread_rwlock_tryrdlock(rwlock);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_thread_rwlock_wrlock(switch_thread_rwlock_t * rwlock)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_thread_rwlock_wrlock(rwlock);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_thread_rwlock_trywrlock(switch_thread_rwlock_t * rwlock)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_thread_rwlock_trywrlock(rwlock);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_thread_rwlock_unlock(switch_thread_rwlock_t * rwlock)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_thread_rwlock_unlock(rwlock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* thread mutex functions */
|
|
|
|
|
2007-03-30 00:15:25 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_mutex_init(switch_mutex_t ** lock, unsigned int flags, switch_memory_pool_t *pool)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_thread_mutex_create(lock, flags, pool);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_mutex_destroy(switch_mutex_t * lock)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_thread_mutex_destroy(lock);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_mutex_lock(switch_mutex_t * lock)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_thread_mutex_lock(lock);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_mutex_unlock(switch_mutex_t * lock)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_thread_mutex_unlock(lock);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_mutex_trylock(switch_mutex_t * lock)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_thread_mutex_trylock(lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* time function stubs */
|
|
|
|
|
|
|
|
SWITCH_DECLARE(switch_time_t) switch_time_now(void)
|
|
|
|
{
|
2007-03-29 22:31:56 +00:00
|
|
|
return (switch_time_t) apr_time_now();
|
2007-03-09 20:44:13 +00:00
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_time_exp_gmt_get(switch_time_t * result, switch_time_exp_t * input)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
2007-03-29 22:31:56 +00:00
|
|
|
return apr_time_exp_gmt_get(result, (apr_time_exp_t *) input);
|
2007-03-09 20:44:13 +00:00
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_time_exp_get(switch_time_t * result, switch_time_exp_t * input)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
2007-03-29 22:31:56 +00:00
|
|
|
return apr_time_exp_get(result, (apr_time_exp_t *) input);
|
2007-03-09 20:44:13 +00:00
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_time_exp_lt(switch_time_exp_t * result, switch_time_t input)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
2007-03-29 22:31:56 +00:00
|
|
|
return apr_time_exp_lt((apr_time_exp_t *) result, input);
|
2007-03-09 20:44:13 +00:00
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_time_exp_gmt(switch_time_exp_t * result, switch_time_t input)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
2007-03-29 22:31:56 +00:00
|
|
|
return apr_time_exp_gmt((apr_time_exp_t *) result, input);
|
2007-03-09 20:44:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(void) switch_sleep(switch_interval_time_t t)
|
|
|
|
{
|
|
|
|
#if defined(HAVE_USLEEP)
|
|
|
|
usleep(t);
|
|
|
|
#elif defined(WIN32)
|
2007-03-29 22:31:56 +00:00
|
|
|
Sleep((DWORD) ((t) / 1000));
|
2007-03-09 20:44:13 +00:00
|
|
|
#else
|
|
|
|
apr_sleep(t);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SWITCH_DECLARE(switch_status_t) switch_rfc822_date(char *date_str, switch_time_t t)
|
|
|
|
{
|
|
|
|
return apr_rfc822_date(date_str, t);
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(switch_time_t) switch_time_make(switch_time_t sec, int32_t usec)
|
|
|
|
{
|
2007-03-29 22:31:56 +00:00
|
|
|
return ((switch_time_t) (sec) * APR_USEC_PER_SEC + (switch_time_t) (usec));
|
2007-03-09 20:44:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Thread condition locks */
|
|
|
|
|
2007-03-30 00:15:25 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_thread_cond_create(switch_thread_cond_t ** cond, switch_memory_pool_t *pool)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_thread_cond_create(cond, pool);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_thread_cond_wait(switch_thread_cond_t * cond, switch_mutex_t * mutex)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_thread_cond_wait(cond, mutex);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_thread_cond_timedwait(switch_thread_cond_t * cond, switch_mutex_t * mutex, switch_interval_time_t timeout)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_thread_cond_timedwait(cond, mutex, timeout);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_thread_cond_signal(switch_thread_cond_t * cond)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_thread_cond_signal(cond);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_thread_cond_broadcast(switch_thread_cond_t * cond)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_thread_cond_broadcast(cond);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_thread_cond_destroy(switch_thread_cond_t * cond)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_thread_cond_destroy(cond);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* file i/o stubs */
|
|
|
|
|
2007-03-30 00:15:25 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_file_open(switch_file_t ** newf, const char *fname, int32_t flag, switch_fileperms_t perm,
|
|
|
|
switch_memory_pool_t *pool)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_file_open(newf, fname, flag, perm, pool);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_file_seek(switch_file_t * thefile, switch_seek_where_t where, int64_t *offset)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
apr_status_t rv;
|
2007-03-29 22:31:56 +00:00
|
|
|
apr_off_t off = (apr_off_t) (*offset);
|
2007-03-09 20:44:13 +00:00
|
|
|
rv = apr_file_seek(thefile, where, &off);
|
2007-03-29 22:31:56 +00:00
|
|
|
*offset = (int64_t) off;
|
2007-03-09 20:44:13 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_file_close(switch_file_t * thefile)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_file_close(thefile);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:15:25 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_file_remove(const char *path, switch_memory_pool_t *pool)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_file_remove(path, pool);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_file_read(switch_file_t * thefile, void *buf, switch_size_t *nbytes)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_file_read(thefile, buf, nbytes);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_file_write(switch_file_t * thefile, const void *buf, switch_size_t *nbytes)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_file_write(thefile, buf, nbytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(switch_status_t) switch_file_exists(const char *filename)
|
|
|
|
{
|
|
|
|
int32_t wanted = APR_FINFO_TYPE;
|
2007-03-29 22:31:56 +00:00
|
|
|
apr_finfo_t info = { 0 };
|
2007-03-09 20:44:13 +00:00
|
|
|
if (filename) {
|
|
|
|
apr_stat(&info, filename, wanted, NULL);
|
|
|
|
if (info.filetype != APR_NOFILE) {
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* thread stubs */
|
|
|
|
|
|
|
|
|
2007-03-30 00:15:25 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_threadattr_create(switch_threadattr_t ** new_attr, switch_memory_pool_t *pool)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_threadattr_create(new_attr, pool);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_threadattr_detach_set(switch_threadattr_t * attr, int32_t on)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_threadattr_detach_set(attr, on);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_threadattr_stacksize_set(switch_threadattr_t * attr, switch_size_t stacksize)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_threadattr_stacksize_set(attr, stacksize);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_thread_create(switch_thread_t ** new_thread, switch_threadattr_t * attr,
|
2007-03-30 00:15:25 +00:00
|
|
|
switch_thread_start_t func, void *data, switch_memory_pool_t *cont)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_thread_create(new_thread, attr, func, data, cont);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* socket stubs */
|
|
|
|
|
2007-03-30 00:15:25 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_socket_create(switch_socket_t ** new_sock, int family, int type, int protocol, switch_memory_pool_t *pool)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_socket_create(new_sock, family, type, protocol, pool);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_socket_shutdown(switch_socket_t * sock, switch_shutdown_how_e how)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_socket_shutdown(sock, how);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_socket_close(switch_socket_t * sock)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_socket_close(sock);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_socket_bind(switch_socket_t * sock, switch_sockaddr_t * sa)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_socket_bind(sock, sa);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_socket_listen(switch_socket_t * sock, int32_t backlog)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_socket_listen(sock, backlog);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:15:25 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_socket_accept(switch_socket_t ** new_sock, switch_socket_t * sock, switch_memory_pool_t *pool)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_socket_accept(new_sock, sock, pool);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_socket_connect(switch_socket_t * sock, switch_sockaddr_t * sa)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_socket_connect(sock, sa);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_socket_send(switch_socket_t * sock, const char *buf, switch_size_t *len)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_socket_send(sock, buf, len);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_socket_sendto(switch_socket_t * sock, switch_sockaddr_t * where, int32_t flags, const char *buf, switch_size_t *len)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_socket_sendto(sock, where, flags, buf, len);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_socket_recv(switch_socket_t * sock, char *buf, switch_size_t *len)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_socket_recv(sock, buf, len);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_sockaddr_info_get(switch_sockaddr_t ** sa, const char *hostname, int32_t family,
|
2007-03-30 00:15:25 +00:00
|
|
|
switch_port_t port, int32_t flags, switch_memory_pool_t *pool)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_sockaddr_info_get(sa, hostname, family, port, flags, pool);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_socket_opt_set(switch_socket_t * sock, int32_t opt, int32_t on)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_socket_opt_set(sock, opt, on);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_socket_timeout_set(switch_socket_t * sock, switch_interval_time_t t)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_socket_timeout_set(sock, t);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_sockaddr_ip_get(char **addr, switch_sockaddr_t * sa)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_sockaddr_ip_get(addr, sa);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_mcast_join(switch_socket_t * sock, switch_sockaddr_t * join, switch_sockaddr_t * iface, switch_sockaddr_t * source)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_mcast_join(sock, join, iface, source);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* socket functions */
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(char *) switch_get_addr(char *buf, switch_size_t len, switch_sockaddr_t * in)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
2007-03-29 22:31:56 +00:00
|
|
|
return get_addr(buf, len, &in->sa.sin.sin_addr);
|
2007-03-09 20:44:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(uint16_t) switch_sockaddr_get_port(switch_sockaddr_t * sa)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return sa->port;
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(int32_t) switch_sockaddr_get_family(switch_sockaddr_t * sa)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return sa->family;
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_socket_recvfrom(switch_sockaddr_t * from, switch_socket_t * sock, int32_t flags, char *buf, size_t *len)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
apr_status_t r;
|
|
|
|
|
|
|
|
if ((r = apr_socket_recvfrom(from, sock, flags, buf, len)) == APR_SUCCESS) {
|
|
|
|
from->port = ntohs(from->sa.sin.sin_port);
|
|
|
|
/* from->ipaddr_ptr = &(from->sa.sin.sin_addr);
|
|
|
|
* from->ipaddr_ptr = inet_ntoa(from->sa.sin.sin_addr);
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* poll stubs */
|
|
|
|
|
2007-03-30 00:15:25 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_pollset_create(switch_pollset_t ** pollset, uint32_t size, switch_memory_pool_t *p, uint32_t flags)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_pollset_create(pollset, size, p, flags);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_pollset_add(switch_pollset_t * pollset, const switch_pollfd_t * descriptor)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_pollset_add(pollset, descriptor);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_poll(switch_pollfd_t * aprset, int32_t numsock, int32_t *nsds, switch_interval_time_t timeout)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_poll(aprset, numsock, nsds, timeout);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:15:25 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_socket_create_pollfd(switch_pollfd_t ** poll, switch_socket_t * sock, int16_t flags, switch_memory_pool_t *pool)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
switch_pollset_t *pollset;
|
|
|
|
|
|
|
|
void *ptr = NULL;
|
|
|
|
|
|
|
|
if ((ptr = apr_palloc(pool, sizeof(switch_pollfd_t))) == 0) {
|
|
|
|
return SWITCH_STATUS_MEMERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (switch_pollset_create(&pollset, 1, pool, flags) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
return SWITCH_STATUS_GENERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(ptr, 0, sizeof(switch_pollfd_t));
|
|
|
|
*poll = ptr;
|
|
|
|
|
|
|
|
(*poll)->desc_type = APR_POLL_SOCKET;
|
|
|
|
(*poll)->reqevents = flags;
|
|
|
|
(*poll)->desc.s = sock;
|
|
|
|
(*poll)->client_data = sock;
|
|
|
|
|
|
|
|
if (switch_pollset_add(pollset, *poll) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
return SWITCH_STATUS_GENERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* apr-util stubs */
|
|
|
|
|
|
|
|
/* UUID Handling (apr-util) */
|
|
|
|
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(void) switch_uuid_format(char *buffer, const switch_uuid_t * uuid)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
2007-03-29 22:31:56 +00:00
|
|
|
apr_uuid_format(buffer, (const apr_uuid_t *) uuid);
|
2007-03-09 20:44:13 +00:00
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(void) switch_uuid_get(switch_uuid_t * uuid)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
2007-03-29 22:31:56 +00:00
|
|
|
apr_uuid_get((apr_uuid_t *) uuid);
|
2007-03-09 20:44:13 +00:00
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_uuid_parse(switch_uuid_t * uuid, const char *uuid_str)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
2007-03-29 22:31:56 +00:00
|
|
|
return apr_uuid_parse((apr_uuid_t *) uuid, uuid_str);
|
2007-03-09 20:44:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* FIFO queues (apr-util) */
|
|
|
|
|
2007-03-30 00:15:25 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_queue_create(switch_queue_t ** queue, unsigned int queue_capacity, switch_memory_pool_t *pool)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_queue_create(queue, queue_capacity, pool);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(unsigned int) switch_queue_size(switch_queue_t * queue)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_queue_size(queue);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_queue_pop(switch_queue_t * queue, void **data)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_queue_pop(queue, data);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_queue_push(switch_queue_t * queue, void *data)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_queue_push(queue, data);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_queue_trypop(switch_queue_t * queue, void **data)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_queue_trypop(queue, data);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_queue_trypush(switch_queue_t * queue, void *data)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return apr_queue_trypush(queue, data);
|
|
|
|
}
|
|
|
|
|
2007-03-24 19:33:34 +00:00
|
|
|
SWITCH_DECLARE(int) switch_vasprintf(char **ret, const char *fmt, va_list ap)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
2007-03-09 21:12:41 +00:00
|
|
|
#ifdef HAVE_VASPRINTF
|
2007-03-29 22:31:56 +00:00
|
|
|
return vasprintf(ret, fmt, ap);
|
2007-03-09 21:12:41 +00:00
|
|
|
#else
|
2007-03-24 19:33:34 +00:00
|
|
|
char *buf;
|
|
|
|
int len;
|
|
|
|
size_t buflen;
|
2007-03-24 20:12:41 +00:00
|
|
|
va_list ap2;
|
2007-03-24 19:33:34 +00:00
|
|
|
|
2007-03-24 20:12:41 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
ap2 = ap;
|
|
|
|
#else
|
|
|
|
va_copy(ap2, ap);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
len = vsnprintf(NULL, 0, fmt, ap2);
|
2007-03-24 19:54:38 +00:00
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
if (len > 0 && (buf = malloc((buflen = (size_t) (len + 1)))) != NULL) {
|
2007-03-24 19:33:34 +00:00
|
|
|
len = vsnprintf(buf, buflen, fmt, ap);
|
|
|
|
*ret = buf;
|
|
|
|
} else {
|
|
|
|
*ret = NULL;
|
2007-03-24 19:54:38 +00:00
|
|
|
len = -1;
|
2007-03-23 22:44:28 +00:00
|
|
|
}
|
2007-03-24 19:54:38 +00:00
|
|
|
|
2007-03-24 20:12:41 +00:00
|
|
|
va_end(ap2);
|
2007-03-24 19:33:34 +00:00
|
|
|
return len;
|
2007-03-23 22:44:28 +00:00
|
|
|
|
2007-03-09 21:12:41 +00:00
|
|
|
#endif
|
2007-03-09 20:44:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 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 expandtab:
|
|
|
|
*/
|