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