Files
flexnbd-c/src/readwrite.c

224 lines
5.3 KiB
C
Raw Normal View History

#include "nbdtypes.h"
#include "ioutil.h"
#include "util.h"
#include "serve.h"
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
int socket_connect(struct sockaddr* to, struct sockaddr* from)
{
int fd = socket(to->sa_family == AF_INET ? PF_INET : PF_INET6, SOCK_STREAM, 0);
2012-06-22 10:05:41 +01:00
if( fd < 0 ){
warn( "Couldn't create client socket");
return -1;
}
2012-06-11 14:34:17 +01:00
if (NULL != from) {
2012-06-22 10:05:41 +01:00
if ( 0 > bind(fd, from, sizeof(struct sockaddr_in6)) ){
warn( "bind() failed");
close( fd );
return -1;
}
}
if ( 0 > connect(fd, to, sizeof(struct sockaddr_in6)) ) {
warn( "connect failed" );
close( fd );
return -1;
2012-06-11 14:34:17 +01:00
}
return fd;
}
2012-06-22 10:05:41 +01:00
int socket_nbd_read_hello(int fd, off64_t * out_size)
{
struct nbd_init init;
2012-06-22 10:05:41 +01:00
if ( 0 > readloop(fd, &init, sizeof(init)) ) {
warn( "Couldn't read init" );
goto fail;
}
2012-06-11 14:34:17 +01:00
if (strncmp(init.passwd, INIT_PASSWD, 8) != 0) {
2012-06-22 10:05:41 +01:00
warn("wrong passwd");
goto fail;
2012-06-11 14:34:17 +01:00
}
if (be64toh(init.magic) != INIT_MAGIC) {
2012-06-22 10:05:41 +01:00
warn("wrong magic (%x)", be64toh(init.magic));
goto fail;
}
if ( NULL != out_size ) {
*out_size = be64toh(init.size);
2012-06-11 14:34:17 +01:00
}
2012-06-22 10:05:41 +01:00
return 1;
fail:
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)
{
request->magic = htobe32(REQUEST_MAGIC);
request->type = htobe32(type);
((int*) request->handle)[0] = rand();
((int*) request->handle)[1] = rand();
request->from = htobe64(from);
request->len = htobe32(len);
}
void read_reply(int fd, struct nbd_request *request, struct nbd_reply *reply)
{
struct nbd_reply_raw reply_raw;
ERROR_IF_NEGATIVE(readloop(fd, &reply_raw, sizeof(struct nbd_reply_raw)),
"Couldn't read reply");
nbd_r2h_reply( &reply_raw, reply );
if (reply->magic != REPLY_MAGIC) {
error("Reply magic incorrect (%x)", reply->magic);
2012-06-11 14:34:17 +01:00
}
if (reply->error != 0) {
error("Server replied with error %d", reply->error);
2012-06-11 14:34:17 +01:00
}
if (strncmp(request->handle, reply->handle, 8) != 0) {
error("Did not reply with correct handle");
2012-06-11 14:34:17 +01:00
}
}
void wait_for_data( int fd, int timeout_secs )
{
fd_set fds;
struct timeval tv = {timeout_secs, 0};
int selected;
FD_ZERO( &fds );
FD_SET( fd, &fds );
selected = select( FD_SETSIZE,
&fds, NULL, NULL,
timeout_secs >=0 ? &tv : NULL );
FATAL_IF( -1 == selected, "Select failed" );
ERROR_IF( 0 == selected, "Timed out waiting for reply" );
}
void socket_nbd_read(int fd, off64_t from, int len, int out_fd, void* out_buf, int timeout_secs)
{
struct nbd_request request;
struct nbd_reply reply;
fill_request(&request, REQUEST_READ, from, len);
FATAL_IF_NEGATIVE(writeloop(fd, &request, sizeof(request)),
"Couldn't write request");
wait_for_data( fd, timeout_secs );
read_reply(fd, &request, &reply);
if (out_buf) {
FATAL_IF_NEGATIVE(readloop(fd, out_buf, len),
"Read failed");
}
else {
FATAL_IF_NEGATIVE(
splice_via_pipe_loop(fd, out_fd, len),
"Splice failed"
);
}
}
void socket_nbd_write(int fd, off64_t from, int len, int in_fd, void* in_buf, int timeout_secs)
{
struct nbd_request request;
struct nbd_reply reply;
fill_request(&request, REQUEST_WRITE, from, len);
2012-06-22 10:05:41 +01:00
ERROR_IF_NEGATIVE(writeloop(fd, &request, sizeof(request)),
"Couldn't write request");
if (in_buf) {
2012-06-22 10:05:41 +01:00
ERROR_IF_NEGATIVE(writeloop(fd, in_buf, len),
"Write failed");
}
else {
2012-06-22 10:05:41 +01:00
ERROR_IF_NEGATIVE(
splice_via_pipe_loop(in_fd, fd, len),
"Splice failed"
);
}
wait_for_data( fd, timeout_secs );
read_reply(fd, &request, &reply);
}
int socket_nbd_disconnect( int fd )
{
int success = 1;
struct nbd_request request;
fill_request( &request, REQUEST_DISCONNECT, 0, 0 );
/* FIXME: This shouldn't be a FATAL error. We should just drop
* the mirror without affecting the main server.
*/
FATAL_IF_NEGATIVE( writeloop( fd, &request, sizeof( request ) ),
"Failed to write the disconnect request." );
return success;
}
#define CHECK_RANGE(error_type) { \
2012-06-22 10:05:41 +01:00
off64_t size;\
int success = socket_nbd_read_hello(params->client, &size); \
if ( success ) {\
if (params->from < 0 || (params->from + params->len) > size) {\
fatal(error_type \
" request %d+%d is out of range given size %d", \
params->from, params->len, size\
);\
}\
}\
else {\
fatal( error_type " connection failed." );\
}\
}
void do_read(struct mode_readwrite_params* params)
{
params->client = socket_connect(&params->connect_to.generic, &params->connect_from.generic);
2012-06-22 10:05:41 +01:00
FATAL_IF_NEGATIVE( params->client, "Couldn't connect." );
CHECK_RANGE("read");
socket_nbd_read(params->client, params->from, params->len,
params->data_fd, NULL, 10);
close(params->client);
}
void do_write(struct mode_readwrite_params* params)
{
params->client = socket_connect(&params->connect_to.generic, &params->connect_from.generic);
2012-06-22 10:05:41 +01:00
FATAL_IF_NEGATIVE( params->client, "Couldn't connect." );
CHECK_RANGE("write");
socket_nbd_write(params->client, params->from, params->len,
params->data_fd, NULL, 10);
close(params->client);
}