socket.h (3423B)
1 /* 2 * libdivecomputer 3 * 4 * Copyright (C) 2017 Jef Driesen 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation; either 9 * version 2.1 of the License, or (at your option) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with this library; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19 * MA 02110-1301 USA 20 */ 21 22 #ifndef DC_SOCKET_H 23 #define DC_SOCKET_H 24 25 #ifdef _WIN32 26 #define WIN32_LEAN_AND_MEAN 27 #define NOGDI 28 #include <winsock2.h> 29 #include <windows.h> 30 #else 31 #include <errno.h> // errno 32 #include <unistd.h> // close 33 #include <sys/types.h> // socket, getsockopt 34 #include <sys/socket.h> // socket, getsockopt 35 #include <sys/select.h> // select 36 #include <sys/ioctl.h> // ioctl 37 #include <sys/time.h> 38 #include <time.h> 39 #endif 40 41 #include <libdivecomputer/common.h> 42 #include <libdivecomputer/context.h> 43 44 #include "iostream-private.h" 45 46 #ifdef _WIN32 47 typedef SOCKET s_socket_t; 48 typedef int s_ssize_t; 49 typedef DWORD s_errcode_t; 50 typedef int s_socklen_t; 51 #define S_ERRNO WSAGetLastError () 52 #define S_EINTR WSAEINTR 53 #define S_EAGAIN WSAEWOULDBLOCK 54 #define S_ENOMEM WSA_NOT_ENOUGH_MEMORY 55 #define S_EINVAL WSAEINVAL 56 #define S_EACCES WSAEACCES 57 #define S_EAFNOSUPPORT WSAEAFNOSUPPORT 58 #define S_INVALID INVALID_SOCKET 59 #define S_IOCTL ioctlsocket 60 #define S_CLOSE closesocket 61 #else 62 typedef int s_socket_t; 63 typedef ssize_t s_ssize_t; 64 typedef int s_errcode_t; 65 typedef socklen_t s_socklen_t; 66 #define S_ERRNO errno 67 #define S_EINTR EINTR 68 #define S_EAGAIN EAGAIN 69 #define S_ENOMEM ENOMEM 70 #define S_EINVAL EINVAL 71 #define S_EACCES EACCES 72 #define S_EAFNOSUPPORT EAFNOSUPPORT 73 #define S_INVALID -1 74 #define S_IOCTL ioctl 75 #define S_CLOSE close 76 #endif 77 78 #ifdef __cplusplus 79 extern "C" { 80 #endif /* __cplusplus */ 81 82 typedef struct dc_socket_t { 83 dc_iostream_t base; 84 s_socket_t fd; 85 int timeout; 86 } dc_socket_t; 87 88 dc_status_t 89 dc_socket_syserror (s_errcode_t errcode); 90 91 dc_status_t 92 dc_socket_init (dc_context_t *context); 93 94 dc_status_t 95 dc_socket_exit (dc_context_t *context); 96 97 dc_status_t 98 dc_socket_open (dc_iostream_t *iostream, int family, int type, int protocol); 99 100 dc_status_t 101 dc_socket_close (dc_iostream_t *iostream); 102 103 dc_status_t 104 dc_socket_connect (dc_iostream_t *iostream, const struct sockaddr *addr, s_socklen_t addrlen); 105 106 dc_status_t 107 dc_socket_set_timeout (dc_iostream_t *iostream, int timeout); 108 109 dc_status_t 110 dc_socket_get_available (dc_iostream_t *iostream, size_t *value); 111 112 dc_status_t 113 dc_socket_poll (dc_iostream_t *iostream, int timeout); 114 115 dc_status_t 116 dc_socket_read (dc_iostream_t *iostream, void *data, size_t size, size_t *actual); 117 118 dc_status_t 119 dc_socket_write (dc_iostream_t *iostream, const void *data, size_t size, size_t *actual); 120 121 dc_status_t 122 dc_socket_ioctl (dc_iostream_t *iostream, unsigned int request, void *data, size_t size); 123 124 dc_status_t 125 dc_socket_sleep (dc_iostream_t *abstract, unsigned int timeout); 126 127 dc_status_t 128 dc_socket_close (dc_iostream_t *iostream); 129 130 #ifdef __cplusplus 131 } 132 #endif /* __cplusplus */ 133 #endif /* DC_SOCKET_H */