vconnect.c 5.42 KB
Newer Older
1
/*
2
vconnect.c
3
Copyright (C) 2017 Belledonne Communications SARL
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

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 2
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, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
19 20


21 22 23 24
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

25 26 27 28 29 30 31 32 33

#include "bctoolbox/vconnect.h"
#include "bctoolbox/port.h"
#include "bctoolbox/logging.h"
#include <sys/types.h>
#include <stdarg.h>
#include <errno.h>


34

35

36 37 38 39
/**============================================================
 * Wrappers around default libc calls used to handle windows
 * differences in function prototypes. 
 *=============================================================*/
40 41 42 43 44 45 46 47 48

static bctbx_socket_t vsocket_socket(int socket_family, int socket_type, int protocol){
	return socket(socket_family, socket_type, protocol);
}



static int vsocket_connect(bctbx_socket_t sock, const struct sockaddr *address, socklen_t address_len){
	#ifdef _WIN32
Sandrine Avakian's avatar
Sandrine Avakian committed
49
		return connect(sock, address, (int)address_len);
50
	#else 
Sandrine Avakian's avatar
Sandrine Avakian committed
51
		return connect(sock, address, address_len);
52 53 54
	#endif
}

55

56
static int vsocket_bind(bctbx_socket_t sock, const struct sockaddr *address, socklen_t address_len){
57 58 59 60 61 62
	#ifdef _WIN32
		return bind(sock, address, (int)address_len);
	#else 
		return bind(sock, address, address_len);
	#endif
}
63

64

65 66 67 68 69 70 71 72 73 74
static int vsocket_getsockname(bctbx_socket_t sockfd, struct sockaddr *addr, socklen_t *addrlen){
	#ifdef _WIN32
		return getsockname(sockfd, addr, (int*)addrlen);
	#else 
		return getsockname(sockfd, addr, addrlen);
	#endif
}

static int vsocket_getsockopt(bctbx_socket_t sockfd, int level, int optname, 
						void *optval, socklen_t* optlen){
75
	#ifdef _WIN32
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
		return getsockopt(sockfd, level, optname, (char*)optval,  (int*)optlen);
	#else 
		return getsockopt(sockfd, level, optname, optval,  optlen);
	#endif

}


static int vsocket_setsockopt(bctbx_socket_t sockfd, int level, int optname, 
							const void *optval, socklen_t optlen){
	#ifdef _WIN32
		return setsockopt(sockfd, level, optname, (char*)optval,  (int)optlen);
	#else 
		return setsockopt(sockfd, level, optname, optval,  optlen);
	#endif

}

static int vsocket_close(bctbx_socket_t sock){
	#ifdef _WIN32
	return closesocket(sock);
97
	#else
98
	return close(sock);
99
	#endif
100 101 102 103 104 105 106 107 108 109 110
}

static int vsocket_shutdown(bctbx_socket_t sock, int how){
	return shutdown(sock, how);
}

	
#if	!defined(_WIN32) && !defined(_WIN32_WCE) 	 
static char* vsocket_error(int err){  
	 return strerror (err);
}
111

112
#else
113

114 115
static  char* vsocket_error(int err){  
	 return (char*)__bctbx_getWinSocketError(err);
116 117 118
 }
#endif

Sandrine Avakian's avatar
Sandrine Avakian committed
119 120 121 122
/**===================================================
 * Socket API default methods pointer definition.
 * ===================================================*/
 
123
static const bctbx_vsocket_methods_t bcSocketAPI = {
124 125 126 127 128 129 130 131 132
	vsocket_socket,
	vsocket_connect,  	
	vsocket_bind,
	vsocket_getsockname,
	vsocket_getsockopt,
	vsocket_setsockopt,
	vsocket_close,
	vsocket_error,
	vsocket_shutdown,
133 134 135
};


136
static bctbx_vsocket_api_t bcvSocket = {
Sandrine Avakian's avatar
Sandrine Avakian committed
137
	"bctbx_socket",          /* vSockName */
138 139 140 141 142 143
	&bcSocketAPI,			/*pSocketMethods */
};



/* Pointer to default socket methods initialized to standard libc implementation here.*/
144
static  bctbx_vsocket_api_t *pDefaultvSocket = &bcvSocket;
145 146


147
bctbx_socket_t bctbx_socket(int socket_family, int socket_type, int protocol){
148
	return pDefaultvSocket->pSocketMethods->pFuncSocket( socket_family, socket_type, protocol);
149 150 151
}


152
int bctbx_socket_close(bctbx_socket_t sock){
153
	return pDefaultvSocket->pSocketMethods->pFuncClose(sock);
154 155 156
}


157
int bctbx_bind(bctbx_socket_t sock, const struct sockaddr *address, socklen_t address_len){
158 159
	return pDefaultvSocket->pSocketMethods->pFuncBind(sock, address, address_len);

160 161 162
}


163
int bctbx_connect(bctbx_socket_t sock, const struct sockaddr *address, socklen_t address_len){
164
	return pDefaultvSocket->pSocketMethods->pFuncConnect(sock, address, address_len);
165 166
}

167
int bctbx_getsockname(bctbx_socket_t sockfd, struct sockaddr *addr, socklen_t *addrlen){
168
	return pDefaultvSocket->pSocketMethods->pFuncGetSockName(sockfd, addr, addrlen);
169 170 171
}


172
int bctbx_getsockopt(bctbx_socket_t sockfd, int level, int optname, 
173
						void *optval, socklen_t* optlen){
174 175
	return pDefaultvSocket->pSocketMethods->pFuncGetSockOpt(sockfd, level, optname, optval, optlen);

176 177
}

178
int bctbx_setsockopt(bctbx_socket_t sockfd, int level, int optname, 
179
							const void *optval, socklen_t optlen){
180 181
	return pDefaultvSocket->pSocketMethods->pFuncSetSockOpt(sockfd, level, optname, optval, optlen);

182 183 184
}


185
int bctbx_shutdown(bctbx_socket_t sock, int how){
186
	return pDefaultvSocket->pSocketMethods->pFuncShutdown(sock, how);
187 188
}

189

190 191
char* bctbx_socket_error(int err){
	return pDefaultvSocket->pSocketMethods->pFuncGetError(err);
192
}
193

194 195
void bctbx_vsocket_api_set_default(bctbx_vsocket_api_t *my_vsocket_api) {

196 197 198 199
	if (my_vsocket_api == NULL){
		pDefaultvSocket = &bcvSocket;
	}
	else {
200 201 202
		pDefaultvSocket = my_vsocket_api ;
	}

203 204
}

205
bctbx_vsocket_api_t* bctbx_vsocket_api_get_default(void) {
206 207 208
	return pDefaultvSocket;	
}

209
bctbx_vsocket_api_t* bctbx_vsocket_api_get_standard(void) {
210 211 212
	return &bcvSocket;
}

213

214