subrascasse

Sub Rascasse: an iPhone dive log for Subsurface cloud storage, with a pure-Swift git client (GPL-2.0)
git clone https://git.schurger.org/subrascasse.git
Log | Files | Refs | README | LICENSE

socket.c (8418B)


      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 #include "socket.h"
     23 #include "platform.h"
     24 
     25 #include "common-private.h"
     26 #include "context-private.h"
     27 
     28 #ifndef MSG_NOSIGNAL
     29 #define MSG_NOSIGNAL 0
     30 #endif
     31 
     32 dc_status_t
     33 dc_socket_syserror (s_errcode_t errcode)
     34 {
     35 	switch (errcode) {
     36 	case S_EINVAL:
     37 		return DC_STATUS_INVALIDARGS;
     38 	case S_ENOMEM:
     39 		return DC_STATUS_NOMEMORY;
     40 	case S_EACCES:
     41 		return DC_STATUS_NOACCESS;
     42 	case S_EAFNOSUPPORT:
     43 		return DC_STATUS_UNSUPPORTED;
     44 	default:
     45 		return DC_STATUS_IO;
     46 	}
     47 }
     48 
     49 dc_status_t
     50 dc_socket_init (dc_context_t *context)
     51 {
     52 #ifdef _WIN32
     53 	// Initialize the winsock dll.
     54 	WSADATA wsaData;
     55 	WORD wVersionRequested = MAKEWORD (2, 2);
     56 	int rc = WSAStartup (wVersionRequested, &wsaData);
     57 	if (rc != 0) {
     58 		SYSERROR (context, rc);
     59 		return DC_STATUS_UNSUPPORTED;
     60 	}
     61 
     62 	// Confirm that the winsock dll supports version 2.2.
     63 	// Note that if the dll supports versions greater than 2.2 in addition to
     64 	// 2.2, it will still return 2.2 since that is the version we requested.
     65 	if (LOBYTE (wsaData.wVersion) != 2 ||
     66 		HIBYTE (wsaData.wVersion) != 2) {
     67 		ERROR (context, "Incorrect winsock version.");
     68 		return DC_STATUS_UNSUPPORTED;
     69 	}
     70 #endif
     71 
     72 	return DC_STATUS_SUCCESS;
     73 }
     74 
     75 dc_status_t
     76 dc_socket_exit (dc_context_t *context)
     77 {
     78 #ifdef _WIN32
     79 	// Terminate the winsock dll.
     80 	if (WSACleanup () != 0) {
     81 		s_errcode_t errcode = S_ERRNO;
     82 		SYSERROR (context, errcode);
     83 		return dc_socket_syserror(errcode);
     84 	}
     85 #endif
     86 
     87 	return DC_STATUS_SUCCESS;
     88 }
     89 
     90 dc_status_t
     91 dc_socket_open (dc_iostream_t *abstract, int family, int type, int protocol)
     92 {
     93 	dc_status_t status = DC_STATUS_SUCCESS;
     94 	dc_socket_t *device = (dc_socket_t *) abstract;
     95 
     96 	// Default to blocking reads.
     97 	device->timeout = -1;
     98 
     99 	// Initialize the socket library.
    100 	status = dc_socket_init (abstract->context);
    101 	if (status != DC_STATUS_SUCCESS) {
    102 		return status;
    103 	}
    104 
    105 	// Open the socket.
    106 	device->fd = socket (family, type, protocol);
    107 	if (device->fd == S_INVALID) {
    108 		s_errcode_t errcode = S_ERRNO;
    109 		SYSERROR (abstract->context, errcode);
    110 		status = dc_socket_syserror(errcode);
    111 		goto error;
    112 	}
    113 
    114 #ifdef SO_NOSIGPIPE
    115 	int optval = 1;
    116 	if (setsockopt(device->fd, SOL_SOCKET, SO_NOSIGPIPE, &optval, sizeof(optval)) != 0) {
    117 		s_errcode_t errcode = S_ERRNO;
    118 		SYSERROR (abstract->context, errcode);
    119 		status = dc_socket_syserror(errcode);
    120 		goto error_close;
    121 	}
    122 #endif
    123 
    124 	return DC_STATUS_SUCCESS;
    125 
    126 #ifdef SO_NOSIGPIPE
    127 error_close:
    128 	S_CLOSE (device->fd);
    129 #endif
    130 error:
    131 	dc_socket_exit (abstract->context);
    132 	return status;
    133 }
    134 
    135 dc_status_t
    136 dc_socket_close (dc_iostream_t *abstract)
    137 {
    138 	dc_status_t status = DC_STATUS_SUCCESS;
    139 	dc_socket_t *socket = (dc_socket_t *) abstract;
    140 	dc_status_t rc = DC_STATUS_SUCCESS;
    141 
    142 	// Terminate all send and receive operations.
    143 	shutdown (socket->fd, 0);
    144 
    145 	// Close the socket.
    146 	if (S_CLOSE (socket->fd) != 0) {
    147 		s_errcode_t errcode = S_ERRNO;
    148 		SYSERROR (abstract->context, errcode);
    149 		dc_status_set_error(&status, dc_socket_syserror(errcode));
    150 	}
    151 
    152 	// Terminate the socket library.
    153 	rc = dc_socket_exit (abstract->context);
    154 	if (rc != DC_STATUS_SUCCESS) {
    155 		dc_status_set_error(&status, rc);
    156 	}
    157 
    158 	return status;
    159 }
    160 
    161 dc_status_t
    162 dc_socket_connect (dc_iostream_t *abstract, const struct sockaddr *addr, s_socklen_t addrlen)
    163 {
    164 	dc_socket_t *socket = (dc_socket_t *) abstract;
    165 
    166 	if (connect (socket->fd, addr, addrlen) != 0) {
    167 		s_errcode_t errcode = S_ERRNO;
    168 		SYSERROR (abstract->context, errcode);
    169 		return dc_socket_syserror(errcode);
    170 	}
    171 
    172 	return DC_STATUS_SUCCESS;
    173 }
    174 
    175 dc_status_t
    176 dc_socket_set_timeout (dc_iostream_t *abstract, int timeout)
    177 {
    178 	dc_socket_t *socket = (dc_socket_t *) abstract;
    179 
    180 	socket->timeout = timeout;
    181 
    182 	return DC_STATUS_SUCCESS;
    183 }
    184 
    185 dc_status_t
    186 dc_socket_get_available (dc_iostream_t *abstract, size_t *value)
    187 {
    188 	dc_socket_t *socket = (dc_socket_t *) abstract;
    189 
    190 #ifdef _WIN32
    191 	unsigned long bytes = 0;
    192 #else
    193 	int bytes = 0;
    194 #endif
    195 
    196 	if (S_IOCTL (socket->fd, FIONREAD, &bytes) != 0) {
    197 		s_errcode_t errcode = S_ERRNO;
    198 		SYSERROR (abstract->context, errcode);
    199 		return dc_socket_syserror(errcode);
    200 	}
    201 
    202 	if (value)
    203 		*value = bytes;
    204 
    205 	return DC_STATUS_SUCCESS;
    206 }
    207 
    208 dc_status_t
    209 dc_socket_poll (dc_iostream_t *abstract, int timeout)
    210 {
    211 	dc_socket_t *socket = (dc_socket_t *) abstract;
    212 	int rc = 0;
    213 
    214 	do {
    215 		fd_set fds;
    216 		FD_ZERO (&fds);
    217 		FD_SET (socket->fd, &fds);
    218 
    219 		struct timeval tv, *ptv = NULL;
    220 		if (timeout > 0) {
    221 			tv.tv_sec  = (timeout / 1000);
    222 			tv.tv_usec = (timeout % 1000) * 1000;
    223 			ptv = &tv;
    224 		} else if (timeout == 0) {
    225 			tv.tv_sec  = 0;
    226 			tv.tv_usec = 0;
    227 			ptv = &tv;
    228 		}
    229 
    230 		rc = select (socket->fd + 1, &fds, NULL, NULL, ptv);
    231 	} while (rc < 0 && S_ERRNO == S_EINTR);
    232 
    233 	if (rc < 0) {
    234 		s_errcode_t errcode = S_ERRNO;
    235 		SYSERROR (abstract->context, errcode);
    236 		return dc_socket_syserror(errcode);
    237 	} else if (rc == 0) {
    238 		return DC_STATUS_TIMEOUT;
    239 	} else {
    240 		return DC_STATUS_SUCCESS;
    241 	}
    242 }
    243 
    244 dc_status_t
    245 dc_socket_read (dc_iostream_t *abstract, void *data, size_t size, size_t *actual)
    246 {
    247 	dc_status_t status = DC_STATUS_SUCCESS;
    248 	dc_socket_t *socket = (dc_socket_t *) abstract;
    249 	size_t nbytes = 0;
    250 
    251 	while (nbytes < size) {
    252 		fd_set fds;
    253 		FD_ZERO (&fds);
    254 		FD_SET (socket->fd, &fds);
    255 
    256 		struct timeval tvt;
    257 		if (socket->timeout > 0) {
    258 			tvt.tv_sec  = (socket->timeout / 1000);
    259 			tvt.tv_usec = (socket->timeout % 1000) * 1000;
    260 		} else if (socket->timeout == 0) {
    261 			timerclear (&tvt);
    262 		}
    263 
    264 		int rc = select (socket->fd + 1, &fds, NULL, NULL, socket->timeout >= 0 ? &tvt : NULL);
    265 		if (rc < 0) {
    266 			s_errcode_t errcode = S_ERRNO;
    267 			if (errcode == S_EINTR)
    268 				continue; // Retry.
    269 			SYSERROR (abstract->context, errcode);
    270 			status = dc_socket_syserror(errcode);
    271 			goto out;
    272 		} else if (rc == 0) {
    273 			break; // Timeout.
    274 		}
    275 
    276 		s_ssize_t n = recv (socket->fd, (char *) data + nbytes, size - nbytes, 0);
    277 		if (n < 0) {
    278 			s_errcode_t errcode = S_ERRNO;
    279 			if (errcode == S_EINTR || errcode == S_EAGAIN)
    280 				continue; // Retry.
    281 			SYSERROR (abstract->context, errcode);
    282 			status = dc_socket_syserror(errcode);
    283 			goto out;
    284 		} else if (n == 0) {
    285 			break; // EOF reached.
    286 		}
    287 
    288 		nbytes += n;
    289 	}
    290 
    291 	if (nbytes != size) {
    292 		status = DC_STATUS_TIMEOUT;
    293 	}
    294 
    295 out:
    296 	if (actual)
    297 		*actual = nbytes;
    298 
    299 	return status;
    300 }
    301 
    302 dc_status_t
    303 dc_socket_write (dc_iostream_t *abstract, const void *data, size_t size, size_t *actual)
    304 {
    305 	dc_status_t status = DC_STATUS_SUCCESS;
    306 	dc_socket_t *socket = (dc_socket_t *) abstract;
    307 	size_t nbytes = 0;
    308 
    309 	while (nbytes < size) {
    310 		fd_set fds;
    311 		FD_ZERO (&fds);
    312 		FD_SET (socket->fd, &fds);
    313 
    314 		int rc = select (socket->fd + 1, NULL, &fds, NULL, NULL);
    315 		if (rc < 0) {
    316 			s_errcode_t errcode = S_ERRNO;
    317 			if (errcode == S_EINTR)
    318 				continue; // Retry.
    319 			SYSERROR (abstract->context, errcode);
    320 			status = dc_socket_syserror(errcode);
    321 			goto out;
    322 		} else if (rc == 0) {
    323 			break; // Timeout.
    324 		}
    325 
    326 		s_ssize_t n = send (socket->fd, (const char *) data + nbytes, size - nbytes, MSG_NOSIGNAL);
    327 		if (n < 0) {
    328 			s_errcode_t errcode = S_ERRNO;
    329 			if (errcode == S_EINTR || errcode == S_EAGAIN)
    330 				continue; // Retry.
    331 			SYSERROR (abstract->context, errcode);
    332 			status = dc_socket_syserror(errcode);
    333 			goto out;
    334 		} else if (n == 0) {
    335 			break; // EOF.
    336 		}
    337 
    338 		nbytes += n;
    339 	}
    340 
    341 	if (nbytes != size) {
    342 		status = DC_STATUS_TIMEOUT;
    343 	}
    344 
    345 out:
    346 	if (actual)
    347 		*actual = nbytes;
    348 
    349 	return status;
    350 }
    351 
    352 dc_status_t
    353 dc_socket_ioctl (dc_iostream_t *abstract, unsigned int request, void *data, size_t size)
    354 {
    355 	return DC_STATUS_UNSUPPORTED;
    356 }
    357 
    358 dc_status_t
    359 dc_socket_sleep (dc_iostream_t *abstract, unsigned int timeout)
    360 {
    361 	if (dc_platform_sleep (timeout) != 0) {
    362 		s_errcode_t errcode = S_ERRNO;
    363 		SYSERROR (abstract->context, errcode);
    364 		return dc_socket_syserror(errcode);
    365 	}
    366 
    367 	return DC_STATUS_SUCCESS;
    368 }