port.h 4.77 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
    (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_port_h
#define belle_sip_port_h

22 23
#include <sys/stat.h>

Simon Morlat's avatar
Simon Morlat committed
24 25 26 27 28 29 30
#ifndef WIN32
#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
31
#include <netinet/tcp.h>
32
#include <pthread.h>
33
#include <strings.h>
Simon Morlat's avatar
Simon Morlat committed
34 35 36 37

#else

#include <winsock2.h>
38 39
#include <ws2tcpip.h>

40
#define strcasecmp _stricmp
Ghislain MARY's avatar
Ghislain MARY committed
41
#ifdef _MSC_VER
42
#define snprintf _snprintf
Ghislain MARY's avatar
Ghislain MARY committed
43
#define vsnprintf _vsnprintf
44
#define strdup _strdup
Simon Morlat's avatar
Simon Morlat committed
45 46
#else
#include <stdint.h>
47
#endif
Simon Morlat's avatar
Simon Morlat committed
48 49 50 51 52 53

/*AI_NUMERICSERV is not defined for windows XP. Since it is not essential, we define it to 0 (does nothing)*/
#ifndef AI_NUMERICSERV
#define AI_NUMERICSERV 0
#endif

Ghislain MARY's avatar
Ghislain MARY committed
54 55 56 57
#endif

#if defined(WIN32) || defined(__QNX__)
/* Mingw32 does not define AI_V4MAPPED, however it is supported starting from Windows Vista. QNX also does not define AI_V4MAPPED. */
58 59 60
#	ifndef AI_V4MAPPED
#	define AI_V4MAPPED 0x00000800
#	endif
61
#	ifndef AI_ALL
Simon Morlat's avatar
Simon Morlat committed
62
#	define AI_ALL 0x00000100
63
#	endif
64 65 66
#	ifndef IPV6_V6ONLY
#	define IPV6_V6ONLY 27
#	endif
67 68
#endif

69 70 71 72
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) || defined(__WIN32__)
#ifdef BELLESIP_INTERNAL_EXPORTS
#define BELLESIP_INTERNAL_EXPORT __declspec(dllexport)
#else
73
#define BELLESIP_INTERNAL_EXPORT
74 75 76 77 78
#endif
#else
#define BELLESIP_INTERNAL_EXPORT extern
#endif

Simon Morlat's avatar
Simon Morlat committed
79 80 81 82
/*
 * Socket abstraction layer
 */

83 84
BELLESIP_INTERNAL_EXPORT int belle_sip_init_sockets(void);
BELLESIP_INTERNAL_EXPORT void belle_sip_uninit_sockets(void);
85
int belle_sip_socket_set_nonblocking (belle_sip_socket_t sock);
Simon Morlat's avatar
Simon Morlat committed
86
int belle_sip_socket_set_dscp(belle_sip_socket_t sock, int ai_family, int dscp);
87
int belle_sip_socket_enable_dual_stack(belle_sip_socket_t sock);
88

Simon Morlat's avatar
Simon Morlat committed
89 90
#if defined(WIN32)

91 92 93 94 95
#if defined(__MINGW32__) || !defined(WINAPI_FAMILY_PARTITION)
// Only use with x being WINAPI_PARTITION_DESKTOP to test if building on desktop
#define WINAPI_FAMILY_PARTITION(x) 1
#endif

96 97
typedef HANDLE belle_sip_thread_t;

98 99
#define belle_sip_thread_self_id()		(unsigned long)GetCurrentThreadId()
#define belle_sip_thread_get_id(thread)			(unsigned long)GetThreadId(thread)
100 101 102 103 104 105 106 107

typedef DWORD belle_sip_thread_key_t;
int belle_sip_thread_key_create(belle_sip_thread_key_t *key, void (*destructor)(void*) );
int belle_sip_thread_setspecific(belle_sip_thread_key_t key,const void *value);
const void* belle_sip_thread_getspecific(belle_sip_thread_key_t key);
int belle_sip_thread_key_delete(belle_sip_thread_key_t key);


108
static BELLESIP_INLINE void close_socket(belle_sip_socket_t s){
Simon Morlat's avatar
Simon Morlat committed
109 110 111
	closesocket(s);
}

112
static BELLESIP_INLINE int get_socket_error(void){
Simon Morlat's avatar
Simon Morlat committed
113 114 115
	return WSAGetLastError();
}

116
const char *belle_sip_get_socket_error_string();
117
const char *belle_sip_get_socket_error_string_from_code(int code);
118

119
#if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
120
BELLESIP_INTERNAL_EXPORT void belle_sip_sleep(unsigned int ms);
Ghislain MARY's avatar
Ghislain MARY committed
121 122 123 124
#else
#define belle_sip_sleep Sleep
#endif

125
#define usleep(us) belle_sip_sleep((us)/1000)
126
static BELLESIP_INLINE int inet_aton(const char *ip, struct in_addr *p){
Simon Morlat's avatar
Simon Morlat committed
127 128 129 130
	*(long*)p=inet_addr(ip);
	return 0;
}

131 132
#define BELLESIP_EWOULDBLOCK WSAEWOULDBLOCK
#define BELLESIP_EINPROGRESS WSAEINPROGRESS
Simon Morlat's avatar
Simon Morlat committed
133 134 135

#else

136 137
#define WINAPI_FAMILY_PARTITION(x) 1

138
typedef pthread_t belle_sip_thread_t;
139 140
#define belle_sip_thread_self_id()			(unsigned long)pthread_self()
#define belle_sip_thread_get_id(thread)			(unsigned long)thread
141 142 143 144 145 146 147

typedef pthread_key_t belle_sip_thread_key_t;
#define belle_sip_thread_key_create(key,destructor)		pthread_key_create(key,destructor)
#define belle_sip_thread_setspecific(key,value)			pthread_setspecific(key,value)
#define belle_sip_thread_getspecific(key)			pthread_getspecific(key)
#define belle_sip_thread_key_delete(key)				pthread_key_delete(key)

148
static BELLESIP_INLINE void close_socket(belle_sip_socket_t s){
Simon Morlat's avatar
Simon Morlat committed
149 150 151
	close(s);
}

152
static BELLESIP_INLINE int get_socket_error(void){
Simon Morlat's avatar
Simon Morlat committed
153 154 155 156 157
	return errno;
}
#define belle_sip_get_socket_error_string() strerror(errno)
#define belle_sip_get_socket_error_string_from_code(code) strerror(code)

158 159 160
#define BELLESIP_EWOULDBLOCK EWOULDBLOCK
#define BELLESIP_EINPROGRESS EINPROGRESS

Simon Morlat's avatar
Simon Morlat committed
161 162
#endif

163
#define belle_sip_error_code_is_would_block(err) ((err)==BELLESIP_EWOULDBLOCK || (err)==BELLESIP_EINPROGRESS)
Simon Morlat's avatar
Simon Morlat committed
164 165 166

#endif