Exemplo n.º 1
0
/*
** This function generates <count> many random bytes and
** places them in the location pointed to by <ptr>. It is
** a responsibility of the caller to have allocated
** sufficient space. Returns 0 on success, and -1 on failure.
*/
int
I2RandomBytes(
	I2RandomSource	src,
	unsigned char 	*ptr,
	unsigned int	count
	)
{
	if(!src)
		return -1;

	switch (src->type) {
		case I2RAND_DEV:
			if (I2Readn(src->fd, ptr, count) != (signed) count) {
				I2ErrLog(src->eh,
					"I2randomBytes: I2Readn() failed: %M");
				return -1;
			}
			break;
		case I2RAND_EGD:
		default:
			/* UNREACHED */
			I2ErrLog(src->eh,
		"I2randomBytes: unknown/unsupported random source type");
			return -1;
	}

	return 0;
}
Exemplo n.º 2
0
/*
** Initialize the source of random bytes. Possible types are:
** I2RAND_DEV - random device (eg, /dev/urandom or /dev/random) - <data>
** is then interpreted as char* pathname of the device.
** I2RAND_EGD - enthropy generating daemon (EGD) - <data> is
** then interpreted as char* pathname to the local socket the EGD daemon
** is listening on.
** Returns 0 on success, or -1 on failure.
*/
I2RandomSource
I2RandomSourceInit(I2ErrHandle eh, int type, void* data)
{
	I2RandomSource	rand_src;

	if(!eh)
		return NULL;

	if( !(rand_src = malloc(sizeof(struct I2RandomSourceRec)))){
		I2ErrLog(eh,"malloc():%M");
		return NULL;
	}
	rand_src->eh = eh;
	rand_src->type = type;

	switch (type) {
		case I2RAND_DEV:
	
			if(!data)
				data = I2_RANDOMDEV_PATH;
			if( (rand_src->fd = open((char *)data, O_RDONLY))<0){
				I2ErrLog(eh, "I2randomBytes:open():%M");
				return NULL;
			}
			break;
		case I2RAND_EGD:
			I2ErrLog(eh,
			"I2randomBytes: I2RAND_EGD not yet implemented");
			free(rand_src);
			return NULL;
			/* UNREACHED */
		default:
			I2ErrLog(eh,
			"I2randomBytes:unknown/unsupported random source type");
			free(rand_src);
			return NULL;
			/* UNREACHED */
	}

	return rand_src;
}
Exemplo n.º 3
0
Arquivo: io.c Projeto: bringhurst/ndt
/*
 * Function:    I2CopyFile
 *
 * Description:    
 *              Copy one file to another using mmap for speed.
 *
 * In Args:    
 *
 * Out Args:    
 *
 * Scope:    
 * Returns:    
 *              0 on success
 * Side Effect:    
 */
int
I2CopyFile(
        I2ErrHandle eh,
        int         tofd,
        int         fromfd,
        off_t       len
        )
{
    struct stat sbuf;
    int         rc;
    void        *fptr,*tptr;

    if((rc = fstat(fromfd,&sbuf)) != 0){
        I2ErrLog(eh,"I2CopyFile: fstat: %M, status of from file");
        return rc;
    }

    if(len == 0){
        len = sbuf.st_size;
    }
    else{
        len = MIN(len,sbuf.st_size);
    }

    if((rc = ftruncate(tofd,len)) != 0){
        I2ErrLog(eh,"I2CopyFile: ftrunctate(%llu): %M, sizing to file",len);
        return rc;
    }

    if(!(fptr = mmap(NULL,len,PROT_READ|PROT_WRITE,MAP_SHARED,fromfd,0))){
        I2ErrLog(eh,"I2CopyFile: mmap(from file): %M");
        return -1;
    }

    if(!(tptr = mmap(NULL,len,PROT_READ|PROT_WRITE,MAP_SHARED,tofd,0))){
        I2ErrLog(eh,"I2CopyFile: mmap(to file): %M");
        return -1;
    }

    memcpy(tptr,fptr,len);

    if((rc = munmap(fptr,len)) != 0){
        I2ErrLog(eh,"I2CopyFile: munmap(from file): %M");
        return -1;
    }

    if((rc = munmap(tptr,len)) != 0){
        I2ErrLog(eh,"I2CopyFile: munmap(to file): %M");
        return -1;
    }

    return 0;
}
Exemplo n.º 4
0
void
I2RandomSourceClose(
	I2RandomSource	src
	)
{
	if(!src)
		return;

	switch (src->type) {
		case I2RAND_DEV:
			close(src->fd);
			break;
		case I2RAND_EGD:
		default:
			/* UNREACHED */
			I2ErrLog(src->eh,
		"I2randomBytes: unknown/unsupported random source type");
	}

	free(src);

	return;
}