port.h 4.03 KB
Newer Older
Simon Morlat's avatar
Simon Morlat committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
	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
    the Free Software Foundation, either version 3 of the License, or
    (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>
Simon Morlat's avatar
Simon Morlat committed
33 34 35 36

#else

#include <winsock2.h>
37 38 39 40 41 42 43 44
#include <ws2tcpip.h>

#ifdef _MSC_VER
typedef unsigned long long uint64_t;
typedef long long int64_t;
typedef unsigned char uint8_t;
#define strcasecmp(a,b) _stricmp(a,b)
#define snprintf _snprintf
Ghislain MARY's avatar
Ghislain MARY committed
45
#define vsnprintf _vsnprintf
46
#define strdup _strdup
Simon Morlat's avatar
Simon Morlat committed
47 48
#else
#include <stdint.h>
49
#endif
Simon Morlat's avatar
Simon Morlat committed
50 51 52 53 54 55 56 57

/*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

#endif

58 59 60 61
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) || defined(__WIN32__)
#ifdef BELLESIP_INTERNAL_EXPORTS
#define BELLESIP_INTERNAL_EXPORT __declspec(dllexport)
#else
62
#define BELLESIP_INTERNAL_EXPORT
63 64 65 66 67
#endif
#else
#define BELLESIP_INTERNAL_EXPORT extern
#endif

Simon Morlat's avatar
Simon Morlat committed
68 69 70 71
/*
 * Socket abstraction layer
 */

72 73
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
74
int belle_sip_socket_set_nonblocking (belle_sip_socket_t sock);
Simon Morlat's avatar
Simon Morlat committed
75
int belle_sip_socket_set_dscp(belle_sip_socket_t sock, int ai_family, int dscp);
Simon Morlat's avatar
Simon Morlat committed
76
 
Simon Morlat's avatar
Simon Morlat committed
77 78
#if defined(WIN32)

79 80 81 82 83 84 85 86 87 88 89
typedef HANDLE belle_sip_thread_t;

#define belle_sip_thread_self()		GetCurrentThread()

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


90
static BELLESIP_INLINE void close_socket(belle_sip_socket_t s){
Simon Morlat's avatar
Simon Morlat committed
91 92 93
	closesocket(s);
}

94
static BELLESIP_INLINE int get_socket_error(void){
Simon Morlat's avatar
Simon Morlat committed
95 96 97
	return WSAGetLastError();
}

Simon Morlat's avatar
Simon Morlat committed
98
const char *belle_sip_get_socket_error_string();
Ghislain MARY's avatar
Ghislain MARY committed
99
const char *belle_sip_get_socket_error_string_from_code(int code);
Simon Morlat's avatar
Simon Morlat committed
100

101
#ifdef WINAPI_FAMILY_PHONE_APP
102
BELLESIP_INTERNAL_EXPORT void belle_sip_sleep(unsigned int ms);
Ghislain MARY's avatar
Ghislain MARY committed
103 104 105 106
#else
#define belle_sip_sleep Sleep
#endif

107
#define usleep(us) belle_sip_sleep((us)/1000)
108
static BELLESIP_INLINE int inet_aton(const char *ip, struct in_addr *p){
Simon Morlat's avatar
Simon Morlat committed
109 110 111 112
	*(long*)p=inet_addr(ip);
	return 0;
}

Ghislain MARY's avatar
Ghislain MARY committed
113 114
#define BELLESIP_EWOULDBLOCK WSAEWOULDBLOCK
#define BELLESIP_EINPROGRESS WSAEINPROGRESS
Simon Morlat's avatar
Simon Morlat committed
115 116 117

#else

118 119 120 121 122 123 124 125 126
typedef pthread_t belle_sip_thread_t;
#define belle_sip_thread_self()			pthread_self()

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)

127
static BELLESIP_INLINE void close_socket(belle_sip_socket_t s){
Simon Morlat's avatar
Simon Morlat committed
128 129 130
	close(s);
}

131
static BELLESIP_INLINE int get_socket_error(void){
Simon Morlat's avatar
Simon Morlat committed
132 133 134 135 136
	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
137 138 139
#define BELLESIP_EWOULDBLOCK EWOULDBLOCK
#define BELLESIP_EINPROGRESS EINPROGRESS

Simon Morlat's avatar
Simon Morlat committed
140 141
#endif

142
#define belle_sip_error_code_is_would_block(err) ((err)==BELLESIP_EWOULDBLOCK || (err)==BELLESIP_EINPROGRESS)
Simon Morlat's avatar
Simon Morlat committed
143 144 145

#endif