-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.c
192 lines (143 loc) · 3.74 KB
/
node.c
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* Node is a client for Hub and a server for Client. */
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netinet/ip.h>
#include<arpa/inet.h>
#include<pthread.h>
#include<errno.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/time.h>
#include <sys/stat.h>
#include <fcntl.h>
/* Message sent to hub */
struct hubmsg_t {
int mtype;
long port;
};
/* Message to/from client */
struct clientmsg_t {
int mtype;
char name[4000];
long from; // pos or length
};
struct sockaddr_in address, client_addr, hub_addr;
void* handleClient(void* arg)
{
printf("Accepted connection...\n");
int sock = *((int*) arg);
int i, len, f;
char file[256];
struct clientmsg_t clientmsg;
struct clientmsg_t sent;
struct sockaddr_in address; // client
len = sizeof(address);
getpeername(sock, (struct sockaddr*)&address, &len);
// new socket structure
i = recv(sock, &clientmsg, sizeof(struct clientmsg_t), 0);
if (i < 0)
{
perror("[Node] recv from client error:");
return NULL;
}
else printf("Received message...\n");
if (clientmsg.mtype == 4)
{
/* Client requested stuff */
strcpy(file, "source/");
strcat(file,clientmsg.name);
printf("Sending file %s...\n", file);
/* Open file, bin mode, read-only. */
f = open(file, O_RDONLY);
if (f < 0)
{
printf("[Node] Error opening the file '%s'\n", file);
perror(":");
return NULL;
}
if (lseek(f, clientmsg.from, SEEK_SET) < 0)
{
printf("Can't seek in %s.\n",file);
return NULL;
}
sent.mtype = 5;
sent.from = read(f, sent.name, 100);
if (sent.from < 0)
{
printf("Error reading file %s.\n", file);
return NULL;
}
int ok;
ok = send(sock, &sent, sizeof(struct clientmsg_t), 0);
if (ok<0)
{
perror("[Node] Send to client error:");
return NULL;
}
}
else
{
/* Unrecognized message (0 = Client finished) */
}
return NULL;
}
int main() {
int sock, sockhub, sock_client;
int i, len;
struct hubmsg_t hubmsg;
struct clientmsg_t clientmsg;
pthread_t thr;
printf("Starting...\n");
sockhub = socket(AF_INET, SOCK_STREAM, 0);
if (sock<0) {
perror("[Node] Error creating:");
}
printf("Created sock...\n");
/* First of all, the node must connect, i.e. send message to the hub. */
hub_addr.sin_family = AF_INET;
hub_addr.sin_addr.s_addr = INADDR_ANY;
hub_addr.sin_port = htons(1026);
if (connect(sockhub, (struct sockaddr*) &hub_addr, sizeof(struct sockaddr))<0) {
perror("[Node] Connect error:");
}
struct timeval tv;
struct timezone tz;
gettimeofday(&tv, &tz);
srand(tv.tv_usec);
hubmsg.mtype = 1;
hubmsg.port = 1026 + rand()%1000;
printf("Created message to send...\n");
i = send(sockhub, &hubmsg, sizeof(struct hubmsg_t), 0);
if (i<0) perror("[Node] can't send to hub :");
printf("Sent.\n");
/* Second of all, the node must be able to accept connections from clients.
I.e. create a new socket, as the socket used for the communication with the hub cannot be reused.
(as it already acts like a client, and we need one to act like a server) */
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock<0) {
perror("[Node] Error creating:");
}
printf("Created second socket...\n");
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(hubmsg.port);
if (bind(sock, (struct sockaddr*) &address, sizeof(struct sockaddr))<0) {
perror("[Node] Error binding");
}
printf("Binded...\n");
if (listen(sock, 20) < 0)
perror("[Node] won't listen");
printf("Listening...\n");
while(1) {
len = sizeof(struct sockaddr_in);
sock_client = accept(sock, (struct sockaddr*) &client_addr, &len);
pthread_create(&thr, 0, handleClient, &sock_client);
sleep(1);
}
close(sock_client);
close(sockhub);
close(sock);
return 0;
}