aboutsummaryrefslogtreecommitdiff
path: root/src/transport/transport_api.c
blob: b98cabf0bdf95634f3e491062cdcecebbf125183 (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
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
/*
     This file is part of GNUnet.
     (C) 2009, 2010 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 transport/transport_api.c
 * @brief library to access the low-level P2P IO service
 * @author Christian Grothoff
 */
#include "platform.h"
#include "gnunet_client_lib.h"
#include "gnunet_constants.h"
#include "gnunet_container_lib.h"
#include "gnunet_arm_service.h"
#include "gnunet_hello_lib.h"
#include "gnunet_protocols.h"
#include "gnunet_server_lib.h"
#include "gnunet_time_lib.h"
#include "gnunet_transport_service.h"
#include "transport.h"

/**
 * After how long do we give up on transmitting a HELLO
 * to the service?
 */
#define OFFER_HELLO_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)

/**
 * After how long do we automatically retry an unsuccessful
 * CONNECT request?
 */
#define CONNECT_RETRY_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 750)

/**
 * How long should ARM wait when starting up the
 * transport service before reporting back?
 */
#define START_SERVICE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)

/**
 * How long should ARM wait when stopping the
 * transport service before reporting back?
 */
#define STOP_SERVICE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)


/**
 * What stage are we in for transmission processing?
 */
enum TransmitStage
  {
    /**
     * No active message.
     */
    TS_NEW = 0,

    /**
     * Message in local queue, not given to service.
     */
    TS_QUEUED = 1,

    /**
     * Message given to service, not confirmed (no SEND_OK).
     */
    TS_TRANSMITTED = 2,

    /**
     * One message was given to service and before it was confirmed,
     * another one was already queued (waiting for SEND_OK to pass on
     * to service).
     */
    TS_TRANSMITTED_QUEUED = 3
  };


/**
 * Handle for a transmission-ready request.
 */
struct GNUNET_TRANSPORT_TransmitHandle
{

  /**
   * Neighbour for this handle, NULL for control-traffic.
   */
  struct NeighbourList *neighbour;

  /**
   * Function to call when notify_size bytes are available
   * for transmission.
   */
  GNUNET_CONNECTION_TransmitReadyNotify notify;

  /**
   * Closure for notify.
   */
  void *notify_cls;

  /**
   * transmit_ready task Id.  The task is used to introduce the
   * artificial delay that may be required to maintain the bandwidth
   * limits.  Later, this will be the ID of the "transmit_timeout"
   * task which is used to signal a timeout if the transmission could
   * not be done in a timely fashion.
   */
  GNUNET_SCHEDULER_TaskIdentifier notify_delay_task;

  /**
   * Timeout for this request.
   */
  struct GNUNET_TIME_Absolute timeout;

  /**
   * How many bytes is our notify callback waiting for?
   */
  size_t notify_size;

  /**
   * How important is this message?
   */
  unsigned int priority;

};


/**
 * Handle for a control message queue entry.
 */
struct ControlMessage
{

  /**
   * This is a doubly-linked list.
   */
  struct ControlMessage *next;

  /**
   * This is a doubly-linked list.
   */
  struct ControlMessage *prev;

  /**
   * Overall transport handle.
   */
  struct GNUNET_TRANSPORT_Handle *h;

  /**
   * Function to call when notify_size bytes are available
   * for transmission.
   */
  GNUNET_CONNECTION_TransmitReadyNotify notify;

  /**
   * Closure for notify.
   */
  void *notify_cls;

  /**
   * transmit_ready task Id.  The task is used to introduce the
   * artificial delay that may be required to maintain the bandwidth
   * limits.  Later, this will be the ID of the "transmit_timeout"
   * task which is used to signal a timeout if the transmission could
   * not be done in a timely fashion.
   */
  GNUNET_SCHEDULER_TaskIdentifier notify_delay_task;

  /**
   * How many bytes is our notify callback waiting for?
   */
  size_t notify_size;

};


/**
 * Entry in linked list of all of our current neighbours.
 */
struct NeighbourList
{

  /**
   * This is a linked list.
   */
  struct NeighbourList *next;

  /**
   * Overall transport handle.
   */
  struct GNUNET_TRANSPORT_Handle *h;

  /**
   * Active transmit handle; available if 'transmit_forbidden'
   * is GNUNET_NO.
   */
  struct GNUNET_TRANSPORT_TransmitHandle transmit_handle;

  /**
   * Identity of this neighbour.
   */
  struct GNUNET_PeerIdentity id;

  /**
   * At what time did we reset last_sent last?
   */
  struct GNUNET_TIME_Absolute last_quota_update;

  /**
   * How many bytes have we sent since the "last_quota_update"
   * timestamp?
   */
  uint64_t last_sent;

  /**
   * Quota for outbound traffic to the neighbour in bytes/ms.
   */
  uint32_t quota_out;

  /**
   * Set to GNUNET_NO if we are currently allowed to accept a
   * message to the transport service for this peer, GNUNET_YES
   * if we have one and are waiting for transmission, GNUNET_SYSERR
   * if we are waiting for confirmation AND have already accepted
   * yet another message.
   */
  enum TransmitStage transmit_stage;

  /**
   * Have we received a notification that this peer is connected
   * to us right now?
   */
  int is_connected;

};


/**
 * Linked list of requests from clients for our HELLO that were
 * deferred.
 */
struct HelloWaitList
{

  /**
   * This is a linked list.
   */
  struct HelloWaitList *next;

  /**
   * Reference back to our transport handle.
   */
  struct GNUNET_TRANSPORT_Handle *handle;

  /**
   * Callback to call once we got our HELLO.
   */
  GNUNET_TRANSPORT_HelloUpdateCallback rec;

  /**
   * Closure for rec.
   */
  void *rec_cls;

};


/**
 * Handle for the transport service (includes all of the
 * state for the transport service).
 */
struct GNUNET_TRANSPORT_Handle
{

  /**
   * Closure for the callbacks.
   */
  void *cls;

  /**
   * Function to call for received data.
   */
  GNUNET_TRANSPORT_ReceiveCallback rec;

  /**
   * function to call on connect events
   */
  GNUNET_TRANSPORT_NotifyConnect nc_cb;

  /**
   * function to call on disconnect events
   */
  GNUNET_TRANSPORT_NotifyDisconnect nd_cb;

  /**
   * Head of DLL of control messages.
   */
  struct ControlMessage *control_head;

  /**
   * Tail of DLL of control messages.
   */
  struct ControlMessage *control_tail;

  /**
   * The current HELLO message for this peer.  Updated
   * whenever transports change their addresses.
   */
  struct GNUNET_HELLO_Message *my_hello;

  /**
   * My client connection to the transport service.
   */
  struct GNUNET_CLIENT_Connection *client;

  /**
   * Handle to our registration with the client for notification.
   */
  struct GNUNET_CLIENT_TransmitHandle *network_handle;

  /**
   * Linked list of pending requests for our HELLO.
   */
  struct HelloWaitList *hwl_head;

  /**
   * My scheduler.
   */
  struct GNUNET_SCHEDULER_Handle *sched;

  /**
   * My configuration.
   */
  const struct GNUNET_CONFIGURATION_Handle *cfg;

  /**
   * Linked list of the current neighbours of this peer.
   */
  struct NeighbourList *neighbours;

  /**
   * ID of the task trying to reconnect to the service.
   */
  GNUNET_SCHEDULER_TaskIdentifier reconnect_task;

  /**
   * ID of the task trying to trigger transmission for a peer
   * while maintaining bandwidth quotas.
   */
  GNUNET_SCHEDULER_TaskIdentifier quota_task;

  /**
   * Delay until we try to reconnect.
   */
  struct GNUNET_TIME_Relative reconnect_delay;

};


// FIXME: replace with hash map!
/**
 * Get the neighbour list entry for the given peer
 *
 * @param h our context
 * @param peer peer to look up
 * @return NULL if no such peer entry exists
 */
static struct NeighbourList *
neighbour_find (struct GNUNET_TRANSPORT_Handle *h,
                const struct GNUNET_PeerIdentity *peer)
{
  struct NeighbourList *pos;

  pos = h->neighbours;
  while ((pos != NULL) &&
         (0 != memcmp (peer, &pos->id, sizeof (struct GNUNET_PeerIdentity))))
    pos = pos->next;
  return pos;
}


/**
 * Schedule the task to send one message, either from the control
 * list or the peer message queues  to the service.
 */
static void schedule_transmission (struct GNUNET_TRANSPORT_Handle *h);


/**
 * Function called by the scheduler when the timeout for bandwidth
 * availablility for the target neighbour is reached.
 *
 * @param cls the 'struct GNUNET_TRANSPORT_Handle*'
 * @param tc scheduler context
 */
static void
quota_transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  struct GNUNET_TRANSPORT_Handle *h = cls;

  h->quota_task = GNUNET_SCHEDULER_NO_TASK;
  schedule_transmission (h);
}


