aboutsummaryrefslogtreecommitdiff
path: root/src/util/network.c
blob: 7fd6f6c47eb7773beff577d1e6a88b297a1db91d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
/*
     This file is part of GNUnet.
     (C) 2009 Christian Grothoff (and other contributing authors)

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 2, or (at your
     option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/

/**
 * @file util/network/network.c
 * @brief basic, low-level TCP networking interface
 * @author Christian Grothoff
 *
 * This code is rather complex.  Only modify it if you
 * 1) Have a NEW testcase showing that the new code
 *    is needed and correct
 * 2) All EXISTING testcases pass with the new code
 * These rules should apply in general, but for this
 * module they are VERY, VERY important.
 *
 * TODO:
 * - can we merge receive_ready and receive_again?
 * - can we integrate the nth.timeout_task with the write_task's timeout?
 */

#include "platform.h"
#include "gnunet_common.h"
#include "gnunet_network_lib.h"
#include "gnunet_scheduler_lib.h"

#define DEBUG_NETWORK GNUNET_NO

struct GNUNET_NETWORK_TransmitHandle
{

  /**
   * Function to call if the send buffer has notify_size
   * bytes available.
   */
  GNUNET_NETWORK_TransmitReadyNotify notify_ready;

  /**
   * Closure for notify_ready.
   */
  void *notify_ready_cls;

  /**
   * Our socket handle.
   */
  struct GNUNET_NETWORK_SocketHandle *sh;

  /**
   * Timeout for receiving (in absolute time).
   */
  struct GNUNET_TIME_Absolute transmit_timeout;

  /**
   * Task called on timeout.
   */
  GNUNET_SCHEDULER_TaskIdentifier timeout_task;

  /**
   * At what number of bytes available in the
   * write buffer should the notify method be called?
   */
  size_t notify_size;

};

/**
 * @brief handle for a network socket
 */
struct GNUNET_NETWORK_SocketHandle
{

  /**
   * Scheduler that was used for the connect task.
   */
  struct GNUNET_SCHEDULER_Handle *sched;

  /**
   * Address information for connect (may be NULL).
   */
  struct addrinfo *ai;

  /**
   * Index for the next struct addrinfo for connect attempts (may be NULL)
   */
  struct addrinfo *ai_pos;

  /**
   * Network address of the other end-point, may be NULL.
   */
  struct sockaddr *addr;

  /**
   * Pointer to our write buffer.
   */
  char *write_buffer;

  /**
   * Size of our write buffer.
   */
  size_t write_buffer_size;

  /**
   * Current write-offset in write buffer (where
   * would we write next).
   */
  size_t write_buffer_off;

  /**
   * Current read-offset in write buffer (how many
   * bytes have already been send).
   */
  size_t write_buffer_pos;

  /**
   * Length of addr.
   */
  socklen_t addrlen;

  /**
   * Connect task that we may need to wait for.
   */
  GNUNET_SCHEDULER_TaskIdentifier connect_task;

  /**
   * Read task that we may need to wait for.
   */
  GNUNET_SCHEDULER_TaskIdentifier read_task;

  /**
   * Write task that we may need to wait for.
   */
  GNUNET_SCHEDULER_TaskIdentifier write_task;

  /**
   * The handle we return for GNUNET_NETWORK_notify_transmit_ready.
   */
  struct GNUNET_NETWORK_TransmitHandle nth;

  /**
   * Underlying OS's socket, set to -1 after fatal errors.
   */
  int sock;

  /**
   * Port to connect to.
   */
  uint16_t port;

  /**
   * Function to call on data received, NULL
   * if no receive is pending.
   */
  GNUNET_NETWORK_Receiver receiver;

  /**
   * Closure for receiver.
   */
  void *receiver_cls;

  /**
   * Timeout for receiving (in absolute time).
   */
  struct GNUNET_TIME_Absolute receive_timeout;

  /**
   * Maximum number of bytes to read
   * (for receiving).
   */
  size_t max;

};


/**
 * Create a socket handle by boxing an existing OS socket.  The OS
 * socket should henceforth be no longer used directly.
 * GNUNET_socket_destroy will close it.
 *
 * @param sched scheduler to use
 * @param osSocket existing socket to box
 * @param maxbuf maximum write buffer size for the socket (use
 *        0 for sockets that need no write buffers, such as listen sockets)
 * @return the boxed socket handle
 */
struct GNUNET_NETWORK_SocketHandle *
GNUNET_NETWORK_socket_create_from_existing (struct GNUNET_SCHEDULER_Handle
                                            *sched, int osSocket,
                                            size_t maxbuf)
{
  struct GNUNET_NETWORK_SocketHandle *ret;
  ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_SocketHandle) + maxbuf);
  ret->write_buffer = (char *) &ret[1];
  ret->write_buffer_size = maxbuf;
  ret->sock = osSocket;
  ret->sched = sched;
  return ret;
}


