OpenTTD

udp.cpp
Go to the documentation of this file.
1 /* $Id: udp.cpp 27893 2017-08-13 18:38:42Z frosch $ */
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 "../../date_func.h"
18 #include "../../debug.h"
19 #include "udp.h"
20
21 #include "../../safeguards.h"
22
27 NetworkUDPSocketHandler::NetworkUDPSocketHandler(NetworkAddressList *bind)
28 {
29 if (bind != NULL) {
30 for (NetworkAddress *addr = bind->Begin(); addr != bind->End(); addr++) {
31 *this->bind.Append() = *addr;
32 }
33 } else {
34 /* As hostname NULL and port 0/NULL don't go well when
35 * resolving it we need to add an address for each of
36 * the address families we support. */
37 *this->bind.Append() = NetworkAddress(NULL, 0, AF_INET);
38 *this->bind.Append() = NetworkAddress(NULL, 0, AF_INET6);
39 }
40 }
41
42
47 bool NetworkUDPSocketHandler::Listen()
48 {
49 /* Make sure socket is closed */
50 this->Close();
51
52 for (NetworkAddress *addr = this->bind.Begin(); addr != this->bind.End(); addr++) {
53 addr->Listen(SOCK_DGRAM, &this->sockets);
54 }
55
56 return this->sockets.Length() != 0;
57 }
58
62 void NetworkUDPSocketHandler::Close()
63 {
64 for (SocketList::iterator s = this->sockets.Begin(); s != this->sockets.End(); s++) {
65 closesocket(s->second);
66 }
67 this->sockets.Clear();
68 }
69
70 NetworkRecvStatus NetworkUDPSocketHandler::CloseConnection(bool error)
71 {
72 NetworkSocketHandler::CloseConnection(error);
73 return NETWORK_RECV_STATUS_OKAY;
74 }
75
83 void NetworkUDPSocketHandler::SendPacket(Packet *p, NetworkAddress *recv, bool all, bool broadcast)
84 {
85 if (this->sockets.Length() == 0) this->Listen();
86
87 for (SocketList::iterator s = this->sockets.Begin(); s != this->sockets.End(); s++) {
88 /* Make a local copy because if we resolve it we cannot
89 * easily unresolve it so we can resolve it later again. */
90 NetworkAddress send(*recv);
91
92 /* Not the same type */
93 if (!send.IsFamily(s->first.GetAddress()->ss_family)) continue;
94
95 p->PrepareToSend();
96
97 #ifndef BEOS_NET_SERVER /* will work around this, some day; maybe. */
98 if (broadcast) {
99 /* Enable broadcast */
100 unsigned long val = 1;
101 if (setsockopt(s->second, SOL_SOCKET, SO_BROADCAST, (char *) &val, sizeof(val)) < 0) {
102 DEBUG(net, 1, "[udp] setting broadcast failed with: %i", GET_LAST_ERROR());
103 }
104 }
105 #endif
106
107 /* Send the buffer */
108 int res = sendto(s->second, (const char*)p->buffer, p->size, 0, (const struct sockaddr *)send.GetAddress(), send.GetAddressLength());
109 DEBUG(net, 7, "[udp] sendto(%s)", send.GetAddressAsString());
110
111 /* Check for any errors, but ignore it otherwise */
112 if (res == -1) DEBUG(net, 1, "[udp] sendto(%s) failed with: %i", send.GetAddressAsString(), GET_LAST_ERROR());
113
114 if (!all) break;
115 }
116 }
117
121 void NetworkUDPSocketHandler::ReceivePackets()
122 {
123 for (SocketList::iterator s = this->sockets.Begin(); s != this->sockets.End(); s++) {
124 for (int i = 0; i < 1000; i++) { // Do not infinitely loop when DoSing with UDP
125 struct sockaddr_storage client_addr;
126 memset(&client_addr, 0, sizeof(client_addr));
127
128 Packet p(this);
129 socklen_t client_len = sizeof(client_addr);
130
131 /* Try to receive anything */
132 SetNonBlocking(s->second); // Some OSes seem to lose the non-blocking status of the socket
133 int nbytes = recvfrom(s->second, (char*)p.buffer, SEND_MTU, 0, (struct sockaddr *)&client_addr, &client_len);
134
135 /* Did we get the bytes for the base header of the packet? */
136 if (nbytes <= 0) break; // No data, i.e. no packet
137 if (nbytes <= 2) continue; // Invalid data; try next packet
138
139 NetworkAddress address(client_addr, client_len);
140 p.PrepareToRead();
141
142 /* If the size does not match the packet must be corrupted.
143 * Otherwise it will be marked as corrupted later on. */
144 if (nbytes != p.size) {
145 DEBUG(net, 1, "received a packet with mismatching size from %s", address.GetAddressAsString());
146 continue;
147 }
148
149 /* Handle the packet */
150 this->HandleUDPPacket(&p, &address);
151 }
152 }
153 }
154
155
161 void NetworkUDPSocketHandler::SendNetworkGameInfo(Packet *p, const NetworkGameInfo *info)
162 {
163 p->Send_uint8 (NETWORK_GAME_INFO_VERSION);
164
165 /*
166 * Please observe the order.
167 * The parts must be read in the same order as they are sent!
168 */
169
170 /* Update the documentation in udp.h on changes
171 * to the NetworkGameInfo wire-protocol! */
172
173 /* NETWORK_GAME_INFO_VERSION = 4 */
174 {
175 /* Only send the GRF Identification (GRF_ID and MD5 checksum) of
176 * the GRFs that are needed, i.e. the ones that the server has
177 * selected in the NewGRF GUI and not the ones that are used due
178 * to the fact that they are in [newgrf-static] in openttd.cfg */
179 const GRFConfig *c;
180 uint count = 0;
181
182 /* Count number of GRFs to send information about */
183 for (c = info->grfconfig; c != NULL; c = c->next) {
184 if (!HasBit(c->flags, GCF_STATIC)) count++;
185 }
186 p->Send_uint8 (count); // Send number of GRFs
187
188 /* Send actual GRF Identifications */
189 for (c = info->grfconfig; c != NULL; c = c->next) {
190 if (!HasBit(c->flags, GCF_STATIC)) this->SendGRFIdentifier(p, &c->ident);
191 }
192 }
193
194 /* NETWORK_GAME_INFO_VERSION = 3 */
195 p->Send_uint32(info->game_date);
196 p->Send_uint32(info->start_date);
197
198 /* NETWORK_GAME_INFO_VERSION = 2 */
199 p->Send_uint8 (info->companies_max);
200 p->Send_uint8 (info->companies_on);
201 p->Send_uint8 (info->spectators_max);
202
203 /* NETWORK_GAME_INFO_VERSION = 1 */
204 p->Send_string(info->server_name);
205 p->Send_string(info->server_revision);
206 p->Send_uint8 (info->server_lang);
207 p->Send_bool (info->use_password);
208 p->Send_uint8 (info->clients_max);
209 p->Send_uint8 (info->clients_on);
210 p->Send_uint8 (info->spectators_on);
211 p->Send_string(info->map_name);
212 p->Send_uint16(info->map_width);
213 p->Send_uint16(info->map_height);
214 p->Send_uint8 (info->map_set);
215 p->Send_bool (info->dedicated);
216 }
217
223 void NetworkUDPSocketHandler::ReceiveNetworkGameInfo(Packet *p, NetworkGameInfo *info)
224 {
225 static const Date MAX_DATE = ConvertYMDToDate(MAX_YEAR, 11, 31); // December is month 11
226
227 info->game_info_version = p->Recv_uint8();
228
229 /*
230 * Please observe the order.
231 * The parts must be read in the same order as they are sent!
232 */
233
234 /* Update the documentation in udp.h on changes
235 * to the NetworkGameInfo wire-protocol! */
236
237 switch (info->game_info_version) {
238 case 4: {
239 GRFConfig **dst = &info->grfconfig;
240 uint i;
241 uint num_grfs = p->Recv_uint8();
242
243 /* Broken/bad data. It cannot have that many NewGRFs. */
244 if (num_grfs > NETWORK_MAX_GRF_COUNT) return;
245
246 for (i = 0; i < num_grfs; i++) {
247 GRFConfig *c = new GRFConfig();
248 this->ReceiveGRFIdentifier(p, &c->ident);
249 this->HandleIncomingNetworkGameInfoGRFConfig(c);
250
251 /* Append GRFConfig to the list */
252 *dst = c;
253 dst = &c->next;
254 }
255 }
256 FALLTHROUGH;
257
258 case 3:
259 info->game_date = Clamp(p->Recv_uint32(), 0, MAX_DATE);
260 info->start_date = Clamp(p->Recv_uint32(), 0, MAX_DATE);
261 FALLTHROUGH;
262
263 case 2:
264 info->companies_max = p->Recv_uint8 ();
265 info->companies_on = p->Recv_uint8 ();
266 info->spectators_max = p->Recv_uint8 ();
267 FALLTHROUGH;
268
269 case 1:
270 p->Recv_string(info->server_name, sizeof(info->server_name));
271 p->Recv_string(info->server_revision, sizeof(info->server_revision));
272 info->server_lang = p->Recv_uint8 ();
273 info->use_password = p->Recv_bool ();
274 info->clients_max = p->Recv_uint8 ();
275 info->clients_on = p->Recv_uint8 ();
276 info->spectators_on = p->Recv_uint8 ();
277 if (info->game_info_version < 3) { // 16 bits dates got scrapped and are read earlier
278 info->game_date = p->Recv_uint16() + DAYS_TILL_ORIGINAL_BASE_YEAR;
279 info->start_date = p->Recv_uint16() + DAYS_TILL_ORIGINAL_BASE_YEAR;
280 }
281 p->Recv_string(info->map_name, sizeof(info->map_name));
282 info->map_width = p->Recv_uint16();
283 info->map_height = p->Recv_uint16();
284 info->map_set = p->Recv_uint8 ();
285 info->dedicated = p->Recv_bool ();
286
287 if (info->server_lang >= NETWORK_NUM_LANGUAGES) info->server_lang = 0;
288 if (info->map_set >= NETWORK_NUM_LANDSCAPES) info->map_set = 0;
289 }
290 }
291
297 void NetworkUDPSocketHandler::HandleUDPPacket(Packet *p, NetworkAddress *client_addr)
298 {
299 PacketUDPType type;
300
301 /* New packet == new client, which has not quit yet */
302 this->Reopen();
303
304 type = (PacketUDPType)p->Recv_uint8();
305
306 switch (this->HasClientQuit() ? PACKET_UDP_END : type) {
307 case PACKET_UDP_CLIENT_FIND_SERVER: this->Receive_CLIENT_FIND_SERVER(p, client_addr); break;
308 case PACKET_UDP_SERVER_RESPONSE: this->Receive_SERVER_RESPONSE(p, client_addr); break;
309 case PACKET_UDP_CLIENT_DETAIL_INFO: this->Receive_CLIENT_DETAIL_INFO(p, client_addr); break;
310 case PACKET_UDP_SERVER_DETAIL_INFO: this->Receive_SERVER_DETAIL_INFO(p, client_addr); break;
311 case PACKET_UDP_SERVER_REGISTER: this->Receive_SERVER_REGISTER(p, client_addr); break;
312 case PACKET_UDP_MASTER_ACK_REGISTER: this->Receive_MASTER_ACK_REGISTER(p, client_addr); break;
313 case PACKET_UDP_CLIENT_GET_LIST: this->Receive_CLIENT_GET_LIST(p, client_addr); break;
314 case PACKET_UDP_MASTER_RESPONSE_LIST: this->Receive_MASTER_RESPONSE_LIST(p, client_addr); break;
315 case PACKET_UDP_SERVER_UNREGISTER: this->Receive_SERVER_UNREGISTER(p, client_addr); break;
316 case PACKET_UDP_CLIENT_GET_NEWGRFS: this->Receive_CLIENT_GET_NEWGRFS(p, client_addr); break;
317 case PACKET_UDP_SERVER_NEWGRFS: this->Receive_SERVER_NEWGRFS(p, client_addr); break;
318 case PACKET_UDP_MASTER_SESSION_KEY: this->Receive_MASTER_SESSION_KEY(p, client_addr); break;
319
320 default:
321 if (this->HasClientQuit()) {
322 DEBUG(net, 0, "[udp] received invalid packet type %d from %s", type, client_addr->GetAddressAsString());
323 } else {
324 DEBUG(net, 0, "[udp] received illegal packet from %s", client_addr->GetAddressAsString());
325 }
326 break;
327 }
328 }
329
335 void NetworkUDPSocketHandler::ReceiveInvalidPacket(PacketUDPType type, NetworkAddress *client_addr)
336 {
337 DEBUG(net, 0, "[udp] received packet type %d on wrong port from %s", type, client_addr->GetAddressAsString());
338 }
339
340 void NetworkUDPSocketHandler::Receive_CLIENT_FIND_SERVER(Packet *p, NetworkAddress *client_addr) { this->ReceiveInvalidPacket(PACKET_UDP_CLIENT_FIND_SERVER, client_addr); }
341 void NetworkUDPSocketHandler::Receive_SERVER_RESPONSE(Packet *p, NetworkAddress *client_addr) { this->ReceiveInvalidPacket(PACKET_UDP_SERVER_RESPONSE, client_addr); }
342 void NetworkUDPSocketHandler::Receive_CLIENT_DETAIL_INFO(Packet *p, NetworkAddress *client_addr) { this->ReceiveInvalidPacket(PACKET_UDP_CLIENT_DETAIL_INFO, client_addr); }
343 void NetworkUDPSocketHandler::Receive_SERVER_DETAIL_INFO(Packet *p, NetworkAddress *client_addr) { this->ReceiveInvalidPacket(PACKET_UDP_SERVER_DETAIL_INFO, client_addr); }
344 void NetworkUDPSocketHandler::Receive_SERVER_REGISTER(Packet *p, NetworkAddress *client_addr) { this->ReceiveInvalidPacket(PACKET_UDP_SERVER_REGISTER, client_addr); }
345 void NetworkUDPSocketHandler::Receive_MASTER_ACK_REGISTER(Packet *p, NetworkAddress *client_addr) { this->ReceiveInvalidPacket(PACKET_UDP_MASTER_ACK_REGISTER, client_addr); }
346 void NetworkUDPSocketHandler::Receive_CLIENT_GET_LIST(Packet *p, NetworkAddress *client_addr) { this->ReceiveInvalidPacket(PACKET_UDP_CLIENT_GET_LIST, client_addr); }
347 void NetworkUDPSocketHandler::Receive_MASTER_RESPONSE_LIST(Packet *p, NetworkAddress *client_addr) { this->ReceiveInvalidPacket(PACKET_UDP_MASTER_RESPONSE_LIST, client_addr); }
348 void NetworkUDPSocketHandler::Receive_SERVER_UNREGISTER(Packet *p, NetworkAddress *client_addr) { this->ReceiveInvalidPacket(PACKET_UDP_SERVER_UNREGISTER, client_addr); }
349 void NetworkUDPSocketHandler::Receive_CLIENT_GET_NEWGRFS(Packet *p, NetworkAddress *client_addr) { this->ReceiveInvalidPacket(PACKET_UDP_CLIENT_GET_NEWGRFS, client_addr); }
350 void NetworkUDPSocketHandler::Receive_SERVER_NEWGRFS(Packet *p, NetworkAddress *client_addr) { this->ReceiveInvalidPacket(PACKET_UDP_SERVER_NEWGRFS, client_addr); }
351 void NetworkUDPSocketHandler::Receive_MASTER_SESSION_KEY(Packet *p, NetworkAddress *client_addr) { this->ReceiveInvalidPacket(PACKET_UDP_MASTER_SESSION_KEY, client_addr); }
352
353 #endif /* ENABLE_NETWORK */
NETWORK_RECV_STATUS_OKAY
Everything is okay.
Definition: core.h:27
NetworkGameInfo::start_date
Date start_date
When the game started.
Definition: game.h:38
NetworkGameInfo::spectators_max
byte spectators_max
Max spectators allowed on server.
Definition: game.h:55
NetworkGameInfo::map_height
uint16 map_height
Map height.
Definition: game.h:41
NetworkSocketHandler::HasClientQuit
bool HasClientQuit() const
Whether the current client connected to the socket has quit.
Definition: core.h:71
NetworkUDPSocketHandler::Receive_CLIENT_GET_NEWGRFS
virtual void Receive_CLIENT_GET_NEWGRFS(Packet *p, NetworkAddress *client_addr)
The client requests information about some NewGRFs.
Definition: udp.cpp:349
Packet
Internal entity of a packet.
Definition: packet.h:44
PACKET_UDP_MASTER_SESSION_KEY
Sends a fresh session key to the client.
Definition: udp.h:36
NetworkUDPSocketHandler::NetworkUDPSocketHandler
NetworkUDPSocketHandler(NetworkAddressList *bind=NULL)
Create an UDP socket but don&#39;t listen yet.
Definition: udp.cpp:27
NetworkUDPSocketHandler::Receive_SERVER_REGISTER
virtual void Receive_SERVER_REGISTER(Packet *p, NetworkAddress *client_addr)
Registers the server to the master server.
Definition: udp.cpp:344
Packet::Recv_uint32
uint32 Recv_uint32()
Read a 32 bits integer from the packet.
Definition: packet.cpp:250
PACKET_UDP_CLIENT_FIND_SERVER
Queries a game server for game information.
Definition: udp.h:25
DAYS_TILL_ORIGINAL_BASE_YEAR
#define DAYS_TILL_ORIGINAL_BASE_YEAR
The offset in days from the &#39;_date == 0&#39; till &#39;ConvertYMDToDate(ORIGINAL_BASE_YEAR, 0, 1)&#39;.
Definition: date_type.h:82
NetworkGameInfo::game_info_version
byte game_info_version
Version of the game info.
Definition: game.h:49
PACKET_UDP_CLIENT_GET_NEWGRFS
Requests the name for a list of GRFs (GRF_ID and MD5)
Definition: udp.h:34
NetworkUDPSocketHandler::ReceivePackets
void ReceivePackets()
Receive a packet at UDP level.
Definition: udp.cpp:121
Packet::PrepareToSend
void PrepareToSend()
Writes the packet size from the raw packet from packet->size.
Definition: packet.cpp:65
NetworkGameInfo::server_lang
byte server_lang
Language of the server (we should make a nice table for this)
Definition: game.h:50
Packet::Send_string
void Send_string(const char *data)
Sends a string over the network.
Definition: packet.cpp:152
NetworkUDPSocketHandler::Receive_SERVER_RESPONSE
virtual void Receive_SERVER_RESPONSE(Packet *p, NetworkAddress *client_addr)
Return of server information to the client.
Definition: udp.cpp:341
PACKET_UDP_CLIENT_GET_LIST
Request for serverlist from master server.
Definition: udp.h:31
NetworkUDPSocketHandler::SendNetworkGameInfo
void SendNetworkGameInfo(Packet *p, const NetworkGameInfo *info)
Serializes the NetworkGameInfo struct to the packet.
Definition: udp.cpp:161
Packet::Send_uint8
void Send_uint8(uint8 data)
Package a 8 bits integer in the packet.
Definition: packet.cpp:100
NetworkUDPSocketHandler::Receive_SERVER_UNREGISTER
virtual void Receive_SERVER_UNREGISTER(Packet *p, NetworkAddress *client_addr)
A server unregisters itself at the master server.
Definition: udp.cpp:348
NetworkUDPSocketHandler::sockets
SocketList sockets
The opened sockets.
Definition: udp.h:55
SmallVector::Clear
void Clear()
Remove all items from the list.
Definition: smallvec_type.hpp:98
NetworkUDPSocketHandler::Receive_MASTER_SESSION_KEY
virtual void Receive_MASTER_SESSION_KEY(Packet *p, NetworkAddress *client_addr)
The master server sends us a session key.
Definition: udp.cpp:351
GCF_STATIC
GRF file is used statically (can be used in any MP game)
Definition: newgrf_config.h:26
NetworkAddress
Wrapper for (un)resolved network addresses; there&#39;s no reason to transform a numeric IP to a string a...
Definition: address.h:31
SmallVector::Begin
const T * Begin() const
Get the pointer to the first item (const)
Definition: smallvec_type.hpp:272
PacketUDPType
PacketUDPType
Enum with all types of UDP packets.
Definition: udp.h:24
NetworkGameInfo::map_width
uint16 map_width
Map width.
Definition: game.h:40
Packet::Send_uint32
void Send_uint32(uint32 data)
Package a 32 bits integer in the packet.
Definition: packet.cpp:121
SmallVector< NetworkAddress, 4 >
PACKET_UDP_SERVER_UNREGISTER
Request to be removed from the server-list.
Definition: udp.h:33
GRFConfig::ident
GRFIdentifier ident
grfid and md5sum to uniquely identify newgrfs
Definition: newgrf_config.h:158
NetworkSocketHandler::Reopen
void Reopen()
Reopen the socket so we can send/receive stuff again.
Definition: core.h:76
SmallVector::End
const T * End() const
Get the pointer behind the last valid item (const)
Definition: smallvec_type.hpp:292
GRFConfig::next
struct GRFConfig * next
NOSAVE: Next item in the linked list.
Definition: newgrf_config.h:178
NetworkGameInfo::server_name
char server_name[NETWORK_NAME_LENGTH]
Server name.
Definition: game.h:42
SmallVector::Append
T * Append(uint to_add=1)
Append an item and return it.
Definition: smallvec_type.hpp:134
NetworkUDPSocketHandler::ReceiveInvalidPacket
void ReceiveInvalidPacket(PacketUDPType, NetworkAddress *client_addr)
Helper for logging receiving invalid packets.
Definition: udp.cpp:335
NetworkAddress::IsFamily
bool IsFamily(int family)
Checks of this address is of the given family.
Definition: address.cpp:147
NetworkGameInfo
The game information that is sent from the server to the clients.
Definition: game.h:36
NetworkGameInfo::companies_max
byte companies_max
Max companies allowed on server.
Definition: game.h:53
Packet::PrepareToRead
void PrepareToRead()
Prepares the packet so it can be read.
Definition: packet.cpp:200
NetworkUDPSocketHandler::Receive_CLIENT_FIND_SERVER
virtual void Receive_CLIENT_FIND_SERVER(Packet *p, NetworkAddress *client_addr)
Queries to the server for information about the game.
Definition: udp.cpp:340
NetworkUDPSocketHandler::HandleIncomingNetworkGameInfoGRFConfig
virtual void HandleIncomingNetworkGameInfoGRFConfig(GRFConfig *config)
Function that is called for every GRFConfig that is read when receiving a NetworkGameInfo.
Definition: udp.h:232
NetworkGameInfo::companies_on
byte companies_on
How many started companies do we have.
Definition: game.h:52
SmallVector::Length
uint Length() const
Get the number of items in the list.
Definition: smallvec_type.hpp:262
Packet::buffer
byte * buffer
The buffer of this packet, of basically variable length up to SEND_MTU.
Definition: packet.h:56
PACKET_UDP_END
Must ALWAYS be on the end of this list!! (period)
Definition: udp.h:37
GRFConfig
Information about GRF, used in the game and (part of it) in savegames.
Definition: newgrf_config.h:153
SmallPair< NetworkAddress, SOCKET >
NetworkAddress::GetAddressLength
int GetAddressLength()
Get the (valid) length of the address.
Definition: address.h:114
Packet::Send_uint16
void Send_uint16(uint16 data)
Package a 16 bits integer in the packet.
Definition: packet.cpp:110
NetworkGameInfo::clients_max
byte clients_max
Max clients allowed on server.
Definition: game.h:51
NetworkUDPSocketHandler::Receive_CLIENT_GET_LIST
virtual void Receive_CLIENT_GET_LIST(Packet *p, NetworkAddress *client_addr)
The client requests a list of servers.
Definition: udp.cpp:346
PACKET_UDP_SERVER_REGISTER
Packet to register itself to the master server.
Definition: udp.h:29
PACKET_UDP_MASTER_ACK_REGISTER
Packet indicating registration has succeeded.
Definition: udp.h:30
NetworkGameInfo::use_password
bool use_password
Is this server passworded?
Definition: game.h:48
NetworkUDPSocketHandler::Receive_SERVER_NEWGRFS
virtual void Receive_SERVER_NEWGRFS(Packet *p, NetworkAddress *client_addr)
The server returns information about some NewGRFs.
Definition: udp.cpp:350
PACKET_UDP_SERVER_DETAIL_INFO
Reply of the game server about details of the game, such as companies.
Definition: udp.h:28
GRFConfig::flags
uint8 flags
NOSAVE: GCF_Flags, bitset.
Definition: newgrf_config.h:168
NetworkRecvStatus
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
Definition: core.h:26
NETWORK_GAME_INFO_VERSION
static const byte NETWORK_GAME_INFO_VERSION
What version of game-info do we use?
Definition: config.h:38
NetworkServerGameInfo::clients_on
byte clients_on
Current count of clients on server.
Definition: game.h:30
NetworkGameInfo::server_revision
char server_revision[NETWORK_REVISION_LENGTH]
The version number the server is using (e.g.: &#39;r304&#39; or 0.5.0)
Definition: game.h:44
Packet::size
PacketSize size
The size of the whole packet for received packets.
Definition: packet.h:52
NetworkUDPSocketHandler::Receive_CLIENT_DETAIL_INFO
virtual void Receive_CLIENT_DETAIL_INFO(Packet *p, NetworkAddress *client_addr)
Query for detailed information about companies.
Definition: udp.cpp:342
NetworkUDPSocketHandler::Receive_MASTER_ACK_REGISTER
virtual void Receive_MASTER_ACK_REGISTER(Packet *p, NetworkAddress *client_addr)
The master server acknowledges the registration.
Definition: udp.cpp:345
Packet::Recv_bool
bool Recv_bool()
Read a boolean from the packet.
Definition: packet.cpp:212
NetworkUDPSocketHandler::Listen
bool Listen()
Start listening on the given host and port.
Definition: udp.cpp:47
NetworkGameInfo::dedicated
bool dedicated
Is this a dedicated server?
Definition: game.h:45
NetworkUDPSocketHandler::SendPacket
void SendPacket(Packet *p, NetworkAddress *recv, bool all=false, bool broadcast=false)
Send a packet over UDP.
Definition: udp.cpp:83
Clamp
static T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:139
DEBUG
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:39
PACKET_UDP_SERVER_NEWGRFS
Sends the list of NewGRF&#39;s requested.
Definition: udp.h:35
NetworkServerGameInfo::map_name
char map_name[NETWORK_NAME_LENGTH]
Map which is played ["random" for a randomized map].
Definition: game.h:29
NetworkGameInfo::map_set
byte map_set
Graphical set.
Definition: game.h:56
NetworkGameInfo::game_date
Date game_date
Current date.
Definition: game.h:39
NetworkUDPSocketHandler::HandleUDPPacket
void HandleUDPPacket(Packet *p, NetworkAddress *client_addr)
Handle an incoming packets by sending it to the correct function.
Definition: udp.cpp:297
NetworkUDPSocketHandler::Close
void Close()
Close the given UDP socket.
Definition: udp.cpp:62
Packet::Recv_uint8
uint8 Recv_uint8()
Read a 8 bits integer from the packet.
Definition: packet.cpp:221
NetworkUDPSocketHandler::Receive_SERVER_DETAIL_INFO
virtual void Receive_SERVER_DETAIL_INFO(Packet *p, NetworkAddress *client_addr)
Reply with detailed company information.
Definition: udp.cpp:343
SetNonBlocking
static bool SetNonBlocking(SOCKET d)
Try to set the socket into non-blocking mode.
Definition: os_abstraction.h:314
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:63
error
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Definition: openttd.cpp:110
SEND_MTU
static const uint16 SEND_MTU
Number of bytes we can pack in a single packet.
Definition: config.h:35
PACKET_UDP_MASTER_RESPONSE_LIST
Response from master server with server ip&#39;s + port&#39;s.
Definition: udp.h:32
NetworkUDPSocketHandler::bind
NetworkAddressList bind
The address to bind to.
Definition: udp.h:53
NetworkUDPSocketHandler::ReceiveNetworkGameInfo
void ReceiveNetworkGameInfo(Packet *p, NetworkGameInfo *info)
Deserializes the NetworkGameInfo struct from the packet.
Definition: udp.cpp:223
NETWORK_NUM_LANGUAGES
static const uint NETWORK_NUM_LANGUAGES
Number of known languages (to the network protocol) + 1 for &#39;any&#39;.
Definition: config.h:62
NetworkUDPSocketHandler::CloseConnection
NetworkRecvStatus CloseConnection(bool error=true)
Close the current connection; for TCP this will be mostly equivalent to Close(), but for UDP it just ...
Definition: udp.cpp:70
PACKET_UDP_CLIENT_DETAIL_INFO
Queries a game server about details of the game, such as companies.
Definition: udp.h:27
Date
int32 Date
The type to store our dates in.
Definition: date_type.h:16
PACKET_UDP_SERVER_RESPONSE
Reply of the game server with game information.
Definition: udp.h:26
NetworkUDPSocketHandler::Receive_MASTER_RESPONSE_LIST
virtual void Receive_MASTER_RESPONSE_LIST(Packet *p, NetworkAddress *client_addr)
The server sends a list of servers.
Definition: udp.cpp:347
HasBit
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:105
NetworkSocketHandler::ReceiveGRFIdentifier
void ReceiveGRFIdentifier(Packet *p, GRFIdentifier *grf)
Deserializes the GRFIdentifier (GRF ID and MD5 checksum) from the packet.
Definition: core.cpp:122
ConvertYMDToDate
Date ConvertYMDToDate(Year year, Month month, Day day)
Converts a tuple of Year, Month and Day to a Date.
Definition: date.cpp:149
Packet::Recv_uint16
uint16 Recv_uint16()
Read a 16 bits integer from the packet.
Definition: packet.cpp:235
NETWORK_NUM_LANDSCAPES
static const uint NETWORK_NUM_LANDSCAPES
The number of landscapes in OpenTTD.
Definition: config.h:72
Packet::Send_bool
void Send_bool(bool data)
Package a boolean in the packet.
Definition: packet.cpp:91
NetworkGameInfo::grfconfig
GRFConfig * grfconfig
List of NewGRF files used.
Definition: game.h:37
NetworkSocketHandler::SendGRFIdentifier
void SendGRFIdentifier(Packet *p, const GRFIdentifier *grf)
Serializes the GRFIdentifier (GRF ID and MD5 checksum) to the packet.
Definition: core.cpp:108
MAX_YEAR
static const Year MAX_YEAR
MAX_YEAR, nicely rounded value of the number of years that can be encoded in a single 32 bits date...
Definition: date_type.h:94
udp.h
Basic functions to receive and send UDP packets.
NetworkAddress::GetAddress
const sockaddr_storage * GetAddress()
Get the address in its internal representation.
Definition: address.cpp:128
NetworkAddress::GetAddressAsString
void GetAddressAsString(char *buffer, const char *last, bool with_family=true)
Get the address as a string, e.g.
Definition: address.cpp:81
NetworkGameInfo::spectators_on
byte spectators_on
How many spectators do we have?
Definition: game.h:54
NETWORK_MAX_GRF_COUNT
static const uint NETWORK_MAX_GRF_COUNT
Maximum number of GRFs that can be sent.
Definition: config.h:60
Packet::Recv_string
void Recv_string(char *buffer, size_t size, StringValidationSettings settings=SVS_REPLACE_WITH_QUESTION_MARK)
Reads a string till it finds a &#39;\0&#39; in the stream.
Definition: packet.cpp:290