/**
 * Update the quota values for the given neighbour now.
 *
 * @param n neighbour to update
 */
static void
update_quota (struct NeighbourList *n)
{
  struct GNUNET_TIME_Relative delta;
  uint64_t allowed;
  uint64_t remaining;

  delta = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
  allowed = delta.value * n->quota_out;
  if (n->last_sent < allowed)
    {
      remaining = allowed - n->last_sent;
      if (n->quota_out > 0)
        remaining /= n->quota_out;
      else
        remaining = 0;
      if (remaining > MAX_BANDWIDTH_CARRY)
        remaining = MAX_BANDWIDTH_CARRY;
      n->last_sent = 0;
      n->last_quota_update = GNUNET_TIME_absolute_get ();
      n->last_quota_update.value -= remaining;
    }
  else
    {
      n->last_sent -= allowed;
      n->last_quota_update = GNUNET_TIME_absolute_get ();
    }
}


/**
 * Figure out which transmission to a peer can be done right now.
 * If none can, schedule a task to call 'schedule_transmission'
 * whenever a peer transmission can be done in the future and
 * return NULL.  Otherwise return the next transmission to be
 * performed.
 *
 * @param h handle to transport
 * @return NULL to wait longer before doing any peer transmissions
 */
static struct GNUNET_TRANSPORT_TransmitHandle *
schedule_peer_transmission (struct GNUNET_TRANSPORT_Handle *h)
{
  struct GNUNET_TRANSPORT_TransmitHandle *ret;
  struct GNUNET_TRANSPORT_TransmitHandle *th;
  struct NeighbourList *n;
  struct NeighbourList *next;
  struct GNUNET_TIME_Relative retry_time;
  struct GNUNET_TIME_Relative duration;
  uint64_t available;

  if (h->quota_task != GNUNET_SCHEDULER_NO_TASK)
    {
      GNUNET_SCHEDULER_cancel (h->sched,
			       h->quota_task);
      h->quota_task = GNUNET_SCHEDULER_NO_TASK;
    }
  retry_time = GNUNET_TIME_UNIT_FOREVER_REL;
  ret = NULL;
  next = h->neighbours;
  while (NULL != (n = next))
    {
      next = n->next;
      if (n->transmit_stage != TS_QUEUED)
	continue; /* not eligible */
      th = &n->transmit_handle;
      /* check outgoing quota */
      duration = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
      if (duration.value > MIN_QUOTA_REFRESH_TIME)
	{
	  update_quota (n);
	  duration = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
	}
      available = duration.value * n->quota_out;
      if (available < n->last_sent + th->notify_size)
	{
	  /* calculate how much bandwidth we'd still need to
	     accumulate and based on that how long we'll have
	     to wait... */
	  available = n->last_sent + th->notify_size - available;
	  duration = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
						    available / n->quota_out);
	  if (duration.value == 0)
	    duration = GNUNET_TIME_UNIT_MILLISECONDS;
	  if (th->timeout.value <
	      GNUNET_TIME_relative_to_absolute (duration).value)
	    {
	      /* signal timeout! */
#if DEBUG_TRANSPORT
	      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
			  "Would need %llu ms before bandwidth is available for delivery to `%4s', that is too long.  Signaling timeout.\n",
			  duration.value, GNUNET_i2s (&n->id));
#endif
	      if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
		{
		  GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
		  th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
		}	      
	      n->transmit_stage = TS_NEW;
	      if (NULL != th->notify)
		GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
	      continue;
	    }
#if DEBUG_TRANSPORT
	  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
		      "Need more bandwidth, delaying delivery to `%4s' by %llu ms\n",
		      GNUNET_i2s (&n->id), duration.value);