/**
 * Create a socket handle by accepting on a listen socket.  This
 * function may block if the listen socket has no connection ready.
 *
 * @param sched scheduler to use
 * @param access function to use to check if access is allowed
 * @param access_cls closure for access
 * @param lsock listen socket
 * @param maxbuf maximum write buffer size for the socket (use
 *        0 for sockets that need no write buffers, such as listen sockets)
 * @return the socket handle, NULL on error
 */
struct GNUNET_NETWORK_SocketHandle *
GNUNET_NETWORK_socket_create_from_accept (struct GNUNET_SCHEDULER_Handle
                                          *sched,
                                          GNUNET_NETWORK_AccessCheck access,
                                          void *access_cls, int lsock,
                                          size_t maxbuf)
{
  struct GNUNET_NETWORK_SocketHandle *ret;
  char addr[32];
  char msg[INET6_ADDRSTRLEN];
  socklen_t addrlen;
  int fam;
  int fd;
  int aret;
  struct sockaddr_in *v4;
  struct sockaddr_in6 *v6;
  struct sockaddr *sa;
  void *uaddr;

  addrlen = sizeof (addr);
  fd = accept (lsock, (struct sockaddr *) &addr, &addrlen);
  if (fd == -1)
    {
      GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "accept");
      return NULL;
    }
  if (0 != fcntl (fd, F_SETFD, fcntl (fd, F_GETFD) | FD_CLOEXEC))
    GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
                         "fcntl");
  if (addrlen > sizeof (addr))
    {
      GNUNET_break (0);
      GNUNET_break (0 == CLOSE (fd));
      return NULL;
    }

  sa = (struct sockaddr *) addr;
  v6 = (struct sockaddr_in6 *) addr;
  if ((sa->sa_family == AF_INET6) && (IN6_IS_ADDR_V4MAPPED (&v6->sin6_addr)))
    {
      /* convert to V4 address */
      v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
      memset (v4, 0, sizeof (struct sockaddr_in));
      v4->sin_family = AF_INET;
      memcpy (&v4->sin_addr,
              &((char *) &v6->sin6_addr)[sizeof (struct in6_addr) -
                                         sizeof (struct in_addr)],
              sizeof (struct in_addr));
      v4->sin_port = v6->sin6_port;
      uaddr = v4;
      addrlen = sizeof (struct sockaddr_in);
    }
  else
    {
      uaddr = GNUNET_malloc (addrlen);
      memcpy (uaddr, addr, addrlen);
    }

  if ((access != NULL) &&
      (GNUNET_YES != (aret = access (access_cls, uaddr, addrlen))))
    {
      if (aret == GNUNET_NO)
        {
          fam = ((struct sockaddr *) addr)->sa_family;
          GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                      _("Access denied to `%s'\n"),
                      inet_ntop (fam,
                                 (fam == AF_INET6)
                                 ? (const void *) &((struct sockaddr_in6 *)
                                                    &addr)->sin6_addr : (const
                                                                         void
                                                                         *)
                                 &((struct sockaddr_in *) &addr)->sin_addr,
                                 msg, sizeof (msg)));
        }
      GNUNET_break (0 == SHUTDOWN (fd, SHUT_RDWR));
      GNUNET_break (0 == CLOSE (fd));
      GNUNET_free (uaddr);
      return NULL;
    }
  ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_SocketHandle) + maxbuf);
  ret->write_buffer = (char *) &ret[1];
  ret->write_buffer_size = maxbuf;
  ret->addr = uaddr;
  ret->addrlen = addrlen;
  ret->sock = fd;
  ret->sched = sched;
  return ret;
}

/**
 * Obtain the network address of the other party.
 *
 * @param sock the client to get the address for
 * @param addr where to store the address
 * @param addrlen where to store the length of the address
 * @return GNUNET_OK on success
 */
int
GNUNET_NETWORK_socket_get_address (struct GNUNET_NETWORK_SocketHandle *sock,
                                   void **addr, size_t * addrlen)
{
  if ((sock->addr == NULL) || (sock->addrlen == 0))
    return GNUNET_NO;
  *addr = GNUNET_malloc (sock->addrlen);
  memcpy (*addr, sock->addr, sock->addrlen);
  *addrlen = sock->addrlen;
  return GNUNET_OK;
}


/**
 * Set if a socket should use blocking or non-blocking IO.
 *
 * @return GNUNET_OK on success, GNUNET_SYSERR on error
 */
