utils.h 4.9 KB
Newer Older
Simon Morlat's avatar
Simon Morlat committed
1 2 3 4 5 6
/*
	belle-sip - SIP (RFC3261) library.
    Copyright (C) 2010  Belledonne Communications SARL

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
7
    the Free Software Foundation, either version 2 of the License, or
Simon Morlat's avatar
Simon Morlat committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef BELLE_SIP_UTILS_H
#define BELLE_SIP_UTILS_H

#include <stdarg.h>
#include <stdio.h>
Simon Morlat's avatar
Simon Morlat committed
24
#include <stdlib.h>
25
#include <errno.h>
26
#include "belle-sip/defs.h"
Simon Morlat's avatar
Simon Morlat committed
27

28 29 30 31 32 33 34 35 36 37 38
#ifdef BCTBX_LOG_DOMAIN
#undef BCTBX_LOG_DOMAIN
#endif
#ifndef BELLE_SIP_LOG_DOMAIN
#define BELLE_SIP_LOG_DOMAIN "belle-sip"
#endif

#define BCTBX_LOG_DOMAIN BELLE_SIP_LOG_DOMAIN


#include "bctoolbox/logging.h"
39
#include "bctoolbox/vconnect.h"
Simon Morlat's avatar
Simon Morlat committed
40 41 42

BELLE_SIP_BEGIN_DECLS

43 44 45 46 47
#define belle_sip_malloc bctbx_malloc
#define belle_sip_malloc0 bctbx_malloc0
#define belle_sip_realloc bctbx_realloc
#define belle_sip_free bctbx_free
#define belle_sip_strdup bctbx_strdup
Simon Morlat's avatar
Simon Morlat committed
48 49

BELLE_SIP_END_DECLS
50

Simon Morlat's avatar
Simon Morlat committed
51 52 53 54
/***************/
/* logging api */
/***************/

55 56 57 58 59 60 61
#define BELLE_SIP_LOG_FATAL BCTBX_LOG_FATAL
#define	BELLE_SIP_LOG_ERROR BCTBX_LOG_ERROR
#define	BELLE_SIP_LOG_WARNING BCTBX_LOG_WARNING
#define	BELLE_SIP_LOG_MESSAGE BCTBX_LOG_MESSAGE
#define	BELLE_SIP_LOG_DEBUG	BCTBX_LOG_DEBUG
#define	BELLE_SIP_LOG_END BCTBX_LOG_END
#define  belle_sip_log_level BctbxLogLevel
Simon Morlat's avatar
Simon Morlat committed
62

63
#define belle_sip_log_function_t BctoolboxLogFunc
Simon Morlat's avatar
Simon Morlat committed
64

65

Ghislain MARY's avatar
Ghislain MARY committed
66 67 68 69 70 71 72
typedef enum {
	BELLE_SIP_NOT_IMPLEMENTED = -2,
	BELLE_SIP_BUFFER_OVERFLOW = -1,
	BELLE_SIP_OK = 0
} belle_sip_error_code;


73 74 75 76 77 78
#ifdef __GNUC__
#define BELLE_SIP_CHECK_FORMAT_ARGS(m,n) __attribute__((format(printf,m,n)))
#else
#define BELLE_SIP_CHECK_FORMAT_ARGS(m,n)
#endif

Simon Morlat's avatar
Simon Morlat committed
79 80
BELLE_SIP_BEGIN_DECLS

81
#define belle_sip_log_level_enabled(level) bctbx_log_level_enabled(BELLE_SIP_LOG_DOMAIN,level)
82 83

#ifdef BELLE_SIP_DEBUG_MODE
84
#define belle_sip_deb(...) bctbx_debug(...)
85 86 87 88 89 90 91 92 93 94 95 96 97 98
#else

#define belle_sip_debug(...)

#endif

#ifdef BELLE_SIP_NOMESSAGE_MODE

#define belle_sip_log(...)
#define belle_sip_message(...)
#define belle_sip_warning(...)

#else