#endif
	  retry_time = GNUNET_TIME_relative_min (retry_time,
						 duration);
	  continue;
	}
#if DEBUG_TRANSPORT
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
		  "Bandwidth available for transmission to `%4s'\n",
		  GNUNET_i2s (&n->id));
#endif
      if ( (ret == NULL) ||
	   (ret->priority < th->priority) )
	ret = th;
    }
  if (ret == NULL)
    h->quota_task = GNUNET_SCHEDULER_add_delayed (h->sched,
						  retry_time,
						  &quota_transmit_ready,
						  h);
  return ret;
}


/**
 * Transmit message(s) to service.
 *
 * @param cls handle to transport 
 * @param size number of bytes available in buf
 * @param buf where to copy the message
 * @return number of bytes copied to buf
 */
static size_t
transport_notify_ready (void *cls, size_t size, void *buf)
{
  struct GNUNET_TRANSPORT_Handle *h = cls;
  struct ControlMessage *cm;
  struct GNUNET_TRANSPORT_TransmitHandle *th;
  struct NeighbourList *n;
  struct OutboundMessage obm;
  size_t ret;
  size_t mret;
  char *cbuf;

  h->network_handle = NULL;
  if (buf == NULL)
    {
      schedule_transmission (h);
      return 0;
    }
#if DEBUG_TRANSPORT
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Ready to transmit %u bytes to transport service\n", size);
#endif
  cbuf = buf;
  ret = 0;
  while ( (NULL != (cm = h->control_head)) &&
	  (cm->notify_size <= size) )
    {
      if (cm->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
        {
          GNUNET_SCHEDULER_cancel (h->sched, cm->notify_delay_task);
          cm->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
        }
      GNUNET_CONTAINER_DLL_remove (h->control_head,
				   h->control_tail,
				   cm);
      ret += cm->notify (cm->notify_cls, size, &cbuf[ret]);
      GNUNET_free (cm);
      size -= ret;
    }
  while ( (NULL != (th = schedule_peer_transmission (h))) &&
	  (th->notify_size <= size) )
    {
      if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
        {
          GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
          th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
        }
      n = th->neighbour;
      switch (n->transmit_stage)
	{
	case TS_NEW:
	  GNUNET_break (0);
	  break;
	case TS_QUEUED:
	  n->transmit_stage = TS_TRANSMITTED;
	  break;
	case TS_TRANSMITTED:
	  GNUNET_break (0);
	  break;
	case TS_TRANSMITTED_QUEUED:
	  GNUNET_break (0);
	  break;
	default:
	  GNUNET_break (0);
	}
      GNUNET_assert (size >= sizeof (struct OutboundMessage));
      mret = th->notify (th->notify_cls, 
			 size - sizeof (struct OutboundMessage),
			 &cbuf[ret + sizeof (struct OutboundMessage)]);
      GNUNET_assert (mret <= size - sizeof (struct OutboundMessage));
      if (mret != 0)	
	{
	  obm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND);
	  obm.header.size = htons (mret + sizeof (struct OutboundMessage));
	  obm.priority = htonl (th->priority);
	  obm.timeout = GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining (th->timeout));
	  obm.peer = n->id;
	  memcpy (&cbuf[ret], &obm, sizeof (struct OutboundMessage));
	  ret += (mret + sizeof (struct OutboundMessage));
	  size -= (mret + sizeof (struct OutboundMessage));
	}
      else
	{
	  switch (n->transmit_stage)
	    {
	    case TS_NEW:
	      GNUNET_break (0);
	      break;
	    case TS_QUEUED:
	      GNUNET_break (0);
	      break;
	    case TS_TRANSMITTED:
	      n->transmit_stage = TS_NEW;
	      break;
	    case TS_TRANSMITTED_QUEUED:
	      GNUNET_break (0);
	      break;
	    default:
	      GNUNET_break (0);
	    }
	}
    }
  schedule_transmission (h);
#if DEBUG_TRANSPORT
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Transmitting %u bytes to transport service\n", ret);
#endif
  return ret;
}


/**
 * Schedule the task to send one message, either from the control
 * list or the peer message queues  to the service.
 */
static void
schedule_transmission (struct GNUNET_TRANSPORT_Handle *h)
{  
  size_t size;
  struct GNUNET_TIME_Relative timeout;
  struct GNUNET_TRANSPORT_TransmitHandle *th;

  if (NULL != h->network_handle)
    return;
  if (h->client == NULL)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                  "Could not yet schedule transmission: we are not yet connected to the transport service!\n");
      return;                   /* not yet connected */
    }
  if (NULL != h->control_head) 
    {
      size = h->control_head->notify_size;
      timeout = GNUNET_TIME_UNIT_FOREVER_REL;
    }
  else
    {
      th = schedule_peer_transmission (h);
      if (th == NULL)
	{
	  /* no transmission ready right now */
	  return;
	}
      size = th->notify_size;
      timeout = GNUNET_TIME_absolute_get_remaining (th->timeout);
    }
  h->network_handle = 
    GNUNET_CLIENT_notify_transmit_ready (h->client,
					 size,
					 timeout,
					 GNUNET_NO,
					 &transport_notify_ready,
					 h);
  GNUNET_assert (NULL != h->network_handle);
}


/**
 * Called when our transmit request timed out before any transport
 * reported success connecting to the desired peer or before the
 * transport was ready to receive.  Signal error and free
 * TransmitHandle.
 */
