OpenTTD Source  1.11.0-beta2
tcp.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
12 #include "../../stdafx.h"
13 #include "../../debug.h"
14 
15 #include "tcp.h"
16 
17 #include "../../safeguards.h"
18 
25  packet_queue(nullptr), packet_recv(nullptr),
26  sock(s), writable(false)
27 {
28 }
29 
30 NetworkTCPSocketHandler::~NetworkTCPSocketHandler()
31 {
32  this->CloseConnection();
33 
34  if (this->sock != INVALID_SOCKET) closesocket(this->sock);
35  this->sock = INVALID_SOCKET;
36 }
37 
39 {
40  this->writable = false;
42 
43  /* Free all pending and partially received packets */
44  while (this->packet_queue != nullptr) {
45  Packet *p = this->packet_queue->next;
46  delete this->packet_queue;
47  this->packet_queue = p;
48  }
49  delete this->packet_recv;
50  this->packet_recv = nullptr;
51 
53 }
54 
62 {
63  Packet *p;
64  assert(packet != nullptr);
65 
66  packet->PrepareToSend();
67 
68  /* Reallocate the packet as in 99+% of the times we send at most 25 bytes and
69  * keeping the other 1400+ bytes wastes memory, especially when someone tries
70  * to do a denial of service attack! */
71  packet->buffer = ReallocT(packet->buffer, packet->size);
72 
73  /* Locate last packet buffered for the client */
74  p = this->packet_queue;
75  if (p == nullptr) {
76  /* No packets yet */
77  this->packet_queue = packet;
78  } else {
79  /* Skip to the last packet */
80  while (p->next != nullptr) p = p->next;
81  p->next = packet;
82  }
83 }
84 
96 {
97  ssize_t res;
98  Packet *p;
99 
100  /* We can not write to this socket!! */
101  if (!this->writable) return SPS_NONE_SENT;
102  if (!this->IsConnected()) return SPS_CLOSED;
103 
104  p = this->packet_queue;
105  while (p != nullptr) {
106  res = send(this->sock, (const char*)p->buffer + p->pos, p->size - p->pos, 0);
107  if (res == -1) {
108  int err = GET_LAST_ERROR();
109  if (err != EWOULDBLOCK) {
110  /* Something went wrong.. close client! */
111  if (!closing_down) {
112  DEBUG(net, 0, "send failed with error %d", err);
113  this->CloseConnection();
114  }
115  return SPS_CLOSED;
116  }
117  return SPS_PARTLY_SENT;
118  }
119  if (res == 0) {
120  /* Client/server has left us :( */
121  if (!closing_down) this->CloseConnection();
122  return SPS_CLOSED;
123  }
124 
125  p->pos += res;
126 
127  /* Is this packet sent? */
128  if (p->pos == p->size) {
129  /* Go to the next packet */
130  this->packet_queue = p->next;
131  delete p;
132  p = this->packet_queue;
133  } else {
134  return SPS_PARTLY_SENT;
135  }
136  }
137 
138  return SPS_ALL_SENT;
139 }
140 
146 {
147  ssize_t res;
148 
149  if (!this->IsConnected()) return nullptr;
150 
151  if (this->packet_recv == nullptr) {
152  this->packet_recv = new Packet(this);
153  }
154 
155  Packet *p = this->packet_recv;
156 
157  /* Read packet size */
158  if (p->pos < sizeof(PacketSize)) {
159  while (p->pos < sizeof(PacketSize)) {
160  /* Read the size of the packet */
161  res = recv(this->sock, (char*)p->buffer + p->pos, sizeof(PacketSize) - p->pos, 0);
162  if (res == -1) {
163  int err = GET_LAST_ERROR();
164  if (err != EWOULDBLOCK) {
165  /* Something went wrong... (104 is connection reset by peer) */
166  if (err != 104) DEBUG(net, 0, "recv failed with error %d", err);
167  this->CloseConnection();
168  return nullptr;
169  }
170  /* Connection would block, so stop for now */
171  return nullptr;
172  }
173  if (res == 0) {
174  /* Client/server has left */
175  this->CloseConnection();
176  return nullptr;
177  }
178  p->pos += res;
179  }
180 
181  /* Read the packet size from the received packet */
182  p->ReadRawPacketSize();
183 
184  if (p->size > SEND_MTU) {
185  this->CloseConnection();
186  return nullptr;
187  }
188  }
189 
190  /* Read rest of packet */
191  while (p->pos < p->size) {
192  res = recv(this->sock, (char*)p->buffer + p->pos, p->size - p->pos, 0);
193  if (res == -1) {
194  int err = GET_LAST_ERROR();
195  if (err != EWOULDBLOCK) {
196  /* Something went wrong... (104 is connection reset by peer) */
197  if (err != 104) DEBUG(net, 0, "recv failed with error %d", err);
198  this->CloseConnection();
199  return nullptr;
200  }
201  /* Connection would block */
202  return nullptr;
203  }
204  if (res == 0) {
205  /* Client/server has left */
206  this->CloseConnection();
207  return nullptr;
208  }
209 
210  p->pos += res;
211  }
212 
213  /* Prepare for receiving a new packet */
214  this->packet_recv = nullptr;
215 
216  p->PrepareToRead();
217  return p;
218 }
219 
226 {
227  fd_set read_fd, write_fd;
228  struct timeval tv;
229 
230  FD_ZERO(&read_fd);
231  FD_ZERO(&write_fd);
232 
233  FD_SET(this->sock, &read_fd);
234  FD_SET(this->sock, &write_fd);
235 
236  tv.tv_sec = tv.tv_usec = 0; // don't block at all.
237  if (select(FD_SETSIZE, &read_fd, &write_fd, nullptr, &tv) < 0) return false;
238 
239  this->writable = !!FD_ISSET(this->sock, &write_fd);
240  return FD_ISSET(this->sock, &read_fd) != 0;
241 }
Packet::PrepareToSend
void PrepareToSend()
Writes the packet size from the raw packet from packet->size.
Definition: packet.cpp:61
Packet::buffer
byte * buffer
The buffer of this packet, of basically variable length up to SEND_MTU.
Definition: packet.h:52
NetworkSocketHandler
SocketHandler for all network sockets in OpenTTD.
Definition: core.h:41
SPS_CLOSED
@ SPS_CLOSED
The connection got closed.
Definition: tcp.h:22
NetworkTCPSocketHandler::IsConnected
bool IsConnected() const
Whether this socket is currently bound to a socket.
Definition: tcp.h:41
NetworkTCPSocketHandler::sock
SOCKET sock
The socket currently connected to.
Definition: tcp.h:34
NetworkSocketHandler::CloseConnection
virtual NetworkRecvStatus CloseConnection(bool error=true)
Close the current connection; for TCP this will be mostly equivalent to Close(), but for UDP it just ...
Definition: core.h:59
SPS_ALL_SENT
@ SPS_ALL_SENT
All packets in the queue are sent.
Definition: tcp.h:25
NetworkTCPSocketHandler::ReceivePacket
virtual Packet * ReceivePacket()
Receives a packet for the given client.
Definition: tcp.cpp:145
NetworkTCPSocketHandler::packet_recv
Packet * packet_recv
Partially received packet.
Definition: tcp.h:32
DEBUG
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:35
SendPacketsState
SendPacketsState
The states of sending the packets.
Definition: tcp.h:21
Packet::PrepareToRead
void PrepareToRead()
Prepares the packet so it can be read.
Definition: packet.cpp:196
NetworkTCPSocketHandler::CloseConnection
NetworkRecvStatus CloseConnection(bool error=true) override
Close the current connection; for TCP this will be mostly equivalent to Close(), but for UDP it just ...
Definition: tcp.cpp:38
Packet::size
PacketSize size
The size of the whole packet for received packets.
Definition: packet.h:48
SEND_MTU
static const uint16 SEND_MTU
Number of bytes we can pack in a single packet.
Definition: config.h:33
NetworkTCPSocketHandler::SendPacket
virtual void SendPacket(Packet *packet)
This function puts the packet in the send-queue and it is send as soon as possible.
Definition: tcp.cpp:61
Packet::next
Packet * next
The next packet.
Definition: packet.h:42
Packet::ReadRawPacketSize
void ReadRawPacketSize()
Reads the packet size from the raw packet and stores it in the packet->size.
Definition: packet.cpp:186
Packet
Internal entity of a packet.
Definition: packet.h:40
NetworkTCPSocketHandler::NetworkTCPSocketHandler
NetworkTCPSocketHandler(SOCKET s=INVALID_SOCKET)
Construct a socket handler for a TCP connection.
Definition: tcp.cpp:23
NetworkTCPSocketHandler::writable
bool writable
Can we write to this socket?
Definition: tcp.h:35
SPS_NONE_SENT
@ SPS_NONE_SENT
The buffer is still full, so no (parts of) packets could be sent.
Definition: tcp.h:23
NetworkTCPSocketHandler::SendPackets
SendPacketsState SendPackets(bool closing_down=false)
Sends all the buffered packets out for this client.
Definition: tcp.cpp:95
NetworkTCPSocketHandler::CanSendReceive
bool CanSendReceive()
Check whether this socket can send or receive something.
Definition: tcp.cpp:225
PacketSize
uint16 PacketSize
Size of the whole packet.
Definition: packet.h:19
NetworkRecvStatus
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
Definition: core.h:22
tcp.h
ReallocT
static T * ReallocT(T *t_ptr, size_t num_elements)
Simplified reallocation function that allocates the specified number of elements of the given type.
Definition: alloc_func.hpp:111
error
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Definition: openttd.cpp:129
SPS_PARTLY_SENT
@ SPS_PARTLY_SENT
The packets are partly sent; there are more packets to be sent in the queue.
Definition: tcp.h:24
NETWORK_RECV_STATUS_OKAY
@ NETWORK_RECV_STATUS_OKAY
Everything is okay.
Definition: core.h:23
NetworkTCPSocketHandler::packet_queue
Packet * packet_queue
Packets that are awaiting delivery.
Definition: tcp.h:31
Packet::pos
PacketSize pos
The current read/write position in the packet.
Definition: packet.h:50