Skip to content

Commit

Permalink
Add cmd arg to salt swarm hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
inetic committed Jun 13, 2017
1 parent 654e78f commit 647ab32
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 22 deletions.
6 changes: 4 additions & 2 deletions build.sh
Expand Up @@ -55,16 +55,18 @@ echo "Building HTTP test server..."
clang $CFLAGS -o test_server test_server.c \
-I ./Libevent/include ./Libevent/.libs/libevent.a

COMMON_C=(log.c icmp_handler.c network.c sha1.c timer.c utp_bufferevent.c http_util.c config.c)

echo "Building injector..."
clang $CFLAGS -o injector injector.c log.c icmp_handler.c network.c sha1.c timer.c utp_bufferevent.c http_util.c dht.o \
clang $CFLAGS -o injector injector.c "${COMMON_C[@]}" dht.o \
$INC libutp/libutp.a ./Libevent/.libs/libevent.a ./Libevent/.libs/libevent_pthreads.a \
./libsodium/src/libsodium/.libs/libsodium.a \
./libbtdht/libbtdht.a ./libbtdht/btutils/libbtutils.a \
`pkg-config --cflags libevent` \
-lstdc++ $LRT $LM $LB

echo "Building injector_helper..."
clang $CFLAGS -o injector_helper injector_helper.c log.c icmp_handler.c network.c sha1.c timer.c utp_bufferevent.c http_util.c dht.o \
clang $CFLAGS -o injector_helper injector_helper.c "${COMMON_C[@]}" dht.o \
$INC libutp/libutp.a ./Libevent/.libs/libevent.a ./Libevent/.libs/libevent_pthreads.a \
./libsodium/src/libsodium/.libs/libsodium.a ./libbtdht/libbtdht.a ./libbtdht/btutils/libbtutils.a \
`pkg-config --cflags libevent` \
Expand Down
54 changes: 54 additions & 0 deletions config.c
@@ -0,0 +1,54 @@
#include "config.h"
#include "sha1.h"
#include "string.h"
#include <stdlib.h>
#include <stdio.h>

struct config {
byte* injector_swarm;
byte* injector_proxy_swarm;
};

static byte* compute_swarm(const char* base, const char* salt)
{
SHA1_CTX ctx;
SHA1Init(&ctx);
SHA1Update(&ctx, (byte*) base, strlen(base));

if (salt) {
SHA1Update(&ctx, (byte*) salt, strlen(salt));
}

byte* hash_out = malloc(20);

SHA1Final(hash_out, &ctx);

return hash_out;
}

config* config_new(const char *swarm_salt)
{
config *c = malloc(sizeof(config));

c->injector_swarm = compute_swarm("injectors", swarm_salt);
c->injector_proxy_swarm = compute_swarm("injector proxies", swarm_salt);

return c;
}

void config_delete(config *c)
{
free(c->injector_swarm);
free(c->injector_proxy_swarm);
free(c);
}

const byte* injector_swarm(config* c)
{
return c->injector_swarm;
}

const byte* injector_proxy_swarm(config* c)
{
return c->injector_proxy_swarm;
}
14 changes: 14 additions & 0 deletions config.h
@@ -0,0 +1,14 @@
#ifndef __CONFIG_H__
#define __CONFIG_H__

#include "libutp/utp_types.h" // byte

typedef struct config config;

config *config_new(const char *swarm_salt);
void config_delete(config*);

const byte* injector_swarm(config*);
const byte* injector_proxy_swarm(config*);

#endif // __CONFIG_H__
2 changes: 0 additions & 2 deletions constants.h

This file was deleted.

21 changes: 13 additions & 8 deletions injector.c
Expand Up @@ -16,7 +16,6 @@
#include "utp.h"
#include "timer.h"
#include "network.h"
#include "constants.h"
#include "utp_bufferevent.h"
#include "http_util.h"