static int
socket_set_blocking (int handle, int doBlock)
{
#if MINGW
  u_long mode;
  mode = !doBlock;
#if HAVE_PLIBC_FD
  if (ioctlsocket (plibc_fd_get_handle (handle), FIONBIO, &mode) ==
      SOCKET_ERROR)
    {
      SetErrnoFromWinsockError (WSAGetLastError ());
      GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctlsocket");
      return GNUNET_SYSERR;
    }
#else
  if (ioctlsocket (handle, FIONBIO, &mode) == SOCKET_ERROR)
    {
      SetErrnoFromWinsockError (WSAGetLastError ());
      GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctlsocket");
      return GNUNET_SYSERR;
    }
#endif
  /* store the blocking mode */
#if HAVE_PLIBC_FD
  plibc_fd_set_blocking (handle, doBlock);
#else
  __win_SetHandleBlockingMode (handle, doBlock);
#endif
  return GNUNET_OK;

#else
  /* not MINGW */
  int flags = fcntl (handle, F_GETFL);
  if (flags == -1)
    {
      GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl");
      return GNUNET_SYSERR;
    }
  if (doBlock)
    flags &= ~O_NONBLOCK;
  else
    flags |= O_NONBLOCK;
  if (0 != fcntl (handle, F_SETFL, flags))
    {
      GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl");
      return GNUNET_SYSERR;
    }
  return GNUNET_OK;
#endif
}


/**
 * Initiate asynchronous TCP connect request.
 *
 * @param sock what socket to connect
 * @return GNUNET_SYSERR error (no more addresses to try)
 */
static int
try_connect (struct GNUNET_NETWORK_SocketHandle *sock)
{
  int s;

  if (sock->addr != NULL)
    {
      GNUNET_free (sock->addr);
      sock->addr = NULL;
      sock->addrlen = 0;
    }
  while (1)
    {
      if (sock->ai_pos == NULL)
        {
          /* no more addresses to try, fatal! */
          return GNUNET_SYSERR;
        }
      switch (sock->ai_pos->ai_family)
        {
        case AF_INET:
          ((struct sockaddr_in *) sock->ai_pos->ai_addr)->sin_port =
            htons (sock->port);
          break;
        case AF_INET6:
          ((struct sockaddr_in6 *) sock->ai_pos->ai_addr)->sin6_port =
            htons (sock->port);
          break;
        default:
          sock->ai_pos = sock->ai_pos->ai_next;
          continue;
        }
      s = SOCKET (sock->ai_pos->ai_family, SOCK_STREAM, 0);
      if (s == -1)
        {
          /* maybe unsupported address family, try next */
          GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "socket");
          sock->ai_pos = sock->ai_pos->ai_next;
          continue;
        }
      if (0 != fcntl (s, F_SETFD, fcntl (s, F_GETFD) | FD_CLOEXEC))
        GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
                             "fcntl");
      if (GNUNET_SYSERR == socket_set_blocking (s, GNUNET_NO))
        {
          /* we'll treat this one as fatal */
          GNUNET_break (0 == CLOSE (s));
          return GNUNET_SYSERR;
        }
      if ((0 != CONNECT (s,
                         sock->ai_pos->ai_addr,
                         sock->ai_pos->ai_addrlen)) && (errno != EINPROGRESS))
        {
          /* maybe refused / unsupported address, try next */
          GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "connect");
          GNUNET_break (0 == CLOSE (s));
          continue;
        }
      break;
    }
  /* got one! copy address information! */
  sock->addrlen = sock->ai_pos->ai_addrlen;
  sock->addr = GNUNET_malloc (sock->addrlen);
  memcpy (sock->addr, sock->ai_pos->ai_addr, sock->addrlen);
  sock->ai_pos = sock->ai_pos->ai_next;
  sock->sock = s;
  return GNUNET_OK;
}


/**
 * Scheduler let us know that we're either ready to
 * write on the socket OR connect timed out.  Do the
 * right thing.
 */
