From 662b9c2d077ea7d71e487cb8c9c83a3259ab46b4 Mon Sep 17 00:00:00 2001 From: nick Date: Tue, 18 Jun 2013 15:36:15 +0100 Subject: [PATCH] readwrite: Expose a couple of points of functionality --- src/readwrite.c | 55 ++++++++++++++++++++++++++++++++----------------- src/readwrite.h | 6 ++++++ 2 files changed, 42 insertions(+), 19 deletions(-) diff --git a/src/readwrite.c b/src/readwrite.c index eeb8f5d..731e5ca 100644 --- a/src/readwrite.c +++ b/src/readwrite.c @@ -41,41 +41,58 @@ int socket_connect(struct sockaddr* to, struct sockaddr* from) 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 ( 0 > readloop(fd, &init, sizeof(init)) ) { - warn( "Couldn't read init" ); + if ( strncmp( init_raw->passwd, INIT_PASSWD, 8 ) != 0 ) { + warn( "wrong passwd" ); goto fail; } - if (strncmp(init.passwd, INIT_PASSWD, 8) != 0) { - warn("wrong passwd"); - goto fail; - } - if (be64toh(init.magic) != INIT_MAGIC) { - warn("wrong magic (%x)", be64toh(init.magic)); + if ( be64toh( init_raw->magic ) != INIT_MAGIC ) { + warn( "wrong magic (%x)", be64toh( init_raw->magic ) ); goto fail; } + if ( NULL != out_size ) { - *out_size = be64toh(init.size); + *out_size = be64toh( init_raw->size ); } return 1; fail: 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) { - struct nbd_init init; struct nbd_init_raw init_raw; - - 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); + nbd_hello_to_buf( &init_raw, out_size ); if ( 0 > writeloop( fd, &init_raw, sizeof( init_raw ) ) ) { warn( SHOW_ERRNO( "failed to write hello to socket" ) ); diff --git a/src/readwrite.h b/src/readwrite.h index e58c398..947f20b 100644 --- a/src/readwrite.h +++ b/src/readwrite.h @@ -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); 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