utils.h 5.99 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>
Simon Morlat's avatar
Simon Morlat committed
25
#include <errno.h>
Ghislain MARY's avatar
Ghislain MARY committed
26
#include "belle-sip/defs.h"
Simon Morlat's avatar
Simon Morlat committed
27 28 29 30 31 32 33 34 35 36 37


BELLE_SIP_BEGIN_DECLS

BELLESIP_EXPORT void *belle_sip_malloc(size_t size);
BELLESIP_EXPORT void *belle_sip_malloc0(size_t size);
BELLESIP_EXPORT void *belle_sip_realloc(void *ptr, size_t size);
BELLESIP_EXPORT void belle_sip_free(void *ptr);
BELLESIP_EXPORT char * belle_sip_strdup(const char *s);

BELLE_SIP_END_DECLS
38

Simon Morlat's avatar
Simon Morlat committed
39 40 41 42 43
/***************/
/* logging api */
/***************/

typedef enum {
Simon Morlat's avatar
Simon Morlat committed
44 45 46
	BELLE_SIP_LOG_FATAL=1,
	BELLE_SIP_LOG_ERROR=1<<1,
	BELLE_SIP_LOG_WARNING=1<<2,
47 48 49
	BELLE_SIP_LOG_MESSAGE=1<<3,
	BELLE_SIP_LOG_DEBUG=1<<4,
	BELLE_SIP_LOG_END=1<<5
Simon Morlat's avatar
Simon Morlat committed
50 51 52 53 54
} belle_sip_log_level;


typedef void (*belle_sip_log_function_t)(belle_sip_log_level lev, const char *fmt, va_list args);

55

Ghislain MARY's avatar
Ghislain MARY committed
56 57 58 59 60 61 62
typedef enum {
	BELLE_SIP_NOT_IMPLEMENTED = -2,
	BELLE_SIP_BUFFER_OVERFLOW = -1,
	BELLE_SIP_OK = 0
} belle_sip_error_code;


63 64 65 66 67 68
#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
69 70
BELLE_SIP_BEGIN_DECLS

Simon Morlat's avatar
Simon Morlat committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84
extern belle_sip_log_function_t belle_sip_logv_out;

extern unsigned int __belle_sip_log_mask;

#define belle_sip_log_level_enabled(level)   (__belle_sip_log_mask & (level))

#if !defined(WIN32) && !defined(_WIN32_WCE)
#define belle_sip_logv(level,fmt,args) \
{\
        if (belle_sip_logv_out!=NULL && belle_sip_log_level_enabled(level)) \
                belle_sip_logv_out(level,fmt,args);\
        if ((level)==BELLE_SIP_LOG_FATAL) abort();\
}while(0)
#else
85
BELLESIP_EXPORT void belle_sip_logv(int level, const char *fmt, va_list args);
Simon Morlat's avatar
Simon Morlat committed
86 87 88 89
#endif


#ifdef BELLE_SIP_DEBUG_MODE
90
static BELLESIP_INLINE void belle_sip_debug(const char *fmt,...)
Simon Morlat's avatar
Simon Morlat committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
{
  va_list args;
  va_start (args, fmt);
  belle_sip_logv(BELLE_SIP_LOG_DEBUG, fmt, args);
  va_end (args);
}
#else

#define belle_sip_debug(...)

#endif

#ifdef BELLE_SIP_NOMESSAGE_MODE

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

#else

111
static BELLESIP_INLINE void BELLE_SIP_CHECK_FORMAT_ARGS(2,3) belle_sip_log(belle_sip_log_level lev, const char *fmt,...){
Simon Morlat's avatar
Simon Morlat committed
112 113 114 115 116 117
        va_list args;
        va_start (args, fmt);
        belle_sip_logv(lev, fmt, args);
        va_end (args);
}

118
static BELLESIP_INLINE void BELLE_SIP_CHECK_FORMAT_ARGS(1,2) belle_sip_message(const char *fmt,...)
Simon Morlat's avatar
Simon Morlat committed
119 120 121 122 123 124 125
{
        va_list args;
        va_start (args, fmt);
        belle_sip_logv(BELLE_SIP_LOG_MESSAGE, fmt, args);
        va_end (args);
}