static void
control_transmit_timeout (void *cls,
			  const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  struct ControlMessage *th = cls;

  th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
  if (NULL != th->notify)
    th->notify (th->notify_cls, 0, NULL);
  GNUNET_CONTAINER_DLL_remove (th->h->control_head,
			       th->h->control_tail,
			       th);
  GNUNET_free (th);
}


/**
 * Queue control request for transmission to the transport
 * service.
 *
 * @param h handle to the transport service
 * @param size number of bytes to be transmitted
 * @param at_head request must be added to the head of the queue
 *        (otherwise request will be appended)
 * @param timeout how long this transmission can wait (at most)
 * @param notify function to call to get the content
 * @param notify_cls closure for notify
 */
static void
schedule_control_transmit (struct GNUNET_TRANSPORT_Handle *h,
                           size_t size,
                           int at_head,
                           struct GNUNET_TIME_Relative timeout,
                           GNUNET_CONNECTION_TransmitReadyNotify notify,
                           void *notify_cls)
{
  struct ControlMessage *th;

#if DEBUG_TRANSPORT
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Control transmit of %u bytes within %llums requested\n",
              size, (unsigned long long) timeout.value);
#endif
  th = GNUNET_malloc (sizeof (struct ControlMessage));
  th->h = h;
  th->notify = notify;
  th->notify_cls = notify_cls;
  th->notify_size = size;
  th->notify_delay_task
    = GNUNET_SCHEDULER_add_delayed (h->sched,
                                    timeout, &control_transmit_timeout, th);
  if (at_head)    
    GNUNET_CONTAINER_DLL_insert (h->control_head,
				 h->control_tail,
				 th);
  else
    GNUNET_CONTAINER_DLL_insert_after (h->control_head,
				       h->control_tail,
				       h->control_tail,
				       th);
  schedule_transmission (h);
}


struct SetQuotaContext
{
  struct GNUNET_TRANSPORT_Handle *handle;

  struct GNUNET_PeerIdentity target;

  GNUNET_SCHEDULER_Task cont;

  void *cont_cls;

  struct GNUNET_TIME_Absolute timeout;

  uint32_t quota_in;
};


/**
 * Send SET_QUOTA message to the service.
 *
 * @param cls the 'struct SetQuotaContext'
 * @param size number of bytes available in buf
 * @param buf where to copy the message
 * @return number of bytes copied to buf
 */
static size_t
send_set_quota (void *cls, size_t size, void *buf)
{
  struct SetQuotaContext *sqc = cls;
  struct QuotaSetMessage *msg;

  if (buf == NULL)
    {
      GNUNET_SCHEDULER_add_continuation (sqc->handle->sched,
                                         sqc->cont,
                                         sqc->cont_cls,
                                         GNUNET_SCHEDULER_REASON_TIMEOUT);
      GNUNET_free (sqc);
      return 0;
    }
#if DEBUG_TRANSPORT
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Transmitting `%s' request with respect to `%4s'.\n",
              "SET_QUOTA", 
	      GNUNET_i2s (&sqc->target));
#endif
  GNUNET_assert (size >= sizeof (struct QuotaSetMessage));
  msg = buf;
  msg->header.size = htons (sizeof (struct QuotaSetMessage));
  msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
  msg->quota_in = htonl (sqc->quota_in);
  memcpy (&msg->peer, &sqc->target, sizeof (struct GNUNET_PeerIdentity));
  if (sqc->cont != NULL)
    GNUNET_SCHEDULER_add_continuation (sqc->handle->sched,
                                       sqc->cont,
                                       sqc->cont_cls,
                                       GNUNET_SCHEDULER_REASON_PREREQ_DONE);
  GNUNET_free (sqc);
  return sizeof (struct QuotaSetMessage);
}


/**
 * Set the share of incoming bandwidth for the given
 * peer to the specified amount.
 *
 * @param handle connection to transport service
 * @param target who's bandwidth quota is being changed
 * @param quota_in incoming bandwidth quota in bytes per ms
 * @param quota_out outgoing bandwidth quota in bytes per ms
 * @param timeout how long to wait until signaling failure if
 *        we can not communicate the quota change
 * @param cont continuation to call when done, will be called
 *        either with reason "TIMEOUT" or with reason "PREREQ_DONE"
 * @param cont_cls closure for continuation
 */
void
GNUNET_TRANSPORT_set_quota (struct GNUNET_TRANSPORT_Handle *handle,
                            const struct GNUNET_PeerIdentity *target,
                            uint32_t quota_in,
                            uint32_t quota_out,
                            struct GNUNET_TIME_Relative timeout,
                            GNUNET_SCHEDULER_Task cont, void *cont_cls)
{
  struct NeighbourList *n;
  struct SetQuotaContext *sqc;

  n = neighbour_find (handle, target);
  if (n != NULL)
    {
      update_quota (n);
      if (n->quota_out < quota_out)
        n->last_quota_update = GNUNET_TIME_absolute_get ();
      n->quota_out = quota_out;
    }
  sqc = GNUNET_malloc (sizeof (struct SetQuotaContext));
  sqc->handle = handle;
  sqc->target = *target;
  sqc->cont = cont;
  sqc->cont_cls = cont_cls;
  sqc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
  sqc->quota_in = quota_in;
  schedule_control_transmit (handle,
                             sizeof (struct QuotaSetMessage),
                             GNUNET_NO, timeout, &send_set_quota, sqc);
}


/**
 * Obtain the HELLO message for this peer.
 *
 * @param handle connection to transport service
 * @param rec function to call with the HELLO, sender will be our peer
 *            identity; message and sender will be NULL on timeout
 *            (handshake with transport service pending/failed).
 *             cost estimate will be 0.
 * @param rec_cls closure for rec
 */
void
GNUNET_TRANSPORT_get_hello (struct GNUNET_TRANSPORT_Handle *handle,
                            GNUNET_TRANSPORT_HelloUpdateCallback rec,
                            void *rec_cls)
{
  struct HelloWaitList *hwl;

  hwl = GNUNET_malloc (sizeof (struct HelloWaitList));
  hwl->next = handle->hwl_head;
  handle->hwl_head = hwl;
  hwl->handle = handle;
  hwl->rec = rec;
  hwl->rec_cls = rec_cls;
  if (handle->my_hello == NULL)
    return;    
  rec (rec_cls, (const struct GNUNET_MessageHeader *) handle->my_hello);
}



