SA-MP Forums Archive
Check if string contains an IP address - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Check if string contains an IP address (/showthread.php?tid=535962)



Check if string contains an IP address - beckzy - 06.09.2014

Although I have been developing the same gamemode since 2007, I am actually terrible at maths. This has got me stumped. I need to check if a string contains an IP address. How would I do this? I have found the following:

pawn Code:
#define MAX_NUM 10
#define MY_SERVER_IP "255.255.255.255:7777"
stock ContainsIP(const string[])
{
    new num[MAX_NUM][128];
    new numpos[MAX_NUM];
    new dotcount,i;
    if(strfind(string,MY_SERVER_IP)!=-1)return 0;
    for(new npos=0;npos<=MAX_NUM;npos++)
    {
        new c;
        for(c=0;i<strlen(string);i++)
        {

            new ch = string[i];

            if((string[i] == '.' || string[i] == ':') && ((i>0 && '0' <= string[i-1] <= '9') || (i>1 && '0' <= string[i-2] <= '9') ))
            {
                dotcount++;
                i++;
                break;
            }
            if ((npos==0 || i - numpos[npos - 1] < strlen(num[npos-1])+4) && '0' <= ch <= '9' || (c == 0 && ch == '-'))
            {
                num[npos][c] = string[i];
                numpos[npos] = i;
                c++;
            }


        }

    }
    new found=0;
    for(new npos=0;npos<MAX_NUM;npos++)
    {
        if( strlen(num[npos]) > 0 && 0 < strval(num[npos]) <= 225)
        {
            found++;
        }
    }
    if(found >= 3 && dotcount >= 3)return 1;
    return 0;
}
Should I use that, or does anyone have a better method?


Re: Check if string contains an IP address - Evocator - 06.09.2014

Maybe this could help.

pawn Code:
stock stringContainsIP(string[])
{
    new
        dotCount
    ;
    for(new i; string[i] != EOS; ++i)
    {
        if(('0' <= string[i] <= '9') || string[i] == '.' || string[i] == ':')
        {
            if((string[i] == '.') && (string[i + 1] != '.') && ('0' <= string[i - 1] <= '9'))
            {
                ++dotCount;
            }
            continue;
        }
    }
    return (dotCount > 2);
}
Credits aren't mine tho.


Re: Check if string contains an IP address - beckzy - 06.09.2014

That doesn't work properly. It will detect (for example) "13.123.211:5.35" as being an IP address when it isn't.


Re: Check if string contains an IP address - Stinged - 06.09.2014

You expect that to work 100% correctly?
That looks like an IP, even if it isn't, so it will always detect it as one.
You should just warn the admins that it's possible advertising and stop the detected ip from sending.


Re: Check if string contains an IP address - beckzy - 06.09.2014

There must be a way to check if a string contains a VALID IP address?


Re: Check if string contains an IP address - silenthill - 06.09.2014

try this....Credits aren't mine tho.


pawn Code:
stock detectIP(text[])
{
    new dotCount;
    for(new i; i < strlen(text); ++i)
    {
        if('0' <= text[i] <= '9')
        {
            do
            {
                if(text[i] == '.') ++dotCount;
                ++i;
            }
            while(('0' <= text[i] <= '9') || text[i] == '.' || text[i] == ':');
        }
    }
    if(dotCount >= 3) return true;
    return false;
}



Re: Check if string contains an IP address - beckzy - 06.09.2014

Same problem.

Quote:
Originally Posted by BeckzyBoi
View Post
That doesn't work properly. It will detect (for example) "13.123.211:5.35" as being an IP address when it isn't.



Re: Check if string contains an IP address - Isolated - 06.09.2014

check if the dotcount = 2.

I'd also recommend checking to see if there is 3 sections of numbers, and also check for :


Re: Check if string contains an IP address - Vince - 06.09.2014

For usability, try the regex plugin. Although it may be a bit over the top to use an entire plugin for a function that you'll probably use only once. You can also use sscanf for quick matching, but it won't detect it in the middle of other text;
pawn Code:
if(sscanf(text, "p<.>{a<i>[4]}"))



Re: Check if string contains an IP address - beckzy - 06.09.2014

****** told me not to use the regex plugin. He never gave me an alternative option though.