From 7544a59da190f71edcfcca80b71e9cdc8f548052 Mon Sep 17 00:00:00 2001 From: nick Date: Wed, 6 Jun 2012 12:35:01 +0100 Subject: [PATCH] mirror: Add --bind to our mirror mode. Mirroring doesn't actually work yet, of course. --- src/control.c | 28 ++++++++++++++++++++++------ src/flexnbd.c | 15 ++++++++++----- src/options.h | 4 +++- 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/src/control.c b/src/control.c index 8a6e6a8..9d9c5fa 100644 --- a/src/control.c +++ b/src/control.c @@ -173,14 +173,17 @@ int control_mirror(struct control_params* client, int linesc, char** lines) int fd, map_fd; struct mirror_status *mirror; union mysockaddr connect_to; + union mysockaddr connect_from; + int use_connect_from = 0; uint64_t max_bytes_per_second; int action_at_finish; + if (linesc < 2) { write_socket("1: mirror takes at least two parameters"); return -1; } - + if (parse_ip_to_sockaddr(&connect_to.generic, lines[0]) == 0) { write_socket("1: bad IP address"); return -1; @@ -192,14 +195,22 @@ int control_mirror(struct control_params* client, int linesc, char** lines) return -1; } connect_to.v4.sin_port = htobe16(connect_to.v4.sin_port); - - max_bytes_per_second = 0; + if (linesc > 2) { + if (parse_ip_to_sockaddr(&connect_from.generic, lines[2]) == 0) { + write_socket("1: bad bind address"); + return -1; + } + use_connect_from = 1; + } + + max_bytes_per_second = 0; + if (linesc > 3) { max_bytes_per_second = atoi(lines[2]); } action_at_finish = ACTION_PROXY; - if (linesc > 3) { + if (linesc > 4) { if (strcmp("proxy", lines[3]) == 0) action_at_finish = ACTION_PROXY; else if (strcmp("exit", lines[3]) == 0) @@ -212,12 +223,17 @@ int control_mirror(struct control_params* client, int linesc, char** lines) } } - if (linesc > 4) { + if (linesc > 5) { write_socket("1: unrecognised parameters to mirror"); return -1; } + + /** I don't like use_connect_from but socket_connect doesn't take *mysockaddr :( */ + if (use_connect_from) + fd = socket_connect(&connect_to.generic, &connect_from.generic); + else + fd = socket_connect(&connect_to.generic, NULL); - fd = socket_connect(&connect_to.generic, NULL); remote_size = socket_nbd_read_hello(fd); remote_size = remote_size; // shush compiler diff --git a/src/flexnbd.c b/src/flexnbd.c index ac5abf8..2e15904 100644 --- a/src/flexnbd.c +++ b/src/flexnbd.c @@ -257,7 +257,7 @@ void read_acl_param( int c, char **sock ) read_sock_param( c, sock, acl_help_text ); } -void read_mirror_param( int c, char **sock, char **ip_addr, char **ip_port ) +void read_mirror_param( int c, char **sock, char **ip_addr, char **ip_port, char **bind_addr ) { switch( c ){ case 'h': @@ -273,6 +273,8 @@ void read_mirror_param( int c, char **sock, char **ip_addr, char **ip_port ) case 'p': *ip_port = optarg; break; + case 'b': + *bind_addr = optarg; case 'v': set_debug(1); break; @@ -426,13 +428,13 @@ int mode_mirror( int argc, char *argv[] ) { int c; char *sock = NULL; - char *remote_argv[3] = {0}; + char *remote_argv[4] = {0}; int err = 0; while (1) { c = getopt_long( argc, argv, mirror_short_options, mirror_options, NULL); if ( -1 == c ) break; - read_mirror_param( c, &sock, &remote_argv[0], &remote_argv[1] ); + read_mirror_param( c, &sock, &remote_argv[0], &remote_argv[1], &remote_argv[2] ); } if ( NULL == sock ){ @@ -444,8 +446,11 @@ int mode_mirror( int argc, char *argv[] ) err = 1; } if ( err ) { exit_err( mirror_help_text ); } - - do_remote_command( "mirror", sock, 2, remote_argv ); + + if (argv[2] == NULL) + do_remote_command( "mirror", sock, 2, remote_argv ); + else + do_remote_command( "mirror", sock, 3, remote_argv ); return 0; } diff --git a/src/options.h b/src/options.h index 0349d50..d1425a0 100644 --- a/src/options.h +++ b/src/options.h @@ -136,10 +136,11 @@ struct option mirror_options[] = { GETOPT_SOCK, GETOPT_ADDR, GETOPT_PORT, + GETOPT_BIND, GETOPT_VERBOSE, {0} }; -static char mirror_short_options[] = "hs:l:p:" SOPT_VERBOSE; +static char mirror_short_options[] = "hs:l:p:b:" SOPT_VERBOSE; static char mirror_help_text[] = "Usage: flexnbd " CMD_MIRROR " \n\n" "Start mirroring from the server with control socket SOCK to one at ADDR:PORT.\n\n" @@ -147,6 +148,7 @@ static char mirror_help_text[] = "\t--" OPT_ADDR ",-l \tThe address to mirror to.\n" "\t--" OPT_PORT ",-p \tThe port to mirror to.\n" SOCK_LINE + BIND_LINE VERBOSE_LINE;