Perl, 72 bytes
#!perl -ap
@a=map{unpack N,pack C4,split/\./,$_}@F;$_=abs($a[1]-$a[0])+1
Usage:
$ echo 10.0.2.0 10.0.3.255 | perl ip-range.pl
512$
This is already longer than primo's Perl program, so not too interesting.
Perl, 119 bytes, for obsolete IP address format
#!perl -ap
sub v(){/^0/?oct:$_}@a=map{$m=3;@p=split/\./,$_;$_=pop@p;$s=v;$s+=v<<8*$m--for@p;$s}@F;$_=abs($a[1]-$a[0])+1
Usage:
$ echo 10.0.2.0 10.0.3.255 | perl ip-obsolete.pl
512$
$ echo 10.512 10.1023 | perl ip-obsolete.pl
512$
$ echo 0xa.0x200 012.01777 | perl ip-obsolete.pl
512$
This program accepts the obsolete format for IP addresses! This includes addresses with 1, 2, or 3 parts, or with hexadecimal or octal parts. Quoting the inet_addr(3) manual page,
Values specified using dot notation take one of the following forms:
a.b.c.d
a.b.c
a.b
a
... When a three part address is specified, the last part is interpreted as a 16-bit quantity and placed in the rightmost two bytes of the network address. ... When a two part address is supplied, the last part is interpreted as a 24-bit quantity and placed in the rightmost three bytes of the network address. ... When only one part is given, the value is stored directly in the network address without any byte rearrangement.
All numbers supplied as ``parts'' in a dot notation may be decimal, octal, or hexadecimal, as specified in the C language (i.e., a leading 0x or 0X implies hexadecimal; a leading 0 implies octal; otherwise, the number is interpreted as decimal).
Most programs no longer accept this obsolete format, but ping 0177.1
still worked in OpenBSD 5.5.