IsExternalAddress type fixup, move into i_tcp.c / SOCK_ abstraction

This commit is contained in:
AJ Martinez 2023-03-27 23:44:24 -07:00 committed by James R
parent 0df4251bc7
commit e09b21ddae
4 changed files with 30 additions and 26 deletions

View file

@ -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); 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. // Generate a message for an authenticating client to sign, with some guarantees about who we are.
void GenerateChallenge(uint8_t *buf) void GenerateChallenge(uint8_t *buf)
{ {
@ -292,7 +267,7 @@ shouldsign_t ShouldSignChallenge(uint8_t *message)
if ((max(now, then) - min(now, then)) > 60*5) if ((max(now, then) - min(now, then)) > 60*5)
return SIGN_BADTIME; return SIGN_BADTIME;
if (realIP != claimedIP && IsExternalAddress(&realIP)) if (realIP != claimedIP && I_IsExternalAddress(&realIP))
return SIGN_BADIP; return SIGN_BADIP;
return SIGN_OK; return SIGN_OK;

View file

@ -93,6 +93,7 @@ boolean (*I_SetBanAddress) (const char *address, const char *mask) = NULL;
boolean (*I_SetBanUsername) (const char *username) = NULL; boolean (*I_SetBanUsername) (const char *username) = NULL;
boolean (*I_SetBanReason) (const char *reason) = NULL; boolean (*I_SetBanReason) (const char *reason) = NULL;
boolean (*I_SetUnbanTime) (time_t timestamp) = NULL; boolean (*I_SetUnbanTime) (time_t timestamp) = NULL;
boolean (*I_IsExternalAddress) (const void *p) = NULL;
bannednode_t *bannednode = NULL; bannednode_t *bannednode = NULL;

View file

@ -176,6 +176,7 @@ extern boolean (*I_SetBanAddress) (const char *address,const char *mask);
extern boolean (*I_SetBanUsername) (const char *username); extern boolean (*I_SetBanUsername) (const char *username);
extern boolean (*I_SetBanReason) (const char *reason); extern boolean (*I_SetBanReason) (const char *reason);
extern boolean (*I_SetUnbanTime) (time_t timestamp); extern boolean (*I_SetUnbanTime) (time_t timestamp);
extern boolean (*I_IsExternalAddress) (const void *p);
struct bannednode_t struct bannednode_t
{ {

View file

@ -1522,6 +1522,31 @@ static void SOCK_ClearBans(void)
banned = NULL; 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) boolean I_InitTcpNetwork(void)
{ {
char serverhostname[255]; char serverhostname[255];
@ -1622,6 +1647,8 @@ boolean I_InitTcpNetwork(void)
I_SetBanUsername = SOCK_SetBanUsername; I_SetBanUsername = SOCK_SetBanUsername;
I_SetBanReason = SOCK_SetBanReason; I_SetBanReason = SOCK_SetBanReason;
I_SetUnbanTime = SOCK_SetUnbanTime; I_SetUnbanTime = SOCK_SetUnbanTime;
I_IsExternalAddress = SOCK_IsExternalAddress;
bannednode = SOCK_bannednode; bannednode = SOCK_bannednode;
return ret; return ret;