diff --git a/src/d_clisrv.c b/src/d_clisrv.c index 787ff3d9e..c8092b151 100644 --- a/src/d_clisrv.c +++ b/src/d_clisrv.c @@ -219,31 +219,6 @@ consvar_t cv_httpsource = CVAR_INIT ("http_source", "", CV_SAVE, NULL, NULL); consvar_t cv_kicktime = CVAR_INIT ("kicktime", "10", CV_SAVE, CV_Unsigned, NULL); -// https://github.com/jameds/holepunch/blob/master/holepunch.c#L75 -static int IsExternalAddress (const void *p) -{ - const int a = ((const unsigned char*)p)[0]; - const int b = ((const unsigned char*)p)[1]; - - if (*(const int*)p == ~0)/* 255.255.255.255 */ - return 0; - - switch (a) - { - case 0: - case 10: - case 127: - return 0; - case 172: - return (b & ~15) != 16;/* 16 - 31 */ - case 192: - return b != 168; - default: - return 1; - } -} - - // Generate a message for an authenticating client to sign, with some guarantees about who we are. void GenerateChallenge(uint8_t *buf) { @@ -292,7 +267,7 @@ shouldsign_t ShouldSignChallenge(uint8_t *message) if ((max(now, then) - min(now, then)) > 60*5) return SIGN_BADTIME; - if (realIP != claimedIP && IsExternalAddress(&realIP)) + if (realIP != claimedIP && I_IsExternalAddress(&realIP)) return SIGN_BADIP; return SIGN_OK; diff --git a/src/d_net.c b/src/d_net.c index effc48eb3..917b181fe 100644 --- a/src/d_net.c +++ b/src/d_net.c @@ -93,6 +93,7 @@ boolean (*I_SetBanAddress) (const char *address, const char *mask) = NULL; boolean (*I_SetBanUsername) (const char *username) = NULL; boolean (*I_SetBanReason) (const char *reason) = NULL; boolean (*I_SetUnbanTime) (time_t timestamp) = NULL; +boolean (*I_IsExternalAddress) (const void *p) = NULL; bannednode_t *bannednode = NULL; diff --git a/src/i_net.h b/src/i_net.h index 9433b89fb..5755f6169 100644 --- a/src/i_net.h +++ b/src/i_net.h @@ -176,6 +176,7 @@ extern boolean (*I_SetBanAddress) (const char *address,const char *mask); extern boolean (*I_SetBanUsername) (const char *username); extern boolean (*I_SetBanReason) (const char *reason); extern boolean (*I_SetUnbanTime) (time_t timestamp); +extern boolean (*I_IsExternalAddress) (const void *p); struct bannednode_t { diff --git a/src/i_tcp.c b/src/i_tcp.c index eb4521928..18e79fbd6 100644 --- a/src/i_tcp.c +++ b/src/i_tcp.c @@ -1522,6 +1522,31 @@ static void SOCK_ClearBans(void) banned = NULL; } +// https://github.com/jameds/holepunch/blob/master/holepunch.c#L75 +static int SOCK_IsExternalAddress (const void *p) +{ + const int a = ((const unsigned char*)p)[0]; + const int b = ((const unsigned char*)p)[1]; + + if (*(const UINT32*)p == (UINT32)~0)/* 255.255.255.255 */ + return 0; + + switch (a) + { + case 0: + case 10: + case 127: + return 0; + case 172: + return (b & ~15) != 16;/* 16 - 31 */ + case 192: + return b != 168; + default: + return 1; + } +} + + boolean I_InitTcpNetwork(void) { char serverhostname[255]; @@ -1622,6 +1647,8 @@ boolean I_InitTcpNetwork(void) I_SetBanUsername = SOCK_SetBanUsername; I_SetBanReason = SOCK_SetBanReason; I_SetUnbanTime = SOCK_SetUnbanTime; + I_IsExternalAddress = SOCK_IsExternalAddress; + bannednode = SOCK_bannednode; return ret;