Skip to content

Commit

Permalink
Update to protocol v3
Browse files Browse the repository at this point in the history
The main changes are that objects are now sent with a single message for
all types and that the submission time has been replaced with an
expiry time. Because the object type is now directly in the message,
the object store no longer prepends it to the files.

The timestamp protocol type has been removed because they are now
always 64-bit.

The signatures for public keys and messages now include the entire
object header apart from the nonce.

The timestamp is sadly no longer included in the message objects so
instead it now just writes out the current system time when saving a
message in maildir format.
  • Loading branch information
bpeel committed Dec 1, 2017
1 parent 76381db commit 36bdc82
Show file tree
Hide file tree
Showing 15 changed files with 386 additions and 660 deletions.
12 changes: 2 additions & 10 deletions src/ntb-blob.c
Expand Up @@ -29,18 +29,12 @@
#include "ntb-util.h"

void
ntb_blob_dynamic_init(struct ntb_buffer *buffer,
enum ntb_proto_inv_type type)
ntb_blob_dynamic_init(struct ntb_buffer *buffer)
{
struct ntb_blob *blob;

ntb_buffer_init(buffer);

ntb_buffer_set_length(buffer,
NTB_STRUCT_OFFSET(struct ntb_blob, data));

blob = (struct ntb_blob *) buffer->data;
blob->type = type;
}

struct ntb_blob *
Expand All @@ -55,14 +49,12 @@ ntb_blob_dynamic_end(struct ntb_buffer *buffer)
}

