--- a/src/network/core/udp.c Wed Jan 03 20:00:29 2007 +0000
+++ b/src/network/core/udp.c Thu Jan 11 13:16:26 2007 +0000
@@ -3,14 +3,8 @@
#ifdef ENABLE_NETWORK
#include "../../stdafx.h"
-#include "../../date.h"
#include "../../debug.h"
#include "../../macros.h"
-#include "../../newgrf_config.h"
-
-#include "os_abstraction.h"
-#include "config.h"
-#include "game.h"
#include "packet.h"
#include "udp.h"
@@ -19,25 +13,6 @@
*/
/**
- * Send a packet over UDP
- * @param udp the socket to send over
- * @param p the packet to send
- * @param recv the receiver (target) of the packet
- */
-void NetworkSendUDP_Packet(SOCKET udp, Packet *p, struct sockaddr_in *recv)
-{
- int res;
-
- NetworkSend_FillPacketSize(p);
-
- /* Send the buffer */
- res = sendto(udp, p->buffer, p->size, 0, (struct sockaddr *)recv, sizeof(*recv));
-
- /* Check for any errors, but ignore it otherwise */
- if (res == -1) DEBUG(net, 1, "[udp] sendto failed with: %i", GET_LAST_ERROR());
-}
-
-/**
* Start listening on the given host and port.
* @param udp the place where the (references to the) UDP are stored
* @param host the host (ip) to listen on
@@ -45,12 +20,12 @@
* @param broadcast whether to allow broadcast sending/receiving
* @return true if the listening succeeded
*/
-bool NetworkUDPListen(SOCKET *udp, uint32 host, uint16 port, bool broadcast)
+bool NetworkUDPListen(SOCKET *udp, const uint32 host, const uint16 port, const bool broadcast)
{
struct sockaddr_in sin;
/* Make sure socket is closed */
- closesocket(*udp);
+ NetworkUDPClose(udp);
*udp = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (*udp == INVALID_SOCKET) {
@@ -92,10 +67,42 @@
}
/**
+ * Close the given UDP socket
+ * @param udp the socket to close
+ */
+void NetworkUDPClose(SOCKET *udp)
+{
+ if (*udp == INVALID_SOCKET) return;
+
+ closesocket(*udp);
+ *udp = INVALID_SOCKET;
+}
+
+
+/**
+ * Send a packet over UDP
+ * @param udp the socket to send over
+ * @param p the packet to send
+ * @param recv the receiver (target) of the packet
+ */
+void NetworkSendUDP_Packet(const SOCKET udp, Packet *p, const struct sockaddr_in *recv)
+{
+ int res;
+
+ NetworkSend_FillPacketSize(p);
+
+ /* Send the buffer */
+ res = sendto(udp, p->buffer, p->size, 0, (struct sockaddr *)recv, sizeof(*recv));
+
+ /* Check for any errors, but ignore it otherwise */
+ if (res == -1) DEBUG(net, 1, "[udp] sendto failed with: %i", GET_LAST_ERROR());
+}
+
+/**
* Receive a packet at UDP level
* @param udp the socket to receive the packet on
*/
-void NetworkUDPReceive(SOCKET udp)
+void NetworkUDPReceive(const SOCKET udp)
{
struct sockaddr_in client_addr;
socklen_t client_len;
@@ -109,17 +116,25 @@
/* Try to receive anything */
nbytes = recvfrom(udp, p.buffer, packet_len, 0, (struct sockaddr *)&client_addr, &client_len);
- /* We got some bytes for the base header of the packet.
- * Assume we received the whole packet. */
+ /* We got some bytes for the base header of the packet. */
if (nbytes > 2) {
NetworkRecv_ReadPacketSize(&p);
+ /* If the size does not match the packet must be corrupted.
+ * Otherwise it will be marked as corrupted later on. */
+ if (nbytes != p.size) {
+ DEBUG(net, 1, "received a packet with mismatching size from %s:%d",
+ inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
+
+ return;
+ }
+
/* Put the position on the right place */
p.pos = 2;
p.next = NULL;
/* Handle the packet */
- NetworkHandleUDPPacket(&p, &client_addr);
+ NetworkHandleUDPPacket(udp, &p, &client_addr);
}
}
@@ -168,6 +183,8 @@
* The parts must be read in the same order as they are sent!
*/
+ /* Update the documentation in udp.h on changes
+ * to the NetworkGameInfo wire-protocol! */
/* NETWORK_GAME_INFO_VERSION = 4 */
{
@@ -229,6 +246,9 @@
* The parts must be read in the same order as they are sent!
*/
+ /* Update the documentation in udp.h on changes
+ * to the NetworkGameInfo wire-protocol! */
+
switch (info->game_info_version) {
case 4: {
GRFConfig *c, **dst = &info->grfconfig;