Raised This Month: $32 Target: $400
 8% 

[INC] IP/NET converter and checker


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Zefir
Member
Join Date: Oct 2007
Location: Kiev, Ukraine
Old 07-09-2009 , 11:08   [INC] IP/NET converter and checker
Reply With Quote #1

Off pages
PHP Code:
#if defined _ip_included
  #endinput
#endif
#define _ip_included

/*
    IP converter by Zefir<[email protected]>
    http://up.org.ua/_tag/ip.git

    developed for Cerberus project
    http://cerberus.cstrike.in.ua/
    2 jule 2009 (c) Zefir


    WARNING!!! INPUT DATA NOT CHECKED FOR AVAILABLE RANGE
    WARNING!!! mask used only left filled, not mask as 11101111 11110000 000...

    inet_aton(ip_string[])
        convert x.x.x.x to LONG
        return unsigned long

    inet_ntoa(ip_number)
        convert LONG to x.x.x.x
        return string[16]

    inet_atom(mask_string)
        convert x.x.x.x to /xx
        return digit 0-32

    inet_ntom(mask_number)
        convert LONG to /xx
        return digit 0-32

    inet_mtoa(mask_string)
        convert /xx to x.x.x.x
        return string[16]

    inet_mton(mask_string)
        convert /xx to LONG
        return unsigned long

    inet_maskton(mask_string[])
        convert both types (/xx or x.x.x.x)  to LONG
        return unsigned long

    inet_range(net_string, &net, &mask)
        get net and mask as LONG from subnet x.x.x.x/xx OR x.x.x.x/x.x.x.x
        return true if mask /32

    in_range(net[], ip[])
        check net[] subnet contained ip[] address

    ip_local(ip)
        return true if ip as LONG be contained in any from this networks:
            127.0.0.0/8
            10.0.0.0/8
            172.16.0.0/12
            192.168.0.0/16
            169.254.0.0/16
*/

stock inet_aton(const ip_string[]) {
    static 
ip[16], ips[4], ijcurips = {0,0,0,0}; 0cur 0
    copy
(ip15ip_string)
    while (
&& (contain(ip[cur], ".")) > -1) {
        
ip[cur i++] = EOS
        ips
[j++] = str_to_num(ip[cur])
        
cur += i
    
}
    
ips[j] = str_to_num(ip[cur])
    return (
ips[0] << 24) | (ips[1] << 16) | (ips[2] << 8) | ips[3]
}

stock inet_ntoa(ip) {
    static 
ip_string[16]; ip_string[0] = EOS
    formatex
(ip_string15"%d.%d.%d.%d"ip >>> 24ip >>> 16 255ip >>> 255ip 255)
    return 
ip_string
}


