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
|
/*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* TCPSocket.cpp
* Implementation of the TCP Socket classes
* Copyright (C) 2005 Simon Newton
*/
#if HAVE_CONFIG_H
#include <config.h>
#endif // HAVE_CONFIG_H
#include <errno.h>
#include <fcntl.h>
/* FreeBSD needs types.h before tcp.h */
#include <sys/types.h>
#ifndef _WIN32
#include <netinet/tcp.h>
#endif // !_WIN32
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#ifdef _WIN32
#include <ola/win/CleanWinSock2.h>
#include <Ws2tcpip.h>
#include <winioctl.h>
#else
#include <sys/ioctl.h>
#endif // _WIN32
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif // HAVE_ARPA_INET_H
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h> // Required by FreeBSD
#endif // HAVE_NETINET_IN_H
#include <string>
#include "common/network/SocketHelper.h"
#include "ola/Logging.h"
#include "ola/io/Descriptor.h"
#include "ola/network/NetworkUtils.h"
#include "ola/network/Socket.h"
#include "ola/network/SocketCloser.h"
#include "ola/network/TCPSocketFactory.h"
namespace ola {
namespace network {
// TCPSocket
// ------------------------------------------------
/**
* Get the remote IPAddress and port for this socket
*/
GenericSocketAddress TCPSocket::GetPeerAddress() const {
#ifdef _WIN32
return ola::network::GetPeerAddress(m_handle.m_handle.m_fd);
#else
return ola::network::GetPeerAddress(m_handle);
#endif // _WIN32
}
GenericSocketAddress TCPSocket::GetLocalAddress() const {
#ifdef _WIN32
return ola::network::GetLocalAddress(m_handle.m_handle.m_fd);
#else
return ola::network::GetLocalAddress(m_handle);
#endif // _WIN32
}
TCPSocket::TCPSocket(int sd) {
#ifdef _WIN32
m_handle.m_handle.m_fd = sd;
m_handle.m_type = ola::io::SOCKET_DESCRIPTOR;
#else
m_handle = sd;
#endif // _WIN32
SetNoSigPipe(m_handle);
}
/*
* Close this TCPSocket
*/
bool TCPSocket::Close() {
if (m_handle != ola::io::INVALID_DESCRIPTOR) {
#ifdef _WIN32
closesocket(m_handle.m_handle.m_fd);
#else
close(m_handle);
#endif // _WIN32
m_handle = ola::io::INVALID_DESCRIPTOR;
}
return true;
}
/*
* Set the TCP_NODELAY option
*/
bool TCPSocket::SetNoDelay() {
int flag = 1;
#ifdef _WIN32
int sd = m_handle.m_handle.m_fd;
#else
int sd = m_handle;
#endif // _WIN32
int result = setsockopt(sd, IPPROTO_TCP, TCP_NODELAY,
reinterpret_cast<char*>(&flag),
sizeof(flag));
if (result < 0) {
OLA_WARN << "Can't set TCP_NODELAY for " << sd << ", "
<< strerror(errno);
return false;
}
return true;
}
/*
* Connect
* @param ip_address the IP to connect to
* @param port the port to connect to
* @param blocking whether to block on connect or not
*/
TCPSocket* TCPSocket::Connect(const SocketAddress &endpoint) {
struct sockaddr server_address;
if (!endpoint.ToSockAddr(&server_address, sizeof(server_address)))
return NULL;
int sd = socket(endpoint.Family(), SOCK_STREAM, 0);
if (sd < 0) {
OLA_WARN << "socket() failed, " << strerror(errno);
return NULL;
}
SocketCloser closer(sd);
int r = connect(sd, &server_address, sizeof(server_address));
if (r) {
OLA_WARN << "connect(" << endpoint << "): " << strerror(errno);
return NULL;
}
TCPSocket *socket = new TCPSocket(closer.Release());
socket->SetReadNonBlocking();
return socket;
}
// TCPAcceptingSocket
// ------------------------------------------------
/*
* Create a new TCPListeningSocket
*/
TCPAcceptingSocket::TCPAcceptingSocket(TCPSocketFactoryInterface *factory)
: ReadFileDescriptor(),
m_handle(ola::io::INVALID_DESCRIPTOR),
m_factory(factory) {
}
/**
* Clean up
*/
TCPAcceptingSocket::~TCPAcceptingSocket() {
Close();
}
/*
* Start listening
* @param endpoint the SocketAddress to listen on
* @param backlog the backlog
* @return true if it succeeded, false otherwise
*/
bool TCPAcceptingSocket::Listen(const SocketAddress &endpoint, int backlog) {
struct sockaddr server_address;
int reuse_flag = 1;
if (m_handle != ola::io::INVALID_DESCRIPTOR)
return false;
if (!endpoint.ToSockAddr(&server_address, sizeof(server_address)))
return false;
int sd = socket(endpoint.Family(), SOCK_STREAM, 0);
if (sd < 0) {
OLA_WARN << "socket() failed: " << strerror(errno);
return false;
}
SocketCloser closer(sd);
#ifdef _WIN32
ola::io::DescriptorHandle temp_handle;
temp_handle.m_handle.m_fd = sd;
temp_handle.m_type = ola::io::SOCKET_DESCRIPTOR;
if (!ola::io::ConnectedDescriptor::SetNonBlocking(temp_handle)) {
#else
if (!ola::io::ConnectedDescriptor::SetNonBlocking(sd)) {
#endif // _WIN32
OLA_WARN << "Failed to mark TCP accept socket as non-blocking";
return false;
}
int ok = setsockopt(sd,
SOL_SOCKET,
SO_REUSEADDR,
reinterpret_cast<char*>(&reuse_flag),
sizeof(reuse_flag));
if (ok < 0) {
OLA_WARN << "can't set reuse for " << sd << ", " << strerror(errno);
return false;
}
if (bind(sd, &server_address, sizeof(server_address)) == -1) {
OLA_WARN << "bind to " << endpoint << " failed, " << strerror(errno);
return false;
}
if (listen(sd, backlog)) {
OLA_WARN << "listen on " << endpoint << " failed, " << strerror(errno);
return false;
}
#ifdef _WIN32
m_handle.m_handle.m_fd = closer.Release();
m_handle.m_type = ola::io::SOCKET_DESCRIPTOR;
#else
m_handle = closer.Release();
#endif // _WIN32
return true;
}
/*
* Stop listening & close this socket
* @return true if close succeeded, false otherwise
*/
bool TCPAcceptingSocket::Close() {
bool ret = true;
if (m_handle != ola::io::INVALID_DESCRIPTOR) {
#ifdef _WIN32
if (closesocket(m_handle.m_handle.m_fd)) {
#else
if (close(m_handle)) {
#endif // _WIN32
OLA_WARN << "close() failed " << strerror(errno);
ret = false;
}
}
m_handle = ola::io::INVALID_DESCRIPTOR;
return ret;
}
/*
* Accept new connections
* @return a new connected socket
*/
void TCPAcceptingSocket::PerformRead() {
if (m_handle == ola::io::INVALID_DESCRIPTOR)
return;
while (1) {
struct sockaddr_in cli_address;
socklen_t length = sizeof(cli_address);
#ifdef _WIN32
int sd = accept(m_handle.m_handle.m_fd, (struct sockaddr*) &cli_address,
&length);
#else
int sd = accept(m_handle, (struct sockaddr*) &cli_address, &length);
#endif // _WIN32
if (sd < 0) {
#ifdef _WIN32
if (WSAGetLastError() == WSAEWOULDBLOCK) {
#else
if (errno == EWOULDBLOCK) {
#endif // _WIN32
return;
}
OLA_WARN << "accept() failed, " << strerror(errno);
return;
}
if (m_factory) {
// The callback takes ownership of the new socket descriptor
// coverity[RESOURCE_LEAK]
m_factory->NewTCPSocket(sd);
} else {
OLA_WARN << "Accepted new TCP Connection but no factory registered";
#ifdef _WIN32
closesocket(sd);
#else
close(sd);
#endif // _WIN32
}
}
}
/**
* Get the local IPAddress and port for this socket
*/
GenericSocketAddress TCPAcceptingSocket::GetLocalAddress() const {
#ifdef _WIN32
return ola::network::GetLocalAddress(m_handle.m_handle.m_fd);
#else
return ola::network::GetLocalAddress(m_handle);
#endif // _WIN32
}
} // namespace network
} // namespace ola
|