GNUnet  0.11.x
network.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2009-2013 GNUnet e.V.
4 
5  GNUnet is free software: you can redistribute it and/or modify it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  GNUnet is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Affero General Public License for more details.
14 
15  You should have received a copy of the GNU Affero General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "disk.h"
30 
31 #define LOG(kind, ...) GNUNET_log_from (kind, "util-network", __VA_ARGS__)
32 #define LOG_STRERROR_FILE(kind, syscall, \
33  filename) GNUNET_log_from_strerror_file (kind, \
34  "util-network", \
35  syscall, \
36  filename)
37 #define LOG_STRERROR(kind, syscall) GNUNET_log_from_strerror (kind, \
38  "util-network", \
39  syscall)
40 
41 #define DEBUG_NETWORK GNUNET_EXTRA_LOGGING
42 
43 
44 #ifndef INVALID_SOCKET
45 #define INVALID_SOCKET -1
46 #endif
47 
48 
53 {
54  int fd;
55 
59  int af;
60 
64  int type;
65 
69  socklen_t addrlen;
70 
74  struct sockaddr *addr;
75 };
76 
77 
84 int
86 {
87  static int cache_v4 = -1;
88  static int cache_v6 = -1;
89  static int cache_un = -1;
90  int s;
91  int ret;
92 
93  switch (pf)
94  {
95  case PF_INET:
96  if (-1 != cache_v4)
97  return cache_v4;
98  break;
99 
100  case PF_INET6:
101  if (-1 != cache_v6)
102  return cache_v6;
103  break;
104 
105 #ifdef PF_UNIX
106  case PF_UNIX:
107  if (-1 != cache_un)
108  return cache_un;
109  break;
110 #endif
111  }
112  s = socket (pf, SOCK_STREAM, 0);
113  if (-1 == s)
114  {
115  if (EAFNOSUPPORT != errno)
116  {
118  "socket");
119  return GNUNET_SYSERR;
120  }
121  ret = GNUNET_NO;
122  }
123  else
124  {
125  close (s);
126  ret = GNUNET_OK;
127  }
128  switch (pf)
129  {
130  case PF_INET:
131  cache_v4 = ret;
132  break;
133 
134  case PF_INET6:
135  cache_v6 = ret;
136  break;
137 
138 #ifdef PF_UNIX
139  case PF_UNIX:
140  cache_un = ret;
141  break;
142 #endif
143  }
144  return ret;
145 }
146 
147 
157 char *
159 {
160  struct sockaddr_un dummy;
161  size_t slen;
162  char *end;
163  struct GNUNET_HashCode sh;
165  size_t upm;
166 
167  upm = sizeof(dummy.sun_path);
168  slen = strlen (unixpath);
169  if (slen < upm)
170  return unixpath; /* no shortening required */
171  GNUNET_CRYPTO_hash (unixpath, slen, &sh);
172  while (16 + strlen (unixpath) >= upm)
173  {
174  if (NULL == (end = strrchr (unixpath, '/')))
175  {
177  _ (
178  "Unable to shorten unix path `%s' while keeping name unique\n"),
179  unixpath);
180  GNUNET_free (unixpath);
181  return NULL;
182  }
183  *end = '\0';
184  }
185  GNUNET_CRYPTO_hash_to_enc (&sh, &ae);
186  ae.encoding[16] = '\0';
187  strcat (unixpath, (char *) ae.encoding);
188  return unixpath;
189 }
190 
191 
201 void
202 GNUNET_NETWORK_unix_precheck (const struct sockaddr_un *un)
203 {
204  int s;
205  int eno;
206  struct stat sbuf;
207  int ret;
208 
209  s = socket (AF_UNIX, SOCK_STREAM, 0);
210  if (-1 == s)
211  {
213  "Failed to open AF_UNIX socket");
214  return;
215  }
216  ret = connect (s,
217  (struct sockaddr *) un,
218  sizeof(struct sockaddr_un));
219  eno = errno;
220  GNUNET_break (0 == close (s));
221  if (0 == ret)
222  return; /* another process is listening, do not remove! */
223  if (ECONNREFUSED != eno)
224  return; /* some other error, likely "no such file or directory" -- all well */
225  /* should unlink, but sanity checks first */
226  if (0 != stat (un->sun_path,
227  &sbuf))
228  return; /* failed to 'stat', likely does not exist after all */
229  if (S_IFSOCK != (S_IFMT & sbuf.st_mode))
230  return; /* refuse to unlink anything except sockets */
231  /* finally, really unlink */
233  "Removing left-over `%s' from previous exeuction\n",
234  un->sun_path);
235  if (0 != unlink (un->sun_path))
237  "unlink",
238  un->sun_path);
239 }
240 
241 
242 #ifndef FD_COPY
243 #define FD_COPY(s, d) do { GNUNET_memcpy ((d), (s), sizeof(fd_set)); } while (0)
244 #endif
245 
246 
254 int
256  int doBlock)
257 {
258  int flags = fcntl (fd->fd, F_GETFL);
259 
260  if (flags == -1)
261  {
263  "fcntl");
264  return GNUNET_SYSERR;
265  }
266  if (doBlock)
267  flags &= ~O_NONBLOCK;
268 
269  else
270  flags |= O_NONBLOCK;
271  if (0 != fcntl (fd->fd,
272  F_SETFL,
273  flags))
274 
275  {
277  "fcntl");
278  return GNUNET_SYSERR;
279  }
280  return GNUNET_OK;
281 }
282 
283 
291 static int
293 {
294  int i;
295  i = fcntl (h->fd, F_GETFD);
296  if (i < 0)
297  return GNUNET_SYSERR;
298  if (i == (i | FD_CLOEXEC))
299  return GNUNET_OK;
300  i |= FD_CLOEXEC;
301  if (fcntl (h->fd, F_SETFD, i) < 0)
302  return GNUNET_SYSERR;
303 
304  return GNUNET_OK;
305 }
306 
307 
308 #ifdef DARWIN
309 
314 static int
315 socket_set_nosigpipe (const struct GNUNET_NETWORK_Handle *h)
316 {
317  int abs_value = 1;
318 
319  if (0 !=
320  setsockopt (h->fd, SOL_SOCKET, SO_NOSIGPIPE,
321  (const void *) &abs_value,
322  sizeof(abs_value)))
323  return GNUNET_SYSERR;
324  return GNUNET_OK;
325 }
326 
327 
328 #endif
329 
330 
338 static void
340 {
341  int value = 1;
342 
343  if (0 !=
344  setsockopt (h->fd,
345  IPPROTO_TCP,
346  TCP_NODELAY,
347  &value, sizeof(value)))
349  "setsockopt");
350 }
351 
352 
365 static int
367  int af,
368  int type)
369 {
370  int eno;
371 
372  h->af = af;
373  h->type = type;
374  if (h->fd == INVALID_SOCKET)
375  {
376  eno = errno;
377  GNUNET_free (h);
378  errno = eno;
379  return GNUNET_SYSERR;
380  }
381 
382  if (h->fd >= FD_SETSIZE)
383  {
385  errno = EMFILE;
386  return GNUNET_SYSERR;
387  }
388 
391  "socket_set_inheritable");
392 
394  {
395  eno = errno;
396  GNUNET_break (0);
398  errno = eno;
399  return GNUNET_SYSERR;
400  }
401 #ifdef DARWIN
402  if (GNUNET_SYSERR == socket_set_nosigpipe (h))
403  {
404  eno = errno;
405  GNUNET_break (0);
407  errno = eno;
408  return GNUNET_SYSERR;
409  }
410 #endif
411  if ((type == SOCK_STREAM)
412 #ifdef AF_UNIX
413  && (af != AF_UNIX)
414 #endif
415  )
416  socket_set_nodelay (h);
417  return GNUNET_OK;
418 }
419 
420 
429 struct GNUNET_NETWORK_Handle *
431  struct sockaddr *address,
432  socklen_t *address_len)
433 {
434  struct GNUNET_NETWORK_Handle *ret;
435  int eno;
436 
437  ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
438 #if DEBUG_NETWORK
439  {
440  struct sockaddr_storage name;
441  socklen_t namelen = sizeof(name);
442 
443  int gsn = getsockname (desc->fd,
444  (struct sockaddr *) &name,
445  &namelen);
446 
447  if (0 == gsn)
449  "Accepting connection on `%s'\n",
450  GNUNET_a2s ((const struct sockaddr *) &name,
451  namelen));
452  }
453 #endif
454  ret->fd = accept (desc->fd,
455  address,
456  address_len);
457  if (-1 == ret->fd)
458  {
459  eno = errno;
460  GNUNET_free (ret);
461  errno = eno;
462  return NULL;
463  }
464  if (GNUNET_OK !=
466  (NULL != address) ? address->sa_family :
467  desc->af,
468  SOCK_STREAM))
469  {
470  return NULL;
471  }
472  return ret;
473 }
474 
475 
484 int
486  const struct sockaddr *address,
487  socklen_t address_len)
488 {
489  int ret;
490 
491 #ifdef IPV6_V6ONLY
492 #ifdef IPPROTO_IPV6
493  {
494  const int on = 1;
495 
496  if (AF_INET6 == desc->af)
497  if (setsockopt (desc->fd,
498  IPPROTO_IPV6,
499  IPV6_V6ONLY,
500  (const void *) &on,
501  sizeof(on)))
503  "setsockopt");
504  }
505 #endif
506 #endif
507  if (AF_UNIX == address->sa_family)
508  GNUNET_NETWORK_unix_precheck ((const struct sockaddr_un *) address);
509  {
510  const int on = 1;
511 
512  /* This is required here for TCP sockets, but only on UNIX */
513  if ((SOCK_STREAM == desc->type) &&
514  (0 != setsockopt (desc->fd,
515  SOL_SOCKET,
516  SO_REUSEADDR,
517  &on, sizeof(on))))
519  "setsockopt");
520  }
521  {
522  /* set permissions of newly created non-abstract UNIX domain socket to
523  "user-only"; applications can choose to relax this later */
524  mode_t old_mask = 0; /* assigned to make compiler happy */
525  const struct sockaddr_un *un = (const struct sockaddr_un *) address;
526  int not_abstract = 0;
527 
528  if ((AF_UNIX == address->sa_family)
529  && ('\0' != un->sun_path[0])) /* Not an abstract socket */
530  not_abstract = 1;
531  if (not_abstract)
532  old_mask = umask (S_IWGRP | S_IRGRP | S_IXGRP | S_IWOTH | S_IROTH
533  | S_IXOTH);
534 
535  ret = bind (desc->fd,
536  address,
537  address_len);
538 
539  if (not_abstract)
540  (void) umask (old_mask);
541  }
542  if (0 != ret)
543  return GNUNET_SYSERR;
544 
545  desc->addr = GNUNET_malloc (address_len);
546  GNUNET_memcpy (desc->addr, address, address_len);
547  desc->addrlen = address_len;
548 
549  return GNUNET_OK;
550 }
551 
552 
559 int
561 {
562  int ret;
563 
564  ret = close (desc->fd);
565 
566  const struct sockaddr_un *un = (const struct sockaddr_un *) desc->addr;
567 
568  /* Cleanup the UNIX domain socket and its parent directories in case of non
569  abstract sockets */
570  if ((AF_UNIX == desc->af) &&
571  (NULL != desc->addr) &&
572  ('\0' != un->sun_path[0]))
573  {
574  char *dirname = GNUNET_strndup (un->sun_path,
575  sizeof(un->sun_path));
576 
577  if (0 != unlink (dirname))
578  {
580  "unlink",
581  dirname);
582  }
583  else
584  {
585  size_t len;
586 
587  len = strlen (dirname);
588  while ((len > 0) && (dirname[len] != DIR_SEPARATOR))
589  len--;
590  dirname[len] = '\0';
591  if ((0 != len) && (0 != rmdir (dirname)))
592  {
593  switch (errno)
594  {
595  case EACCES:
596  case ENOTEMPTY:
597  case EPERM:
598  /* these are normal and can just be ignored */
599  break;
600 
601  default:
603  "rmdir",
604  dirname);
605  break;
606  }
607  }
608  }
609  GNUNET_free (dirname);
610  }
612  return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR;
613 }
614 
615 
621 void
623 {
624  GNUNET_free (desc->addr);
625  GNUNET_free (desc);
626 }
627 
628 
635 struct GNUNET_NETWORK_Handle *
637 {
638  struct GNUNET_NETWORK_Handle *ret;
639 
640  if (fcntl (fd, F_GETFD) < 0)
641  return NULL; /* invalid FD */
642  ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
643  ret->fd = fd;
644  ret->af = AF_UNSPEC;
645  return ret;
646 }
647 
648 
657 int
659  const struct sockaddr *address,
660  socklen_t address_len)
661 {
662  int ret;
663 
664  ret = connect (desc->fd,
665  address,
666  address_len);
667 
668  return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
669 }
670 
671 
682 int
684  int level,
685  int optname,
686  void *optval,
687  socklen_t *optlen)
688 {
689  int ret;
690 
691  ret = getsockopt (desc->fd,
692  level,
693  optname,
694  optval, optlen);
695 
696  return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
697 }
698 
699 
707 int
709  int backlog)
710 {
711  int ret;
712 
713  ret = listen (desc->fd,
714  backlog);
715 
716  return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
717 }
718 
719 
726 ssize_t
728 {
729  int error;
730 
731  /* How much is there to be read? */
732  int pending;
733 
734  error = ioctl (desc->fd,
735  FIONREAD,
736  &pending);
737  if (0 == error)
738  return (ssize_t) pending;
739  return GNUNET_SYSERR;
740 }
741 
742 
753 ssize_t
755  void *buffer,
756  size_t length,
757  struct sockaddr *src_addr,
758  socklen_t *addrlen)
759 {
760  int flags = 0;
761 
762 #ifdef MSG_DONTWAIT
763  flags |= MSG_DONTWAIT;
764 #endif
765  return recvfrom (desc->fd,
766  buffer,
767  length,
768  flags,
769  src_addr,
770  addrlen);
771 }
772 
773 
782 ssize_t
784  void *buffer,
785  size_t length)
786 {
787  int ret;
788  int flags;
789 
790  flags = 0;
791 
792 #ifdef MSG_DONTWAIT
793  flags |= MSG_DONTWAIT;
794 #endif
795  ret = recv (desc->fd,
796  buffer,
797  length,
798  flags);
799  return ret;
800 }
801 
802 
811 ssize_t
813  const void *buffer,
814  size_t length)
815 {
816  int ret;
817  int flags;
818 
819  flags = 0;
820 #ifdef MSG_DONTWAIT
821  flags |= MSG_DONTWAIT;
822 #endif
823 #ifdef MSG_NOSIGNAL
824  flags |= MSG_NOSIGNAL;
825 #endif
826  ret = send (desc->fd,
827  buffer,
828  length,
829  flags);
830  return ret;
831 }
832 
833 
845 ssize_t
847  const void *message,
848  size_t length,
849  const struct sockaddr *dest_addr,
850  socklen_t dest_len)
851 {
852  int flags = 0;
853 
854 #ifdef MSG_DONTWAIT
855  flags |= MSG_DONTWAIT;
856 #endif
857 #ifdef MSG_NOSIGNAL
858  flags |= MSG_NOSIGNAL;
859 #endif
860  return sendto (desc->fd,
861  message,
862  length,
863  flags,
864  dest_addr,
865  dest_len);
866 }
867 
868 
879 int
881  int level,
882  int option_name,
883  const void *option_value,
884  socklen_t option_len)
885 {
886  int ret;
887 
888  ret = setsockopt (fd->fd,
889  level,
890  option_name,
891  option_value,
892  option_len);
893 
894  return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
895 }
896 
897 
908 struct GNUNET_NETWORK_Handle *
910  int type,
911  int protocol)
912 {
913  struct GNUNET_NETWORK_Handle *ret;
914  int fd;
915 
916  fd = socket (domain, type, protocol);
917  if (-1 == fd)
918  return NULL;
919  ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
920  ret->fd = fd;
921  if (GNUNET_OK !=
923  domain,
924  type))
925  return NULL;
926  return ret;
927 }
928 
929 
936 int
938  int how)
939 {
940  int ret;
941 
942  ret = shutdown (desc->fd, how);
943 
944  return (0 == ret) ? GNUNET_OK : GNUNET_SYSERR;
945 }
946 
947 
957 int
959 {
960  int ret = 0;
961 
962 #ifdef __linux__
963  int value = 0;
964 
965  if (0 !=
966  (ret =
967  setsockopt (desc->fd,
968  SOL_SOCKET,
969  SO_SNDBUF,
970  &value,
971  sizeof(value))))
973  "setsockopt");
974  if (0 !=
975  (ret =
976  setsockopt (desc->fd,
977  SOL_SOCKET,
978  SO_RCVBUF,
979  &value,
980  sizeof(value))))
982  "setsockopt");
983 #endif
984  return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
985 }
986 
987 
993 void
995 {
996  FD_ZERO (&fds->sds);
997  fds->nsds = 0;
998 }
999 
1000 
1007 void
1009  const struct GNUNET_NETWORK_Handle *desc)
1010 {
1011  FD_SET (desc->fd,
1012  &fds->sds);
1013  fds->nsds = GNUNET_MAX (fds->nsds,
1014  desc->fd + 1);
1015 }
1016 
1017 
1025 int
1027  const struct GNUNET_NETWORK_Handle *desc)
1028 {
1029  return FD_ISSET (desc->fd,
1030  &fds->sds);
1031 }
1032 
1033 
1040 void
1042  const struct GNUNET_NETWORK_FDSet *src)
1043 {
1044  int nfds;
1045 
1046  for (nfds = src->nsds; nfds >= 0; nfds--)
1047  if (FD_ISSET (nfds, &src->sds))
1048  FD_SET (nfds, &dst->sds);
1049  dst->nsds = GNUNET_MAX (dst->nsds,
1050  src->nsds);
1051 }
1052 
1053 
1060 void
1062  const struct GNUNET_NETWORK_FDSet *from)
1063 {
1064  FD_COPY (&from->sds,
1065  &to->sds);
1066  to->nsds = from->nsds;
1067 }
1068 
1069 
1076 int
1078 {
1079  return desc->fd;
1080 }
1081 
1082 
1089 struct sockaddr*
1091 {
1092  return desc->addr;
1093 }
1094 
1095 
1102 socklen_t
1104 {
1105  return desc->addrlen;
1106 }
1107 
1108 
1116 void
1118  const fd_set *from,
1119  int nfds)
1120 {
1121  FD_COPY (from,
1122  &to->sds);
1123  to->nsds = nfds;
1124 }
1125 
1126 
1133 void
1135  int nfd)
1136 {
1137  GNUNET_assert ((nfd >= 0) && (nfd < FD_SETSIZE));
1138  FD_SET (nfd, &to->sds);
1139  to->nsds = GNUNET_MAX (nfd + 1,
1140  to->nsds);
1141 }
1142 
1143 
1151 int
1153  int nfd)
1154 {
1155  if ((-1 == nfd) ||
1156  (NULL == to))
1157  return GNUNET_NO;
1158  return FD_ISSET (nfd, &to->sds) ? GNUNET_YES : GNUNET_NO;
1159 }
1160 
1161 
1167 void
1169  const struct GNUNET_DISK_FileHandle *h)
1170 {
1171  int fd;
1172 
1175  &fd,
1176  sizeof(int)));
1177  FD_SET (fd,
1178  &fds->sds);
1179  fds->nsds = GNUNET_MAX (fd + 1,
1180  fds->nsds);
1181 }
1182 
1183 
1189 void
1191  const struct GNUNET_DISK_FileHandle *h)
1192 {
1194 }
1195 
1196 
1204 int
1206  const struct GNUNET_DISK_FileHandle *h)
1207 {
1208  return FD_ISSET (h->fd,
1209  &fds->sds);
1210 }
1211 
1212 
1220 int
1222  const struct GNUNET_NETWORK_FDSet *fds2)
1223 {
1224  int nfds;
1225 
1226  nfds = GNUNET_MIN (fds1->nsds,
1227  fds2->nsds);
1228  while (nfds > 0)
1229  {
1230  nfds--;
1231  if ((FD_ISSET (nfds,
1232  &fds1->sds)) &&
1233  (FD_ISSET (nfds,
1234  &fds2->sds)))
1235  return GNUNET_YES;
1236  }
1237  return GNUNET_NO;
1238 }
1239 
1240 
1246 struct GNUNET_NETWORK_FDSet *
1248 {
1249  struct GNUNET_NETWORK_FDSet *fds;
1250 
1251  fds = GNUNET_new (struct GNUNET_NETWORK_FDSet);
1253  return fds;
1254 }
1255 
1256 
1262 void
1264 {
1265  GNUNET_free (fds);
1266 }
1267 
1268 
1276 int
1278  uint16_t port)
1279 {
1280  struct GNUNET_NETWORK_Handle *socket;
1281  int bind_status;
1282  int socktype;
1283  char open_port_str[6];
1284  struct addrinfo hint;
1285  struct addrinfo *ret;
1286  struct addrinfo *ai;
1287 
1288  GNUNET_snprintf (open_port_str,
1289  sizeof(open_port_str),
1290  "%u",
1291  (unsigned int) port);
1292  socktype = (IPPROTO_TCP == ipproto) ? SOCK_STREAM : SOCK_DGRAM;
1293  ret = NULL;
1294  memset (&hint, 0, sizeof(hint));
1295  hint.ai_family = AF_UNSPEC; /* IPv4 and IPv6 */
1296  hint.ai_socktype = socktype;
1297  hint.ai_protocol = ipproto;
1298  hint.ai_addrlen = 0;
1299  hint.ai_addr = NULL;
1300  hint.ai_canonname = NULL;
1301  hint.ai_next = NULL;
1302  hint.ai_flags = AI_PASSIVE | AI_NUMERICSERV; /* Wild card address */
1303  GNUNET_assert (0 == getaddrinfo (NULL,
1304  open_port_str,
1305  &hint,
1306  &ret));
1307  bind_status = GNUNET_NO;
1308  for (ai = ret; NULL != ai; ai = ai->ai_next)
1309  {
1310  socket = GNUNET_NETWORK_socket_create (ai->ai_family,
1311  ai->ai_socktype,
1312  ai->ai_protocol);
1313  if (NULL == socket)
1314  continue;
1315  bind_status = GNUNET_NETWORK_socket_bind (socket,
1316  ai->ai_addr,
1317  ai->ai_addrlen);
1318  GNUNET_NETWORK_socket_close (socket);
1319  if (GNUNET_OK != bind_status)
1320  break;
1321  }
1322  freeaddrinfo (ret);
1323  return bind_status;
1324 }
1325 
1326 
1336 int
1338  struct GNUNET_NETWORK_FDSet *wfds,
1339  struct GNUNET_NETWORK_FDSet *efds,
1340  const struct GNUNET_TIME_Relative timeout)
1341 {
1342  int nfds;
1343  struct timeval tv;
1344 
1345  if (NULL != rfds)
1346  nfds = rfds->nsds;
1347  else
1348  nfds = 0;
1349  if (NULL != wfds)
1350  nfds = GNUNET_MAX (nfds,
1351  wfds->nsds);
1352  if (NULL != efds)
1353  nfds = GNUNET_MAX (nfds,
1354  efds->nsds);
1355  if ((0 == nfds) &&
1356  (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us))
1357  {
1358  GNUNET_break (0);
1360  _ (
1361  "Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
1362  "select");
1363  }
1364  if (timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us > (unsigned
1365  long long)
1366  LONG_MAX)
1367  {
1368  tv.tv_sec = LONG_MAX;
1369  tv.tv_usec = 999999L;
1370  }
1371  else
1372  {
1373  tv.tv_sec = (long) (timeout.rel_value_us
1374  / GNUNET_TIME_UNIT_SECONDS.rel_value_us);
1375  tv.tv_usec =
1376  (timeout.rel_value_us
1377  - (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value_us));
1378  }
1379  return select (nfds,
1380  (NULL != rfds) ? &rfds->sds : NULL,
1381  (NULL != wfds) ? &wfds->sds : NULL,
1382  (NULL != efds) ? &efds->sds : NULL,
1383  (timeout.rel_value_us ==
1384  GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) ? NULL : &tv);
1385 }
1386 
1387 
1388 /* end of network.c */
int GNUNET_NETWORK_socket_listen(const struct GNUNET_NETWORK_Handle *desc, int backlog)
Listen on a socket.
Definition: network.c:708
int GNUNET_NETWORK_socket_setsockopt(struct GNUNET_NETWORK_Handle *fd, int level, int option_name, const void *option_value, socklen_t option_len)
Set socket option.
Definition: network.c:880
int GNUNET_NETWORK_get_fd(const struct GNUNET_NETWORK_Handle *desc)
Return file descriptor for this network handle.
Definition: network.c:1077
int GNUNET_NETWORK_fdset_overlap(const struct GNUNET_NETWORK_FDSet *fds1, const struct GNUNET_NETWORK_FDSet *fds2)
Checks if two fd sets overlap.
Definition: network.c:1221
int GNUNET_NETWORK_socket_set_blocking(struct GNUNET_NETWORK_Handle *fd, int doBlock)
Set if a socket should use blocking or non-blocking IO.
Definition: network.c:255
struct sockaddr * GNUNET_NETWORK_get_addr(const struct GNUNET_NETWORK_Handle *desc)
Return sockaddr for this network handle.
Definition: network.c:1090
int nsds
Maximum number of any socket descriptor in the set (plus one)
static int initialize_network_handle(struct GNUNET_NETWORK_Handle *h, int af, int type)
Perform proper canonical initialization for a network handle.
Definition: network.c:366
uint64_t rel_value_us
The actual value.
#define AI_NUMERICSERV
AI_NUMERICSERV not defined in windows.
Definition: platform.h:224
static int end
Set if we are to shutdown all services (including ARM).
Definition: gnunet-arm.c:34
ssize_t GNUNET_NETWORK_socket_send(const struct GNUNET_NETWORK_Handle *desc, const void *buffer, size_t length)
Send data (always non-blocking).
Definition: network.c:812
socklen_t GNUNET_NETWORK_get_addrlen(const struct GNUNET_NETWORK_Handle *desc)
Return sockaddr length for this network handle.
Definition: network.c:1103
void GNUNET_NETWORK_fdset_set_native(struct GNUNET_NETWORK_FDSet *to, int nfd)
Set a native fd in a set.
Definition: network.c:1134
ssize_t GNUNET_NETWORK_socket_recv(const struct GNUNET_NETWORK_Handle *desc, void *buffer, size_t length)
Read data from a connected socket (always non-blocking).
Definition: network.c:783
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_TIME_UNIT_SECONDS
One second.
int GNUNET_NETWORK_socket_getsockopt(const struct GNUNET_NETWORK_Handle *desc, int level, int optname, void *optval, socklen_t *optlen)
Get socket options.
Definition: network.c:683
int GNUNET_NETWORK_socket_disable_corking(struct GNUNET_NETWORK_Handle *desc)
Disable the "CORK" feature for communication with the given socket, forcing the OS to immediately flu...
Definition: network.c:958
int GNUNET_NETWORK_socket_bind(struct GNUNET_NETWORK_Handle *desc, const struct sockaddr *address, socklen_t address_len)
Bind a socket to a particular address.
Definition: network.c:485
int GNUNET_NETWORK_fdset_test_native(const struct GNUNET_NETWORK_FDSet *to, int nfd)
Test native fd in a set.
Definition: network.c:1152
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_new(type)
Allocate a struct or union of the given type.
int GNUNET_NETWORK_socket_shutdown(struct GNUNET_NETWORK_Handle *desc, int how)
Shut down socket operations.
Definition: network.c:937
static int socket_set_inheritable(const struct GNUNET_NETWORK_Handle *h)
Make a socket non-inheritable to child processes.
Definition: network.c:292
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:99
void GNUNET_NETWORK_fdset_copy_native(struct GNUNET_NETWORK_FDSet *to, const fd_set *from, int nfds)
Copy a native fd set.
Definition: network.c:1117
#define _(String)
GNU gettext support macro.
Definition: platform.h:178
static void socket_set_nodelay(const struct GNUNET_NETWORK_Handle *h)
Disable delays when sending data via the socket.
Definition: network.c:339
int GNUNET_snprintf(char *buf, size_t size, const char *format,...) __attribute__((format(printf
Like snprintf, just aborts if the buffer is of insufficient size.
void GNUNET_NETWORK_socket_free_memory_only_(struct GNUNET_NETWORK_Handle *desc)
Only free memory of a socket, keep the file descriptor untouched.
Definition: network.c:622
struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_accept(const struct GNUNET_NETWORK_Handle *desc, struct sockaddr *address, socklen_t *address_len)
accept a new connection on a socket
Definition: network.c:430
void GNUNET_NETWORK_fdset_destroy(struct GNUNET_NETWORK_FDSet *fds)
Releases the associated memory of an fd set.
Definition: network.c:1263
#define GNUNET_log_strerror(level, cmd)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
struct GNUNET_NETWORK_FDSet * GNUNET_NETWORK_fdset_create()
Creates an fd set.
Definition: network.c:1247
int GNUNET_NETWORK_socket_connect(const struct GNUNET_NETWORK_Handle *desc, const struct sockaddr *address, socklen_t address_len)
Connect a socket to some remote address.
Definition: network.c:658
static struct GNUNET_TIME_Relative timeout
Desired timeout for the lookup (default is no timeout).
Definition: gnunet-abd.c:61
static char * value
Value of the record to add/remove.
#define INVALID_SOCKET
Definition: network.c:45
#define GNUNET_log_strerror_file(level, cmd, filename)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:41
#define LOG_STRERROR_FILE(kind, syscall, filename)
Definition: network.c:32
#define GNUNET_MAX(a, b)
Definition: gnunet_common.h:95
#define GNUNET_MIN(a, b)
Definition: gnunet_common.h:93
ssize_t GNUNET_NETWORK_socket_sendto(const struct GNUNET_NETWORK_Handle *desc, const void *message, size_t length, const struct sockaddr *dest_addr, socklen_t dest_len)
Send data to a particular destination (always non-blocking).
Definition: network.c:846
collection of IO descriptors
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
const char * GNUNET_a2s(const struct sockaddr *addr, socklen_t addrlen)
Convert a "struct sockaddr*" (IPv4 or IPv6 address) to a string (for printing debug messages)...
A 512-bit hashcode.
struct sockaddr * addr
Address we were bound to, or NULL.
Definition: network.c:74
#define DIR_SEPARATOR
Definition: platform.h:164
int GNUNET_NETWORK_socket_select(struct GNUNET_NETWORK_FDSet *rfds, struct GNUNET_NETWORK_FDSet *wfds, struct GNUNET_NETWORK_FDSet *efds, const struct GNUNET_TIME_Relative timeout)
Check if sockets or pipes meet certain conditions.
Definition: network.c:1337
void GNUNET_CRYPTO_hash_to_enc(const struct GNUNET_HashCode *block, struct GNUNET_CRYPTO_HashAsciiEncoded *result)
Convert hash to ASCII encoding.
Definition: crypto_hash.c:55
static struct GNUNET_TRANSPORT_AddressIdentifier * ai
Handle to the operation that publishes our address.
void GNUNET_NETWORK_fdset_zero(struct GNUNET_NETWORK_FDSet *fds)
Reset FD set.
Definition: network.c:994
static unsigned int pending
The number of queries that are outstanding.
fd_set sds
Bitset with the descriptors.
void GNUNET_NETWORK_fdset_handle_set(struct GNUNET_NETWORK_FDSet *fds, const struct GNUNET_DISK_FileHandle *h)
Add a file handle to the fd set.
Definition: network.c:1168
void GNUNET_NETWORK_fdset_set(struct GNUNET_NETWORK_FDSet *fds, const struct GNUNET_NETWORK_Handle *desc)
Add a socket to the FD set.
Definition: network.c:1008
int GNUNET_NETWORK_fdset_handle_isset(const struct GNUNET_NETWORK_FDSet *fds, const struct GNUNET_DISK_FileHandle *h)
Check if a file handle is part of an fd set.
Definition: network.c:1205
0-terminated ASCII encoding of a struct GNUNET_HashCode.
enum GNUNET_GenericReturnValue GNUNET_DISK_internal_file_handle_(const struct GNUNET_DISK_FileHandle *fh, void *dst, size_t dst_len)
Retrieve OS file handle.
Definition: disk.c:1632
#define GNUNET_strndup(a, length)
Wrapper around GNUNET_xstrndup_.
static uint16_t port
Port number.
Definition: gnunet-bcd.c:83
void GNUNET_NETWORK_fdset_copy(struct GNUNET_NETWORK_FDSet *to, const struct GNUNET_NETWORK_FDSet *from)
Copy one fd set to another.
Definition: network.c:1061
socklen_t addrlen
Number of bytes in addr.
Definition: network.c:69
const char * name
#define FD_COPY(s, d)
Definition: network.c:243
int GNUNET_NETWORK_test_port_free(int ipproto, uint16_t port)
Test if the given port is available.
Definition: network.c:1277
#define LOG_STRERROR(kind, syscall)
Definition: network.c:37
#define LOG(kind,...)
Definition: network.c:31
#define GNUNET_log(kind,...)
handle to a socket
Definition: network.c:52
char * GNUNET_NETWORK_shorten_unixpath(char *unixpath)
Given a unixpath that is too long (larger than UNIX_PATH_MAX), shorten it to an acceptable length whi...
Definition: network.c:158
int af
Address family / domain.
Definition: network.c:59
ssize_t GNUNET_NETWORK_socket_recvfrom_amount(const struct GNUNET_NETWORK_Handle *desc)
How much data is available to be read on this descriptor?
Definition: network.c:727
int fd
File handle on Unix-like systems.
void GNUNET_NETWORK_unix_precheck(const struct sockaddr_un *un)
If services crash, they can leave a unix domain socket file on the disk.
Definition: network.c:202
struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_box_native(int fd)
Box a native socket (and check that it is a socket).
Definition: network.c:636
int GNUNET_NETWORK_fdset_isset(const struct GNUNET_NETWORK_FDSet *fds, const struct GNUNET_NETWORK_Handle *desc)
Check whether a socket is part of the fd set.
Definition: network.c:1026
ssize_t GNUNET_NETWORK_socket_recvfrom(const struct GNUNET_NETWORK_Handle *desc, void *buffer, size_t length, struct sockaddr *src_addr, socklen_t *addrlen)
Read data from a socket (always non-blocking).
Definition: network.c:754
void GNUNET_NETWORK_fdset_add(struct GNUNET_NETWORK_FDSet *dst, const struct GNUNET_NETWORK_FDSet *src)
Add one fd set to another.
Definition: network.c:1041
static char * address
GNS address for this phone.
Handle used to access files (and pipes).
int GNUNET_NETWORK_socket_close(struct GNUNET_NETWORK_Handle *desc)
Close a socket.
Definition: network.c:560
int type
Type of the socket.
Definition: network.c:64
#define GNUNET_malloc(size)
Wrapper around malloc.
int GNUNET_NETWORK_test_pf(int pf)
Test if the given protocol family is supported by this system.
Definition: network.c:85
Internal DISK related helper functions.
#define GNUNET_free(ptr)
Wrapper around free.
Time for relative time used by GNUnet, in microseconds.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_create(int domain, int type, int protocol)
Create a new socket.
Definition: network.c:909
void GNUNET_NETWORK_fdset_handle_set_first(struct GNUNET_NETWORK_FDSet *fds, const struct GNUNET_DISK_FileHandle *h)
Add a file handle to the fd set.
Definition: network.c:1190