[go: up one dir, main page]

Menu

[r7]: / trunk / socket.cpp  Maximize  Restore  History

Download this file

291 lines (210 with data), 6.3 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
/*
EHS is a library for adding web server support to a C++ application
Copyright (C) 2001, 2002 Zac Hansen
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; version 2
of the License only.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
or see http://www.gnu.org/licenses/gpl.html
Zac Hansen ( xaxxon@slackworks.com )
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
/*
* Solaris 2.6 troubles
*/
#ifdef sun
#ifndef BSD_COMP
#define BSD_COMP 1
#endif
#ifndef _XPG4_2
#define _XPG4_2 1
#endif
#ifndef __EXTENSIONS__
#define __EXTENSIONS__ 1
#endif
#ifndef __PRAGMA_REDEFINE_EXTNAME
#define __PRAGMA_REDEFINE_EXTNAME 1
#endif
#endif // sun
#include "socket.h"
#ifndef _WIN32
#include <assert.h>
#include <stdio.h>
#include <sys/ioctl.h>
#endif
#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0 // no support
#endif // MSG_NOSIGNAL
#include <sstream>
using namespace std;
Socket::Socket ( int inAcceptSocket,
sockaddr_in * ipoInternetSocketAddress )
{
nAcceptSocket = inAcceptSocket;
memcpy ( &oInternetSocketAddress,
ipoInternetSocketAddress,
sizeof ( oInternetSocketAddress ) );
}
Socket::~Socket ( )
{
}
NetworkAbstraction::InitResult
Socket::Init ( int inPort ///< port on which to listen
)
{
#ifdef _WIN32
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
// Found a usable winsock dll?
assert( err == 0 );
/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we */
/* requested. */
if (
LOBYTE( wsaData.wVersion ) != 2
|| HIBYTE( wsaData.wVersion ) != 2
) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
WSACleanup( );
#ifdef EHS_DEBUG
cerr << "[EHS_DEBUG] Critical Error: Couldn't find useable Winsock DLL. Must be at least 2.2. Aborting" << endl;
#endif
assert( false );
}
/* The WinSock DLL is acceptable. Proceed. */
#endif // End WIN32-specific network initialization code
// need to create a socket for listening for new connections
#ifdef EHS_DEBUG
if ( nAcceptSocket != 0 ) {
cerr << "[EHS_DEBUG] nAcceptSocket = " << nAcceptSocket << endl;
assert ( nAcceptSocket == 0 );
}
#endif
nAcceptSocket = socket ( AF_INET, SOCK_STREAM, 0 );
if ( nAcceptSocket == -1 ) {
#ifdef EHS_DEBUG
cerr << "[EHS_DEBUG] Error: socket() failed" << endl;
#endif
return INITSOCKET_SOCKETFAILED;
}
#ifdef _WIN32
u_long MyTrueVar = 1;
ioctlsocket ( nAcceptSocket, FIONBIO, &MyTrueVar );
#else
int MyTrueVar = 1;
ioctl ( nAcceptSocket, FIONBIO, &MyTrueVar );
MyTrueVar = 1; // not sure if it was changed in ioctl, so re-set it
setsockopt ( nAcceptSocket, SOL_SOCKET, SO_REUSEADDR, (const void *) &MyTrueVar, sizeof ( int ) );
#endif
// bind the socket to the appropriate port
struct sockaddr_in oSocketInfo;
memset ( &oSocketInfo, 0, sizeof ( oSocketInfo ) );
int nResult = -1;
// IP address is 32 bits, int is 32 bits..
// binds to all interfaces. This maybe should be customizable
int nSinAddr = INADDR_ANY;
oSocketInfo.sin_family = AF_INET;
memcpy ( & ( oSocketInfo.sin_addr ), &nSinAddr, sizeof ( oSocketInfo.sin_addr ) );
oSocketInfo.sin_port = htons ( inPort );
nResult = bind ( nAcceptSocket, (sockaddr *)&oSocketInfo, sizeof ( sockaddr_in ) );
if ( nResult == -1 ) {
#ifdef EHS_DEBUG
cerr << "[EHS_DEBUG] Error: bind() failed" << endl;
#endif
return INITSOCKET_BINDFAILED;
}
// listen
nResult = listen ( nAcceptSocket, 20 );
if ( nResult != 0 ) {
#ifdef EHS_DEBUG
cerr << "[EHS_DEBUG] Error: listen() failed" << endl;
#endif
return INITSOCKET_LISTENFAILED;
}
return INITSOCKET_SUCCESS;
}
int Socket::Read ( void * ipBuffer, int ipBufferLength )
{
//return read ( nAcceptSocket, ipBuffer, ipBufferLength );
return recv ( nAcceptSocket,
#ifdef _WIN32
(char *) ipBuffer,
#else
ipBuffer,
#endif
ipBufferLength, 0 );
}
int Socket::Send ( const void * ipMessage, size_t inLength, int inFlags )
{
return send ( nAcceptSocket,
#ifdef _WIN32
(const char *) ipMessage,
#else
ipMessage,
#endif
inLength, inFlags | MSG_NOSIGNAL );
}
void Socket::Close ( )
{
#ifdef _WIN32
closesocket ( nAcceptSocket );
#else
close ( nAcceptSocket );
#endif
}
NetworkAbstraction * Socket::Accept ( )
{
size_t oInternetSocketAddressLength = sizeof ( oInternetSocketAddress );
int nNewFd = accept ( nAcceptSocket,
(sockaddr *) &oInternetSocketAddress,
#ifdef _WIN32
(int *) &oInternetSocketAddressLength
#else
&oInternetSocketAddressLength
#endif
);
#ifdef EHS_DEBUG
cerr
<< "Got a connection from " << GetAddress() << ":"
<< ntohs(oInternetSocketAddress.sin_port) << endl;
#endif
if ( nNewFd == -1 ) {
return NULL;
}
Socket * poSocket = new Socket ( nNewFd, &oInternetSocketAddress );
#ifdef EHS_DEBUG
cerr
<< "[EHS_DEBUG] Allocated new socket object with fd="
<< nNewFd << " and socket @0x" << hex << poSocket << endl;
#endif
return poSocket;
}
string Socket::GetAddress ( )
{
ostringstream oss;
oss
<< ((ntohl(oInternetSocketAddress.sin_addr.s_addr) >> 24 ) & 0xff) << "."
<< ((ntohl(oInternetSocketAddress.sin_addr.s_addr) >> 16 ) & 0xff) << "."
<< ((ntohl(oInternetSocketAddress.sin_addr.s_addr) >> 8 ) & 0xff) << "."
<< ((ntohl(oInternetSocketAddress.sin_addr.s_addr) >> 0 ) & 0xff) << endl;
return oss.str();
}
int Socket::GetPort ( )
{
return ntohs ( oInternetSocketAddress.sin_port );
}