Expand Down Expand Up @@ -267,10 +266,11 @@ void usage(char *name)
fprintf(stderr, " %s [options] -p <listening-port>\n", name);
fprintf(stderr, "\n");
fprintf(stderr, "Options:\n");
fprintf(stderr, " -h Help\n");
fprintf(stderr, " -p <port> Local port\n");
fprintf(stderr, " -s <IP> Source IP\n");
fprintf(stderr, " -d Print debug output\n");
fprintf(stderr, " -h Help\n");
fprintf(stderr, " -p <port> Local port\n");
fprintf(stderr, " -s <IP> Source IP\n");
fprintf(stderr, " -d Print debug output\n");
fprintf(stderr, " -a <swarm-salt> Use <swarm-salt> to calculate swarm locations.\n");
fprintf(stderr, "\n");
exit(1);
}
Expand All @@ -279,9 +279,10 @@ int main(int argc, char *argv[])
{
char *address = "0.0.0.0";
char *port = NULL;
const char* swarm_salt = "";

for (;;) {
int c = getopt(argc, argv, "hp:s:nd");
int c = getopt(argc, argv, "hp:s:nda:");
if (c == -1)
break;
switch (c) {
Expand All @@ -297,6 +298,9 @@ int main(int argc, char *argv[])
case 'd':
o_debug++;
break;
case 'a':
swarm_salt = optarg;
break;
default:
die("Unhandled argument: %c\n", c);
}
Expand All @@ -306,12 +310,13 @@ int main(int argc, char *argv[])
usage(argv[0]);
}

network *n = network_setup(address, port);
config *conf = config_new(swarm_salt);
network *n = network_setup(address, conf, port);

utp_set_callback(n->utp, UTP_ON_ACCEPT, &utp_on_accept);

timer_callback cb = ^{
dht_announce(n->dht, injector_swarm, ^(const byte *peers, uint num_peers) {
dht_announce(n->dht, injector_swarm(n->conf), ^(const byte *peers, uint num_peers) {
if (!peers) {
printf("announce complete\n");
}
Expand Down
21 changes: 13 additions & 8 deletions injector_helper.c
Expand Up @@ -39,7 +39,6 @@ handle_connection(utp_socket *s)

#include "log.h"
#include "timer.h"
#include "constants.h"
#include "network.h"
#include "utp_bufferevent.h"
#include "http_util.h"
Expand Down Expand Up @@ -209,7 +208,7 @@ static void start_announcing_self_in_dht(proxy *p)
if (p->announce_timer) return;

timer_callback do_announce = ^{
dht_announce(p->net->dht, injector_proxy_swarm, ^(const byte *peers, uint num_peers) {
dht_announce(p->net->dht, injector_proxy_swarm(p->net->conf), ^(const byte *peers, uint num_peers) {
if (!peers) {
LOG("announce to injector_proxy_swarm complete\n");
}
Expand Down Expand Up @@ -694,7 +693,7 @@ static void start_injector_search(proxy *p)
return;
}

dht_get_peers(p->net->dht, injector_swarm,
dht_get_peers(p->net->dht, injector_swarm(p->net->conf),
^(const byte *peers, uint num_peers) {
// TODO: Ensure safety after p is destroyed.
on_injectors_found(p, peers, num_peers);
Expand Down Expand Up @@ -825,23 +824,25 @@ void usage(char *name)
fprintf(stderr, " %s [options]\n", name);
fprintf(stderr, "\n");
fprintf(stderr, "Options:\n");
fprintf(stderr, " -h Help\n");
fprintf(stderr, " -i A.B.C.D:P Disable injector DHT search and use this endpoint instead\n");
fprintf(stderr, " -d Pring debug messages\n");
fprintf(stderr, " -h Help\n");
fprintf(stderr, " -i A.B.C.D:P Disable injector DHT search and use this endpoint instead\n");
fprintf(stderr, " -d Pring debug messages\n");
fprintf(stderr, " -a <swarm-salt> Use <swarm-salt> to calculate swarm locations.\n");
fprintf(stderr, "\n");
exit(1);
}

int main(int argc, char *argv[])
{
char *address = "0.0.0.0";
const char *swarm_salt = "";

endpoint debug_injector = zero_endpoint;

bool print_debug = false;

for (;;) {
int c = getopt(argc, argv, "hi:d");
int c = getopt(argc, argv, "hi:da:");
if (c == -1)
break;
switch (c) {
Expand Down Expand Up @@ -870,12 +871,16 @@ int main(int argc, char *argv[])
}
break;
}
case 'a':
swarm_salt = optarg;
break;
default:
die("Unhandled argument: %c\n", c);
}
}

network *n = network_setup(address, UTP_LISTENING_PORT);
config *c = config_new(swarm_salt);
network *n = network_setup(address, c, UTP_LISTENING_PORT);

proxy *p = proxy_create(n, debug_injector, print_debug);

Expand Down
5 changes: 4 additions & 1 deletion network.c
Expand Up @@ -111,7 +111,7 @@ void evdns_log_cb(int severity, const char *msg)
debug("[evdns] %d %s\n", severity, msg);
}

network* network_setup(char *address, char *port)
network* network_setup(char *address, config *conf, char *port)
{
signal(SIGPIPE, SIG_IGN);

Expand All @@ -129,6 +129,8 @@ network* network_setup(char *address, char *port)

network *n = alloc(network);

n->conf = conf;

n->fd = socket(((struct sockaddr*)res->ai_addr)->sa_family, SOCK_DGRAM, IPPROTO_UDP);
if (n->fd < 0) {
pdie("socket");
Expand Down Expand Up @@ -272,6 +274,7 @@ int network_loop(network *n)
utp_destroy(n->utp);
dht_destroy(n->dht);
close(n->fd);
config_delete(n->conf);
evhttp_free(n->http);
free(n);

Expand Down
4 changes: 3 additions & 1 deletion network.h
Expand Up @@ -8,6 +8,7 @@

#include "utp.h"
#include "dht.h"
#include "config.h"


#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y))
Expand All @@ -29,9 +30,10 @@ typedef struct {
utp_context *utp;
dht *dht;
evhttp *http;
config *conf;
} network;

network* network_setup(char *address, char *port);
network* network_setup(char *address, config*, char *port);
int network_loop(network *n);

#endif // __NETWORK_H__

0 comments on commit 647ab32

Please sign in to comment.