Skip to content

Commit

Permalink
format & fix
Browse files Browse the repository at this point in the history
  • Loading branch information
radkesvat committed Apr 28, 2024
1 parent 8bd9eb1 commit 226a873
Show file tree
Hide file tree
Showing 35 changed files with 353 additions and 203 deletions.
2 changes: 1 addition & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ WarningsAsErrors: "*"
CheckOptions:
- { key: readability-identifier-naming.NamespaceCase, value: lower_case }
- { key: readability-identifier-naming.ClassCase, value: CamelCase }
- { key: readability-identifier-naming.StructCase, value: CamelCase }
- { key: readability-identifier-naming.StructCase, value: lower_case }
- { key: readability-identifier-naming.TemplateParameterCase, value: CamelCase }
- { key: readability-identifier-naming.FunctionCase, value: camelBack }
- { key: readability-identifier-naming.VariableCase, value: lower_case }
Expand Down
10 changes: 8 additions & 2 deletions core/main.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
#include "api.h"
#include "config_file.h"
#include "core_settings.h"
#include "hbase.h"
#include "hlog.h"
#include "loggers/core_logger.h"
#include "loggers/dns_logger.h"
#include "loggers/network_logger.h"
#include "managers/node_manager.h"
#include "managers/socket_manager.h"
#include "os_helpers.h"
#include "static_tunnels.h"
#include "stc/common.h"
#include "utils/fileutils.h"
#include "utils/stringutils.h"
#include "ww.h"
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>

#undef hlog
#define hlog getCoreLogger() // NOLINT
Expand Down
30 changes: 18 additions & 12 deletions tunnels/client/protobuf/protobuf_client.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
#include "protobuf_client.h"
#include "basic_types.h"
#include "buffer_stream.h"
#include "loggers/network_logger.h"
#include "node.h"
#include "shiftbuffer.h"
#include "tunnel.h"
#include "uleb128.h"
#include <stdatomic.h>
#include <stddef.h>
#include <stdlib.h>
/*
we shall not use nanopb or any protobuf lib because they need atleast 1 memcopy
i have read the byte array implemntation of the protoc and
we do encoding/decoding right to the buffer
*/
#include "buffer_stream.h"
// #include <pb_encode.h>
// #include <pb_decode.h>
// #include "packet.pb.h"
#include "uleb128.h"

#define MAX_PACKET_SIZE (65536 * 1)
enum
{
kMaxPacketSize = (65536 * 1)
};

typedef struct protobuf_client_state_s
{
Expand Down Expand Up @@ -79,18 +85,18 @@ static inline void downStream(tunnel_t *self, context_t *c)
}
unsigned int read_len = (bufferStreamLen(bstream) >= 4 ? 4 : 2);
unsigned char uleb_encoded_buf[4];
bufferStreamViewBytesAt(bstream, uleb_encoded_buf, 1, read_len);
bufferStreamViewBytesAt(bstream, 1, uleb_encoded_buf, read_len);
// if (uleb_encoded_buf[0] != '\n')
// {
// LOGE("ProtoBufClient: rejected, invalid data");
// goto disconnect;
// }

