Raised This Month: $51 Target: $400
 12% 

Need help comparing IPs


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
jeezybox
Junior Member
Join Date: Dec 2019
Old 12-12-2019 , 18:11   Need help comparing IPs
Reply With Quote #1

Hello, basically I am having issues comparing IPs.

I am trying to create a plugin, to detect players who play on same IP and different teams. (LAN detection if you can call that).

My logic is as follows:

Player 1 and player 2 have same IP and they are on different team, I use an inner loop to check player 1 with all other players, then incrementing the first variable and re-checking with all the players again. But for some reason it is not working, any idea, I am new to pawn, couldn't find an include to compare IPs or I am not even sure whether it is comparing them right.

Code:
#include <sourcemod>
#include <sdktools>
#include <cstrike>


public OnPluginStart()
{
	CreateConVar("Sm KAMERA", "1.0", "test");
	RegConsoleCmd("SM_kamera", CmdIp);
	LoadTranslations("common.phrases");
}
public Action:CmdIp(client, args)
{
	if(!IsClientInGame(client))
		return Plugin_Continue;

	new ilee = 0;
	PrintToConsole(client, "Cam:");
	for (new i = 1; i <= GetMaxClients(); i++)
	{
		if(IsClientInGame(i))
		{
			new String:clientname[32];
			new String:clientip[32];
			GetClientName(i, clientname, 32);
			GetClientIP(i, clientip, 32);
			
			ilee = i;
			
			for(new c = 1; c <= GetMaxClients(); c++){
				new String:clientname2[32];
				new String:clientip2[32];
				GetClientName(c, clientname2, 32);
				
				if(strcmp(clientname[i], clientname2[c]) != 0)
				{
					GetClientIP(c, clientip2, 32);					
					if(strcmp(clientip, clientip2, true) == 0)
					{
						if(GetClientTeam(i) != GetClientTeam(c))
						{
							PrintToConsole(client, "%d) %s (%s) cam with %s (%s)", c, clientname2, clientip2, clientname, clientip);
						}
					}
			}	
		}
	}
	

	PrintToConsole(client, "players: %d.", ilee);
	return Plugin_Handled;
}
jeezybox is offline
Chrissy
Member
Join Date: May 2013
Old 12-12-2019 , 18:34   Re: Need help comparing IPs
Reply With Quote #2

Try:
Code:
StrEqual(IP1, IP2)

Last edited by Chrissy; 12-12-2019 at 18:34.
Chrissy is offline
jeezybox
Junior Member
Join Date: Dec 2019
Old 12-13-2019 , 07:32   Re: Need help comparing IPs
Reply With Quote #3

It is not working for some reason.
jeezybox is offline
Balimbanana
Member
Join Date: Jan 2017
Old 12-13-2019 , 16:59   Re: Need help comparing IPs
Reply With Quote #4

Well, I am not entirely sure if this is quite what you are looking for, but you could do it in an array with duplicate checks like this:
Code:
public Action CmdIp(int client, int args)
{
	Handle cliparr = CreateArray(MAXPLAYERS+1);
	Handle clarr = CreateArray(MAXPLAYERS+1);
	for (int i = 1;i<MaxClients+1;i++)
	{
		if (IsValidEntity(i))
		{
			if (IsClientConnected(i))
			{
				char curip[32];
				GetClientIP(i,curip,sizeof(curip));
				int finddupe = FindStringInArray(cliparr,curip);
				if (finddupe == -1)
				{
					PushArrayString(cliparr,curip);
					PushArrayCell(clarr,i);
				}
				else
				{
					int lastcl = GetArrayCell(clarr,finddupe);
					PrintToConsole(client,"%N and %N are on the same IP %s",i,lastcl,curip);
				}
			}
		}
	}
	PrintToConsole(client,"%i IPs checked.",GetArraySize(cliparr));
	CloseHandle(clarr);
	CloseHandle(cliparr);
	return Plugin_Handled;
}
With using 7 bots, and 2 clients on same IP, adding 3 bots after client joins, the output is:
Code:
Bot02 and Bot01 are on the same IP 127.0.0.1
Bot03 and Bot01 are on the same IP 127.0.0.1
Bot04 and Bot01 are on the same IP 127.0.0.1
Balimbanana2 and Balimbanana are on the same IP 192.168.0.#
Bot05 and Bot01 are on the same IP 127.0.0.1
Bot06 and Bot01 are on the same IP 127.0.0.1
Bot07 and Bot01 are on the same IP 127.0.0.1
2 IPs checked.
Edit: In this example, there are no team checks, but the ID's you would be checking are variables i and lastcl.
So you could add the if (GetClientTeam(i) != GetClientTeam(lastcl)) and run more commands from there.

Last edited by Balimbanana; 12-13-2019 at 17:56.
Balimbanana is offline
jeezybox
Junior Member
Join Date: Dec 2019
Old 12-13-2019 , 21:46   Re: Need help comparing IPs
Reply With Quote #5

Quote:
Originally Posted by Balimbanana View Post
Well, I am not entirely sure if this is quite what you are looking for, but you could do it in an array with duplicate checks like this:
Code:
public Action CmdIp(int client, int args)
{
	Handle cliparr = CreateArray(MAXPLAYERS+1);
	Handle clarr = CreateArray(MAXPLAYERS+1);
	for (int i = 1;i<MaxClients+1;i++)
	{
		if (IsValidEntity(i))
		{
			if (IsClientConnected(i))
			{
				char curip[32];
				GetClientIP(i,curip,sizeof(curip));
				int finddupe = FindStringInArray(cliparr,curip);
				if (finddupe == -1)
				{
					PushArrayString(cliparr,curip);
					PushArrayCell(clarr,i);
				}
				else
				{
					int lastcl = GetArrayCell(clarr,finddupe);
					PrintToConsole(client,"%N and %N are on the same IP %s",i,lastcl,curip);
				}
			}
		}
	}
	PrintToConsole(client,"%i IPs checked.",GetArraySize(cliparr));
	CloseHandle(clarr);
	CloseHandle(cliparr);
	return Plugin_Handled;
}
With using 7 bots, and 2 clients on same IP, adding 3 bots after client joins, the output is:
Code:
Bot02 and Bot01 are on the same IP 127.0.0.1
Bot03 and Bot01 are on the same IP 127.0.0.1
Bot04 and Bot01 are on the same IP 127.0.0.1
Balimbanana2 and Balimbanana are on the same IP 192.168.0.#
Bot05 and Bot01 are on the same IP 127.0.0.1
Bot06 and Bot01 are on the same IP 127.0.0.1
Bot07 and Bot01 are on the same IP 127.0.0.1
2 IPs checked.
Edit: In this example, there are no team checks, but the ID's you would be checking are variables i and lastcl.
So you could add the if (GetClientTeam(i) != GetClientTeam(lastcl)) and run more commands from there.
I'll try be more specific. I didn't test this yet, but I want to ask, does this check all players IP and compare to each other or just my (client) IP gets compared to the rest of the players.

What I wanted to do is, compare for example every T side player, with CT site players and if they have the same IP in different teams, it should print on console.
jeezybox is offline
Balimbanana
Member
Join Date: Jan 2017
Old 12-14-2019 , 00:17   Re: Need help comparing IPs
Reply With Quote #6

This will run through all players and check what IP's each of them have, and if they have the same one, it prints.
Here is an example with teams:
Code:
public Action CmdIp(int client, int args)
{
	Handle cliparr = CreateArray(MAXPLAYERS+1);
	Handle clarr = CreateArray(MAXPLAYERS+1);
	for (int i = 1;i<MaxClients+1;i++)
	{
		if (IsValidEntity(i))
		{
			if (IsClientConnected(i))
			{
				char curip[32];
				GetClientIP(i,curip,sizeof(curip));
				int finddupe = FindStringInArray(cliparr,curip);
				if (finddupe == -1)
				{
					PushArrayString(cliparr,curip);
					PushArrayCell(clarr,i);
				}
				else
				{
					int lastcl = GetArrayCell(clarr,finddupe);
					if (GetClientTeam(i) != GetClientTeam(lastcl))
					{
						PrintToConsole(client,"%N and %N are on the same IP %s and they are on different teams.",i,lastcl,curip);
						ChangeClientTeam(i,GetClientTeam(lastcl));
					}
					else PrintToConsole(client,"%N and %N are on the same IP %s and same team.",i,lastcl,curip);
				}
			}
		}
	}
	PrintToConsole(client,"%i IPs checked.",GetArraySize(cliparr));
	CloseHandle(clarr);
	CloseHandle(cliparr);
	return Plugin_Handled;
}
Having 6 bots, and 2 of them (Bot04 and Bot06) are on the other team, this is the output:
Code:
Bot02 and Bot01 are on the same IP 127.0.0.1 and same team.
Bot03 and Bot01 are on the same IP 127.0.0.1 and same team.
Bot04 and Bot01 are on the same IP 127.0.0.1 and they are on different teams.
"Bot04<5><__BOT__><Unassigned>" joined team "CT"
Bot05 and Bot01 are on the same IP 127.0.0.1 and same team.
Bot06 and Bot01 are on the same IP 127.0.0.1 and they are on different teams.
"Bot06<7><__BOT__><Unassigned>" joined team "CT"
1 IPs checked.
Then running again gives:
Bot02 and Bot01 are on the same IP 127.0.0.1 and same team.
Bot03 and Bot01 are on the same IP 127.0.0.1 and same team.
Bot04 and Bot01 are on the same IP 127.0.0.1 and same team.
Bot05 and Bot01 are on the same IP 127.0.0.1 and same team.
Bot06 and Bot01 are on the same IP 127.0.0.1 and same team.
The IP 127.0.0.1 is the loopback address, basically meaning that it is the server's address. If you are going to be running bots, you would probably want to exclude that IP from checks, or use (!IsFakeClient(i))
As of current, it just checks whichever team was found first in the array of players on the same team. So if Bot01 was on CT, the next checks would be whether or not Bot02 through 6 are on CT.

This will automatically add them to the team of the first found duplicate IP.
Balimbanana is offline
MAGNAT2645
Senior Member
Join Date: Nov 2015
Location: AlliedMods.net
Old 12-14-2019 , 02:28   Re: Need help comparing IPs
Reply With Quote #7

Quote:
Originally Posted by Chrissy View Post
Try:
Code:
StrEqual(IP1, IP2)
It's same as strcmp == 0, because this stock function is actually a wrapper for strcmp.

Code:
stock bool StrEqual(const char[] str1, const char[] str2, bool caseSensitive=true)
{
	return (strcmp(str1, str2, caseSensitive) == 0);
}
__________________
MAGNAT2645 is offline
jeezybox
Junior Member
Join Date: Dec 2019
Old 12-14-2019 , 11:13   Re: Need help comparing IPs
Reply With Quote #8

Quote:
Originally Posted by Balimbanana View Post
This will run through all players and check what IP's each of them have, and if they have the same one, it prints.
Here is an example with teams:
Code:
public Action CmdIp(int client, int args)
{
	Handle cliparr = CreateArray(MAXPLAYERS+1);
	Handle clarr = CreateArray(MAXPLAYERS+1);
	for (int i = 1;i<MaxClients+1;i++)
	{
		if (IsValidEntity(i))
		{
			if (IsClientConnected(i))
			{
				char curip[32];
				GetClientIP(i,curip,sizeof(curip));
				int finddupe = FindStringInArray(cliparr,curip);
				if (finddupe == -1)
				{
					PushArrayString(cliparr,curip);
					PushArrayCell(clarr,i);
				}
				else
				{
					int lastcl = GetArrayCell(clarr,finddupe);
					if (GetClientTeam(i) != GetClientTeam(lastcl))
					{
						PrintToConsole(client,"%N and %N are on the same IP %s and they are on different teams.",i,lastcl,curip);
						ChangeClientTeam(i,GetClientTeam(lastcl));
					}
					else PrintToConsole(client,"%N and %N are on the same IP %s and same team.",i,lastcl,curip);
				}
			}
		}
	}
	PrintToConsole(client,"%i IPs checked.",GetArraySize(cliparr));
	CloseHandle(clarr);
	CloseHandle(cliparr);
	return Plugin_Handled;
}
Having 6 bots, and 2 of them (Bot04 and Bot06) are on the other team, this is the output:
Code:
Bot02 and Bot01 are on the same IP 127.0.0.1 and same team.
Bot03 and Bot01 are on the same IP 127.0.0.1 and same team.
Bot04 and Bot01 are on the same IP 127.0.0.1 and they are on different teams.
"Bot04<5><__BOT__><Unassigned>" joined team "CT"
Bot05 and Bot01 are on the same IP 127.0.0.1 and same team.
Bot06 and Bot01 are on the same IP 127.0.0.1 and they are on different teams.
"Bot06<7><__BOT__><Unassigned>" joined team "CT"
1 IPs checked.
Then running again gives:
Bot02 and Bot01 are on the same IP 127.0.0.1 and same team.
Bot03 and Bot01 are on the same IP 127.0.0.1 and same team.
Bot04 and Bot01 are on the same IP 127.0.0.1 and same team.
Bot05 and Bot01 are on the same IP 127.0.0.1 and same team.
Bot06 and Bot01 are on the same IP 127.0.0.1 and same team.
The IP 127.0.0.1 is the loopback address, basically meaning that it is the server's address. If you are going to be running bots, you would probably want to exclude that IP from checks, or use (!IsFakeClient(i))
As of current, it just checks whichever team was found first in the array of players on the same team. So if Bot01 was on CT, the next checks would be whether or not Bot02 through 6 are on CT.

This will automatically add them to the team of the first found duplicate IP.
Okay, this works perfectly as I described, thanks a lot for the help.
jeezybox is offline
Reply



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

Forum Jump


All times are GMT -4. The time now is 18:28.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode