[go: up one dir, main page]

Menu

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

Download this file

361 lines (320 with data), 8.7 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
/* $Id$
*
* EHS is a library for embedding HTTP(S) support into a C++ application
*
* Copyright (C) 2004 Zachary J. Hansen
*
* Code cleanup, new features and bugfixes: Copyright (C) 2010 Fritz Elfert
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation;
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* This can be found in the 'COPYING' file.
*
*/
#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 "ehs.h"
#include "socket.h"
#include "debug.h"
#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0 // no support
#endif // MSG_NOSIGNAL
#include <iostream>
#include <sstream>
#include <cstring>
#include <cerrno>
#include <cstdio>
#include <stdexcept>
#ifdef _WIN32
const char *net_strerror() {
static char ret[1024];
int err = net_errno;
if (!FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_FROM_HMODULE,
GetModuleHandleA("wsock32.dll"), err, 0, ret, sizeof(ret), NULL)) {
snprintf(ret, sizeof(ret), "Unknown error %d (0x%08x)", err, err);
}
return ret;
}
#else
const char *net_strerror() {
return strerror(net_errno);
}
#endif
using namespace std;
Socket::Socket()
: m_fd(-1),
m_peer(sockaddr_in()),
m_bindaddr(sockaddr_in()),
m_pBindHelper(NULL)
{
memset(&m_peer, 0, sizeof(m_peer));
memset(&m_bindaddr, 0, sizeof(m_bindaddr));
}
Socket::Socket(ehs_socket_t fd, sockaddr_in * peer) :
m_fd(fd),
m_peer(*peer),
m_bindaddr(sockaddr_in()),
m_pBindHelper(NULL)
{
memcpy(&m_peer, peer, sizeof(m_peer));
memset(&m_bindaddr, 0, sizeof(m_bindaddr));
socklen_t len = sizeof(m_bindaddr);
getsockname(fd, reinterpret_cast<struct sockaddr *>(&m_bindaddr),
#ifdef _WIN32
reinterpret_cast<int *>(&len)
#else
&len
#endif
);
}
Socket::~Socket()
{
Close();
}
void Socket::RegisterBindHelper(PrivilegedBindHelper *helper)
{
m_pBindHelper = helper;
}
void Socket::SetBindAddress(const char *bindAddress)
{
in_addr_t baddr =
(bindAddress && strlen(bindAddress)) ? inet_addr(bindAddress) : INADDR_ANY;
memcpy(&(m_bindaddr.sin_addr), &baddr, sizeof(m_bindaddr.sin_addr));
}
void Socket::Init(int port)
{
string sError;
#ifdef _WIN32
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD(2, 2);
// Found a usable winsock dll?
if (0 != WSAStartup(wVersionRequested, &wsaData)) {
throw runtime_error("Socket::Init: WSAStartup() returned an error");
}
/* 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 (2 != LOBYTE(wsaData.wVersion) || 2 != HIBYTE(wsaData.wVersion)) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
WSACleanup();
throw runtime_error("Socket::Init: Could not find a suitable (v 2.2) Winsock DLL");
}
/* The WinSock DLL is acceptable. Proceed. */
#endif // End WIN32-specific network initialization code
// need to create a socket for listening for new connections
if (INVALID_SOCKET != m_fd) {
#ifdef _WIN32
WSACleanup();
#endif
throw runtime_error("Socket::Init: Socket already initialized");
}
m_fd = socket(AF_INET, SOCK_STREAM, 0);
if (INVALID_SOCKET == m_fd) {
sError.assign("socket: ").append(net_strerror());
#ifdef _WIN32
WSACleanup();
#endif
throw runtime_error(sError);
}
#ifdef _WIN32
u_long one = 1;
ioctlsocket(m_fd, FIONBIO, &one);
setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<char *>(&one), sizeof(int));
#else
int one = 1;
ioctl(m_fd, FIONBIO, &one);
setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<const void *>(&one), sizeof(int));
#endif
// bind the socket to the appropriate port
struct sockaddr_in sa;
memset(&sa, 0, sizeof(sa));
int nResult = -1;
sa.sin_family = AF_INET;
memcpy(&(sa.sin_addr), &m_bindaddr.sin_addr, sizeof(sa.sin_addr));
sa.sin_port = htons(port);
if ((1024 > port) && m_pBindHelper) {
struct in_addr in;
memcpy(&in, &m_bindaddr.sin_addr, sizeof(in));
string ba(inet_ntoa(in));
nResult = m_pBindHelper->BindPrivilegedPort(m_fd, ba.c_str(), port) ? 0 : -1;
} else {
nResult = ::bind(m_fd, reinterpret_cast<sockaddr *>(&sa), sizeof(sa));
}
if (-1 == nResult) {
sError.assign("bind: ").append(net_strerror());
#ifdef _WIN32
WSACleanup();
#endif
throw runtime_error(sError);
}
// listen
nResult = listen(m_fd, 20);
if (0 != nResult) {
sError.assign("listen: ").append(net_strerror());
#ifdef _WIN32
WSACleanup();
#endif
throw runtime_error(sError);
}
}
int Socket::Read(void *buf, int bufsize)
{
int ret = 0;
if (bufsize > 0) {
again:
ret = recv(m_fd,
#ifdef _WIN32
reinterpret_cast<char *>(buf),
#else
buf,
#endif
bufsize, 0);
if (ret < 0) {
int err = net_errno;
switch (err) {
case EAGAIN:
case EINTR:
#ifdef _WIN32
case WSAEWOULDBLOCK:
#endif
goto again;
break;
#ifdef _WIN32
default:
EHS_TRACE("NET_ERR: %d", err);
break;
#endif
}
}
}
return ret;
}
int Socket::Send(const void *buf, size_t buflen, int flags)
{
int ret = 0;
if (buflen > 0) {
again:
ret = send(m_fd,
#ifdef _WIN32
reinterpret_cast<const char *>(buf),
#else
buf,
#endif
buflen, flags|MSG_NOSIGNAL);
if (ret < 0) {
switch (net_errno) {
case EAGAIN:
case EINTR:
#ifdef _WIN32
case WSAEWOULDBLOCK:
#endif
goto again;
}
}
}
return ret;
}
void Socket::Close()
{
if (INVALID_SOCKET == m_fd)
return;
#ifdef _WIN32
closesocket(m_fd);
#else
close(m_fd);
#endif
}
NetworkAbstraction *Socket::Accept()
{
string sError;
socklen_t addrlen = sizeof(m_peer);
retry:
ehs_socket_t fd = accept(m_fd, reinterpret_cast<sockaddr *>(&m_peer),
#ifdef _WIN32
reinterpret_cast<int *>(&addrlen)
#else
&addrlen
#endif
);
EHS_TRACE("Got a connection from %s:%hu\n",
GetRemoteAddress().c_str(), ntohs(m_peer.sin_port));
if (INVALID_SOCKET == fd) {
switch (net_errno) {
case EAGAIN:
case EINTR:
#ifdef _WIN32
case WSAEWOULDBLOCK:
#endif
goto retry;
break;
}
sError.assign("accept: ").append(net_strerror());
#ifdef _WIN32
WSACleanup();
#endif
throw runtime_error(sError);
return NULL;
}
return new Socket(fd, &m_peer);
}
string Socket::GetPeer() const
{
char buf[20];
string ret(GetRemoteAddress());
snprintf(buf, 20, ":%d", GetRemotePort());
ret.append(buf);
return ret;
}
string Socket::GetRemoteAddress() const
{
struct in_addr in;
memcpy (&in, &m_peer.sin_addr.s_addr, sizeof(in));
return string(inet_ntoa(in));
}
int Socket::GetRemotePort() const
{
return ntohs(m_peer.sin_port);
}
string Socket::GetLocalAddress() const
{
struct in_addr in;
memcpy (&in, &m_bindaddr.sin_addr.s_addr, sizeof(in));
return string(inet_ntoa(in));
}
int Socket::GetLocalPort() const
{
return ntohs(m_bindaddr.sin_port);
}