stock inet_atom(const mask_string[]) {
    static 
ipiip inet_aton(mask_string), 0
    
while (!(ip 1) && i++ < 31ip >>>= 1
    
return 31 i
}

stock inet_ntom(mask) {
    static 
i0
    
while (!(mask 1) && i++ < 31mask >>>= 1
    
return 31 i
}

stock bool:inet_range(const net_string[], &net, &mask) {
    static 
lnet[16], i
    
if ((contain(net_string"/")) > -1) {
        
copy(lnetinet_string)
        
mask inet_maskton(net_string[1])
    } else {
        
mask 0
    
}
    
net inet_aton(lnet) & mask
    
return mask == 4294967295
}

stock bool:in_range(const net_string[], const ip_string[]) {
    static 
netmask
    inet_range
(net_stringnetmask)
    return 
inet_aton(ip_string) & mask == net
}

// All int is signed. And mask first bit, its bit of sign. :(
// Set if mask > 0 and signed shift of mask bits count - 1
stock inet_mton(const mask_string[]) {
    static 
mask
    mask 
str_to_num(mask_string)
    if (!
mask) return 0
    
return 4294967295 << (31 mask)
}

stock inet_mtoa(const mask_string[])
    return 
inet_ntoa(inet_mton(mask_string))

stock inet_maskton(const mask_string[]) {
    if (
contain(mask_string".") > -1)
        return 
inet_aton(mask_string)
    return 
inet_mton(mask_string)
}

/*
net: 127.0.0.0/8,    net = 2130706432, mask = 4278190080
net: 10.0.0.0/8,     net = 167772160,  mask = 4278190080
net: 172.16.0.0/12,  net = 2886729728, mask = 4293918720
net: 192.168.0.0/16, net = 3232235520, mask = 4294901760
net: 169.254.0.0/16, net = 2851995648, mask = 4294901760
*/
stock bool:ip_local(ip) {
    return    
2130706432 == ip 4278190080 ||
        
167772160  == ip 4278190080 ||
        
2886729728 == ip 4293918720 ||
        
3232235520 == ip 4294901760 ||
        
2851995648 == ip 4294901760


Added check on local networks
Attached Files
File Type: inc ip.inc (3.3 KB, 636 views)
__________________

Last edited by Zefir; 12-18-2010 at 02:41. Reason: Fixed linet_range
Zefir is offline
Send a message via ICQ to Zefir
AGquit
New Member
Join Date: Jul 2009
Location: Nizhni Tagil
Old 07-13-2009 , 11:43   Re: [INC] IP/NET converter and checker
Reply With Quote #2

хм.. а в чем заключается смысл этого плагина? до меня не дошло.... :-D
AGquit is offline
Send a message via ICQ to AGquit Send a message via Skype™ to AGquit
[ --<-@ ] Black Rose
ANNIHILATED
Join Date: Sep 2005
Location: Stockholm, Sweden.
Old 07-13-2009 , 13:24   Re: [INC] IP/NET converter and checker
Reply With Quote #3

Quote:
Originally Posted by AGquit View Post
хм.. а в чем заключается смысл этого плагина? до меня не дошло.... :-D
Is that so? Idiot.
[ --<-@ ] Black Rose is offline
Zefir
Member
Join Date: Oct 2007
Location: Kiev, Ukraine
Old 07-13-2009 , 13:37   Re: [INC] IP/NET converter and checker
Reply With Quote #4

AGquit, this is not plugin. This is library for manipulate IP, NET and MASK.

Это не плагин, ты вообще боюсь разделом ошибся, это для разработчиков библиотека для работы с адресами сетями и масками.
Это не едят, это готовить надо .
__________________
Zefir is offline
Send a message via ICQ to Zefir
AGquit
New Member
Join Date: Jul 2009
Location: Nizhni Tagil
Old 07-13-2009 , 15:15   Re: [INC] IP/NET converter and checker
Reply With Quote #5

*ROFL* мля...
AGquit is offline
Send a message via ICQ to AGquit Send a message via Skype™ to AGquit
TheRadiance
Senior Member
Join Date: Nov 2007
Location: Kazakhstan
Old 07-15-2009 , 13:06   Re: [INC] IP/NET converter and checker
Reply With Quote #6

Good work, i'll test it

Спасибо, воспользуюсь
TheRadiance is offline
Send a message via ICQ to TheRadiance
Zefir
Member
Join Date: Oct 2007
Location: Kiev, Ukraine
Old 07-15-2009 , 18:34   Re: [INC] IP/NET converter and checker
Reply With Quote #7

Да пожалста

Ты мне только плиз в аську ответь
__________________
Zefir is offline
Send a message via ICQ to Zefir
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 07-15-2009 , 18:36   Re: [INC] IP/NET converter and checker
Reply With Quote #8

Keep it English only, or talk in Multilingual forums.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Zefir
Member
Join Date: Oct 2007
Location: Kiev, Ukraine
Old 07-15-2009 , 18:56   Re: [INC] IP/NET converter and checker
Reply With Quote #9

Ok, sorry
__________________
Zefir is offline
Send a message via ICQ to Zefir
ikplayer
Junior Member
Join Date: Sep 2009
Location: Rus=)
Old 05-23-2010 , 15:39   Re: [INC] IP/NET converter and checker
Reply With Quote #10

Code:
/* 
net: 127.0.0.0/8,    net = 2130706432, mask = 4286578688 
net: 10.0.0.0/8,     net = 167772160,  mask = 4286578688 
net: 172.16.0.0/12,  net = 2886729728, mask = 4294443008 
net: 192.168.0.0/16, net = 3232235520, mask = 4294934528 
net: 169.254.0.0/16, net = 2851995648, mask = 4294934528 
*/
Mask is wrong.
To do it good: accept to all masks <<1
ikplayer 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 21:59.


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