Skip to content

Commit

Permalink
signing and response header
Browse files Browse the repository at this point in the history
  • Loading branch information
ghazel committed May 15, 2017
1 parent 5aafc3f commit efa5000
Show file tree
Hide file tree
Showing 8 changed files with 313 additions and 11 deletions.
148 changes: 148 additions & 0 deletions base64.c
@@ -0,0 +1,148 @@
/*
* Base64 encoding/decoding (RFC1341)
* Copyright (c) 2005-2011, Jouni Malinen <j@w1.fi>
*
* This software may be distributed under the terms of the BSD license.
* See README for more details.
*/

#include <stdlib.h>
#include <string.h>

#include "base64.h"

static const unsigned char base64_table[65] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

static const unsigned char base64_urlsafe_table[65] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";

/**
* base64_encode - Base64 encode
* @src: Data to be encoded
* @len: Length of the data to be encoded
* @out_len: Pointer to output length variable, or %NULL if not used
* Returns: Allocated buffer of out_len bytes of encoded data,
* or %NULL on failure
*
* Caller is responsible for freeing the returned buffer. Returned buffer is
* nul terminated to make it easier to use as a C string. The nul terminator is
* not included in out_len.
*/
char* base64_table_encode(const unsigned char *table, const unsigned char *src, size_t len, size_t *out_len)
{
size_t olen = len * 4 / 3 + 4; /* 3-byte blocks to 4-byte */
olen += olen / 72; /* line feeds */
olen++; /* nul termination */
if (olen < len) {
return NULL; /* integer overflow */
}
unsigned char *out = malloc(olen);
if (!out) {
return NULL;
}

const unsigned char *end = src + len;
const unsigned char *in = src;
unsigned char *pos = out;
while (end - in >= 3) {
*pos++ = table[in[0] >> 2];
*pos++ = table[((in[0] & 0x03) << 4) | (in[1] >> 4)];
*pos++ = table[((in[1] & 0x0f) << 2) | (in[2] >> 6)];
*pos++ = table[in[2] & 0x3f];
in += 3;
}

if (end - in) {
*pos++ = table[in[0] >> 2];
if (end - in == 1) {
*pos++ = table[(in[0] & 0x03) << 4];
} else {
*pos++ = table[((in[0] & 0x03) << 4) | (in[1] >> 4)];
*pos++ = table[(in[1] & 0x0f) << 2];
}
}

*pos = '\0';
if (out_len) {
*out_len = pos - out;
}
return (char*)out;
}

char* base64_encode(const unsigned char *src, size_t len, size_t *out_len)
{
return base64_table_encode(base64_table, src, len, out_len);
}

char* base64_urlsafe_encode(const unsigned char *src, size_t len, size_t *out_len)
{
return base64_table_encode(base64_urlsafe_table, src, len, out_len);
}


/**
* base64_decode - Base64 decode
* @src: Data to be decoded
* @len: Length of the data to be decoded
* @out_len: Pointer to output length variable
* Returns: Allocated buffer of out_len bytes of decoded data,
* or %NULL on failure
*
* Caller is responsible for freeing the returned buffer.
*/
unsigned char* base64_decode(const unsigned char *src, size_t len, size_t *out_len)
{
size_t i;

static unsigned char dtable[256] = {0};
if (!dtable[0]) {
memset(dtable, 0x80, 256);
for (i = 0; i < sizeof(base64_table) - 1; i++) {
dtable[base64_table[i]] = (unsigned char)i;
if (base64_urlsafe_table[i] != base64_table[i]) {
dtable[base64_urlsafe_table[i]] = (unsigned char)i;
}
}
dtable['='] = 0;
}

size_t count = 0;
for (i = 0; i < len; i++) {
if (dtable[src[i]] != 0x80) {
count++;
}
}

if (count == 0 || count % 4) {
return NULL;
}

size_t olen = count / 4 * 3;
unsigned char *out = malloc(olen);
if (!out) {
return NULL;
}
unsigned char *pos = out;

count = 0;
unsigned char block[4];
for (i = 0; i < len; i++) {
unsigned char tmp = dtable[src[i]];
if (tmp == 0x80) {
continue;
}

block[count] = tmp;
count++;
if (count == 4) {
*pos++ = (block[0] << 2) | (block[1] >> 4);
*pos++ = (block[1] << 4) | (block[2] >> 2);
*pos++ = (block[2] << 6) | block[3];
count = 0;
}
}

*out_len = pos - out;
return out;
}
16 changes: 16 additions & 0 deletions base64.h
@@ -0,0 +1,16 @@
/*
* Base64 encoding/decoding (RFC1341)
* Copyright (c) 2005, Jouni Malinen <j@w1.fi>
*
* This software may be distributed under the terms of the BSD license.
* See README for more details.
*/

