port.h 4.53 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>
Simon Morlat's avatar
Simon Morlat committed
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);
Simon Morlat's avatar
Simon Morlat committed
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);
Simon Morlat's avatar
Simon Morlat committed
88
 
Simon Morlat's avatar
Simon Morlat committed
89 90
#if defined(WIN32)

91 92
typedef HANDLE belle_sip_thread_t;

93 94
#define belle_sip_thread_self_id()		(unsigned long)GetCurrentThreadId()
#define belle_sip_thread_get_id(thread)			(unsigned long)GetThreadId(thread)
95 96 97 98 99 100 101 102

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);


103
static BELLESIP_INLINE void close_socket(belle_sip_socket_t s){
Simon Morlat's avatar
Simon Morlat committed
104 105 106
	closesocket(s);
}

107
static BELLESIP_INLINE int get_socket_error(void){
Simon Morlat's avatar
Simon Morlat committed
108 109 110
	return WSAGetLastError();
}

Simon Morlat's avatar
Simon Morlat committed
111
const char *belle_sip_get_socket_error_string();
Ghislain MARY's avatar
Ghislain MARY committed
112
const char *belle_sip_get_socket_error_string_from_code(int code);
Simon Morlat's avatar
Simon Morlat committed
113

114
#ifdef WINAPI_FAMILY_PHONE_APP
115
BELLESIP_INTERNAL_EXPORT void belle_sip_sleep(unsigned int ms);
Ghislain MARY's avatar
Ghislain MARY committed
116 117 118 119
#else
#define belle_sip_sleep Sleep
#endif

120
#define usleep(us) belle_sip_sleep((us)/1000)
121
static BELLESIP_INLINE int inet_aton(const char *ip, struct in_addr *p){
Simon Morlat's avatar
Simon Morlat committed
122 123 124 125
	*(long*)p=inet_addr(ip);
	return 0;
}

Ghislain MARY's avatar
Ghislain MARY committed
126 127
#define BELLESIP_EWOULDBLOCK WSAEWOULDBLOCK
#define BELLESIP_EINPROGRESS WSAEINPROGRESS
Simon Morlat's avatar
Simon Morlat committed
128 129 130

#else

131
typedef pthread_t belle_sip_thread_t;
132 133
#define belle_sip_thread_self_id()			(unsigned long)pthread_self()
#define belle_sip_thread_get_id(thread)			(unsigned long)thread
134 135 136 137 138 139 140

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)

141
static BELLESIP_INLINE void close_socket(belle_sip_socket_t s){
Simon Morlat's avatar
Simon Morlat committed
142 143 144
	close(s);
}

145
static BELLESIP_INLINE int get_socket_error(void){
Simon Morlat's avatar
Simon Morlat committed
146 147 148 149 150
	return errno;
}
#define belle_sip_get_socket_error_string() strerror(errno)
#define belle_sip_get_socket_error_string_from_code(code) strerror(code)

Ghislain MARY's avatar
Ghislain MARY committed
151 152 153
#define BELLESIP_EWOULDBLOCK EWOULDBLOCK
#define BELLESIP_EINPROGRESS EINPROGRESS

Simon Morlat's avatar
Simon Morlat committed
154 155
#endif

156
#define belle_sip_error_code_is_would_block(err) ((err)==BELLESIP_EWOULDBLOCK || (err)==BELLESIP_EINPROGRESS)
Simon Morlat's avatar
Simon Morlat committed
157 158 159

#endif