コード例 #1
0
ファイル: easyfec.c プロジェクト: prtitrz/Nefs
char * decode(char *data, int k, int m, unsigned *index, int miss, int chunksize)
{
	fec_t *test;
	char *output;
	unsigned char *inpkts[k];
	unsigned char *outpkts[miss];
	int i, j=0, h;

	output = (char *)malloc((k * chunksize + 1) * sizeof(char));
	for (i = 0; i < k; i++) {
		inpkts[i] = data + index[i] * chunksize;
	}
	for (i = 0; i < k; i++) {
		if (index[i] != i) {
			outpkts[j] = output + i * chunksize; 
			j++;
		}
		else {
			for (h = 0; h < chunksize; h++) {
				output[i * chunksize + h] = inpkts[i][h];
			}
		}
	}
	test = fec_new(k, m);
	fec_decode(test, inpkts, outpkts, index, chunksize);
	output[k * chunksize] = '\0';
	fec_free(test);
	return output;
}
コード例 #2
0
ファイル: rs.c プロジェクト: alordred/UDPspeeder
void* get_code(int k,int n)
{
	if (table==0)
	{
		table=(void* (*)[256]) malloc(sizeof(void*)*256*256);
		if(!table)
		{
		    return table;
		}
		memset(table,0,sizeof(void*)*256*256);
	}
	if(table[k][n]==0)
	{
		table[k][n]=fec_new(k,n);
	}
	return table[k][n];
}
コード例 #3
0
ファイル: easyfec.c プロジェクト: prtitrz/Nefs
char * encode(char *data, int k, int m, int chunksize)
{
	fec_t *test;
	char *output;
	unsigned char *src[k];
	unsigned char *fecs[m-k];
	unsigned block[m-k];
	int i;

	output = (char *)malloc((m * chunksize + 1) * sizeof(char));
	for (i = 0; i < k; i++) {
		src[i] = output + i * chunksize;
		strncpy(src[i], data + (i * chunksize), chunksize);
	}
	for (i = 0; i < m - k; i++){
		block[i] = k + i;
		fecs[i] = output + (k + i) * chunksize;
	}
	test = fec_new(k, m);
	fec_encode(test, src, fecs, block, (m - k), chunksize);
	output[m * chunksize] = '\0';
	fec_free(test);
	return output;
}
コード例 #4
0
ファイル: fectest.c プロジェクト: OpenNoah/mtd-utils
int main(void)
{
	int i, j;
	unsigned char buf[NR_PKTS * PKT_SIZE];
	unsigned char pktbuf[(NR_PKTS + DROPS) * PKT_SIZE];
	struct fec_parms *fec;
	unsigned char *srcs[NR_PKTS];
	unsigned char *pkt[NR_PKTS + DROPS];
	int pktnr[NR_PKTS + DROPS];
	struct timeval then, now;

	srand(3453);
	for (i=0; i < sizeof(buf); i++)
		if (i < ERASE_SIZE)
			buf[i] = rand();
		else
			buf[i] = 0;

	for (i=0; i < NR_PKTS + DROPS; i++)
		srcs[i] = buf + (i * PKT_SIZE);

	for (i=0; i < NR_PKTS + DROPS; i++) {
		pkt[i] = malloc(PKT_SIZE);
		pktnr[i] = -1;
	}
	fec = fec_new(NR_PKTS, NR_PKTS + DROPS);
	if (!fec) {
		printf("fec_init() failed\n");
		exit(1);
	}
	j = 0;
	for (i=0; i < NR_PKTS + DROPS; i++) {
#if 1
		if (i == 27  || i == 40  || i == 44 || i == 45 || i == 56 )
			continue;
#endif
		if (i == 69 || i == 93 || i == 103)
			continue;
		fec_encode(fec, srcs, pkt[j], i, PKT_SIZE);
		pktnr[j] = i;
		j++;
	}
	gettimeofday(&then, NULL);
	if (fec_decode(fec, pkt, pktnr, PKT_SIZE)) {
		printf("Decode failed\n");
		exit(1);
	}

	for (i=0; i < NR_PKTS; i++)
		memcpy(pktbuf + (i*PKT_SIZE), pkt[i], PKT_SIZE);
	gettimeofday(&now, NULL);
	now.tv_sec -= then.tv_sec;
	now.tv_usec -= then.tv_usec;
	if (now.tv_usec < 0) {
		now.tv_usec += 1000000;
		now.tv_sec--;
	}

	if (memcmp(pktbuf, buf, ERASE_SIZE)) {
		int fd;
		printf("Compare failed\n");
		fd = open("before", O_WRONLY|O_TRUNC|O_CREAT, 0644);
		if (fd >= 0)
			write(fd, buf, ERASE_SIZE);
		close(fd);
		fd = open("after", O_WRONLY|O_TRUNC|O_CREAT, 0644);
		if (fd >= 0)
			write(fd, pktbuf, ERASE_SIZE);
		
		exit(1);
	}

	printf("Decoded in %ld.%06lds\n", now.tv_sec, now.tv_usec);
	return 0;
}
コード例 #5
0
ファイル: test.c プロジェクト: B-Rich/mixminion
int
main(int argc, char *argv[])
{
    char buf[256];
    void *code ;

    int kk ;
    int i ;

    int *ixs ;

    int lim = GF_SIZE + 1 ;

    if (lim > 1024) lim = 1024 ;

#if 0
    test_gf();
#endif
    for ( kk = KK ; kk > 2 ; kk-- ) {
	code = fec_new(kk, lim);
	ixs = my_malloc(kk * sizeof(int), "ixs" );

	for (i=0; i<kk; i++) ixs[i] = kk - i ;
	sprintf(buf, "kk=%d, kk - i", kk); 
	test_decode(code, kk, ixs, SZ, buf);

	for (i=0; i<kk; i++) ixs[i] = i ;
	test_decode(code, kk, ixs, SZ, "i");

if (0) {
	for (i=0; i<kk; i++) ixs[i] = i ;
	ixs[0] = ixs[kk/2] ;
	test_decode(code, kk, ixs, SZ, "0 = 1 (error expected)");
	}

if (0)
	for (i= lim-1 ; i >= kk ; i--) {
	    int j ;
	    for (j=0; j<KK; j++) ixs[j] = kk - j ;
	    ixs[0] = i ;
	    test_decode(code, kk, ixs, SZ, "0 = big");
	}

if (0)
	for (i= lim - kk ; i >= 0 && i>= lim - kk - 4 ; i--) {
	    int j ;
	    for (j=0; j<kk; j++)
		ixs[j] = kk -1 - j + i ;
	    test_decode(code, kk, ixs, SZ, "shifted j");
	}
if (1)  {
	int j, max_i0 = KK/2 ;
	if (max_i0 + KK > lim)
	    max_i0 = lim - KK ;
	for (i= 0 ; i <= max_i0 ; i++) {
	    for (j=0; j<kk; j++)
		ixs[j] = j + i ;
	    test_decode(code, kk, ixs, SZ, "shifted j");
	}
	}
	fprintf(stderr, "\n");
	free(ixs);
	fec_free(code);
    }
    return 0;
}
コード例 #6
0
ファイル: serve_image.c プロジェクト: ericpaulbishop/mtdutil
int main(int argc, char **argv)
{
	struct addrinfo *ai;
	struct addrinfo hints;
	struct addrinfo *runp;
	int ret;
	int sock;
	struct image_pkt pktbuf;
	int rfd;
	struct stat st;
	int writeerrors = 0;
	uint32_t erasesize;
	unsigned char *image, *blockptr = NULL;
	uint32_t block_nr, pkt_nr;
	int nr_blocks;
	struct timeval then, now, nextpkt;
	long time_msecs;
	int pkts_per_block;
	int total_pkts_per_block;
	struct fec_parms *fec;
	unsigned char *last_block;
	uint32_t *block_crcs;
	long tosleep;
	uint32_t sequence = 0;

	if (argc == 6) {
		tx_rate = atol(argv[5]) * 1024;
		if (tx_rate < PKT_SIZE || tx_rate > 20000000) {
			fprintf(stderr, "Bogus TX rate %d KiB/s\n", tx_rate);
			exit(1);
		}
		argc = 5;
	}
	if (argc != 5) {
		fprintf(stderr, "usage: %s <host> <port> <image> <erasesize> [<tx_rate>]\n",
			PROGRAM_NAME);
		exit(1);
	}
	pkt_delay = (sizeof(pktbuf) * 1000000) / tx_rate;
	printf("Inter-packet delay (avg): %dµs\n", pkt_delay);
	printf("Transmit rate: %d KiB/s\n", tx_rate / 1024);

	erasesize = atol(argv[4]);
	if (!erasesize) {
		fprintf(stderr, "erasesize cannot be zero\n");
		exit(1);
	}

	pkts_per_block = (erasesize + PKT_SIZE - 1) / PKT_SIZE;
	total_pkts_per_block = pkts_per_block * 3 / 2;

	/* We have to pad it with zeroes, so can't use it in-place */
	last_block = malloc(pkts_per_block * PKT_SIZE);
	if (!last_block) {
		fprintf(stderr, "Failed to allocate last-block buffer\n");
		exit(1);
	}

	fec = fec_new(pkts_per_block, total_pkts_per_block);
	if (!fec) {
		fprintf(stderr, "Error initialising FEC\n");
		exit(1);
	}

	memset(&hints, 0, sizeof(hints));
	hints.ai_flags = AI_ADDRCONFIG;
	hints.ai_socktype = SOCK_DGRAM;

	ret = getaddrinfo(argv[1], argv[2], &hints, &ai);
	if (ret) {
		fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret));
		exit(1);
	}
	runp = ai;
	for (runp = ai; runp; runp = runp->ai_next) {
		sock = socket(runp->ai_family, runp->ai_socktype,
			      runp->ai_protocol);
		if (sock == -1) {
			perror("socket");
			continue;
		}
		if (connect(sock, runp->ai_addr, runp->ai_addrlen) == 0)
			break;
		perror("connect");
		close(sock);
	}
	if (!runp)
		exit(1);

	rfd = open(argv[3], O_RDONLY);
	if (rfd < 0) {
		perror("open");
		exit(1);
	}

	if (fstat(rfd, &st)) {
		perror("fstat");
		exit(1);
	}

	if (st.st_size % erasesize) {
		fprintf(stderr, "Image size %" PRIu64 " bytes is not a multiple of erasesize %d bytes\n",
				st.st_size, erasesize);
		exit(1);
	}
	image = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, rfd, 0);
	if (image == MAP_FAILED) {
		perror("mmap");
		exit(1);
	}

	nr_blocks = st.st_size / erasesize;

	block_crcs = malloc(nr_blocks * sizeof(uint32_t));
	if (!block_crcs) {
		fprintf(stderr, "Failed to allocate memory for CRCs\n");
		exit(1);
	}

	memcpy(last_block, image + (nr_blocks - 1) * erasesize, erasesize);
	memset(last_block + erasesize, 0, (PKT_SIZE * pkts_per_block) - erasesize);

	printf("Checking CRC....");
	fflush(stdout);

	pktbuf.hdr.resend = 0;
	pktbuf.hdr.totcrc = htonl(mtd_crc32(-1, image, st.st_size));
	pktbuf.hdr.nr_blocks = htonl(nr_blocks);
	pktbuf.hdr.blocksize = htonl(erasesize);
	pktbuf.hdr.thislen = htonl(PKT_SIZE);
	pktbuf.hdr.nr_pkts = htons(total_pkts_per_block);

	printf("%08x\n", ntohl(pktbuf.hdr.totcrc));
	printf("Checking block CRCs....");
	fflush(stdout);
	for (block_nr=0; block_nr < nr_blocks; block_nr++) {
		printf("\rChecking block CRCS.... %d/%d",
		       block_nr + 1, nr_blocks);
		fflush(stdout);
		block_crcs[block_nr] = mtd_crc32(-1, image + (block_nr * erasesize), erasesize);
	}

	printf("\nImage size %ld KiB (0x%08lx). %d blocks at %d pkts/block\n"
	       "Estimated transmit time per cycle: %ds\n",
	       (long)st.st_size / 1024, (long) st.st_size,
	       nr_blocks, pkts_per_block,
	       nr_blocks * pkts_per_block * pkt_delay / 1000000);
	gettimeofday(&then, NULL);
	nextpkt = then;

