AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Getting the first two octects of an IP address... (https://forums.alliedmods.net/showthread.php?t=85079)

P4rD0nM3 02-05-2009 11:38

Getting the first two octects of an IP address...
 
Hello, I was wondering how do you get the first two octets of an IP address?

Code:
    new clientIP[16];     get_user_ip(id, clientIP, 15, 1);

111.111.XXX.XXX

Does that mean I have to do get_user_up(id,clientIP,7,1)?

Arkshine 02-05-2009 12:30

Re: Getting the first two octects of an IP address...
 
If you want the 7 first characters, just do that :

new clientIP[8];
get_user_ip(id, clientIP, 7, 1);

Dores 02-05-2009 12:37

Re: Getting the first two octects of an IP address...
 
If you want both separated:

Code:
new ip[9], octet1[4], octet2[4]; get_user_ip(id, ip, 8, 1); // 111.111.XXX.XXX // ip now contains 111.111. strtok(ip, octet1, 3, ip, 7, '.'); strtok(ip, octet2, 3, ip, 7, '.'); // octet1 now contains 111 // octet2 now contains 111

P4rD0nM3 02-05-2009 16:24

Re: Getting the first two octects of an IP address...
 
Well I have this...

Code:
#include <amxmodx> new const cafeIP[][] = {     "192.168.",     "172.16.",     "10.16." }; public client_connect(id) {     new clientIP[8];     get_user_ip(id, clientIP, 7, 1);         for(new i = 0; i < sizeof(clientIP); i++)     {         if(containi(clientIP, clientIP[i]) != -1)         { // Give players automatic $16000 }     } }

And as you can see on the 10.16. if someone connects with 210.16, it returns true as well.

Since 10.16 is contained in 210.16.

YamiKaitou 02-05-2009 16:32

Re: Getting the first two octects of an IP address...
 
PHP Code:

new ip[9], newip[9];
get_user_ip(idip81); // 111.111.XXX.XXX
// ip now contains 111.111.

strtok(ipnewip3ip7'.');
add(newip8".");
strtok(ipnewip[3], 3ip7'.');
// newip contains 111.111 

Should work, not tested

P4rD0nM3 02-05-2009 17:05

Re: Getting the first two octects of an IP address...
 
I didn't quite get it Yami sorry. I apologize.

YamiKaitou 02-05-2009 17:06

Re: Getting the first two octects of an IP address...
 
newip will contain the first two octets only, no matter if it is 8.69 or 123.238

Exolent[jNr] 02-05-2009 17:11

Re: Getting the first two octects of an IP address...
 
Or what about:
Code:
new ip[9], octet1[4], octet2[4]; get_user_ip(client, ip, sizeof(ip) - 1, 1); replace_all(ip, sizeof(ip) - 1, ".", " "); strbreak(ip, octet1, sizeof(octet1) - 1, ip, sizeof(ip) - 1); strbreak(ip, octet2, sizeof(octet2) - 1, ip, sizeof(ip) - 1); // octet1 contains first number // octet2 contains second number

P4rD0nM3 02-05-2009 17:27

Re: Getting the first two octects of an IP address...
 
Ok let's say that 210.16 was client that connected.

But how do I make sure that when I compare

210.16 to 10.16 it wouldn't think that it's the same IP. Because that's what's happening.

YamiKaitou 02-05-2009 17:43

Re: Getting the first two octects of an IP address...
 
Depending on which script you use,

Mine (you will have to remove the last '.' in the list of IPs or add another '.' to the end of newip):
PHP Code:

if (equal(newipcafeIP[i])) 



All times are GMT -4. The time now is 01:45.

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