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
|
#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;
}
|