static void
connect_continuation (void *cls,
                      const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  struct GNUNET_NETWORK_SocketHandle *sock = cls;
  unsigned int len;
  int error;

  /* nobody needs to wait for us anymore... */
  sock->connect_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
  /* Note: write-ready does NOT mean connect succeeded,
     we need to use getsockopt to be sure */
  len = sizeof (error);
  errno = 0;
  error = 0;
  if ((0 == (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) ||
      (0 != getsockopt (sock->sock, SOL_SOCKET, SO_ERROR, &error, &len)) ||
      (error != 0) || (errno != 0))
    {
      /* connect failed / timed out */
      GNUNET_break (0 == CLOSE (sock->sock));
      sock->sock = -1;
      if (GNUNET_SYSERR == try_connect (sock))
        {
          /* failed for good */
          GNUNET_break (sock->ai_pos == NULL);
          freeaddrinfo (sock->ai);
          sock->ai = NULL;
          return;
        }
      sock->connect_task = GNUNET_SCHEDULER_add_write (tc->sched, GNUNET_NO,    /* abort on shutdown */
                                                       GNUNET_SCHEDULER_PRIORITY_KEEP,
                                                       GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
                                                       GNUNET_NETWORK_CONNECT_RETRY_TIMEOUT,
                                                       sock->sock,
                                                       &connect_continuation,
                                                       sock);
      return;
    }
  /* connect succeeded! clean up "ai" */
#if DEBUG_NETWORK
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connection succeeded!\n");
#endif
  freeaddrinfo (sock->ai);
  sock->ai_pos = NULL;
  sock->ai = NULL;
}


/**
 * Create a socket handle by (asynchronously) connecting to a host.
 * This function returns immediately, even if the connection has not
 * yet been established.  This function only creates TCP connections.
 *
 * @param sched scheduler to use
 * @param hostname name of the host to connect to
 * @param port port to connect to
 * @param maxbuf maximum write buffer size for the socket (use
 *        0 for sockets that need no write buffers, such as listen sockets)
 * @return the socket handle
 */
struct GNUNET_NETWORK_SocketHandle *
GNUNET_NETWORK_socket_create_from_connect (struct GNUNET_SCHEDULER_Handle
                                           *sched, const char *hostname,
                                           uint16_t port, size_t maxbuf)
{
  struct GNUNET_NETWORK_SocketHandle *ret;
  struct addrinfo hints;
  int ec;

  ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_SocketHandle) + maxbuf);
  ret->sock = -1;
  ret->sched = sched;
  ret->write_buffer = (char *) &ret[1];
  ret->write_buffer_size = maxbuf;
  ret->port = port;
  memset (&hints, 0, sizeof (hints));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  if (0 != (ec = getaddrinfo (hostname, NULL, &hints, &ret->ai)))
    {
      GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
                  "`%s' failed for hostname `%s': %s\n",
                  "getaddrinfo", hostname, gai_strerror (ec));
      GNUNET_free (ret);
      return NULL;
    }
  ret->ai_pos = ret->ai;
  if (GNUNET_SYSERR == try_connect (ret))
    {
      freeaddrinfo (ret->ai);
      GNUNET_free (ret);
      return NULL;
    }
  ret->connect_task = GNUNET_SCHEDULER_add_write (sched, GNUNET_NO,     /* abort on shutdown */
                                                  GNUNET_SCHEDULER_PRIORITY_KEEP,
                                                  GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
                                                  GNUNET_NETWORK_CONNECT_RETRY_TIMEOUT,
                                                  ret->sock,
                                                  &connect_continuation, ret);
  return ret;

}


/**
 * Create a socket handle by (asynchronously) connecting to a host.
 * This function returns immediately, even if the connection has not
 * yet been established.  This function only creates TCP connections.
 *
 * @param sched scheduler to use
 * @param af_family address family to use
 * @param serv_addr server address
 * @param addrlen length of server address
 * @param maxbuf maximum write buffer size for the socket (use
 *        0 for sockets that need no write buffers, such as listen sockets)
 * @return the socket handle
 */
struct GNUNET_NETWORK_SocketHandle *
GNUNET_NETWORK_socket_create_from_sockaddr (struct GNUNET_SCHEDULER_Handle
                                            *sched, int af_family,
                                            const struct sockaddr *serv_addr,
                                            socklen_t addrlen, size_t maxbuf)
{
  int s;

  s = SOCKET (af_family, SOCK_STREAM, 0);
  if (s == -1)
    {
      GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING |
                           GNUNET_ERROR_TYPE_BULK, "socket");
      return NULL;
    }
  if (0 != fcntl (s, F_SETFD, fcntl (s, F_GETFD) | FD_CLOEXEC))
    GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
                         "fcntl");
  if (GNUNET_SYSERR == socket_set_blocking (s, GNUNET_NO))
    {
      /* we'll treat this one as fatal */
      GNUNET_break (0 == CLOSE (s));
      return NULL;
    }
  if ((0 != CONNECT (s, serv_addr, addrlen)) && (errno != EINPROGRESS))
    {
      /* maybe refused / unsupported address, try next */
      GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "connect");
      GNUNET_break (0 == CLOSE (s));
      return NULL;
    }
  return GNUNET_NETWORK_socket_create_from_existing (sched, s, maxbuf);
}


/**
 * Check if socket is valid (no fatal errors have happened so far).
 * Note that a socket that is still trying to connect is considered
 * valid.
 *
 * @param sock socket to check
 * @return GNUNET_YES if valid, GNUNET_NO otherwise
 */
int
GNUNET_NETWORK_socket_check (struct GNUNET_NETWORK_SocketHandle *sock)
{
  if (sock->ai != NULL)
    return GNUNET_YES;          /* still trying to connect */
  return (sock->sock == -1) ? GNUNET_NO : GNUNET_YES;
}