126
static BELLESIP_INLINE void BELLE_SIP_CHECK_FORMAT_ARGS(1,2) belle_sip_warning(const char *fmt,...)
Simon Morlat's avatar
Simon Morlat committed
127 128 129 130 131 132 133 134 135
{
        va_list args;
        va_start (args, fmt);
        belle_sip_logv(BELLE_SIP_LOG_WARNING, fmt, args);
        va_end (args);
}

#endif

136
static BELLESIP_INLINE void BELLE_SIP_CHECK_FORMAT_ARGS(1,2) belle_sip_error(const char *fmt,...)
Simon Morlat's avatar
Simon Morlat committed
137 138 139 140 141 142 143
{
        va_list args;
        va_start (args, fmt);
        belle_sip_logv(BELLE_SIP_LOG_ERROR, fmt, args);
        va_end (args);
}

144
static BELLESIP_INLINE void BELLE_SIP_CHECK_FORMAT_ARGS(1,2) belle_sip_fatal(const char *fmt,...)
Simon Morlat's avatar
Simon Morlat committed
145 146 147 148 149 150 151 152 153
{
        va_list args;
        va_start (args, fmt);
        belle_sip_logv(BELLE_SIP_LOG_FATAL, fmt, args);
        va_end (args);
}



154 155
BELLESIP_EXPORT void belle_sip_set_log_file(FILE *file);
BELLESIP_EXPORT void belle_sip_set_log_handler(belle_sip_log_function_t func);
156
BELLESIP_EXPORT belle_sip_log_function_t belle_sip_get_log_handler();
Simon Morlat's avatar
Simon Morlat committed
157

158
BELLESIP_EXPORT char * BELLE_SIP_CHECK_FORMAT_ARGS(1,2) belle_sip_strdup_printf(const char *fmt,...);
Simon Morlat's avatar
Simon Morlat committed
159

160
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, ...);
161
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
162

163
BELLESIP_EXPORT void belle_sip_set_log_level(int level);
Simon Morlat's avatar
Simon Morlat committed
164

Sylvain Berfini's avatar
Sylvain Berfini committed
165
BELLESIP_EXPORT char * belle_sip_random_token(char *ret, size_t size);
Simon Morlat's avatar
Simon Morlat committed
166

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

Ghislain MARY's avatar
Ghislain MARY committed
169
BELLESIP_EXPORT char * belle_sip_octets_to_text(const unsigned char *hash, size_t hash_len, char *ret, size_t size);
Simon Morlat's avatar
Simon Morlat committed
170

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

Sylvain Berfini's avatar
Sylvain Berfini committed
173
BELLESIP_EXPORT const char* belle_sip_version_to_string();
174

175 176 177 178 179
/**
 * 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
180 181 182 183
BELLESIP_EXPORT uint64_t belle_sip_time_ms(void);

BELLESIP_EXPORT unsigned int belle_sip_random(void);

184 185 186
#if defined(WIN32)

#include <winsock2.h>
187
#include <ws2tcpip.h>
188 189

typedef SOCKET belle_sip_socket_t;
190
typedef HANDLE belle_sip_fd_t;
Simon Morlat's avatar
Simon Morlat committed
191
#else
192 193 194 195 196

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

197
typedef int belle_sip_socket_t;
198
typedef int belle_sip_fd_t;
199

Simon Morlat's avatar
Simon Morlat committed
200 201
#endif

202 203 204
BELLESIP_EXPORT int belle_sip_getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res);
BELLESIP_EXPORT void belle_sip_freeaddrinfo(struct addrinfo *res);

205 206

typedef void (*belle_sip_background_task_end_callback_t)(void *);
jehan's avatar
jehan committed
207 208 209
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);

210

Simon Morlat's avatar
Simon Morlat committed
211 212 213 214
BELLE_SIP_END_DECLS

#endif