aae9d687231688a43baeed7a81695b6a1ab1082c
[oweals/gnunet.git] / src / util / network.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 2, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file util/network/network.c
23  * @brief basic, low-level TCP networking interface
24  * @author Christian Grothoff
25  *
26  * This code is rather complex.  Only modify it if you
27  * 1) Have a NEW testcase showing that the new code
28  *    is needed and correct
29  * 2) All EXISTING testcases pass with the new code
30  * These rules should apply in general, but for this
31  * module they are VERY, VERY important.
32  *
33  * TODO:
34  * - can we merge receive_ready and receive_again?
35  * - can we integrate the nth.timeout_task with the write_task's timeout?
36  */
37
38 #include "platform.h"
39 #include "gnunet_common.h"
40 #include "gnunet_network_lib.h"
41 #include "gnunet_scheduler_lib.h"
42
43 #define DEBUG_NETWORK GNUNET_NO
44
45 /**
46  * List of address families to give as hints to
47  * getaddrinfo, in reverse order of  preference.
48  */
49 static int address_families[] = 
50   { AF_INET, AF_INET6, AF_UNSPEC };
51
52 struct GNUNET_NETWORK_TransmitHandle
53 {
54
55   /**
56    * Function to call if the send buffer has notify_size
57    * bytes available.
58    */
59   GNUNET_NETWORK_TransmitReadyNotify notify_ready;
60
61   /**
62    * Closure for notify_ready.
63    */
64   void *notify_ready_cls;
65
66   /**
67    * Our socket handle.
68    */
69   struct GNUNET_NETWORK_SocketHandle *sh;
70
71   /**
72    * Timeout for receiving (in absolute time).
73    */
74   struct GNUNET_TIME_Absolute transmit_timeout;
75
76   /**
77    * Task called on timeout.
78    */
79   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
80
81   /**
82    * At what number of bytes available in the
83    * write buffer should the notify method be called?
84    */
85   size_t notify_size;
86
87 };
88
89 /**
90  * @brief handle for a network socket
91  */
92 struct GNUNET_NETWORK_SocketHandle
93 {
94
95   /**
96    * Scheduler that was used for the connect task.
97    */
98   struct GNUNET_SCHEDULER_Handle *sched;
99
100   /**
101    * Address information for connect (may be NULL).
102    */
103   struct addrinfo *ai;
104
105   /**
106    * Index for the next struct addrinfo for connect attempts (may be NULL)
107    */
108   struct addrinfo *ai_pos;
109
110   /**
111    * Network address of the other end-point, may be NULL.
112    */
113   struct sockaddr *addr;
114
115   /**
116    * Pointer to the hostname if socket was
117    * created using DNS lookup, otherwise NULL.
118    */
119   char *hostname;
120
121   /**
122    * Pointer to our write buffer.
123    */
124   char *write_buffer;
125
126   /**
127    * Size of our write buffer.
128    */
129   size_t write_buffer_size;
130
131   /**
132    * Current write-offset in write buffer (where
133    * would we write next).
134    */
135   size_t write_buffer_off;
136
137   /**
138    * Current read-offset in write buffer (how many
139    * bytes have already been send).
140    */
141   size_t write_buffer_pos;
142
143   /**
144    * Length of addr.
145    */
146   socklen_t addrlen;
147
148   /**
149    * Offset in our address family list
150    * that we used last.
151    */
152   int af_fam_offset;
153
154   /**
155    * Connect task that we may need to wait for.
156    */
157   GNUNET_SCHEDULER_TaskIdentifier connect_task;
158
159   /**
160    * Read task that we may need to wait for.
161    */
162   GNUNET_SCHEDULER_TaskIdentifier read_task;
163
164   /**
165    * Write task that we may need to wait for.
166    */
167   GNUNET_SCHEDULER_TaskIdentifier write_task;
168
169   /**
170    * The handle we return for GNUNET_NETWORK_notify_transmit_ready.
171    */
172   struct GNUNET_NETWORK_TransmitHandle nth;
173
174   /**
175    * Underlying OS's socket, set to -1 after fatal errors.
176    */
177   int sock;
178
179   /**
180    * Port to connect to.
181    */
182   uint16_t port;
183
184   /**
185    * Function to call on data received, NULL
186    * if no receive is pending.
187    */
188   GNUNET_NETWORK_Receiver receiver;
189
190   /**
191    * Closure for receiver.
192    */
193   void *receiver_cls;
194
195   /**
196    * Timeout for receiving (in absolute time).
197    */
198   struct GNUNET_TIME_Absolute receive_timeout;
199
200   /**
201    * Maximum number of bytes to read
202    * (for receiving).
203    */
204   size_t max;
205
206 };
207
208
209 /**
210  * Create a socket handle by boxing an existing OS socket.  The OS
211  * socket should henceforth be no longer used directly.
212  * GNUNET_socket_destroy will close it.
213  *
214  * @param sched scheduler to use
215  * @param osSocket existing socket to box
216  * @param maxbuf maximum write buffer size for the socket (use
217  *        0 for sockets that need no write buffers, such as listen sockets)
218  * @return the boxed socket handle
219  */
220 struct GNUNET_NETWORK_SocketHandle *
221 GNUNET_NETWORK_socket_create_from_existing (struct GNUNET_SCHEDULER_Handle
222                                             *sched, int osSocket,
223                                             size_t maxbuf)
224 {
225   struct GNUNET_NETWORK_SocketHandle *ret;
226   ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_SocketHandle) + maxbuf);
227   ret->write_buffer = (char *) &ret[1];
228   ret->write_buffer_size = maxbuf;
229   ret->sock = osSocket;
230   ret->sched = sched;
231   return ret;
232 }
233
234
235 /**
236  * Create a socket handle by accepting on a listen socket.  This
237  * function may block if the listen socket has no connection ready.
238  *
239  * @param sched scheduler to use
240  * @param access function to use to check if access is allowed
241  * @param access_cls closure for access
242  * @param lsock listen socket
243  * @param maxbuf maximum write buffer size for the socket (use
244  *        0 for sockets that need no write buffers, such as listen sockets)
245  * @return the socket handle, NULL on error
246  */
247 struct GNUNET_NETWORK_SocketHandle *
248 GNUNET_NETWORK_socket_create_from_accept (struct GNUNET_SCHEDULER_Handle
249                                           *sched,
250                                           GNUNET_NETWORK_AccessCheck access,
251                                           void *access_cls, int lsock,
252                                           size_t maxbuf)
253 {
254   struct GNUNET_NETWORK_SocketHandle *ret;
255   char addr[32];
256   socklen_t addrlen;
257   int fd;
258   int aret;
259   struct sockaddr_in *v4;
260   struct sockaddr_in6 *v6;
261   struct sockaddr *sa;
262   void *uaddr;
263
264   addrlen = sizeof (addr);
265   fd = accept (lsock, (struct sockaddr *) &addr, &addrlen);
266   if (fd == -1)
267     {
268       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "accept");
269       return NULL;
270     }
271 #ifndef MINGW
272   // FIXME NILS
273   if (0 != fcntl (fd, F_SETFD, fcntl (fd, F_GETFD) | FD_CLOEXEC))
274     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
275                          "fcntl");
276 #endif
277   if (addrlen > sizeof (addr))
278     {
279       GNUNET_break (0);
280       GNUNET_break (0 == CLOSE (fd));
281       return NULL;
282     }
283
284   sa = (struct sockaddr *) addr;
285   v6 = (struct sockaddr_in6 *) addr;
286   if ((sa->sa_family == AF_INET6) && (IN6_IS_ADDR_V4MAPPED (&v6->sin6_addr)))
287     {
288       /* convert to V4 address */
289       v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
290       memset (v4, 0, sizeof (struct sockaddr_in));
291       v4->sin_family = AF_INET;
292       memcpy (&v4->sin_addr,
293               &((char *) &v6->sin6_addr)[sizeof (struct in6_addr) -
294                                          sizeof (struct in_addr)],
295               sizeof (struct in_addr));
296       v4->sin_port = v6->sin6_port;
297       uaddr = v4;
298       addrlen = sizeof (struct sockaddr_in);
299     }
300   else
301     {
302       uaddr = GNUNET_malloc (addrlen);
303       memcpy (uaddr, addr, addrlen);
304     }
305
306   if ((access != NULL) &&
307       (GNUNET_YES != (aret = access (access_cls, uaddr, addrlen))))
308     {
309       if (aret == GNUNET_NO)
310         GNUNET_log (GNUNET_ERROR_TYPE_INFO,
311                     _("Access denied to `%s'\n"),
312                     GNUNET_a2s(uaddr, addrlen));
313       GNUNET_break (0 == SHUTDOWN (fd, SHUT_RDWR));
314       GNUNET_break (0 == CLOSE (fd));
315       GNUNET_free (uaddr);
316       return NULL;
317     }
318 #if DEBUG_NETWORK
319   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
320               _("Accepting connection from `%s'\n"),
321               GNUNET_a2s(uaddr, addrlen));
322 #endif
323   ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_SocketHandle) + maxbuf);
324   ret->write_buffer = (char *) &ret[1];
325   ret->write_buffer_size = maxbuf;
326   ret->addr = uaddr;
327   ret->addrlen = addrlen;
328   ret->sock = fd;
329   ret->sched = sched;
330   return ret;
331 }
332
333 /**
334  * Obtain the network address of the other party.
335  *
336  * @param sock the client to get the address for
337  * @param addr where to store the address
338  * @param addrlen where to store the length of the address
339  * @return GNUNET_OK on success
340  */
341 int
342 GNUNET_NETWORK_socket_get_address (struct GNUNET_NETWORK_SocketHandle *sock,
343                                    void **addr, size_t * addrlen)
344 {
345   if ((sock->addr == NULL) || (sock->addrlen == 0))
346     return GNUNET_NO;
347   *addr = GNUNET_malloc (sock->addrlen);
348   memcpy (*addr, sock->addr, sock->addrlen);
349   *addrlen = sock->addrlen;
350   return GNUNET_OK;
351 }
352
353
354 /**
355  * Set if a socket should use blocking or non-blocking IO.
356  *
357  * @return GNUNET_OK on success, GNUNET_SYSERR on error
358  */
359 static int
360 socket_set_blocking (int handle, int doBlock)
361 {
362 #if MINGW
363   u_long mode;
364   mode = !doBlock;
365 #if HAVE_PLIBC_FD
366   if (ioctlsocket (plibc_fd_get_handle (handle), FIONBIO, &mode) ==
367       SOCKET_ERROR)
368     {
369       SetErrnoFromWinsockError (WSAGetLastError ());
370       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctlsocket");
371       return GNUNET_SYSERR;
372     }
373 #else
374   if (ioctlsocket (handle, FIONBIO, &mode) == SOCKET_ERROR)
375     {
376       SetErrnoFromWinsockError (WSAGetLastError ());
377       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctlsocket");
378       return GNUNET_SYSERR;
379     }
380 #endif
381   /* store the blocking mode */
382 #if HAVE_PLIBC_FD
383   plibc_fd_set_blocking (handle, doBlock);
384 #else
385   __win_SetHandleBlockingMode (handle, doBlock);
386 #endif
387   return GNUNET_OK;
388
389 #else
390   /* not MINGW */
391   int flags = fcntl (handle, F_GETFL);
392   if (flags == -1)
393     {
394       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl");
395       return GNUNET_SYSERR;
396     }
397   if (doBlock)
398     flags &= ~O_NONBLOCK;
399   else
400     flags |= O_NONBLOCK;
401   if (0 != fcntl (handle, F_SETFL, flags))
402     {
403       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl");
404       return GNUNET_SYSERR;
405     }
406   return GNUNET_OK;
407 #endif
408 }
409
410
411 /**
412  * Perform a DNS lookup for the hostname associated
413  * with the current socket, iterating over the address
414  * families as specified in the "address_families" array.
415  */
416 static void
417 try_lookup (struct GNUNET_NETWORK_SocketHandle *sock)
418 {
419   struct addrinfo hints;
420   int ec;
421
422   while ( (sock->ai_pos == NULL) &&
423           (sock->af_fam_offset > 0) )
424     {
425       if (sock->ai != NULL)
426         freeaddrinfo (sock->ai);
427       memset (&hints, 0, sizeof (hints));
428       hints.ai_family = address_families[--sock->af_fam_offset];
429       hints.ai_socktype = SOCK_STREAM;
430       if (0 != (ec = getaddrinfo (sock->hostname, NULL, &hints, &sock->ai)))
431         {
432           GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
433                       "`%s' failed for hostname `%s': %s\n",
434                       "getaddrinfo", sock->hostname, gai_strerror (ec));
435           sock->ai = NULL;
436         }
437       sock->ai_pos = sock->ai;
438     }
439 }
440
441
442 /**
443  * Initiate asynchronous TCP connect request.
444  *
445  * @param sock what socket to connect
446  * @return GNUNET_SYSERR error (no more addresses to try)
447  */
448 static int
449 try_connect (struct GNUNET_NETWORK_SocketHandle *sock)
450 {
451   int s;
452
453   if (sock->addr != NULL)
454     {
455       GNUNET_free (sock->addr);
456       sock->addr = NULL;
457       sock->addrlen = 0;
458     }
459   while (1)
460     {
461       if (sock->ai_pos == NULL)
462         try_lookup (sock);
463       if (sock->ai_pos == NULL)
464         {
465           /* no more addresses to try, fatal! */
466           return GNUNET_SYSERR;
467         }
468       switch (sock->ai_pos->ai_family)
469         {
470         case AF_INET:
471           ((struct sockaddr_in *) sock->ai_pos->ai_addr)->sin_port =
472             htons (sock->port);
473           break;
474         case AF_INET6:
475           ((struct sockaddr_in6 *) sock->ai_pos->ai_addr)->sin6_port =
476             htons (sock->port);
477           break;
478         default:
479           sock->ai_pos = sock->ai_pos->ai_next;
480           continue;
481         }
482       s = SOCKET (sock->ai_pos->ai_family, SOCK_STREAM, 0);
483       if (s == -1)
484         {
485           /* maybe unsupported address family, try next */
486           GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "socket");
487           sock->ai_pos = sock->ai_pos->ai_next;
488           continue;
489         }
490 #ifndef MINGW
491       // FIXME NILS
492       if (0 != fcntl (s, F_SETFD, fcntl (s, F_GETFD) | FD_CLOEXEC))
493         GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
494                              "fcntl");
495 #endif
496       if (GNUNET_SYSERR == socket_set_blocking (s, GNUNET_NO))
497         {
498           /* we'll treat this one as fatal */
499           GNUNET_break (0 == CLOSE (s));
500           return GNUNET_SYSERR;
501         }
502 #if DEBUG_NETWORK
503       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
504                   _("Trying to connect to `%s'\n"),
505                   GNUNET_a2s(sock->ai_pos->ai_addr,
506                              sock->ai_pos->ai_addrlen));
507 #endif
508       if ((0 != CONNECT (s,
509                          sock->ai_pos->ai_addr,
510                          sock->ai_pos->ai_addrlen)) && (errno != EINPROGRESS))
511         {
512           /* maybe refused / unsupported address, try next */
513           GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "connect");
514           GNUNET_break (0 == CLOSE (s));
515           sock->ai_pos = sock->ai_pos->ai_next;
516           continue;
517         }
518       break;
519     }
520   /* got one! copy address information! */
521   sock->addrlen = sock->ai_pos->ai_addrlen;
522   sock->addr = GNUNET_malloc (sock->addrlen);
523   memcpy (sock->addr, sock->ai_pos->ai_addr, sock->addrlen);
524   sock->ai_pos = sock->ai_pos->ai_next;
525   sock->sock = s;
526   return GNUNET_OK;
527 }
528
529
530 /**
531  * Scheduler let us know that we're either ready to
532  * write on the socket OR connect timed out.  Do the
533  * right thing.
534  */
535 static void
536 connect_continuation (void *cls,
537                       const struct GNUNET_SCHEDULER_TaskContext *tc)
538 {
539   struct GNUNET_NETWORK_SocketHandle *sock = cls;
540   unsigned int len;
541   int error;
542
543   /* nobody needs to wait for us anymore... */
544   sock->connect_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
545   /* Note: write-ready does NOT mean connect succeeded,
546      we need to use getsockopt to be sure */
547   len = sizeof (error);
548   errno = 0;
549   error = 0;
550   if ((0 == (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) ||
551       (0 != getsockopt (sock->sock, SOL_SOCKET, SO_ERROR, &error, &len)) ||
552       (error != 0) || (errno != 0))
553     {
554 #if DEBUG_NETWORK
555       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
556                   "Failed to establish TCP connection to `%s'\n",
557                   GNUNET_a2s(sock->addr, sock->addrlen));
558 #endif
559       /* connect failed / timed out */
560       GNUNET_break (0 == CLOSE (sock->sock));
561       sock->sock = -1;
562       if (GNUNET_SYSERR == try_connect (sock))
563         {
564           /* failed for good */
565 #if DEBUG_NETWORK
566           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
567                       "Failed to establish TCP connection, no further addresses to try.\n");
568 #endif
569           /* connect failed / timed out */
570           GNUNET_break (sock->ai_pos == NULL);
571           freeaddrinfo (sock->ai);
572           sock->ai = NULL;
573           return;
574         }
575       sock->connect_task = GNUNET_SCHEDULER_add_write (tc->sched, GNUNET_NO,    /* abort on shutdown */
576                                                        GNUNET_SCHEDULER_PRIORITY_KEEP,
577                                                        GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
578                                                        GNUNET_NETWORK_CONNECT_RETRY_TIMEOUT,
579                                                        sock->sock,
580                                                        &connect_continuation,
581                                                        sock);
582       return;
583     }
584   /* connect succeeded! clean up "ai" */
585 #if DEBUG_NETWORK
586   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
587               "Connection to `%s' succeeded!\n",
588               GNUNET_a2s(sock->addr, sock->addrlen));
589 #endif
590   freeaddrinfo (sock->ai);
591   sock->ai_pos = NULL;
592   sock->ai = NULL;
593 }
594
595
596 /**
597  * Create a socket handle by (asynchronously) connecting to a host.
598  * This function returns immediately, even if the connection has not
599  * yet been established.  This function only creates TCP connections.
600  *
601  * @param sched scheduler to use
602  * @param hostname name of the host to connect to
603  * @param port port to connect to
604  * @param maxbuf maximum write buffer size for the socket (use
605  *        0 for sockets that need no write buffers, such as listen sockets)
606  * @return the socket handle
607  */
608 struct GNUNET_NETWORK_SocketHandle *
609 GNUNET_NETWORK_socket_create_from_connect (struct GNUNET_SCHEDULER_Handle
610                                            *sched, const char *hostname,
611                                            uint16_t port, size_t maxbuf)
612 {
613   struct GNUNET_NETWORK_SocketHandle *ret;
614
615   ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_SocketHandle) + maxbuf);
616   ret->sock = -1;
617   ret->sched = sched;
618   ret->write_buffer = (char *) &ret[1];
619   ret->write_buffer_size = maxbuf;
620   ret->port = port;
621   ret->af_fam_offset = sizeof (address_families) / sizeof(address_families[0]);
622   ret->hostname = GNUNET_strdup (hostname);
623   if (GNUNET_SYSERR == try_connect (ret))
624     {
625       if (NULL != ret->ai)
626         freeaddrinfo (ret->ai);
627       GNUNET_free (ret->hostname);
628       GNUNET_free (ret);
629       return NULL;
630     }
631   ret->connect_task = GNUNET_SCHEDULER_add_write (sched, GNUNET_NO,     /* abort on shutdown */
632                                                   GNUNET_SCHEDULER_PRIORITY_KEEP,
633                                                   GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
634                                                   GNUNET_NETWORK_CONNECT_RETRY_TIMEOUT,
635                                                   ret->sock,
636                                                   &connect_continuation, ret);
637   return ret;
638
639 }
640
641
642 /**
643  * Create a socket handle by (asynchronously) connecting to a host.
644  * This function returns immediately, even if the connection has not
645  * yet been established.  This function only creates TCP connections.
646  *
647  * @param sched scheduler to use
648  * @param af_family address family to use
649  * @param serv_addr server address
650  * @param addrlen length of server address
651  * @param maxbuf maximum write buffer size for the socket (use
652  *        0 for sockets that need no write buffers, such as listen sockets)
653  * @return the socket handle
654  */
655 struct GNUNET_NETWORK_SocketHandle *
656 GNUNET_NETWORK_socket_create_from_sockaddr (struct GNUNET_SCHEDULER_Handle
657                                             *sched, int af_family,
658                                             const struct sockaddr *serv_addr,
659                                             socklen_t addrlen, size_t maxbuf)
660 {
661   int s;
662   struct GNUNET_NETWORK_SocketHandle *ret;
663
664   s = SOCKET (af_family, SOCK_STREAM, 0);
665   if (s == -1)
666     {
667       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING |
668                            GNUNET_ERROR_TYPE_BULK, "socket");
669       return NULL;
670     }
671 #ifndef MINGW
672   if (0 != fcntl (s, F_SETFD, fcntl (s, F_GETFD) | FD_CLOEXEC))
673     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
674                          "fcntl");
675 #endif
676   if (GNUNET_SYSERR == socket_set_blocking (s, GNUNET_NO))
677     {
678       /* we'll treat this one as fatal */
679       GNUNET_break (0 == CLOSE (s));
680       return NULL;
681     }
682 #if DEBUG_NETWORK
683   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
684               _("Trying to connect to `%s'\n"),
685               GNUNET_a2s(serv_addr, addrlen));
686 #endif
687   if ((0 != CONNECT (s, serv_addr, addrlen)) && (errno != EINPROGRESS))
688     {
689       /* maybe refused / unsupported address, try next */
690       GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "connect");
691       GNUNET_break (0 == CLOSE (s));
692       return NULL;
693     }
694   ret = GNUNET_NETWORK_socket_create_from_existing (sched, s, maxbuf);
695   ret->addr = GNUNET_malloc (addrlen);
696   memcpy (ret->addr, serv_addr, addrlen);
697   ret->addrlen = addrlen;
698   return ret;
699 }
700
701
702 /**
703  * Check if socket is valid (no fatal errors have happened so far).
704  * Note that a socket that is still trying to connect is considered
705  * valid.
706  *
707  * @param sock socket to check
708  * @return GNUNET_YES if valid, GNUNET_NO otherwise
709  */
710 int
711 GNUNET_NETWORK_socket_check (struct GNUNET_NETWORK_SocketHandle *sock)
712 {
713   if (sock->ai != NULL)
714     return GNUNET_YES;          /* still trying to connect */
715   return (sock->sock == -1) ? GNUNET_NO : GNUNET_YES;
716 }
717
718
719 /**
720  * Scheduler let us know that the connect task is finished (or was
721  * cancelled due to shutdown).  Now really clean up.
722  */
723 static void
724 destroy_continuation (void *cls,
725                       const struct GNUNET_SCHEDULER_TaskContext *tc)
726 {
727   struct GNUNET_NETWORK_SocketHandle *sock = cls;
728   GNUNET_NETWORK_TransmitReadyNotify notify;
729
730   if (sock->write_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
731     {
732       GNUNET_SCHEDULER_add_after (sock->sched,
733                                   GNUNET_YES,
734                                   GNUNET_SCHEDULER_PRIORITY_KEEP,
735                                   sock->write_task,
736                                   &destroy_continuation, sock);
737       return;
738     }
739   if (sock->sock != -1)
740     {
741 #if DEBUG_NETWORK
742       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down socket.\n");
743 #endif
744       SHUTDOWN (sock->sock, SHUT_RDWR);
745     }
746   if (sock->read_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
747     {
748       GNUNET_SCHEDULER_add_after (sock->sched,
749                                   GNUNET_YES,
750                                   GNUNET_SCHEDULER_PRIORITY_KEEP,
751                                   sock->read_task,
752                                   &destroy_continuation, sock);
753       return;
754     }
755   if (NULL != (notify = sock->nth.notify_ready))
756     {
757       sock->nth.notify_ready = NULL;
758       notify (sock->nth.notify_ready_cls, 0, NULL);
759       if (sock->nth.timeout_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
760         {
761           GNUNET_SCHEDULER_cancel (sock->sched, sock->nth.timeout_task);
762           sock->nth.timeout_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
763         }
764     }
765   if (sock->sock != -1)
766     GNUNET_break (0 == CLOSE (sock->sock));
767   GNUNET_free_non_null (sock->addr);
768   if (sock->ai != NULL)
769     freeaddrinfo (sock->ai);      
770   GNUNET_free_non_null (sock->hostname);
771   GNUNET_free (sock);
772 }
773
774
775 /**
776  * Close the socket and free associated resources. Pending
777  * transmissions are simply dropped.  A pending receive call will be
778  * called with an error code of "EPIPE".
779  *
780  * @param sock socket to destroy
781  */
782 void
783 GNUNET_NETWORK_socket_destroy (struct GNUNET_NETWORK_SocketHandle *sock)
784 {
785   if (sock->write_buffer_off == 0)
786     sock->ai_pos = NULL;        /* if we're still trying to connect and have
787                                    no message pending, stop trying! */
788   GNUNET_assert (sock->sched != NULL);
789   GNUNET_SCHEDULER_add_after (sock->sched,
790                               GNUNET_YES,
791                               GNUNET_SCHEDULER_PRIORITY_KEEP,
792                               sock->connect_task,
793                               &destroy_continuation, sock);
794 }
795
796 /**
797  * Tell the receiver callback that a timeout was reached.
798  */
799 static void
800 signal_timeout (struct GNUNET_NETWORK_SocketHandle *sh)
801 {
802   GNUNET_NETWORK_Receiver receiver;
803
804 #if DEBUG_NETWORK
805   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
806               "Network signals time out to receiver!\n");
807 #endif
808   GNUNET_assert (NULL != (receiver = sh->receiver));
809   sh->receiver = NULL;
810   receiver (sh->receiver_cls, NULL, 0, NULL, 0, 0);
811 }
812
813
814 /**
815  * Tell the receiver callback that we had an IO error.
816  */
817 static void
818 signal_error (struct GNUNET_NETWORK_SocketHandle *sh, int errcode)
819 {
820   GNUNET_NETWORK_Receiver receiver;
821   GNUNET_assert (NULL != (receiver = sh->receiver));
822   sh->receiver = NULL;
823   receiver (sh->receiver_cls, NULL, 0, sh->addr, sh->addrlen, errcode);
824 }
825
826
827 /**
828  * This function is called once we either timeout
829  * or have data ready to read.
830  */
831 static void
832 receive_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
833 {
834   struct GNUNET_NETWORK_SocketHandle *sh = cls;
835   struct GNUNET_TIME_Absolute now;
836   char buffer[sh->max];
837   ssize_t ret;
838   GNUNET_NETWORK_Receiver receiver;
839
840   sh->read_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
841   now = GNUNET_TIME_absolute_get ();
842   if ((now.value > sh->receive_timeout.value) ||
843       (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) ||
844       (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
845     {
846 #if DEBUG_NETWORK
847       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
848                   "Receive encounters error: time out...\n");
849 #endif
850       signal_timeout (sh);
851       return;
852     }
853   if (sh->sock == -1)
854     {
855       /* connect failed for good */
856 #if DEBUG_NETWORK
857       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
858                   "Receive encounters error, socket closed...\n");
859 #endif
860       signal_error (sh, ECONNREFUSED);
861       return;
862     }
863   GNUNET_assert (FD_ISSET (sh->sock, tc->read_ready));
864 RETRY:
865   ret = RECV (sh->sock, buffer, sh->max,
866 #ifndef MINGW
867       // FIXME MINGW
868       MSG_DONTWAIT
869 #else
870       0
871 #endif
872       );
873   if (ret == -1)
874     {
875       if (errno == EINTR)
876         goto RETRY;
877 #if DEBUG_NETWORK
878       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
879                   "Error receiving: %s\n", STRERROR (errno));
880 #endif
881       signal_error (sh, errno);
882       return;
883     }
884 #if DEBUG_NETWORK
885   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
886               "receive_ready read %u/%u bytes from `%s'!\n",
887               (unsigned int) ret,
888               sh->max,
889               GNUNET_a2s(sh->addr, sh->addrlen));
890 #endif
891   GNUNET_assert (NULL != (receiver = sh->receiver));
892   sh->receiver = NULL;
893   receiver (sh->receiver_cls, buffer, ret, sh->addr, sh->addrlen, 0);
894 }
895
896
897 /**
898  * This function is called after establishing a connection either has
899  * succeeded or timed out.  Note that it is possible that the attempt
900  * timed out and that we're immediately retrying.  If we are retrying,
901  * we need to wait again (or timeout); if we succeeded, we need to
902  * wait for data (or timeout).
903  */
904 static void
905 receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
906 {
907   struct GNUNET_NETWORK_SocketHandle *sh = cls;
908   struct GNUNET_TIME_Absolute now;
909
910   sh->read_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
911   if ((sh->sock == -1) &&
912       (sh->connect_task == GNUNET_SCHEDULER_NO_PREREQUISITE_TASK))
913     {
914       /* not connected and no longer trying */
915 #if DEBUG_NETWORK
916       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
917                   "Receive encounters error, socket closed...\n");
918 #endif
919       signal_error (sh, ECONNREFUSED);
920       return;
921     }
922   now = GNUNET_TIME_absolute_get ();
923   if ((now.value > sh->receive_timeout.value) ||
924       (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
925     {
926 #if DEBUG_NETWORK
927       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
928                   "Receive encounters error: time out...\n");
929 #endif
930       signal_timeout (sh);
931       return;
932     }
933   if (sh->connect_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
934     {
935       /* connect was retried */
936       sh->read_task = GNUNET_SCHEDULER_add_after (tc->sched,
937                                                   GNUNET_YES,
938                                                   GNUNET_SCHEDULER_PRIORITY_KEEP,
939                                                   sh->connect_task,
940                                                   &receive_again, sh);
941       return;
942     }
943   /* connect succeeded, wait for data! */
944   sh->read_task = GNUNET_SCHEDULER_add_read (tc->sched,
945                                              GNUNET_YES,
946                                              GNUNET_SCHEDULER_PRIORITY_KEEP,
947                                              sh->connect_task,
948                                              GNUNET_TIME_absolute_get_remaining
949                                              (sh->receive_timeout),
950                                              sh->sock, &receive_ready,
951                                              sh);
952 }
953
954
955 /**
956  * Receive data from the given socket.  Note that this function will
957  * call "receiver" asynchronously using the scheduler.  It will
958  * "immediately" return.  Note that there MUST only be one active
959  * receive call per socket at any given point in time (so do not
960  * call receive again until the receiver callback has been invoked).
961  *
962  * @param sched scheduler to use
963  * @param sock socket handle
964  * @param max maximum number of bytes to read
965  * @param timeout maximum amount of time to wait (use -1 for "forever")
966  * @param receiver function to call with received data
967  * @param receiver_cls closure for receiver
968  * @return scheduler task ID used for receiving, GNUNET_SCHEDULER_NO_PREREQUISITE_TASK on error
969  */
970 GNUNET_SCHEDULER_TaskIdentifier
971 GNUNET_NETWORK_receive (struct GNUNET_NETWORK_SocketHandle *sock,
972                         size_t max,
973                         struct GNUNET_TIME_Relative timeout,
974                         GNUNET_NETWORK_Receiver receiver, void *receiver_cls)
975 {
976   struct GNUNET_SCHEDULER_TaskContext tc;
977
978   GNUNET_assert ((sock->read_task == GNUNET_SCHEDULER_NO_PREREQUISITE_TASK) &&
979                  (sock->receiver == NULL));
980   sock->receiver = receiver;
981   sock->receiver_cls = receiver_cls;
982   sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
983   sock->max = max;
984   memset (&tc, 0, sizeof (tc));
985   tc.sched = sock->sched;
986   tc.reason = GNUNET_SCHEDULER_REASON_PREREQ_DONE;
987   receive_again (sock, &tc);
988   return sock->read_task;
989 }
990
991
992 /**
993  * Cancel receive job on the given socket.  Note that the
994  * receiver callback must not have been called yet in order
995  * for the cancellation to be valid.
996  *
997  * @param sock socket handle
998  * @param task task identifier returned from the receive call
999  * @return closure of the original receiver callback
1000  */
1001 void *
1002 GNUNET_NETWORK_receive_cancel (struct GNUNET_NETWORK_SocketHandle *sock,
1003                                GNUNET_SCHEDULER_TaskIdentifier task)
1004 {
1005   GNUNET_assert (sock->read_task == task);
1006   GNUNET_assert (sock == GNUNET_SCHEDULER_cancel (sock->sched, task));
1007   sock->read_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1008   sock->receiver = NULL;
1009   return sock->receiver_cls;
1010 }
1011
1012
1013 /**
1014  * Try to call the transmit notify method (check if we do
1015  * have enough space available first)!
1016  *
1017  * @param sock socket for which we should do this processing
1018  * @return GNUNET_YES if we were able to call notify
1019  */
1020 static int
1021 process_notify (struct GNUNET_NETWORK_SocketHandle *sock)
1022 {
1023   size_t used;
1024   size_t avail;
1025   size_t size;
1026   GNUNET_NETWORK_TransmitReadyNotify notify;
1027
1028   GNUNET_assert (sock->write_task == GNUNET_SCHEDULER_NO_PREREQUISITE_TASK);
1029   if (NULL == (notify = sock->nth.notify_ready))
1030     return GNUNET_NO;
1031   used = sock->write_buffer_off - sock->write_buffer_pos;
1032   avail = sock->write_buffer_size - used;
1033   size = sock->nth.notify_size;
1034   if (sock->nth.notify_size > avail)
1035     return GNUNET_NO;
1036   sock->nth.notify_ready = NULL;
1037   if (sock->nth.timeout_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
1038     {
1039       GNUNET_SCHEDULER_cancel (sock->sched, sock->nth.timeout_task);
1040       sock->nth.timeout_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1041     }
1042   if (sock->write_buffer_size - sock->write_buffer_off < size)
1043     {
1044       /* need to compact */
1045       memmove (sock->write_buffer,
1046                &sock->write_buffer[sock->write_buffer_pos], used);
1047       sock->write_buffer_off -= sock->write_buffer_pos;
1048       sock->write_buffer_pos = 0;
1049     }
1050   GNUNET_assert (sock->write_buffer_size - sock->write_buffer_off >= size);
1051   size = notify (sock->nth.notify_ready_cls,
1052                  sock->write_buffer_size - sock->write_buffer_off,
1053                  &sock->write_buffer[sock->write_buffer_off]);
1054   sock->write_buffer_off += size;
1055   return GNUNET_YES;
1056 }
1057
1058
1059 /**
1060  * Task invoked by the scheduler when a call to transmit
1061  * is timing out (we never got enough buffer space to call
1062  * the callback function before the specified timeout
1063  * expired).
1064  *
1065  * This task notifies the client about the timeout.
1066  */
1067 static void
1068 transmit_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1069 {
1070   struct GNUNET_NETWORK_SocketHandle *sock = cls;
1071   GNUNET_NETWORK_TransmitReadyNotify notify;
1072
1073 #if DEBUG_NETWORK
1074   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmit fails, time out reached.\n");
1075 #endif
1076   notify = sock->nth.notify_ready;
1077   sock->nth.notify_ready = NULL;
1078   notify (sock->nth.notify_ready_cls, 0, NULL);
1079 }
1080
1081
1082 static void
1083 transmit_error (struct GNUNET_NETWORK_SocketHandle *sock)
1084 {
1085   if (sock->nth.notify_ready == NULL)
1086     return;                     /* nobody to tell about it */
1087   if (sock->nth.timeout_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
1088     {
1089       GNUNET_SCHEDULER_cancel (sock->sched, sock->nth.timeout_task);
1090       sock->nth.timeout_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1091     }
1092   transmit_timeout (sock, NULL);
1093 }
1094
1095
1096 /**
1097  * See if we are now connected.  If not, wait longer for
1098  * connect to succeed.  If connected, we should be able
1099  * to write now as well, unless we timed out.
1100  */
1101 static void
1102 transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1103 {
1104   struct GNUNET_NETWORK_SocketHandle *sock = cls;
1105   ssize_t ret;
1106   size_t have;
1107
1108   GNUNET_assert (sock->write_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK);
1109   sock->write_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1110   if (sock->connect_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
1111     {
1112       /* still waiting for connect */
1113       GNUNET_assert (sock->write_task ==
1114                      GNUNET_SCHEDULER_NO_PREREQUISITE_TASK);
1115       sock->write_task =
1116         GNUNET_SCHEDULER_add_delayed (tc->sched, GNUNET_NO,
1117                                       GNUNET_SCHEDULER_PRIORITY_KEEP,
1118                                       sock->connect_task,
1119                                       GNUNET_TIME_UNIT_ZERO, &transmit_ready,
1120                                       sock);
1121       return;
1122     }
1123   if (sock->sock == -1)
1124     {
1125 #if DEBUG_NETWORK
1126       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1127                   _("Could not satisfy pending transmission request, socket closed or connect failed.\n"));
1128 #endif
1129       transmit_error (sock);
1130       return;                   /* connect failed for good, we're finished */
1131     }
1132   if ((tc->write_ready == NULL) || (!FD_ISSET (sock->sock, tc->write_ready)))
1133     {
1134       /* special circumstances: not yet ready to write */
1135       goto SCHEDULE_WRITE;
1136     }
1137   GNUNET_assert (sock->write_buffer_off >= sock->write_buffer_pos);
1138   process_notify (sock);
1139   have = sock->write_buffer_off - sock->write_buffer_pos;
1140   if (have == 0)
1141     {
1142       /* no data ready for writing, terminate write loop */
1143       return;
1144     }
1145 RETRY:
1146   ret = SEND (sock->sock,
1147               &sock->write_buffer[sock->write_buffer_pos],
1148               have,
1149 #ifndef MINGW
1150               // FIXME NILS
1151               MSG_DONTWAIT | MSG_NOSIGNAL
1152 #else
1153               0
1154 #endif
1155   );
1156   if (ret == -1)
1157     {
1158       if (errno == EINTR)
1159         goto RETRY;
1160 #if DEBUG_NETWORK
1161       GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "send");
1162 #endif
1163       SHUTDOWN (sock->sock, SHUT_RDWR);
1164       GNUNET_break (0 == CLOSE (sock->sock));
1165       sock->sock = -1;
1166       transmit_error (sock);
1167       return;
1168     }
1169 #if DEBUG_NETWORK
1170   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1171               "transmit_ready transmitted %u/%u bytes to `%s'\n",
1172               (unsigned int) ret,
1173               have,
1174               GNUNET_a2s(sock->addr, sock->addrlen));
1175 #endif
1176   sock->write_buffer_pos += ret;
1177   if (sock->write_buffer_pos == sock->write_buffer_off)
1178     {
1179       /* transmitted all pending data */
1180       sock->write_buffer_pos = 0;
1181       sock->write_buffer_off = 0;
1182     }
1183   if ((sock->write_buffer_off == 0) && (NULL == sock->nth.notify_ready))
1184     return;                     /* all data sent! */
1185   /* not done writing, schedule more */
1186 SCHEDULE_WRITE:
1187   if (sock->write_task == GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
1188     sock->write_task =
1189       GNUNET_SCHEDULER_add_write (tc->sched,
1190                                   GNUNET_NO,
1191                                   GNUNET_SCHEDULER_PRIORITY_KEEP,
1192                                   GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
1193                                   GNUNET_TIME_UNIT_FOREVER_REL,
1194                                   sock->sock, &transmit_ready, sock);
1195 }
1196
1197
1198 /**
1199  * Ask the socket to call us once the specified number of bytes
1200  * are free in the transmission buffer.  May call the notify
1201  * method immediately if enough space is available.
1202  *
1203  * @param sock socket
1204  * @param size number of bytes to send
1205  * @param timeout after how long should we give up (and call
1206  *        notify with buf NULL and size 0)?
1207  * @param notify function to call
1208  * @param notify_cls closure for notify
1209  * @return non-NULL if the notify callback was queued,
1210  *         NULL if we are already going to notify someone else (busy)
1211  */
1212 struct GNUNET_NETWORK_TransmitHandle *
1213 GNUNET_NETWORK_notify_transmit_ready (struct GNUNET_NETWORK_SocketHandle
1214                                       *sock, size_t size,
1215                                       struct GNUNET_TIME_Relative timeout,
1216                                       GNUNET_NETWORK_TransmitReadyNotify
1217                                       notify, void *notify_cls)
1218 {
1219   if (sock->nth.notify_ready != NULL)
1220     return NULL;
1221   GNUNET_assert (notify != NULL);
1222   GNUNET_assert (sock->write_buffer_size >= size);
1223
1224   if ((sock->sock == -1) &&
1225       (sock->connect_task == GNUNET_SCHEDULER_NO_PREREQUISITE_TASK))
1226     {
1227 #if DEBUG_NETWORK
1228       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1229                   "Transmission request fails, connection failed.\n");
1230 #endif
1231       notify (notify_cls, 0, NULL);
1232       return &sock->nth;
1233     }
1234   GNUNET_assert (sock->write_buffer_off <= sock->write_buffer_size);
1235   GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_size);
1236   GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_off);
1237   sock->nth.notify_ready = notify;
1238   sock->nth.notify_ready_cls = notify_cls;
1239   sock->nth.sh = sock;
1240   sock->nth.notify_size = size;
1241   sock->nth.transmit_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1242   sock->nth.timeout_task = GNUNET_SCHEDULER_add_delayed (sock->sched,
1243                                                          GNUNET_NO,
1244                                                          GNUNET_SCHEDULER_PRIORITY_KEEP,
1245                                                          GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
1246                                                          timeout,
1247                                                          &transmit_timeout,
1248                                                          sock);
1249   if (sock->write_task == GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
1250     sock->write_task = GNUNET_SCHEDULER_add_delayed (sock->sched,
1251                                                      GNUNET_NO,
1252                                                      GNUNET_SCHEDULER_PRIORITY_KEEP,
1253                                                      sock->connect_task,
1254                                                      GNUNET_TIME_UNIT_ZERO,
1255                                                      &transmit_ready, sock);
1256   return &sock->nth;
1257 }
1258
1259
1260 /**
1261  * Cancel the specified transmission-ready
1262  * notification.
1263  */
1264 void
1265 GNUNET_NETWORK_notify_transmit_ready_cancel (struct
1266                                              GNUNET_NETWORK_TransmitHandle *h)
1267 {
1268   GNUNET_assert (h->notify_ready != NULL);
1269   GNUNET_SCHEDULER_cancel (h->sh->sched, h->timeout_task);
1270   h->timeout_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1271   h->notify_ready = NULL;
1272 }
1273
1274
1275 #if 0                           /* keep Emacsens' auto-indent happy */
1276 {
1277 #endif
1278 #ifdef __cplusplus
1279 }
1280 #endif