Introduce socket_nbd_write_hello() and a macro to display errno results nicely

This commit is contained in:
nick
2013-02-08 15:53:27 +00:00
parent 56ce7d35c2
commit ecfd108a53
3 changed files with 21 additions and 0 deletions

View File

@@ -56,6 +56,25 @@ fail:
return 0; return 0;
} }
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);
if ( 0 > writeloop( fd, &init_raw, sizeof( init_raw ) ) ) {
warn( SHOW_ERRNO( "failed to write hello to socket" ) );
return 0;
}
return 1;
}
void fill_request(struct nbd_request *request, int type, off64_t from, int len) void fill_request(struct nbd_request *request, int type, off64_t from, int len)
{ {
request->magic = htobe32(REQUEST_MAGIC); request->magic = htobe32(REQUEST_MAGIC);

View File

@@ -7,6 +7,7 @@
int socket_connect(struct sockaddr* to, struct sockaddr* from); int socket_connect(struct sockaddr* to, struct sockaddr* from);
int socket_nbd_read_hello(int fd, off64_t * size); int socket_nbd_read_hello(int fd, off64_t * size);
int socket_nbd_write_hello(int fd, off64_t size);
void socket_nbd_read(int fd, off64_t from, int len, int out_fd, void* out_buf, int timeout_secs); void socket_nbd_read(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); 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 );

View File

@@ -148,6 +148,7 @@ void mylog(int line_level, const char* format, ...);
#define NULLCHECK(value) FATAL_IF_NULL(value, "BUG: " #value " is null") #define NULLCHECK(value) FATAL_IF_NULL(value, "BUG: " #value " is null")
#define SHOW_ERRNO( msg ) msg ": %s (%i)", ( errno == 0 ? "EOF" : strerror(errno) ), errno
#endif #endif