summaryrefslogtreecommitdiffstats
path: root/src/client/client.c
blob: ff95ba14f44d6bd58566e03dd2740f1091fb6968 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * This file is part of the Distributed Network Block Device 3
 *
 * Copyright(c) 2011-2012 Johann Latocha <johann@latocha.de>
 *
 * This file may be licensed under the terms of of the
 * GNU General Public License Version 2 (the ``GPL'').
 *
 * Software distributed under the License is distributed
 * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
 * express or implied. See the GPL for the specific language
 * governing rights and limitations.
 *
 * You should have received a copy of the GPL along with this
 * program. If not, go to http://www.gnu.org/licenses/gpl.html
 * or write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <getopt.h>
#include <sys/ioctl.h>
#include <netinet/in.h>

#include "../types.h"
#include "../version.h"

void dnbd3_print_help(char* argv_0)
{
    printf("Usage: %s -h <host> -p <port> -v <vid> -r <rid> -d <device>\n", argv_0);
    printf("Start the DNBD3 client.\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");
    printf("-r or --rid \t\t Release-ID of exported image.\n");
    printf("-d or --device \t\t DNBD3 device name.\n");
    printf("-c or --changehost \t Change dnbd3-server on device (DEBUG).\n");
    printf("-H or --help \t\t Show this help text and quit.\n");
    printf("-V or --version \t Show version and quit.\n");
    exit(EXIT_SUCCESS);
}

void dnbd3_print_version()
{
    printf("Version: %s\n", VERSION_STRING);
    exit(EXIT_SUCCESS);
}

int main(int argc, char *argv[])
{
    int fd;
    char *host = NULL;
    char *port = NULL;
    int vid = 0;
    int rid = 0;
    char *dev = NULL;
    int change_host = 0;

    int opt = 0;
    int longIndex = 0;
    static const char *optString = "h:p:v:r:d:c:HV?";
    static const struct option longOpts[] =
    {
    { "host", required_argument, NULL, 'h' },
    { "port", required_argument, NULL, 'p' },
    { "vid", required_argument, NULL, 'v' },
    { "rid", required_argument, NULL, 'r' },
    { "device", required_argument, NULL, 'd' },
    { "changehost", required_argument, NULL, 'c' },
    { "help", no_argument, NULL, 'H' },
    { "version", no_argument, NULL, 'V' }, };

    opt = getopt_long(argc, argv, optString, longOpts, &longIndex);

    while (opt != -1)
    {
        switch (opt)
        {
        case 'h':
            host = optarg;
            break;
        case 'p':
            port = optarg;
            break;
        case 'v':
            vid = atoi(optarg);
            break;
        case 'r':
            rid = atoi(optarg);
            break;
        case 'd':
            dev = optarg;
            break;
        case 'c':
            host = optarg;
            change_host = 1;
            break;
        case 'H':
            dnbd3_print_help(argv[0]);
            break;
        case 'V':
            dnbd3_print_version();
            break;
        case '?':
            dnbd3_print_help(argv[0]);
        }
        opt = getopt_long(argc, argv, optString, longOpts, &longIndex);
    }

    // change host
    if (change_host && host && dev && !port && (vid == 0) && (rid == 0))
    {
        fd = open(dev, O_RDONLY);

        if (ioctl(fd, IOCTL_DISCONNECT) < 0)
            printf("ERROR: ioctl not successful\n");

        if (ioctl(fd, IOCTL_SET_HOST, host) < 0)
            printf("ERROR: ioctl not successful\n");

        if (ioctl(fd, IOCTL_CONNECT) < 0)
            printf("ERROR: ioctl not successful\n");

        close(fd);
        exit(EXIT_SUCCESS);
    }

    // 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");

        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);
        exit(EXIT_SUCCESS);
    }

    dnbd3_print_help(argv[0]);
    exit(EXIT_FAILURE);
}