#ifndef __BASE64_H__
#define __BASE64_H__

char* base64_encode(const unsigned char *src, size_t len, size_t *out_len);
char* base64_urlsafe_encode(const unsigned char *src, size_t len, size_t *out_len);
unsigned char* base64_decode(const unsigned char *src, size_t len, size_t *out_len);

#endif /* __BASE64_H__ */
8 changes: 6 additions & 2 deletions build.sh
Expand Up @@ -14,10 +14,14 @@ bjam toolset=clang cxxflags="-std=c++14"
cp `find bin -name libbtdht.a` .
cd ..

FLAGS="-g -O0 -Werror -Wall -Wextra -Wno-deprecated-declarations -Wno-unused-parameter -Wno-unused-variable -Werror=shadow -Wfatal-errors \
FLAGS="-g -Werror -Wall -Wextra -Wno-deprecated-declarations -Wno-unused-parameter -Wno-unused-variable -Werror=shadow -Wfatal-errors \
-fPIC -fblocks -fdata-sections -ffunction-sections \
-fno-rtti -fno-exceptions -fno-common -fno-inline -fno-optimize-sibling-calls -funwind-tables -fno-omit-frame-pointer -fstack-protector-all \
-std=gnu11 -D__FAVOR_BSD -D_BSD_SOURCE"
# debug
FLAGS="$FLAGS -O0 -fsanitize=address"
#release
#FLAGS="$FLAGS -O3"

CFLAGS="$FLAGS -std=gnu11"
CPPFLAGS="$FLAGS -std=c++14"
Expand All @@ -29,7 +33,7 @@ echo "int main() {}"|clang -x c - -lrt 2>/dev/null && LRT="-lrt"
#fi

clang $CPPFLAGS -c dht.cpp -I ./libbtdht/src -I ./libbtdht/btutils/src
clang $CFLAGS -o injector injector.c log.c icmp_handler.c network.c sha1.c timer.c utp_bufferevent.c dht.o \
clang $CFLAGS -o injector base64.c injector.c log.c icmp_handler.c hash_table.c network.c sha1.c timer.c utp_bufferevent.c dht.o \
-I ./libutp libutp/libutp.a \
./libbtdht/libbtdht.a ./libbtdht/btutils/libbtutils.a \
`pkg-config --cflags libsodium` `pkg-config --libs libsodium` \
Expand Down
6 changes: 3 additions & 3 deletions client.c
Expand Up @@ -17,14 +17,14 @@ handle_connection(socket_t s)
{
for (;;) {
req = http_read_request(s);
content_hash = dht_get(hash(req->url));
sig = dht_get(hash(req->url));
// these can happen in parallel, but only one http_response should be returned
if (!content_hash) {
if (!sig) {
injector = injector_get_any_connection();
data = request(injector, req->url);
http_respond(s, data);
} else {
swarm_hash = dht_get(content_hash);
swarm_hash = dht_get(sig);
if (swarm_hash) {
data = swarm(swarm_hash);
http_respond(s, data);
Expand Down
47 changes: 47 additions & 0 deletions hash_table.c
@@ -0,0 +1,47 @@
#include <stdio.h>
#include <assert.h>

#include "hash_table.h"
#include "khash.h"


typedef void* hash_table_val;
KHASH_MAP_INIT_STR(hash_table_val, hash_table_val);
typedef khash_t(hash_table_val) hash_table;


hash_table* hash_table_create()
{
return kh_init(hash_table_val);
}

void* hash_get(hash_table *h, const char *key)
{
khint_t k = kh_get(hash_table_val, h, key);
if (k == kh_end(h)) {
return NULL;
}
return kh_val(h, k);
}

void* hash_get_or_insert(hash_table *h, const char *key, create_fn c)
{
int absent;
khint_t k = kh_put(hash_table_val, h, key, &absent);
if (absent) {
kh_val(h, k) = c();
}
return kh_val(h, k);
}

void hash_set(hash_table *h, const char *key, void *val)
{
int absent;
khint_t k = kh_put(hash_table_val, h, key, &absent);
kh_val(h, k) = val;
}

void hash_table_free(hash_table *h)
{
kh_destroy(hash_table_val, h);
}
11 changes: 11 additions & 0 deletions hash_table.h
@@ -0,0 +1,11 @@

struct kh_hash_table_val_s;
typedef struct kh_hash_table_val_s hash_table;

typedef void* (^create_fn)();

hash_table* hash_table_create();
void* hash_get(hash_table *h, const char *key);
void* hash_get_or_insert(hash_table *h, const char *key, create_fn c);
void hash_set(hash_table *h, const char *key, void *val);
void hash_table_free(hash_table *h);

0 comments on commit efa5000

Please sign in to comment.