• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Pawn] how to get the last digit of a player's IP
#13
Both IP_LAN and IP_PUBLIC represent the "Player's IP's". IP_LAN from a player who connects through LAN, and IP_PUBLIC from someone who connects from a remote system. (Thus both IP_LAN and IP_PUBLIC represent an IP which was received by using GetPlayerIP()).
To clarify, your whole script would look like (with some extra added comments):

Code:
forward httpResponse(playerid, response_code, data[]); //Bad naming, by the way. Something like "OnVPNLookup" would be better
public httpResponse(playerid, response_code, data[])
{
    new
        name[MAX_PLAYER_NAME  1], //name[MAX_PLAYERS]  ==> name[MAX_PLAYER_NAME  1] :: Sure that was a mistake. " 1" for the null terminator
        string[192],
        ip[16],
        ip_part[4]
    ;
    GetPlayerName(playerid, name, MAX_PLAYER_NAME);
    GetPlayerIp(playerid, ip, sizeof(ip));
    sscanf(ip, "p<.>dddd", ip_part[0], ip_part[1], ip_part[2], ip_part[3]);
    
    if (ip_part[0] == 192 && ip_part[1] == 168 && ip_part[2] == 1 && (1 <= ip_part[3] <= 255))
    {
        printf("%s(%d) IP(%s) is a LAN type, therefore able to enter the server.", name, playerid, ip);
        return; //Just 'return;': There is no need to return a value. No function is directly calling httpResponse, so that value is sent to nowhere.
    }
    // If the site sends an OK message
    if(response_code == 200)
    {
        new server_response[2];
        if (sscanf(data, "s[2]", server_response)) //Failsafe check 1
        {
            printf("ERROR: sscanf() failed to unformat data[] in httpResponse(). Cannot check VPN status for player %s(%d) IP: %s. Returned data[]=%s", name, playerid, ip, data);
            return;
        }
        // If a player is using VPN
        if(!strcmp(server_response, "Y"))
        {
            format(string, sizeof(string), "ANTI VPN: %s(%d) has been kicked from the server due to VPN usage.", name, playerid);
            SendClientMessageToAll(COLOR_RED, string);
            printf("%s(%d) IP(%s) is not legit, therefore not able to enter the server.", name, playerid, ip);
            //SetTimerEx("DelayKick", 100, false, "i", playerid);
        }
        else if (!strcmp(server_response, "N"))
        {
            printf("%s(%d) IP(%s) is legit, therefore able to enter the server.", name, playerid, ip);
        }
        else //Failsafe, should not trigger
        {
            printf("ERROR: Invalid response from server in httpResponse(). Unformatted response is: %s", server_response);
        }
    }
    else
    {
        printf("ERROR: Failed to perform VPN check on %s(%d). Server responded with: %d", name, playerid, response_code);
    }
}

This time I did not check the code though. Merely added a few comments to point out a thing or two (and another example of sscanf(), just for the sake of showing how simple the function can be).
The point is, that first ip check should do the trick (note that it still doesn't check for "127.0.0.1" in this code, since that's not what you're having issues with).


EDIT:
You editted your post:
Quote:what you've done so far is compare a specific ip with another specific ip to see if they match which is not what i really want.
Well, I was already thinking you'd think something like that. See begin of post (the part where those variables with the IPs are imitating someone's "real" IP). The comparisment code(s) I sent were mere examples.

EDIT 2:
Quote:another idea i just thought of while making this edit is to somehow use range values like in Kotlin where you simply do: 1..10
You pretty much can:
Code:
if (1 <= VARIABLE <= 10) //Same as "1..10" (doesn't work PAWN tho) or, also the same as "VARIABLE >= 1 && VARIABLE <= 10" (does work PAWN (it's a basic statement - just like the "1 <= VARIABLE <= 10"))

EDIT 3:
Created this function to make things even easier (NOTE: checks if an IP is >ANY< LAN IP, not just 192.168.1.{0...255}
Code:
/*
IsLocalIP(const ip[]):
  input (string): An IPv4 address
  outputs:
    true: Given IPv4 address is a local address
    false: Given IPv4 is not a local address (either a remote address or an invalid IPv4 address)
*/
bool:IsLocalIP(const ip[]) //Got the LAN IPs information from: https://en.wikipedia.org/wiki/Private_network
{
    new ip_part[4];
    if (sscanf(ip, "p<.>dddd", ip_part[0], ip_part[1], ip_part[2], ip_part[3]))
    {
        return false;
    }
    if (ip_part[0] == 127 && ip_part[1] == 0 && ip_part[2] == 0 && ip_part[3] == 1)
    {
        return true;
    }
    if ((ip_part[0] == 10) && (0 <= ip_part[1] <= 255) && (0 <= ip_part[2] <= 255) && (0 <= ip_part[3] <= 255))
    {
        return true;
    }
    if ((ip_part[0] == 172) && (16 <= ip_part[1] <= 31) && (0 <= ip_part[2] <= 255) && (0 <= ip_part[3] <= 255))
    {
        return true;
    }
    if ((ip_part[0] == 192) && (ip_part[1] == 168) && (0 <= ip_part[2] <= 255) && (0 <= ip_part[3] <= 255))
    {
        return true;
    }
    return false;
}

//EXAMPLE USAGE:
hook OnPlayerConnect(playerid)
{
    new
        name[MAX_PLAYER_NAME  1]
        ip[16]
    ;
    GetPlayerName(playerid, name, MAX_PLAYER_NAME);
    GetPlayerIp(playerid, ip, 16);

    if (IsLocalIP(ip))
    {
        printf(">> %s(%i) is connected via localhost or LAN!", name, playerid);
    }
    else
    {
        printf(">> %s(%i) is remotely connected to the server", name, playerid);
    }
    return 1;
}
  Reply


Messages In This Thread
RE: how to get the last digit of a player's IP - by Kwarde - 2021-07-16, 07:36 PM

Forum Jump: