port.h 4.38 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 40
#include <ws2tcpip.h>

#ifdef _MSC_VER
41
#define strcasecmp _stricmp
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
#include <strings.h>
48
#endif
Simon Morlat's avatar
Simon Morlat committed
49 50 51 52 53 54

/*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
55 56 57 58
#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. */
59 60 61 62 63
#	ifndef AI_V4MAPPED
#	define AI_V4MAPPED 0x00000800
#	endif
#endif

64 65 66 67
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) || defined(__WIN32__)
#ifdef BELLESIP_INTERNAL_EXPORTS
#define BELLESIP_INTERNAL_EXPORT __declspec(dllexport)
#else
68
#define BELLESIP_INTERNAL_EXPORT
69 70 71 72 73
#endif
#else
#define BELLESIP_INTERNAL_EXPORT extern
#endif

Simon Morlat's avatar
Simon Morlat committed
74 75 76 77
/*
 * Socket abstraction layer
 */

78 79
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
80
int belle_sip_socket_set_nonblocking (belle_sip_socket_t sock);
Simon Morlat's avatar
Simon Morlat committed
81
int belle_sip_socket_set_dscp(belle_sip_socket_t sock, int ai_family, int dscp);
Simon Morlat's avatar
Simon Morlat committed
82
 
Simon Morlat's avatar
Simon Morlat committed
83 84
#if defined(WIN32)

85 86
typedef HANDLE belle_sip_thread_t;

87 88
#define belle_sip_thread_self_id()		(unsigned long)GetCurrentThreadId()
#define belle_sip_thread_get_id(thread)			(unsigned long)GetThreadId(thread)
89 90 91 92 93 94 95 96

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


97
static BELLESIP_INLINE void close_socket(belle_sip_socket_t s){
Simon Morlat's avatar
Simon Morlat committed
98 99 100
	closesocket(s);
}

101
static BELLESIP_INLINE int get_socket_error(void){
Simon Morlat's avatar
Simon Morlat committed
102 103 104
	return WSAGetLastError();
}

Simon Morlat's avatar
Simon Morlat committed
105
const char *belle_sip_get_socket_error_string();
Ghislain MARY's avatar
Ghislain MARY committed
106
const char *belle_sip_get_socket_error_string_from_code(int code);
Simon Morlat's avatar
Simon Morlat committed
107

108
#ifdef WINAPI_FAMILY_PHONE_APP
109
BELLESIP_INTERNAL_EXPORT void belle_sip_sleep(unsigned int ms);
Ghislain MARY's avatar
Ghislain MARY committed
110 111 112 113
#else
#define belle_sip_sleep Sleep
#endif

114
#define usleep(us) belle_sip_sleep((us)/1000)
115
static BELLESIP_INLINE int inet_aton(const char *ip, struct in_addr *p){
Simon Morlat's avatar
Simon Morlat committed
116 117 118 119
	*(long*)p=inet_addr(ip);
	return 0;
}

Ghislain MARY's avatar
Ghislain MARY committed
120 121
#define BELLESIP_EWOULDBLOCK WSAEWOULDBLOCK
#define BELLESIP_EINPROGRESS WSAEINPROGRESS
Simon Morlat's avatar
Simon Morlat committed
122 123 124

#else

125
typedef pthread_t belle_sip_thread_t;
126 127
#define belle_sip_thread_self_id()			(unsigned long)pthread_self()
#define belle_sip_thread_get_id(thread)			(unsigned long)thread
128 129 130 131 132 133 134

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)

135
static BELLESIP_INLINE void close_socket(belle_sip_socket_t s){
Simon Morlat's avatar
Simon Morlat committed
136 137 138
	close(s);
}

139
static BELLESIP_INLINE int get_socket_error(void){
Simon Morlat's avatar
Simon Morlat committed
140 141 142 143 144
	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
145 146 147
#define BELLESIP_EWOULDBLOCK EWOULDBLOCK
#define BELLESIP_EINPROGRESS EINPROGRESS

Simon Morlat's avatar
Simon Morlat committed
148 149
#endif

150
#define belle_sip_error_code_is_would_block(err) ((err)==BELLESIP_EWOULDBLOCK || (err)==BELLESIP_EINPROGRESS)
Simon Morlat's avatar
Simon Morlat committed
151 152 153

#endif