-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPCSBSocket.lin.cpp
More file actions
394 lines (348 loc) · 11 KB
/
PCSBSocket.lin.cpp
File metadata and controls
394 lines (348 loc) · 11 KB
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
* Copyright (c) 2004, Ben Supnik and Chris Serio.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#include "PCSBSocket.h"
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/ioctl.h>
#include <asm/ioctls.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#define MAJOR_VERSION_REQUIRED 2
#define MINOR_VERSION_REQUIRED 0
#define BLOCKING 0
#define NON_BLOCKING 1
//FILE * hexdump = fopen("hexdump.txt","w");
//extern FILE * hexdump;
/* Construct a new socket. If the socket is to be a server listening port,
* Pass 0 to dynamically allocate a port or a positive number to pick a
* well-known one. */
PCSBSocket::PCSBSocket(unsigned short inPort, bool inServer)
{
int nResult;
int off = 0;
int on = 1;
socketStatus = status_Ready;
mReceivedRelease = false;
mSentRelease = false;
mIsAServer = false;
sIn.sin_family = AF_INET;
sIn.sin_addr.s_addr = INADDR_ANY; //Let windows pick a suitable addy
sIn.sin_port = htons(inPort); //Set the local port or if 0 let windows assign one
mLinSocket = socket(PF_INET,SOCK_STREAM,0); //start the socket for TCP/IP comms
if(mLinSocket == -1)
{
socketStatus = status_Error;
return;
}
if (setsockopt(mLinSocket, IPPROTO_TCP, TCP_NODELAY, (const char *)&on, sizeof(on))<0)
{
printf("-----------------------Could not set TCP_NODELAY on port %d-------------\n",inPort);
}
if (bind(mLinSocket, (struct sockaddr*)&sIn, sizeof(sIn)) != 0) //Bind the socket to the local addy
{
socketStatus = status_Error;
return;
}
unsigned long nSetSocketType = NON_BLOCKING;
nResult = ioctl(mLinSocket,FIONBIO,&nSetSocketType); //set to non-blocking
//We're a server so start listening!
if(inServer)
{
mIsAServer = true;
if(listen(mLinSocket, SOMAXCONN))
{
printf("Could not enable listening!");
socketStatus = status_Error;
return;
}
}
}
PCSBSocket::PCSBSocket(int inWorkerSocket)
{
mLinSocket = inWorkerSocket;
socketStatus = status_Connected;
mReceivedRelease = false;
mSentRelease = false;
mIsAServer = false;
unsigned long nSetSocketType = NON_BLOCKING;
int nResult = ioctl(mLinSocket,FIONBIO,&nSetSocketType); //set to non-blocking
}
/* Start up the networking DLL for the first time. Returns true if
the DLL is ok and sockets can be used. */
/* BAS - note: we ignore the init and shutdown DLL since we can recursively
* open winsock. :-) */
bool PCSBSocket::StartupNetworking(bool inInitDLL)
{
/* Rock on. We don't need to do this networking startup bullcrap. */
return true;
}
/* Tear down the network. Used when the network is all done and ready
to be closed down. */
void PCSBSocket::ShutdownNetworking(bool inCloseDLL)
{
/* this is a REAL os where networking is on by default! */
}
/* Close the socket. you will be forcefully disconnected if necessary. */
PCSBSocket::~PCSBSocket()
{
shutdown(mLinSocket, SHUT_RDWR);
close(mLinSocket);
}
PCSBSocket * PCSBSocket::Accept()
{
printf("Socket was accepted!\n");
int workerSocket = accept(mLinSocket,NULL,NULL);
socketStatus = status_Ready;
if(workerSocket == -1)
return NULL; // No one called.
return new PCSBSocket(workerSocket); // Return a new socket object.
}
/* Get the socket's internal status. */
PCSBSocket::Status PCSBSocket::GetStatus(void)
{
struct timeval timeout;
timeout.tv_sec = 0; //no timeout!
timeout.tv_usec = 0; //no timeout!
if ((socketStatus == status_Ready) && mIsAServer)
{
fd_set socketSetRead;
FD_ZERO(&socketSetRead);
FD_SET(mLinSocket,&socketSetRead);
int nReturn = select(mLinSocket+1,&socketSetRead,NULL,NULL,&timeout);
if (nReturn > 0)
{
if(FD_ISSET(mLinSocket,&socketSetRead)) //Able to read on it?
{
socketStatus = status_Connecting;
return socketStatus;
}
else
{
socketStatus = status_Disconnected;
return socketStatus;
}
}
else if(nReturn == 0) //Didn't have enough time to complete request fully (non-blocking)
{
}
else //Must be a socket error :(
{
socketStatus = status_Error;
}
}
if (socketStatus == status_Connecting)
{
fd_set socketSetWrite,socketSetError;
FD_ZERO(&socketSetWrite);
FD_ZERO(&socketSetError);
FD_SET(mLinSocket,&socketSetWrite); //macro to form set
FD_SET(mLinSocket,&socketSetError);
int nReturn = select(mLinSocket+1,NULL,&socketSetWrite,&socketSetError,&timeout);
// Check to see if our connection completed.
if (nReturn > 0)
{
if((FD_ISSET(mLinSocket,&socketSetWrite)) && !(FD_ISSET(mLinSocket,&socketSetError)))
{ //If the socket is ready to write with no errors
socketStatus = status_Connected;
return socketStatus;
}
else
{
socketStatus = status_Disconnected;
return socketStatus;
}
}
else if(nReturn == 0) //Didn't have enough time to complete request fully (non-blocking)
{
}
else //Must be a socket error :(
{
socketStatus = status_Error;
}
}
return socketStatus;
}
/* Whether an orderly release has been received. Note that it may
not be possible to tell if one has been issued without first reading
buffered data. */
bool PCSBSocket::ReceivedRelease(void)
{
return mReceivedRelease;
}
/* Connect to a server at a port and IP as a client. */
void PCSBSocket::Connect(unsigned long inIP, unsigned short inPort)
{
socketStatus = status_Connecting;
struct sockaddr_in sOut;
int nResult;
sOut.sin_family = AF_INET;
sOut.sin_port = htons(inPort); //set the remote connection port
sOut.sin_addr.s_addr = htonl(inIP); //set the remote IP to connect to
nResult = connect(mLinSocket,(sockaddr*)&sOut,sizeof(sOut)); //connect
if(nResult == -1)
{
if((errno != EAGAIN) && (errno != EWOULDBLOCK) && (errno != EINPROGRESS)) //If it's a mere blocking error, check if connection completed by using select()
{
printf("error = %d", errno);
socketStatus = status_Error;
}
}
else
socketStatus = status_Connected; //If it didn't have an error to begin with
}
/* Read as much data from the socket as you want and is possible.
Positive numbers indicate a read, 0 indicates that no data is
available (either due to a lack of data sent or an orderly release).
A negative number indicates an error, either an unknown error or
a disconnect. To further diagnose 0 or -1 responses, use
GetStatus() and ReceivedRelease(). */
long PCSBSocket::ReadData(void* outBuf,long inLength)
{
int nReturn;
nReturn = recv(mLinSocket,(char*)outBuf,inLength,0);
if(nReturn == -1)
{
if(errno == EAGAIN)
{
//printf("No Data on Socket yet!\n");
return 0; // ** Signal no data
}
else if((errno == ECONNABORTED) || (errno == ECONNREFUSED) || (errno == ECONNRESET) || (errno == EHOSTUNREACH) || (errno == ENETDOWN) || (errno == ENETRESET) || (errno == ENETUNREACH) || (errno == ETIMEDOUT))
{
socketStatus = status_Disconnected;
return -1;
}
else
{
socketStatus = status_Error; // signal a real error.
return -1; //
}
}
else if(nReturn == 0)
{
mReceivedRelease = true;
if(mSentRelease)
{
socketStatus = status_Disconnected;
return -1;
}
else
return 0;
}
else
{
//printf("%s\n",(char*)outBuf);
return nReturn;
}
}
/* Write as much data to the socket as you want and is possible. Positive
numbers indicate a write, zero indicates that no data was written due to
flow control. -1 indicates that an error occured, either a socket closure
or otherwise. */
long PCSBSocket::WriteData(const void* inBuf,long inLength)
{
int nReturn;
//fprintf(hexdump,"\n");
//DumpHex(hexdump,(char*)inBuf,inLength);
//fflush(hexdump);
nReturn = send(mLinSocket,(char*)inBuf,inLength,NULL);
if(nReturn == -1)
{
if(errno == EWOULDBLOCK)
{
return 0; // ** Signal no data
}
else if((errno == ECONNABORTED) || (errno == ECONNREFUSED) || (errno == ECONNRESET) || (errno == EHOSTUNREACH) || (errno == ENETDOWN) || (errno == ENETRESET) || (errno == ENETUNREACH) || (errno == ETIMEDOUT))
{
socketStatus = status_Disconnected;
return -1;
}
else
{
socketStatus = status_Error; // signal a real error.
return -1;
}
}
else
return nReturn;
}
/* Disconnect the socket now. This will be a disorderly disconnect */
void PCSBSocket::Disconnect(void)
{
shutdown(mLinSocket,SHUT_RDWR);
socketStatus = status_Disconnected;
}
/* Indicate that you have no more data to send. The socket will be
* closed after the other side finishes sending data. You can still
* call disconnect to force the issue. */
void PCSBSocket::Release(void)
{
if(shutdown(mLinSocket,SHUT_WR)==-1)
socketStatus = status_Error;
mSentRelease = true;
}
/* Get the local IP address of the machine and port we are bound to.
* Also return the remote side port and IP, or 0 if we're not connected.
* Pass NULL for anything you don't want. */
void PCSBSocket::GetAddresses(ConnectionData* dataStruct)
{
int nReturn;
struct sockaddr_in tempStruct;
socklen_t sizeStruct = sizeof(tempStruct);
/***********************Remote Addresses****************************/
nReturn = getpeername(mLinSocket,(struct sockaddr*)&tempStruct,&sizeStruct);
dataStruct->remoteIP = ntohl(tempStruct.sin_addr.s_addr);
dataStruct->remotePort = ntohs(tempStruct.sin_port);
/***********************Local Addresses****************************/
nReturn = getsockname(mLinSocket,(struct sockaddr*)&tempStruct,&sizeStruct);
dataStruct->localIP = ntohl(tempStruct.sin_addr.s_addr);
dataStruct->localPort = ntohs(tempStruct.sin_port);
}
/**********************************************************************
* DNS Access
**********************************************************************/
/* Looks up an IP address (dot-number form or DNS form. */
unsigned long PCSBSocket::LookupAddress(const char* inAddress)
{
struct hostent* host;
struct in_addr *ipAddress;
unsigned long ip;
//try it as an IP first
ip = inet_addr(inAddress);
if (ip != INADDR_NONE)
return ntohl(ip);
host = gethostbyname(inAddress);
if(host == NULL)
return 0;
ipAddress = (struct in_addr *)(host->h_addr);
if (!ipAddress)
return 0;
return ntohl(ipAddress->s_addr);
}