summaryrefslogtreecommitdiffstats
path: root/src/client
diff options
context:
space:
mode:
authorJohann Latocha2012-02-03 15:21:50 +0100
committerJohann Latocha2012-02-03 15:21:50 +0100
commit0b824d5827677746bf04d9d286a45c20d8fb160c (patch)
tree0d15c9310b0b508e394d9db7d46dd089c4844eee /src/client
parent[BUILD] Install target added (diff)
downloaddnbd3-0b824d5827677746bf04d9d286a45c20d8fb160c.tar.gz
dnbd3-0b824d5827677746bf04d9d286a45c20d8fb160c.tar.xz
dnbd3-0b824d5827677746bf04d9d286a45c20d8fb160c.zip
[CLIENT] Connecting via config file
Diffstat (limited to 'src/client')
-rw-r--r--src/client/client.c80
1 files changed, 64 insertions, 16 deletions
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 <getopt.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
+#include <glib.h>
#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 <host> -p <port> -v <vid> -r <rid> -d <device>\n", argv_0);
+ printf("Usage: %s -h <host> -p <port> -v <vid> -r <rid> -d <device> || -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);