Skip to content

Recent Articles

21
Oct 13

FreeBSD 10: Full ZFS install with encryption

FreeBSD 10-RELEASE is being testing and rolled out as we speak, currently on it’s first BETA!




And yes you can do full ZFS install from bsdinstall with disk encryption!

13
Sep 13

PHP ipcalc: Coding with subnets and IP addresses

Working with IP address and subnets in code is a day to day task for a network administrator. Say you have an IP and you need to find out if it’s part of a subnet, or what the network address is of a subnet, what’s the best way? In the past I would reach for ipcalc (net-mgmt/ipcalc), a handy little tool for doing all sorts of calculations on IPs and subnets, but calling exec on a CLI tool and parsing the output is not exactly efficient.

The truth is, working with IP addresses is pretty easy, with the right formular you can get any of information you need. In this post I am going to share a little PHP class with some of my favourite IP address calculation methods.

1. Convert CIDR to netmask

Slash notation is beautifully simple, but some things just insist on netmasks. e.g. 21 = 255.255.248.0

    public function cidr2netmask($cidr)
    {
        for( $i = 1; $i <= 32; $i++ )
        $bin .= $cidr >= $i ? '1' : '0';

        $netmask = long2ip(bindec($bin));

        if ( $netmask == "0.0.0.0")
        return false;

    return $netmask;
    }

2. Get network address from cidr subnet

If you know the IP and the subnet size, but whats the network address? e.g. 10.0.2.56/21 = 10.0.0.0

    public function cidr2network($ip, $cidr)
    {
        $network = long2ip((ip2long($ip)) & ((-1 << (32 - (int)$cidr))));

    return $network;
    }

3. Convert netmask to CIDR

Got a netmask, but require slash notation? e.g. 255.255.255.128 = 25

    public function netmask2cidr($netmask)
    {
        $bits = 0;
        $netmask = explode(".", $netmask);

        foreach($netmask as $octect)
        $bits += strlen(str_replace("0", "", decbin($octect)));

    return $bits;
    }

4. Is IP address in subnet?

e.g. is 10.5.21.30 in 10.5.16.0/20 == true, is 192.168.50.2 in 192.168.30.0/23 == false

    public function cidr_match($ip, $network, $cidr)
    {
        if ((ip2long($ip) & ~((1 << (32 - $cidr)) - 1) ) == ip2long($network))
        {
            return true;
        }

    return false;
    }

The CIDR PHP class

And for completeness, here is the full class.

class cidr
{
    // convert cidr to netmask
    // e.g. 21 = 255.255.248.0
    public function cidr2netmask($cidr)
    {
        for( $i = 1; $i <= 32; $i++ )
        $bin .= $cidr >= $i ? '1' : '0';

        $netmask = long2ip(bindec($bin));

        if ( $netmask == "0.0.0.0")
        return false;

    return $netmask;
    }

    // get network address from cidr subnet
    // e.g. 10.0.2.56/21 = 10.0.0.0
    public function cidr2network($ip, $cidr)
    {
        $network = long2ip((ip2long($ip)) & ((-1 << (32 - (int)$cidr))));

    return $network;
    }

    // convert netmask to cidr
    // e.g. 255.255.255.128 = 25
    public function netmask2cidr($netmask)
    {
        $bits = 0;
        $netmask = explode(".", $netmask);

        foreach($netmask as $octect)
        $bits += strlen(str_replace("0", "", decbin($octect)));

    return $bits;
    }

    // is ip in subnet
    // e.g. is 10.5.21.30 in 10.5.16.0/20 == true
    //      is 192.168.50.2 in 192.168.30.0/23 == false 
    public function cidr_match($ip, $network, $cidr)
    {
        if ((ip2long($ip) & ~((1 << (32 - $cidr)) - 1) ) == ip2long($network))
        {
            return true;
        }

    return false;
    }
}
3
Sep 13

FreeBSD: Die Hard tribute in beastie.4th for 9.2 RELEASE

Remember Die Hard? As a tribute FreeBSD 9.2 has a fun new beastie in the boot menu!

The CEO Workstation – Nakatomi Socrates

FreeBSD 9.2 Nakatomi Socrates

 

 

FreeBSD 9.2 Nakatomi Socrates

 

Nakatomi socrates BSD 9.2

Grab a copy now! http://svnweb.freebsd.org/base/releng/9.2/sys/boot/forth/beastie.4th?revision=255027&view=co

: tribute-art ( x y -- ) \ see tribute[bw]-logo

	\ Disable the brand art (we're going to use that space)
	s" set loader_brand=none" evaluate

	\ Blank out the frame of the menu and move the title to left
	s" set loader_menu_title=" evaluate
	s" set loader_menu_frame=none" evaluate

	\ Move the menu to the center of the screen
	s" set loader_menu_x=26" evaluate
	s" set loader_menu_y=12" evaluate
	s" set loader_menu_timeout_x=21" evaluate
	s" set loader_menu_timeout_y=24" evaluate

	2 - swap 39 - swap \ top-left (see `fbsdbw-logo' comments above)

	2dup at-xy 11 spaces ." ,d      b." 1+
	2dup at-xy ."         ,d88]      [88b." 1+
	2dup at-xy ."      ,d888P" 34 emit ."  ,d88b. " 34 emit ." Y888b." 1+
	2dup at-xy ."  , <888P" 34 emit ."  ,dP" 34 emit ." ,db." 34 emit
	           ." Yb. " 34 emit ." Y888> ," 1+
	2dup at-xy ." <88b.~ ,d888  " 34 emit ." YP" 34 emit
	           ."   888b. ~,d88>" 1+
	2dup at-xy ."  ," 34 emit ." Y888888P" 34 emit ." ,db.,db." 34 emit
	           ." Y888888P" 34 emit ." ," 1+
	2dup at-xy ." <88b." 34 emit ." YP" 34 emit ."  _ " 34 emit
	           ." YP" 34 emit 34 emit ." YP" 34 emit ."  _ " 34 emit
	           ." YP" 34 emit ." ,d88>" 1+
	2dup at-xy ."   " 34 emit ." Y88b.,dP          Yb.,d88P" 34 emit 1+
	     at-xy ."      " 34 emit ." YP" 34 emit ."               " 34 emit
	           ." YP" 34 emit ." "
;

: tribute-text ( x y -- ) \ see tribute[bw]-logo

	swap 2 - swap \ beastie adjustment (see `fbsdbw-logo' comments above)

	2dup at-xy ." CEO Workstation" 1+
	1+
	2dup at-xy ." Nakatomi Socrates FreeBSD 9.2" 1+
	2dup at-xy ." Z-Level Central Core" 1+
	1+
	     at-xy ." Preliminary Clearance Approved."
;

: tribute-logo ( x y -- ) \ color Socrates tribute (16 rows x 32 columns)

	\ Produce the tribute art in bright green
	2dup at-xy ." " 2dup tribute-art ." "

	\ Produce the tribute text in regular green
	2dup at-xy ." " 2dup tribute-text ." "

	\ Distinguish the ``Free'' in tribute-text
	2 + swap 16 + swap at-xy ." Free"

 	\ Put the cursor back at the bottom
 	0 25 at-xy
;

: tributebw-logo ( x y -- ) \ Socrates tribute (16 rows x 32 columns)

	\ Produce the tribute art and text
	2dup tribute-art tribute-text

 	\ Put the cursor back at the bottom
 	0 25 at-xy
;

Although, this probably wont be the default in 9.2 RELEASE, maybe this one will…
FreeBSD 9.2 Nakatomi Socrates

FreeBSD 9.2 Nakatomi Socrates