123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467 |
- #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;
- }
- struct record_header *record_hdr = (struct record_header*) p;
- uint32_t record_length = RECORD_LEN(record_hdr);
- printf("HERE\n");
- fflush(stdout);
- uint8_t *decrypted_data = calloc(1, info->app_data_len);
- 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)){
- fprintf(stdout,"upstream decryption failed\n");
- fflush(stdout);
- 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");
- 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'){
- 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);
- 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;
- }
|