/**
 * Stop receiving updates about changes to our HELLO message.
 *
 * @param handle connection to transport service
 * @param rec function previously registered to be called with the HELLOs
 * @param rec_cls closure for rec
 */
void
GNUNET_TRANSPORT_get_hello_cancel (struct GNUNET_TRANSPORT_Handle *handle,
				   GNUNET_TRANSPORT_HelloUpdateCallback rec,
				   void *rec_cls)
{
  struct HelloWaitList *pos;
  struct HelloWaitList *prev;

  prev = NULL;
  pos = handle->hwl_head;
  while (pos != NULL)
    {
      if ( (pos->rec == rec) &&
	   (pos->rec_cls == rec_cls) )
	break;
      prev = pos;
      pos = pos->next;
    }
  GNUNET_break (pos != NULL);
  if (pos == NULL)
    return;
  if (prev == NULL)
    handle->hwl_head = pos->next;
  else
    prev->next = pos->next;
  GNUNET_free (pos);
}


/**
 * Send HELLO message to the service.
 *
 * @param cls the HELLO message to send
 * @param size number of bytes available in buf
 * @param buf where to copy the message
 * @return number of bytes copied to buf
 */
static size_t
send_hello (void *cls, size_t size, void *buf)
{
  struct GNUNET_MessageHeader *hello = cls;
  uint16_t msize;

  if (buf == NULL)
    {
#if DEBUG_TRANSPORT
      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                  "Timeout while trying to transmit `%s' request.\n",
                  "HELLO");
#endif
      GNUNET_free (hello);
      return 0;
    }
#if DEBUG_TRANSPORT
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Transmitting `%s' request.\n", "HELLO");
#endif
  msize = ntohs (hello->size);
  GNUNET_assert (size >= msize);
  memcpy (buf, hello, msize);
  GNUNET_free (hello);
  return msize;
}


/**
 * Offer the transport service the HELLO of another peer.  Note that
 * the transport service may just ignore this message if the HELLO is
 * malformed or useless due to our local configuration.
 *
 * @param handle connection to transport service
 * @param hello the hello message
 */
void
GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
                              const struct GNUNET_MessageHeader *hello)
{
  struct GNUNET_MessageHeader *hc;
  uint16_t size;

  GNUNET_break (ntohs (hello->type) == GNUNET_MESSAGE_TYPE_HELLO);
  size = ntohs (hello->size);
  GNUNET_break (size >= sizeof (struct GNUNET_MessageHeader));
  hc = GNUNET_malloc (size);
  memcpy (hc, hello, size);
  schedule_control_transmit (handle,
                             size,
                             GNUNET_NO, OFFER_HELLO_TIMEOUT, &send_hello, hc);
}


/**
 * Transmit START message to service.
 *
 * @param cls unused
 * @param size number of bytes available in buf
 * @param buf where to copy the message
 * @return number of bytes copied to buf
 */
static size_t
send_start (void *cls, size_t size, void *buf)
{
  struct GNUNET_MessageHeader *s = buf;

  if (buf == NULL)
    {
      /* Can only be shutdown, just give up */
#if DEBUG_TRANSPORT
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "Shutdown while trying to transmit `%s' request.\n",
                  "START");
#endif
      return 0;
    }
#if DEBUG_TRANSPORT
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Transmitting `%s' request.\n", "START");
#endif
  GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
  s->size = htons (sizeof (struct GNUNET_MessageHeader));
  s->type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_START);
  return sizeof (struct GNUNET_MessageHeader);
}


/**
 * Free neighbour. 
 * 
 * @param n the entry to free
 */
static void
neighbour_free (struct NeighbourList *n)
{
  struct GNUNET_TRANSPORT_Handle *h;
  struct NeighbourList *prev;
  struct NeighbourList *pos;

  h = n->h;
#if DEBUG_TRANSPORT
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Removing neighbour `%s' from list of connected peers.\n",
              GNUNET_i2s (&n->id));
#endif
  GNUNET_break (n->is_connected == GNUNET_NO);
  GNUNET_break (n->transmit_stage == TS_NEW);

  prev = NULL;
  pos = h->neighbours;
  while (pos != n)
    {
      prev = pos;
      pos = pos->next;
    }
  if (prev == NULL)
    h->neighbours = n->next;
  else
    prev->next = n->next;
  GNUNET_free (n);
}


/**
 * Mark neighbour as disconnected. 
 * 
 * @param n the entry to mark as disconnected
 */
