Tor  0.4.3.0-alpha-dev
socket.c
Go to the documentation of this file.
1 /* Copyright (c) 2003-2004, Roger Dingledine
2  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3  * Copyright (c) 2007-2019, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
5 
6 /**
7  * \file socket.c
8  * \brief Compatibility and utility functions for working with network
9  * sockets.
10  **/
11 
12 #define SOCKET_PRIVATE
13 #include "lib/net/socket.h"
14 #include "lib/net/socketpair.h"
15 #include "lib/net/address.h"
16 #include "lib/cc/compat_compiler.h"
17 #include "lib/err/torerr.h"
18 #include "lib/lock/compat_mutex.h"
19 #include "lib/log/log.h"
20 #include "lib/log/util_bug.h"
21 
22 #ifdef _WIN32
23 #include <winsock2.h>
24 #include <windows.h>
25 #endif
26 #ifdef HAVE_UNISTD_H
27 #include <unistd.h>
28 #endif
29 #ifdef HAVE_FCNTL_H
30 #include <fcntl.h>
31 #endif
32 #include <stddef.h>
33 #include <string.h>
34 #ifdef __FreeBSD__
35 #include <sys/sysctl.h>
36 #endif
37 
38 /** Called before we make any calls to network-related functions.
39  * (Some operating systems require their network libraries to be
40  * initialized.) */
41 int
43 {
44 #ifdef _WIN32
45  /* This silly exercise is necessary before windows will allow
46  * gethostbyname to work. */
47  WSADATA WSAData;
48  int r;
49  r = WSAStartup(0x101,&WSAData);
50  if (r) {
51  log_warn(LD_NET,"Error initializing windows network layer: code was %d",r);
52  return -1;
53  }
54  if (sizeof(SOCKET) != sizeof(tor_socket_t)) {
55  log_warn(LD_BUG,"The tor_socket_t type does not match SOCKET in size; Tor "
56  "might not work. (Sizes are %d and %d respectively.)",
57  (int)sizeof(tor_socket_t), (int)sizeof(SOCKET));
58  }
59  /* WSAData.iMaxSockets might show the max sockets we're allowed to use.
60  * We might use it to complain if we're trying to be a server but have
61  * too few sockets available. */
62 #endif /* defined(_WIN32) */
63  return 0;
64 }
65 
66 /**
67  * Warn the user if any system network parameters should be changed.
68  */
69 void
71 {
72 #ifdef __FreeBSD__
73  if (server_mode) {
74  int random_id_state;
75  size_t state_size = sizeof(random_id_state);
76 
77  if (sysctlbyname("net.inet.ip.random_id", &random_id_state,
78  &state_size, NULL, 0)) {
79  log_warn(LD_CONFIG,
80  "Failed to figure out if IP ids are randomized.");
81  } else if (random_id_state == 0) {
82  log_warn(LD_CONFIG, "Looks like IP ids are not randomized. "
83  "Please consider setting the net.inet.ip.random_id sysctl, "
84  "so your relay makes it harder to figure out how busy it is.");
85  }
86  }
87 #else /* !defined(__FreeBSD__) */
88  (void) server_mode;
89 #endif /* defined(__FreeBSD__) */
90 }
91 
92 /* When set_max_file_sockets() is called, update this with the max file
93  * descriptor value so we can use it to check the limit when opening a new
94  * socket. Default value is what Debian sets as the default hard limit. */
95 static int max_sockets = 1024;
96 
97 /** Return the maximum number of allowed sockets. */
98 int
100 {
101  return max_sockets;
102 }
103 
104 /** Set the maximum number of allowed sockets to <b>n</b> */
105 void
107 {
108  max_sockets = n;
109 }
110 
111 #undef DEBUG_SOCKET_COUNTING
112 #ifdef DEBUG_SOCKET_COUNTING
113 #include "lib/container/bitarray.h"
114 
115 /** A bitarray of all fds that should be passed to tor_socket_close(). Only
116  * used if DEBUG_SOCKET_COUNTING is defined. */
117 static bitarray_t *open_sockets = NULL;
118 /** The size of <b>open_sockets</b>, in bits. */
119 static int max_socket = -1;
120 #endif /* defined(DEBUG_SOCKET_COUNTING) */
121 
122 /** Count of number of sockets currently open. (Undercounts sockets opened by
123  * eventdns and libevent.) */
124 static int n_sockets_open = 0;
125 
126 /** Mutex to protect open_sockets, max_socket, and n_sockets_open. */
128 
129 /** Helper: acquire the socket accounting lock. */
130 static inline void
132 {
133  if (PREDICT_UNLIKELY(!socket_accounting_mutex))
136 }
137 
138 /** Helper: release the socket accounting lock. */
139 static inline void
141 {
143 }
144 
145 /** As close(), but guaranteed to work for sockets across platforms (including
146  * Windows, where close()ing a socket doesn't work. Returns 0 on success and
147  * the socket error code on failure. */
148 int
150 {
151  int r = 0;
152 
153  /* On Windows, you have to call close() on fds returned by open(),
154  * and closesocket() on fds returned by socket(). On Unix, everything
155  * gets close()'d. We abstract this difference by always using
156  * tor_close_socket to close sockets, and always using close() on
157  * files.
158  */
159  #if defined(_WIN32)
160  r = closesocket(s);
161  #else
162  r = close(s);
163  #endif
164 
165  if (r != 0) {
166  int err = tor_socket_errno(-1);
167  log_info(LD_NET, "Close returned an error: %s", tor_socket_strerror(err));
168  return err;
169  }
170 
171  return r;
172 }
173 
174 /** @{ */
175 #ifdef DEBUG_SOCKET_COUNTING
176 /** Helper: if DEBUG_SOCKET_COUNTING is enabled, remember that <b>s</b> is
177  * now an open socket. */
178 static inline void
179 mark_socket_open(tor_socket_t s)
180 {
181  /* XXXX This bitarray business will NOT work on windows: sockets aren't
182  small ints there. */
183  if (s > max_socket) {
184  if (max_socket == -1) {
185  open_sockets = bitarray_init_zero(s+128);
186  max_socket = s+128;
187  } else {
188  open_sockets = bitarray_expand(open_sockets, max_socket, s+128);
189  max_socket = s+128;
190  }
191  }
192  if (bitarray_is_set(open_sockets, s)) {
193  log_warn(LD_BUG, "I thought that %d was already open, but socket() just "
194  "gave it to me!", s);
195  }
196  bitarray_set(open_sockets, s);
197 }
198 static inline void
199 mark_socket_closed(tor_socket_t s)
200 {
201  if (s > max_socket || ! bitarray_is_set(open_sockets, s)) {
202  log_warn(LD_BUG, "Closing a socket (%d) that wasn't returned by tor_open_"
203  "socket(), or that was already closed or something.", s);
204  } else {
205  tor_assert(open_sockets && s <= max_socket);
206  bitarray_clear(open_sockets, s);
207  }
208 }
209 #else /* !defined(DEBUG_SOCKET_COUNTING) */
210 #define mark_socket_open(s) ((void) (s))
211 #define mark_socket_closed(s) ((void) (s))
212 #endif /* defined(DEBUG_SOCKET_COUNTING) */
213 /** @} */
214 
215 /** As tor_close_socket_simple(), but keeps track of the number
216  * of open sockets. Returns 0 on success, -1 on failure. */
217 MOCK_IMPL(int,
219 {
220  int r = tor_close_socket_simple(s);
221 
223  mark_socket_closed(s);
224  if (r == 0) {
225  --n_sockets_open;
226  } else {
227 #ifdef _WIN32
228  if (r != WSAENOTSOCK)
229  --n_sockets_open;
230 #else
231  if (r != EBADF)
232  --n_sockets_open; // LCOV_EXCL_LINE -- EIO and EINTR too hard to force.
233 #endif /* defined(_WIN32) */
234  r = -1;
235  }
236 
237  tor_assert_nonfatal(n_sockets_open >= 0);
239  return r;
240 }
241 
242 /** As socket(), but counts the number of open sockets. */
244 tor_open_socket,(int domain, int type, int protocol))
245 {
246  return tor_open_socket_with_extensions(domain, type, protocol, 1, 0);
247 }
248 
249 /** Mockable wrapper for connect(). */
251 tor_connect_socket,(tor_socket_t sock, const struct sockaddr *address,
252  socklen_t address_len))
253 {
254  return connect(sock,address,address_len);
255 }
256 
257 /** As socket(), but creates a nonblocking socket and
258  * counts the number of open sockets. */
260 tor_open_socket_nonblocking(int domain, int type, int protocol)
261 {
262  return tor_open_socket_with_extensions(domain, type, protocol, 1, 1);
263 }
264 
265 /** As socket(), but counts the number of open sockets and handles
266  * socket creation with either of SOCK_CLOEXEC and SOCK_NONBLOCK specified.
267  * <b>cloexec</b> and <b>nonblock</b> should be either 0 or 1 to indicate
268  * if the corresponding extension should be used.*/
270 tor_open_socket_with_extensions(int domain, int type, int protocol,
271  int cloexec, int nonblock)
272 {
273  tor_socket_t s;
274 
275  /* We are about to create a new file descriptor so make sure we have
276  * enough of them. */
277  if (get_n_open_sockets() >= max_sockets - 1) {
278 #ifdef _WIN32
279  WSASetLastError(WSAEMFILE);
280 #else
281  errno = EMFILE;
282 #endif
283  return TOR_INVALID_SOCKET;
284  }
285 
286 #if defined(SOCK_CLOEXEC) && defined(SOCK_NONBLOCK)
287  int ext_flags = (cloexec ? SOCK_CLOEXEC : 0) |
288  (nonblock ? SOCK_NONBLOCK : 0);
289  s = socket(domain, type|ext_flags, protocol);
290  if (SOCKET_OK(s))
291  goto socket_ok;
292  /* If we got an error, see if it is EINVAL. EINVAL might indicate that,
293  * even though we were built on a system with SOCK_CLOEXEC and SOCK_NONBLOCK
294  * support, we are running on one without. */
295  if (errno != EINVAL)
296  return s;
297 #endif /* defined(SOCK_CLOEXEC) && defined(SOCK_NONBLOCK) */
298 
299  s = socket(domain, type, protocol);
300  if (! SOCKET_OK(s))
301  return s;
302 
303 #if defined(FD_CLOEXEC)
304  if (cloexec) {
305  if (fcntl(s, F_SETFD, FD_CLOEXEC) == -1) {
306  log_warn(LD_FS,"Couldn't set FD_CLOEXEC: %s", strerror(errno));
308  return TOR_INVALID_SOCKET;
309  }
310  }
311 #else /* !defined(FD_CLOEXEC) */
312  (void)cloexec;
313 #endif /* defined(FD_CLOEXEC) */
314 
315  if (nonblock) {
316  if (set_socket_nonblocking(s) == -1) {
318  return TOR_INVALID_SOCKET;
319  }
320  }
321 
322  goto socket_ok; /* So that socket_ok will not be unused. */
323 
324  socket_ok:
326  return s;
327 }
328 
329 /**
330  * For socket accounting: remember that we are the owner of the socket
331  * <b>s</b>. This will prevent us from overallocating sockets, and prevent us
332  * from asserting later when we close the socket <b>s</b>.
333  */
334 void
336 {
338  ++n_sockets_open;
339  mark_socket_open(s);
341 }
342 
343 /**
344  * For socket accounting: declare that we are no longer the owner of the
345  * socket <b>s</b>. This will prevent us from overallocating sockets, and
346  * prevent us from asserting later when we close the socket <b>s</b>.
347  */
348 void
350 {
352  --n_sockets_open;
353  mark_socket_closed(s);
355 }
356 
357 /** As accept(), but counts the number of open sockets. */
359 tor_accept_socket(tor_socket_t sockfd, struct sockaddr *addr, socklen_t *len)
360 {
361  return tor_accept_socket_with_extensions(sockfd, addr, len, 1, 0);
362 }
363 
364 /** As accept(), but returns a nonblocking socket and
365  * counts the number of open sockets. */
367 tor_accept_socket_nonblocking(tor_socket_t sockfd, struct sockaddr *addr,
368  socklen_t *len)
369 {
370  return tor_accept_socket_with_extensions(sockfd, addr, len, 1, 1);
371 }
372 
373 /** As accept(), but counts the number of open sockets and handles
374  * socket creation with either of SOCK_CLOEXEC and SOCK_NONBLOCK specified.
375  * <b>cloexec</b> and <b>nonblock</b> should be either 0 or 1 to indicate
376  * if the corresponding extension should be used.*/
378 tor_accept_socket_with_extensions(tor_socket_t sockfd, struct sockaddr *addr,
379  socklen_t *len, int cloexec, int nonblock)
380 {
381  tor_socket_t s;
382 
383  /* We are about to create a new file descriptor so make sure we have
384  * enough of them. */
385  if (get_n_open_sockets() >= max_sockets - 1) {
386 #ifdef _WIN32
387  WSASetLastError(WSAEMFILE);
388 #else
389  errno = EMFILE;
390 #endif
391  return TOR_INVALID_SOCKET;
392  }
393 
394 #if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC) \
395  && defined(SOCK_NONBLOCK)
396  int ext_flags = (cloexec ? SOCK_CLOEXEC : 0) |
397  (nonblock ? SOCK_NONBLOCK : 0);
398  s = accept4(sockfd, addr, len, ext_flags);
399  if (SOCKET_OK(s))
400  goto socket_ok;
401  /* If we got an error, see if it is ENOSYS. ENOSYS indicates that,
402  * even though we were built on a system with accept4 support, we
403  * are running on one without. Also, check for EINVAL, which indicates that
404  * we are missing SOCK_CLOEXEC/SOCK_NONBLOCK support. */
405  if (errno != EINVAL && errno != ENOSYS)
406  return s;
407 #endif /* defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC) ... */
408 
409  s = accept(sockfd, addr, len);
410  if (!SOCKET_OK(s))
411  return s;
412 
413 #if defined(FD_CLOEXEC)
414  if (cloexec) {
415  if (fcntl(s, F_SETFD, FD_CLOEXEC) == -1) {
416  log_warn(LD_NET, "Couldn't set FD_CLOEXEC: %s", strerror(errno));
418  return TOR_INVALID_SOCKET;
419  }
420  }
421 #else /* !defined(FD_CLOEXEC) */
422  (void)cloexec;
423 #endif /* defined(FD_CLOEXEC) */
424 
425  if (nonblock) {
426  if (set_socket_nonblocking(s) == -1) {
428  return TOR_INVALID_SOCKET;
429  }
430  }
431 
432  goto socket_ok; /* So that socket_ok will not be unused. */
433 
434  socket_ok:
436  return s;
437 }
438 
439 /** Return the number of sockets we currently have opened. */
440 int
442 {
443  int n;
445  n = n_sockets_open;
447  return n;
448 }
449 
450 /**
451  * Allocate a pair of connected sockets. (Like socketpair(family,
452  * type,protocol,fd), but works on systems that don't have
453  * socketpair.)
454  *
455  * Currently, only (AF_UNIX, SOCK_STREAM, 0) sockets are supported.
456  *
457  * Note that on systems without socketpair, this call will fail if
458  * localhost is inaccessible (for example, if the networking
459  * stack is down). And even if it succeeds, the socket pair will not
460  * be able to read while localhost is down later (the socket pair may
461  * even close, depending on OS-specific timeouts). The socket pair
462  * should work on IPv4-only, IPv6-only, and dual-stack systems, as long
463  * as they have the standard localhost addresses.
464  *
465  * Returns 0 on success and -errno on failure; do not rely on the value
466  * of errno or WSAGetLastError().
467  **/
468 /* It would be nicer just to set errno, but that won't work for windows. */
469 int
470 tor_socketpair(int family, int type, int protocol, tor_socket_t fd[2])
471 {
472  int r;
473 //don't use win32 socketpairs (they are always bad)
474 #if defined(HAVE_SOCKETPAIR) && !defined(_WIN32)
475 
476 #ifdef SOCK_CLOEXEC
477  r = socketpair(family, type|SOCK_CLOEXEC, protocol, fd);
478  if (r == 0)
479  goto sockets_ok;
480  /* If we got an error, see if it is EINVAL. EINVAL might indicate that,
481  * even though we were built on a system with SOCK_CLOEXEC support, we
482  * are running on one without. */
483  if (errno != EINVAL)
484  return -errno;
485 #endif /* defined(SOCK_CLOEXEC) */
486 
487  r = socketpair(family, type, protocol, fd);
488  if (r < 0)
489  return -errno;
490 #else /* !(defined(HAVE_SOCKETPAIR) && !defined(_WIN32)) */
491  r = tor_ersatz_socketpair(family, type, protocol, fd);
492  if (r < 0)
493  return -r;
494 #endif /* defined(HAVE_SOCKETPAIR) && !defined(_WIN32) */
495 
496 #if defined(FD_CLOEXEC)
497  if (SOCKET_OK(fd[0])) {
498  r = fcntl(fd[0], F_SETFD, FD_CLOEXEC);
499  if (r == -1) {
500  close(fd[0]);
501  close(fd[1]);
502  return -errno;
503  }
504  }
505  if (SOCKET_OK(fd[1])) {
506  r = fcntl(fd[1], F_SETFD, FD_CLOEXEC);
507  if (r == -1) {
508  close(fd[0]);
509  close(fd[1]);
510  return -errno;
511  }
512  }
513 #endif /* defined(FD_CLOEXEC) */
514  goto sockets_ok; /* So that sockets_ok will not be unused. */
515 
516  sockets_ok:
518  if (SOCKET_OK(fd[0])) {
519  ++n_sockets_open;
520  mark_socket_open(fd[0]);
521  }
522  if (SOCKET_OK(fd[1])) {
523  ++n_sockets_open;
524  mark_socket_open(fd[1]);
525  }
527 
528  return 0;
529 }
530 
531 /** Mockable wrapper for getsockname(). */
532 MOCK_IMPL(int,
533 tor_getsockname,(tor_socket_t sock, struct sockaddr *address,
534  socklen_t *address_len))
535 {
536  return getsockname(sock, address, address_len);
537 }
538 
539 /**
540  * Find the local address associated with the socket <b>sock</b>, and
541  * place it in *<b>addr_out</b>. Return 0 on success, -1 on failure.
542  *
543  * (As tor_getsockname, but instead places the result in a tor_addr_t.) */
544 int
546 {
547  struct sockaddr_storage ss;
548  socklen_t ss_len = sizeof(ss);
549  memset(&ss, 0, sizeof(ss));
550 
551  if (tor_getsockname(sock, (struct sockaddr *) &ss, &ss_len) < 0)
552  return -1;
553 
554  return tor_addr_from_sockaddr(addr_out, (struct sockaddr *)&ss, NULL);
555 }
556 
557 /** Turn <b>socket</b> into a nonblocking socket. Return 0 on success, -1
558  * on failure.
559  */
560 int
562 {
563 #if defined(_WIN32)
564  unsigned long nonblocking = 1;
565  ioctlsocket(sock, FIONBIO, (unsigned long*) &nonblocking);
566 #else
567  int flags;
568 
569  flags = fcntl(sock, F_GETFL, 0);
570  if (flags == -1) {
571  log_warn(LD_NET, "Couldn't get file status flags: %s", strerror(errno));
572  return -1;
573  }
574  flags |= O_NONBLOCK;
575  if (fcntl(sock, F_SETFL, flags) == -1) {
576  log_warn(LD_NET, "Couldn't set file status flags: %s", strerror(errno));
577  return -1;
578  }
579 #endif /* defined(_WIN32) */
580 
581  return 0;
582 }
583 
584 /** Read from <b>sock</b> to <b>buf</b>, until we get <b>count</b> bytes or
585  * reach the end of the file. Return the number of bytes read, or -1 on
586  * error. Only use if fd is a blocking fd. */
587 ssize_t
588 read_all_from_socket(tor_socket_t sock, char *buf, size_t count)
589 {
590  size_t numread = 0;
591  ssize_t result;
592 
593  if (count > SIZE_T_CEILING || count > SSIZE_MAX) {
594  errno = EINVAL;
595  return -1;
596  }
597 
598  while (numread < count) {
599  result = tor_socket_recv(sock, buf+numread, count-numread, 0);
600  if (result<0)
601  return -1;
602  else if (result == 0)
603  break;
604  numread += result;
605  }
606  return (ssize_t)numread;
607 }
608 
609 /** Write <b>count</b> bytes from <b>buf</b> to <b>sock</b>. Return the number
610  * of bytes written, or -1 on error. Only use if fd is a blocking fd. */
611 ssize_t
612 write_all_to_socket(tor_socket_t fd, const char *buf, size_t count)
613 {
614  size_t written = 0;
615  ssize_t result;
616  raw_assert(count < SSIZE_MAX);
617 
618  while (written != count) {
619  result = tor_socket_send(fd, buf+written, count-written, 0);
620  if (result<0)
621  return -1;
622  written += result;
623  }
624  return (ssize_t)count;
625 }
626 
627 /**
628  * On Windows, WSAEWOULDBLOCK is not always correct: when you see it,
629  * you need to ask the socket for its actual errno. Also, you need to
630  * get your errors from WSAGetLastError, not errno. (If you supply a
631  * socket of -1, we check WSAGetLastError, but don't correct
632  * WSAEWOULDBLOCKs.)
633  *
634  * The upshot of all of this is that when a socket call fails, you
635  * should call tor_socket_errno <em>at most once</em> on the failing
636  * socket to get the error.
637  */
638 #if defined(_WIN32)
639 int
640 tor_socket_errno(tor_socket_t sock)
641 {
642  int optval, optvallen=sizeof(optval);
643  int err = WSAGetLastError();
644  if (err == WSAEWOULDBLOCK && SOCKET_OK(sock)) {
645  if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (void*)&optval, &optvallen))
646  return err;
647  if (optval)
648  return optval;
649  }
650  return err;
651 }
652 #endif /* defined(_WIN32) */
653 
654 #if defined(_WIN32)
655 #define E(code, s) { code, (s " [" #code " ]") }
656 struct { int code; const char *msg; } windows_socket_errors[] = {
657  E(WSAEINTR, "Interrupted function call"),
658  E(WSAEACCES, "Permission denied"),
659  E(WSAEFAULT, "Bad address"),
660  E(WSAEINVAL, "Invalid argument"),
661  E(WSAEMFILE, "Too many open files"),
662  E(WSAEWOULDBLOCK, "Resource temporarily unavailable"),
663  E(WSAEINPROGRESS, "Operation now in progress"),
664  E(WSAEALREADY, "Operation already in progress"),
665  E(WSAENOTSOCK, "Socket operation on nonsocket"),
666  E(WSAEDESTADDRREQ, "Destination address required"),
667  E(WSAEMSGSIZE, "Message too long"),
668  E(WSAEPROTOTYPE, "Protocol wrong for socket"),
669  E(WSAENOPROTOOPT, "Bad protocol option"),
670  E(WSAEPROTONOSUPPORT, "Protocol not supported"),
671  E(WSAESOCKTNOSUPPORT, "Socket type not supported"),
672  /* What's the difference between NOTSUPP and NOSUPPORT? :) */
673  E(WSAEOPNOTSUPP, "Operation not supported"),
674  E(WSAEPFNOSUPPORT, "Protocol family not supported"),
675  E(WSAEAFNOSUPPORT, "Address family not supported by protocol family"),
676  E(WSAEADDRINUSE, "Address already in use"),
677  E(WSAEADDRNOTAVAIL, "Cannot assign requested address"),
678  E(WSAENETDOWN, "Network is down"),
679  E(WSAENETUNREACH, "Network is unreachable"),
680  E(WSAENETRESET, "Network dropped connection on reset"),
681  E(WSAECONNABORTED, "Software caused connection abort"),
682  E(WSAECONNRESET, "Connection reset by peer"),
683  E(WSAENOBUFS, "No buffer space available"),
684  E(WSAEISCONN, "Socket is already connected"),
685  E(WSAENOTCONN, "Socket is not connected"),
686  E(WSAESHUTDOWN, "Cannot send after socket shutdown"),
687  E(WSAETIMEDOUT, "Connection timed out"),
688  E(WSAECONNREFUSED, "Connection refused"),
689  E(WSAEHOSTDOWN, "Host is down"),
690  E(WSAEHOSTUNREACH, "No route to host"),
691  E(WSAEPROCLIM, "Too many processes"),
692  /* Yes, some of these start with WSA, not WSAE. No, I don't know why. */
693  E(WSASYSNOTREADY, "Network subsystem is unavailable"),
694  E(WSAVERNOTSUPPORTED, "Winsock.dll out of range"),
695  E(WSANOTINITIALISED, "Successful WSAStartup not yet performed"),
696  E(WSAEDISCON, "Graceful shutdown now in progress"),
697 #ifdef WSATYPE_NOT_FOUND
698  E(WSATYPE_NOT_FOUND, "Class type not found"),
699 #endif
700  E(WSAHOST_NOT_FOUND, "Host not found"),
701  E(WSATRY_AGAIN, "Nonauthoritative host not found"),
702  E(WSANO_RECOVERY, "This is a nonrecoverable error"),
703  E(WSANO_DATA, "Valid name, no data record of requested type)"),
704 
705  /* There are some more error codes whose numeric values are marked
706  * <b>OS dependent</b>. They start with WSA_, apparently for the same
707  * reason that practitioners of some craft traditions deliberately
708  * introduce imperfections into their baskets and rugs "to allow the
709  * evil spirits to escape." If we catch them, then our binaries
710  * might not report consistent results across versions of Windows.
711  * Thus, I'm going to let them all fall through.
712  */
713  { -1, NULL },
714 };
715 /** There does not seem to be a strerror equivalent for Winsock errors.
716  * Naturally, we have to roll our own.
717  */
718 const char *
719 tor_socket_strerror(int e)
720 {
721  int i;
722  for (i=0; windows_socket_errors[i].code >= 0; ++i) {
723  if (e == windows_socket_errors[i].code)
724  return windows_socket_errors[i].msg;
725  }
726  return strerror(e);
727 }
728 #endif /* defined(_WIN32) */
int set_socket_nonblocking(tor_socket_t sock)
Definition: socket.c:561
tor_socket_t tor_connect_socket(tor_socket_t sock, const struct sockaddr *address, socklen_t address_len)
Definition: socket.c:252
void tor_mutex_release(tor_mutex_t *m)
unsigned int bitarray_t
Definition: bitarray.h:30
#define MOCK_IMPL(rv, funcname, arglist)
Definition: testsupport.h:133
int tor_close_socket(tor_socket_t s)
Definition: socket.c:218
ssize_t read_all_from_socket(tor_socket_t sock, char *buf, size_t count)
Definition: socket.c:588
static tor_mutex_t * socket_accounting_mutex
Definition: socket.c:127
Implements a variable-sized (but non-resizeable) bit-array.
int get_max_sockets(void)
Definition: socket.c:99
void tor_take_socket_ownership(tor_socket_t s)
Definition: socket.c:335
ssize_t write_all_to_socket(tor_socket_t fd, const char *buf, size_t count)
Definition: socket.c:612
#define tor_assert(expr)
Definition: util_bug.h:102
tor_socket_t tor_open_socket(int domain, int type, int protocol)
Definition: socket.c:244
int tor_addr_from_getsockname(struct tor_addr_t *addr_out, tor_socket_t sock)
Definition: socket.c:545
int tor_socketpair(int family, int type, int protocol, tor_socket_t fd[2])
Definition: socket.c:470
static void bitarray_set(bitarray_t *b, int bit)
Definition: bitarray.h:68
int network_init(void)
Definition: socket.c:42
Header for socketpair.c.
#define SOCKET_OK(s)
Definition: nettypes.h:39
static bitarray_t * bitarray_expand(bitarray_t *ba, unsigned int n_bits_old, unsigned int n_bits_new)
Definition: bitarray.h:43
tor_socket_t tor_accept_socket_with_extensions(tor_socket_t sockfd, struct sockaddr *addr, socklen_t *len, int cloexec, int nonblock)
Definition: socket.c:378
void set_max_sockets(int n)
Definition: socket.c:106
static int n_sockets_open
Definition: socket.c:124
int tor_getsockname(tor_socket_t sock, struct sockaddr *address, socklen_t *address_len)
Definition: socket.c:534
#define SIZE_T_CEILING
Definition: torint.h:126
int get_n_open_sockets(void)
Definition: socket.c:441
static void socket_accounting_unlock(void)
Definition: socket.c:140
Utility macros to handle different features and behavior in different compilers.
Headers for address.h.
static void bitarray_clear(bitarray_t *b, int bit)
Definition: bitarray.h:74
static void socket_accounting_lock(void)
Definition: socket.c:131
static bitarray_t * bitarray_init_zero(unsigned int n_bits)
Definition: bitarray.h:33
#define tor_socket_t
Definition: nettypes.h:36
void check_network_configuration(bool server_mode)
Definition: socket.c:70
#define TOR_INVALID_SOCKET
Definition: nettypes.h:41
#define LD_FS
Definition: log.h:70
tor_socket_t tor_open_socket_nonblocking(int domain, int type, int protocol)
Definition: socket.c:260
int tor_close_socket_simple(tor_socket_t s)
Definition: socket.c:149
tor_socket_t tor_accept_socket(tor_socket_t sockfd, struct sockaddr *addr, socklen_t *len)
Definition: socket.c:359
tor_socket_t tor_accept_socket_nonblocking(tor_socket_t sockfd, struct sockaddr *addr, socklen_t *len)
Definition: socket.c:367
Header for compat_mutex.c.
void tor_mutex_acquire(tor_mutex_t *m)
tor_mutex_t * tor_mutex_new(void)
Definition: compat_mutex.c:17
Headers for torerr.c.
void tor_release_socket_ownership(tor_socket_t s)
Definition: socket.c:349
#define LD_NET
Definition: log.h:66
Headers for log.c.
Header for socket.c.
int server_mode(const or_options_t *options)
Definition: routermode.c:34
Macros to manage assertions, fatal and non-fatal.
static unsigned int bitarray_is_set(bitarray_t *b, int bit)
Definition: bitarray.h:81
int tor_addr_from_sockaddr(tor_addr_t *a, const struct sockaddr *sa, uint16_t *port_out)
Definition: address.c:165
tor_socket_t tor_open_socket_with_extensions(int domain, int type, int protocol, int cloexec, int nonblock)
Definition: socket.c:270
#define LD_BUG
Definition: log.h:86
#define LD_CONFIG
Definition: log.h:68