SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 06/09/2014, 06:07 PM   #1
beckzy
Beta Tester
 
Join Date: Dec 2006
Posts: 1,276
Reputation: 172
Default Check if string contains an IP address

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?
beckzy is offline   Reply With Quote
Old 06/09/2014, 06:52 PM   #2
Evocator
Huge Clucker
 
Join Date: Nov 2013
Posts: 464
Reputation: 158
Default Re: Check if string contains an IP address

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.
Evocator is offline   Reply With Quote
Old 06/09/2014, 07:04 PM   #3
beckzy
Beta Tester
 
Join Date: Dec 2006
Posts: 1,276
Reputation: 172
Default Re: Check if string contains an IP address

That doesn't work properly. It will detect (for example) "13.123.211:5.35" as being an IP address when it isn't.
beckzy is offline   Reply With Quote
Old 06/09/2014, 07:55 PM   #4
Stinged
High-roller
 
Join Date: Feb 2013
Posts: 1,571
Reputation: 187
Default Re: Check if string contains an IP address

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.
Stinged is offline   Reply With Quote
Old 06/09/2014, 08:46 PM   #5
beckzy
Beta Tester
 
Join Date: Dec 2006
Posts: 1,276
Reputation: 172
Default Re: Check if string contains an IP address

There must be a way to check if a string contains a VALID IP address?
beckzy is offline   Reply With Quote
Old 06/09/2014, 09:15 PM   #6
silenthill
Huge Clucker
 
silenthill's Avatar
 
Join Date: Nov 2011
Posts: 342
Reputation: 21
Default Re: Check if string contains an IP address

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;
}
silenthill is offline   Reply With Quote
Old 06/09/2014, 09:46 PM   #7
beckzy
Beta Tester
 
Join Date: Dec 2006
Posts: 1,276
Reputation: 172
Default Re: Check if string contains an IP address

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.
beckzy is offline   Reply With Quote
Old 06/09/2014, 09:53 PM   #8
Isolated
Gangsta
 
Isolated's Avatar
 
Join Date: Mar 2013
Location: United Kingdom
Posts: 801
Reputation: 155
Default Re: Check if string contains an IP address

check if the dotcount = 2.

I'd also recommend checking to see if there is 3 sections of numbers, and also check for :
Isolated is offline   Reply With Quote
Old 06/09/2014, 10:02 PM   #9
Vince
Spam Machine
 
Vince's Avatar
 
Join Date: Sep 2007
Location: Belgium
Posts: 10,067
Reputation: 2660
Default Re: Check if string contains an IP address

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]}"))
__________________
Vince is offline   Reply With Quote
Old 06/09/2014, 10:07 PM   #10
beckzy
Beta Tester
 
Join Date: Dec 2006
Posts: 1,276
Reputation: 172
Default Re: Check if string contains an IP address

****** told me not to use the regex plugin. He never gave me an alternative option though.
beckzy is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
String check Jefff Scripting Help 3 10/05/2013 02:00 PM
Check users under an IP Address Neil. Scripting Help 5 17/02/2013 05:34 PM
check for a exact string Jstylezzz Scripting Help 4 14/10/2012 12:08 PM
How to check if a string contains spaces? Superthijs Scripting Help 7 04/02/2012 11:15 AM
Dialog string check Hipflop Help Archive 5 09/07/2011 06:14 PM


All times are GMT. The time now is 05:15 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.