[ENet-discuss] RE: enet_host_connect using a broadcast address

LoneSock lonesock at gmail.com
Thu Mar 17 16:04:45 PST 2005


Hi, again.

OK, here's how I've implemented my fix.  BTW, I'm not sure if this is
a desirable feature, of if broadcast ability was left out on purpose. 
Anyway, here are the changes (only tested on Win2k, using mingw gcc
3.3.1):  (and sorry this in't in diff format, just showing the 1st
part of each changed function's new version)



[in win32.c, to enable broadcasting.  I did the same thing in unix,c,
but can't test it]

ENetSocket
enet_socket_create (ENetSocketType type, const ENetAddress * address)
{
    ENetSocket newSocket = socket (PF_INET, type ==
ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0);
    u_long nonBlocking = 1,
        receiveBufferSize = ENET_HOST_RECEIVE_BUFFER_SIZE;
    struct sockaddr_in sin;
    int allowBroadcasting = 1;	//	this is new!

    if (newSocket == ENET_SOCKET_NULL)
      return ENET_SOCKET_NULL;

    if (type == ENET_SOCKET_TYPE_DATAGRAM)
    {
        ioctlsocket (newSocket, FIONBIO, & nonBlocking);

        setsockopt (newSocket, SOL_SOCKET, SO_RCVBUF, (char *) &
receiveBufferSize, sizeof (int));
        //	allow broadcasting
        setsockopt (newSocket, SOL_SOCKET, SO_BROADCAST, (char *) &
allowBroadcasting, sizeof (int));
    }



[in protocol.c, handling an ACK when address != broadcast]

static int
enet_protocol_handle_incoming_commands (ENetHost * host, ENetEvent * event)
{
    ENetProtocolHeader * header;
    ENetProtocol * command;
    ENetPeer * peer;
    enet_uint8 * currentData;
    size_t commandCount;

    if (host -> receivedDataLength < sizeof (ENetProtocolHeader))
      return 0;

    header = (ENetProtocolHeader *) host -> receivedData;

    header -> peerID = ENET_NET_TO_HOST_16 (header -> peerID);
    header -> sentTime = ENET_NET_TO_HOST_32 (header -> sentTime);

    if (header -> peerID == 0xFFFF)
      peer = NULL;
    else
    if (header -> peerID >= host -> peerCount)
      return 0;
    else
    {
       peer = & host -> peers [header -> peerID];

       if (peer -> state == ENET_PEER_STATE_DISCONNECTED ||
           peer -> state == ENET_PEER_STATE_ZOMBIE || 
           (host -> receivedAddress.host != peer -> address.host &&
		   peer -> address.host != 0xffffffffL) || //	allow any addr if broadcast
           header -> challenge != peer -> challenge)
         return 0;
       else
       {
         peer -> address.host = host -> receivedAddress.host;	//	just
in case of broadcast
         peer -> address.port = host -> receivedAddress.port;
		}         
    }

hope this helps someone.

Jonathan

-- 
Piranha are people too.


More information about the ENet-discuss mailing list