Ejemplo n.º 1
0
Archivo: main.c Proyecto: w4kfu/Stunts
int main(int argc, char **argv)
{
        int fd;
        struct stat st;
        unsigned char *buf = NULL;
	struct conf_c conf = {0};
	struct s_comp comp = {0};

	parse_opt(argc, argv, &conf);
	check_opt(&conf, argv[0]);
        fd = open(conf.in, O_RDONLY);
        if (fd == -1)
        {
                perror("open()");
                exit(EXIT_FAILURE);
        }
        if (fstat(fd, &st) == -1)
        {
                perror("fstat()");
                exit(EXIT_FAILURE);
        }
        if ((buf = malloc(sizeof (char) * st.st_size)) == NULL)
        {
                perror("malloc()");
                exit(EXIT_FAILURE);
        }
        if (read(fd, buf, st.st_size) != st.st_size)
        {
                perror("read()");
                goto clean;
        }
	huff(buf, st.st_size, &comp);
	if (comp.tree)
	{
		if (conf.dot)
			dotty(comp.tree, conf.dot);
		uncomp(&comp, buf + st.st_size);
		if (comp.buf_out)
			dump_to_file(conf.out, comp.buf_out, comp.size);
	}
clean:
        free(buf);
        close(fd);
        return 0;
}
Ejemplo n.º 2
0
/*
 * Deoptimize the TCP data of an SKB.
 */
int tcp_deoptimize(pDeduplicator pd, __u8 *ippacket, __u8 *regenerated_packet) {

	struct iphdr *iph = NULL;
	struct tcphdr *tcph = NULL;
	__u16 oldsize = 0, newsize = 0; /* Store old, and new size of the TCP data. */
	__u8 *tcpdata = NULL; /* Starting location for the TCP data. */
	char message[LOGSZ];
	UncompReturnStatus status;
	

	if (DEBUG_DEDUPLICATION1 == true) {
		sprintf(message, "[SDEDUP]: Entering into TCP DEOPTIMIZATION \n");
		logger(LOG_INFO, message);
	}

	if ((ippacket != NULL)) { // If the skb or state_decompress is NULL abort compression.
		iph = (struct iphdr *) ippacket; // Access ip header.

		if ((iph->protocol == IPPROTO_TCP)) { // If this is not a TCP segment abort compression.

			if (DEBUG_DEDUPLICATION == true) {
				sprintf(message, "[SDEDUP]: IP Packet ID %u\n", ntohs(iph->id));
				logger(LOG_INFO, message);
			}

			tcph = (struct tcphdr *) (((u_int32_t *) ippacket) + iph->ihl); // Access tcp header.
			oldsize = (__u16)(ntohs(iph->tot_len) - iph->ihl * 4) - tcph->doff* 4;
			tcpdata = (__u8 *) tcph + tcph->doff * 4; // Find starting location of the TCP data.

			if ((oldsize > 0) && (regenerated_packet != NULL)) {

#ifdef BASIC
				if(deoptimize(tcpdata, oldsize, regenerated_packet, &newsize) == 2)
					return HASH_NOT_FOUND;
#endif

#ifdef ROLLING
				uncomp(pd,regenerated_packet, &newsize, tcpdata, oldsize, &status);
				if(status.code == UNCOMP_FP_NOT_FOUND)
					return HASH_NOT_FOUND;
#endif

				memmove(tcpdata, regenerated_packet, newsize); // Move decompressed data to packet.
				iph->tot_len = htons(ntohs(iph->tot_len) + (newsize - oldsize));// Fix packet length.
				__set_tcp_option((__u8 *) iph, 31, 3, 0); // Set compression flag to 0.
                    /* Bellido: change from decreasing seq number to changing most significant bit
				tcph->seq = htonl(ntohl(tcph->seq) - 8000); // Decrease SEQ number.
                */
                tcph->seq = htonl(ntohl(tcph->seq) ^ 1 << 31 ); // Change most significant bit

				if (DEBUG_DEDUPLICATION == true) {
					sprintf( message,
							"[SDEDUP]: Decompressing [%d] size of data to [%d] \n",
							oldsize, newsize);
					logger(LOG_INFO, message);
				}
				// return OK;
				// fruiz return amount of expanded data
				if (newsize >= oldsize) return newsize-oldsize; else return ERROR;
			}
		}
	}

	if (DEBUG_DEDUPLICATION == true) {
		sprintf( message, "[SDEDUP]: Packet NULL\n");
		logger(LOG_INFO, message);
	}
	return ERROR;
}