Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merged protocolv2

  • Loading branch information...
commit a0f60561612a1b002c4762317e66c3b877d8e90e 2 parents b12fc89 + d44ee3e
Caleb James DeLisle authored
View
2  benc/serialization/standard/test/benc_serializeString_test.c
@@ -65,10 +65,12 @@ int main()
{
char out[512];
struct Allocator* alloc = CanaryAllocator_new(MallocAllocator_new(1<<20), NULL);
+
struct Writer* writer = ArrayWriter_new(out, 512, alloc);
struct Reader* reader = ArrayReader_new(out, 512, alloc);
testSerialize(writer, reader);
testParse(writer, reader, alloc);
+
Allocator_free(alloc);
}
View
178 dht/AddressMapper.c
@@ -1,178 +0,0 @@
-/* vim: set expandtab ts=4 sw=4: */
-/*
- * You may redistribute this program and/or modify it under the terms of
- * the GNU General Public License as published by the Free Software Foundation,
- * either version 3 of the License, or (at your option) any later version.
- *
- * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-#include "util/Bits.h"
-#include "memory/Allocator.h"
-#include "AddressMapper.h"
-
-#include <stdint.h>
-#include "util/platform/libc/string.h"
-#include <stdlib.h>
-
-/* if set, address mapper linked list entries are validated before use */
-#define ENTRY_VALIDATION
-
-/* convenience macro for entryGet() that assumes the pointer to
- * the map is in scope
- */
-#define ENTRY(e) entryGet(e, map)
-
-/* gets the index of a linked list entry with respect to the
- * start of the element array
- */
-#define entryIndexOf(map, entry) ((entryGet(entry, map)) - &(map)->entries[0])
-
-
-void AddressMapper_init(struct AddressMapper* map)
-{
- /* initialize the doubly linked list */
-
- for (int i = 0; i < AddressMapper_MAX_ENTRIES; i++) {
- map->entries[i].prev = &map->entries[i - 1];
- map->entries[i].next = &map->entries[i + 1];
- }
-
- map->entries[0].prev = &map->entries[AddressMapper_MAX_ENTRIES - 1];
- map->entries[AddressMapper_MAX_ENTRIES - 1].next = &map->entries[0];
-
- /* set the initial head */
- map->head = &map->entries[0];
-}
-
-struct AddressMapper* AddressMapper_new(struct Allocator* allocator)
-{
- struct AddressMapper* map;
-
- map = Allocator_calloc(allocator, sizeof(struct AddressMapper), 1);
- AddressMapper_init(map);
-
- return map;
-}
-
-/* validate the given address mapper entry to detect linked list corruption then
- * return a pointer to itself.
- * aborts on error
- */
-static inline struct AddressMapper_Entry* entryGet(struct AddressMapper_Entry* entry,
- struct AddressMapper* map)
-{
-#ifdef ENTRY_VALIDATION
- if ((entry == NULL) ||
- (entry < &map->entries[0]) ||
- (entry > &map->entries[AddressMapper_MAX_ENTRIES - 1]) ||
- (((((uintptr_t) entry) - (uintptr_t) &map->entries[0])
- % sizeof(struct AddressMapper_Entry)) != 0)) {
- fprintf(stderr, "AddressMapper: linked list corruption detected.\n");
- abort();
- }
-#endif
- return entry;
-}
-
-/**
- * Move the given entry to the tail of the linked list
- */
-static inline void moveEntryToTail(struct AddressMapper_Entry* entry, struct AddressMapper* map)
-{
- if (ENTRY(entry) == ENTRY(map->head)) {
- map->head = ENTRY(ENTRY(map->head)->next);
- } else {
- /* detach entry */
- ENTRY(ENTRY(entry)->prev)->next = ENTRY(ENTRY(entry)->next);
- ENTRY(ENTRY(entry)->next)->prev = ENTRY(ENTRY(entry)->prev);
-
- /* insert at the tail */
- ENTRY(ENTRY(map->head)->prev)->next = ENTRY(entry);
- ENTRY(entry)->prev = ENTRY(ENTRY(map->head)->prev);
- ENTRY(entry)->next = ENTRY(map->head);
- ENTRY(map->head)->prev = ENTRY(entry);
- }
-}
-
-/**
- * Move the given entry to the head of the linked list.
- *
- * Unlike moveEntryToTail, this function has an optimisation for
- * moving the tail entry to the head, since this is a common case
- */
-static inline void moveEntryToHead(struct AddressMapper_Entry* entry, struct AddressMapper* map)
-{
- if (ENTRY(entry) != ENTRY(map->head)) {
- if (ENTRY(entry) != ENTRY(ENTRY(map->head)->prev)) {
- /* detach entry */
- ENTRY(entry->prev)->next = ENTRY(entry->next);
- ENTRY(entry->next)->prev = ENTRY(entry->prev);
-
- /* insert at the tail */
- ENTRY(ENTRY(map->head)->prev)->next = ENTRY(entry);
- ENTRY(entry)->prev = ENTRY(ENTRY(map->head)->prev);
- ENTRY(entry)->next = ENTRY(map->head);
- ENTRY(map->head)->prev = ENTRY(entry);
- }
- map->head = ENTRY(entry);
- }
-}
-
-/**
- * This is a very hot loop,
- * a large amount of code relies on this being fast so it is a good target for optimization.
- */
-int AddressMapper_indexOf(uint64_t label, struct AddressMapper* map)
-{
- struct AddressMapper_Entry* entry;
-
- /* search from head to tail */
- entry = ENTRY(map->head);
-
- do {
- if (entry->label == label) {
- /* move entry to head */
- moveEntryToHead(entry, map);
- return entryIndexOf(map, entry);
- }
- entry = ENTRY(entry->next);
- } while (ENTRY(entry) != ENTRY(map->head));
-
- return -1;
-}
-
-int AddressMapper_remove(int index, struct AddressMapper* map)
-{
- if ((index >= 0) && (index < AddressMapper_MAX_ENTRIES)) {
- struct AddressMapper_Entry* entry;
- /* move entry to tail */
- entry = ENTRY(&map->entries[index]);
- moveEntryToTail(entry, map);
-
- /* invalidate the entry */
- ENTRY(entry)->label = 0L;
-
- return 0;
- }
-
- return -1;
-}
-
-int AddressMapper_put(uint64_t label, uint8_t address[16], struct AddressMapper* map)
-{
- struct AddressMapper_Entry* tail;
-
- tail = ENTRY(ENTRY(map->head)->prev);
- Bits_memcpyConst(tail->address, address, 16);
- ENTRY(tail)->label = label;
- map->head = tail;
-
- return entryIndexOf(map, tail);
-}
View
94 dht/AddressMapper.h
@@ -1,94 +0,0 @@
-/* vim: set expandtab ts=4 sw=4: */
-/*
- * You may redistribute this program and/or modify it under the terms of
- * the GNU General Public License as published by the Free Software Foundation,
- * either version 3 of the License, or (at your option) any later version.
- *
- * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
- */
-#ifndef AddressMapper_H
-#define AddressMapper_H
-
-/** Address mapper doubly linked list LRU cache.
- * The most recently used item is on the head.
- * The least recently used item is on the tail.
- *
- * When an item is used, it is relocated to the head. Searching from the
- * head therefore means items will be checked in most-recently-used order.
- *
- * When inserting a new item,
- * the item to replace - the least-recently-used item - is simply the tail.
- * Then the tail is set to be the head, because it is the most recently used item.
- */
-
-#include "memory/Allocator.h"
-#include "util/Assert.h"
-#include <stdint.h>
-
-struct AddressMapper_Entry
-{
- uint64_t label;
- uint8_t address[16];
- struct AddressMapper_Entry* next;
- struct AddressMapper_Entry* prev;
-};
-
-// WARNING: Some compilers are not sure what the size of this structure is
-// sometimes they pad it and sometimes they don't
-// it cannot safely be included in other structures!
-#define AddressMapper_MAX_ENTRIES 128
-struct AddressMapper
-{
- struct AddressMapper_Entry entries[AddressMapper_MAX_ENTRIES];
- struct AddressMapper_Entry* head;
-};
-
-/**
- * Allocate a new address map with the given allocator.
- * The map is automatically initialized.
- *
- * @param allocator the allocator to allocate the map from.
- * @return pointer to the newly created map.
- */
-struct AddressMapper* AddressMapper_new(struct Allocator* allocator);
-
-/**
- * Initialize the given address map, ready for use.
- *
- * @param map pointer to an address map to initialize.
- */
-void AddressMapper_init(struct AddressMapper* map);
-
-/* Searches for and returns the index of a map element with the the given label.
- *
- * @param label label to search for in the map.
- * @param map the map to search in.
- * @return the index of the element, or -1 if the label was not found.
- */
-int AddressMapper_indexOf(uint64_t label, struct AddressMapper* map);
-
-/**
- * Remove the address map element with the given index.
- *
- * @param index the index of the element to remove.
- * @param map the map to remove from.
- * @return 0 if the entry is removed, -1 if it could not be found.
- */
-int AddressMapper_remove(int index, struct AddressMapper* map);
-
-/**
- * Put a new element in the map.
- *
- * @param label the label of the new element.
- * @param address the address of the new element.
- * @param map the map to add to.
- */
-int AddressMapper_put(uint64_t label, uint8_t address[16], struct AddressMapper* map);
-
-#endif
View
1  dht/CMakeLists.txt
@@ -19,7 +19,6 @@ add_library(dht
DHTModuleRegistry.c
SerializationModule.c
ReplyModule.c
- AddressMapper.c
)
# This is needed because NACL must be built first because header files are generated.
View
1  dht/dhtcore/VersionList.c
@@ -67,6 +67,7 @@ printf("writing [%d]\n", list->versions[i]);
ver = Endian_hostToBigEndian32(ver);
Writer_write(w, (uint8_t*) &ver, numberSize);
}
+ Writer_write(w, &numberSize, 1);
return out;
}
View
1  dht/test/CMakeLists.txt
@@ -10,7 +10,6 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
set(Test_FILES
- address_mapper_lru_test.c
DHTModules_handleIncoming_test.c
DHTModules_handleOutgoing_test.c
)
View
493 dht/test/address_mapper_lru_test.c
@@ -1,493 +0,0 @@
-/* vim: set expandtab ts=4 sw=4: */
-/*
- * You may redistribute this program and/or modify it under the terms of
- * the GNU General Public License as published by the Free Software Foundation,
- * either version 3 of the License, or (at your option) any later version.
- *
- * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
- */
-#include "memory/MallocAllocator.h"
-#include "dht/AddressMapper.h"
-#include "util/Bits.h"
-
-#include <stdio.h>
-#include <time.h>
-
-/* collection of data required to run tests */
-struct AppState {
- struct AddressMapper* map;
- struct Allocator* allocator;
-};
-
-/* repeat each test this many times to guard against an occasional pass */
-#define NUM_REPEAT 100
-
-/* structure used by tests to report back their information */
-struct TestInfo {
- /* test name */
- const char* name;
- /* set to true if the test passed (default=false) */
- bool pass;
- /* if the test failed then the failure message is given here (default=NULL) */
- const char* failMessage;
-};
-
-/* generates an incremental number for a dummy address */
-static uint8_t* getDummyAddress(void)
-{
- static uint8_t address[16];
-
- if (++address[0] == 0x00) {
- for (int i = 1; i < 15; i++) {
- if (address[i] == 0xFF) {
- address[i + 1]++;
- address[i] = 0;
- } else {
- address[i]++;
- break;
- }
- }
- }
-
- return address;
-}
-
-/* runTest_ReplaceLeastRecentlyUsed
- *
- * Tests that the address mapper replaces the last recently used item
- * when adding a new item to a full address map.
- *
- * 1. Fill the address map
- * 2. Add another entry
- * 3. Check that only the least recent entry was removed
- */
-static int runTest_replaceLeastRecentlyUsed(struct AppState* state,
- struct AddressMapper* map,
- struct TestInfo* info)
-{
- info->name = "Replace least recently used";
-
- /* fill the address map */
- for (unsigned int label = 1; label <= AddressMapper_MAX_ENTRIES; label++) {
- AddressMapper_put(label, getDummyAddress(), map);
- }
-
- /* santity check that the address mapper was filled */
- for (unsigned int label = 1; label <= AddressMapper_MAX_ENTRIES; label++) {
- int index;
-
- index = AddressMapper_indexOf(label, map);
-
- if (index == -1) {
- info->failMessage = "Address mapper could not be filled";
- return 0;
- }
- }
-
- /* add one more entry */
- AddressMapper_put(AddressMapper_MAX_ENTRIES + 1, getDummyAddress(), map);
-
- /* now check that only the first item was evicted from the address map */
-
- for (unsigned int label = 1; label <= AddressMapper_MAX_ENTRIES + 1; label++) {
- int index;
-
- index = AddressMapper_indexOf(label, map);
-
- if ((label != 1) && (index == -1)) {
- info->failMessage = "An item other than the least recently used was removed";
- return 0;
- } else if ((label == 1) && (index != -1)) {
- info->failMessage = "Least recently used item was not removed";
- return 0;
- }
- }
-
- info->pass = true;
- return 0;
-}
-
-/* runTest_removeAndPut
- *
- * Tets the "remove" functionality of the address mapper is optimal by checking
- * that the address mapper replaces "removed" entries when new entries are added,
- * rather than entries that are still valid.
- *
- * 1. Fill the address map
- * 2. Remove a random selection of entries
- * 3. Add new entries to replace the previously removed entries
- * 4. Check that only the removed entries were replaced
- */
-static int runTest_removeAndPut(struct AppState* state,
- struct AddressMapper* map,
- struct TestInfo* info)
-{
- /* the labels to remove - a maximum of 8 */
- uint64_t removeLabels[8];
- info->name = "Remove and put";
-
- /* fill the address map */
- for (unsigned int label = 1; label <= AddressMapper_MAX_ENTRIES; label++) {
- AddressMapper_put(label, getDummyAddress(), map);
- }
-
- /* remove a random number (between 1 and 8) of labels, at random */
- unsigned int numToRemove = (rand() % 8) + 1;
-
- for (unsigned int i = 0; i < numToRemove; i++) {
- generateRandomLabel:
- removeLabels[i] = (rand() % AddressMapper_MAX_ENTRIES) + 1;
- /* check the label is unique */
- for (unsigned int j = 0; j < i; j++) {
- if (removeLabels[j] == removeLabels[i]) {
- goto generateRandomLabel;
- }
- }
-
-
- int index;
- index = AddressMapper_indexOf(removeLabels[i], map);
-
- if (index == -1) {
- info->failMessage = "Could not find item which should have been "
- "present in map";
- return 0;
- }
-
- int result;
- result = AddressMapper_remove(index, map);
-
- if (result != 0) {
- info->failMessage = "Could not remove item which should have been "
- "present in map";
- return 0;
- }
- }
-
- /* put some new items in the map to replace the removed ones */
- for (unsigned int i = 1; i <= numToRemove; i++) {
- AddressMapper_put(AddressMapper_MAX_ENTRIES + i, getDummyAddress(), map);
- }
-
- /* check that the map contents is as expected.
- * check that all of the original items are present apart from the ones
- * that were removed
- */
- for (unsigned int label = 1; label <= AddressMapper_MAX_ENTRIES; label++) {
- /* find out if this item should have been removed */
- bool wasRemoved = false;
-
- for (unsigned int i = 0; i < numToRemove; i++) {
- if (label == removeLabels[i]) {
- wasRemoved = true;
- }
- }
-
- int index;
-
- index = AddressMapper_indexOf(label, map);
-
- if ((wasRemoved == true) && (index != -1)) {
- info->failMessage = "Removed item still exists in the map";
- return 0;
- } else if ((wasRemoved == false) && (index == -1)) {
- info->failMessage = "Non-removed item is missing from the map";
- return 0;
- }
- }
-
- info->pass = true;
- return 0;
-}
-
-/* runTest_dontReplaceMostRecentlyUsed
- *
- * Tests that the address mapper does not replace the most recently used item
- * when adding a new item to a full address map
- *
- * 1. Fill address map with labels 1 .. N
- * 2. Query the index of the least recently used item (label=1) in order to promote it to
- * being the most recently used item
- * 3. Add a new entry to the address map
- * 4. Ensure it is entry with label==2 which has been removed
- */
-static int runTest_dontReplaceMostRecentlyUsed(struct AppState* state,
- struct AddressMapper* map,
- struct TestInfo* info)
-{
- info->name = "Don't replace most recently used";
-
- /* fill the address map such that the item with label==1 is the least recently used */
- for (unsigned int label = 1; label <= AddressMapper_MAX_ENTRIES; label++) {
- AddressMapper_put(label, getDummyAddress(), map);
- }
-
- /* touch item #1. #2 is now the least recently used */
- int index;
- index = AddressMapper_indexOf(1, map);
-
- if (index == -1) {
- info->failMessage = "Failed to fill address map - first item was dropped";
- }
-
- /* add a new entry and check that #1 was not replaced.
- * check that #2 was replaced.
- */
-
- AddressMapper_put(AddressMapper_MAX_ENTRIES + 1, getDummyAddress(), map);
-
- index = AddressMapper_indexOf(1, map);
-
- if (index == -1) {
- info->failMessage = "Address mapper wrongly removed #1 when it was not the "
- "least recently used";
- return 0;
- }
-
- index = AddressMapper_indexOf(2, map);
-
- if (index != -1) {
- info->failMessage = "Address mapper replaced an item other than the "
- "least recently used";
- return 0;
- }
-
- info->pass = true;
- return 0;
-}
-
-static uint64_t rand64(void)
-{
- uint64_t r;
-
- r = (uint32_t) rand();
- r <<= 32;
- r |= (uint32_t) rand();
- return r;
-}
-
-/* runTest_orderCheck
- *
- * Tests that the address mapper properly maintains its entries in order of most
- * recently used and replaces entries in order of least recently used.
- *
- * 1. Fill address mapper
- * 2. Shuffle the contents of the address map
- * 2. Queries the address mapper in a random order
- * 3. Add new entries to the map such that only the most recently used
- * of the previously entries should still be presnt.
- * 4. Query the address map to see if only the most recently used item remains.
- */
-static int runTest_orderCheck(struct AppState* state,
- struct AddressMapper* map,
- struct TestInfo* info)
-{
- info->name = "Order check";
-
- /* the labels used for this test, stored in a random order */
- uint64_t* labels;
- const uint64_t labelMin = 0xF000000000000000;
- const uint64_t labelMax = 0xFFFFFFFFFFFFFFFF;
-
- labels = Allocator_calloc(state->allocator, AddressMapper_MAX_ENTRIES, sizeof(uint64_t));
-
- if (labels == NULL) {
- fprintf(stderr, "Failed to allocate memory\n");
- return -1;
- }
-
- /* now generate random unique labels and add each one to the map */
-
- for (int i = 0; i < AddressMapper_MAX_ENTRIES; i++) {
- generateLabel:
- labels[i] = (rand64() % (labelMax - labelMin + 1)) + labelMin;
-
- /* check the label is unique */
- for (int j = 0; j < i; j++) {
- if (labels[i] == labels[j]) {
- goto generateLabel;
- }
- }
-
- AddressMapper_put(labels[i], getDummyAddress(), map);
- }
-
- /* shuffle the map */
- for (int i = 0; i < AddressMapper_MAX_ENTRIES; i++) {
- int index;
- uint64_t randomLabel;
-
- randomLabel = labels[rand() % AddressMapper_MAX_ENTRIES];
- index = AddressMapper_indexOf(randomLabel, map);
-
- if (index == -1) {
- info->failMessage = "Map has lost an element";
- goto out;
- }
- }
-
- /* query the items in a random order.
- * after this, labels[0] will be the least recently used
- * item and labels[AddressMapper_MAX_ENTRIES - 1] will be the
- * most recently used
- */
- for (int i = 0; i < AddressMapper_MAX_ENTRIES; i++) {
- uint64_t tmp;
- size_t r;
-
- /* swap labels[i] with a random element */
- r = rand() % AddressMapper_MAX_ENTRIES;
-
- tmp = labels[i];
- labels[i] = labels[r];
- labels[r] = tmp;
-
- int index;
- index = AddressMapper_indexOf(labels[i], map);
-
- if (index == -1) {
- info->failMessage = "Map has lost an element";
- goto out;
- }
- }
-
- /* now check the items are replaced in the correct order.
- * The new labels are guaranteed to be unique from the labels already
- * in the map.
- * After this action there should be just one item left,
- * the most recently used item from above - this is the
- * last item in the shuffled array.
- */
- for (int i = 0; i < AddressMapper_MAX_ENTRIES - 1; i++) {
- /* add a new entry */
- uint64_t newLabel = labels[i] - labelMin;
- AddressMapper_put(newLabel, getDummyAddress(), map);
- }
-
- /* verify that only most recently used item remains, as expected */
- for (int i = 0; i < AddressMapper_MAX_ENTRIES; i++) {
- int index;
- index = AddressMapper_indexOf(labels[i], map);
-
- /* items < (AddressMapper_MAX_ENTRIES - 1) should have been replaced as least used */
- if ((i < (AddressMapper_MAX_ENTRIES - 1)) && (index != -1)) {
- info->failMessage = "Element was not removed as expected";
- goto out;
- }
-
- /* item == (AddressMapper_MAX_ENTRIES - 1) should not have been replaced */
- if ((i == (AddressMapper_MAX_ENTRIES - 1)) && (index == -1)) {
- info->failMessage = "Unexpected item was lost";
- goto out;
- }
- }
-
- info->pass = true;
-out:
- return 0;
-}
-
-static void initAppState(struct AppState* state)
-{
- state->allocator = MallocAllocator_new(64 * 1024);
-
- if (state->allocator == NULL) {
- fprintf(stderr, "Failed to create allocator\n");
- abort();
- }
-
- state->map = AddressMapper_new(state->allocator);
-
- if (state->map == NULL) {
- fprintf(stderr, "Failed to create address map\n");
- abort();
- }
-}
-
-static void deinitAppState(struct AppState* state)
-{
- Allocator_free(state->allocator);
-}
-
-/* if the user passed an unsigned integer then use that as a seed,
- * otherwise use the current time
- */
-static void applySeed(int argc, char** argv)
-{
- unsigned int seedValue;
-
- if (argc <= 1) {
- seedValue = time(NULL);
- } else {
- int n;
- n = sscanf(argv[1], "%u", &seedValue);
- if (n != 1) {
- fprintf(stderr, "Invalid seed value: %s\n", argv[1]);
- abort();
- }
- }
-
- srand(seedValue);
-}
-
-/* list of tests to run (array of function pointers) */
-static int (*testRunList[])(struct AppState* state,
- struct AddressMapper* map,
- struct TestInfo* info) =
-{
- runTest_replaceLeastRecentlyUsed,
- runTest_dontReplaceMostRecentlyUsed,
- runTest_removeAndPut,
- runTest_orderCheck,
- NULL
-};
-
-
-int main(int argc, char** argv)
-{
- int err = 0;
- struct AppState state;
-
- applySeed(argc, argv);
-
- int testNum = 0;
- while (testRunList[testNum] != NULL) {
- struct TestInfo info;
- for (int rpt = 0; rpt < NUM_REPEAT; rpt++) {
- int testErr;
-
- Bits_memset(&info, 0, sizeof(info));
- initAppState(&state);
- testErr = testRunList[testNum](&state, state.map, &info);
- deinitAppState(&state);
-
- if (testErr != 0) {
- fprintf(stderr, "Failed to initialise test: %s\n", info.name);
- goto errorExitTest;
- }
-
- /* if the test failed then print its error message now and move on to the next test */
- if (info.pass == false) {
- printf("%s: Failed", info.name);
- if (info.failMessage != NULL) {
- printf(" - %s.", info.failMessage);
- }
- printf("\n");
- /* nonzero exit code if a test has failed */
- err = -1;
- goto nextTest;
- }
- }
- nextTest:
- testNum++;
- }
-
-errorExitTest:
- return err;
-}
View
112 net/Ducttape.c
@@ -114,18 +114,8 @@ static inline uint8_t sendToRouter(struct Node* node,
struct SessionManager_Session* session =
SessionManager_getSession(addr->ip6.bytes, addr->key, context->sm);
- // Whichever has a declared version should transfer it to the other.
- // Since 0 is the default version, if the router doesn't know the version
- // it should not set the session's version to 0.
- if (session->version) {
- node->version = session->version;
- } else if (node->version) {
- session->version = node->version;
- }
- if (session->version && !session->sendHandle_be) {
- // If we have a v1 session and don't know the send handle, reset the session.
- CryptoAuth_reset(&session->iface);
- }
+ node->version = session->version =
+ (node->version > session->version) ? node->version : session->version;
context->session = session;
context->layer = Ducttape_SessionLayer_OUTER;
@@ -251,19 +241,7 @@ static inline uint8_t incomingForMe(struct Message* message,
// Check the checksum.
struct Headers_UDPHeader* uh = (struct Headers_UDPHeader*) message->bytes;
- if (uh->checksum_be == 0) {
- #ifdef Version_0_COMPAT
- uint8_t keyAddr[40];
- Address_printIp(keyAddr, &addr);
- Log_warn(context->logger, "Router packet with blank checksum from [%s].", keyAddr);
- #else
- #ifdef Log_DEBUG
- uint8_t keyAddr[40];
- Address_printIp(keyAddr, &addr);
- Log_debug(context->logger, "Router packet with blank checksum from [%s].", keyAddr);
- #endif
- #endif
- } else if (Checksum_udpIp6(context->ip6Header->sourceAddr, (uint8_t*)uh, message->length)) {
+ if (Checksum_udpIp6(context->ip6Header->sourceAddr, (uint8_t*)uh, message->length)) {
#ifdef Log_DEBUG
uint8_t keyAddr[40];
Address_printIp(keyAddr, &addr);
@@ -326,6 +304,7 @@ uint8_t Ducttape_injectIncomingForMe(struct Message* message,
struct SessionManager_Session s;
AddressCalc_addressForPublicKey(s.ip6, herPublicKey);
+ s.version = Version_CURRENT_PROTOCOL;
context->session = &s;
uint8_t ret = incomingForMe(message, context, herPublicKey);
@@ -381,6 +360,7 @@ static inline uint8_t sendToSwitch(struct Message* message,
}
} else {
debugHandles(context->logger, session, "Sending protocol 0 message");
+ Assert_always(!"wtf why are we communicating with a v0 node?!");
}
Message_shift(message, Headers_SwitchHeader_SIZE);
@@ -595,7 +575,8 @@ static inline int core(struct Message* message, struct Ducttape_pvt* context)
if (Endian_bigEndianToHost32(nonce_be) < 4) {
uint32_t handle_be = ((uint32_t*)message->bytes)[0];
session->sendHandle_be = handle_be | HANDLE_FLAG_BIT_be;
- session->version = (session->version) ? session->version : 1;
+ session->version =
+ (session->version) ? session->version : Version_CURRENT_PROTOCOL;
debugHandles(context->logger, session, "New session, incoming layer3");
}
}
@@ -809,11 +790,7 @@ static uint8_t handleControlMessage(struct Ducttape_pvt* context,
if (Checksum_engine(message->bytes, message->length)) {
Log_info(context->logger, "ctrl packet from [%s] with invalid checksum.", labelStr);
- #ifndef Version_0_COMPAT
- return Error_NONE;
- #endif
- // This will break error responses since they were
- // not sending proper checksums as of 5610464f7bc44ec09ffac81b3507d4df905d6d98
+ return Error_NONE;
}
bool pong = false;
@@ -873,9 +850,7 @@ static uint8_t handleControlMessage(struct Ducttape_pvt* context,
} else if (ctrl->type_be == Control_PING_be) {
Message_shift(message, -Control_HEADER_SIZE);
- #ifdef Version_0_COMPAT
- if (message->length >= Control_Ping_MIN_SIZE) {
- #endif
+
if (message->length < Control_Ping_MIN_SIZE) {
Log_info(context->logger, "dropped runt ping");
return Error_INVALID;
@@ -883,9 +858,6 @@ static uint8_t handleControlMessage(struct Ducttape_pvt* context,
struct Control_Ping* ping = (struct Control_Ping*) message->bytes;
ping->magic = Control_Pong_MAGIC;
ping->version_be = Endian_hostToBigEndian32(Version_CURRENT_PROTOCOL);
- #ifdef Version_0_COMPAT
- }
- #endif
Message_shift(message, Control_HEADER_SIZE);
ctrl->type_be = Control_PONG_be;
@@ -917,11 +889,7 @@ static inline uint8_t* extractPublicKey(struct Message* message,
return NULL;
}
if (*version == 0) {
- #ifndef Version_0_COMPAT
- return NULL;
- #endif
- // version 0 nodes are missing the handle
- Message_shift(message, 4);
+ return NULL;
}
union Headers_CryptoAuth* caHeader = (union Headers_CryptoAuth*) message->bytes;
@@ -965,39 +933,26 @@ static uint8_t incomingFromSwitch(struct Message* message, struct Interface* swi
return handleControlMessage(context, message, switchHeader, switchIf);
}
- if (message->length < 4) {
+ if (message->length < 8) {
Log_info(context->logger, "runt");
return Error_INVALID;
}
// #1 try to get the session using the handle.
uint32_t version = 1;
- uint32_t handle_be = ((uint32_t*)message->bytes)[0];
+ uint32_t handle_be;
+ Message_pop(message, &handle_be, 4);
+ uint32_t nonce = Endian_bigEndianToHost32(((uint32_t*)message->bytes)[0]);
+
struct SessionManager_Session* session = NULL;
- if (handle_be & HANDLE_FLAG_BIT_be) {
+ if (nonce > 3) {
uint32_t realHandle = Endian_bigEndianToHost32(handle_be & ~HANDLE_FLAG_BIT_be);
session = SessionManager_sessionForHandle(realHandle, context->sm);
if (session) {
- if (session->version == 0) {
- uint8_t hex[9];
- Hex_encode(hex, 9, message->bytes, 4);
- Log_debug(context->logger, "0 version session input: [%s] [%u]", hex, realHandle);
- debugHandles(context->logger, session, "Got 0 version session");
- session = NULL;
- version = 0;
- } else {
- debugHandles(context->logger, session, "Got running session");
- Message_shift(message, -4);
- }
- } else {
- version = 0;
+ debugHandles(context->logger, session, "Got running session");
}
- }
-
- // #2 no session, try the message as a handshake.
- if (!session && message->length >= Headers_CryptoAuth_SIZE) {
- Message_shift(message, -4);
+ } else if (message->length >= Headers_CryptoAuth_SIZE) {
uint8_t ip6[16];
uint8_t* herKey = extractPublicKey(message, &version, ip6);
if (herKey) {
@@ -1008,32 +963,6 @@ static uint8_t incomingFromSwitch(struct Message* message, struct Interface* swi
}
}
- // #3 try the message as a protocol version 0 message.
- #ifdef Version_0_COMPAT
- if (!session) {
- int herAddrIndex = AddressMapper_indexOf(switchHeader->label_be, context->addrMap);
- uint8_t* herKey = NULL;
- if (herAddrIndex == -1) {
- uint64_t label = Endian_bigEndianToHost64(switchHeader->label_be);
- struct Node* n = RouterModule_getNode(label, context->routerModule);
- if (n) {
- herAddrIndex = AddressMapper_put(switchHeader->label_be,
- n->address.ip6.bytes,
- context->addrMap);
- herKey = n->address.key;
- }
- }
- if (herAddrIndex != -1) {
- Log_debug(context->logger, "Handling packet from legacy protocol version 0 node.");
- session = SessionManager_getSession(context->addrMap->entries[herAddrIndex].address,
- herKey,
- context->sm);
- session->version = 0;
- }
- }
- #endif
-
-
if (!session) {
#ifdef Log_INFO
uint8_t path[20];
@@ -1091,11 +1020,6 @@ struct Ducttape* Ducttape_register(uint8_t privateKey[32],
context->eventBase = eventBase;
Identity_set(context);
- #ifdef Version_0_COMPAT
- context->addrMap = Allocator_calloc(allocator, sizeof(struct AddressMapper), 1);
- AddressMapper_init(context->addrMap);
- #endif
-
context->ipTunnel = ipTun;
ipTun->nodeInterface.receiveMessage = sendToNode;
View
7 net/Ducttape_pvt.h
@@ -17,9 +17,6 @@
#include "dht/Address.h"
#include "util/version/Version.h"
-#ifdef Version_0_COMPAT
- #include "dht/AddressMapper.h"
-#endif
#include "dht/DHTModule.h"
#include "dht/DHTModuleRegistry.h"
#include "dht/dhtcore/RouterModule.h"
@@ -70,10 +67,6 @@ struct Ducttape_pvt
struct SessionManager* sm;
- #ifdef Version_0_COMPAT
- struct AddressMapper* addrMap;
- #endif
-
struct EventBase* eventBase;
struct Log* logger;
View
14 net/SwitchPinger.c
@@ -82,18 +82,8 @@ static uint8_t receiveMessage(struct Message* msg, struct Interface* iface)
ctx->incomingVersion = Endian_bigEndianToHost32(pongHeader->version_be);
Message_shift(msg, -Control_Pong_HEADER_SIZE);
if (pongHeader->magic != Control_Pong_MAGIC) {
- #ifdef Version_0_COMPAT
- if (pongHeader->magic == Control_Ping_MAGIC) {
- Log_info(ctx->logger, "got pong from legacy version 0 node.");
- ctx->incomingVersion = 0;
- } else {
- Log_debug(ctx->logger, "dropped invalid switch pong");
- return Error_INVALID;
- }
- #else
- Log_debug(ctx->logger, "dropped invalid switch pong");
- return Error_INVALID;
- #endif
+ Log_debug(ctx->logger, "dropped invalid switch pong");
+ return Error_INVALID;
}
} else {
Log_debug(ctx->logger, "got runt pong message, length: [%d]", msg->length);
View
4 test/cjdroute_routerPing_test.c
@@ -33,7 +33,7 @@ uint8_t catchResponse(struct Message* msg, struct Interface* iface)
int main()
{
- char* pingBenc = "d1:q4:ping4:txid4:abcde";
+ char* pingBenc = "d1:q4:ping4:txid4:abcd1:pi2ee";
struct Allocator* alloc = CanaryAllocator_new(MallocAllocator_new(1<<22), NULL);
struct TestFramework* tf = TestFramework_setUp("0123456789abcdefghijklmnopqrstuv", alloc, NULL);
struct Ducttape_pvt* dt = Identity_cast((struct Ducttape_pvt*) tf->ducttape);
@@ -71,7 +71,7 @@ int main()
udp->checksum_be = 0;
struct Message m2 = { .bytes = buff, .length = buffLen, .padding = 0 };
Ducttape_injectIncomingForMe(&m2, &dt->public, herPublicKey);
- Assert_always(dt->switchInterface.receiverContext);
+ Assert_always(!dt->switchInterface.receiverContext);
// good checksum
udp->checksum_be =
View
5 util/version/Version.h
@@ -130,7 +130,7 @@
* ----------------------------------
*
* Version 2:
- * Future
+ * February 21, 2013
*/
#define Version_isCompat2(x, y) \
(Version_isCompat1(x, y) || (x == 2 && y > 0))
@@ -154,8 +154,7 @@
/**
* The current protocol version.
*/
-#define Version_CURRENT_PROTOCOL 1
-#define Version_0_COMPAT
+#define Version_CURRENT_PROTOCOL 2
#define Version_1_COMPAT
Please sign in to comment.
Something went wrong with that request. Please try again.