aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/hash/sysvchecksum.c19
-rw-r--r--src/hash/sysvchecksum.h4
-rw-r--r--src/net.c13
-rw-r--r--src/net/common.h1
-rw-r--r--src/net/util.c20
-rw-r--r--src/types/map.c4
6 files changed, 40 insertions, 21 deletions
diff --git a/src/hash/sysvchecksum.c b/src/hash/sysvchecksum.c
index 661ee8d..107a998 100644
--- a/src/hash/sysvchecksum.c
+++ b/src/hash/sysvchecksum.c
@@ -12,12 +12,15 @@ void sysvchecksum_update(uint8_t* aa, size_t len, struct sysvchecksum_hash* hash
hash->check += aa[i];
}
-uint32_t sysvchecksum_final(struct sysvchecksum_hash* hash){
- uint32_t r = hash->check % (int)pow(2,16) + (hash->check % (int)pow(2,32)) / (int)pow(2,16);
- return (r % (int)pow(2,16)) + r / (int)pow(2,16);
+uint16_t sysvchecksum_final(struct sysvchecksum_hash* hash){
+ //uint16_t r = hash->check % (int)pow(2,16) + (hash->check % (int)pow(2,32)) / (int)pow(2,16);
+ //return (r % (int)pow(2,16)) + (r / (int)pow(2,16));
+ //rewrote from https://github.com/cxmcc/unixsums/blob/master/sysvsum/sysvsum.go, pow breaks with clang -O3 somehow
+ uint32_t r = (hash->check & 0xffff) + (hash->check >> 16);
+ return r & 0xffff + (r >> 16);
}
-uint32_t sysvchecksum(uint8_t* aa, size_t len){
+uint16_t sysvchecksum(uint8_t* aa, size_t len){
struct sysvchecksum_hash a = sysvchecksum_init();
sysvchecksum_update(aa, len, &a);
return sysvchecksum_final(&a);
@@ -28,8 +31,8 @@ common_hash_init_update(sysvchecksum);
int l_sysvchecksum_final(lua_State* L){
struct sysvchecksum_hash* a = (struct sysvchecksum_hash*)lua_touserdata(L, 1);
- uint32_t u = sysvchecksum_final(a);
- char digest[32];
+ uint16_t u = sysvchecksum_final(a);
+ char digest[16];
sprintf(digest,"%x",u);
lua_pushstring(L, digest);
return 1;
@@ -40,9 +43,9 @@ int l_sysvchecksum(lua_State* L){
size_t len = 0;
uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
- char digest[32];
+ char digest[16];
- uint32_t u = sysvchecksum(a, len);
+ uint16_t u = sysvchecksum(a, len);
sprintf(digest,"%x",u);
lua_pushstring(L, digest);
diff --git a/src/hash/sysvchecksum.h b/src/hash/sysvchecksum.h
index 3414f7f..cae322d 100644
--- a/src/hash/sysvchecksum.h
+++ b/src/hash/sysvchecksum.h
@@ -7,8 +7,8 @@ struct sysvchecksum_hash {
struct sysvchecksum_hash sysvchecksum_init();
void sysvchecksum_update(uint8_t*, size_t, struct sysvchecksum_hash*);
-uint32_t sysvchecksum_final(struct sysvchecksum_hash*);
-uint32_t sysvchecksum(uint8_t*, size_t);
+uint16_t sysvchecksum_final(struct sysvchecksum_hash*);
+uint16_t sysvchecksum(uint8_t*, size_t);
int l_sysvchecksum(lua_State*);
int l_sysvchecksum_init(lua_State*);
diff --git a/src/net.c b/src/net.c
index 48c685d..b71b290 100644
--- a/src/net.c
+++ b/src/net.c
@@ -13,7 +13,7 @@ void* handle_client(void *_arg){
int client_fd = args->fd;
char* buffer;
char dummy[2] = {0, 0};
- int header_eof;
+ int header_eof = -1;
lua_State* L = args->L;
//sleep(1);
//create state for this thread
@@ -43,7 +43,12 @@ void* handle_client(void *_arg){
//read full request
//time_start(recv)
int64_t bytes_received = recv_full_buffer(client_fd, &buffer, &header_eof, &read_state);
-
+ /*
+ for(int i = 0; i != header_eof; i++)
+ putchar(buffer[i]);
+ putchar('\n');
+ printf("hi %li:%i\n", bytes_received,header_eof);
+ */
//ignore if header is just fucked
if(bytes_received >= -1){
parray_t* table;
@@ -67,7 +72,6 @@ void* handle_client(void *_arg){
sprintf(portc, "%i", args->port);
str* aa = str_init(portc);
-
str_push(aa, sk->c);
//parray_t* v = parray_find(paths, aa->c);
@@ -97,7 +101,7 @@ void* handle_client(void *_arg){
int req_idx = lua_gettop(L);
lua_newtable(L);
int res_idx = lua_gettop(L);
-
+
//handle cookies
//TODO: enable and test with valgrind
if(0 && sC != NULL){
@@ -236,7 +240,6 @@ void* handle_client(void *_arg){
}
parray_clear(table, STR);
}
-
shutdown(client_fd, 2);
close(client_fd);
free(args);
diff --git a/src/net/common.h b/src/net/common.h
index 04447f1..4120734 100644
--- a/src/net/common.h
+++ b/src/net/common.h
@@ -22,6 +22,7 @@
#define max_con 200
//2^42
+#define MAX_HEADER_SIZE (1<<20)
#define BUFFER_SIZE 20000
#define HTTP_BUFFER_SIZE 4098
#define max_content_length 200000
diff --git a/src/net/util.c b/src/net/util.c
index 868da0f..1fec987 100644
--- a/src/net/util.c
+++ b/src/net/util.c
@@ -47,6 +47,10 @@ int64_t recv_full_buffer(int client_fd, char** _buffer, int* header_eof, int* st
}
len += n;
+ if(len >= MAX_HEADER_SIZE){
+ *_buffer = buffer;
+ return -2;//p_fatal("too large");
+ }
if(*header_eof == -1){
buffer = realloc(buffer, len + BUFFER_SIZE + 1);
memset(buffer + len, 0, n + 1);
@@ -80,7 +84,14 @@ int parse_header(char* buffer, int header_eof, parray_t** _table){
str_clear(current);
item++;
if(buffer[oi] == '\n') break;
- } else str_pushl(current, buffer + oi, 1);
+ } else {
+ str_pushl(current, buffer + oi, 1);
+ }
+ }
+
+ if(item != 3){
+ *_table = table;
+ return -1;
}
int key = 1;
@@ -103,10 +114,12 @@ int parse_header(char* buffer, int header_eof, parray_t** _table){
continue;
} else str_pushl(current, buffer + i, 1);
}
- parray_set(table, sw->c, (void*)str_init(current->c));
+ if(sw != NULL){
+ parray_set(table, sw->c, (void*)str_init(current->c));
+ str_free(sw);
+ }
str_free(current);
- if(sw != NULL) str_free(sw);
*_table = table;
return 0;
}
@@ -135,7 +148,6 @@ void http_build(str** _dest, int code, char* code_det, char* header_vs, char* co
*
*/
void http_code(int code, char* code_det){
- //this was done with a script btw
switch(code){
case 100: sprintf(code_det,"Continue"); break;
case 101: sprintf(code_det,"Switching Protocols"); break;
diff --git a/src/types/map.c b/src/types/map.c
index a15762a..31e79a9 100644
--- a/src/types/map.c
+++ b/src/types/map.c
@@ -115,7 +115,7 @@ void map_clear(map_t* M, enum free_type free){
map_lclear(M);
}
-int main(){
+int __main(){
int i = 5;
int b = 24;
int c = 9;
@@ -132,4 +132,4 @@ int main(){
map_clear(m, NONE);
return 0;
-} \ No newline at end of file
+}