|
@@ -10,14 +10,14 @@
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
* GNU General Public License for more details.
|
|
- *
|
|
|
|
|
|
+ *
|
|
* You should have received a copy of the GNU General Public License
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*
|
|
*
|
|
* Additional permission under GNU GPL version 3 section 7
|
|
* Additional permission under GNU GPL version 3 section 7
|
|
- *
|
|
|
|
|
|
+ *
|
|
* If you modify this Program, or any covered work, by linking or combining
|
|
* If you modify this Program, or any covered work, by linking or combining
|
|
- * it with the OpenSSL library (or a modified version of that library),
|
|
|
|
|
|
+ * it with the OpenSSL library (or a modified version of that library),
|
|
* containing parts covered by the terms of the OpenSSL Licence and the
|
|
* containing parts covered by the terms of the OpenSSL Licence and the
|
|
* SSLeay license, the licensors of this Program grant you additional
|
|
* SSLeay license, the licensors of this Program grant you additional
|
|
* permission to convey the resulting work. Corresponding Source for a
|
|
* permission to convey the resulting work. Corresponding Source for a
|
|
@@ -60,14 +60,14 @@ typedef struct {
|
|
|
|
|
|
int main(void){
|
|
int main(void){
|
|
int listen_socket;
|
|
int listen_socket;
|
|
-
|
|
|
|
|
|
+
|
|
struct sockaddr_in address;
|
|
struct sockaddr_in address;
|
|
struct sockaddr_in remote_addr;
|
|
struct sockaddr_in remote_addr;
|
|
socklen_t addr_size;
|
|
socklen_t addr_size;
|
|
|
|
|
|
connections = calloc(1, sizeof(connection_table));
|
|
connections = calloc(1, sizeof(connection_table));
|
|
connections->first = NULL;
|
|
connections->first = NULL;
|
|
-
|
|
|
|
|
|
+
|
|
int32_t ous_in[2];
|
|
int32_t ous_in[2];
|
|
if(pipe(ous_in) < 0){
|
|
if(pipe(ous_in) < 0){
|
|
printf("Failed to create pipe\n");
|
|
printf("Failed to create pipe\n");
|
|
@@ -137,7 +137,7 @@ int main(void){
|
|
addr_size = sizeof(remote_addr);
|
|
addr_size = sizeof(remote_addr);
|
|
int new_socket;
|
|
int new_socket;
|
|
new_socket = accept(listen_socket, (struct sockaddr *) &remote_addr,
|
|
new_socket = accept(listen_socket, (struct sockaddr *) &remote_addr,
|
|
- &addr_size);
|
|
|
|
|
|
+ &addr_size);
|
|
if(new_socket < 0){
|
|
if(new_socket < 0){
|
|
perror("accept");
|
|
perror("accept");
|
|
exit(1);
|
|
exit(1);
|
|
@@ -147,11 +147,11 @@ int main(void){
|
|
//assign a new stream_id and create a pipe for the session
|
|
//assign a new stream_id and create a pipe for the session
|
|
connection *new_conn = calloc(1, sizeof(connection));
|
|
connection *new_conn = calloc(1, sizeof(connection));
|
|
new_conn->stream_id = last_id++;
|
|
new_conn->stream_id = last_id++;
|
|
-
|
|
|
|
|
|
+
|
|
new_conn->socket = new_socket;
|
|
new_conn->socket = new_socket;
|
|
new_conn->state = NEW_STREAM;
|
|
new_conn->state = NEW_STREAM;
|
|
new_conn->next = NULL;
|
|
new_conn->next = NULL;
|
|
-
|
|
|
|
|
|
+
|
|
if(connections->first == NULL){
|
|
if(connections->first == NULL){
|
|
connections->first = new_conn;
|
|
connections->first = new_conn;
|
|
printf("Added first connection with id: %d\n", new_conn->stream_id);
|
|
printf("Added first connection with id: %d\n", new_conn->stream_id);
|
|
@@ -234,6 +234,9 @@ void *ous_IO(void *args){
|
|
uint8_t *buffer = emalloc(BUFSIZ);
|
|
uint8_t *buffer = emalloc(BUFSIZ);
|
|
int32_t buffer_len = BUFSIZ;
|
|
int32_t buffer_len = BUFSIZ;
|
|
|
|
|
|
|
|
+ uint8_t *input_buffer = NULL;
|
|
|
|
+ uint32_t input_buffer_len;
|
|
|
|
+
|
|
int32_t bytes_read;
|
|
int32_t bytes_read;
|
|
|
|
|
|
/* Select on proxy pipes, demux thread, and ous to send and receive data*/
|
|
/* Select on proxy pipes, demux thread, and ous to send and receive data*/
|
|
@@ -303,7 +306,7 @@ void *ous_IO(void *args){
|
|
|
|
|
|
if(FD_ISSET(ous, &read_fds) && FD_ISSET(ous_out, &write_fds)){
|
|
if(FD_ISSET(ous, &read_fds) && FD_ISSET(ous_out, &write_fds)){
|
|
|
|
|
|
- bytes_read = recv(ous, buffer, 4, 0);
|
|
|
|
|
|
+ bytes_read = recv(ous, (uint8_t *) &input_buffer_len, 4, 0);
|
|
#ifdef DEBUG_IO
|
|
#ifdef DEBUG_IO
|
|
printf("Received %d bytes from OUS\n", bytes_read);
|
|
printf("Received %d bytes from OUS\n", bytes_read);
|
|
for(int i=0; i< bytes_read; i++){
|
|
for(int i=0; i< bytes_read; i++){
|
|
@@ -318,27 +321,29 @@ void *ous_IO(void *args){
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
- uint32_t *chunk_len = (uint32_t*) buffer;
|
|
|
|
-
|
|
|
|
- fprintf(stderr, "Length of this chunk: %u\n", *chunk_len);
|
|
|
|
|
|
+ uint32_t chunk_len = input_buffer_len;
|
|
|
|
|
|
-
|
|
|
|
- bytes_read = recv(ous, buffer, *chunk_len, 0);
|
|
|
|
|
|
+ bytes_sent = write(ous_out, (uint8_t *) &input_buffer_len, bytes_read);
|
|
|
|
+ //TODO: check return
|
|
|
|
+
|
|
|
|
+ input_buffer = malloc(input_buffer_len);
|
|
|
|
+
|
|
|
|
+ bytes_read = recv(ous, input_buffer, chunk_len, 0);
|
|
#ifdef DEBUG_IO
|
|
#ifdef DEBUG_IO
|
|
printf("Received %d bytes from OUS\n", bytes_read);
|
|
printf("Received %d bytes from OUS\n", bytes_read);
|
|
for(int i=0; i< bytes_read; i++){
|
|
for(int i=0; i< bytes_read; i++){
|
|
- printf("%02x ", buffer[i]);
|
|
|
|
|
|
+ printf("%02x ", input_buffer[i]);
|
|
}
|
|
}
|
|
printf("\n");
|
|
printf("\n");
|
|
fflush(stdout);
|
|
fflush(stdout);
|
|
#endif
|
|
#endif
|
|
|
|
|
|
if(bytes_read > 0){
|
|
if(bytes_read > 0){
|
|
- bytes_sent = write(ous_out, buffer, bytes_read);
|
|
|
|
|
|
+ bytes_sent = write(ous_out, input_buffer, bytes_read);
|
|
#ifdef DEBUG_IO
|
|
#ifdef DEBUG_IO
|
|
printf("Sent %d bytes to demultiplexer\n", bytes_sent);
|
|
printf("Sent %d bytes to demultiplexer\n", bytes_sent);
|
|
for(int i=0; i< bytes_sent; i++){
|
|
for(int i=0; i< bytes_sent; i++){
|
|
- printf("%02x ", buffer[i]);
|
|
|
|
|
|
+ printf("%02x ", input_buffer[i]);
|
|
}
|
|
}
|
|
printf("\n");
|
|
printf("\n");
|
|
fflush(stdout);
|
|
fflush(stdout);
|
|
@@ -358,6 +363,9 @@ void *ous_IO(void *args){
|
|
fprintf(stderr, "Error reading from OUS\n");
|
|
fprintf(stderr, "Error reading from OUS\n");
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ free(input_buffer);
|
|
|
|
+ input_buffer = NULL;
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
@@ -560,7 +568,7 @@ void *multiplex_data(void *args){
|
|
#ifdef DEBUG_UPSTREAM
|
|
#ifdef DEBUG_UPSTREAM
|
|
printf("Received %d data bytes from sockfd (id %d):\n", bytes_read, conn->stream_id);
|
|
printf("Received %d data bytes from sockfd (id %d):\n", bytes_read, conn->stream_id);
|
|
for(int i=0; i< bytes_read; i++){
|
|
for(int i=0; i< bytes_read; i++){
|
|
- printf("%02x ", buffer[i]);
|
|
|
|
|
|
+ printf("%02x ", buffer[i]);
|
|
}
|
|
}
|
|
printf("\n");
|
|
printf("\n");
|
|
printf("%s\n", buffer);
|
|
printf("%s\n", buffer);
|
|
@@ -612,8 +620,7 @@ void *multiplex_data(void *args){
|
|
void *demultiplex_data(void *args){
|
|
void *demultiplex_data(void *args){
|
|
ous_pipes *pipes = (ous_pipes *) args;
|
|
ous_pipes *pipes = (ous_pipes *) args;
|
|
|
|
|
|
- int32_t buffer_len = BUFSIZ;
|
|
|
|
- uint8_t *buffer = calloc(1, buffer_len);
|
|
|
|
|
|
+ uint8_t *buffer = NULL;
|
|
uint8_t *p;
|
|
uint8_t *p;
|
|
|
|
|
|
uint8_t *partial_block = NULL;
|
|
uint8_t *partial_block = NULL;
|
|
@@ -622,208 +629,235 @@ void *demultiplex_data(void *args){
|
|
data_block *saved_data = NULL;
|
|
data_block *saved_data = NULL;
|
|
|
|
|
|
for(;;){
|
|
for(;;){
|
|
- printf("Demux thread waiting to read\n");
|
|
|
|
- int32_t bytes_read = read(pipes->out, buffer, buffer_len-partial_block_len);
|
|
|
|
-
|
|
|
|
- if(bytes_read > 0){
|
|
|
|
- int32_t chunk_remaining = bytes_read;
|
|
|
|
- p = buffer;
|
|
|
|
-
|
|
|
|
- //didn't read a full slitheen block last time
|
|
|
|
- if(partial_block_len > 0){
|
|
|
|
- //process first part of slitheen info
|
|
|
|
- memmove(buffer+partial_block_len, buffer, bytes_read);
|
|
|
|
- memcpy(buffer, partial_block, partial_block_len);
|
|
|
|
- chunk_remaining += partial_block_len;
|
|
|
|
- free(partial_block);
|
|
|
|
- partial_block = NULL;
|
|
|
|
- partial_block_len = 0;
|
|
|
|
- }
|
|
|
|
|
|
+ uint32_t chunk_len;
|
|
|
|
|
|
- while(chunk_remaining > 0){
|
|
|
|
|
|
+ int32_t bytes_read = read(pipes->out, (uint8_t *) &chunk_len, 4);
|
|
|
|
|
|
-#ifdef DEBUG_PARSE
|
|
|
|
- printf("Received a new chunk of len %d bytes\n", chunk_remaining);
|
|
|
|
-#endif
|
|
|
|
|
|
+ fprintf(stdout, "Length of this chunk: %u\n", chunk_len);
|
|
|
|
|
|
- if(chunk_remaining < SLITHEEN_HEADER_LEN){
|
|
|
|
|
|
+ buffer = calloc(1, chunk_len);
|
|
|
|
+
|
|
|
|
+ bytes_read = read(pipes->out, buffer, chunk_len);
|
|
|
|
+
|
|
|
|
+ if(bytes_read <= 0){
|
|
|
|
+ printf("Error: read %d bytes from OUS_out\n", bytes_read);
|
|
|
|
+ goto err;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if(bytes_read < chunk_len) {
|
|
|
|
+ printf("Error: read %d out of %d bytes\n", bytes_read, chunk_len);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ int32_t chunk_remaining = bytes_read;
|
|
|
|
+ p = buffer;
|
|
|
|
|
|
#ifdef DEBUG_PARSE
|
|
#ifdef DEBUG_PARSE
|
|
- printf("Partial header: ");
|
|
|
|
- int i;
|
|
|
|
- for(i = 0; i< chunk_remaining; i++){
|
|
|
|
- printf("%02x ", p[i]);
|
|
|
|
- }
|
|
|
|
- printf("\n");
|
|
|
|
|
|
+ printf("Received a new chunk of len %d bytes\n", chunk_remaining);
|
|
#endif
|
|
#endif
|
|
|
|
+ //didn't read a full slitheen block last time
|
|
|
|
+ if(partial_block_len > 0){
|
|
|
|
+ //process first part of slitheen info
|
|
|
|
+ memmove(buffer+partial_block_len, buffer, bytes_read);
|
|
|
|
+ memcpy(buffer, partial_block, partial_block_len);
|
|
|
|
+ chunk_remaining += partial_block_len;
|
|
|
|
+ free(partial_block);
|
|
|
|
+ partial_block = NULL;
|
|
|
|
+ partial_block_len = 0;
|
|
|
|
+ }
|
|
|
|
|
|
- if(partial_block != NULL) printf("UH OH (PB)\n");
|
|
|
|
- partial_block = calloc(1, chunk_remaining);
|
|
|
|
- memcpy(partial_block, p, chunk_remaining);
|
|
|
|
- partial_block_len = chunk_remaining;
|
|
|
|
- chunk_remaining = 0;
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
|
|
+ while(chunk_remaining > 0){
|
|
|
|
+ /*TODO: investigate assumption that we only ever receive chunks
|
|
|
|
+ * that contain full slitheen blocks */
|
|
|
|
|
|
- //decrypt header to see if we have entire block
|
|
|
|
- uint8_t *tmp_header = malloc(SLITHEEN_HEADER_LEN);
|
|
|
|
- memcpy(tmp_header, p, SLITHEEN_HEADER_LEN);
|
|
|
|
|
|
+#ifdef DEBUG_PARSE
|
|
|
|
+ printf("Chunk remaining: %d bytes\n", chunk_remaining);
|
|
|
|
+#endif
|
|
|
|
|
|
- if(!peek_header(tmp_header)){
|
|
|
|
- printf("This chunk doesn't contain a Slitheen block\n");
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
|
|
+ if(chunk_remaining < SLITHEEN_HEADER_LEN){
|
|
|
|
|
|
- struct slitheen_hdr *sl_hdr = (struct slitheen_hdr *) tmp_header;
|
|
|
|
- //first see if sl_hdr corresponds to a valid stream. If not, ignore rest of read bytes
|
|
|
|
#ifdef DEBUG_PARSE
|
|
#ifdef DEBUG_PARSE
|
|
- printf("Slitheen header:\n");
|
|
|
|
|
|
+ printf("Incomplete Slitheen block: ");
|
|
int i;
|
|
int i;
|
|
- for(i = 0; i< SLITHEEN_HEADER_LEN; i++){
|
|
|
|
- printf("%02x ", tmp_header[i]);
|
|
|
|
|
|
+ for(i = 0; i< chunk_remaining; i++){
|
|
|
|
+ printf("%02x ", p[i]);
|
|
}
|
|
}
|
|
printf("\n");
|
|
printf("\n");
|
|
#endif
|
|
#endif
|
|
- if(ntohs(sl_hdr->len) > chunk_remaining){
|
|
|
|
- printf("ERROR: slitheen block doesn't fit in resource remaining!\n");
|
|
|
|
- printf("Saving in partial block\n");
|
|
|
|
-
|
|
|
|
- if(partial_block != NULL) printf("UH OH (PB)\n");
|
|
|
|
- partial_block = calloc(1, ntohs(sl_hdr->len));
|
|
|
|
- memcpy(partial_block, p, chunk_remaining);
|
|
|
|
- partial_block_len = chunk_remaining;
|
|
|
|
- chunk_remaining = 0;
|
|
|
|
- free(tmp_header);
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
|
|
+ /*
|
|
|
|
+ if(partial_block != NULL) printf("UH OH (PB)\n");
|
|
|
|
+
|
|
|
|
+ partial_block = calloc(1, chunk_remaining);
|
|
|
|
+ memcpy(partial_block, p, chunk_remaining);
|
|
|
|
+ partial_block_len = chunk_remaining;
|
|
|
|
+ chunk_remaining = 0;
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
|
|
- super_decrypt(p);
|
|
|
|
|
|
+ //decrypt header to see if we have entire block
|
|
|
|
+ uint8_t *tmp_header = malloc(SLITHEEN_HEADER_LEN);
|
|
|
|
+ memcpy(tmp_header, p, SLITHEEN_HEADER_LEN);
|
|
|
|
|
|
- sl_hdr = (struct slitheen_hdr *) p;
|
|
|
|
|
|
+ if(!peek_header(tmp_header)){
|
|
|
|
+ printf("This chunk doesn't contain a Slitheen block\n");
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ struct slitheen_hdr *sl_hdr = (struct slitheen_hdr *) tmp_header;
|
|
|
|
+ //first see if sl_hdr corresponds to a valid stream. If not, ignore rest of read bytes
|
|
|
|
+#ifdef DEBUG
|
|
|
|
+ printf("Slitheen header:\n");
|
|
|
|
+ int i;
|
|
|
|
+ for(i = 0; i< SLITHEEN_HEADER_LEN; i++){
|
|
|
|
+ printf("%02x ", tmp_header[i]);
|
|
|
|
+ }
|
|
|
|
+ printf("\n");
|
|
|
|
+#endif
|
|
|
|
+ if(ntohs(sl_hdr->len) > chunk_remaining){
|
|
|
|
+ printf("ERROR: slitheen block doesn't fit in resource remaining!\n");
|
|
|
|
+ printf("Saving in partial block\n");
|
|
|
|
+
|
|
|
|
+ if(partial_block != NULL) printf("UH OH (PB)\n");
|
|
|
|
+ partial_block = calloc(1, ntohs(sl_hdr->len));
|
|
|
|
+ memcpy(partial_block, p, chunk_remaining);
|
|
|
|
+ partial_block_len = chunk_remaining;
|
|
|
|
+ chunk_remaining = 0;
|
|
free(tmp_header);
|
|
free(tmp_header);
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ super_decrypt(p);
|
|
|
|
|
|
- p += SLITHEEN_HEADER_LEN;
|
|
|
|
- chunk_remaining -= SLITHEEN_HEADER_LEN;
|
|
|
|
|
|
+ sl_hdr = (struct slitheen_hdr *) p;
|
|
|
|
+ free(tmp_header);
|
|
|
|
|
|
- if((!sl_hdr->len) && (sl_hdr->garbage)){
|
|
|
|
|
|
+ p += SLITHEEN_HEADER_LEN;
|
|
|
|
+ chunk_remaining -= SLITHEEN_HEADER_LEN;
|
|
|
|
+
|
|
|
|
+ if((!sl_hdr->len) && (sl_hdr->garbage)){
|
|
|
|
|
|
#ifdef DEBUG_PARSE
|
|
#ifdef DEBUG_PARSE
|
|
- printf("%d Garbage bytes\n", ntohs(sl_hdr->garbage));
|
|
|
|
|
|
+ printf("%d Garbage bytes\n", ntohs(sl_hdr->garbage));
|
|
#endif
|
|
#endif
|
|
- p += ntohs(sl_hdr->garbage);
|
|
|
|
- chunk_remaining -= ntohs(sl_hdr->garbage);
|
|
|
|
- continue;
|
|
|
|
- }
|
|
|
|
|
|
|
|
- int32_t sock =-1;
|
|
|
|
- if(connections->first == NULL){
|
|
|
|
- printf("Error: there are no connections\n");
|
|
|
|
- } else {
|
|
|
|
- connection *last = connections->first;
|
|
|
|
|
|
+ //there might be more garbage bytes than we have chunk left
|
|
|
|
+ p += ntohs(sl_hdr->garbage);
|
|
|
|
+ chunk_remaining -= ntohs(sl_hdr->garbage);
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ int32_t sock =-1;
|
|
|
|
+ if(connections->first == NULL){
|
|
|
|
+ printf("Error: there are no connections\n");
|
|
|
|
+ } else {
|
|
|
|
+ connection *last = connections->first;
|
|
|
|
+ if (last->stream_id == sl_hdr->stream_id){
|
|
|
|
+ sock = last->socket;
|
|
|
|
+ }
|
|
|
|
+ while(last->next != NULL){
|
|
|
|
+ last = last->next;
|
|
if (last->stream_id == sl_hdr->stream_id){
|
|
if (last->stream_id == sl_hdr->stream_id){
|
|
sock = last->socket;
|
|
sock = last->socket;
|
|
}
|
|
}
|
|
- while(last->next != NULL){
|
|
|
|
- last = last->next;
|
|
|
|
- if (last->stream_id == sl_hdr->stream_id){
|
|
|
|
- sock = last->socket;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if(sock == -1){
|
|
|
|
- printf("No stream id exists. Possibly invalid header\n");
|
|
|
|
- break;
|
|
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if(sock == -1){
|
|
|
|
+ printf("No stream id exists. Possibly invalid header\n");
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
|
#ifdef DEBUG_PARSE
|
|
#ifdef DEBUG_PARSE
|
|
- printf("Received information for stream id: %d of length: %u\n", sl_hdr->stream_id, ntohs(sl_hdr->len));
|
|
|
|
|
|
+ printf("Received information for stream id: %d of length: %u\n", sl_hdr->stream_id, ntohs(sl_hdr->len));
|
|
#endif
|
|
#endif
|
|
|
|
|
|
- //figure out how much to skip
|
|
|
|
- int32_t padding = 0;
|
|
|
|
- if(ntohs(sl_hdr->len) %16){
|
|
|
|
- padding = 16 - ntohs(sl_hdr->len)%16;
|
|
|
|
- }
|
|
|
|
- p += 16; //IV
|
|
|
|
-
|
|
|
|
- //check counter to see if we are missing data
|
|
|
|
- if(sl_hdr->counter > expected_next_count){
|
|
|
|
- //save any future data
|
|
|
|
- printf("Received header with count %lu. Expected count %lu.\n",
|
|
|
|
- sl_hdr->counter, expected_next_count);
|
|
|
|
- if((saved_data == NULL) || (saved_data->count > sl_hdr->counter)){
|
|
|
|
- data_block *new_block = malloc(sizeof(data_block));
|
|
|
|
- new_block->count = sl_hdr->counter;
|
|
|
|
- new_block->len = ntohs(sl_hdr->len);
|
|
|
|
- new_block->data = malloc(ntohs(sl_hdr->len));
|
|
|
|
-
|
|
|
|
- memcpy(new_block->data, p, ntohs(sl_hdr->len));
|
|
|
|
-
|
|
|
|
- new_block->socket = sock;
|
|
|
|
- new_block->next = saved_data;
|
|
|
|
-
|
|
|
|
- saved_data = new_block;
|
|
|
|
-
|
|
|
|
- } else {
|
|
|
|
- data_block *last = saved_data;
|
|
|
|
- while((last->next != NULL) && (last->next->count < sl_hdr->counter)){
|
|
|
|
- last = last->next;
|
|
|
|
- }
|
|
|
|
- data_block *new_block = malloc(sizeof(data_block));
|
|
|
|
- new_block->count = sl_hdr->counter;
|
|
|
|
- new_block->len = ntohs(sl_hdr->len);
|
|
|
|
- new_block->data = malloc(ntohs(sl_hdr->len));
|
|
|
|
- memcpy(new_block->data, p, ntohs(sl_hdr->len));
|
|
|
|
- new_block->socket = sock;
|
|
|
|
- new_block->next = last->next;
|
|
|
|
-
|
|
|
|
- last->next = new_block;
|
|
|
|
- }
|
|
|
|
|
|
+ //figure out how much to skip
|
|
|
|
+ int32_t padding = 0;
|
|
|
|
+ if(ntohs(sl_hdr->len) %16){
|
|
|
|
+ padding = 16 - ntohs(sl_hdr->len)%16;
|
|
|
|
+ }
|
|
|
|
+ p += 16; //IV
|
|
|
|
+
|
|
|
|
+ //check counter to see if we are missing data
|
|
|
|
+ if(sl_hdr->counter > expected_next_count){
|
|
|
|
+ //save any future data
|
|
|
|
+ printf("Received header with count %lu. Expected count %lu.\n",
|
|
|
|
+ sl_hdr->counter, expected_next_count);
|
|
|
|
+ if((saved_data == NULL) || (saved_data->count > sl_hdr->counter)){
|
|
|
|
+ data_block *new_block = malloc(sizeof(data_block));
|
|
|
|
+ new_block->count = sl_hdr->counter;
|
|
|
|
+ new_block->len = ntohs(sl_hdr->len);
|
|
|
|
+ new_block->data = malloc(ntohs(sl_hdr->len));
|
|
|
|
+
|
|
|
|
+ memcpy(new_block->data, p, ntohs(sl_hdr->len));
|
|
|
|
+
|
|
|
|
+ new_block->socket = sock;
|
|
|
|
+ new_block->next = saved_data;
|
|
|
|
+
|
|
|
|
+ saved_data = new_block;
|
|
|
|
+
|
|
} else {
|
|
} else {
|
|
- int32_t bytes_sent = send(sock, p, ntohs(sl_hdr->len), 0);
|
|
|
|
|
|
+ data_block *last = saved_data;
|
|
|
|
+ while((last->next != NULL) && (last->next->count < sl_hdr->counter)){
|
|
|
|
+ last = last->next;
|
|
|
|
+ }
|
|
|
|
+ data_block *new_block = malloc(sizeof(data_block));
|
|
|
|
+ new_block->count = sl_hdr->counter;
|
|
|
|
+ new_block->len = ntohs(sl_hdr->len);
|
|
|
|
+ new_block->data = malloc(ntohs(sl_hdr->len));
|
|
|
|
+ memcpy(new_block->data, p, ntohs(sl_hdr->len));
|
|
|
|
+ new_block->socket = sock;
|
|
|
|
+ new_block->next = last->next;
|
|
|
|
+
|
|
|
|
+ last->next = new_block;
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ int32_t bytes_sent = send(sock, p, ntohs(sl_hdr->len), 0);
|
|
|
|
+ if(bytes_sent <= 0){
|
|
|
|
+ printf("Error writing to socket for stream id %d\n", sl_hdr->stream_id);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //increment expected counter
|
|
|
|
+ expected_next_count++;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //now check to see if there is saved data to write out
|
|
|
|
+ if(saved_data != NULL){
|
|
|
|
+ data_block *current_block = saved_data;
|
|
|
|
+ while((current_block != NULL) && (expected_next_count == current_block->count)){
|
|
|
|
+ int32_t bytes_sent = send(current_block->socket, current_block->data,
|
|
|
|
+ current_block->len, 0);
|
|
if(bytes_sent <= 0){
|
|
if(bytes_sent <= 0){
|
|
printf("Error writing to socket for stream id %d\n", sl_hdr->stream_id);
|
|
printf("Error writing to socket for stream id %d\n", sl_hdr->stream_id);
|
|
}
|
|
}
|
|
-
|
|
|
|
- //increment expected counter
|
|
|
|
expected_next_count++;
|
|
expected_next_count++;
|
|
|
|
+ saved_data = current_block->next;
|
|
|
|
+ free(current_block->data);
|
|
|
|
+ free(current_block);
|
|
|
|
+ current_block = saved_data;
|
|
}
|
|
}
|
|
|
|
+ }
|
|
|
|
|
|
- //now check to see if there is saved data to write out
|
|
|
|
- if(saved_data != NULL){
|
|
|
|
- data_block *current_block = saved_data;
|
|
|
|
- while((current_block != NULL) && (expected_next_count == current_block->count)){
|
|
|
|
- int32_t bytes_sent = send(current_block->socket, current_block->data,
|
|
|
|
- current_block->len, 0);
|
|
|
|
- if(bytes_sent <= 0){
|
|
|
|
- printf("Error writing to socket for stream id %d\n", sl_hdr->stream_id);
|
|
|
|
- }
|
|
|
|
- expected_next_count++;
|
|
|
|
- saved_data = current_block->next;
|
|
|
|
- free(current_block->data);
|
|
|
|
- free(current_block);
|
|
|
|
- current_block = saved_data;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ p += ntohs(sl_hdr->len); //encrypted data
|
|
|
|
+ p += 16; //mac
|
|
|
|
+ p += padding;
|
|
|
|
+ p += ntohs(sl_hdr->garbage);
|
|
|
|
|
|
- p += ntohs(sl_hdr->len); //encrypted data
|
|
|
|
- p += 16; //mac
|
|
|
|
- p += padding;
|
|
|
|
- p += ntohs(sl_hdr->garbage);
|
|
|
|
|
|
+ chunk_remaining -= ntohs(sl_hdr->len) + 16 + padding + 16 + ntohs(sl_hdr->garbage);
|
|
|
|
|
|
- chunk_remaining -= ntohs(sl_hdr->len) + 16 + padding + 16 + ntohs(sl_hdr->garbage);
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
|
|
|
+ free(buffer);
|
|
|
|
+ buffer = NULL;
|
|
|
|
|
|
- } else {
|
|
|
|
- printf("Error: read %d bytes from OUS_out\n", bytes_read);
|
|
|
|
- goto err;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
}
|
|
}
|
|
err:
|
|
err:
|
|
- free(buffer);
|
|
|
|
|
|
+
|
|
|
|
+ if (buffer != NULL) {
|
|
|
|
+ free(buffer);
|
|
|
|
+ }
|
|
|
|
+
|
|
close(pipes->out);
|
|
close(pipes->out);
|
|
pthread_exit(NULL);
|
|
pthread_exit(NULL);
|
|
|
|
|
|
@@ -852,5 +886,3 @@ int remove_connection(uint16_t stream_id){
|
|
|
|
|
|
return 1;
|
|
return 1;
|
|
}
|
|
}
|
|
-
|
|
|
|
-
|
|
|