1 /*
2 	belle-sip - SIP (RFC3261) library.
3     Copyright (C) 2010  Belledonne Communications SARL
4 
5     This program is free software: you can redistribute it and/or modify
6     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
8     (at your option) any later version.
9 
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License
16     along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #ifndef BELLE_SIP_UTILS_H
20 #define BELLE_SIP_UTILS_H
21 
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <errno.h>
26 #include "belle-sip/defs.h"
27 
28 #ifdef BCTBX_LOG_DOMAIN
29 #undef BCTBX_LOG_DOMAIN
30 #endif
31 #ifndef BELLE_SIP_LOG_DOMAIN
32 #define BELLE_SIP_LOG_DOMAIN "belle-sip"
33 #endif
34 
35 #define BCTBX_LOG_DOMAIN BELLE_SIP_LOG_DOMAIN
36 
37 
38 #include "bctoolbox/logging.h"
39 #include "bctoolbox/vconnect.h"
40 
41 BELLE_SIP_BEGIN_DECLS
42 
43 #define belle_sip_malloc bctbx_malloc
44 #define belle_sip_malloc0 bctbx_malloc0
45 #define belle_sip_realloc bctbx_realloc
46 #define belle_sip_free bctbx_free
47 #define belle_sip_strdup bctbx_strdup
48 
49 BELLE_SIP_END_DECLS
50 
51 /***************/
52 /* logging api */
53 /***************/
54 
55 #define BELLE_SIP_LOG_FATAL BCTBX_LOG_FATAL
56 #define	BELLE_SIP_LOG_ERROR BCTBX_LOG_ERROR
57 #define	BELLE_SIP_LOG_WARNING BCTBX_LOG_WARNING
58 #define	BELLE_SIP_LOG_MESSAGE BCTBX_LOG_MESSAGE
59 #define	BELLE_SIP_LOG_DEBUG	BCTBX_LOG_DEBUG
60 #define	BELLE_SIP_LOG_END BCTBX_LOG_END
61 #define  belle_sip_log_level BctbxLogLevel
62 
63 #define belle_sip_log_function_t BctoolboxLogFunc
64 
65 
66 typedef enum {
67 	BELLE_SIP_NOT_IMPLEMENTED = -2,
68 	BELLE_SIP_BUFFER_OVERFLOW = -1,
69 	BELLE_SIP_OK = 0
70 } belle_sip_error_code;
71 
72 
73 #ifdef __GNUC__
74 #define BELLE_SIP_CHECK_FORMAT_ARGS(m,n) __attribute__((format(printf,m,n)))
75 #else
76 #define BELLE_SIP_CHECK_FORMAT_ARGS(m,n)
77 #endif
78 
79 BELLE_SIP_BEGIN_DECLS
80 
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 #else
86 
87 #define belle_sip_debug(...)
88 
89 #endif
90 
91 #ifdef BELLE_SIP_NOMESSAGE_MODE
92 
93 #define belle_sip_log(...)
94 #define belle_sip_message(...)
95 #define belle_sip_warning(...)
96 
97 #else
98 
99 #define belle_sip_log bctbx_log
100 #define belle_sip_message bctbx_message
101 #define belle_sip_warning bctbx_warning
102 #define belle_sip_error bctbx_error
103 #define belle_sip_fatal bctbx_fatal
104 #define belle_sip_logv bctbx_logv
105 #endif
106 
107 
108 #define belle_sip_set_log_file bctbx_set_log_file
109 #define belle_sip_set_log_handler bctbx_set_log_handler
110 #define belle_sip_get_log_handler bctbx_get_log_handler
111 
112 #define belle_sip_strdup_printf bctbx_strdup_printf
113 #define belle_sip_strcat_vprintf bctbx_strcat_vprintf
114 #define belle_sip_strcat_printf bctbx_strcat_printf
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);
118 
119 #define belle_sip_set_log_level(level) bctbx_set_log_level(BELLE_SIP_LOG_DOMAIN,level);
120 
121 BELLESIP_EXPORT char * belle_sip_random_token(char *ret, size_t size);
122 
123 BELLESIP_EXPORT unsigned char * belle_sip_random_bytes(unsigned char *ret, size_t size);
124 
125 BELLESIP_EXPORT char * belle_sip_octets_to_text(const unsigned char *hash, size_t hash_len, char *ret, size_t size);
126 
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  * Returns string without surrounding quotes if any, else just call belle_sip_strdup().
133 **/
134 BELLESIP_EXPORT char *belle_sip_unquote_strdup(const char *str);
135 
136 BELLESIP_EXPORT uint64_t belle_sip_time_ms(void);
137 
138 BELLESIP_EXPORT unsigned int belle_sip_random(void);
139 
140 /** Connect API */
141 BELLESIP_EXPORT void belle_sip_set_socket_api(bctbx_vsocket_api_t* my_api);
142 
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 #include <sys/types.h>
153 #include <sys/socket.h>
154 #include <netdb.h>
155 
156 typedef int belle_sip_socket_t;
157 typedef int belle_sip_fd_t;
158 
159 #endif
160 
161 
162 typedef void (*belle_sip_background_task_end_callback_t)(void *);
163 BELLESIP_EXPORT unsigned long belle_sip_begin_background_task(const char *name, belle_sip_background_task_end_callback_t cb, void *data);
164 BELLESIP_EXPORT void belle_sip_end_background_task(unsigned long id);
165 
166 /**
167  * create a directory if it doesn't already exists
168  *
169  * @param[in]   path        The directory to be created
170  * @return 0 in case of succes, -1 otherwise, note it returns -1 if the directory already exists
171  */
172 BELLESIP_EXPORT int belle_sip_mkdir(const char *path);
173 
174 BELLE_SIP_END_DECLS
175 
176 #endif
177 
178