port.h 2.96 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>
Simon Morlat's avatar
Simon Morlat committed
31 32 33 34 35
#include <pthread.h>

#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
Simon Morlat's avatar
Simon Morlat committed
44 45
#else
#include <stdint.h>
46
#endif
Simon Morlat's avatar
Simon Morlat committed
47 48 49 50 51 52 53 54 55 56 57 58

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

/*
 * Socket abstraction layer
 */

59 60
int belle_sip_init_sockets(void);
void belle_sip_uninit_sockets(void);
Simon Morlat's avatar
Simon Morlat committed
61 62
int belle_sip_socket_set_nonblocking (belle_sip_socket_t sock);
 
Simon Morlat's avatar
Simon Morlat committed
63 64
#if defined(WIN32)

Simon Morlat's avatar
Simon Morlat committed
65 66 67
int belle_sip_init_sockets(void);
void belle_sip_uninit_sockets(void);

Simon Morlat's avatar
Simon Morlat committed
68 69 70 71 72 73 74 75
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
76
const char *belle_sip_get_socket_error_string();
Ghislain MARY's avatar
Ghislain MARY committed
77
const char *belle_sip_get_socket_error_string_from_code(int code);
Simon Morlat's avatar
Simon Morlat committed
78

Simon Morlat's avatar
Simon Morlat committed
79 80 81 82 83 84
#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;
}

85
#ifndef EWOULDBLOCK
Simon Morlat's avatar
Simon Morlat committed
86
#define EWOULDBLOCK WSAEWOULDBLOCK
87 88 89
#endif

#ifndef EINPROGRESS
Simon Morlat's avatar
Simon Morlat committed
90
#define EINPROGRESS WSAEINPROGRESS
91
#endif
Simon Morlat's avatar
Simon Morlat committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131

#else

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)

#endif

/*
 * Thread abstraction layer
 */

#ifdef WIN32

typedef HANDLE belle_sip_thread_t;
int belle_sip_thread_join(belle_sip_thread_t thread, void **retptr);
int belle_sip_thread_create(belle_sip_thread_t *thread, void *attr, void * (*routine)(void*), void *arg);

#else

#include <pthread.h>

typedef pthread_t belle_sip_thread_t;
#define belle_sip_thread_join(thread,retptr) pthread_join(thread,retptr)
#define belle_sip_thread_create(thread,attr,routine,arg) pthread_create(thread,attr,routine,arg)

#endif




#endif