static void
neighbour_disconnect (struct NeighbourList *n)
{
  struct GNUNET_TRANSPORT_Handle *h = n->h;
#if DEBUG_TRANSPORT
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Removing neighbour `%s' from list of connected peers.\n",
              GNUNET_i2s (&n->id));
#endif
  GNUNET_break (n->is_connected == GNUNET_YES);
  n->is_connected = GNUNET_NO;
  if (h->nc_cb != NULL)
    h->nd_cb (h->cls, &n->id);
  if (n->transmit_stage == TS_NEW)
    neighbour_free (n);
}


/**
 * Function we use for handling incoming messages.
 *
 * @param cls closure (struct GNUNET_TRANSPORT_Handle *)
 * @param msg message received, NULL on timeout or fatal error
 */
static void demultiplexer (void *cls, 
			   const struct GNUNET_MessageHeader *msg);


/**
 * Try again to connect to transport service.
 *
 * @param cls the handle to the transport service
 * @param tc scheduler context
 */
static void
reconnect (void *cls, 
	   const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  struct GNUNET_TRANSPORT_Handle *h = cls;
  struct ControlMessage *pos;
  struct NeighbourList *n;

  h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
  if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
    {
      /* shutdown, just give up */
      return;
    }
  /* Forget about all neighbours that we used to be connected to */
  n = h->neighbours;
  while (NULL != n)
    {
      if (n->is_connected)
	neighbour_disconnect (n);
      n = n->next;
    }
#if DEBUG_TRANSPORT
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to transport service.\n");
#endif
  GNUNET_assert (h->client == NULL);
  h->client = GNUNET_CLIENT_connect (h->sched, "transport", h->cfg);
  GNUNET_assert (h->client != NULL);
  /* make sure we don't send "START" twice, remove existing entry from
     queue (if present) */
  pos = h->control_head;
  while (pos != NULL)
    {
      if (pos->notify == &send_start)
        {
	  GNUNET_CONTAINER_DLL_remove (h->control_head,
				       h->control_tail,
				       pos);
          if (GNUNET_SCHEDULER_NO_TASK != pos->notify_delay_task)
            {
              GNUNET_SCHEDULER_cancel (h->sched, pos->notify_delay_task);
              pos->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
            }
          GNUNET_free (pos);
          break;
        }
      pos = pos->next;
    }
  schedule_control_transmit (h,
                             sizeof (struct GNUNET_MessageHeader),
                             GNUNET_YES,
                             GNUNET_TIME_UNIT_FOREVER_REL, &send_start, NULL);
  GNUNET_CLIENT_receive (h->client,
                         &demultiplexer, h, GNUNET_TIME_UNIT_FOREVER_REL);
}


/**
 * Function that will schedule the job that will try
 * to connect us again to the client.
 *
 * @param h transport service to reconnect
 */
static void
schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h)
{
#if DEBUG_TRANSPORT
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Scheduling task to reconnect to transport service in %llu ms.\n",
              h->reconnect_delay.value);
#endif
  GNUNET_assert (h->client == NULL);
  GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
  h->reconnect_task
    = GNUNET_SCHEDULER_add_delayed (h->sched,
                                    h->reconnect_delay, &reconnect, h);
  if (h->reconnect_delay.value == 0)
    {
      h->reconnect_delay = GNUNET_TIME_UNIT_MILLISECONDS;
    }
  else 
    {
      h->reconnect_delay = GNUNET_TIME_relative_multiply (h->reconnect_delay, 2);
      h->reconnect_delay = GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
						     h->reconnect_delay);
    }
}


/**
 * Add neighbour to our list
 */
static struct NeighbourList *
neighbour_add (struct GNUNET_TRANSPORT_Handle *h,
               const struct GNUNET_PeerIdentity *pid)
{
  struct NeighbourList *n;

  /* check for duplicates */
  if (NULL != (n = neighbour_find (h, pid)))
    {
      GNUNET_break (0);
      return n;
    }
#if DEBUG_TRANSPORT
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Creating entry for neighbour `%4s'.\n", 
	      GNUNET_i2s (pid));
#endif
  n = GNUNET_malloc (sizeof (struct NeighbourList));
  n->id = *pid;
  n->last_quota_update = GNUNET_TIME_absolute_get ();
  n->next = h->neighbours;
  n->quota_out = GNUNET_CONSTANTS_DEFAULT_BPM_IN_OUT;
  n->h = h;
  h->neighbours = n;  
  return n;
}


/**
 * Connect to the transport service.  Note that the connection may
 * complete (or fail) asynchronously.
 *
 * @param sched scheduler to use
 * @param cfg configuration to use
 * @param cls closure for the callbacks
 * @param rec receive function to call
 * @param nc function to call on connect events
 * @param nd function to call on disconnect events
 */
struct GNUNET_TRANSPORT_Handle *
GNUNET_TRANSPORT_connect (struct GNUNET_SCHEDULER_Handle *sched,
                          const struct GNUNET_CONFIGURATION_Handle *cfg,
                          void *cls,
                          GNUNET_TRANSPORT_ReceiveCallback rec,
                          GNUNET_TRANSPORT_NotifyConnect nc,
                          GNUNET_TRANSPORT_NotifyDisconnect nd)
{
  struct GNUNET_TRANSPORT_Handle *ret;

  GNUNET_ARM_start_services (cfg, sched, "peerinfo", "transport", NULL);
  ret = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_Handle));
  ret->sched = sched;
  ret->cfg = cfg;
  ret->cls = cls;
  ret->rec = rec;
  ret->nc_cb = nc;
  ret->nd_cb = nd;
  ret->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
  schedule_reconnect (ret);
  return ret;
}


/**
 * Disconnect from the transport service.
 */
void
GNUNET_TRANSPORT_disconnect (struct GNUNET_TRANSPORT_Handle *handle)
{
  struct GNUNET_TRANSPORT_TransmitHandle *th;
  struct NeighbourList *n;
  struct HelloWaitList *hwl;
  struct GNUNET_CLIENT_Connection *client;

#if DEBUG_TRANSPORT
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transport disconnect called!\n");
#endif
  while (NULL != (n = handle->neighbours))
    {
      handle->neighbours = n->next;
      switch (n->transmit_stage)
	{
	case TS_NEW:
	case TS_TRANSMITTED:
	  /* nothing to do */
	  break;
	case TS_QUEUED:
	case TS_TRANSMITTED_QUEUED:
	  th = &n->transmit_handle;
	  if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
	    {
	      GNUNET_SCHEDULER_cancel (handle->sched,
				       th->notify_delay_task);
	      th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
	    }
	  GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));        
	  break;
	default:
	  GNUNET_break (0);
	}
      GNUNET_free (n);
    }
  while (NULL != (hwl = handle->hwl_head))
    {
      handle->hwl_head = hwl->next;
      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                  _
                  ("Disconnect while notification for `%s' still registered.\n"),
                  "HELLO");
      if (hwl->rec != NULL)
        hwl->rec (hwl->rec_cls, NULL);
      GNUNET_free (hwl);
    }
  if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
    {
      GNUNET_SCHEDULER_cancel (handle->sched, handle->reconnect_task);
      handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
    }
  if (handle->quota_task != GNUNET_SCHEDULER_NO_TASK)
    {
      GNUNET_SCHEDULER_cancel (handle->sched, handle->quota_task);
      handle->quota_task = GNUNET_SCHEDULER_NO_TASK;
    }
  GNUNET_free_non_null (handle->my_hello);
  handle->my_hello = NULL;
  GNUNET_ARM_stop_services (handle->cfg, handle->sched, "transport",
                            "peerinfo", NULL);
  if (NULL != handle->network_handle)
    {
      GNUNET_CLIENT_notify_transmit_ready_cancel (handle->network_handle);
      handle->network_handle = NULL;
    }
  if (NULL != (client = handle->client))
    {
#if DEBUG_TRANSPORT
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "Disconnecting from transport service for good.\n");
#endif
      handle->client = NULL;
      GNUNET_CLIENT_disconnect (client);
    }
  GNUNET_free (handle);
}


