#include "sdl.h" #include #include "sdl-netplay.h" char *ServerHost; static int LocalPortTCP=0xFCE; static int LocalPortUDP=0xFCE; static int RemotePortTCP=0xFCE; static int RemotePortUDP; /* Not configurable, figured out during handshake. */ static TCPsocket Socket; static UDPsocket UDPSocket; static SDLNet_SocketSet set; static void en32(uint8 *buf, uint32 morp) { buf[0]=morp; buf[1]=morp>>8; buf[2]=morp>>16; buf[3]=morp>>24; } static uint32 de32(uint8 *morp) { return(morp[0]|(morp[1]<<8)|(morp[2]<<16)|(morp[3]<<24)); } int FCEUD_NetworkConnect(void) { IPaddress rip; SDLNet_Init(); if(netplay==1) /* Be a server. */ { TCPsocket tmp; Uint16 p=LocalPortUDP; SDLNet_ResolveHost(&rip,NULL,LocalPortTCP); UDPSocket=SDLNet_UDP_Open(&p); tmp=SDLNet_TCP_Open(&rip); Socket=SDLNet_TCP_Accept(&tmp); memcpy(&rip,SDLNet_TCP_GetPeerAddress(Socket),sizeof(IPaddress)); { uint8 buf[12]; uint32 player=1; magic=SDL_GetTicks(); SDLNet_Write32(buf,uport); SDLNet_Write32(buf+4,1); SDLNet_Write32(buf+8,magic); SDLNet_TCP_Send(Socket, buf, 12); /* Get the UDP port the client is waiting for data on. */ SDLNet_TCP_Recv(Socket, buf, 2); RemotePortUDP=de32(buf); } } else /* Be a client */ { SDLNet_ResolveHost(&rip,ServerHost,RemotePortTCP); Socket=SDLNet_TCP_Open(&rip); { Uint16 p=LocalPortUDP; uint8 buf[12]; UDPSocket=SDLNet_UDP_Open(&p); /* Now, tell the server what local UDP port it should send to. */ en32(buf,p); SDLNet_TCP_Send(Socket, buf, 4); /* Get the UDP port from the server we should send data to. */ SDLNet_TCP_Recv(Socket, buf, 12); RemotePortUDP=de32(buf); magic=de32(buf+8); } set=SDLNet_AllocSocketSet(1); SDLNet_TCP_AddSocket(set,TCPSocket); SDLNet_UDP_AddSocket(set,UDPSocket); } // End client connect code. rip.port=RemotePortUDP; SDLNet_UDP_Bind(UDPSocket, 0, &rip); } static int CheckUDP(uint8 *packet, int32 len, int32 alt) { uint32 crc; uint32 repcrc; crc=FCEUI_CRC32(0,packet+4,len+8); repcrc=de32(packet); if(crc!=repcrc) return(0); /* CRC32 mismatch, bad packet. */ packet+=4; if(de32(packet)!=magic) /* Magic number mismatch, bad or spoofed packet. */ return(0); packet+=4; if(alt) { if(de32(packet)