medfall

A super great game engine
Log | Files | Refs

commit 97e9c80741e4dfadb706e0c0b8572236dfb7d9eb
parent f240fe4f6a2d784026a2c7d4dbbfa1ffee84f377
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Wed Aug  2 20:17:21 -0700

Check packets at least say they came from the server

Diffstat:
hm.cc | 3+++
platform_network.h | 27+++++++++++++++++++++++++++
2 files changed, 30 insertions(+), 0 deletions(-)
diff --git a/hm.cc b/hm.cc @@ -430,6 +430,9 @@ GAME_FRAME( game_frame ) { if( !sock_tryrecv_udp( sock, buf, sizeof( buf ), &recv_addr, &len ) ) { len = 0; } + if( recv_addr != server_addr ) { + len = 0; + } ReadStream rs( buf, len ); diff --git a/platform_network.h b/platform_network.h @@ -3,6 +3,7 @@ #include <arpa/inet.h> #include "platform.h" +#include "ggformat.h" #include "log.h" enum TransportProtocol { NET_UDP, NET_TCP }; @@ -24,6 +25,17 @@ struct NetAddress { u16 port; }; +inline bool operator==( const NetAddress & lhs, const NetAddress & rhs ) { + if( lhs.type != rhs.type ) return false; + if( lhs.port != rhs.port ) return false; + if( lhs.type == NET_IPV4 ) return memcmp( &lhs.ipv4, &rhs.ipv4, sizeof( lhs.ipv4 ) ) == 0; + return memcmp( &lhs.ipv6, &rhs.ipv6, sizeof( lhs.ipv6 ) ) == 0; +} + +inline bool operator!=( const NetAddress & lhs, const NetAddress & rhs ) { + return !( lhs == rhs ); +} + struct UDPSocket { int fd; IPvX ipvx; @@ -214,3 +226,18 @@ inline void sock_destroy( TCPSocket * sock ) { } sock->fd = INVALID_SOCKET; } + +inline void format( FormatBuffer * fb, const NetAddress & addr, const FormatOpts & opts ) { + char buf[ INET6_ADDRSTRLEN ]; + const sockaddr_storage sa = netaddress_to_sockaddr( addr ); + + void * src = ( void * ) &( ( sockaddr_in * ) &sa )->sin_addr; + if( sa.ss_family == AF_INET6 ) { + src = ( void * ) &( ( sockaddr_in6 * ) &sa )->sin6_addr; + } + inet_ntop( sa.ss_family, src, buf, sizeof( buf ) ); + + format( fb, buf ); + format( fb, ":" ); + format( fb, addr.port, FormatOpts() ); +}