diff options
author | Christian Grothoff <christian@grothoff.org> | 2017-01-17 22:40:43 +0100 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2017-01-17 22:40:43 +0100 |
commit | 4ed4bcba53601d97793663017efcda877c3c490a (patch) | |
tree | 2ac5e30ae8fa61386ed39214173a3b973306655b | |
parent | 7f756511ec2bafd5a5a2c3f2c40f7c08f3c39f85 (diff) | |
download | gnunet-4ed4bcba53601d97793663017efcda877c3c490a.tar.gz gnunet-4ed4bcba53601d97793663017efcda877c3c490a.zip |
add logic to properly decode and dispatch decrypted messages
-rw-r--r-- | src/cadet/gnunet-service-cadet-new_tunnels.c | 246 |
1 files changed, 216 insertions, 30 deletions
diff --git a/src/cadet/gnunet-service-cadet-new_tunnels.c b/src/cadet/gnunet-service-cadet-new_tunnels.c index 7d281022c..74e56aa35 100644 --- a/src/cadet/gnunet-service-cadet-new_tunnels.c +++ b/src/cadet/gnunet-service-cadet-new_tunnels.c | |||
@@ -29,6 +29,8 @@ | |||
29 | * - when managing connections, distinguish those that | 29 | * - when managing connections, distinguish those that |
30 | * have (recently) had traffic from those that were | 30 | * have (recently) had traffic from those that were |
31 | * never ready (or not recently) | 31 | * never ready (or not recently) |
32 | * - implement sending and receiving KX messages | ||
33 | * - implement processing of incoming decrypted plaintext messages | ||
32 | * - clean up KX logic! | 34 | * - clean up KX logic! |
33 | */ | 35 | */ |
34 | #include "platform.h" | 36 | #include "platform.h" |
@@ -351,6 +353,16 @@ struct CadetTunnel | |||
351 | struct GNUNET_SCHEDULER_Task *rekey_task; | 353 | struct GNUNET_SCHEDULER_Task *rekey_task; |
352 | 354 | ||
353 | /** | 355 | /** |
356 | * Tokenizer for decrypted messages. | ||
357 | */ | ||
358 | struct GNUNET_MessageStreamTokenizer *mst; | ||
359 | |||
360 | /** | ||
361 | * Dispatcher for decrypted messages only (do NOT use for sending!). | ||
362 | */ | ||
363 | struct GNUNET_MQ_Handle *mq; | ||
364 | |||
365 | /** | ||
354 | * DLL of connections that are actively used to reach the destination peer. | 366 | * DLL of connections that are actively used to reach the destination peer. |
355 | */ | 367 | */ |
356 | struct CadetTConnection *connection_head; | 368 | struct CadetTConnection *connection_head; |
@@ -1187,6 +1199,8 @@ destroy_tunnel (void *cls) | |||
1187 | GNUNET_SCHEDULER_cancel (t->maintain_connections_task); | 1199 | GNUNET_SCHEDULER_cancel (t->maintain_connections_task); |
1188 | t->maintain_connections_task = NULL; | 1200 | t->maintain_connections_task = NULL; |
1189 | } | 1201 | } |
1202 | GNUNET_MST_destroy (t->mst); | ||
1203 | GNUNET_MQ_destroy (t->mq); | ||
1190 | GNUNET_free (t); | 1204 | GNUNET_free (t); |
1191 | } | 1205 | } |
1192 | 1206 | ||
@@ -1374,6 +1388,161 @@ GCT_consider_path (struct CadetTunnel *t, | |||
1374 | 1388 | ||
1375 | 1389 | ||
1376 | /** | 1390 | /** |
1391 | * | ||
1392 | * | ||
1393 | * @param cls the `struct CadetTunnel` for which we decrypted the message | ||
1394 | * @param msg the message we received on the tunnel | ||
1395 | */ | ||
1396 | static void | ||
1397 | handle_plaintext_keepalive (void *cls, | ||
1398 | const struct GNUNET_MessageHeader *msg) | ||
1399 | { | ||
1400 | struct CadetTunnel *t = cls; | ||
1401 | GNUNET_break (0); // FIXME | ||
1402 | } | ||
1403 | |||
1404 | |||
1405 | /** | ||
1406 | * Check that @a msg is well-formed. | ||
1407 | * | ||
1408 | * @param cls the `struct CadetTunnel` for which we decrypted the message | ||
1409 | * @param msg the message we received on the tunnel | ||
1410 | * @return #GNUNET_OK (any variable-size payload goes) | ||
1411 | */ | ||
1412 | static int | ||
1413 | check_plaintext_data (void *cls, | ||
1414 | const struct GNUNET_CADET_Data *msg) | ||
1415 | { | ||
1416 | return GNUNET_OK; | ||
1417 | } | ||
1418 | |||
1419 | |||
1420 | /** | ||
1421 | * | ||
1422 | * | ||
1423 | * @param cls the `struct CadetTunnel` for which we decrypted the message | ||
1424 | * @param msg the message we received on the tunnel | ||
1425 | */ | ||
1426 | static void | ||
1427 | handle_plaintext_data (void *cls, | ||
1428 | const struct GNUNET_CADET_Data *msg) | ||
1429 | { | ||
1430 | struct CadetTunnel *t = cls; | ||
1431 | GNUNET_break (0); // FIXME! | ||
1432 | } | ||
1433 | |||
1434 | |||
1435 | /** | ||
1436 | * | ||
1437 | * | ||
1438 | * @param cls the `struct CadetTunnel` for which we decrypted the message | ||
1439 | * @param ack the message we received on the tunnel | ||
1440 | */ | ||
1441 | static void | ||
1442 | handle_plaintext_data_ack (void *cls, | ||
1443 | const struct GNUNET_CADET_DataACK *ack) | ||
1444 | { | ||
1445 | struct CadetTunnel *t = cls; | ||
1446 | GNUNET_break (0); // FIXME! | ||
1447 | } | ||
1448 | |||
1449 | |||
1450 | /** | ||
1451 | * | ||
1452 | * | ||
1453 | * @param cls the `struct CadetTunnel` for which we decrypted the message | ||
1454 | * @param cc the message we received on the tunnel | ||
1455 | */ | ||
1456 | static void | ||
1457 | handle_plaintext_channel_create (void *cls, | ||
1458 | const struct GNUNET_CADET_ChannelCreate *cc) | ||
1459 | { | ||
1460 | struct CadetTunnel *t = cls; | ||
1461 | GNUNET_break (0); // FIXME! | ||
1462 | } | ||
1463 | |||
1464 | |||
1465 | /** | ||
1466 | * | ||
1467 | * | ||
1468 | * @param cls the `struct CadetTunnel` for which we decrypted the message | ||
1469 | * @param cm the message we received on the tunnel | ||
1470 | */ | ||
1471 | static void | ||
1472 | handle_plaintext_channel_nack (void *cls, | ||
1473 | const struct GNUNET_CADET_ChannelManage *cm) | ||
1474 | { | ||
1475 | struct CadetTunnel *t = cls; | ||
1476 | GNUNET_break (0); // FIXME! | ||
1477 | } | ||
1478 | |||
1479 | |||
1480 | /** | ||
1481 | * | ||
1482 | * | ||
1483 | * @param cls the `struct CadetTunnel` for which we decrypted the message | ||
1484 | * @param cm the message we received on the tunnel | ||
1485 | */ | ||
1486 | static void | ||
1487 | handle_plaintext_channel_ack (void *cls, | ||
1488 | const struct GNUNET_CADET_ChannelManage *cm) | ||
1489 | { | ||
1490 | struct CadetTunnel *t = cls; | ||
1491 | GNUNET_break (0); // FIXME! | ||
1492 | } | ||
1493 | |||
1494 | |||
1495 | /** | ||
1496 | * | ||
1497 | * | ||
1498 | * @param cls the `struct CadetTunnel` for which we decrypted the message | ||
1499 | * @param cm the message we received on the tunnel | ||
1500 | */ | ||
1501 | static void | ||
1502 | handle_plaintext_channel_destroy (void *cls, | ||
1503 | const struct GNUNET_CADET_ChannelManage *cm) | ||
1504 | { | ||
1505 | struct CadetTunnel *t = cls; | ||
1506 | GNUNET_break (0); // FIXME! | ||
1507 | } | ||
1508 | |||
1509 | |||
1510 | /** | ||
1511 | * Handles a message we decrypted, by injecting it into | ||
1512 | * our message queue (which will do the dispatching). | ||
1513 | * | ||
1514 | * @param cls the `struct CadetTunnel` that got the message | ||
1515 | * @param msg the message | ||
1516 | * @return #GNUNET_OK (continue to process) | ||
1517 | */ | ||
1518 | static int | ||
1519 | handle_decrypted (void *cls, | ||
1520 | const struct GNUNET_MessageHeader *msg) | ||
1521 | { | ||
1522 | struct CadetTunnel *t = cls; | ||
1523 | |||
1524 | GNUNET_MQ_inject_message (t->mq, | ||
1525 | msg); | ||
1526 | return GNUNET_OK; | ||
1527 | } | ||
1528 | |||
1529 | |||
1530 | /** | ||
1531 | * Function called if we had an error processing | ||
1532 | * an incoming decrypted message. | ||
1533 | * | ||
1534 | * @param cls the `struct CadetTunnel` | ||
1535 | * @param error error code | ||
1536 | */ | ||
1537 | static void | ||
1538 | decrypted_error_cb (void *cls, | ||
1539 | enum GNUNET_MQ_Error error) | ||
1540 | { | ||
1541 | GNUNET_break_op (0); | ||
1542 | } | ||
1543 | |||
1544 | |||
1545 | /** | ||
1377 | * Create a tunnel to @a destionation. Must only be called | 1546 | * Create a tunnel to @a destionation. Must only be called |
1378 | * from within #GCP_get_tunnel(). | 1547 | * from within #GCP_get_tunnel(). |
1379 | * | 1548 | * |
@@ -1383,6 +1552,37 @@ GCT_consider_path (struct CadetTunnel *t, | |||
1383 | struct CadetTunnel * | 1552 | struct CadetTunnel * |
1384 | GCT_create_tunnel (struct CadetPeer *destination) | 1553 | GCT_create_tunnel (struct CadetPeer *destination) |
1385 | { | 1554 | { |
1555 | struct GNUNET_MQ_MessageHandler handlers[] = { | ||
1556 | GNUNET_MQ_hd_fixed_size (plaintext_keepalive, | ||
1557 | GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE, | ||
1558 | struct GNUNET_MessageHeader, | ||
1559 | NULL), | ||
1560 | GNUNET_MQ_hd_var_size (plaintext_data, | ||
1561 | GNUNET_MESSAGE_TYPE_CADET_DATA, | ||
1562 | struct GNUNET_CADET_Data, | ||
1563 | NULL), | ||
1564 | GNUNET_MQ_hd_fixed_size (plaintext_data_ack, | ||
1565 | GNUNET_MESSAGE_TYPE_CADET_DATA_ACK, | ||
1566 | struct GNUNET_CADET_DataACK, | ||
1567 | NULL), | ||
1568 | GNUNET_MQ_hd_fixed_size (plaintext_channel_create, | ||
1569 | GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE, | ||
1570 | struct GNUNET_CADET_ChannelCreate, | ||
1571 | NULL), | ||
1572 | GNUNET_MQ_hd_fixed_size (plaintext_channel_nack, | ||
1573 | GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK, | ||
1574 | struct GNUNET_CADET_ChannelManage, | ||
1575 | NULL), | ||
1576 | GNUNET_MQ_hd_fixed_size (plaintext_channel_ack, | ||
1577 | GNUNET_MESSAGE_TYPE_CADET_CHANNEL_ACK, | ||
1578 | struct GNUNET_CADET_ChannelManage, | ||
1579 | NULL), | ||
1580 | GNUNET_MQ_hd_fixed_size (plaintext_channel_destroy, | ||
1581 | GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY, | ||
1582 | struct GNUNET_CADET_ChannelManage, | ||
1583 | NULL), | ||
1584 | GNUNET_MQ_handler_end () | ||
1585 | }; | ||
1386 | struct CadetTunnel *t; | 1586 | struct CadetTunnel *t; |
1387 | 1587 | ||
1388 | t = GNUNET_new (struct CadetTunnel); | 1588 | t = GNUNET_new (struct CadetTunnel); |
@@ -1394,6 +1594,15 @@ GCT_create_tunnel (struct CadetPeer *destination) | |||
1394 | t->maintain_connections_task | 1594 | t->maintain_connections_task |
1395 | = GNUNET_SCHEDULER_add_now (&maintain_connections_cb, | 1595 | = GNUNET_SCHEDULER_add_now (&maintain_connections_cb, |
1396 | t); | 1596 | t); |
1597 | t->mq = GNUNET_MQ_queue_for_callbacks (NULL, | ||
1598 | NULL, | ||
1599 | NULL, | ||
1600 | NULL, | ||
1601 | handlers, | ||
1602 | &decrypted_error_cb, | ||
1603 | t); | ||
1604 | t->mst = GNUNET_MST_create (&handle_decrypted, | ||
1605 | t); | ||
1397 | return t; | 1606 | return t; |
1398 | } | 1607 | } |
1399 | 1608 | ||
@@ -1489,8 +1698,6 @@ GCT_handle_encrypted (struct CadetTConnection *ct, | |||
1489 | uint16_t size = ntohs (msg->header.size); | 1698 | uint16_t size = ntohs (msg->header.size); |
1490 | char cbuf [size] GNUNET_ALIGN; | 1699 | char cbuf [size] GNUNET_ALIGN; |
1491 | ssize_t decrypted_size; | 1700 | ssize_t decrypted_size; |
1492 | const struct GNUNET_MessageHeader *msgh; | ||
1493 | unsigned int off; | ||
1494 | 1701 | ||
1495 | GNUNET_STATISTICS_update (stats, | 1702 | GNUNET_STATISTICS_update (stats, |
1496 | "# received encrypted", | 1703 | "# received encrypted", |
@@ -1522,34 +1729,13 @@ GCT_handle_encrypted (struct CadetTConnection *ct, | |||
1522 | 1729 | ||
1523 | GCT_change_estate (t, | 1730 | GCT_change_estate (t, |
1524 | CADET_TUNNEL_KEY_OK); | 1731 | CADET_TUNNEL_KEY_OK); |
1525 | 1732 | /* The MST will ultimately call #handle_decrypted() on each message. */ | |
1526 | #if 0 | 1733 | GNUNET_break_op (GNUNET_OK == |
1527 | /* FIXME: this is bad, as the structs returned from | 1734 | GNUNET_MST_from_buffer (t->mst, |
1528 | this loop may be unaligned, see util's MST for | 1735 | cbuf, |
1529 | how to do this right. | 1736 | decrypted_size, |
1530 | => Change MST API to use new MQ-style handlers! */ | 1737 | GNUNET_YES, |
1531 | off = 0; | 1738 | GNUNET_NO)); |
1532 | while (off + sizeof (struct GNUNET_MessageHeader) <= decrypted_size) | ||
1533 | { | ||
1534 | uint16_t msize; | ||
1535 | |||
1536 | msgh = (const struct GNUNET_MessageHeader *) &cbuf[off]; | ||
1537 | msize = ntohs (msgh->size); | ||
1538 | if (msize < sizeof (struct GNUNET_MessageHeader)) | ||
1539 | { | ||
1540 | GNUNET_break_op (0); | ||
1541 | return; | ||
1542 | } | ||
1543 | if (off + msize < decrypted_size) | ||
1544 | { | ||
1545 | GNUNET_break_op (0); | ||
1546 | return; | ||
1547 | } | ||
1548 | handle_decrypted (t, | ||
1549 | msgh); | ||
1550 | off += msize; | ||
1551 | } | ||
1552 | #endif | ||
1553 | } | 1739 | } |
1554 | 1740 | ||
1555 | 1741 | ||