|
@@ -116,25 +116,112 @@ int read_header(flow *f, struct packet_info *info){
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
- struct record_header *record_hdr = (struct record_header*) p;
|
|
|
- uint32_t record_length = RECORD_LEN(record_hdr);
|
|
|
+ uint8_t *record_ptr = NULL;
|
|
|
+ struct record_header *record_hdr;
|
|
|
+ uint32_t record_length;
|
|
|
+ if(f->upstream_remaining > 0){
|
|
|
+ //check to see whether the previous record has finished
|
|
|
+ if(f->upstream_remaining > info->app_data_len){
|
|
|
+ //ignore entire packet for now
|
|
|
+ printf("US: received some data but still waiting\n");
|
|
|
+ queue_block *new_block = calloc(1, sizeof(queue_block));
|
|
|
+ uint8_t *block_data = calloc(1, info->app_data_len);
|
|
|
+ memcpy(block_data, p, info->app_data_len);
|
|
|
+
|
|
|
+ new_block->len = info->app_data_len;
|
|
|
+ new_block->offset = 0;
|
|
|
+ new_block->data = block_data;
|
|
|
+ new_block->next = NULL;
|
|
|
+ //add block to upstream data chain
|
|
|
+ if(f->upstream_queue == NULL){
|
|
|
+ f->upstream_queue = new_block;
|
|
|
+ } else {
|
|
|
+ queue_block *last = f->upstream_queue;
|
|
|
+ while(last->next != NULL){
|
|
|
+ last = last->next;
|
|
|
+ }
|
|
|
+ last->next = new_block;
|
|
|
+ }
|
|
|
+
|
|
|
+ f->upstream_remaining -= info->app_data_len;
|
|
|
+ return 0;
|
|
|
+
|
|
|
+
|
|
|
+ } else {
|
|
|
+ //process what we have
|
|
|
+ printf("US: received remainder of packet\n");
|
|
|
+ printf("US: %d bytes of packet unused\n", info->app_data_len - f->upstream_remaining);
|
|
|
+ record_hdr = (struct record_header*) f->upstream_queue->data;
|
|
|
+ record_length = RECORD_LEN(record_hdr);
|
|
|
+ record_ptr = calloc(1, record_length+ RECORD_HEADER_LEN);
|
|
|
+ queue_block *current = f->upstream_queue;
|
|
|
+ int32_t offset =0;
|
|
|
+ while(f->upstream_queue != NULL){
|
|
|
+ memcpy(record_ptr+offset, current->data, current->len);
|
|
|
+ offset += current->len;
|
|
|
+ free(current->data);
|
|
|
+ f->upstream_queue = current->next;
|
|
|
+ free(current);
|
|
|
+ }
|
|
|
+ memcpy(record_ptr+offset, p, f->upstream_remaining);
|
|
|
+ p = record_ptr;
|
|
|
+ f->upstream_remaining = 0;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ //check to see if the new record is too long
|
|
|
+ printf("US: received beginning of packet\n");
|
|
|
+ record_hdr = (struct record_header*) p;
|
|
|
+ record_length = RECORD_LEN(record_hdr);
|
|
|
+ if(record_length > info->app_data_len){
|
|
|
+ printf("ERROR: record bigger than incoming packet\n");
|
|
|
+ fflush(stdout);
|
|
|
+
|
|
|
+ //add info to upstream queue
|
|
|
+ queue_block *new_block = calloc(1, sizeof(queue_block));
|
|
|
+ uint8_t *block_data = calloc(1, info->app_data_len);
|
|
|
+ memcpy(block_data, p, info->app_data_len);
|
|
|
+
|
|
|
+ new_block->len = info->app_data_len - RECORD_HEADER_LEN;
|
|
|
+ new_block->offset = record_length; //re-appropriate this for len of record
|
|
|
+ new_block->data = block_data;
|
|
|
+ new_block->next = NULL;
|
|
|
+
|
|
|
+ //add block to upstream queue
|
|
|
+ if(f->upstream_queue == NULL){
|
|
|
+ f->upstream_queue = new_block;
|
|
|
+ } else {
|
|
|
+ queue_block *last = f->upstream_queue;
|
|
|
+ while(last->next != NULL){
|
|
|
+ last = last->next;
|
|
|
+ }
|
|
|
+ last->next = new_block;
|
|
|
+ }
|
|
|
+
|
|
|
+ f->upstream_remaining = record_length - new_block->len;
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
printf("HERE\n");
|
|
|
fflush(stdout);
|
|
|
|
|
|
- uint8_t *decrypted_data = calloc(1, info->app_data_len);
|
|
|
+ p+= RECORD_HEADER_LEN;
|
|
|
+ uint8_t *decrypted_data = calloc(1, record_length);
|
|
|
|
|
|
printf("HERE2\n");
|
|
|
fflush(stdout);
|
|
|
- p+= RECORD_HEADER_LEN;
|
|
|
|
|
|
memcpy(decrypted_data, p, record_length);
|
|
|
printf("HERE3\n");
|
|
|
fflush(stdout);
|
|
|
|
|
|
- if(!encrypt(f, decrypted_data, decrypted_data, record_length, 0, record_hdr->type, 0)){
|
|
|
+ int32_t decrypted_len = encrypt(f, decrypted_data, decrypted_data, record_length, 0, record_hdr->type, 0);
|
|
|
+ if(decrypted_len<0){
|
|
|
fprintf(stdout,"upstream decryption failed\n");
|
|
|
- fflush(stdout);
|
|
|
+ fflush(stdout);
|
|
|
+ if(record_ptr != NULL)
|
|
|
+ free(record_ptr);
|
|
|
return 0;
|
|
|
} else {
|
|
|
fprintf(stdout, "upstream decryption succeeded\n");
|
|
@@ -159,6 +246,8 @@ int read_header(flow *f, struct packet_info *info){
|
|
|
uint8_t *upstream_data;
|
|
|
if(header_ptr == NULL){
|
|
|
printf("UPSTREAM: No x-slitheen header found\n");
|
|
|
+ if(record_ptr != NULL)
|
|
|
+ free(record_ptr);
|
|
|
return 0;
|
|
|
}
|
|
|
printf("UPSTREAM: Found x-slitheen header\n");
|
|
@@ -170,7 +259,7 @@ int read_header(flow *f, struct packet_info *info){
|
|
|
char *messages[50]; //TODO:make not just 10?
|
|
|
messages[0] = header_ptr;
|
|
|
char *c = header_ptr;
|
|
|
- while(*c != '\r'){
|
|
|
+ while(*c != '\r' && *c != '\0'){
|
|
|
if(*c == ' '){
|
|
|
*c = '\0';
|
|
|
messages[num_messages] = c+1;
|
|
@@ -344,6 +433,8 @@ int read_header(flow *f, struct packet_info *info){
|
|
|
printf("failed to write all bytes to pipe\n");
|
|
|
}*/
|
|
|
free(decrypted_data);
|
|
|
+ if(record_ptr != NULL)
|
|
|
+ free(record_ptr);
|
|
|
|
|
|
return 0;
|
|
|
|