/**
 * Function we use for handling incoming messages.
 *
 * @param cls closure (struct GNUNET_TRANSPORT_Handle *)
 * @param msg message received, NULL on timeout or fatal error
 */
static void
demultiplexer (void *cls, const struct GNUNET_MessageHeader *msg)
{
  struct GNUNET_TRANSPORT_Handle *h = cls;
  const struct DisconnectInfoMessage *dim;
  const struct ConnectInfoMessage *cim;
  const struct InboundMessage *im;
  const struct GNUNET_MessageHeader *imm;
  const struct SendOkMessage *okm;
  struct HelloWaitList *hwl;
  struct HelloWaitList *next_hwl;
  struct NeighbourList *n;
  struct GNUNET_PeerIdentity me;
  uint16_t size;

  if (h->client == NULL)
    {
      /* shutdown initiated from 'GNUNET_TRANSPORT_disconnect',
	 finish clean up work! */
      GNUNET_free (h);
      return;
    }
  if (msg == NULL) 
    {
#if DEBUG_TRANSPORT
      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
		  "Error receiving from transport service, disconnecting temporarily.\n");
#endif
      if (h->network_handle != NULL)
	{
	  GNUNET_CLIENT_notify_transmit_ready_cancel (h->network_handle);
	  h->network_handle = NULL;
	}
      GNUNET_CLIENT_disconnect (h->client);
      h->client = NULL;
      schedule_reconnect (h);
      return;
    }
  GNUNET_CLIENT_receive (h->client,
                         &demultiplexer, h, GNUNET_TIME_UNIT_FOREVER_REL);
  size = ntohs (msg->size);
  switch (ntohs (msg->type))
    {
    case GNUNET_MESSAGE_TYPE_HELLO:
      if (GNUNET_OK !=
          GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) msg,
                               &me))
        {
          GNUNET_break (0);
          break;
        }
#if DEBUG_TRANSPORT
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "Receiving (my own) `%s' message, I am `%4s'.\n",
                  "HELLO", GNUNET_i2s (&me));
#endif
      GNUNET_free_non_null (h->my_hello);
      h->my_hello = NULL;
      if (size < sizeof (struct GNUNET_MessageHeader))
        {
          GNUNET_break (0);
          break;
        }
      h->my_hello = GNUNET_malloc (size);
      memcpy (h->my_hello, msg, size);
      hwl = h->hwl_head;
      while (NULL != hwl)
        {
	  next_hwl = hwl->next;
          hwl->rec (hwl->rec_cls,
		    (const struct GNUNET_MessageHeader *) h->my_hello);
	  hwl = next_hwl;
        }
      break;
    case GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT:
      if (size != sizeof (struct ConnectInfoMessage))
        {
          GNUNET_break (0);
          break;
        }
      cim = (const struct ConnectInfoMessage *) msg;
#if DEBUG_TRANSPORT
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "Receiving `%s' message for `%4s'.\n",
                  "CONNECT", GNUNET_i2s (&cim->id));
#endif
      n = neighbour_find (h, &cim->id);
      if (n == NULL)
	n = neighbour_add (h,
			   &cim->id);
      GNUNET_break (n->is_connected == GNUNET_NO);
      n->is_connected = GNUNET_YES;
      if (h->nc_cb != NULL)
	h->nc_cb (h->cls, &n->id,
		  GNUNET_TIME_relative_ntoh (cim->latency), 
		  ntohs (cim->distance));
      break;
    case GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT:
      if (size != sizeof (struct DisconnectInfoMessage))
        {
          GNUNET_break (0);
          break;
        }
      dim = (const struct DisconnectInfoMessage *) msg;
#if DEBUG_TRANSPORT
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "Receiving `%s' message for `%4s'.\n",
                  "DISCONNECT",
		  GNUNET_i2s (&dim->peer));
#endif
      n = neighbour_find (h, &dim->peer);
      GNUNET_break (n != NULL);      
      if (n != NULL)
	neighbour_disconnect (n);      	
      break;
    case GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK:
      if (size != sizeof (struct SendOkMessage))
        {
          GNUNET_break (0);
          break;
        }
      okm = (const struct SendOkMessage *) msg;
#if DEBUG_TRANSPORT
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "Receiving `%s' message, transmission %s.\n", "SEND_OK",
                  ntohl (okm->success) == GNUNET_OK ? "succeeded" : "failed");
#endif
      n = neighbour_find (h, &okm->peer);
      GNUNET_assert (n != NULL);
      switch (n->transmit_stage)
	{
	case TS_NEW:
	  GNUNET_break (0);
	  break;
	case TS_QUEUED:
	  GNUNET_break (0);
	  break;
	case TS_TRANSMITTED:
	  n->transmit_stage = TS_NEW;
	  break;
	case TS_TRANSMITTED_QUEUED:
	  n->transmit_stage = TS_QUEUED;
	  schedule_transmission (h);
	  break;
	default:
	  GNUNET_break (0);
	}
      break;
    case GNUNET_MESSAGE_TYPE_TRANSPORT_RECV:
#if DEBUG_TRANSPORT
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "Receiving `%s' message.\n", "RECV");
#endif
      if (size <
          sizeof (struct InboundMessage) +
          sizeof (struct GNUNET_MessageHeader))
        {
          GNUNET_break (0);
          break;
        }
      im = (const struct InboundMessage *) msg;
      imm = (const struct GNUNET_MessageHeader *) &im[1];
      if (ntohs (imm->size) + sizeof (struct InboundMessage) != size)
        {
          GNUNET_break (0);
          break;
        }
