1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdint.h>
- #include <regex.h>
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #include <unistd.h>
- #include <pthread.h>
- #include <string.h>
- #include <openssl/bio.h>
- #include <openssl/evp.h>
- #include "relay.h"
- #include "slitheen.h"
- #include "flow.h"
- #include "crypto.h"
- /** Called when a TLS application record is received for a
- * tagged flow. Upstream packets will be checked for covert
- * requests to censored sites, downstream packets will be
- * replaced with data from the censored queue or with garbage
- *
- * Inputs:
- * f: the tagged flow
- * info: the processed received application packet
- *
- * Output:
- * 0 on success, 1 on failure
- */
- int replace_packet(flow *f, struct packet_info *info){
- printf("Enter replace_packet\n");
- fflush(stdout);
- if (info == NULL || info->tcp_hdr == NULL){
- return 0;
- }
- #ifdef DEBUG
- fprintf(stderr,"Flow: %d > %d (%s)\n", info->ip_hdr->src.s_addr, info->ip_hdr->dst.s_addr, (info->ip_hdr->src.s_addr != f->src_ip.s_addr)? "incoming":"outgoing");
- fprintf(stderr,"ID number: %u\n", htonl(info->ip_hdr->id));
- fprintf(stderr,"Sequence number: %u\n", htonl(info->tcp_hdr->sequence_num));
- fprintf(stderr,"Acknowledgement number: %u\n", htonl(info->tcp_hdr->ack_num));
- #endif
- if(info->app_data_len <= 0){
- return 0;
- }
- /* if outgoing, decrypt and look at header */
- if(info->ip_hdr->src.s_addr == f->src_ip.s_addr){
- read_header(f, info);
- return 0;
- } else {
- #ifdef DEBUG
- printf("Current sequence number: %d\n", f->seq_num);
- printf("Received sequence number: %d\n", htonl(tcp_hdr->sequence_num));
- #endif
- uint32_t offset = htonl(info->tcp_hdr->sequence_num) - f->seq_num;
- if(offset == 0)
- f->seq_num += info->app_data_len;
- else
- printf("Offset is nonzero\n");
- fflush(stdout);
- /* if incoming, replace with data from queue */
- //if(htonl(tcp_hdr->sequence_num) >= f->seq_num){
- process_downstream(f, offset, info);
- //}//TODO: need to do something about replaying packets (maybe store previously sent data??
- #ifdef DEBUG //TODO: fix
- uint8_t *p = (uint8_t *) info->tcp_hdr;
- fprintf(stdout, "ip hdr length: %d\n", htons(info->ip_hdr->len));
- fprintf(stdout, "Injecting the following packet:\n");
- for(int i=0; i< htons(info->ip_hdr->len); i++){
- fprintf(stdout, "%02x ", p[i]);
- }
- fprintf(stdout, "\n");
- fflush(stdout);
- #endif
- }
- printf("Done replace_packet\n");
- fflush(stdout);
- return 0;
- }
- /** Reads the HTTP header of upstream data and searches for
- * a covert request in an x-slitheen header. Sends this
- * request to the indicated site and saves the response to
- * the censored queue
- *
- * TODO: change this to take SOCKS5 proxy connection and then relay data
- * based on stream ID
- *
- * Inputs:
- * f: the tagged flow
- * info: the processed received packet
- *
- * Ouput:
- * 0 on success, 1 on failure
- */
- int read_header(flow *f, struct packet_info *info){
- uint8_t *p = info->app_data;
- printf("IN read_header\n");
- fflush(stdout);
- if (info->tcp_hdr == NULL){
- return 0;
- }
- 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);
- p+= RECORD_HEADER_LEN;
- uint8_t *decrypted_data = calloc(1, record_length);
- printf("HERE2\n");
- fflush(stdout);
- memcpy(decrypted_data, p, record_length);
- printf("HERE3\n");
- fflush(stdout);
- 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);
- if(record_ptr != NULL)
- free(record_ptr);
- return 0;
- } else {
- fprintf(stdout, "upstream decryption succeeded\n");
- fflush(stdout);
- }
- if(record_hdr->type == 0x15){
- printf("received alert\n");
- for(int i=0; i<record_length; i++){
- printf("%02x ", decrypted_data[i]);
- }
- fflush(stdout);
- }
- printf("Upstream data:\n");
- printf("%s\n", decrypted_data+EVP_GCM_TLS_EXPLICIT_IV_LEN);
- //TODO: re-write this to take a SOCKS connection request
- /* search through decrypted data for x-ignore */
- char *header_ptr = strstr((const char *) decrypted_data, "X-Slitheen");
- 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");
- fprintf(stdout,"UPSTREAM Flow: %x > %x (%s)\n", info->ip_hdr->src.s_addr, info->ip_hdr->dst.s_addr, (info->ip_hdr->src.s_addr != f->src_ip.s_addr)? "incoming":"outgoing");
- fprintf(stdout, "Sequence number: %d\n", info->tcp_hdr->sequence_num);
- header_ptr += strlen("X-Slitheen: ");
- int32_t num_messages = 1;
- char *messages[50]; //TODO:make not just 10?
- messages[0] = header_ptr;
- char *c = header_ptr;
- while(*c != '\r' && *c != '\0'){
- if(*c == ' '){
- *c = '\0';
- messages[num_messages] = c+1;
- num_messages ++;
- }
- c++;
- }
- c++;
- *c = '\0';
- printf("upstream data (%d messages):\n", num_messages);
- for(int i=0; i< num_messages-1; i++){
- char *message = messages[i];
- printf("Message (%d bytes): %s\n", strlen(message), message);
- fflush(stdout);
- //b64 decode the data
- int32_t decode_len = strlen(message);
- if(message[decode_len-2] == '='){
- decode_len = decode_len*3/4 - 2;
- } else if(message[decode_len-1] == '='){
- decode_len = decode_len*3/4 - 1;
- } else {
- decode_len = decode_len*3/4;
- }
- upstream_data = calloc(1, decode_len + 1);
- printf("HERE\n");
- fflush(stdout);
- BIO *bio, *b64;
- bio = BIO_new_mem_buf(message, -1);
- printf("HERE 2\n");
- fflush(stdout);
- b64 = BIO_new(BIO_f_base64());
- printf("HERE 3\n");
- fflush(stdout);
- bio = BIO_push(b64, bio);
- printf("HERE 4\n");
- fflush(stdout);
- BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
- printf("HERE 5\n");
- fflush(stdout);
- int32_t output_len = BIO_read(bio, upstream_data, strlen(message));
- printf("HERE 6\n");
- fflush(stdout);
- BIO_free_all(bio);
- if(output_len != decode_len)
- printf("UH OH, lens dont match\n. %d != %d", decode_len, output_len);
- fflush(stdout);
- printf("Decoded to get %d bytes:\n", output_len);
- for(int i=0; i< output_len; i++){
- printf("%02x ", upstream_data[i]);
- }
- printf("\n");
- fflush(stdout);
- p = upstream_data;
- while(output_len > 0){
- printf("Bytes left to process: %d\n", output_len);
- fflush(stdout);
- struct sl_up_hdr *sl_hdr = (struct sl_up_hdr *) p;
- uint8_t stream_id = sl_hdr->stream_id;
- uint16_t stream_len = ntohs(sl_hdr->len);
- p += sizeof(struct sl_up_hdr);
- output_len -= sizeof(struct sl_up_hdr);
- if(stream_len > output_len)
- printf("UH OH missing part of stream\n");
- printf("Received bytes with stream id %d of len %d\n", stream_id, stream_len);
- fflush(stdout);
- //If a thread for this stream id exists, get the thread info and pipe data
- int32_t stream_pipe = -1;
- if(streams->first != NULL){
- stream *last = streams->first;
- if(last->stream_id == stream_id){
- printf("Found stream in table!\n");
- fflush(stdout);
- stream_pipe = last->pipefd;
- }
- while(last->next != NULL){
- last = last->next;
- if(last->stream_id == stream_id){
- printf("Found stream in table!\n");
- fflush(stdout);
- stream_pipe = last->pipefd;
- break;
- }
- }
- }
- if(stream_pipe != -1){
- int32_t bytes_sent = write(stream_pipe, p, stream_len);
- if(bytes_sent < 0){
- printf("Error writing to stream pipe\n");
- fflush(stdout);
- }
- } else {
- printf("Stream not found\n");
- printf("Adding thread\n");
- fflush(stdout);
- /*Else, spawn a thread to handle the proxy to this site*/
- pthread_t *proxy_thread = calloc(1, sizeof(pthread_t));
- int32_t pipefd[2];
- if(pipe(pipefd) < 0){
- printf("Failed to create pipe for new thread\n");
- return 1;
- }
- uint8_t *initial_data = calloc(1,stream_len);
- memcpy(initial_data, p, stream_len);
- struct proxy_thread_data *thread_data =
- calloc(1, sizeof(struct proxy_thread_data));
- thread_data->initial_data = initial_data;
- thread_data->initial_len = stream_len;
- thread_data->f = f;
- thread_data->stream_id = stream_id;
- thread_data->pipefd = pipefd[0];
-
- pthread_create(proxy_thread, NULL, proxy_covert_site, (void *) thread_data);
- //add stream to table
- stream *new_stream = calloc(1, sizeof(stream));
- new_stream->stream_id = stream_id;
- new_stream->pipefd = pipefd[1];
- if(streams->first == NULL){
- streams->first = new_stream;
- } else {
- stream *last = streams->first;
- while(last->next != NULL){
- last = last->next;
- }
- last->next = new_stream;
- }
- printf("Added new stream to table\n");
- fflush(stdout);
- }
- printf("Finished processing %d data\n", stream_len);
- fflush(stdout);
- output_len -= stream_len;
- p += stream_len;
- printf("Done adding thread\n");
- fflush(stdout);
- }
- printf("Finished processing bytes\n");
- fflush(stdout);
- printf("Freeing %p\n", upstream_data);
- fflush(stdout);
- free(upstream_data);
- }
- printf("Finished processing messages\n");
- fflush(stdout);
- //save a reference to the proxy threads in a global table
-
- /*int32_t bytes_written = write(pipefd[1], message,
- strlen( (const char *) message));
- if(bytes_written < strlen( (const char *) message)){
- printf("failed to write all bytes to pipe\n");
- }*/
- free(decrypted_data);
- if(record_ptr != NULL)
- free(record_ptr);
- return 0;
- err:
- if(upstream_data != NULL){
- printf("Freeing %p\n", upstream_data);
- fflush(stdout);
- free(upstream_data);
- }
- if(decrypted_data != NULL){
- free(decrypted_data);
- }
- return 1;
- }
- /** Called by spawned pthreads in read_header to send upstream
- * data to the censored site and receive responses. Downstream
- * data is stored in the flow's censored_queue. Function and
- * thread will terminate when the client closes the connection
- * to the covert destination
- *
- * Input:
- * A struct that contains the following information:
- * - the tagged flow
- * - the initial upstream data (including connect request)
- * - the read end of the pipe
- *
- */
- void *proxy_covert_site(void *data){
- struct proxy_thread_data *thread_data =
- (struct proxy_thread_data *) data;
- flow *f = thread_data->f;
- uint8_t *p = thread_data->initial_data;
- uint8_t stream_id = thread_data->stream_id;
- printf("request: ");
- for(int i=0; i< sizeof(struct socks_req); i++){
- printf("%02x ", p[i]);
- }
- printf("\n");
- fflush(stdout);
- struct socks_req *clnt_req = (struct socks_req *) p;
- p += 4;
- //see if it's a connect request
- if(clnt_req->cmd != 0x01){
- printf("Error: issued a non-connect command\n");
- fflush(stdout);
- goto err;
- }
- struct sockaddr_in dest;
- dest.sin_family = AF_INET;
- uint8_t domain_len;
- switch(clnt_req->addr_type){
- case 0x01:
- //IPv4
- dest.sin_addr.s_addr = *((uint32_t*) p);
- printf("destination addr: %d\n", ntohl(dest.sin_addr.s_addr));
- fflush(stdout);
- p += 4;
- break;
-
- case 0x03:
- //domain name
- domain_len = p[0];
- p++;
- uint8_t *domain_name = calloc(1, domain_len+1);
- memcpy(domain_name, p, domain_len);
- domain_name[domain_len] = '\0';
- struct hostent *host;
- host = gethostbyname((const char *) domain_name);
- dest.sin_addr = *((struct in_addr *) host->h_addr);
- printf("destination addr: %d\n", ntohl(dest.sin_addr.s_addr));
- fflush(stdout);
- p += domain_len;
- printf("Freeing %p\n", domain_name);
- fflush(stdout);
- free(domain_name);
- break;
- case 0x04:
- //IPv6
- goto err;//TODO: fix this
- break;
- }
- //now set the port
- dest.sin_port = *((uint16_t *) p);
- printf("destination port: %d\n", ntohs(dest.sin_port));
- fflush(stdout);
- int32_t handle = socket(AF_INET, SOCK_STREAM, 0);
- if(handle < 0){
- printf("error: constructing socket failed\n");
- fflush(stdout);
- goto err;
- }
- struct sockaddr_in my_addr;
- socklen_t my_addr_len = sizeof(my_addr);
- int32_t error = connect (handle, (struct sockaddr *) &dest, sizeof (struct sockaddr));
- if(error <0){
- printf("error connecting\n");
- fflush(stdout);
- goto err;
- }
- getsockname(handle, (struct sockaddr *) &my_addr, &my_addr_len);
- uint8_t *response = calloc(1, 11);
- //now send the reply to the client
- response[0] = 0x05;
- response[1] = 0x00;//TODO: make this accurate
- response[2] = 0x00;
- response[3] = 0x01;
- *((uint32_t *) (response + 4)) = my_addr.sin_addr.s_addr;
- *((uint16_t *) (response + 8)) = my_addr.sin_port;
- printf("Bound to %x:%d\n", my_addr.sin_addr.s_addr, ntohs(my_addr.sin_port));
- printf("Downstream response:\n");
- for(int i=0; i< 10; i++){
- printf("%02x ", response[i]);
- }
- printf("\n");
- fflush(stdout);
- queue_block *new_block = calloc(1, sizeof(queue_block));
- printf("Inserting new block into queue: %p\n", new_block);
- fflush(stdout);
- new_block->len = 10;
- new_block->offset = 0;
- new_block->data = response;
- new_block->next = NULL;
- new_block->stream_id = stream_id;
- printf("downstream_queue is at %p\n", downstream_queue);
- fflush(stdout);
- if(downstream_queue->first_block == NULL){
- downstream_queue->first_block = new_block;
- }
- else{
- queue_block *last = downstream_queue->first_block;
- while(last->next != NULL)
- last = last->next;
- last->next = new_block;
- }
- uint8_t *buffer = calloc(1, BUFSIZ);
- int32_t buffer_len = BUFSIZ;
- //now select on reading from the pipe and from the socket
- for(;;){
- fd_set readfds;
- fd_set writefds;
- int32_t nfds = (handle > thread_data->pipefd) ?
- handle +1 : thread_data->pipefd + 1;
- FD_ZERO(&readfds);
- FD_ZERO(&writefds);
- FD_SET(thread_data->pipefd, &readfds);
- FD_SET(handle, &readfds);
- FD_SET(handle, &writefds);
- if (select(nfds, &readfds, &writefds, NULL, NULL) < 0){
- printf("select error\n");
- fflush(stdout);
- }
- if(FD_ISSET(thread_data->pipefd, &readfds) && FD_ISSET(handle, &writefds)){
- //we have upstream data ready for writing
- printf("Passing along upstream data\n");
- fflush(stdout);
- int32_t bytes_read = read(thread_data->pipefd, buffer, buffer_len);
- printf("Read from stream pipe:\n");
- for(int i=0; i< bytes_read; i++){
- printf("%02x ", buffer[i]);
- }
- printf("\n");
- fflush(stdout);
- if(bytes_read > 0){
- int32_t bytes_sent = send(handle, buffer,
- bytes_read, 0);
- if( bytes_sent < 0){
- printf("error sending request\n");
- fflush(stdout);
- break;
- } else if (bytes_sent < bytes_read){
- //TODO: should update buffer and keep
- //track of length of upstream data
- printf("sent less than full upstream bytes\n");
- fflush(stdout);
- break;
- }
- }
- }
-
- if (FD_ISSET(handle, &readfds)){
- printf("Received downstream data\n");
- fflush(stdout);
- //we have downstream data read for saving
- int32_t bytes_read;
- bytes_read = recv(handle, buffer, buffer_len, 0);
- if(bytes_read <= 0){
- break;
- }
- if(bytes_read > 0){
- printf("Stream %d read %d downstream bytes:\n", stream_id, bytes_read);
- for(int i=0; i< bytes_read; i++){
- printf("%02x ", buffer[i]);
- }
- printf("\n");
- fflush(stdout);
- uint8_t *new_data = calloc(1, bytes_read);
- memcpy(new_data, buffer, bytes_read);
- //make a new queue block
- new_block = calloc(1, sizeof(queue_block));
- printf("Inserting new block into queue: %p\n", new_block);
- fflush(stdout);
- new_block->len = bytes_read;
- new_block->offset = 0;
- new_block->data = new_data;
- new_block->next = NULL;
- new_block->stream_id = stream_id;
- printf("downstream_queue is at %p\n", downstream_queue);
- fflush(stdout);
- if(downstream_queue->first_block == NULL){
- downstream_queue->first_block = new_block;
- }
- else{
- queue_block *last = downstream_queue->first_block;
- while(last->next != NULL)
- last = last->next;
- last->next = new_block;
- }
- } else {
- printf("read 0 bytes\n");
- fflush(stdout);
- }
- }
- }
- printf("Freeing: %p, %p\n", thread_data, buffer);
- free(thread_data);
- free(buffer);
- close(handle);
- return 0;
- err:
- if(thread_data->initial_data != NULL){
- printf("Freeing %p\n", thread_data->initial_data);
- fflush(stdout);
- free(thread_data->initial_data);
- }
- close(handle);
- return 0;
- }
- /** Replaces downstream record contents with data from the
- * censored queue, padding with garbage bytes if no more
- * censored data exists.
- *
- * Inputs:
- * f: the tagged flow
- * data: a pointer to the received packet's application
- * data
- * data_len: the length of the packet's application data
- * offset: if the packet is misordered, the number of
- * application-level bytes in missing packets
- *
- * Output:
- * Returns 0 on sucess
- */
- int process_downstream(flow *f, int32_t offset, struct packet_info *info){
- printf("Enter process_downstream\n");
- fflush(stdout);
- printf("Processing downstream data\n");
- uint8_t changed = 0;
- uint8_t *p = info->app_data;
- uint32_t remaining_packet_len = info->app_data_len;
- printf("This packet has %d bytes of application data\n", remaining_packet_len);
- printf("Sequence number: %u:%u\n", htonl(info->tcp_hdr->sequence_num), htonl(info->tcp_hdr->sequence_num)+ remaining_packet_len);
- fflush(stdout);
- if(f->remaining_record_len > 0){
- //ignore bytes until the end of the record
- if(f->remaining_record_len > remaining_packet_len){ //ignore entire packet
- if(f->outbox_len > 0){
- changed = 1;
- printf("First byte to copy: %02x %02x\n", f->outbox[f->outbox_offset], f->outbox[f->outbox_offset+1]);
- memcpy(p, f->outbox + f->outbox_offset, remaining_packet_len);
- f->outbox_len -= remaining_packet_len;
- f->outbox_offset += remaining_packet_len;
- printf("Copied %d bytes from outbox %p at offset %d, %d bytes left to ignore\n", remaining_packet_len, f->outbox, f->outbox_offset, f->outbox_len);
-
- }
- f->remaining_record_len -= remaining_packet_len;
- printf("Ignored/copied %d bytes, %d bytes left to ignore\n", remaining_packet_len, f->remaining_record_len);
- remaining_packet_len -= remaining_packet_len;
- } else {
- if(f->outbox_len > 0){
- changed = 1;
- memcpy(p, f->outbox + f->outbox_offset, f->remaining_record_len);
- if(f->outbox_len != f->remaining_record_len){
- printf("UH OH: outbox (%d bytes left) and record (%d bytes left) did not align\n", f->outbox_len, f->remaining_record_len);
- }
- printf("Copied %d bytes from outbox, 0 bytes left to ignore\n", f->remaining_record_len);
- printf("Copied %d bytes from outbox %p at offset %d, %d bytes left to ignore\n", f->remaining_record_len, f->outbox, f->outbox_offset, 0);
- printf("First byte copied: %02x %02x\n", p[0], p[1]);
- f->outbox_len = 0;
- f->outbox_offset=0;
- printf("Freeing %p\n", f->outbox);
- fflush(stdout);
- free(f->outbox);
- }
- p += f->remaining_record_len;
- printf("Ignoring %d bytes\n", f->remaining_record_len);
- remaining_packet_len -= f->remaining_record_len;
- f->remaining_record_len = 0;
- }
- }
- printf("There are now %d bytes remaining\n", remaining_packet_len);
- fflush(stdout);
- while(remaining_packet_len > 0){ //while bytes remain in the packet
-
- struct record_header *record_hdr = (struct record_header*) p;
- uint32_t record_len = RECORD_LEN(record_hdr);
- printf("Record length: %d\n", record_len);
- fprintf(stdout, "Record:\n");
- for(int i=0; i< RECORD_HEADER_LEN; i++){
- printf("%02x ", p[i]);
- }
- printf("\n");
- fflush(stdout);
- p += RECORD_HEADER_LEN;
- remaining_packet_len -= RECORD_HEADER_LEN;
- uint8_t *record_ptr = p; //points to the beginning of record data
- uint32_t remaining_record_len = record_len;
- if(record_len > remaining_packet_len){
- printf("Record is too long\n");
- fflush(stdout);
- f->remaining_record_len = record_len - remaining_packet_len;
- if(f->httpstate == PARSE_HEADER || f->httpstate == BEGIN_CHUNK || f->httpstate == END_CHUNK){
- f->httpstate = FORFEIT_REST;
- } else if( f->httpstate == MID_CONTENT || f->httpstate == MID_CHUNK){
- f->remaining_response_len -= record_len - 24; //len of IV and padding
- printf("remaining response len: %d\n", f->remaining_response_len);
- fflush(stdout);
- if(f->remaining_response_len >= 0 && f->replace_response){
- printf("Should be replacing this\n");
- fflush(stdout);
- //#ifdef nothing
- //make a huge record, encrypt it, and then place it in the outbox
- f->outbox = calloc(1, record_len+1);
- f->outbox_len = record_len;
- f->outbox_offset = 0;
- fill_with_downstream(f, f->outbox + EVP_GCM_TLS_EXPLICIT_IV_LEN , record_len - (EVP_GCM_TLS_EXPLICIT_IV_LEN+ 16)); //for now hard coded length of padding. TODO: fix this
- //encrypt
- printf("Encrypting outbox\n");
- fflush(stdout);
- int32_t n = encrypt(f, f->outbox, f->outbox,
- record_len - 16, 1,
- record_hdr->type, 1);
- if(n < 0){
- fprintf(stdout,"outbox encryption failed\n");
- fflush(stdout);
- } else {
-
- printf("Record len was: %d. Outbox len is: %d\n", record_len, n);
- fflush(stdout);
- memcpy(p, f->outbox, remaining_packet_len);
- changed = 1;
- f->outbox_len -= remaining_packet_len;
- f->outbox_offset += remaining_packet_len;
- printf("Copied %d from outbox. Offset is at %d\n", remaining_packet_len, f->outbox_offset);
- fflush(stdout);
- }
- printf("Outbox encryption successful!\n");
- fflush(stdout);
- //#endif
- } else if(f->replace_response){
- printf("record_len exceeds response len by %d\n", f->remaining_response_len);
- fflush(stdout);
- }
- if(f->remaining_response_len == 0){
- if(f->httpstate == MID_CHUNK)
- f->httpstate = END_CHUNK;
- else {
- printf("END_CONTENT\n");
- fflush(stdout);
- f->httpstate = PARSE_HEADER;
- }
- }
- if(f->remaining_response_len < 0){
- f->remaining_response_len = 0;
- f->httpstate = FORFEIT_REST;
- }
- }
- remaining_packet_len -= remaining_packet_len;
- break;
- }
- //now decrypt the record
- printf("Decrypting record\n");
- fflush(stdout);
- int32_t n = encrypt(f, record_ptr, record_ptr, record_len, 1,
- record_hdr->type, 0);
- if(n < 0){
- //do something smarter here
- fprintf(stdout,"application decryption failed\n");
- fflush(stdout);
- return 0;
- }
- changed = 1;
- printf("Decryption succeeded\n");
- fflush(stdout);
-
- printf("Bytes:\n");
- for(int i=0; i< n; i++){
- printf("%02x ", record_ptr[EVP_GCM_TLS_EXPLICIT_IV_LEN+i]);
- }
- printf("\n");
- printf("Text:\n");
- printf("%s\n", record_ptr+EVP_GCM_TLS_EXPLICIT_IV_LEN);
- fflush(stdout);
- p += EVP_GCM_TLS_EXPLICIT_IV_LEN;
- char *len_ptr, *needle;
- remaining_record_len = n;
- while(remaining_record_len > 0){
- switch(f->httpstate){
- case PARSE_HEADER:
- //determine whether it's transfer encoded or otherwise
- printf("IN PARSE HEADER\n");
- fflush(stdout);
- //figure out what the content-type is
- len_ptr = strstr((const char *) p, "Content-Type: image");
- if(len_ptr != NULL){
- printf("SLITHEEN: replaceable content\n");
- fflush(stdout);
- f->replace_response = 1;
- memcpy(len_ptr + 14, "slitheen", 8);
- char *c = len_ptr + 14+8;
- while(c[0] != '\r'){
- c[0] = ' ';
- c++;
- }
- } else {
- printf("SLITHEEN: non-replaceable content\n");
- fflush(stdout);
- f->replace_response = 0;
- }
- len_ptr = strstr((const char *) p, "Transfer-Encoding");
- if(len_ptr != NULL){
- if(!memcmp(len_ptr + 19, "chunked", 7)){
- printf("SLITHEEN: transfer chunked\n");
- fflush(stdout);
- //now find end of header
-
- len_ptr = strstr((const char *) p, "\r\n\r\n");
- if(len_ptr != NULL){
- f->httpstate = BEGIN_CHUNK;
- remaining_record_len -= (((uint8_t *)len_ptr - p) + 4);
- p = (uint8_t *) len_ptr + 4;
- printf("remaining record len: %d\n", remaining_record_len);
- fflush(stdout);
- }
- }
- } else {
- len_ptr = strstr((const char *) p, "Content-Length");
- if(len_ptr != NULL){
- len_ptr += 15;
- f->remaining_response_len = strtol((const char *) len_ptr, NULL, 10);
- printf("SLITHEEN: Content-Length: %d\n", f->remaining_response_len);
- fflush(stdout);
- len_ptr = strstr((const char *) p, "\r\n\r\n");
- if(len_ptr != NULL){
- f->httpstate = MID_CONTENT;
- remaining_record_len -= (((uint8_t *)len_ptr - p) + 4);
- p = (uint8_t *) len_ptr + 4;
- printf("remaining record len: %d\n", remaining_record_len);
- fflush(stdout);
- } else {
- printf("UH OH\n");
- fflush(stdout);
- remaining_record_len = 0;
- f->httpstate = FORFEIT_REST;
- }
- } else {
- f->httpstate = FORFEIT_REST;
- remaining_record_len = 0;
- }
- }
- break;
- case MID_CONTENT:
- //check if content is replaceable
- printf("In MID_CONTENT\n");
- fflush(stdout);
- if(f->remaining_response_len > remaining_record_len){
- if(f->replace_response){
- fill_with_downstream(f, p, remaining_record_len);
- printf("Replaced with:\n");
- fflush(stdout);
- for(int i=0; i< remaining_record_len; i++){
- printf("%02x ", p[i]);
- }
- printf("\n");
- } else {
- printf("Non-replaceable content\n");
- fflush(stdout);
- }
- f->remaining_response_len -= remaining_record_len;
- p += remaining_record_len;
-
- remaining_record_len = 0;
- printf("%d bytes left of body\n", f->remaining_response_len);
- fflush(stdout);
- } else {
- if(f->replace_response){
- fill_with_downstream(f, p, remaining_record_len);
- printf("Replaced with:\n");
- for(int i=0; i< remaining_record_len; i++){
- printf("%02x ", p[i]);
- }
- printf("\n");
- fflush(stdout);
- } else {
- printf("Non-replaceable content\n");
- fflush(stdout);
- }
- remaining_record_len -= f->remaining_response_len;
- p += f->remaining_response_len;
- f->httpstate = PARSE_HEADER;
- f->remaining_response_len = 0;
- printf("rem. response len = %d\n", f->remaining_response_len);
- fflush(stdout);
- }
- break;
- case BEGIN_CHUNK:
- printf("In BEGIN_CHUNK\n");
- fflush(stdout);
- int32_t chunk_size = strtol((const char *) p, NULL, 16);
- printf("chunk size: %x\n", chunk_size);
- fflush(stdout);
- if(chunk_size == 0){
- f->httpstate = END_BODY;
- } else {
- f->httpstate = MID_CHUNK;
- }
- f->remaining_response_len = chunk_size;
- needle = strstr((const char *) p, "\r\n");
- if(needle != NULL){
- remaining_record_len -= ((uint8_t *) needle - p + 2);
- p = (uint8_t *) needle + 2;
- printf("remaining_record len = %d\n", remaining_record_len);
- fflush(stdout);
- } else {
- printf("UH OH\n");
- fflush(stdout);
- remaining_record_len = 0;
- f->httpstate = FORFEIT_REST;
- }
- break;
- case MID_CHUNK:
- printf("In MID_CHUNK\n");
- fflush(stdout);
- if(f->remaining_response_len > remaining_record_len){
- if(f->replace_response){
- fill_with_downstream(f, p, remaining_record_len);
- printf("Replaced with:\n");
- for(int i=0; i< remaining_record_len; i++){
- printf("%02x ", p[i]);
- }
- printf("\n");
- fflush(stdout);
- }
- f->remaining_response_len -= remaining_record_len;
- p += remaining_record_len;
-
- remaining_record_len = 0;
- printf("%d bytes left of chunk\n", f->remaining_response_len);
- fflush(stdout);
- } else {
- if(f->replace_response){
- fill_with_downstream(f, p, remaining_record_len);
- printf("Replaced with:\n");
- for(int i=0; i< remaining_record_len; i++){
- printf("%02x ", p[i]);
- }
- printf("\n");
- fflush(stdout);
- }
- remaining_record_len -= f->remaining_response_len;
- p += f->remaining_response_len;
- f->httpstate = END_CHUNK;
- printf("rem. record len = %d\n", f->remaining_record_len);
- fflush(stdout);
- }
- break;
- case END_CHUNK:
- printf("In END_CHUNK\n");
- fflush(stdout);
- needle = strstr((const char *) p, "\r\n");
- if(needle != NULL){
- f->httpstate = BEGIN_CHUNK;
- p += 2;
- remaining_record_len -= 2;
- } else {
- printf("UH OH\n");
- fflush(stdout);
- remaining_record_len = 0;
- f->httpstate = FORFEIT_REST;
- }
- break;
- case END_BODY:
- printf("In END_BODY\n");
- fflush(stdout);
- needle = strstr((const char *) p, "\r\n");
- if(needle != NULL){
- f->httpstate = PARSE_HEADER;
- p += 2;
- remaining_record_len -= 2;
- } else {
- printf("UH OH\n");
- fflush(stdout);
- remaining_record_len = 0;
- f->httpstate = FORFEIT_REST;
- }
- break;
- case FORFEIT_REST:
- printf("In FORFEIT_REST\n");
- fflush(stdout);
- case USE_REST:
- remaining_record_len = 0;
- break;
- default:
- break;
- }
- }
- /*
- if(f->remaining_response_len > 0){
- if(f->ignore_response){
- uint32_t skip_len = (f->remaining_response_len > remaining_record_len) ?
- remaining_record_len : f->remaining_response_len;
- p += skip_len;
- remaining_record_len -= skip_len;
- f->remaining_response_len -= skip_len;
- } else {//replace with downstream data, for now ignore
- uint32_t skip_len = (f->remaining_response_len > remaining_record_len) ?
- remaining_record_len : f->remaining_response_len;
- p += skip_len;
- remaining_record_len -= skip_len;
- f->remaining_response_len -= skip_len;
- }
- }
- //cycle through all responses
- while(remaining_record_len > 0){
- uint32_t response_len = get_response_length(p);
- response_len = remaining_record_len;
- remaining_record_len -= response_len;
- p += response_len;
- }
- */
- printf("Re-encrypting record\n");
- fflush(stdout);
- if((n = encrypt(f, record_ptr, record_ptr,
- n + EVP_GCM_TLS_EXPLICIT_IV_LEN, 1, record_hdr->type,
- 1)) < 0){
- fprintf(stdout,"encryption failed\n");
- return 0;
- }
- printf("Encryption successful!\n");
- fflush(stdout);
- p = record_ptr + record_len;
- remaining_packet_len -= record_len;
- }
- /*
- uint8_t *p = info->app_data;
- int32_t tmp_len = info->app_data_len;
- printf("remaining packet length: %d\n", tmp_len);
- //step 1: replace record contents
- //note: encrypted message will be original message size + EVP_GCM_TLS_EXPLICIT_IV_LEN + 16 byte pad
- //first check to see if there's anything in the outbox
- if(f->outbox_len > 0){
- #ifdef DEBUG
- if(f->outbox_len < info->app_data_len){
- printf("Next record:\n");
- for(int i=0; i< RECORD_HEADER_LEN; i++){
- printf("%02x ", p[f->outbox_len+i]);
- }
- printf("\n");
- } else {
- printf("Outbox takes up entire packet\n");
- }
- #endif
- //If the outbox contains data we can't change, just forward it as is.
- if(f->outbox_data_len > 0){
- if(tmp_len >= f->outbox_data_len){
- printf("There are %d bytes in the outbox\n", f->outbox_data_len);
- memcpy(p, f->outbox, f->outbox_data_len);
- p += f->outbox_data_len;
- tmp_len -= f->outbox_data_len;
- f->outbox_len -= f->outbox_data_len;
- f->outbox_data_len = 0;
- free(f->outbox);
- //now see if there are remainders of the encrypted record to send...
- if(tmp_len > 0){
- if(tmp_len >= f->outbox_len){
- p += f->outbox_len;
- tmp_len -= f->outbox_len;
- f->outbox_len = 0;
- } else {
- f->outbox_len -= tmp_len;
- tmp_len -= tmp_len;
- }
- }
- } else {
- memcpy(p, f->outbox, tmp_len);
- uint8_t *tmp = calloc(1, f->outbox_data_len - tmp_len);
- f->outbox_data_len -= tmp_len;
- f->outbox_len -= tmp_len;
- memcpy(tmp, f->outbox + tmp_len, f->outbox_data_len);
- free(f->outbox);
- f->outbox = tmp;
- tmp_len -= tmp_len;
- }
- } else {
- //all we have are ingored bytes
- if(tmp_len >= f->outbox_len){
- printf("ignoring %d bytes\n", f->outbox_len);
- p += f->outbox_len;
- tmp_len -= f->outbox_len;
- f->outbox_len = 0;
- } else {
- printf("ignoring %d bytes\n", tmp_len);
- f->outbox_len -= tmp_len;
- tmp_len -= tmp_len;
- }
- }
- }
- while(tmp_len > 0){
- printf("remaining packet length: %d\n", tmp_len);
- struct record_header *record_hdr = (struct record_header*) p;
- uint32_t record_length = RECORD_LEN(record_hdr);
- printf("record length: %d\n", record_length);
- #ifdef DEBUG
- fprintf(stdout, "Record:\n");
- for(int i=0; i< RECORD_HEADER_LEN; i++){
- printf("%02x ", p[i]);
- }
- printf("\n");
- #endif
- p += RECORD_HEADER_LEN;
- if(record_length > tmp_len){
- //the packet is smaller than the record, we'll just forfeit the data
- printf("Cannot see all of record\n");
- tmp_len -= RECORD_HEADER_LEN;
- f->outbox_len = record_length - tmp_len;
- f->outbox_data_len = 0;
- printf("ignoring %d bytes\n", tmp_len);
- break;
- }
- if(record_hdr->type != 0x17){
- //TODO: might need to decrypt and re-encrypt
- //printf("received non-application data\n");
- tmp_len -= (record_length+ RECORD_HEADER_LEN);
- p += record_length;
- continue;
- }
- //We are at a record boundary: decrypt record and change "leaves"
- if(!encrypt(f, p, p, record_length, 1, record_hdr->type, 0)){
- fprintf(stdout,"decryption failed\n");
- return 0;
- }
- printf("encryption successful\n");
- //look for content type
- const char *needle = "Content-Type";
- const char *type = strstr((const char *) p, needle);
- if(type == NULL){
- printf("No content-type header\n");
- tmp_len -= RECORD_HEADER_LEN + record_length;
- p += record_length;
- continue;
- }
- uint8_t *substr = calloc(1, 32);
- memcpy(substr, type, 32);
- if(strstr((const char *) substr, "img") == NULL){
- //we're ignoring this
- printf("This is not a leaf node\n");
- tmp_len -= RECORD_HEADER_LEN + record_length;
- p += record_length;
- free(substr);
- continue;
- }
- free(substr);
- if(type != NULL){
- while((*type != '\r') && (*type != ';')){
- printf("%c", *type);
- type ++;
- }
- printf("\n");
- } else {
- printf("No content-type header\n");
- }
- uint8_t *new_record = calloc(1, record_length);
- memcpy(new_record, p, record_length);
- uint8_t *tmp_p = new_record;
- tmp_p += EVP_GCM_TLS_EXPLICIT_IV_LEN;
-
- struct slitheen_header *sl_hdr = (struct slitheen_header *) tmp_p;
- sl_hdr->marker = 0x01;
- sl_hdr->version = 0x01;
- sl_hdr->len = 0x00;
- int32_t remaining = record_length - (SLITHEEN_HEADER_LEN
- + EVP_GCM_TLS_EXPLICIT_IV_LEN + 16);
- tmp_p += SLITHEEN_HEADER_LEN;
- //Fill as much as we can from the censored_queue
- while((remaining > 0) && f->censored_queue != NULL){
- int32_t block_length = f->censored_queue->len;
- int32_t offset = f->censored_queue->offset;
- #ifdef DEBUG
- printf("Censored queue is at %p.\n", f->censored_queue);
- printf("This block has %d bytes left\n", block_length - offset);
- printf("We need %d bytes\n", remaining);
- #endif
-
- if(block_length > offset + remaining){
- //use part of the block, update offset
- memcpy(tmp_p, f->censored_queue->data+offset, remaining);
- f->censored_queue->offset += remaining;
- tmp_p += remaining;
- sl_hdr->len += remaining;
- remaining -= remaining;
- } else {
- //use all of the block and free it
- memcpy(tmp_p, f->censored_queue->data+offset, block_length - offset);
- free(f->censored_queue->data);
- f->censored_queue = f->censored_queue->next;
- tmp_p += (block_length - offset);
- sl_hdr->len += (block_length - offset);
- remaining -= (block_length - offset);
- }
- }
- sl_hdr->len = htons(sl_hdr->len);
- //now, if we need more data, fill with garbage
- if(remaining >0 ){
- //TODO: note, we may also be receiving misordered packets. Take Ian's suggestion into account here
- memset(tmp_p, 'A', remaining);
- }
- tmp_p = new_record;
- #ifdef DEBUG
- fprintf(stdout, "copied %d data and %d garbage bytes\n", ntohs(sl_hdr->len), remaining);
- printf("Slitheen header\n");
- for(int i=0; i<4; i++)
- printf("%02x ", tmp_p[EVP_GCM_TLS_EXPLICIT_IV_LEN+i]);
- printf("\n");
- #endif
- reencrypt:
- //step 3: encrypt new record
- int32_t success;
- if((success = encrypt(f, tmp_p, tmp_p, record_length-16, 1, 0x17, 1))< 0){
- fprintf(stdout,"encryption failed\n");
- return 0;
- }
- //copy new record into packet
- if(record_length +RECORD_HEADER_LEN > tmp_len){
- //We have a partial record
- memcpy(p, new_record, tmp_len - RECORD_HEADER_LEN);
- f->outbox_data_len = record_length - (tmp_len - RECORD_HEADER_LEN);
- //save left-overs in outbox
- f->outbox = calloc(1, f->outbox_data_len);
- memcpy(f->outbox, new_record + (tmp_len - RECORD_HEADER_LEN),
- f->outbox_data_len);
- free(new_record);
- f->outbox_len = f->outbox_data_len;
- } else {
- memcpy(p, new_record, record_length);
- free(new_record);
- }
- #ifdef DEBUG
- //check to see if next record still exists
- if(info->app_data_len > record_length + RECORD_HEADER_LEN){
- printf("Next record:\n");
- for(int i=0; i< RECORD_HEADER_LEN; i++){
- printf("%02x ", p[record_length+i]);
- }
- printf("\n");
- } else {
- printf("No extra record: %d <= %d + %d\n", data_len, record_length, RECORD_HEADER_LEN);
- }
- #endif
- tmp_len -= record_length+ RECORD_HEADER_LEN;
- p += record_length;
- }
- */
- //step 4: recompute TCP checksum
- if(changed){
- uint16_t sum = tcp_checksum(info);
- printf("Computed checksum: %u\n", sum);
- fflush(stdout);
- }
- printf("Done process_downstream\n");
- fflush(stdout);
- return 0;
- }
- int fill_with_downstream(flow *f, uint8_t *data, int32_t length){
- printf("Enter filling with ds data\n");
- fflush(stdout);
- uint8_t *p = data;
- int32_t remaining = length;
- struct slitheen_header *sl_hdr;
- //Fill as much as we can from the censored_queue
- while((remaining > SLITHEEN_HEADER_LEN) && downstream_queue->first_block != NULL){
- queue_block *first_block = downstream_queue->first_block;
- int32_t block_length = first_block->len;
- int32_t offset = first_block->offset;
- //#ifdef DEBUG
- printf("Censored queue is at %p.\n", first_block);
- printf("This block has %d bytes left\n", block_length - offset);
- printf("We need %d bytes\n", remaining - SLITHEEN_HEADER_LEN);
- //#endif
-
- sl_hdr = (struct slitheen_header *) p;
- sl_hdr->stream_id = first_block->stream_id;
- sl_hdr->len = 0x00;
- sl_hdr->garbage = 0x00;
- p += SLITHEEN_HEADER_LEN;
- remaining -= SLITHEEN_HEADER_LEN;
- if(block_length > offset + remaining){
- //use part of the block, update offset
- memcpy(p, first_block->data+offset, remaining);
- first_block->offset += remaining;
- p += remaining;
- sl_hdr->len = remaining;
- remaining -= remaining;
- } else {
- //use all of the block and free it
- memcpy(p, first_block->data+offset, block_length - offset);
- printf("Freeing first block data (%p)\n", first_block->data);
- fflush(stdout);
- free(first_block->data);
- printf("Freed first block data\n");
- fflush(stdout);
- downstream_queue->first_block = first_block->next;
- free(first_block);
- printf("Freed first block\n");
- fflush(stdout);
- p += (block_length - offset);
- sl_hdr->len = (block_length - offset);
- remaining -= (block_length - offset);
- }
- sl_hdr->len = htons(sl_hdr->len);
- printf("DWNSTRM: slitheen header: ");
- for(int i=0; i< SLITHEEN_HEADER_LEN; i++){
- printf("%02x ",((uint8_t *) sl_hdr)[i]);
- }
- printf("\n");
- }
- //now, if we need more data, fill with garbage
- if(remaining > SLITHEEN_HEADER_LEN ){
- //TODO: note, we may also be receiving misordered packets. Take Ian's suggestion into account here
- sl_hdr = (struct slitheen_header *) p;
- sl_hdr->stream_id = 0x00;
- remaining -= SLITHEEN_HEADER_LEN;
- sl_hdr->len = htons(remaining);
- sl_hdr->garbage = htons(remaining);
- printf("DWNSTRM: slitheen header: ");
- for(int i=0; i< SLITHEEN_HEADER_LEN; i++){
- printf("%02x ", p[i]);
- }
- printf("\n");
- p += SLITHEEN_HEADER_LEN;
- memset(p, 'A', remaining);
- }
- printf("Done filling with ds data\n");
- fflush(stdout);
- return 0;
- }
- /** Computes the TCP checksum of the data according to RFC 793
- * sum all 16-bit words in the segment, padd the last word if
- * needed
- *
- * there is a pseudo-header prefixed to the segment and
- * included in the checksum:
- *
- * +--------+--------+--------+--------+
- * | Source Address |
- * +--------+--------+--------+--------+
- * | Destination Address |
- * +--------+--------+--------+--------+
- * | zero | PTCL | TCP Length |
- * +--------+--------+--------+--------+
- */
- uint16_t tcp_checksum(struct packet_info *info){
- uint16_t tcp_length = info->app_data_len + info->size_tcp_hdr;
- struct in_addr src = info->ip_hdr->src;
- struct in_addr dst = info->ip_hdr->dst;
- uint8_t proto = IPPROTO_TCP;
- //set the checksum to zero
- info->tcp_hdr->chksum = 0;
-
- //sum pseudoheader
- uint32_t sum = (ntohl(src.s_addr)) >> 16;
- sum += (ntohl(src.s_addr)) &0xFFFF;
- sum += (ntohl(dst.s_addr)) >> 16;
- sum += (ntohl(dst.s_addr)) & 0xFFFF;
- sum += proto;
- sum += tcp_length;
- //sum tcp header (with zero-d checksum)
- uint8_t *p = (uint8_t *) info->tcp_hdr;
- for(int i=0; i < info->size_tcp_hdr; i+=2){
- sum += (uint16_t) ((p[i] << 8) + p[i+1]);
- }
- //now sum the application data
- p = info->app_data;
- for(int i=0; i< info->app_data_len-1; i+=2){
- sum += (uint16_t) ((p[i] << 8) + p[i+1]);
- }
- if(info->app_data_len %2 != 0){
- sum += (uint16_t) (p[info->app_data_len - 1]) << 8;
- }
- //now add most significant to last significant bits
- sum = (sum >> 16) + (sum & 0xFFFF);
- sum += sum >>16;
- //now subtract from 0xFF
- sum = 0xFFFF - sum;
- //set chksum to calculated value
- info->tcp_hdr->chksum = ntohs(sum);
- return (uint16_t) sum;
- }
|