Check if string contains an IP address
#1

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?
Reply
#2

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.
Reply
#3

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

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.
Reply
#5

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

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;
}
Reply
#7

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.
Reply
#8

check if the dotcount = 2.

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

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]}"))
Reply
#10

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


Forum Jump:


Users browsing this thread: 1 Guest(s)