-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathTCPSocket.cpp
228 lines (192 loc) · 7.05 KB
/
TCPSocket.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
#include "TCPSocket.h"
#include "FunctionsWindowsNT.h"
TCPSocket::TCPSocket(const char * host, const PORT numPort, bool authRequiered) {
hostname = new char[strlen(host) + 1];
strcpy(hostname, host);
port = numPort;
username = NULL;
password = NULL;
recvData = NULL;
TCPSetLogin = false;
TCPConnected = false;
TCPAuthRequiered = authRequiered;
}
void TCPSocket::login(const char * user, const char * passwd) {
if (TCPAuthRequiered) {
username = new char[strlen(user) + 1];
strcpy(username, user);
password = new char[strlen(passwd) + 1];
strcpy(password, passwd);
TCPSetLogin = true;
}
}
bool TCPSocket::connect() {
WSADATA wsaData;
struct sockaddr_in their_addr;
char * IPAddress;
bool result = false, err = false;
if (!TCPConnected) {
if (TCPAuthRequiered) {
if (!TCPSetLogin) err = true;
}
if (!err) {
if (WSAStartup(MAKEWORD(2, 0), &wsaData) == 0) {
if ((sockData = socket(AF_INET, SOCK_STREAM, 0)) != INVALID_SOCKET) {
IPAddress = FunctionsWindowsNT::DNShostname(hostname);
if (IPAddress != NULL) {
/* Server data struct */
their_addr.sin_family = AF_INET; // ; Family AF_INET
their_addr.sin_addr.s_addr = inet_addr(IPAddress); // ; IP Address = MSN_HOST
their_addr.sin_port = htons(port); // ; Port = MSN_PORT
memset(&(their_addr.sin_zero), '0', 8); // ; IP:Port = MSN_HOST:MSN_PORT
if (TCPFunctions::TCPconnect(sockData, (struct sockaddr *) &their_addr, sizeof(struct sockaddr))) {
TCPConnected = true; result = true;
}
else err = true;
free(IPAddress);
}
else err = true;
}
else err = true;
if (err) WSACleanup();
}
}
}
return result;
}
bool TCPSocket::send(const char * message, unsigned long numBytes) {
if (TCPConnected) {
if (TCPFunctions::TCPsend(sockData, message, numBytes)) return true;
}
return false;
}
bool TCPSocket::recv(bool blocked) {
if (TCPConnected) {
free(recvData);
recvData = (char *) malloc(BUFFER_SIZE + 1);
if (recvData != NULL) {
if (TCPFunctions::TCPrecv(sockData, this, blocked)) return true;
}
}
return false;
}
bool TCPSocket::close() {
if (TCPConnected) {
if (closesocket(sockData) == 0) return true;
}
return false;
}
bool TCPSocket::serverRequieredAuth() {
return TCPAuthRequiered;
}
void TCPSocket::setHostname(const char * host) {
delete [] hostname;
hostname = new char[strlen(host) + 1];
strcpy(hostname, host);
}
void TCPSocket::setUsername(const char * user) {
if (TCPAuthRequiered) {
delete [] username;
username = new char[strlen(user) + 1];
strcpy(username, user);
if (password != NULL) TCPSetLogin = true;
}
}
void TCPSocket::setPassword(const char * passwd) {
if (TCPAuthRequiered) {
delete [] password;
password = new char[strlen(passwd) + 1];
strcpy(password, passwd);
if (username != NULL) TCPSetLogin = true;
}
}
void TCPSocket::setPort(const PORT numPort) { port = numPort; }
void TCPSocket::setReferenceData(char * pointer) {
recvData = pointer;
}
char * TCPSocket::getHostname() { return hostname; }
char * TCPSocket::getUsername() {
if (TCPAuthRequiered) {
if ((TCPSetLogin) || (username != NULL)) {
return username;
}
}
return NULL;
}
char * TCPSocket::getPassword() {
if (TCPAuthRequiered) {
if ((TCPSetLogin) || (password != NULL)) {
return password;
}
}
return NULL;
}
PORT TCPSocket::getPort() { return port; }
char * TCPSocket::getServerData() {
if (recvData != NULL) {
if (strlen(recvData) > 0) {
return recvData;
}
}
return NULL;
}
bool TCPFunctions::TCPconnect(SOCKET socket, const struct sockaddr * sockAddr, int sockAddrSize) {
if (connect(socket, sockAddr, sockAddrSize) != SOCKET_ERROR) return true;
return false;
}
bool TCPFunctions::TCPsend(int socket, const char * message, unsigned long numBytes) {
if (numBytes == 0) {
if (send(socket, message, strlen(message), 0) == strlen(message)) return true;
}
else {
if (send(socket, message, numBytes, 0) == numBytes) return true;
}
return false;
}
/* ##################################################################################
## ##
## [ + ] FUNCTION: TCPrecv ##
## [ + ] DESCRIPTION: block until data is avaliable, recive data from server ##
## set non_block socket flag and get all pending data ##
## [ + ] PARAMETERS: socket -> socket desctiptor ##
## buffer -> message stored ##
## [ + ] RETURN: bool -> true: if recv data correctly ##
## false: if not recv data correctly ##
## ##
################################################################################## */
bool TCPFunctions::TCPrecv(int socket, TCPSocket * object, bool blocked) {
unsigned long total = 0, flag = 1;
char localBuffer[BUFFER_SIZE + 1];
bool err = false, first = true;
char * pBuffer = (char *) malloc(BUFFER_SIZE);
if (pBuffer != NULL) {
*(pBuffer) = '\x00';
if (object->getServerData() != NULL) { free(object->getServerData()); object->setReferenceData(NULL); }
if (!blocked) {
/* set socket flag -> NON_BLOCK */
if (ioctlsocket(socket, FIONBIO, &flag) == SOCKET_ERROR) err = true;
else first = false;
}
for(long n = 0;(((n = recv(socket, localBuffer, BUFFER_SIZE, 0)) > 0) && (!err));) {
if ((first) && (blocked)) {
/* set socket flag -> NON_BLOCK => get all pending data (if no data avaliable - exit for) */
if (ioctlsocket(socket, FIONBIO, &flag) == SOCKET_ERROR) err = true;
else first = false;
}
if (!err) {
total += n;
localBuffer[n] = '\x00';
pBuffer = (char *) realloc(pBuffer, total + 1);
if (pBuffer != NULL) {
object->setReferenceData(pBuffer);
strcat(pBuffer, localBuffer);
}
else err = true;
}
}
}
/* set socket flag -> BLOCK => restore socket flag */
if (!first) { flag--; ioctlsocket(socket, FIONBIO, &flag); }
if ((!err) && (total > 0)) return true;
return false;
}