/**
 * Scheduler let us know that the connect task is finished (or was
 * cancelled due to shutdown).  Now really clean up.
 */
static void
destroy_continuation (void *cls,
                      const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  struct GNUNET_NETWORK_SocketHandle *sock = cls;
  GNUNET_NETWORK_TransmitReadyNotify notify;

  if (sock->write_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
    {
#if DEBUG_NETWORK
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "Destroy code waiting for writes to complete.\n");
#endif
      GNUNET_SCHEDULER_add_after (sock->sched,
                                  GNUNET_YES,
                                  GNUNET_SCHEDULER_PRIORITY_KEEP,
                                  sock->write_task,
                                  &destroy_continuation, sock);
      return;
    }
  if (sock->sock != -1)
    {
#if DEBUG_NETWORK
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down socket.\n");
#endif
      SHUTDOWN (sock->sock, SHUT_RDWR);
    }
  if (sock->read_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
    {
#if DEBUG_NETWORK
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "Destroy code waiting for receive to complete.\n");
#endif
      GNUNET_SCHEDULER_add_after (sock->sched,
                                  GNUNET_YES,
                                  GNUNET_SCHEDULER_PRIORITY_KEEP,
                                  sock->read_task,
                                  &destroy_continuation, sock);
      return;
    }
  if (NULL != (notify = sock->nth.notify_ready))
    {
      sock->nth.notify_ready = NULL;
      notify (sock->nth.notify_ready_cls, 0, NULL);
      if (sock->nth.timeout_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
        {
          GNUNET_SCHEDULER_cancel (sock->sched, sock->nth.timeout_task);
          sock->nth.timeout_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
        }
    }
  if (sock->sock != -1)
    {
#if DEBUG_NETWORK
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Closing socket.\n");
#endif
      GNUNET_break (0 == CLOSE (sock->sock));
    }
  GNUNET_free_non_null (sock->addr);
  if (sock->ai != NULL)
    freeaddrinfo (sock->ai);
  GNUNET_free (sock);
}


/**
 * Close the socket and free associated resources. Pending
 * transmissions are simply dropped.  A pending receive call will be
 * called with an error code of "EPIPE".
 *
 * @param sock socket to destroy
 */
void
GNUNET_NETWORK_socket_destroy (struct GNUNET_NETWORK_SocketHandle *sock)
{
#if DEBUG_NETWORK
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Network asked to destroy socket %p\n", sock);
#endif
  if (sock->write_buffer_off == 0)
    sock->ai_pos = NULL;        /* if we're still trying to connect and have
                                   no message pending, stop trying! */
  GNUNET_assert (sock->sched != NULL);
  GNUNET_SCHEDULER_add_after (sock->sched,
                              GNUNET_YES,
                              GNUNET_SCHEDULER_PRIORITY_KEEP,
                              sock->connect_task,
                              &destroy_continuation, sock);
}

/**
 * Tell the receiver callback that a timeout was reached.
 */
static void
signal_timeout (struct GNUNET_NETWORK_SocketHandle *sh)
{
  GNUNET_NETWORK_Receiver receiver;

#if DEBUG_NETWORK
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Network signals timeout to receiver!\n");
#endif
  GNUNET_assert (NULL != (receiver = sh->receiver));
  sh->receiver = NULL;
  receiver (sh->receiver_cls, NULL, 0, NULL, 0, 0);
}


/**
 * Tell the receiver callback that we had an IO error.
 */
static void
signal_error (struct GNUNET_NETWORK_SocketHandle *sh, int errcode)
{
  GNUNET_NETWORK_Receiver receiver;
  GNUNET_assert (NULL != (receiver = sh->receiver));
  sh->receiver = NULL;
  receiver (sh->receiver_cls, NULL, 0, sh->addr, sh->addrlen, errcode);
}


/**
 * This function is called once we either timeout
 * or have data ready to read.
 */
static void
receive_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  struct GNUNET_NETWORK_SocketHandle *sh = cls;
  struct GNUNET_TIME_Absolute now;
  char buffer[sh->max];
  ssize_t ret;
  GNUNET_NETWORK_Receiver receiver;

  sh->read_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
  now = GNUNET_TIME_absolute_get ();
  if ((now.value > sh->receive_timeout.value) ||
      (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) ||
      (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
    {
#if DEBUG_NETWORK
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "Receive encounters error: timeout...\n");
#endif
      signal_timeout (sh);
      return;
    }
  if (sh->sock == -1)
    {
      /* connect failed for good */
#if DEBUG_NETWORK
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "Receive encounters error, socket closed...\n");
#endif
      signal_error (sh, ECONNREFUSED);
      return;
    }
  GNUNET_assert (FD_ISSET (sh->sock, tc->read_ready));
RETRY:
  ret = RECV (sh->sock, buffer, sh->max, MSG_DONTWAIT);
  if (ret == -1)
    {
      if (errno == EINTR)
        goto RETRY;
#if DEBUG_NETWORK
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "Error receiving: %s\n", STRERROR (errno));
#endif
      signal_error (sh, errno);
      return;
    }
