[Tutorial] Extracting Playerid from IP
#1

Extracting Playerid from IP

What is this??
A small script that returns the playerid from the given IP.
Very simple but there are few who don't get it.

Why this tutorial??
One n00b PMed me asking how to get playerid on OnRconLoginAttempt(ip[],password[],success)

What you need??
The PAWNO!!

How to get the playerid form IP??
I don't know weather there is much more optimized way for this.But I do it by this.

In this small tutorial, all my code are put under OnRconLoginAttempt Callback.

As there is no native function for finding the playerid from his IP.We'll have to write our own.
This works on loops.You keep trying to match the every players IP with the given IP(As the parameter).

Step 1:Create a loop
Create a loop such that the code in the for blocks are executed for all players.i.e From 0 to MAX_PLAYERS

Код:
public OnRconLoginAttempt(ip[], password[], success)
{
	for(new i = 0; i < MAX_PLAYERS; i++)
	{
		
	}
	return 1;
}
Step 2:Check Player's Connection
We'll check if the Player is connected or not then check for his IP instead of wasting CPU.
The function "IsPlayerConnected(playerid)" will tell us if the player is connected.So in an applicable code, it would look similar to this.

Код:
if(IsPlayerConnected(playerid) 
{
     //PLAYER IS CONNECTED - GO CHECK HIS IP
}
else
{
   //NO PLAYER SO CONTINUE THE LOOP
}
After adding ^^(this) the code would look like
Код:
public OnRconLoginAttempt(ip[], password[], success)
{
	for(new i = 0; i < MAX_PLAYERS; i++)
	{
		if(IsPlayerConnected(i))
		{
  			
		}
	}
	return 1;
}
Step 3:Check players IP
Now, we have to check the players IP with the given IP for match.If it matches ,that was the player who attempted the RCON Login or else we have to continue the loop checking other players.

For checking the players IP, we need to get the players IP first.For that we must use "GetPlayerIp(playerid,dest,length);"

The code would look like this:
Код:
new playerIP[16];
GetPlayerIp(i,playerIP,sizeof(playerIP));
To check the player IP and the given we must use "strcmp" as they are strings.
This will check the strings and tell us if they match or not
Код:
if(!strcmp(pIP,ip,true))
{
	//THEY MATCH - SO THIS IS THE PLAYER		    
}
else
{
       //THEY DONT - NOT THIS PLAYER
}
Adding it to the main code:
Код:
public OnRconLoginAttempt(ip[], password[], success)
{
        new playerIP[16];
	for(new i = 0; i < MAX_PLAYERS; i++)
	{
		if(IsPlayerConnected(i))
		{
			GetPlayerIp(i, playerIP,sizeof(playerIP));
			if(!strcmp(playerIP,ip,true))
			{
			    //THE CODE HERE WILL BE EXECUTED IF THE PLAYER IS FOUND
			}
		}
	}
	return 1;
}
Step 4:The final makeup
From the above code you'll get to know the player who attempted the RCON Login.But there is nothing to do.
This final code will deal with the player weather to he his attempt was success or not.
The "success" parameter tells if the password entered by the player was correct or not.
If success is == 1 then it was correct else it was wrong.

Код:
if(success)
{
	SendClientMessage(i,COLOR_GREEN,"You have been logged in as RCON Administrator");
}
else
{
	 Kick(i);
}
Putting this last peice to the main code the final code would be:
Код:
public OnRconLoginAttempt(ip[], password[], success)
{
        new pIP[16];
	for(new i = 0; i < MAX_PLAYERS; i++) //Loop to check the IP of very player who is connected
	{
		if(IsPlayerConnected(i)) //Checks if the player is connected
		{
			GetPlayerIp(i, pIP, 16); //Gets the players IP
			if(!strcmp(pIP,ip,true))  //Checks if the players IP matches
			{
			    if(success)
			    {
					SendClientMessage(i,COLOR_GREEN,"You have logged in as RCON Administrator");
                                        //Player entered the correct RCON password

				}
				else
				{
                                   //Player entered the wrong password hence he gets kicked
				    Kick(i);
				}
			}
		}
	}
	return 1;
}
Final Code
Код:
public OnRconLoginAttempt(ip[], password[], success)
{
        new pIP[16];
	for(new i = 0; i < MAX_PLAYERS; i++)
	{
		if(IsPlayerConnected(i))
		{
			GetPlayerIp(i, pIP, 16);
			if(!strcmp(pIP,ip,true))
			{
			    if(success)
			    {
					SendClientMessage(i,COLOR_GREEN,"You have logged in as RCON Administrator");

				}
				else
				{
				    Kick(i);
				}
			}
		}
	}
	return 1;
}
Bugs && Notes:
Optimized:The variable declaration was which was inside thefor loop is moved outside the loop since it created unnecessary extra variables.
Reply
#2

What if there are 3 players online, all of them with the same IP? If one of those players attempt to log into RCON, all 3 will be kicked.
Reply
#3

Can't help the callback doesn't give sufficient information.
In case the "/rcon" command entered to sent to OnPlayerCommandText() or OnPlayerText then we can do it.But I think it doesn't.

Was checking but no reference is given whether RCON Commands are sent to OnPlayerCommandText but what I explained is done in the documentation.

https://sampwiki.blast.hk/wiki/OnRconLoginAttempt
Reply
#4

Quote:
Originally Posted by Emmet_
Посмотреть сообщение
What if there are 3 players online, all of them with the same IP? If one of those players attempt to log into RCON, all 3 will be kicked.
What if there are 3 players online with the same IP and one of them is hacking? Would you ban the IP?

On topic:
It's all good except for the fact that the variable is created inside of the loop. You should create the variable outside of the loop (so that it doesn't get created several times) and then everything is all good.
Reply
#5

You will face a problem since alot of people "Owners" actually have a dynamic Ip which means every restart to the router the ip will change so this will totally fail.
Reply
#6

Quote:
Originally Posted by SuperViper
Посмотреть сообщение
What if there are 3 players online with the same IP and one of them is hacking? Would you ban the IP?

On topic:
It's all good except for the fact that the variable is created inside of the loop. You should create the variable outside of the loop (so that it doesn't get created several times) and then everything is all good.
Oops!!!In my scripts I have put it outside but in the tut I have put it inside the loop!!!
Fixed!!!
Thanks!!
Reply
#7

Quote:
Originally Posted by [GF]Logic
Посмотреть сообщение
You will face a problem since alot of people "Owners" actually have a dynamic Ip which means every restart to the router the ip will change so this will totally fail.
If they have dynamic IP, that IP will be taken and code should work(Very less probability of address change while we were inside the loop).We are not using that IP for the whole time so it might create a problem rarely.And if you know a solution please tell.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)