99 100 101 102 103 104
#define belle_sip_log bctbx_log
#define belle_sip_message bctbx_message
#define belle_sip_warning bctbx_warning
#define belle_sip_error bctbx_error
#define belle_sip_fatal bctbx_fatal
#define belle_sip_logv bctbx_logv
105 106 107
#endif


108 109 110
#define belle_sip_set_log_file bctbx_set_log_file
#define belle_sip_set_log_handler bctbx_set_log_handler
#define belle_sip_get_log_handler bctbx_get_log_handler
Simon Morlat's avatar
Simon Morlat committed
111

112 113 114
#define belle_sip_strdup_printf bctbx_strdup_printf
#define belle_sip_strcat_vprintf bctbx_strcat_vprintf
#define belle_sip_strcat_printf bctbx_strcat_printf
Simon Morlat's avatar
Simon Morlat committed
115

116
BELLESIP_EXPORT belle_sip_error_code BELLE_SIP_CHECK_FORMAT_ARGS(4,5) belle_sip_snprintf(char *buff, size_t buff_size, size_t *offset, const char *fmt, ...);
117
BELLESIP_EXPORT belle_sip_error_code belle_sip_snprintf_valist(char *buff, size_t buff_size, size_t *offset, const char *fmt, va_list args);
Ghislain MARY's avatar
Ghislain MARY committed
118

119
#define belle_sip_set_log_level(level) bctbx_set_log_level(BELLE_SIP_LOG_DOMAIN,level);
Simon Morlat's avatar
Simon Morlat committed
120

Sylvain Berfini's avatar
Sylvain Berfini committed
121
BELLESIP_EXPORT char * belle_sip_random_token(char *ret, size_t size);
122

Ghislain MARY's avatar
Ghislain MARY committed
123
BELLESIP_EXPORT unsigned char * belle_sip_random_bytes(unsigned char *ret, size_t size);
124

Ghislain MARY's avatar
Ghislain MARY committed
125
BELLESIP_EXPORT char * belle_sip_octets_to_text(const unsigned char *hash, size_t hash_len, char *ret, size_t size);
126

Ghislain MARY's avatar
Ghislain MARY committed
127
BELLESIP_EXPORT char * belle_sip_create_tag(char *ret, size_t size);
128

129
BELLESIP_EXPORT const char* belle_sip_version_to_string(void);
130

131 132 133 134 135
/**
 * Returns string without surrounding quotes if any, else just call belle_sip_strdup().
**/
BELLESIP_EXPORT char *belle_sip_unquote_strdup(const char *str);

Simon Morlat's avatar
Simon Morlat committed
136 137 138 139
BELLESIP_EXPORT uint64_t belle_sip_time_ms(void);

BELLESIP_EXPORT unsigned int belle_sip_random(void);

140 141 142
/** Connect API */
BELLESIP_EXPORT void belle_sip_set_socket_api(bctbx_vsocket_api_t* my_api);

143
#if defined(_WIN32)
144 145

#include <winsock2.h>
146
#include <ws2tcpip.h>
147 148

typedef SOCKET belle_sip_socket_t;
149
typedef HANDLE belle_sip_fd_t;
150
#else
151 152 153 154 155

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>

156
typedef int belle_sip_socket_t;
157
typedef int belle_sip_fd_t;
158

159 160
#endif

161 162

typedef void (*belle_sip_background_task_end_callback_t)(void *);
jehan's avatar
jehan committed
163 164 165
BELLESIP_EXPORT unsigned long belle_sip_begin_background_task(const char *name, belle_sip_background_task_end_callback_t cb, void *data);
BELLESIP_EXPORT void belle_sip_end_background_task(unsigned long id);

166 167 168 169 170 171 172
/**
 * create a directory if it doesn't already exists
 *
 * @param[in]   path        The directory to be created
 * @return 0 in case of succes, -1 otherwise, note it returns -1 if the directory already exists
 */
BELLESIP_EXPORT int belle_sip_mkdir(const char *path);
173

Simon Morlat's avatar
Simon Morlat committed
174 175 176 177
BELLE_SIP_END_DECLS

#endif