#if DEBUG_NETWORK
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Receive got %d bytes from OS!\n", ret);
#endif
  GNUNET_assert (NULL != (receiver = sh->receiver));
  sh->receiver = NULL;
  receiver (sh->receiver_cls, buffer, ret, sh->addr, sh->addrlen, 0);
}


/**
 * This function is called after establishing a connection either has
 * succeeded or timed out.  Note that it is possible that the attempt
 * timed out and that we're immediately retrying.  If we are retrying,
 * we need to wait again (or timeout); if we succeeded, we need to
 * wait for data (or timeout).
 */
static void
receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  struct GNUNET_NETWORK_SocketHandle *sh = cls;
  struct GNUNET_TIME_Absolute now;

  sh->read_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
  if ((sh->sock == -1) &&
      (sh->connect_task == GNUNET_SCHEDULER_NO_PREREQUISITE_TASK))
    {
      /* not connected and no longer trying */
#if DEBUG_NETWORK
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "Receive encounters error, socket closed...\n");
#endif
      signal_error (sh, ECONNREFUSED);
      return;
    }
  now = GNUNET_TIME_absolute_get ();
  if ((now.value > sh->receive_timeout.value) ||
      (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
    {
#if DEBUG_NETWORK
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "Receive encounters error: timeout...\n");
#endif
      signal_timeout (sh);
      return;
    }
  if (sh->connect_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
    {
      /* connect was retried */
#if DEBUG_NETWORK
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "Receive still waits on connect...\n");
#endif
      sh->read_task = GNUNET_SCHEDULER_add_after (tc->sched,
                                                  GNUNET_YES,
                                                  GNUNET_SCHEDULER_PRIORITY_KEEP,
                                                  sh->connect_task,
                                                  &receive_again, sh);
    }
  else
    {
      /* connect succeeded, wait for data! */
#if DEBUG_NETWORK
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "Receive now waits for socket...\n");
#endif
      sh->read_task = GNUNET_SCHEDULER_add_read (tc->sched,
                                                 GNUNET_YES,
                                                 GNUNET_SCHEDULER_PRIORITY_KEEP,
                                                 sh->connect_task,
                                                 GNUNET_TIME_absolute_get_remaining
                                                 (sh->receive_timeout),
                                                 sh->sock, &receive_ready,
                                                 sh);
    }
}


/**
 * Receive data from the given socket.  Note that this function will
 * call "receiver" asynchronously using the scheduler.  It will
 * "immediately" return.  Note that there MUST only be one active
 * receive call per socket at any given point in time (so do not
 * call receive again until the receiver callback has been invoked).
 *
 * @param sched scheduler to use
 * @param sock socket handle
 * @param max maximum number of bytes to read
 * @param timeout maximum amount of time to wait (use -1 for "forever")
 * @param receiver function to call with received data
 * @param receiver_cls closure for receiver
 * @return scheduler task ID used for receiving, GNUNET_SCHEDULER_NO_PREREQUISITE_TASK on error
 */
GNUNET_SCHEDULER_TaskIdentifier
GNUNET_NETWORK_receive (struct GNUNET_NETWORK_SocketHandle *sock,
                        size_t max,
                        struct GNUNET_TIME_Relative timeout,
                        GNUNET_NETWORK_Receiver receiver, void *receiver_cls)
{
  struct GNUNET_SCHEDULER_TaskContext tc;
#if DEBUG_NETWORK
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Network asked to receive from socket...\n");
#endif
  GNUNET_assert ((sock->read_task == GNUNET_SCHEDULER_NO_PREREQUISITE_TASK) &&
                 (sock->receiver == NULL));
  sock->receiver = receiver;
  sock->receiver_cls = receiver_cls;
  sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
  sock->max = max;
  memset (&tc, 0, sizeof (tc));
  tc.sched = sock->sched;
  tc.reason = GNUNET_SCHEDULER_REASON_PREREQ_DONE;
  receive_again (sock, &tc);
  return sock->read_task;
}


/**
 * Cancel receive job on the given socket.  Note that the
 * receiver callback must not have been called yet in order
 * for the cancellation to be valid.
 *
 * @param sock socket handle
 * @param task task identifier returned from the receive call
 * @return closure of the original receiver callback
 */
void *
GNUNET_NETWORK_receive_cancel (struct GNUNET_NETWORK_SocketHandle *sock,
                               GNUNET_SCHEDULER_TaskIdentifier task)
{
  GNUNET_assert (sock->read_task == task);
  GNUNET_assert (sock == GNUNET_SCHEDULER_cancel (sock->sched, task));
  sock->read_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
  sock->receiver = NULL;
  return sock->receiver_cls;
}


