summaryrefslogtreecommitdiffstats
path: root/src/client
diff options
context:
space:
mode:
authorJohann Latocha2011-11-29 15:06:45 +0100
committerJohann Latocha2011-11-29 15:06:45 +0100
commit9970fb00c79834703bc990d052439290338467be (patch)
tree9311a9c766193464b269894b18b7dd4f967652a0 /src/client
downloaddnbd3-9970fb00c79834703bc990d052439290338467be.tar.gz
dnbd3-9970fb00c79834703bc990d052439290338467be.tar.xz
dnbd3-9970fb00c79834703bc990d052439290338467be.zip
initial commit
Diffstat (limited to 'src/client')
-rw-r--r--src/client/client.bak75
-rw-r--r--src/client/client.c95
2 files changed, 170 insertions, 0 deletions
diff --git a/src/client/client.bak b/src/client/client.bak
new file mode 100644
index 0000000..bacfa52
--- /dev/null
+++ b/src/client/client.bak
@@ -0,0 +1,75 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netdb.h>
+#include <arpa/inet.h>
+#include <unistd.h>
+
+#include <math.h>
+
+#include "../include/types.h"
+
+#define FILE_SIZE 721127424
+
+int main(int argc, char *argv[])
+{
+ struct sockaddr_in server;
+ unsigned long addr;
+ int sock;
+
+ // Create socket
+ sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
+ if (sock < 0)
+ {
+ printf("ERROR: Socket failure\n");
+ return EXIT_FAILURE;
+ }
+
+ addr = inet_addr(HOST);
+ memcpy((char *) &server.sin_addr, &addr, sizeof(addr));
+ server.sin_family = AF_INET; // IPv4
+ server.sin_port = htons(PORT); // set port number
+
+ // Connect to server
+ if (connect(sock, (struct sockaddr*) &server, sizeof(server)) < 0)
+ {
+ printf("ERROR: Connect failure\n");
+ return EXIT_FAILURE;
+ }
+
+ // Set data
+ struct dnbd3_request request;
+ struct dnbd3_reply reply;
+ request.num = 0;
+
+ // Send to server
+ int i;
+ off_t blocks = FILE_SIZE / DNBD3_BLOCK_SIZE;
+ int e = log(DNBD3_BLOCK_SIZE) / log(2); // logarithmus dualis
+
+
+ for (i = 0; i < blocks; i++)
+ {
+ request.num = i << e; // multiplie by e
+ send(sock, (char *) &request, sizeof(request), 0);
+ recv(sock, &reply, DNBD3_BLOCK_SIZE, MSG_WAITALL);
+ write(STDOUT_FILENO, reply.data, DNBD3_BLOCK_SIZE);
+ }
+
+ /* Fetch "rest" bytes */
+ int rest = FILE_SIZE % DNBD3_BLOCK_SIZE;
+ if (rest != 0)
+ {
+ request.num = i * DNBD3_BLOCK_SIZE;
+ send(sock, (char *) &request, sizeof(request), 0);
+ recv(sock, &reply, sizeof(struct dnbd3_reply), MSG_WAITALL);
+ write(STDOUT_FILENO, reply.data, rest);
+ }
+
+ close(sock);
+ return EXIT_SUCCESS;
+}
diff --git a/src/client/client.c b/src/client/client.c
new file mode 100644
index 0000000..87f20d5
--- /dev/null
+++ b/src/client/client.c
@@ -0,0 +1,95 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <getopt.h>
+#include <sys/ioctl.h>
+#include <netinet/in.h>
+
+#include "../include/types.h"
+#include "../version.h"
+
+void print_help(char* argv_0)
+{
+ printf("Usage: %s -H <host> -p <port> -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("-d or --device \t\t DNBD3 device name.\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 print_version()
+{
+ printf("Version: %s\n", VERSION_STRING);
+ exit(EXIT_SUCCESS);
+}
+
+int main(int argc, char *argv[])
+{
+ char *host = NULL;
+ char *port = NULL;
+ char *dev = NULL;
+
+ int opt = 0;
+ int longIndex = 0;
+ static const char *optString = "H:p:d:hv?";
+ static const struct option longOpts[] =
+ {
+ { "host", required_argument, NULL, 'H' },
+ { "port", required_argument, NULL, 'p' },
+ { "device", required_argument, NULL, 'd' },
+ { "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 'd':
+ dev = optarg;
+ break;
+ case 'h':
+ print_help(argv[0]);
+ break;
+ case 'v':
+ print_version();
+ break;
+ case '?':
+ print_help(argv[0]);
+ }
+ opt = getopt_long(argc, argv, optString, longOpts, &longIndex);
+ }
+
+ if (!host || !port || !dev)
+ {
+ printf("FATAL: Not enough information specified\n");
+ exit(EXIT_FAILURE);
+ }
+
+ int fd;
+ 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_CONNECT) < 0)
+ printf("ERROR: ioctl not successful\n");
+
+ close(fd);
+
+ exit(EXIT_SUCCESS);
+}