Skip to content

Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.

6.1 KB
Raw Blame
/*
**	Command & Conquer Generals Zero Hour(tm)
**	Copyright 2025 Electronic Arts Inc.
**
**	This program 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, either version 3 of the License, or
**	(at your option) any later version.
**
**	This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
*/
// Modified 2026 by İlyas Akın for the macOS/Linux port; see NOTICE.md and the git history.

////////////////////////////////////////////////////////////////////////////////
//																																						//
//  (c) 2001-2003 Electronic Arts Inc.																				//
//																																						//
////////////////////////////////////////////////////////////////////////////////


#pragma once

#ifndef UDP_HEADER
#define UDP_HEADER

#ifdef _UNIX
#include <errno.h>
#endif

#ifdef _WINDOWS
#include <winsock.h>
#include <io.h>
//#define close _close
//#define read  _read
//#define write _write

#else  //UNIX
#include <errno.h>		// errno and its E* codes: <errno.h> above hangs off _UNIX, which is never defined
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/time.h>
#include <fcntl.h>
#include <limits.h>
#endif

#ifdef AIX
#include <sys/select.h>
#endif

#include "Lib/BaseType.h"

#define DEFAULT_PROTOCOL 0

//#include "wlib/wstypes.h"
//#include "wlib/wtime.h"

class UDP
{
 // DATA
 private:
  Int       fd; 	// -1 when there is no socket
  UnsignedInt       myIP;
  UnsignedShort       myPort;
  struct       sockaddr_in  addr;
  
 public:
  // These defines specify a system independent way to
  //   get error codes for socket services.
  enum sockStat
  {
    OK           =  0,     // Everything's cool
    UNKNOWN      = -1,     // There was an error of unknown type
    ISCONN       = -2,     // The socket is already connected
    INPROGRESS   = -3,     // The socket is non-blocking and the operation
                           //   isn't done yet
    ALREADY      = -4,     // The socket is already attempting a connection
                           //   but isn't done yet
    AGAIN        = -5,     // Try again.
    ADDRINUSE    = -6,     // Address already in use
    ADDRNOTAVAIL = -7,     // That address is not available on the remote host
    BADF         = -8,     // Not a valid FD
    CONNREFUSED  = -9,     // Connection was refused
    INTR         =-10,     // Operation was interrupted
    NOTSOCK      =-11,     // FD wasn't a socket
    PIPE         =-12,     // That operation just made a SIGPIPE
    WOULDBLOCK   =-13,     // That operation would block
    INVAL        =-14,     // Invalid
    TIMEDOUT     =-15      // Timeout
  };

// CODE
 private:
  Int           SetBlocking(Int block);
  void          closeSocket(void);		///< closes fd, if open, and leaves it -1 (none)
	
	Int m_lastError;

 public:
                   UDP();
                  ~UDP();
  Int           Bind(UnsignedInt IP,UnsignedShort port);
  Int           Bind(const char *Host,UnsignedShort port);
  Int           Write(const unsigned char *msg,UnsignedInt len,UnsignedInt IP,UnsignedShort port);
  Int           Read(unsigned char *msg,UnsignedInt len,sockaddr_in *from);
  sockStat         GetStatus(void);
  void             ClearStatus(void);
  //int              Wait(Int sec,Int usec,fd_set &returnSet);
  //int              Wait(Int sec,Int usec,fd_set &givenSet,fd_set &returnSet);

  Int             getLocalAddr(UnsignedInt &ip, UnsignedShort &port);
  Int           getFD(void) { return(fd); }
 
  Int             SetInputBuffer(UnsignedInt bytes);
  Int             SetOutputBuffer(UnsignedInt bytes);
  int              GetInputBuffer(void);
  int              GetOutputBuffer(void);
	Int						AllowBroadcasts(Bool status);

#if !defined(_WIN32)
  /* Port defect 29.  Windows hands a broadcast to a socket bound to one unicast address, and the LAN lobby
     relies on it; BSD and Linux sockets do not, so a POSIX lobby bound to its address heard no game
     announcements at all.  The lobby therefore keeps a second socket, on the wildcard address, that
     takes only broadcasts (LANAPI::listenForBroadcasts).

     ShareAddress(TRUE) before Bind sets SO_REUSEADDR, which lets a socket bound to one address share
     its port with such a wildcard listener - the lobby socket needs it once another copy on the host
     is listening.  On BSD it does not let two sockets share one address and port: that bind still fails.
     Linux's SO_REUSEADDR would allow exactly that, so there the listener binds 255.255.255.255 instead
     of the wildcard, and the lobby socket shares nothing (udp.cpp, Bind).

     BindForBroadcasts binds the wildcard address with SO_REUSEADDR and SO_REUSEPORT (every copy on the
     host has one, and each gets its own copy of a broadcast) and asks for each datagram's destination.
     Read then passes only datagrams sent to 255.255.255.255, the only broadcast the game sends; a
     unicast datagram that reaches the wildcard socket (one sent to a local address nobody bound) is
     dropped, as a Windows lobby bound to its own address would never have seen it. */
  void          ShareAddress(Bool share) { m_shareAddress = share; }
  Int           BindForBroadcasts(UnsignedShort port);
 private:
  Bool          m_shareAddress;
  Bool          m_broadcastsOnly;
  Int           ReadBroadcast(unsigned char *msg,UnsignedInt len,sockaddr_in *from);
#endif
};

#ifdef DEBUG_LOGGING
AsciiString GetWSAErrorString( Int error );
#endif

/* The last socket call's error, for the logs: winsock's on Windows, errno elsewhere.  GetWSAErrorString
	 (udp.cpp) names either. */
inline int lastSocketError( void )
{
#if defined(_WIN32)
	return WSAGetLastError();
#else
	return errno;
#endif
}

#endif