/**
 * Try to call the transmit notify method (check if we do
 * have enough space available first)!
 *
 * @param sock socket for which we should do this processing
 * @return GNUNET_YES if we were able to call notify
 */
static int
process_notify (struct GNUNET_NETWORK_SocketHandle *sock)
{
  size_t used;
  size_t avail;
  size_t size;
  GNUNET_NETWORK_TransmitReadyNotify notify;

  GNUNET_assert (sock->write_task == GNUNET_SCHEDULER_NO_PREREQUISITE_TASK);
  if (NULL == (notify = sock->nth.notify_ready))
    return GNUNET_NO;
  used = sock->write_buffer_off - sock->write_buffer_pos;
  avail = sock->write_buffer_size - used;
  size = sock->nth.notify_size;
  if (sock->nth.notify_size > avail)
    return GNUNET_NO;
  sock->nth.notify_ready = NULL;
  if (sock->nth.timeout_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
    {
      GNUNET_SCHEDULER_cancel (sock->sched, sock->nth.timeout_task);
      sock->nth.timeout_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
    }
  if (sock->write_buffer_size - sock->write_buffer_off < size)
    {
      /* need to compact */
      memmove (sock->write_buffer,
               &sock->write_buffer[sock->write_buffer_pos], used);
      sock->write_buffer_off -= sock->write_buffer_pos;
      sock->write_buffer_pos = 0;
    }
  GNUNET_assert (sock->write_buffer_size - sock->write_buffer_off >= size);
  size = notify (sock->nth.notify_ready_cls,
                 sock->write_buffer_size - sock->write_buffer_off,
                 &sock->write_buffer[sock->write_buffer_off]);
  sock->write_buffer_off += size;
  return GNUNET_YES;
}


/**
 * Task invoked by the scheduler when a call to transmit
 * is timing out (we never got enough buffer space to call
 * the callback function before the specified timeout
 * expired).
 *
 * This task notifies the client about the timeout.
 */
static void
transmit_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  struct GNUNET_NETWORK_SocketHandle *sock = cls;
  GNUNET_NETWORK_TransmitReadyNotify notify;

#if DEBUG_NETWORK
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmit fails, time out reached.\n");
#endif
  notify = sock->nth.notify_ready;
  sock->nth.notify_ready = NULL;
  notify (sock->nth.notify_ready_cls, 0, NULL);
}


static void
transmit_error (struct GNUNET_NETWORK_SocketHandle *sock)
{
  if (sock->nth.notify_ready == NULL)
    return;                     /* nobody to tell about it */
  if (sock->nth.timeout_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
    {
      GNUNET_SCHEDULER_cancel (sock->sched, sock->nth.timeout_task);
      sock->nth.timeout_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
    }
  transmit_timeout (sock, NULL);
}


/**
 * See if we are now connected.  If not, wait longer for
 * connect to succeed.  If connected, we should be able
 * to write now as well, unless we timed out.
 */
static void
transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  struct GNUNET_NETWORK_SocketHandle *sock = cls;
  ssize_t ret;
  size_t have;

#if DEBUG_NETWORK
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Transmit ready called --- will try to send\n");
#endif
  GNUNET_assert (sock->write_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK);
  sock->write_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
  if (sock->connect_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
    {
      /* still waiting for connect */
#if DEBUG_NETWORK
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "Transmission still waiting for connect...\n");
#endif
      GNUNET_assert (sock->write_task ==
                     GNUNET_SCHEDULER_NO_PREREQUISITE_TASK);
      sock->write_task =
        GNUNET_SCHEDULER_add_delayed (tc->sched, GNUNET_NO,
                                      GNUNET_SCHEDULER_PRIORITY_KEEP,
                                      sock->connect_task,
                                      GNUNET_TIME_UNIT_ZERO, &transmit_ready,
                                      sock);
      return;
    }
  if (sock->sock == -1)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                  _
                  ("Did not transmit request, socket closed or connect failed.\n"));
      transmit_error (sock);
      return;                   /* connect failed for good, we're finished */
    }
  if ((tc->write_ready == NULL) || (!FD_ISSET (sock->sock, tc->write_ready)))
    {
#if DEBUG_NETWORK
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "Socket not yet ready for writing, will wait for that.\n");
#endif
      goto SCHEDULE_WRITE;
    }
  GNUNET_assert (sock->write_buffer_off >= sock->write_buffer_pos);
  process_notify (sock);
  have = sock->write_buffer_off - sock->write_buffer_pos;
  if (have == 0)
    {
#if DEBUG_NETWORK
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No data ready for writing.\n");
#endif
      return;
    }
