1 /* 2 vconnect.h 3 Copyright (C) 2017 Belledonne Communications SARL 4 5 This program is free software; you can redistribute it and/or 6 modify it under the terms of the GNU General Public License 7 as published by the Free Software Foundation; either version 2 8 of the License, or (at your option) any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this program; if not, write to the Free Software 17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 18 */ 19 20 #ifndef BCTBX_VCONNECT 21 #define BCTBX_VCONNECT 22 23 #include <fcntl.h> 24 25 #include <bctoolbox/port.h> 26 27 28 #if !defined(_WIN32_WCE) 29 #include <sys/types.h> 30 #include <sys/stat.h> 31 #if _MSC_VER 32 #include <io.h> 33 #endif 34 #endif /*_WIN32_WCE*/ 35 36 37 #ifndef _WIN32 38 #include <unistd.h> 39 #endif 40 41 42 #define BCTBX_VCONNECT_OK 0 /* Successful result */ 43 44 #define BCTBX_VCONNECT_ERROR -255 /* Some kind of socket error occurred */ 45 46 47 #ifdef __cplusplus 48 extern "C"{ 49 #endif 50 51 52 /** 53 * Methods associated with the bctbx_vsocket_api_t. 54 */ 55 typedef struct bctbx_vsocket_methods_t bctbx_vsocket_methods_t; 56 57 /** 58 * Methods pointer prototypes for the socket functions. 59 */ 60 struct bctbx_vsocket_methods_t { 61 62 bctbx_socket_t (*pFuncSocket)(int socket_family, int socket_type, int protocol); 63 int (*pFuncConnect)(bctbx_socket_t sock, const struct sockaddr *address, socklen_t address_len); 64 int (*pFuncBind)(bctbx_socket_t sock, const struct sockaddr *address, socklen_t address_len); 65 66 int (*pFuncGetSockName)(bctbx_socket_t sockfd, struct sockaddr *addr, socklen_t *addrlen); 67 int (*pFuncGetSockOpt)(bctbx_socket_t sockfd, int level, int optname, 68 void *optval, socklen_t *optlen); 69 int (*pFuncSetSockOpt)(bctbx_socket_t sockfd, int level, int optname, 70 const void *optval, socklen_t optlen); 71 int (*pFuncClose)(bctbx_socket_t sock); 72 char* (*pFuncGetError)(int err); 73 int (*pFuncShutdown)(bctbx_socket_t sock, int how); 74 75 76 77 }; 78 79 80 /** 81 * Socket API structure definition 82 */ 83 typedef struct bctbx_vsocket_api_t bctbx_vsocket_api_t; 84 struct bctbx_vsocket_api_t { 85 const char *vSockName; /* Socket API name */ 86 const bctbx_vsocket_methods_t *pSocketMethods; /* Pointer to the socket methods structure */ 87 }; 88 89 90 91 /**=================================================== 92 * Socket API methods prototypes. 93 * The default implementation relies on libc methods. 94 *====================================================*/ 95 96 97 /** 98 * Creates a socket. 99 * @param socket_family type of address structure 100 * @param socket_type socket type i.e SOCK_STREAM 101 * @param protocol protocol family i.e AF_INET 102 * @return Returns a socket file descriptor. 103 */ 104 BCTBX_PUBLIC bctbx_socket_t bctbx_socket(int socket_family, int socket_type, int protocol); 105 106 /** 107 * Get the local socket address. 108 * Calls the function pointer pFuncGetSockName. The default method associated with this pointer 109 * is getsockname. 110 * @param sockfd socket descriptor 111 * @param addr buffer holding the current address to which the socket sockfd is bound 112 * @param addrlen amount of space (in bytes) pointed to by addr 113 * @return On success, zero is returned. On error, -1 is returned, and errno is 114 * set appropriately. 115 */ 116 BCTBX_PUBLIC int bctbx_getsockname(bctbx_socket_t sockfd, struct sockaddr *addr, socklen_t *addrlen); 117 /** 118 * Get socket options. 119 * @param sockfd socket file descriptor 120 * @param level level of the socket option 121 * @param optname name of the option 122 * @param optval buffer in which the value for the requested option(s) are to be returned 123 * @param optlen contains the size of the buffer pointed to by optval, and on return 124 * contains the actual size of the value returned. 125 * @return On success, zero is returned. On error, -1 is returned, and errno is 126 * set appropriately. 127 */ 128 BCTBX_PUBLIC int bctbx_getsockopt(bctbx_socket_t sockfd, int level, int optname, 129 void *optval, socklen_t *optlen); 130 /** 131 * Set socket options. 132 * @param sockfd socket file descriptor 133 * @param level level of the socket option 134 * @param optname name of the option 135 * @param optval buffer holding the value for the requested option 136 * @param optlen contains the size of the buffer pointed to by optval 137 * @return On success, zero is returned. On error, -1 is returned, and errno is 138 * set appropriately. 139 */ 140 BCTBX_PUBLIC int bctbx_setsockopt(bctbx_socket_t sockfd, int level, int optname, 141 const void *optval, socklen_t optlen); 142 143 /** 144 * Shut down part of a full duplex connection. 145 * @param sockfd socket file descriptor 146 * @param how specifies what is shut down in the connection. 147 * @return On success, zero is returned. On error, -1 is returned, and errno is 148 * set appropriately. 149 */ 150 BCTBX_PUBLIC int bctbx_shutdown(bctbx_socket_t sockfd, int how); 151 152 /** 153 * Close a socket file descriptor. 154 * @param sockfd socket file descriptor 155 * @return 0 on success , -1 on error and errno is set. 156 */ 157 BCTBX_PUBLIC int bctbx_socket_close(bctbx_socket_t sockfd); 158 159 /** 160 * Assign a name to a socket. 161 * @param sockfd socket file descriptor to assign the name to. 162 * @param address address of the socket 163 * @param address_len size of the address structure pointed to by address (bytes) 164 * @return On success, zero is returned. On error, -1 is returned, and errno is 165 * set appropriately. 166 */ 167 BCTBX_PUBLIC int bctbx_bind(bctbx_socket_t sockfd, const struct sockaddr *address, socklen_t address_len); 168 169 /** 170 * Initialize a connection to a socket. 171 * @param sockfd socket file descriptor 172 * @param address address of the socket 173 * @param address_len size of the address structure pointed to by address (bytes) 174 * @return On success, zero is returned. On error, -1 is returned, and errno is 175 * set appropriately. 176 */ 177 BCTBX_PUBLIC int bctbx_connect(bctbx_socket_t sockfd, const struct sockaddr *address, socklen_t address_len); 178 179 /** 180 * strerror equivalent. 181 * When an error is returned on a socket operations, returns 182 * the error description based on err (errno) value. 183 * @param err should be set to errno 184 * @return Error description 185 */ 186 BCTBX_PUBLIC char* bctbx_socket_error(int err); 187 188 189 /** 190 * Set default bctbx_vsocket_api_t pointer pDefaultvSocket to my_vsocket_api 191 * if it is not NULL, sets it to the standard API implementation otherwise. 192 * By default, the global pointer is set to use bcvSocket implemented in vconnect.c 193 * @param my_vsocket_api Pointer to a bctbx_vsocket_api_t structure. 194 */ 195 BCTBX_PUBLIC void bctbx_vsocket_api_set_default(bctbx_vsocket_api_t *my_vsocket_api); 196 197 198 /** 199 * Returns the value of the global variable pDefaultvSocket, 200 * pointing to the default bctbx_vsocket_api_t used. 201 * @return Pointer to bctbx_vsocket_api_t set to operate as default. 202 */ 203 BCTBX_PUBLIC bctbx_vsocket_api_t* bctbx_vsocket_api_get_default(void); 204 205 /** 206 * Return pointer to standard bctbx_vsocket_api_t implementation based on libc 207 * functions. 208 * @return pointer to bcSocketAPI 209 */ 210 BCTBX_PUBLIC bctbx_vsocket_api_t* bctbx_vsocket_api_get_standard(void); 211 212 213 #ifdef __cplusplus 214 } 215 #endif 216 217 218 #endif /* BCTBX_VCONNECT */ 219 220