port.h 3.84 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 22 23 24 25 26 27 28 29
/*
	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

#ifndef WIN32
#include <stdint.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
Simon Morlat's avatar
Simon Morlat committed
30
#include <netinet/tcp.h>
31
#include <pthread.h>
Simon Morlat's avatar
Simon Morlat committed
32 33 34 35

#else

#include <winsock2.h>
36 37 38 39 40 41 42 43
#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
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 54 55

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

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

Simon Morlat's avatar
Simon Morlat committed
66 67 68 69
/*
 * Socket abstraction layer
 */

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

77 78 79 80 81 82 83 84 85 86 87
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);


Simon Morlat's avatar
Simon Morlat committed
88 89 90 91 92 93 94 95
static inline void close_socket(belle_sip_socket_t s){
	closesocket(s);
}

static inline int get_socket_error(void){
	return WSAGetLastError();
}

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

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

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

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

#else

116 117 118 119 120 121 122 123 124
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)

Simon Morlat's avatar
Simon Morlat committed
125 126 127 128 129 130 131 132 133 134
static inline void close_socket(belle_sip_socket_t s){
	close(s);
}

static inline int get_socket_error(void){
	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
135 136 137
#define BELLESIP_EWOULDBLOCK EWOULDBLOCK
#define BELLESIP_EINPROGRESS EINPROGRESS

Simon Morlat's avatar
Simon Morlat committed
138 139 140 141 142 143
#endif



#endif