Skip to content

Commit

Permalink
a client, signatures, some https support
Browse files Browse the repository at this point in the history
  • Loading branch information
ghazel committed May 19, 2017
1 parent 51e21ae commit aaa6d09
Show file tree
Hide file tree
Showing 13 changed files with 778 additions and 226 deletions.
3 changes: 1 addition & 2 deletions base64.c
Expand Up @@ -33,8 +33,7 @@ static const unsigned char base64_urlsafe_table[65] =
*/
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 */
size_t olen = BASE64_LENGTH(len);
olen++; /* nul termination */
if (olen < len) {
return NULL; /* integer overflow */
Expand Down
5 changes: 4 additions & 1 deletion base64.h
Expand Up @@ -9,8 +9,11 @@
#ifndef __BASE64_H__
#define __BASE64_H__

#define ROUND_UP(x, n) ((x + (n - 1)) / n)
#define BASE64_LENGTH(x) ROUND_UP(x * 4, 3)

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);
unsigned char* base64_decode(const char *src, size_t len, size_t *out_len);

#endif /* __BASE64_H__ */
77 changes: 77 additions & 0 deletions bev_splice.c
@@ -0,0 +1,77 @@
#include <event2/event.h>
#include <event2/buffer.h>
#include <event2/bufferevent.h>

#include "log.h"
#include "bev_splice.h"
#include "assert.h"


#define WRITE_WATERMARK 64*1024
#define READ_WATERMARK 64*1024


void bev_splice_read_cb(bufferevent *bev, void *ctx)
{
bufferevent *other = (bufferevent *)ctx;
debug("bev_splice_read_cb bev:%p other:%p\n", bev, other);
if (!evbuffer_get_length(bufferevent_get_output(other))) {
bufferevent_write_buffer(other, bufferevent_get_input(bev));
}
}

void bev_splice_write_cb(bufferevent *bev, void *ctx)
{
bufferevent *other = (bufferevent *)ctx;
debug("bev_splice_write_cb bev:%p other:%p\n", bev, other);

if (other) {
bufferevent_write_buffer(bev, bufferevent_get_input(other));
} else {
bufferevent_free(bev);
}
}

void bev_splice_event_cb(bufferevent *bev, short events, void *ctx)
{
bufferevent *other = (bufferevent *)ctx;
debug("bev_splice_event_cb events:%x bev:%p other:%p\n", events, bev, other);

if (events & (BEV_EVENT_EOF|BEV_EVENT_ERROR)) {
if (events & BEV_EVENT_ERROR) {
bufferevent_disable(bev, EV_WRITE);
bufferevent_disable(bev, EV_READ);
}
if (events & BEV_EVENT_EOF) {
bufferevent_disable(bev, EV_READ);
}
if (other) {
bufferevent_write_buffer(other, bufferevent_get_input(bev));
if (evbuffer_get_length(bufferevent_get_output(other))) {
bufferevent_setcb(other, NULL, bev_splice_write_cb, bev_splice_event_cb, NULL);
// data in the input buffer is lost
evbuffer *input = bufferevent_get_input(other);
evbuffer_unfreeze(input, 1);
evbuffer_drain(input, evbuffer_get_length(input));
evbuffer_freeze(input, 1);
assert(!evbuffer_get_length(input));
bufferevent_disable(other, EV_READ);
} else {
bufferevent_free(other);
}
}
bufferevent_free(bev);
}
if (events & BEV_EVENT_CONNECTED) {
bev_splice_write_cb(bev, ctx);
}
}

void bev_splice(bufferevent *bev, bufferevent *other)
{
debug("bev_splice bev:%p other:%p\n", bev, other);
bufferevent_setcb(bev, bev_splice_read_cb, bev_splice_write_cb, bev_splice_event_cb, other);
bufferevent_setcb(other, bev_splice_read_cb, bev_splice_write_cb, bev_splice_event_cb, bev);
bufferevent_setwatermark(bev, EV_READ, 0, READ_WATERMARK);
bufferevent_setwatermark(other, EV_READ, 0, READ_WATERMARK);
}
8 changes: 8 additions & 0 deletions bev_splice.h
@@ -0,0 +1,8 @@
#ifndef __BEV_SPLICE_H__
#define __BEV_SPLICE_H__

#include "network.h"

void bev_splice(bufferevent *bev, bufferevent *other);

#endif // __BEV_SPLICE_H__
12 changes: 10 additions & 2 deletions build.sh
Expand Up @@ -29,7 +29,7 @@ FLAGS="-g -Werror -Wall -Wextra -Wno-deprecated-declarations -Wno-unused-paramet
-fno-rtti -fno-exceptions -fno-common -fno-inline -fno-optimize-sibling-calls -funwind-tables -fno-omit-frame-pointer -fstack-protector-all \
-D__FAVOR_BSD -D_BSD_SOURCE"
# debug
FLAGS="$FLAGS -O0 -fsanitize=address"
FLAGS="$FLAGS -O0 -fsanitize=address -DDEBUG=1"
#release
#FLAGS="$FLAGS -O3"

Expand All @@ -41,7 +41,15 @@ echo -e "#include <math.h>\nint main() { log(2); }"|clang -x c - 2>/dev/null ||
echo -e "#include <Block.h>\nint main() { Block_copy(^{}); }"|clang -x c -fblocks - 2>/dev/null || LB="-lBlocksRuntime"

clang $CPPFLAGS -c dht.cpp -I ./libbtdht/src -I ./libbtdht/btutils/src
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 \

clang $CFLAGS -o injector bev_splice.c base64.c injector.c http.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 \
-I ./Libevent/include ./Libevent/.libs/libevent.a ./Libevent/.libs/libevent_pthreads.a ./Libevent/.libs/libevent_openssl.a \
-I ./libsodium/src/libsodium/include ./libsodium/src/libsodium/.libs/libsodium.a \
-lstdc++ $LRT $LM $LB

clang $CFLAGS -o client bev_splice.c base64.c client.c http.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 \
-I ./Libevent/include ./Libevent/.libs/libevent.a ./Libevent/.libs/libevent_pthreads.a ./Libevent/.libs/libevent_openssl.a \
Expand Down

0 comments on commit aaa6d09

Please sign in to comment.