From 0b824d5827677746bf04d9d286a45c20d8fb160c Mon Sep 17 00:00:00 2001 From: Johann Latocha Date: Fri, 3 Feb 2012 15:21:50 +0100 Subject: [CLIENT] Connecting via config file --- src/client/client.c | 80 ++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 64 insertions(+), 16 deletions(-) (limited to 'src/client/client.c') diff --git a/src/client/client.c b/src/client/client.c index ff95ba1..564d6b0 100644 --- a/src/client/client.c +++ b/src/client/client.c @@ -25,14 +25,18 @@ #include #include #include +#include #include "../types.h" #include "../version.h" +char *_config_file_name = DEFAULT_CLIENT_CONFIG_FILE; + void dnbd3_print_help(char* argv_0) { - printf("Usage: %s -h -p -v -r -d \n", argv_0); + printf("Usage: %s -h -p -v -r -d || -f file\n", argv_0); printf("Start the DNBD3 client.\n"); + printf("-f or --file \t\t Configuration file (default /etc/dnbd3-client.conf)\n"); printf("-h or --host \t\t Host running dnbd3-server.\n"); printf("-p or --port \t\t Port used by server.\n"); printf("-v or --vid \t\t Volume-ID of exported image.\n"); @@ -50,6 +54,30 @@ void dnbd3_print_version() exit(EXIT_SUCCESS); } +void dnbd3_connect(char *host, char *port, int vid, int rid, char *dev) +{ + int fd = open(dev, O_WRONLY); + + printf("Connecting %s to %s:%s vid:%i rid:%i\n", dev, host, port, vid, rid); + + if (ioctl(fd, IOCTL_SET_HOST, host) < 0) + printf("ERROR: ioctl not successful\n"); + + if (ioctl(fd, IOCTL_SET_PORT, port) < 0) + printf("ERROR: ioctl not successful\n"); + + if (ioctl(fd, IOCTL_SET_VID, vid) < 0) + printf("ERROR: ioctl not successful\n"); + + if (ioctl(fd, IOCTL_SET_RID, rid) < 0) + printf("ERROR: ioctl not successful\n"); + + if (ioctl(fd, IOCTL_CONNECT) < 0) + printf("ERROR: ioctl not successful\n"); + + close(fd); +} + int main(int argc, char *argv[]) { int fd; @@ -62,9 +90,10 @@ int main(int argc, char *argv[]) int opt = 0; int longIndex = 0; - static const char *optString = "h:p:v:r:d:c:HV?"; + static const char *optString = "f:h:p:v:r:d:c:HV?"; static const struct option longOpts[] = { + { "file", required_argument, NULL, 'f' }, { "host", required_argument, NULL, 'h' }, { "port", required_argument, NULL, 'p' }, { "vid", required_argument, NULL, 'v' }, @@ -80,6 +109,9 @@ int main(int argc, char *argv[]) { switch (opt) { + case 'f': + _config_file_name = optarg; + break; case 'h': host = optarg; break; @@ -114,7 +146,7 @@ int main(int argc, char *argv[]) // change host if (change_host && host && dev && !port && (vid == 0) && (rid == 0)) { - fd = open(dev, O_RDONLY); + fd = open(dev, O_WRONLY); if (ioctl(fd, IOCTL_DISCONNECT) < 0) printf("ERROR: ioctl not successful\n"); @@ -132,26 +164,42 @@ int main(int argc, char *argv[]) // connect if (host && port && dev && (vid != 0) && (rid != 0)) { - fd = open(dev, O_RDONLY); - - if (ioctl(fd, IOCTL_SET_HOST, host) < 0) - printf("ERROR: ioctl not successful\n"); + dnbd3_connect(host, port, vid, rid, dev); + exit(EXIT_SUCCESS); + } - if (ioctl(fd, IOCTL_SET_PORT, port) < 0) - printf("ERROR: ioctl not successful\n"); + // use configuration file if exist + GKeyFile* gkf; + int i = 0; + size_t j = 0; - if (ioctl(fd, IOCTL_SET_VID, vid) < 0) - printf("ERROR: ioctl not successful\n"); + gkf = g_key_file_new(); - if (ioctl(fd, IOCTL_SET_RID, rid) < 0) - printf("ERROR: ioctl not successful\n"); + if (g_key_file_load_from_file(gkf, _config_file_name, G_KEY_FILE_NONE, NULL)) + { + gchar **groups = NULL; + groups = g_key_file_get_groups(gkf, &j); - if (ioctl(fd, IOCTL_CONNECT) < 0) - printf("ERROR: ioctl not successful\n"); + for (i = 0; i < j; i++) + { + host = g_key_file_get_string(gkf, groups[i], "server", NULL); + port = g_key_file_get_string(gkf, groups[i], "port", NULL); + vid = g_key_file_get_integer(gkf, groups[i], "vid", NULL); + rid = g_key_file_get_integer(gkf, groups[i], "rid", NULL); + dev = g_key_file_get_string(gkf, groups[i], "device", NULL); + dnbd3_connect(host, port, vid, rid, dev); + } - close(fd); + g_strfreev(groups); + g_key_file_free(gkf); exit(EXIT_SUCCESS); } + else + { + printf("WARN: Config file not found: %s\n", _config_file_name); + } + + g_key_file_free(gkf); dnbd3_print_help(argv[0]); exit(EXIT_FAILURE); -- cgit v1.2.3-55-g7522