size_t data_len = 0;
size_t bytes_passed = readUleb128ToUint64(uleb_encoded_buf , uleb_encoded_buf + read_len, &data_len);
size_t bytes_passed = readUleb128ToUint64(uleb_encoded_buf, uleb_encoded_buf + read_len, &data_len);
if (data_len == 0)
{
if (uleb_encoded_buf[0] = 0x0)
if (uleb_encoded_buf[0] == 0x0)
{
LOGE("ProtoBufClient: rejected, invalid data");
goto disconnect;
Expand All @@ -100,7 +106,7 @@ static inline void downStream(tunnel_t *self, context_t *c)
destroyContext(c);
return;
}
if (data_len > MAX_PACKET_SIZE)
if (data_len > kMaxPacketSize)
{
LOGE("ProtoBufClient: rejected, size too large %zu (%zu passed %lu left)", data_len, bytes_passed,
bufferStreamLen(bstream));
Expand All @@ -117,7 +123,7 @@ static inline void downStream(tunnel_t *self, context_t *c)
if (! cstate->first_sent)
{
downstream_ctx->first = true;
cstate->first_sent = true;
cstate->first_sent = true;
}
self->dw->downStream(self->dw, downstream_ctx);
if (! isAlive(c->line))
Expand Down
8 changes: 5 additions & 3 deletions tunnels/client/protobuf/protobuf_client.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#pragma once
#include "api.h"
#include "tunnel.h"
#include "node.h"
#include "basic_types.h"

// ----> encode ---->
// con (protocolbuffers) con
// <---- decode <----

tunnel_t * newProtoBufClient(node_instance_context_t *instance_info);
tunnel_t *newProtoBufClient(node_instance_context_t *instance_info);
api_result_t apiProtoBufClient(tunnel_t *self, const char *msg);
tunnel_t * destroyProtoBufClient(tunnel_t *self);
tunnel_t *destroyProtoBufClient(tunnel_t *self);
tunnel_metadata_t getMetadataProtoBufClient();
26 changes: 16 additions & 10 deletions tunnels/server/protobuf/protobuf_server.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
#include "protobuf_server.h"
#include "basic_types.h"
#include "buffer_stream.h"
#include "loggers/network_logger.h"
#include "node.h"
#include "shiftbuffer.h"
#include "tunnel.h"
#include "uleb128.h"
#include <stdatomic.h>
#include <stddef.h>
#include <stdlib.h>
/*
we shall not use nanopb or any protobuf lib because they need atleast 1 memcopy
i have read the byte array implemntation of the protoc and
we do encoding/decoding right to the buffer
*/
#include "buffer_stream.h"
// #include <pb_encode.h>
// #include <pb_decode.h>
// #include "packet.pb.h"
#include "uleb128.h"

#define MAX_PACKET_SIZE (65536 * 1)
enum
{
kMaxPacketSize = (65536 * 1)
};

typedef struct protobuf_server_state_s
{
Expand Down Expand Up @@ -50,7 +56,7 @@ static inline void upStream(tunnel_t *self, context_t *c)
}
unsigned int read_len = (bufferStreamLen(bstream) >= 4 ? 4 : 2);
unsigned char uleb_encoded_buf[4];
bufferStreamViewBytesAt(bstream, uleb_encoded_buf, 1, read_len);
bufferStreamViewBytesAt(bstream, 1, uleb_encoded_buf, read_len);
// if (uleb_encoded_buf[0] != '\n')
// {
// LOGE("ProtoBufServer: rejected, invalid data");
Expand All @@ -60,7 +66,7 @@ static inline void upStream(tunnel_t *self, context_t *c)
size_t bytes_passed = readUleb128ToUint64(uleb_encoded_buf, uleb_encoded_buf + read_len, &data_len);
if (data_len == 0)
{
if (uleb_encoded_buf[0] = 0x0)
if (uleb_encoded_buf[0] == 0x0)
{
LOGE("ProtoBufServer: rejected, invalid data");

Expand All @@ -71,7 +77,7 @@ static inline void upStream(tunnel_t *self, context_t *c)
destroyContext(c);
return;
}
if (data_len > MAX_PACKET_SIZE)
if (data_len > kMaxPacketSize)
{
LOGE("ProtoBufServer: rejected, size too large %zu (%zu passed %lu left)", data_len, bytes_passed,
bufferStreamLen(bstream));
Expand Down
3 changes: 2 additions & 1 deletion ww/api.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#pragma once

//NOLINTBEGIN
#include "basic_types.h"
#include "buffer_pool.h"
#include "config_file.h"
Expand All @@ -9,3 +9,4 @@
#include "tunnel.h"
#include "utils/hashutils.h"
#include "ww.h"
//NOLINTEND
5 changes: 3 additions & 2 deletions ww/basic_types.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#pragma once
#include "hatomic.h"
#include "hmutex.h"
#include "hplatform.h"
#include "hsocket.h"
#include "htime.h"
#include <stdint.h>
#include <stdatomic.h>
#include <stddef.h>

typedef uint64_t hash_t;

Expand Down
30 changes: 19 additions & 11 deletions ww/buffer_pool.c
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
#include "buffer_pool.h"
#include "hlog.h"
#include "shiftbuffer.h"
#include "utils/mathutils.h"
#include <assert.h> // for assert
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
#ifdef DEBUG
#include "loggers/network_logger.h"
#endif

#define LOW_MEMORY 0 // no preallocation (very small)
#define MED1_MEMORY 1 // APPROX 20MB per thread
#define MED2_MEMORY 2 // APPROX 40MB per thread
#define HIG1_MEMORY 3 // APPROX 56MB per thread
#define HIG2_MEMORY 4 // APPROX 72MB per thread
enum
{
kLowMemory = 0, // no preallocation (very small)
kMeD1Memory = 1, // APPROX 20MB per thread
kMeD2Memory = 2, // APPROX 40MB per thread
kHiG1Memory = 3, // APPROX 56MB per thread
kHiG2Memory = 4 // APPROX 72MB per thread
};

#define MEMORY_PROFILE kHiG2Memory // todo (cmake)

#define MEMORY_PROFILE HIG2_MEMORY // todo (cmake)
enum
{
BASE_READ_BUFSIZE = (1U << 13) // 8K
};

#define BASE_READ_BUFSIZE (1U << 13) // 8K
#define BUFFERPOOL_CONTAINER_LEN ((16 * 4) + (16 * (16 * MEMORY_PROFILE)))
#define BUFFERPOOL_CONTAINER_LEN ((unsigned long) ((16 * 4) + (16 * (16 * MEMORY_PROFILE))))
#define BUFFER_SIZE (BASE_READ_BUFSIZE * (MEMORY_PROFILE > 0 ? MEMORY_PROFILE : 1))

static void firstCharge(buffer_pool_t *pool)
Expand Down Expand Up @@ -112,7 +121,6 @@ shift_buffer_t *appendBufferMerge(buffer_pool_t *pool, shift_buffer_t *restrict

buffer_pool_t *createBufferPool()
{

const unsigned long count_max = 2 * BUFFERPOOL_CONTAINER_LEN;
const unsigned long container_len = count_max * sizeof(shift_buffer_t *);
buffer_pool_t *pool = malloc(sizeof(buffer_pool_t) + container_len);
Expand All @@ -129,7 +137,7 @@ buffer_pool_t *createBufferPool()

buffer_pool_t *createSmallBufferPool()
{
const unsigned long count_max = 2 * (16 * 2);
const unsigned long count_max = (unsigned long) (2 * (16 * 2));
const unsigned long container_len = count_max * sizeof(shift_buffer_t *);
buffer_pool_t *pool = malloc(sizeof(buffer_pool_t) + container_len);
#ifdef DEBUG
Expand Down
4 changes: 1 addition & 3 deletions ww/buffer_pool.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#pragma once

#include "shiftbuffer.h"
#ifdef DEBUG
#include "hatomic.h"
#endif
#include <stdatomic.h>

/*
Just do a big memory allocation at the startup and keep using them, don't talk to os as much as possible.
Expand Down
17 changes: 14 additions & 3 deletions ww/buffer_stream.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
#include "buffer_stream.h"
#include "utils/mathutils.h"
#include "buffer_pool.h"
#include "shiftbuffer.h"
#include "stc/common.h"
#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

extern size_t bufferStreamLen(buffer_stream_t *self);
enum
{
kQCap = 25
};

buffer_stream_t *newBufferStream(struct buffer_pool_s *pool)
{
buffer_stream_t *bs = malloc(sizeof(buffer_stream_t));
bs->q = queue_with_capacity(Q_CAP);
bs->q = queue_with_capacity(kQCap);
bs->pool = pool;
bs->size = 0;
return bs;
Expand Down Expand Up @@ -121,7 +132,7 @@ uint8_t bufferStreamViewByteAt(buffer_stream_t *self, size_t at)
}

// todo (test) this could be implemented incorrectly, you didn't write unit tests -> you choosed to suffer
void bufferStreamViewBytesAt(buffer_stream_t *self, uint8_t *buf, size_t at, size_t len)
void bufferStreamViewBytesAt(buffer_stream_t *self, size_t at, uint8_t *buf, size_t len)
{
size_t bufferstream_i = at;
assert(self->size >= (bufferstream_i + len) && self->size != 0);
Expand Down
9 changes: 5 additions & 4 deletions ww/buffer_stream.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#pragma once
#include "buffer_pool.h"
#include "shiftbuffer.h"
#include <stddef.h>
#include <stdint.h>

/*
buffers look like packets , you want a stream? you want to read the length you want?
Expand All @@ -8,10 +11,8 @@
*/


#define i_TYPE queue, shift_buffer_t * // NOLINT
#include "stc/deq.h"
#define Q_CAP 25

struct buffer_stream_s
{
Expand All @@ -26,9 +27,9 @@ buffer_stream_t *newBufferStream(buffer_pool_t *pool);
void empytBufferStream(buffer_stream_t *self);
void destroyBufferStream(buffer_stream_t *self);
void bufferStreamPush(buffer_stream_t *self, shift_buffer_t *buf);
shift_buffer_t * bufferStreamRead(buffer_stream_t *self, size_t bytes);
shift_buffer_t *bufferStreamRead(buffer_stream_t *self, size_t bytes);
uint8_t bufferStreamViewByteAt(buffer_stream_t *self, size_t at);
void bufferStreamViewBytesAt(buffer_stream_t *self, uint8_t* buf, size_t at, size_t len);
void bufferStreamViewBytesAt(buffer_stream_t *self, size_t at, uint8_t *buf, size_t len);

inline size_t bufferStreamLen(buffer_stream_t *self)
{
Expand Down
11 changes: 9 additions & 2 deletions ww/config_file.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
#include "config_file.h"
#include "hplatform.h"
#include "loggers/core_logger.h"
#include "cJSON.h"
#include "hlog.h"
#include "loggers/core_logger.h" //NOLINT
#include "utils/fileutils.h"
#include "utils/jsonutils.h"
#include <hmutex.h>
#include <pthread.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>

void destroyConfigFile(config_file_t *state)
{
Expand Down
9 changes: 4 additions & 5 deletions ww/config_file.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#pragma once

#include "basic_types.h"
#include "cJSON.h"
#include "hmutex.h"
#include "tunnel.h"
#include "utils/jsonutils.h"
#include <stddef.h>

typedef struct config_file_s
{
Expand All @@ -14,8 +13,8 @@ typedef struct config_file_s
int core_minimum_version;
bool encrypted;

cJSON * root;
cJSON * nodes;
cJSON *root;
cJSON *nodes;
size_t file_prebuffer_size;
hmutex_t guard;
} config_file_t;
Expand Down
Loading

0 comments on commit 226a873

Please sign in to comment.