RETRY:
  ret = SEND (sock->sock,
              &sock->write_buffer[sock->write_buffer_pos],
              have, MSG_DONTWAIT | MSG_NOSIGNAL);
  if (ret == -1)
    {
      if (errno == EINTR)
        goto RETRY;
#if DEBUG_NETWORK
      GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "send");
#endif
      SHUTDOWN (sock->sock, SHUT_RDWR);
      GNUNET_break (0 == CLOSE (sock->sock));
      sock->sock = -1;
      transmit_error (sock);
      return;
    }
#if DEBUG_NETWORK
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmitted %d bytes to OS\n", ret);
#endif
  sock->write_buffer_pos += ret;
  if (sock->write_buffer_pos == sock->write_buffer_off)
    {
      /* transmitted all pending data */
      sock->write_buffer_pos = 0;
      sock->write_buffer_off = 0;
#if DEBUG_NETWORK
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "Transmission buffer now empty.\n", ret);
#endif
    }
  if ((sock->write_buffer_off == 0) && (NULL == sock->nth.notify_ready))
    return;                     /* all data sent! */
  /* not done writing, schedule more */
#if DEBUG_NETWORK
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "More data ready for transmission, scheduling task again!\n");
#endif
SCHEDULE_WRITE:
  if (sock->write_task == GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
    sock->write_task =
      GNUNET_SCHEDULER_add_write (tc->sched,
                                  GNUNET_NO,
                                  GNUNET_SCHEDULER_PRIORITY_KEEP,
                                  GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
                                  GNUNET_TIME_UNIT_FOREVER_REL,
                                  sock->sock, &transmit_ready, sock);
}


/**
 * Ask the socket to call us once the specified number of bytes
 * are free in the transmission buffer.  May call the notify
 * method immediately if enough space is available.
 *
 * @param sock socket
 * @param size number of bytes to send
 * @param timeout after how long should we give up (and call
 *        notify with buf NULL and size 0)?
 * @param notify function to call
 * @param notify_cls closure for notify
 * @return non-NULL if the notify callback was queued,
 *         NULL if we are already going to notify someone else (busy)
 */
struct GNUNET_NETWORK_TransmitHandle *
GNUNET_NETWORK_notify_transmit_ready (struct GNUNET_NETWORK_SocketHandle
                                      *sock, size_t size,
                                      struct GNUNET_TIME_Relative timeout,
                                      GNUNET_NETWORK_TransmitReadyNotify
                                      notify, void *notify_cls)
{
  if (sock->nth.notify_ready != NULL)
    return NULL;
  GNUNET_assert (notify != NULL);
  GNUNET_assert (sock->write_buffer_size >= size);

  if ((sock->sock == -1) &&
      (sock->connect_task == GNUNET_SCHEDULER_NO_PREREQUISITE_TASK))
    {
#if DEBUG_NETWORK
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "Transmit fails, connection failed.\n");
#endif
      notify (notify_cls, 0, NULL);
      return &sock->nth;
    }
  GNUNET_assert (sock->write_buffer_off <= sock->write_buffer_size);
  GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_size);
  GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_off);
  sock->nth.notify_ready = notify;
  sock->nth.notify_ready_cls = notify_cls;
  sock->nth.sh = sock;
  sock->nth.notify_size = size;
  sock->nth.transmit_timeout = GNUNET_TIME_relative_to_absolute (timeout);
  sock->nth.timeout_task = GNUNET_SCHEDULER_add_delayed (sock->sched,
                                                         GNUNET_NO,
                                                         GNUNET_SCHEDULER_PRIORITY_KEEP,
                                                         GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
                                                         timeout,
                                                         &transmit_timeout,
                                                         sock);
#if DEBUG_NETWORK
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Scheduling asynchronous transmission once connect is done...\n");
#endif
  if (sock->write_task == GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
    sock->write_task = GNUNET_SCHEDULER_add_delayed (sock->sched,
                                                     GNUNET_NO,
                                                     GNUNET_SCHEDULER_PRIORITY_KEEP,
                                                     sock->connect_task,
                                                     GNUNET_TIME_UNIT_ZERO,
                                                     &transmit_ready, sock);
  return &sock->nth;
}


/**
 * Cancel the specified transmission-ready
 * notification.
 */
void
GNUNET_NETWORK_notify_transmit_ready_cancel (struct
                                             GNUNET_NETWORK_TransmitHandle *h)
{
  GNUNET_assert (h->notify_ready != NULL);
  GNUNET_SCHEDULER_cancel (h->sh->sched, h->timeout_task);
  h->timeout_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
  h->notify_ready = NULL;
}


#if 0                           /* keep Emacsens' auto-indent happy */
{
#endif
#ifdef __cplusplus
}
#endif