SA-MP Forums Archive
How to detect port - 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: How to detect port (/showthread.php?tid=619539)



How to detect port - NeXoR - 18.10.2016

Hey guys, I am trying to make an Anti Advert, now this is my code:
PHP код:
IsValidIP(ip[])
{
    new 
num1, num2, num3, num4;
    
sscanf(ip, "p<.>iiii", num1, num2, num3, num4);
    if(
num1 > 255 || num2 > 255 || num3 > 255 || num4 > 255) return false;
    if(
num1 < 0 || num2 < 0 || num3 < 0 || num4 < 0) return false;
    return 
true;
} 
How can I detect the port ? (:XXXX)


Re: How to detect port - SickAttack - 19.10.2016

That's obviously going to fail, it isn't that simple as you could simply put characters in between and it won't detect an "IP". Detecting the port isn't necessary, however.

Here's a function I have, it could probably be improved, but it's just fine how it is (note that it's strict). Regex seems to be a lot slower either way.

PHP код:
// ** INCLUDES
#include <a_samp>
#include <sscanf>
// ** DEFINES
// *** FUNCTIONS
#define strcpy(%0,%1,%2) strcat((%0[0] = '\0', %0), %1, %2)
#define isnull(%1) ((!(%1[0])) || (((%1[0]) == '\1') && (!(%1[1]))))
// ** MAIN
main()
{
    print(
"Loaded \"anti_advert.amx\".");
}
// ** CALLBACKS
public OnGameModeInit()
{    
    return 
1;
}
public 
OnGameModeExit()
{
    return 
1;
}
public 
OnPlayerText(playerid, text[])
{
    if(
IsAdvertisement(text))
    {
        
SendClientMessage(playerid, 0xFFFFFFFF, "{FF0000}[ERROR]: {FFFFFF}Your message contains an IP Address (strict detection).");
        return 
0;
    }
    return 
1;
}
// ** FUNCTIONS
stock IsAdvertisement(text[])
{
    new 
message[128], extract[2], element[4][4], count_1, count_2, temp, bool:number_next = false, bool:next_number = false;
    
strcpy(message, text, sizeof(message));
    for(new 
i = 0, j = strlen(message); i < j; i ++)
    {
        switch(
message[i])
        {
            case 
'0'..'9':
            {
                if(
next_number)
                {
                    continue;
                }
                
number_next = false;
                
strmid(extract, message[i], 0, 1);
                
strcat(element[count_1], extract);
                
                
count_2 ++;
                if(
count_2 == 3 || message[i + 1] == EOS)
                {
                    
strmid(extract, message[i + 1], 0, 1);
                    if(
IsNumeric(extract))
                    {
                        
element[0][0] = EOS;
                        
element[1][0] = EOS;
                        
element[2][0] = EOS;
                        
element[3][0] = EOS;
                        
count_1 = 0;
                        
count_2 = 0;
                        
next_number = true;
                        continue;
                    }
                    
temp = strval(element[count_1]);
                    if(
count_1 == 0)
                    {
                        if(
temp <= 255)
                        {
                            
count_1 ++;
                            
count_2 = 0;
                        }
                        else
                        {
                            
element[count_1][0] = EOS;
                            
count_2 = 0;
                            
next_number = true;
                        }
                    }
                    else
                    {
                        if(
temp <= 255)
                        {
                            
count_1 ++;
                            
count_2 = 0;
                        }
                        else
                        {
                            
element[0][0] = EOS;
                            
element[1][0] = EOS;
                            
element[2][0] = EOS;
                            
element[3][0] = EOS;
                            
count_1 = 0;
                            
count_2 = 0;
                            
next_number = true;
                        }
                    }
                }
                if(
count_1 == 4)
                {
                    return 
1;
                }
            }
            default:
            {
                
next_number = false;
                if(
number_next)
                {
                    continue;
                }
                if(!
isnull(element[count_1]))
                {
                    
temp = strval(element[count_1]);
                    if(
count_1 == 0)
                    {
                        if(
temp <= 255)
                        {
                            
count_1 ++;
                            
count_2 = 0;
                            
number_next = true;
                        }
                        else
                        {
                            
element[count_1][0] = EOS;
                            
count_2 = 0;
                        }
                    }
                    else
                    {
                        if(
temp <= 255)
                        {
                            
count_1 ++;
                            
count_2 = 0;
                            
number_next = true;
                        }
                        else
                        {
                            
element[0][0] = EOS;
                            
element[1][0] = EOS;
                            
element[2][0] = EOS;
                            
element[3][0] = EOS;
                            
count_1 = 0;
                            
count_2 = 0;
                        }
                    }
                    if(
count_1 == 4)
                    {
                        return 
1;
                    }
                }
            }
        }
    }
    return 
0;
}
stock IsNumeric(const string[])
{
    return !
sscanf(string, "{d}");
} 
Regex (also strict):

PHP код:
// ** INCLUDES
#include <a_samp>
#include <regex>
// ** DEFINES
// *** FUNCTIONS
#define strcpy(%0,%1,%2) strcat((%0[0] = '\0', %0), %1, %2)
// ** MAIN
main()
{
    print(
"Loaded \"anti_advert_regex.amx\".");
}
// ** CALLBACKS
public OnGameModeInit()
{    
    return 
1;
}
public 
OnGameModeExit()
{
    return 
1;
}
public 
OnPlayerText(playerid, text[])
{
    if(
IsAdvertisement(text))
    {
        
SendClientMessage(playerid, 0xFFFFFFFF, "{FF0000}[ERROR]: {FFFFFF}Your message contains an IP Address (strict detection).");
        return 
0;
    }
    return 
1;
}
// ** FUNCTIONS
stock IsAdvertisement(text[])
{
    new 
message[128], build, expression[] = "(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.+){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)", start, end;
    
strcpy(message, text, sizeof(message));
    for(new 
i = 0, j = strlen(message); i < j; i ++)
    {
        switch(
message[i])
        {
            case 
'0'..'9':
            {
                continue;
            }
            case 
'.':
            {
                continue;
            }
            default:
            {
                
strdel(message, i, i + 1);
                
strins(message, ".", i);
            }
        }
    }
    
build = regex_exbuild(expression);
    
regex_exmatch(message, build);
    
regex_exsearch(message, build, start, end);
    if(
start >= 0)
    {
        return 
1;
    }
    return 
0;
}