#if DEBUG_TRANSPORT
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
		  "Received message of type %u from `%4s'.\n",
		  ntohs (imm->type), GNUNET_i2s (&im->peer));
#endif      
      n = neighbour_find (h, &im->peer);
      if (n == NULL)
	n = neighbour_add (h, &im->peer);
      if (n == NULL) 
	break;
      if (h->rec != NULL)
	h->rec (h->cls, &im->peer, imm,
		GNUNET_TIME_relative_ntoh (im->latency), ntohs(im->distance));
      break;
    default:
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                  _
                  ("Received unexpected message of type %u in %s:%u\n"),
                  ntohs (msg->type), __FILE__, __LINE__);
      GNUNET_break (0);
      break;
    }
}


/**
 * Called when our transmit request timed out before any transport
 * reported success connecting to the desired peer or before the
 * transport was ready to receive.  Signal error and free
 * TransmitHandle.
 *
 * @param cls the 'struct GNUNET_TRANSPORT_TransmitHandle*' that is timing out
 * @param tc scheduler context
 */
static void
peer_transmit_timeout (void *cls,
		       const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
  struct NeighbourList *n;
  GNUNET_CONNECTION_TransmitReadyNotify notify;
  void *notify_cls;

  th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
  n = th->neighbour;
#if DEBUG_TRANSPORT
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
	      "Triggering timeout for request to transmit to `%4s' (%d)\n",
	      GNUNET_i2s (&n->id),
	      n->transmit_stage);
#endif  
  notify = th->notify;
  notify_cls = th->notify_cls;
  switch (n->transmit_stage)
    {
    case TS_NEW:
      GNUNET_break (0);
      break;
    case TS_QUEUED:
      n->transmit_stage = TS_NEW;
      if (n->is_connected == GNUNET_NO)
	neighbour_free (n);
      break;
    case TS_TRANSMITTED:
      GNUNET_break (0);
      break;
    case TS_TRANSMITTED_QUEUED:
      n->transmit_stage = TS_TRANSMITTED;
      break;
    default:
      GNUNET_break (0);
    }
  if (NULL != notify)
    notify (notify_cls, 0, NULL);
}


/**
 * Check if we could queue a message of the given size for
 * transmission.  The transport service will take both its
 * internal buffers and bandwidth limits imposed by the
 * other peer into consideration when answering this query.
 *
 * @param handle connection to transport service
 * @param target who should receive the message
 * @param size how big is the message we want to transmit?
 * @param priority how important is the message?
 * @param timeout after how long should we give up (and call
 *        notify with buf NULL and size 0)?
 * @param notify function to call when we are ready to
 *        send such a message
 * @param notify_cls closure for notify
 * @return NULL if someone else is already waiting to be notified
 *         non-NULL if the notify callback was queued (can be used to cancel
 *         using GNUNET_TRANSPORT_notify_transmit_ready_cancel)
 */
struct GNUNET_TRANSPORT_TransmitHandle *
GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle
                                        *handle,
                                        const struct GNUNET_PeerIdentity
                                        *target, size_t size,
                                        unsigned int priority,
                                        struct GNUNET_TIME_Relative timeout,
                                        GNUNET_CONNECTION_TransmitReadyNotify
                                        notify, void *notify_cls)
{
  struct GNUNET_TRANSPORT_TransmitHandle *th;
  struct NeighbourList *n;

  if (size + sizeof (struct OutboundMessage) >=
      GNUNET_SERVER_MAX_MESSAGE_SIZE)
    {
#if DEBUG_TRANSPORT
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "Message size is %d, max allowed is %d.\n",
                  size + sizeof (struct OutboundMessage), GNUNET_SERVER_MAX_MESSAGE_SIZE);
#endif
      GNUNET_break (0);
      return NULL;
    }
#if DEBUG_TRANSPORT
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Asking transport service for transmission of %u bytes to peer `%4s'.\n",
              size, GNUNET_i2s (target));
#endif
  n = neighbour_find (handle, target);
  if (n == NULL)
    n = neighbour_add (handle, target);
  if (n == NULL) 
    return NULL;
  switch (n->transmit_stage)
    {
    case TS_NEW:
      n->transmit_stage = TS_QUEUED;
      break;
    case TS_QUEUED:
      GNUNET_break (0);
      return NULL;
    case TS_TRANSMITTED:
      n->transmit_stage = TS_TRANSMITTED_QUEUED;
      break;
    case TS_TRANSMITTED_QUEUED:
      GNUNET_break (0);
      return NULL;
    default:
      GNUNET_break (0);
      return NULL;
    }
  th = &n->transmit_handle;
  th->neighbour = n;
  th->notify = notify;
  th->notify_cls = notify_cls;
  th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
  th->notify_size = size + sizeof (struct OutboundMessage);
  th->priority = priority;
  th->notify_delay_task
    = GNUNET_SCHEDULER_add_delayed (handle->sched, timeout,
				    &peer_transmit_timeout, th);
  schedule_transmission (handle);
  return th;
}


/**
 * Cancel the specified transmission-ready notification.
 */
void
GNUNET_TRANSPORT_notify_transmit_ready_cancel (struct
                                               GNUNET_TRANSPORT_TransmitHandle
                                               *th)
{
  struct NeighbourList *n;

  n = th->neighbour;
#if DEBUG_TRANSPORT
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Transmission request of %u bytes to `%4s' was cancelled.\n",
              th->notify_size - sizeof (struct OutboundMessage),
              GNUNET_i2s (&n->id));
#endif
  switch (n->transmit_stage)
    {
    case TS_NEW:
      GNUNET_break (0);
      break;
    case TS_QUEUED:
      n->transmit_stage = TS_NEW;
      if (n->is_connected == GNUNET_NO)
	neighbour_free (n);
      break;
    case TS_TRANSMITTED:
      GNUNET_break (0);
      break;
    case TS_TRANSMITTED_QUEUED:
      n->transmit_stage = TS_TRANSMITTED;
      break;
    default:
      GNUNET_break (0);
    }
}


/* end of transport_api.c */