From c29c5744b88f565eedb48bea1ad42eb011f80d4c Mon Sep 17 00:00:00 2001 From: John Jones Date: Thu, 16 Feb 2017 18:49:17 -0500 Subject: [PATCH] Beginnings of the Record protobuf and more changes to dialer --- conn/dialer.c | 2 +- conn/tcp_transport_dialer.c | 17 ++++++++-- include/libp2p/conn/connection.h | 2 +- include/libp2p/net/p2pnet.h | 2 +- include/libp2p/record/record.h | 55 +++++++++++++++++++++++++++++--- net/socket.c | 2 +- record/record.c | 2 ++ test/test_secio.h | 2 +- 8 files changed, 72 insertions(+), 12 deletions(-) diff --git a/conn/dialer.c b/conn/dialer.c index 1fb2094..dceb261 100644 --- a/conn/dialer.c +++ b/conn/dialer.c @@ -67,7 +67,7 @@ void libp2p_conn_dialer_free(struct Dialer* in) { struct Connection* libp2p_conn_dialer_get_connection(struct Dialer* dialer, struct MultiAddress* multiaddress) { struct Connection* conn = libp2p_conn_transport_dialer_get(dialer->transport_dialers, multiaddress); if (conn == NULL) { - conn = libp2p_conn_connection_new(dialer->fallback_dialer, multiaddress); + conn = dialer->fallback_dialer->dial(dialer->fallback_dialer, multiaddress); } return conn; } diff --git a/conn/tcp_transport_dialer.c b/conn/tcp_transport_dialer.c index 9286459..3b0dc7e 100644 --- a/conn/tcp_transport_dialer.c +++ b/conn/tcp_transport_dialer.c @@ -25,12 +25,12 @@ struct TcpIp* libp2p_conn_parse_ip_multiaddress(struct MultiAddress* addr) { int pos = 0; while (tok != NULL) { switch (pos) { - case 2: { + case 1: { out->ip = malloc(strlen(tok) + 1); strcpy(out->ip, tok); break; } - case 4: { + case 3: { out->port = strtol(tok, NULL, 10); break; } @@ -46,7 +46,18 @@ int libp2p_conn_tcp_can_handle(struct MultiAddress* addr) { return 1; } +int libp2p_conn_tcp_read(const struct Connection* connection, char** out, size_t* num_bytes) { + int buffer_size = 65535; + *out = (char*)malloc(buffer_size); + ssize_t bytes = socket_read(connection->socket_handle, *out, buffer_size, 0); + *num_bytes = bytes; + return bytes > 0; +} +int libp2p_conn_tcp_write(const struct Connection* connection, const char* in, size_t num_bytes) { + ssize_t bytes = socket_write(connection->socket_handle, in, num_bytes, 0); + return bytes == num_bytes; +} struct Connection* libp2p_conn_tcp_dial(struct TransportDialer* transport_dialer, struct MultiAddress* addr) { struct Connection* conn = (struct Connection*) malloc(sizeof(struct Connection*)); @@ -55,6 +66,8 @@ struct Connection* libp2p_conn_tcp_dial(struct TransportDialer* transport_dialer struct hostent* host = gethostbyname(results->ip); struct in_addr** addr_list = (struct in_addr**)host->h_addr_list; socket_connect4(conn->socket_handle, (*addr_list[0]).s_addr, results->port); + conn->read = libp2p_conn_tcp_read; + conn->write = libp2p_conn_tcp_write; return conn; } diff --git a/include/libp2p/conn/connection.h b/include/libp2p/conn/connection.h index d4da1c9..78a24ac 100644 --- a/include/libp2p/conn/connection.h +++ b/include/libp2p/conn/connection.h @@ -24,7 +24,7 @@ struct Connection { * @param out_size the number of bytes to write * @returns 0 on success, otherwise an error code */ - int (*write)(const struct Connection* conn, char* out, size_t out_size); + int (*write)(const struct Connection* conn, const char* out, size_t out_size); }; /** diff --git a/include/libp2p/net/p2pnet.h b/include/libp2p/net/p2pnet.h index 57b0646..132eaf3 100644 --- a/include/libp2p/net/p2pnet.h +++ b/include/libp2p/net/p2pnet.h @@ -11,7 +11,7 @@ int socket_connect4(int s, uint32_t ip, uint16_t port); int socket_listen(int s, uint32_t *localip, uint16_t *localport); ssize_t socket_read(int s, char *buf, size_t len, int flags); - ssize_t socket_write(int s, char *buf, size_t len, int flags); + ssize_t socket_write(int s, const char *buf, size_t len, int flags); /** * Used to send the size of the next transmission for "framed" transmissions. NOTE: This will send in big endian format * @param s the socket descriptor diff --git a/include/libp2p/record/record.h b/include/libp2p/record/record.h index 762668d..f20a02e 100644 --- a/include/libp2p/record/record.h +++ b/include/libp2p/record/record.h @@ -1,7 +1,52 @@ -#ifndef LIBP2P_RECORD_H - #define LIBP2P_RECORD_H +#pragma once - #define RECORD_BUFSIZE 4096 +struct Libp2pRecord { + // the key that references this record + char* key; + // the actual value this record is storing + unsigned char* value; + // hash of the author's public key + char* author; + // a PKI signature for the key + value + author + unsigned char* signature; + // time the record was received, set by receiver + char* time_received; +}; - int libp2p_record_make_put_record (char** record, size_t *rec_size, struct RsaPrivateKey* sk, char* key, char* value, size_t vlen, int sign); -#endif // LIBP2P_RECORD_H +/** + * Convert a Libp2pRecord into protobuf format + * @param in the Libp2pRecord to convert + * @param buffer where to store the protobuf + * @param max_buffer_size the size of the allocated buffer + * @param bytes_written the size written into buffer + * @returns true(1) on success, otherwise false(0) + */ +int libp2p_record_protobuf_encode(const struct Libp2pRecord* in, unsigned char* buffer, size_t max_buffer_size, size_t* bytes_written); + +/** + * Generates an estimate of the buffer size needed to encode the struct + * @param in the Libp2pRecord that you want to encode + * @returns the approximate number of bytes required + */ +size_t libp2p_record_protobuf_encode_size(const struct Libp2pRecord* in); + +/** + * Convert a protobuf byte array into a Libp2pRecord + * @param in the byte array + * @param in_size the size of the byte array + * @param out a pointer to the new Libp2pRecord + * @returns true(1) on success, otherwise false(0) + */ +int libp2p_record_protobuf_decode(const unsigned char* in, size_t in_size, struct Libp2pRecord** out); + +/** + * This method does all the hard stuff in one step. It fills a Libp2pRecord struct, and converts it into a protobuf + * @param record a pointer to the protobuf results + * @param rec_size the number of bytes used in the area pointed to by record + * @param sk the private key used to sign + * @param key the key in the Libp2pRecord + * @param value the value in the Libp2pRecord + * @param vlen the length of value + * @param sign ?? + */ +int libp2p_record_make_put_record (char** record, size_t *rec_size, struct RsaPrivateKey* sk, char* key, char* value, size_t vlen, int sign); diff --git a/net/socket.c b/net/socket.c index c22ad7a..f6ad619 100644 --- a/net/socket.c +++ b/net/socket.c @@ -116,7 +116,7 @@ ssize_t socket_read(int s, char *buf, size_t len, int flags) /* Same reason as socket_read, but to send data instead of receive. */ -ssize_t socket_write(int s, char *buf, size_t len, int flags) +ssize_t socket_write(int s, const char *buf, size_t len, int flags) { return send(s, buf, len, flags); } diff --git a/record/record.c b/record/record.c index c8733af..ea38337 100644 --- a/record/record.c +++ b/record/record.c @@ -7,6 +7,8 @@ #include "mh/hashes.h" #include "mh/multihash.h" +#define RECORD_BUFSIZE 6048 + // libp2p_record_make_put_record creates and signs a dht record for the given key/value pair int libp2p_record_make_put_record (char** record, size_t *rec_size, struct RsaPrivateKey* sk, char* key, char* value, size_t vlen, int sign) { diff --git a/test/test_secio.h b/test/test_secio.h index 11cb89b..e1d7b11 100644 --- a/test/test_secio.h +++ b/test/test_secio.h @@ -2,7 +2,7 @@ #include "libp2p/secio/secio.h" #include "libp2p/net/multistream.h" - +#include "libp2p/net/p2pnet.h" int test_secio_handshake() { int retVal = 0;