Skip to content

Commit

Permalink
c backend tables more work
Browse files Browse the repository at this point in the history
  • Loading branch information
smurfd committed Jun 25, 2024
1 parent 48413af commit d2ca791
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 23 deletions.
40 changes: 25 additions & 15 deletions lotordb/src/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,18 @@ static void *handler_ssl_server(void *conn) {
receive_cryptodata(*(connection*)conn, &dat, &h, BLOCK - 1);
for (u64 i = 0; i < 10; i++) handler_cryptography(dat[i], k2, &cd[i]);
printf("ssl 0x%.16llx 0x%.16llx 0x%.16llx\n", dat[0], dat[1], dat[2]);

int s = ((connection*)conn)->socket;
if (((connection*)conn)->type == 1) {
printf("recv sock %d\n", ((connection*)conn)->socket);
key_recv(((connection*)conn)->socket, &k);
} else if (((connection*)conn)->type == 2) {
table_recv(((connection*)conn)->socket, &t);
dbdata k2;
dbindex k3;
table_recv2(((connection*)conn)->socket, &k2);
table_recv3(((connection*)conn)->socket, &k3);
printf("tbl recv %s %llu %llu\n", k2.unique_id, k2.xxx, k3.index);
table_write_index(&k3, "/tmp/dbindex1.db1");
table_write_data(&k2, &k3);
}
pthread_exit(NULL);
return 0;
Expand All @@ -81,7 +88,6 @@ static void *handler_ssl_server(void *conn) {
// Server handler
static void *handler_server(void *conn) {
u64 g1 = u64rnd(), p1 = u64rnd();

if (((connection*)conn)->socket == -1) return (void*) - 1;
head h = *set_header(&h, g1, p1);
cryptokey k1 = generate_cryptokeys(&h), k2 = *clear_cryptokey(&k2);
Expand All @@ -99,7 +105,6 @@ static void *handler_client(void *conn) {
u64 dat[BLOCK], cd[BLOCK];
cryptokey k1, k2;
head h;

receive_cryptokey(*(connection*)conn, &h, &k1);
k2 = generate_cryptokeys(&h);
send_cryptokey(*(connection*)conn, &h, &k2);
Expand All @@ -118,14 +123,19 @@ static void *handler_client_ssl(void *conn) {
int s = ((connection*)conn)->socket;
tbls t;
kvsh k;

if (((connection*)conn)->type == 1) {
set_key_value_store(&k, "0002", "testvalue", "/tmp");
key_write(&k);
key_del(&k);
key_send(s, &k);
} else if (((connection*)conn)->type == 2) {
table_send(s, &t);
dbdata d;
dbindex di;
set_table2(&d, "stuff", "stuff * 2", 66699);
sleep(1); // TODO: wtf no sleep til brooklyn
table_send2(((connection*)conn)->socket, &d);
set_table3(&di, 1234, "stuff", 1111, "/tmp/dbdata.d1");
table_send3(((connection*)conn)->socket, &di);
}
client_end(*(connection*)conn);
pthread_exit(NULL);
Expand Down Expand Up @@ -158,7 +168,7 @@ int usage(char *arg, int count, char *clisrv) {
}
int type = 0;
if (strcmp(arg, "keys")==0) {type = 1;}
else if (strcmp(arg, "talbe")==0) {type = 2;}
else if (strcmp(arg, "table")==0) {type = 2;}
else {printf("wrong %s type\n", clisrv); exit(0);}
return type;
}
Expand Down Expand Up @@ -268,24 +278,24 @@ int server_listen(connection c) {
int tmpsock = c.socket;
while (cl >= 1) {
cl = accept(tmpsock, (sock*)&cli, (socklen_t*)&len);
pthread_t thrd;
pthread_t thrd, thrd_ssl;
c.socket = cl;
if (pthread_create(&thrd, NULL, handler_server, (void*)&c) < 0) return -1;
if (pthread_create(&thrd, NULL, handler_server, (void*)&c) < 0) {printf("ERR1\n");return -1;}
pthread_join(thrd, NULL);
// TODO: Only if handshake OK, we create SSL thread
if (pthread_create(&thrd, NULL, handler_ssl_server, (void*)&c) < 0) return -1;
pthread_join(thrd, NULL);
if (pthread_create(&thrd_ssl, NULL, handler_ssl_server, (void*)&c) < 0) {printf("ERR2\n");return -1;}
pthread_join(thrd_ssl, NULL);
}
return cl;
}

int client_connect(connection c) {
pthread_t thrd;
if (pthread_create(&thrd, NULL, handler_client, (void*)&c) < 0) return -1;
pthread_t thrd, thrd_ssl;
if (pthread_create(&thrd, NULL, handler_client, (void*)&c) < 0) {printf("ERR3\n");return -1;}
pthread_join(thrd, NULL);
// TODO: Only if handshake OK, we create SSL thread
if (pthread_create(&thrd, NULL, handler_client_ssl, (void*)&c) < 0) return -1;
pthread_join(thrd, NULL);
if (pthread_create(&thrd_ssl, NULL, handler_client_ssl, (void*)&c) < 0) {printf("ERR4\n");return -1;}
pthread_join(thrd_ssl, NULL);
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion lotordb/src/keys.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void key_send(const int s, kvsh *k) {

void key_recv(const int s, kvsh *k) {
char tmphash[131];
recv(s, k, sizeof(struct kvsh), 0);
printf("key recv %d\n", recv(s, k, sizeof(struct kvsh), 0));
(*k).hash[130] = '\0';
hash_new(tmphash, (uint8_t *)(*k).value);
printf("tmp %s\n", tmphash);
Expand Down
53 changes: 49 additions & 4 deletions lotordb/src/tables.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,56 @@
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include "tables.h"
#include "defs.h"

void table_send(const int s, tbls *t) {
send(s, t, sizeof(struct tbls), 0);
void table_write_index(dbindex *t, char path[256]) {
FILE *f;
f = fopen(path, "w");
printf("writing index: %llu %s %llu %s\n", (*t).index, (*t).unique_id, (*t).length, (*t).path);
fprintf(f, "%llu|%s|%llu|%s\n", (*t).index, (*t).unique_id, (*t).length, (*t).path);
fclose(f);
}

void table_recv(const int s, tbls *t) {
recv(s, t, sizeof(struct tbls), 0);
void table_write_data(dbdata *t, dbindex *i) {
FILE *f;
f = fopen((*i).path, "w");
fprintf(f, "%s|%s\n", (*t).unique_id, (*t).data);
fclose(f);
}

void table_send2(const int s, dbdata *d) {
send(s, d, sizeof(struct dbdata), 0);
}

void table_send3(const int s, dbindex *d) {
send(s, d, sizeof(struct dbindex), 0);
}

void set_table2(dbdata *k, char unique_id[256], char data[4096], u64 xxx) {
strncpy((*k).unique_id, unique_id, strlen(unique_id)+1);
strncpy((*k).data, data, strlen(data)+1);
(*k).xxx = xxx;
}

void set_table3(dbindex *k, u64 index, char unique_id[256], u64 length, char path[256]) {
strncpy((*k).unique_id, unique_id, strlen(unique_id)+1);
strncpy((*k).path, path, strlen(path)+1);
(*k).index = index;
(*k).length = length;
}


void table_recv2(const int s, dbdata *k) {
printf("key recv %d\n", recv(s, k, sizeof(struct dbdata), 0));
printf("hsh %s %llu \n", (*k).unique_id, (*k).xxx);
}

void table_recv3(const int s, dbindex *k) {
recv(s, k, sizeof(struct dbindex), 0);
printf("hsh %s %llu \n", (*k).unique_id, (*k).index);
}
82 changes: 79 additions & 3 deletions lotordb/src/tables.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
#ifndef TABLES_H
#define TABLES_H 1
#include <stdio.h>
#include <stdbool.h>
#include "defs.h"
#include <inttypes.h>
#include <netinet/in.h>
#include "keys.h"

#include <sys/socket.h>
#include <stdbool.h>
#include "defs.h"
/*
typedef struct dbindex {
u64 index;
u64 dbindex;
Expand All @@ -23,12 +30,81 @@ typedef struct dbdata {
u64 col;
char data[4048];
} dbdata;
*/
typedef struct dbindex {
u64 index;
char unique_id[256];
u64 length;
char path[256];
} dbindex;

typedef struct dbdata {
char unique_id[256];
char data[4096];
u64 xxx;
} dbdata;

typedef struct tbls {
dbindex i;
dbdata d;
} tbls;

void table_send(const int s, tbls *t);
void table_recv(const int s, tbls *t);
//void table_send(const int s, dbindex *d);//tbls *t);
//void table_recv(const int s, dbindex *d);//tbls *t);
//void table_set_index(tbls *t, u64 index, char unique_id[256], u64 length, char path[256]);
void table_write_index(dbindex *t, char path[256]);
//void table_write_index(tbls *t, char path[256]);
//void table_set_data(tbls *t, char unique_id[256], char data[4096]);
//void table_write_data(tbls *t);//, char unique_id[256], char data[4096]);
void table_write_data(dbdata *t, dbindex *i);

/*
#include <stdio.h>
#include <stdbool.h>
#include <inttypes.h>
#include <netinet/in.h>
*/
void set_table3(dbindex *k, u64 index, char unique_id[256], u64 length, char path[256]);
void table_send3(const int s, dbindex *d);//tbls *t);
void table_recv3(const int s, dbindex *k);

void table_recv2(const int s, dbdata *k);
void set_table2(dbdata *k, char unique_id[256], char data[4096], u64 xxx);
void table_send2(const int s, dbdata *d);//tbls *t);
//void set_key_value_store1(kvsh *k, char key[256], char value[256], char store[256]);
//void key_write1(kvsh *k);
//void key_del1(kvsh *k);
//void key_send1(const int s, kvsh *k);
//void key_recv1(const int s, kvsh *k);


#endif

/*
dbindex:
index, unique_id(encrypt), size, path
1 smurfd1 48 /pth/to/f1.txt
2 smurfd2 4090 /pth/to/f2.txt
3 smurfd3 10 /pth/to/f3.txt
4 foo 100 /pth/to/f4.txt
5 baar 1000 /pth/to/f5.txt
# get data
read index file, decrypt unique ids until you found what we search for (this will be slow when it scales)
Get path for unique_id
Open path
Read size
decrypt
use data
# write data
encrypt x=data+hash
write x to /pth/to/x.txt
add row to index file, with path, len of encrypted data and encrypted, padded unique id like [email protected]
len = getline(&line, &buffer_size, f)
if strstr(line, unique_id) != null
*/

0 comments on commit d2ca791

Please sign in to comment.