Skip to content

Commit ec2bba7

Browse files
committed
add packet tagging support for linux
1 parent fc3e1bd commit ec2bba7

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

netcode.c

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -429,17 +429,18 @@ struct netcode_socket_holder_t
429429
struct netcode_socket_t ipv6;
430430
};
431431

432-
#define NETCODE_SOCKET_ERROR_NONE 0
433-
#define NETCODE_SOCKET_ERROR_CREATE_FAILED 1
434-
#define NETCODE_SOCKET_ERROR_SET_NON_BLOCKING_FAILED 2
435-
#define NETCODE_SOCKET_ERROR_SOCKOPT_IPV6_ONLY_FAILED 3
436-
#define NETCODE_SOCKET_ERROR_SOCKOPT_RCVBUF_FAILED 4
437-
#define NETCODE_SOCKET_ERROR_SOCKOPT_SNDBUF_FAILED 5
438-
#define NETCODE_SOCKET_ERROR_BIND_IPV4_FAILED 6
439-
#define NETCODE_SOCKET_ERROR_BIND_IPV6_FAILED 7
440-
#define NETCODE_SOCKET_ERROR_GET_SOCKNAME_IPV4_FAILED 8
441-
#define NETCODE_SOCKET_ERROR_GET_SOCKNAME_IPV6_FAILED 9
442-
#define NETCODE_SOCKET_ERROR_DISABLE_UDP_PORT_CONNRESET_FAILED 10
432+
#define NETCODE_SOCKET_ERROR_NONE 0
433+
#define NETCODE_SOCKET_ERROR_CREATE_FAILED 1
434+
#define NETCODE_SOCKET_ERROR_SET_NON_BLOCKING_FAILED 2
435+
#define NETCODE_SOCKET_ERROR_SOCKOPT_IPV6_ONLY_FAILED 3
436+
#define NETCODE_SOCKET_ERROR_SOCKOPT_RCVBUF_FAILED 4
437+
#define NETCODE_SOCKET_ERROR_SOCKOPT_SNDBUF_FAILED 5
438+
#define NETCODE_SOCKET_ERROR_BIND_IPV4_FAILED 6
439+
#define NETCODE_SOCKET_ERROR_BIND_IPV6_FAILED 7
440+
#define NETCODE_SOCKET_ERROR_GET_SOCKNAME_IPV4_FAILED 8
441+
#define NETCODE_SOCKET_ERROR_GET_SOCKNAME_IPV6_FAILED 9
442+
#define NETCODE_SOCKET_ERROR_DISABLE_UDP_PORT_CONNRESET_FAILED 10
443+
#define NETCODE_SOCKET_ERROR_ENABLE_PACKET_TAGGING_FAILED 11
443444

444445
void netcode_socket_destroy( struct netcode_socket_t * socket )
445446
{
@@ -640,21 +641,43 @@ int netcode_socket_create( struct netcode_socket_t * s, struct netcode_address_t
640641
if ( setsockopt( s->handle, IPPROTO_IPV6, IPV6_TCLASS, (const char *)&tos, sizeof(tos) ) != 0 )
641642
{
642643
netcode_printf( NETCODE_LOG_LEVEL_ERROR, "error: failed to enable packet tagging (ipv6)\n" );
644+
return NETCODE_SOCKET_ERROR_ENABLE_PACKET_TAGGING_FAILED;
643645
}
644646
}
645647
else
646648
{
647649
int tos = 46;
648650
if ( setsockopt( s->handle, IPPROTO_IP, IP_TOS, (const char *)&tos, sizeof(tos) ) != 0 )
649651
{
650-
netcode_printf( NETCODE_LOG_LEVEL_DEBUG, "failed to enable packet tagging (ipv4)" );
652+
netcode_printf( NETCODE_LOG_LEVEL_ERROR, "error: failed to enable packet tagging (ipv4)\n" );
653+
return NETCODE_SOCKET_ERROR_ENABLE_PACKET_TAGGING_FAILED;
651654
}
652655
}
653656
}
654657

655658
#elif NETCODE_PLATFORM == NETCODE_PLATFORM_LINUX
656659

657-
// todo: linux implementation
660+
if ( netcode_packet_tagging_enabled )
661+
{
662+
if ( address->type == NETCODE_ADDRESS_IPV6 )
663+
{
664+
int tos = 46;
665+
if ( setsockopt( socket->handle, IPPROTO_IPV6, IPV6_TCLASS, (const char *)&tos, sizeof(tos) ) != 0 )
666+
{
667+
netcode_printf( NETCODE_LOG_LEVEL_ERROR, "error: failed to enable packet tagging (ipv6)\n" );
668+
return NETCODE_SOCKET_ERROR_ENABLE_PACKET_TAGGING_FAILED;
669+
}
670+
}
671+
else
672+
{
673+
int tos = 46;
674+
if ( setsockopt( socket->handle, IPPROTO_IP, IP_TOS, (const char *)&tos, sizeof(tos) ) != 0 )
675+
{
676+
netcode_printf( NETCODE_LOG_LEVEL_ERROR, "error: failed to enable packet tagging (ipv4)\n" );
677+
return NETCODE_SOCKET_ERROR_ENABLE_PACKET_TAGGING_FAILED;
678+
}
679+
}
680+
}
658681

659682
#elif NETCODE_PLATFORM == NETCODE_PLATFORM_WINDOWS
660683

@@ -3438,7 +3461,7 @@ int netcode_encryption_manager_add_encryption_mapping( struct netcode_encryption
34383461
for ( i = 0; i < NETCODE_MAX_ENCRYPTION_MAPPINGS; i++ )
34393462
{
34403463
if ( encryption_manager->address[i].type == NETCODE_ADDRESS_NONE ||
3441-
( netcode_encryption_manager_entry_expired( encryption_manager, i, time ) && encryption_manager->client_index[i] == -1 ) )
3464+
( netcode_encryption_manager_entry_expired( encryption_manager, i, time ) && encryption_manager->client_index[i] == -1 ) )
34423465
{
34433466
encryption_manager->timeout[i] = timeout;
34443467
encryption_manager->address[i] = *address;

netcode.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
|| defined(alpha) || defined(__alpha) || defined(__alpha__) \
4545
|| defined(_M_ALPHA) \
4646
|| defined(ARM) || defined(_ARM) || defined(__arm__) \
47-
|| defined(__aarch64__) \
47+
|| defined(__aarch64__) \
4848
|| defined(WIN32) || defined(_WIN32) || defined(__WIN32__) \
4949
|| defined(_WIN32_WCE) || defined(__NT__) \
5050
|| defined(__MIPSEL__)

0 commit comments

Comments
 (0)