Beginnings of the Record protobuf and more changes to dialer

This commit is contained in:
John Jones 2017-02-16 18:49:17 -05:00
parent ccc7ca3e8b
commit c29c5744b8
8 changed files with 72 additions and 12 deletions

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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);
};
/**

View file

@ -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

View file

@ -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);

View file

@ -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);
}

View file

@ -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)
{

View file

@ -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;