struct ntb_blob *
ntb_blob_new(enum ntb_proto_inv_type type,
const void *data,
ntb_blob_new(const void *data,
size_t size)
{
struct ntb_blob *blob =
ntb_alloc(NTB_STRUCT_OFFSET(struct ntb_blob, data) + size);

blob->type = type;
blob->size = size;

ntb_ref_count_init(&blob->ref_count);
Expand Down
8 changes: 2 additions & 6 deletions src/ntb-blob.h
Expand Up @@ -38,8 +38,6 @@
* disk. */

struct ntb_blob {
enum ntb_proto_inv_type type;

struct ntb_ref_count ref_count;

size_t size;
Expand All @@ -49,15 +47,13 @@ struct ntb_blob {
};

void
ntb_blob_dynamic_init(struct ntb_buffer *buffer,
enum ntb_proto_inv_type type);
ntb_blob_dynamic_init(struct ntb_buffer *buffer);

struct ntb_blob *
ntb_blob_dynamic_end(struct ntb_buffer *buffer);

struct ntb_blob *
ntb_blob_new(enum ntb_proto_inv_type type,
const void *data,
ntb_blob_new(const void *data,
size_t size);

struct ntb_blob *
Expand Down
171 changes: 8 additions & 163 deletions src/ntb-connection.c
@@ -1,6 +1,6 @@
/*
* Notbit - A Bitmessage client
* Copyright (C) 2013 Neil Roberts
* Copyright (C) 2013, 2017 Neil Roberts
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
Expand Down Expand Up @@ -228,7 +228,7 @@ addr_command_handler(struct ntb_connection *conn,
ntb_proto_get_command(data,
command_length,

NTB_PROTO_ARGUMENT_TIMESTAMP,
NTB_PROTO_ARGUMENT_64,
&event.timestamp,

NTB_PROTO_ARGUMENT_32,
Expand Down Expand Up @@ -308,7 +308,7 @@ version_command_handler(struct ntb_connection *conn,
NTB_PROTO_ARGUMENT_64,
&event.services,

NTB_PROTO_ARGUMENT_TIMESTAMP,
NTB_PROTO_ARGUMENT_64,
&event.timestamp,

NTB_PROTO_ARGUMENT_64,
Expand Down Expand Up @@ -353,164 +353,15 @@ verack_command_handler(struct ntb_connection *conn,
}

static bool
getpubkey_command_handler(struct ntb_connection *conn,
const uint8_t *data,
uint32_t command_length)
{
struct ntb_connection_object_event event;
ssize_t header_length;
uint64_t address_version;

event.type = NTB_PROTO_INV_TYPE_GETPUBKEY;
event.object_data_length = command_length;
event.object_data = data;

header_length = ntb_proto_get_command(data,
command_length,

NTB_PROTO_ARGUMENT_64,
&event.nonce,

NTB_PROTO_ARGUMENT_TIMESTAMP,
&event.timestamp,

NTB_PROTO_ARGUMENT_VAR_INT,
&address_version,

NTB_PROTO_ARGUMENT_VAR_INT,
&event.stream_number,

NTB_PROTO_ARGUMENT_END);

if (header_length == -1) {
ntb_log("Invalid getpubkey command received from %s",
conn->remote_address_string);
set_error_state(conn);
return false;
}

return emit_event(conn,
NTB_CONNECTION_EVENT_OBJECT,
&event.base);
}

static bool
pubkey_command_handler(struct ntb_connection *conn,
object_command_handler(struct ntb_connection *conn,
const uint8_t *data,
uint32_t command_length)
{
struct ntb_connection_object_event event;
ssize_t header_length;
uint64_t address_version;

event.type = NTB_PROTO_INV_TYPE_PUBKEY;
event.object_data_length = command_length;
event.object_data = data;

header_length = ntb_proto_get_command(data,
command_length,

NTB_PROTO_ARGUMENT_64,
&event.nonce,

NTB_PROTO_ARGUMENT_TIMESTAMP,
&event.timestamp,

NTB_PROTO_ARGUMENT_VAR_INT,
&address_version,

NTB_PROTO_ARGUMENT_VAR_INT,
&event.stream_number,

NTB_PROTO_ARGUMENT_END);

if (header_length == -1) {
ntb_log("Invalid pubkey command received from %s",
conn->remote_address_string);
set_error_state(conn);
return false;
}

return emit_event(conn,
NTB_CONNECTION_EVENT_OBJECT,
&event.base);
}

static bool
msg_command_handler(struct ntb_connection *conn,
const uint8_t *data,
uint32_t command_length)
{
struct ntb_connection_object_event event;
ssize_t header_length;

event.type = NTB_PROTO_INV_TYPE_MSG;
event.object_data_length = command_length;
event.object_data = data;

header_length = ntb_proto_get_command(data,
command_length,

NTB_PROTO_ARGUMENT_64,
&event.nonce,

NTB_PROTO_ARGUMENT_TIMESTAMP,
&event.timestamp,

NTB_PROTO_ARGUMENT_VAR_INT,
&event.stream_number,

NTB_PROTO_ARGUMENT_END);

if (header_length == -1) {
ntb_log("Invalid msg command received from %s",
conn->remote_address_string);
set_error_state(conn);
return false;
}

return emit_event(conn,
NTB_CONNECTION_EVENT_OBJECT,
&event.base);
}

static bool
broadcast_command_handler(struct ntb_connection *conn,
const uint8_t *data,
uint32_t command_length)
{
struct ntb_connection_object_event event;
ssize_t header_length;
uint64_t broadcast_version;

event.type = NTB_PROTO_INV_TYPE_BROADCAST;
event.object_data_length = command_length;
event.object_data = data;

header_length = ntb_proto_get_command(data,
command_length,

NTB_PROTO_ARGUMENT_64,
&event.nonce,

NTB_PROTO_ARGUMENT_TIMESTAMP,
&event.timestamp,

NTB_PROTO_ARGUMENT_VAR_INT,
&broadcast_version,

NTB_PROTO_ARGUMENT_VAR_INT,
&event.stream_number,

NTB_PROTO_ARGUMENT_END);

if (header_length == -1) {
ntb_log("Invalid msg command received from %s",
conn->remote_address_string);
set_error_state(conn);
return false;
}

return emit_event(conn,
NTB_CONNECTION_EVENT_OBJECT,
&event.base);
Expand Down Expand Up @@ -563,10 +414,7 @@ static const struct {
const uint8_t *data,
uint32_t command_length);
} command_handlers[] = {
{ "getpubkey", getpubkey_command_handler },
{ "pubkey", pubkey_command_handler },
{ "msg", msg_command_handler },
{ "broadcast", broadcast_command_handler },
{ "object", object_command_handler },
{ "inv", inv_command_handler },
{ "version", version_command_handler },
{ "addr", addr_command_handler },
Expand Down Expand Up @@ -825,8 +673,6 @@ static void
add_ready_objects(struct ntb_connection *conn)
{
struct ntb_connection_queue_entry *entry;
enum ntb_proto_inv_type type;
const char *command_name;
size_t command_start;

/* Keep adding objects until we either run out or we've filled
Expand All @@ -839,11 +685,9 @@ add_ready_objects(struct ntb_connection *conn)
entry = ntb_container_of(conn->ready_objects.next,
struct ntb_connection_queue_entry,
link);
type = entry->blob->type;
command_name = ntb_proto_get_command_name_for_type(type);

command_start = conn->out_buf.length;
ntb_proto_begin_command(&conn->out_buf, command_name);
ntb_proto_begin_command(&conn->out_buf, "object");
ntb_buffer_append(&conn->out_buf,
entry->blob->data,
entry->blob->size);
Expand Down Expand Up @@ -1173,7 +1017,8 @@ ntb_connection_send_version(struct ntb_connection *conn,
NTB_PROTO_ARGUMENT_64,
NTB_PROTO_SERVICES,

NTB_PROTO_ARGUMENT_TIMESTAMP,
NTB_PROTO_ARGUMENT_64,
ntb_main_context_get_wall_clock(NULL),

NTB_PROTO_ARGUMENT_64,
NTB_PROTO_SERVICES,
Expand Down
6 changes: 0 additions & 6 deletions src/ntb-connection.h
Expand Up @@ -71,12 +71,6 @@ struct ntb_connection_version_event {
struct ntb_connection_object_event {
struct ntb_connection_event base;

enum ntb_proto_inv_type type;

uint64_t nonce;
int64_t timestamp;
uint64_t stream_number;

const uint8_t *object_data;
size_t object_data_length;
};
Expand Down

0 comments on commit 36bdc82

Please sign in to comment.