2007-01-02 17:34:03 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD 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.
|
|
|
|
* OpenTTD 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 OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2007-02-23 08:37:33 +00:00
|
|
|
/**
|
|
|
|
* @file tcp.cpp Basic functions to receive and send TCP packets.
|
|
|
|
*/
|
|
|
|
|
2007-01-02 17:34:03 +00:00
|
|
|
#include "../../stdafx.h"
|
|
|
|
#include "../../debug.h"
|
|
|
|
|
|
|
|
#include "tcp.h"
|
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "../../safeguards.h"
|
|
|
|
|
2011-05-01 13:29:40 +00:00
|
|
|
/**
|
|
|
|
* Construct a socket handler for a TCP connection.
|
|
|
|
* @param s The just opened TCP connection.
|
|
|
|
*/
|
2009-01-14 12:50:13 +00:00
|
|
|
NetworkTCPSocketHandler::NetworkTCPSocketHandler(SOCKET s) :
|
2009-04-07 20:27:13 +00:00
|
|
|
NetworkSocketHandler(),
|
2019-04-10 21:07:06 +00:00
|
|
|
packet_queue(nullptr), packet_recv(nullptr),
|
2009-04-07 20:27:13 +00:00
|
|
|
sock(s), writable(false)
|
2008-12-23 20:52:27 +00:00
|
|
|
{
|
2007-01-12 20:19:49 +00:00
|
|
|
}
|
|
|
|
|
2009-01-14 12:50:13 +00:00
|
|
|
NetworkTCPSocketHandler::~NetworkTCPSocketHandler()
|
2007-02-01 23:50:15 +00:00
|
|
|
{
|
2009-01-22 10:09:56 +00:00
|
|
|
this->CloseConnection();
|
|
|
|
|
2008-12-23 20:52:27 +00:00
|
|
|
if (this->sock != INVALID_SOCKET) closesocket(this->sock);
|
2009-01-22 10:09:56 +00:00
|
|
|
this->sock = INVALID_SOCKET;
|
|
|
|
}
|
|
|
|
|
2009-06-19 20:26:18 +00:00
|
|
|
NetworkRecvStatus NetworkTCPSocketHandler::CloseConnection(bool error)
|
2009-01-22 10:09:56 +00:00
|
|
|
{
|
2007-02-01 23:50:15 +00:00
|
|
|
this->writable = false;
|
2009-06-19 20:26:18 +00:00
|
|
|
NetworkSocketHandler::CloseConnection(error);
|
2007-02-01 23:50:15 +00:00
|
|
|
|
|
|
|
/* Free all pending and partially received packets */
|
2019-04-10 21:07:06 +00:00
|
|
|
while (this->packet_queue != nullptr) {
|
2007-02-01 23:50:15 +00:00
|
|
|
Packet *p = this->packet_queue->next;
|
|
|
|
delete this->packet_queue;
|
|
|
|
this->packet_queue = p;
|
|
|
|
}
|
|
|
|
delete this->packet_recv;
|
2019-04-10 21:07:06 +00:00
|
|
|
this->packet_recv = nullptr;
|
2007-02-01 23:50:15 +00:00
|
|
|
|
2009-01-22 10:09:56 +00:00
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
2007-01-02 17:34:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function puts the packet in the send-queue and it is send as
|
|
|
|
* soon as possible. This is the next tick, or maybe one tick later
|
|
|
|
* if the OS-network-buffer is full)
|
|
|
|
* @param packet the packet to send
|
|
|
|
*/
|
2010-11-30 13:38:46 +00:00
|
|
|
void NetworkTCPSocketHandler::SendPacket(Packet *packet)
|
2007-01-02 17:34:03 +00:00
|
|
|
{
|
|
|
|
Packet *p;
|
2019-04-10 21:07:06 +00:00
|
|
|
assert(packet != nullptr);
|
2007-01-02 17:34:03 +00:00
|
|
|
|
2007-02-01 22:30:35 +00:00
|
|
|
packet->PrepareToSend();
|
2007-01-02 17:34:03 +00:00
|
|
|
|
2010-11-30 13:22:29 +00:00
|
|
|
/* Reallocate the packet as in 99+% of the times we send at most 25 bytes and
|
|
|
|
* keeping the other 1400+ bytes wastes memory, especially when someone tries
|
|
|
|
* to do a denial of service attack! */
|
|
|
|
packet->buffer = ReallocT(packet->buffer, packet->size);
|
|
|
|
|
2007-01-02 17:34:03 +00:00
|
|
|
/* Locate last packet buffered for the client */
|
2007-02-01 23:50:15 +00:00
|
|
|
p = this->packet_queue;
|
2019-04-10 21:07:06 +00:00
|
|
|
if (p == nullptr) {
|
2007-01-02 17:34:03 +00:00
|
|
|
/* No packets yet */
|
2007-02-01 23:50:15 +00:00
|
|
|
this->packet_queue = packet;
|
2007-01-02 17:34:03 +00:00
|
|
|
} else {
|
|
|
|
/* Skip to the last packet */
|
2019-04-10 21:07:06 +00:00
|
|
|
while (p->next != nullptr) p = p->next;
|
2007-01-02 17:34:03 +00:00
|
|
|
p->next = packet;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends all the buffered packets out for this client. It stops when:
|
|
|
|
* 1) all packets are send (queue is empty)
|
|
|
|
* 2) the OS reports back that it can not send any more
|
|
|
|
* data right now (full network-buffer, it happens ;))
|
|
|
|
* 3) sending took too long
|
2010-01-14 21:48:42 +00:00
|
|
|
* @param closing_down Whether we are closing down the connection.
|
2010-11-14 12:05:24 +00:00
|
|
|
* @return \c true if a (part of a) packet could be sent and
|
|
|
|
* the connection is not closed yet.
|
2007-01-02 17:34:03 +00:00
|
|
|
*/
|
2011-02-12 21:09:34 +00:00
|
|
|
SendPacketsState NetworkTCPSocketHandler::SendPackets(bool closing_down)
|
2007-01-02 17:34:03 +00:00
|
|
|
{
|
|
|
|
ssize_t res;
|
|
|
|
Packet *p;
|
|
|
|
|
|
|
|
/* We can not write to this socket!! */
|
2011-02-12 21:09:34 +00:00
|
|
|
if (!this->writable) return SPS_NONE_SENT;
|
|
|
|
if (!this->IsConnected()) return SPS_CLOSED;
|
2007-01-02 17:34:03 +00:00
|
|
|
|
2007-02-01 23:50:15 +00:00
|
|
|
p = this->packet_queue;
|
2019-04-10 21:07:06 +00:00
|
|
|
while (p != nullptr) {
|
2007-02-01 23:50:15 +00:00
|
|
|
res = send(this->sock, (const char*)p->buffer + p->pos, p->size - p->pos, 0);
|
2007-01-02 17:34:03 +00:00
|
|
|
if (res == -1) {
|
|
|
|
int err = GET_LAST_ERROR();
|
|
|
|
if (err != EWOULDBLOCK) {
|
|
|
|
/* Something went wrong.. close client! */
|
2010-01-14 21:48:42 +00:00
|
|
|
if (!closing_down) {
|
|
|
|
DEBUG(net, 0, "send failed with error %d", err);
|
|
|
|
this->CloseConnection();
|
|
|
|
}
|
2011-02-12 21:09:34 +00:00
|
|
|
return SPS_CLOSED;
|
2007-01-02 17:34:03 +00:00
|
|
|
}
|
2011-02-12 21:09:34 +00:00
|
|
|
return SPS_PARTLY_SENT;
|
2007-01-02 17:34:03 +00:00
|
|
|
}
|
|
|
|
if (res == 0) {
|
|
|
|
/* Client/server has left us :( */
|
2010-01-14 21:48:42 +00:00
|
|
|
if (!closing_down) this->CloseConnection();
|
2011-02-12 21:09:34 +00:00
|
|
|
return SPS_CLOSED;
|
2007-01-02 17:34:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p->pos += res;
|
|
|
|
|
|
|
|
/* Is this packet sent? */
|
|
|
|
if (p->pos == p->size) {
|
|
|
|
/* Go to the next packet */
|
2007-02-01 23:50:15 +00:00
|
|
|
this->packet_queue = p->next;
|
2007-02-01 22:30:35 +00:00
|
|
|
delete p;
|
2007-02-01 23:50:15 +00:00
|
|
|
p = this->packet_queue;
|
2007-01-02 17:34:03 +00:00
|
|
|
} else {
|
2011-02-12 21:09:34 +00:00
|
|
|
return SPS_PARTLY_SENT;
|
2007-01-02 17:34:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-12 21:09:34 +00:00
|
|
|
return SPS_ALL_SENT;
|
2007-01-02 17:34:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Receives a packet for the given client
|
2019-04-10 21:07:06 +00:00
|
|
|
* @return The received packet (or nullptr when it didn't receive one)
|
2007-01-02 17:34:03 +00:00
|
|
|
*/
|
2010-11-30 13:38:46 +00:00
|
|
|
Packet *NetworkTCPSocketHandler::ReceivePacket()
|
2007-01-02 17:34:03 +00:00
|
|
|
{
|
|
|
|
ssize_t res;
|
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
if (!this->IsConnected()) return nullptr;
|
2007-01-02 17:34:03 +00:00
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
if (this->packet_recv == nullptr) {
|
2007-02-01 23:50:15 +00:00
|
|
|
this->packet_recv = new Packet(this);
|
2007-01-02 17:34:03 +00:00
|
|
|
}
|
|
|
|
|
2009-10-09 11:03:00 +00:00
|
|
|
Packet *p = this->packet_recv;
|
2007-01-02 17:34:03 +00:00
|
|
|
|
|
|
|
/* Read packet size */
|
|
|
|
if (p->pos < sizeof(PacketSize)) {
|
|
|
|
while (p->pos < sizeof(PacketSize)) {
|
2007-01-12 20:19:49 +00:00
|
|
|
/* Read the size of the packet */
|
2007-02-01 23:50:15 +00:00
|
|
|
res = recv(this->sock, (char*)p->buffer + p->pos, sizeof(PacketSize) - p->pos, 0);
|
2007-01-02 17:34:03 +00:00
|
|
|
if (res == -1) {
|
|
|
|
int err = GET_LAST_ERROR();
|
|
|
|
if (err != EWOULDBLOCK) {
|
|
|
|
/* Something went wrong... (104 is connection reset by peer) */
|
|
|
|
if (err != 104) DEBUG(net, 0, "recv failed with error %d", err);
|
2009-10-07 20:58:14 +00:00
|
|
|
this->CloseConnection();
|
2019-04-10 21:07:06 +00:00
|
|
|
return nullptr;
|
2007-01-02 17:34:03 +00:00
|
|
|
}
|
|
|
|
/* Connection would block, so stop for now */
|
2019-04-10 21:07:06 +00:00
|
|
|
return nullptr;
|
2007-01-02 17:34:03 +00:00
|
|
|
}
|
|
|
|
if (res == 0) {
|
|
|
|
/* Client/server has left */
|
2009-10-07 20:58:14 +00:00
|
|
|
this->CloseConnection();
|
2019-04-10 21:07:06 +00:00
|
|
|
return nullptr;
|
2007-01-02 17:34:03 +00:00
|
|
|
}
|
|
|
|
p->pos += res;
|
|
|
|
}
|
|
|
|
|
2007-02-01 22:30:35 +00:00
|
|
|
/* Read the packet size from the received packet */
|
|
|
|
p->ReadRawPacketSize();
|
2007-01-02 17:34:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Read rest of packet */
|
|
|
|
while (p->pos < p->size) {
|
2007-02-01 23:50:15 +00:00
|
|
|
res = recv(this->sock, (char*)p->buffer + p->pos, p->size - p->pos, 0);
|
2007-01-02 17:34:03 +00:00
|
|
|
if (res == -1) {
|
|
|
|
int err = GET_LAST_ERROR();
|
|
|
|
if (err != EWOULDBLOCK) {
|
|
|
|
/* Something went wrong... (104 is connection reset by peer) */
|
|
|
|
if (err != 104) DEBUG(net, 0, "recv failed with error %d", err);
|
2009-10-07 20:58:14 +00:00
|
|
|
this->CloseConnection();
|
2019-04-10 21:07:06 +00:00
|
|
|
return nullptr;
|
2007-01-02 17:34:03 +00:00
|
|
|
}
|
|
|
|
/* Connection would block */
|
2019-04-10 21:07:06 +00:00
|
|
|
return nullptr;
|
2007-01-02 17:34:03 +00:00
|
|
|
}
|
|
|
|
if (res == 0) {
|
|
|
|
/* Client/server has left */
|
2009-10-07 20:58:14 +00:00
|
|
|
this->CloseConnection();
|
2019-04-10 21:07:06 +00:00
|
|
|
return nullptr;
|
2007-01-02 17:34:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p->pos += res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Prepare for receiving a new packet */
|
2019-04-10 21:07:06 +00:00
|
|
|
this->packet_recv = nullptr;
|
2007-01-02 17:34:03 +00:00
|
|
|
|
2007-02-01 22:30:35 +00:00
|
|
|
p->PrepareToRead();
|
2007-01-02 17:34:03 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2010-10-15 19:33:08 +00:00
|
|
|
/**
|
|
|
|
* Check whether this socket can send or receive something.
|
|
|
|
* @return \c true when there is something to receive.
|
2011-05-01 13:29:40 +00:00
|
|
|
* @note Sets #writable if more data can be sent.
|
2010-10-15 19:33:08 +00:00
|
|
|
*/
|
|
|
|
bool NetworkTCPSocketHandler::CanSendReceive()
|
|
|
|
{
|
|
|
|
fd_set read_fd, write_fd;
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
FD_ZERO(&read_fd);
|
|
|
|
FD_ZERO(&write_fd);
|
|
|
|
|
|
|
|
FD_SET(this->sock, &read_fd);
|
|
|
|
FD_SET(this->sock, &write_fd);
|
|
|
|
|
|
|
|
tv.tv_sec = tv.tv_usec = 0; // don't block at all.
|
2019-04-10 21:07:06 +00:00
|
|
|
if (select(FD_SETSIZE, &read_fd, &write_fd, nullptr, &tv) < 0) return false;
|
2010-10-15 19:33:08 +00:00
|
|
|
|
|
|
|
this->writable = !!FD_ISSET(this->sock, &write_fd);
|
2010-10-15 22:08:57 +00:00
|
|
|
return FD_ISSET(this->sock, &read_fd) != 0;
|
2010-10-15 19:33:08 +00:00
|
|
|
}
|