-
Notifications
You must be signed in to change notification settings - Fork 0
/
connections.cpp
296 lines (233 loc) · 6.67 KB
/
connections.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
#include <iostream>
#include <string>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include "connections.h"
#include "constant.h"
using namespace std;
int UDPsent = 0;
int UDPreceived = 0;
void ipString2Char4(std::string ip, char* buf) // buf must be size 4
{
ip.replace(ip.find("."),1," ");
ip.replace(ip.find("."),1," ");
ip.replace(ip.find("."),1," ");
std::stringstream ssip(ip);
int a;
ssip >> a; buf[0] = (char)a;
ssip >> a; buf[1] = (char)a;
ssip >> a; buf[2] = (char)a;
ssip >> a; buf[3] = (char)a;
}
std::string char42String(char* buf) // buf must be size 4
{
std::stringstream aux;
aux << (unsigned int) ((uint8_t)buf[0]) << ".";
aux << (unsigned int) ((uint8_t)buf[1]) << ".";
aux << (unsigned int) ((uint8_t)buf[2]) << ".";
aux << (unsigned int) ((uint8_t)buf[3]);
return aux.str();
}
//caller need to free the char*
std::string getOwnIPAddr(){
struct ifaddrs * ifAddrStruct=NULL;
struct ifaddrs * ifa=NULL;
void * tmpAddrPtr=NULL;
getifaddrs(&ifAddrStruct);
std::string result;
int i=0;
for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) {
if (!ifa->ifa_addr) {
continue;
}
i++;
if(i==4){
tmpAddrPtr=&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
char addressBuffer[INET_ADDRSTRLEN];
inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
result = addressBuffer;
}
}
if (ifAddrStruct!=NULL) freeifaddrs(ifAddrStruct);
return result;
}
int getUDPSent()
{
return UDPsent;
}
int getUDPReceived()
{
return UDPreceived;
}
int bindSocket(int port)
{
struct sockaddr_in svrAdd, clntAdd;
//create socket
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sockfd < 0)
{
std::cout << "Cannot open socket" << std::endl;
exit(1);
}
memset((char*) &svrAdd,0, sizeof(svrAdd));
svrAdd.sin_family = AF_INET;
svrAdd.sin_addr.s_addr = INADDR_ANY;
svrAdd.sin_port = htons(port);
//bind socket
int ret = ::bind(sockfd, (struct sockaddr *)&svrAdd, sizeof(svrAdd));
if( ret < 0)
{
std::cout << "open_socket: Cannot bind" << std::endl;
exit(1);
}
printf("UDP Listening at port %d...\n", port);
return sockfd;
}
int receiveUDP(int sockfd, char* buf, uint32_t len, std::string& sender)
{
struct sockaddr addr;
socklen_t fromlen = sizeof addr;
int byte_count = recvfrom(sockfd, buf, len, 0, &addr, &fromlen);
if (byte_count == -1)
{
printf("ERROR RECEIVING!!!\n");
exit(-1);
}
struct sockaddr_in *sin = (struct sockaddr_in *) &addr;
sender = inet_ntoa(sin->sin_addr);
UDPreceived++;
return byte_count;
}
void sendUDP(int sockfd, std::string& add, int port, char* buf, uint32_t len)
{
struct sockaddr_in servaddr,cliaddr;
struct hostent *server;
server = gethostbyname(add.c_str());
if(server == NULL)
{
std::cout << "Host does not exist" << std::endl;
exit(1);
}
memset((char *) &servaddr,0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
memcpy((char *) &servaddr.sin_addr.s_addr,(char *) server -> h_addr, server -> h_length);
servaddr.sin_port = htons(port);
/*
//int random = rand() % 100;
//if ((random) <= 10 ) return;
static int counter = 1;
if ((counter++) % 4 == 0 ){
//std::cout << "Drop message: " << (unsigned int) (((Message* )(buf))->type) << " " << add << std::endl;
return;
}
*/
int ret = sendto(sockfd,buf, len, 0, (struct sockaddr *)&servaddr,sizeof(servaddr));
if (ret == -1)
{
printf("Error in sendUDP: cannot send\n");
return;
}
UDPsent++;
//printf("Message sent\n");
}
/* Server operation */
int open_socket(int port)
{
struct sockaddr_in svrAdd, clntAdd;
int listenFd;
//create socket
listenFd = socket(AF_INET, SOCK_STREAM, 0);
int yes = 1;
if (setsockopt(listenFd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1)
{
printf("setsockopt\n");
exit(1);
}
if(listenFd < 0)
{
std::cout << "Cannot open socket" << std::endl;
exit(1);
}
memset((char*) &svrAdd,0, sizeof(svrAdd));
svrAdd.sin_family = AF_INET;
svrAdd.sin_addr.s_addr = INADDR_ANY;
svrAdd.sin_port = htons(port);
//bind socket
int ret = ::bind(listenFd, (struct sockaddr *)&svrAdd, sizeof(svrAdd));
if(ret < 0)
{
std::cout << "open_socket: Cannot bind" << std::endl;
exit(1);
}
printf("TCP Listening at port %d...\n", port);
return listenFd;
}
int listen_socket(int listenFd)
{
socklen_t len; //store size of the address
int connFd;
struct sockaddr_in svrAdd, clntAdd;
listen(listenFd, 5);
len = sizeof(clntAdd);
//this is where client connects. svr will hang in this mode until client conn
connFd = accept(listenFd, (struct sockaddr *)&clntAdd, &len);
if (connFd < 0)
{
std::cout << "Cannot accept connection" << std::endl;
exit(1);
}
else
{
//std::cout << "Server Connection successful " << std::endl;
}
return connFd;
}
/*Client operation */
int connect_to_server(const char* add, int port, int* connectionFd)
{
struct sockaddr_in svrAdd;
struct hostent *server;
if((port > 65535) || (port < 2000))
{
std::cout<<"Please enter port number between 2000 - 65535"<<std::endl;
exit(1);
}
//create client skt
*connectionFd = socket(AF_INET, SOCK_STREAM, 0);
int iSetOption = 1;
setsockopt(*connectionFd, SOL_SOCKET, SO_REUSEADDR, (char*)&iSetOption,
sizeof(iSetOption));
if(*connectionFd < 0)
{
std::cout << "Cannot open socket" << std::endl;
exit(1);
}
else{
//std::cout << "Socket opened" << std::endl;
}
server = gethostbyname(add);
if(server == NULL)
{
std::cout << "Host does not exist" << std::endl;
exit(1);
}
memset((char *) &svrAdd,0, sizeof(svrAdd));
svrAdd.sin_family = AF_INET;
memcpy((char *) &svrAdd.sin_addr.s_addr,(char *) server -> h_addr, server -> h_length);
svrAdd.sin_port = htons(port);
int checker = connect(*connectionFd,(struct sockaddr *) &svrAdd, sizeof(svrAdd));
if (checker < 0)
{
//printf("Cannot connect to: %s \n",add);
return -1;
//exit(1);
}
else{
return 0;
//std::cout << "Client Connection Successful" << std::endl;
}
}