readwrite: Expose a couple of points of functionality

This commit is contained in:
nick
2013-06-18 15:36:15 +01:00
parent 197c1131bf
commit 662b9c2d07
2 changed files with 42 additions and 19 deletions

View File

@@ -41,41 +41,58 @@ int socket_connect(struct sockaddr* to, struct sockaddr* from)
return fd; return fd;
} }
int socket_nbd_read_hello(int fd, off64_t * out_size) int nbd_check_hello( struct nbd_init_raw* init_raw, off64_t* out_size )
{ {
struct nbd_init init; if ( strncmp( init_raw->passwd, INIT_PASSWD, 8 ) != 0 ) {
if ( 0 > readloop(fd, &init, sizeof(init)) ) { warn( "wrong passwd" );
warn( "Couldn't read init" );
goto fail; goto fail;
} }
if (strncmp(init.passwd, INIT_PASSWD, 8) != 0) { if ( be64toh( init_raw->magic ) != INIT_MAGIC ) {
warn("wrong passwd"); warn( "wrong magic (%x)", be64toh( init_raw->magic ) );
goto fail;
}
if (be64toh(init.magic) != INIT_MAGIC) {
warn("wrong magic (%x)", be64toh(init.magic));
goto fail; goto fail;
} }
if ( NULL != out_size ) { if ( NULL != out_size ) {
*out_size = be64toh(init.size); *out_size = be64toh( init_raw->size );
} }
return 1; return 1;
fail: fail:
return 0; return 0;
}
int socket_nbd_read_hello( int fd, off64_t* out_size )
{
struct nbd_init_raw init_raw;
if ( 0 > readloop( fd, &init_raw, sizeof(init_raw) ) ) {
warn( "Couldn't read init" );
return 0;
}
return nbd_check_hello( &init_raw, out_size );
}
void nbd_hello_to_buf( struct nbd_init_raw *buf, off64_t out_size )
{
struct nbd_init init;
memcpy( &init.passwd, INIT_PASSWD, 8 );
init.magic = INIT_MAGIC;
init.size = out_size;
memset( buf, 0, sizeof( struct nbd_init_raw ) ); // ensure reserved is 0s
nbd_h2r_init( &init, buf );
return;
} }
int socket_nbd_write_hello(int fd, off64_t out_size) int socket_nbd_write_hello(int fd, off64_t out_size)
{ {
struct nbd_init init;
struct nbd_init_raw init_raw; struct nbd_init_raw init_raw;
nbd_hello_to_buf( &init_raw, out_size );
memcpy(&init.passwd, INIT_PASSWD, 8);
init.magic = INIT_MAGIC;
init.size = out_size;
memset( &init_raw, 0, sizeof( init_raw ) ); // ensure reserved is 0s
nbd_h2r_init(&init, &init_raw);
if ( 0 > writeloop( fd, &init_raw, sizeof( init_raw ) ) ) { if ( 0 > writeloop( fd, &init_raw, sizeof( init_raw ) ) ) {
warn( SHOW_ERRNO( "failed to write hello to socket" ) ); warn( SHOW_ERRNO( "failed to write hello to socket" ) );

View File

@@ -12,5 +12,11 @@ void socket_nbd_read(int fd, off64_t from, int len, int out_fd, void* out_buf, i
void socket_nbd_write(int fd, off64_t from, int len, int out_fd, void* out_buf, int timeout_secs); void socket_nbd_write(int fd, off64_t from, int len, int out_fd, void* out_buf, int timeout_secs);
int socket_nbd_disconnect( int fd ); int socket_nbd_disconnect( int fd );
/* as you can see, we're slowly accumulating code that should really be in an
* NBD library */
void nbd_hello_to_buf( struct nbd_init_raw* buf, off64_t out_size );
int nbd_check_hello( struct nbd_init_raw* init_raw, off64_t* out_size );
#endif #endif