-
Notifications
You must be signed in to change notification settings - Fork 0
/
IPV4Socket.cc
290 lines (242 loc) · 7.97 KB
/
IPV4Socket.cc
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
#include "IPV4Socket.hh"
#include "Request.hh"
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include "Errcode.hh"
#include "Ex.hh"
/*
All encapsulation for different operating systems networking code is done here
*/
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <cstdlib>
// Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
#pragma comment(lib, "Ws2_32.lib")
#pragma comment(lib, "Mswsock.lib")
#pragma comment(lib, "AdvApi32.lib")
WSADATA Socket::wsaData;
#else // linux
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#endif
#include <signal.h>
#include <memory.h>
#include <fstream>
using namespace std;
#ifdef __linux__
inline void testResult(int result, const char *file, int lineNum, Errcode err) {
if (result < 0) {
throw Ex(file, lineNum, err);
}
}
#elif _WIN32
inline void testResult(int result, const char *file, int lineNum, Errcode err) {
if (result != 0) {
throw Ex(file, lineNum, err);
}
}
#endif
// Initializes Winsock
void Socket::classInit() {
#ifdef _WIN32
testResult(WSAStartup(MAKEWORD(2, 2), &wsaData), __FILE__, __LINE__,
Errcode::SOCKET);
#endif
return;
}
IPV4Socket::~IPV4Socket()
{
close(sckt);
}
// Takes care of allocations made by Winsock
void Socket::classCleanup() {
#ifdef _WIN32
WSACleanup();
#endif
}
#ifdef __linux__
// Constructor for HTTP server
IPV4Socket::IPV4Socket(uint16_t port) : Socket(port) {
int yes = 1;
testResult(sckt = socket(AF_INET, SOCK_STREAM, 0), __FILE__, __LINE__,
Errcode::SOCKET);
testResult(setsockopt(sckt, SOL_SOCKET, SO_REUSEADDR, (const char *) &yes, sizeof(yes)),
__FILE__, __LINE__, Errcode::SETSOCKOPT);
memset(sockaddress, 0, sizeof(sockaddress));
sockaddr_in* sockAddr = (sockaddr_in*)sockaddress;
sockAddr->sin_family = AF_INET;
sockAddr->sin_addr.s_addr = INADDR_ANY;
sockAddr->sin_port = htons(port);
::bind(sckt, (struct sockaddr *)sockAddr, sizeof(sockaddr_in));
testResult(listen(sckt, 20), __FILE__, __LINE__, Errcode::LISTEN);
}
// Constructor for client
IPV4Socket::IPV4Socket(const char *addr, uint16_t port) : Socket(addr, port) {
struct hostent *server;
testResult(sckt = socket(AF_INET, SOCK_STREAM, 0), __FILE__, __LINE__,
Errcode::SOCKET);
server = gethostbyname(address);
if (server == nullptr) {
throw Ex(__FILE__, __LINE__, Errcode::SERVER_INVALID);
}
sockaddr_in* sockAddr = (sockaddr_in*)sockaddress;
sockAddr->sin_family = AF_INET;
// bcopy((char *)server->h_addr, (char *)&sockaddress.sin_addr.s_addr,
// server->h_length);
sockAddr->sin_addr.s_addr = inet_addr(address);
sockAddr->sin_port = htons(port);
if (connect(sckt, (struct sockaddr *)sockaddress, sizeof(sockaddr_in)) < 0) {
throw Ex(__FILE__, __LINE__, Errcode::CONNECTION_FAILURE);
}
}
// Server side
void IPV4Socket::wait() {
struct sockaddr_in client_addrconfig;
socklen_t client_length = sizeof(client_addrconfig);
while (true) {
cout << "WAITING CONNECTION." << endl;
int returnsckt =
accept(sckt, (struct sockaddr *)&client_addrconfig, &client_length);
// int senderSock = accept(listenSock, (struct sockaddr *) &sockaddress,
//&senderNameLen);
if (returnsckt >= 0) {
cout << "CONNECT SUCCESSFULLY"
<< "\n";
req->handleServer(returnsckt);
close(returnsckt);
// if you are not familiar with socket, try below code
// read(senderSock,testin, sizeof(testin)-1);
// cout<<testin<<endl;
// strcpy(testout,"hello,this is server");
// write(senderSock,testout,sizeof(testout));
// cout<<listenSock;
} else {
throw Ex(__FILE__, __LINE__, Errcode::CONNECTION_FAILURE);
}
}
}
#endif
#ifdef _WIN32
// Constructor for server
IPV4Socket::IPV4Socket(uint16_t port) : Socket(port) {
struct addrinfo *result = NULL;
struct addrinfo *ptr = NULL;
struct addrinfo hints;
char port_string[8];
itoa(port, port_string, 10);
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = 1; // TODO: Originally AI_PASSIVE, convert to macro
// TODO: Find out if port should stay as uint16_t or convert to const char *
// Resolve the local address and port to be used by the server
testResult(getaddrinfo(NULL, port_string, &hints, &result), __FILE__,
__LINE__, Errcode::SOCKET);
sckt = INVALID_SOCKET;
// Create a SOCKET for the server to listen for client connections
sckt = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
// TODO: Determine how to best convert for use with test result
if (sckt == INVALID_SOCKET) {
// WSACleanup();
freeaddrinfo(result);
throw Ex1(Errcode::SOCKET);
}
// Setup the TCP listening socket
if (bind(sckt, result->ai_addr, (int)result->ai_addrlen) == SOCKET_ERROR) {
cerr << WSAGetLastError() << endl;
freeaddrinfo(result);
closesocket(sckt);
throw Ex1(Errcode::SOCKET_BIND);
// WSACleanup();
}
freeaddrinfo(result);
if (listen(sckt, SOMAXCONN) == SOCKET_ERROR) {
cerr << "listen failed with error: " << WSAGetLastError() << endl;
closesocket(sckt);
// WSACleanup();
return;
}
// TODO: Check if send/receive needs to be implemented
// TODO: Check how disconnect/shutdown should be handled
}
// Constructor for csp/http client
IPV4Socket::IPV4Socket(const char *addr, uint16_t port) : Socket(addr, port) {
sckt = INVALID_SOCKET;
struct addrinfo *result = NULL;
struct addrinfo *ptr = NULL;
struct addrinfo hints;
char port_string[8];
itoa(port, port_string, 10);
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
// TODO: Find out if port should stay as uint16_t or convert to const char *
// Resolve the address and port to be used by the client
testResult(getaddrinfo(addr, port_string, &hints, &result), __FILE__,
__LINE__, Errcode::SOCKET);
for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {
// Create a SOCKET for connecting to server
sckt = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
testResult(sckt == INVALID_SOCKET, __FILE__, __LINE__, Errcode::SOCKET);
// TODO: Check if connect should be passed to test_result and
// closesocket
// reimplemented in destructor
if (connect(sckt, ptr->ai_addr, (int)ptr->ai_addrlen) == SOCKET_ERROR) {
closesocket(sckt);
sckt = INVALID_SOCKET;
continue;
}
break;
}
testResult(sckt == INVALID_SOCKET, __FILE__, __LINE__, Errcode::SOCKET);
// TODO: Get some more info on this
// Probably should remove and leave to the destructor???
freeaddrinfo(result);
}
// Server side
void IPV4Socket::wait() {
struct sockaddr_in client_addrconfig;
socklen_t client_length = sizeof(client_addrconfig);
SOCKET returnsckt = INVALID_SOCKET;
while (true) {
cout << "WAITING CONNECTION." << endl;
// Accept a client socket
returnsckt = accept(sckt, NULL, NULL);
if (returnsckt == INVALID_SOCKET) {
cerr << "accept failed with error: " << WSAGetLastError() << endl;
closesocket(sckt);
// WSACleanup();
return;
}
if (returnsckt >= 0) {
cout << "CONNECT SUCCESSFULLY"
<< "\n";
req->handle(returnsckt);
close(returnsckt);
// csp18summer: if you are not familiar with socket, try below code
// read(senderSock,testin, sizeof(testin)-1);
// cout<<testin<<endl;
// strcpy(testout,"hello,this is server");
// write(senderSock,testout,sizeof(testout));
// cout<<listenSock;
} else {
throw Ex(__FILE__, __LINE__, Errcode::CONNECTION_FAILURE);
}
}
}
#endif
// this code is outside of windows ifdef???
void IPV4Socket::send(uint32_t reqn) {
write(sckt, &reqn, sizeof(uint32_t));
}
void IPV4Socket::sendAndAwait(uint32_t reqn, Request& r) {
send(reqn);
r.handle(sckt);
}