#ifdef RANDOMDROP
	srand((unsigned)then.tv_usec);
	printf("Random seed %u\n", (unsigned)then.tv_usec);
#endif
	while (1) for (pkt_nr=0; pkt_nr < total_pkts_per_block; pkt_nr++) {

		if (blockptr && pkt_nr == 0) {
			unsigned long amt_sent = total_pkts_per_block * nr_blocks * sizeof(pktbuf);
			gettimeofday(&now, NULL);

			time_msecs = (now.tv_sec - then.tv_sec) * 1000;
			time_msecs += ((int)(now.tv_usec - then.tv_usec)) / 1000;
			printf("\n%ld KiB sent in %ldms (%ld KiB/s)\n",
			       amt_sent / 1024, time_msecs,
			       amt_sent / 1024 * 1000 / time_msecs);
			then = now;
		}

		for (block_nr = 0; block_nr < nr_blocks; block_nr++) {

			int actualpkt;

			/* Calculating the redundant FEC blocks is expensive;
			   the first $pkts_per_block are cheap enough though
			   because they're just copies. So alternate between
			   simple and complex stuff, so that we don't start
			   to choke and fail to keep up with the expected
			   bitrate in the second half of the sequence */
			if (block_nr & 1)
				actualpkt = pkt_nr;
			else
				actualpkt = total_pkts_per_block - 1 - pkt_nr;

			blockptr = image + (erasesize * block_nr);
			if (block_nr == nr_blocks - 1)
				blockptr = last_block;

			fec_encode_linear(fec, blockptr, pktbuf.data, actualpkt, PKT_SIZE);

			pktbuf.hdr.thiscrc = htonl(mtd_crc32(-1, pktbuf.data, PKT_SIZE));
			pktbuf.hdr.block_crc = htonl(block_crcs[block_nr]);
			pktbuf.hdr.block_nr = htonl(block_nr);
			pktbuf.hdr.pkt_nr = htons(actualpkt);
			pktbuf.hdr.pkt_sequence = htonl(sequence++);

			printf("\rSending data block %08x packet %3d/%d",
			       block_nr * erasesize,
			       pkt_nr, total_pkts_per_block);

			if (pkt_nr && !block_nr) {
				unsigned long amt_sent = pkt_nr * nr_blocks * sizeof(pktbuf);

				gettimeofday(&now, NULL);

				time_msecs = (now.tv_sec - then.tv_sec) * 1000;
				time_msecs += ((int)(now.tv_usec - then.tv_usec)) / 1000;
				printf("    (%ld KiB/s)    ",
				       amt_sent / 1024 * 1000 / time_msecs);
			}

			fflush(stdout);

#ifdef RANDOMDROP
			if ((rand() % 1000) < 20) {
				printf("\nDropping packet %d of block %08x\n", pkt_nr+1, block_nr * erasesize);
				continue;
			}
#endif
			gettimeofday(&now, NULL);
#if 1
			tosleep = nextpkt.tv_usec - now.tv_usec +
				(1000000 * (nextpkt.tv_sec - now.tv_sec));

			/* We need hrtimers for this to actually work */
			if (tosleep > 0) {
				struct timespec req;

				req.tv_nsec = (tosleep % 1000000) * 1000;
				req.tv_sec = tosleep / 1000000;

				nanosleep(&req, NULL);
			}
#else
			while (now.tv_sec < nextpkt.tv_sec ||
				 (now.tv_sec == nextpkt.tv_sec &&
				  now.tv_usec < nextpkt.tv_usec)) {
				gettimeofday(&now, NULL);
			}
#endif
			nextpkt.tv_usec += pkt_delay;
			if (nextpkt.tv_usec >= 1000000) {
				nextpkt.tv_sec += nextpkt.tv_usec / 1000000;
				nextpkt.tv_usec %= 1000000;
			}

			/* If the time for the next packet has already
			   passed (by some margin), then we've lost time
			   Adjust our expected timings accordingly. If
			   we're only a little way behind, don't slip yet */
			if (now.tv_usec > (now.tv_usec + (5 * pkt_delay) +
					1000000 * (nextpkt.tv_sec - now.tv_sec))) {
				nextpkt = now;
			}

			if (write(sock, &pktbuf, sizeof(pktbuf)) < 0) {
				perror("write");
				writeerrors++;
				if (writeerrors > 10) {
					fprintf(stderr, "Too many consecutive write errors\n");
					exit(1);
				}
			} else
				writeerrors = 0;



		}
	}
	munmap(image, st.st_size);
	close(rfd);
	close(sock);
	return 0;
}
コード例 #7
0
ファイル: my_test.c プロジェクト: froggatt/MPRTP
int
main(int argc, char *argv[])
{
    char buf[256];
    void *code ;
    
    int k = 4, n = 8;
    int i, item, sz = 1;
    
    int index[n], re_index[k] ;


    static u_char **d_original = NULL, **d_src = NULL, **data ;

        d_original = malloc(k * sizeof(void *));
        data = malloc(k * sizeof(void *));
        d_src = malloc(n * sizeof(void *));
        
        for (i = 0 ; i < k ; i++ ) {
            d_original[i] = malloc(sz);
            data[i] = malloc(sz);
        }
        for (i = 0 ; i < n ; i++ ) {
            d_src[i] = malloc(sz);
        }

    code = fec_new(k, n);

    for (i = 0 ; i < k ; i++ ) {
        for (item=0; item < 1; item++) {
            d_original[i][item] = 3+i;
            printf("d_original[%d][%d] = %d, ",i, item, d_original[i][item]);
            printf("\n");
        }
    }

    for( i = 0 ; i < n ; i++ ) {
        index[i] = n-i-1;
        printf("index[%d] = %d \n", i, index[i]);
    }

    for( i = 0 ; i < n ; i++ ) {
        printf("=======================================\n");
        fec_encode(code, d_original, d_src[i], index[i], sz );
        printf("i = %d, d_src = %x, index = %d \n", i, d_src[i][0], index[i]);
        printf("=======================================\n");
    }

    data[0][0] = d_src[0][0];
    data[1][0] = d_src[1][0];
    data[2][0] = d_src[2][0];
    data[3][0] = d_src[3][0];

    re_index[0] = 7;
    re_index[1] = 6;
    re_index[2] = 5;
    re_index[3] = 4;

    for (i = 0 ; i < k ; i++ ) {
        for (item=0; item < 1; item++) {
            printf("before decode data: data[%d][%d] = %d, ",i, item, data[i][item]);
            printf("\n");
        }
    }

    fec_decode(code, data, re_index, sz);

    for (i = 0 ; i < k ; i++ ) {
        for (item=0; item < 1; item++) {
            printf("decode data: data[%d][%d] = %d, ",i, item, data[i][item]);
            printf("\n");
        }
    }

    fec_free(code);
    return 0;
}