port.h 2.69 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

#else

#include <winsock2.h>
35 36 37 38 39 40 41 42
#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
43 44
#else
#include <stdint.h>
45
#endif
Simon Morlat's avatar
Simon Morlat committed
46 47 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

#endif

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

Simon Morlat's avatar
Simon Morlat committed
64 65 66 67
/*
 * Socket abstraction layer
 */

68 69
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
70 71
int belle_sip_socket_set_nonblocking (belle_sip_socket_t sock);
 
Simon Morlat's avatar
Simon Morlat committed
72 73 74 75 76 77 78 79 80 81
#if defined(WIN32)

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
82
const char *belle_sip_get_socket_error_string();
Ghislain MARY's avatar
Ghislain MARY committed
83
const char *belle_sip_get_socket_error_string_from_code(int code);
Simon Morlat's avatar
Simon Morlat committed
84

Simon Morlat's avatar
Simon Morlat committed
85 86 87 88 89 90
#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;
}

91
#ifndef EWOULDBLOCK
Simon Morlat's avatar
Simon Morlat committed
92
#define EWOULDBLOCK WSAEWOULDBLOCK
93 94 95
#endif

#ifndef EINPROGRESS
Simon Morlat's avatar
Simon Morlat committed
96
#define EINPROGRESS WSAEINPROGRESS
97
#endif
Simon Morlat's avatar
Simon Morlat committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

#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



#endif