-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocal.cpp
310 lines (260 loc) · 7.48 KB
/
local.cpp
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include<stdio.h>
#include<string.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<unistd.h>
#include <list>
#include <fstream>
#include <iostream>
#include <map>
#include <thread>
#include<pthread.h>
#include<stdlib.h>
std::ifstream file("local");
using namespace std;
#define DEFAULT_BUFLEN 50
#define DEFAULT_PORT 27015
#define LOCAL_PORT 27016
map<string, string> localSites;
class Site{
private:
string ip;
string name;
public:
void setIP(string s){
ip = s;
}
void setName(string s){
name = s;
}
string getName(){
return name;
}
string getIP(){
return ip;
}
};
Site split(string s, string del);
string split_v2(string s, string del);
int handle_connection(int client_sock);
string convertToString(char* a, int size);
void updateMap(string name, string ip);
void * tcp_handle_conn (void * client_socket);
int main(int argc , char *argv[])
{
//////////////////////////////////////////////////////////////////////////
if (file.is_open()) {
std::string line;
while (std::getline(file, line)) {
Site b;
b = split(line, ":");
localSites.insert({b.getName(), b.getIP()});
}
file.close();
}
cout <<"\n##########################################" << endl;
map<string, string>::iterator it = localSites.begin();
while (it != localSites.end())
{
cout << "Site: " << it->first << ", IP: " << it->second << std::endl;
++it;
}
cout <<"\n##########################################" << endl;
//////////////////////////////////////////////////////////////////////////
int socket_desc , client_sock , c;
struct sockaddr_in server , client;
//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Could not create socket");
return 1;
}
puts("Socket created");
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(DEFAULT_PORT);
//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
//print the error message
perror("bind failed. Error");
return 1;
}
puts("bind done");
//Listen
listen(socket_desc , 3);
//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
//accept connection from an incoming client
while (client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c))
{
if (client_sock < 0)
{
perror("accept failed");
return 1;
}
puts("Connection accepted");
pthread_t t;
int *pclient = (int *)malloc (sizeof(int));
*pclient = client_sock;
pthread_create(&t, NULL, tcp_handle_conn, pclient);
}
close(socket_desc);
close(client_sock);
return 0;
}
void* tcp_handle_conn (void * client_socket)
{
int read_size;
int sock = *((int*)client_socket);
free(client_socket);
char client_message[DEFAULT_BUFLEN];
//Receive a message from client
while( (read_size = recv(sock, client_message , DEFAULT_BUFLEN , 0)) > 0 )
{
bool flag = false;
client_message[read_size]='\0';
string s = "";
printf("Message received: %s\nBytes received: %d\n\n", client_message, read_size);
for(int i = 0; i < sizeof(client_message); i++){
if(client_message[i] == '.'){
flag = true;
}
if(client_message[i] == '\0'){
break;
}
s = s + client_message[i];
}
cout << s << endl;
string x;
if(flag == true){
x = split_v2(s, "."); //string za pretragu u mapi
}
else{
x = client_message;
}
if(localSites.find(x)!=localSites.end()){
//if the element is found before the end of the map
string connect;
connect = "Connected to " + localSites[x];
char cstr[connect.size() + 1];
connect.copy(cstr, connect.size() + 1);
cstr[connect.size()] = '\0';
send(sock , cstr , strlen(cstr), 0);
}
else{
int sockLocal;
struct sockaddr_in local;
char messageLocal[DEFAULT_BUFLEN];
char local_reply[DEFAULT_BUFLEN];
sockLocal = socket(AF_INET , SOCK_STREAM , 0);
if (sockLocal == -1)
{
printf("Could not create socket");
exit(0);
}
local.sin_addr.s_addr = inet_addr("127.0.0.1");
local.sin_family = AF_INET;
local.sin_port = htons(LOCAL_PORT);
if (connect(sockLocal , (struct sockaddr *)&local , sizeof(local)) < 0)
{
perror("connect failed. Error");
exit(0);
}
puts("Connected to Local server\n");
char cstr[x.size() + 1];
x.copy(cstr, x.size() + 1);
cstr[x.size()] = '\0';
if( send(sockLocal , cstr , strlen(cstr), 0) < 0)
{
puts("Send failed");
exit(0);
}
memset(local_reply,'\0', DEFAULT_BUFLEN);
if( recv(sockLocal , local_reply , DEFAULT_BUFLEN , 0)<0)
{
puts("recv failed");
break;
}
if(local_reply[0] == 'x'){
char *message = "Couldn't find IP address.";
send(sock , message , strlen(message), 0);
}
else
{
string ip;
string connect;
for(int i = 0; i < sizeof(local_reply); i++){
if(local_reply[i] == '\0'){
break;
}
connect = connect + local_reply[i];
ip = ip + local_reply[i];
}
connect = "Connected to " + connect;
char cstr[connect.size() + 1];
connect.copy(cstr, connect.size() + 1);
cstr[connect.size()] = '\0';
send(sock , cstr , strlen(cstr), 0);
updateMap(x, ip);
}
close(sockLocal);
}
}
if(read_size == 0)
{
puts("Client disconnected");
fflush(stdout);
}
else if(read_size == -1)
{
perror("recv failed");
}
}
Site split(string s, string del)
{
Site site;
string x;
int i = 1;
int start, end = -1 * del.size();
do {
start = end + del.size();
end = s.find(del, start);
x = s.substr(start, end - start);
if( i % 2 != 0){
site.setName(x);
}
else{
site.setIP(x);
}
i++;
} while (end != -1);
return site;
}
string split_v2(string s, string del = " "){
string x;
int start, end = -1 * del.size();
start = end + del.size();
end = s.find(del, start);
x = s.substr(start, end - start);
return x;
}
string convertToString(char* a, int size)
{
int i;
string s = "";
for (i = 0; i < size; i++) {
if(a[i] == '\0' || a[i] == '\n'){
break;
}
s = s + a[i];
}
return s;
}
void updateMap(string name, string ip){
localSites.insert({name, ip});
cout << "DATA BASE UPDATED\n"<< endl;
}