From owner-freebsd-net@FreeBSD.ORG Sat Aug 10 00:52:43 2013 Return-Path: Delivered-To: freebsd-net@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTP id F313DD07 for ; Sat, 10 Aug 2013 00:52:42 +0000 (UTC) (envelope-from ml@my.gd) Received: from mail-we0-f178.google.com (mail-we0-f178.google.com [74.125.82.178]) (using TLSv1 with cipher ECDHE-RSA-RC4-SHA (128/128 bits)) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 8087F2DF4 for ; Sat, 10 Aug 2013 00:52:42 +0000 (UTC) Received: by mail-we0-f178.google.com with SMTP id u57so4086604wes.9 for ; Fri, 09 Aug 2013 17:52:33 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-gm-message-state:references:mime-version:in-reply-to:content-type :content-transfer-encoding:message-id:cc:from:subject:date:to; bh=240XYwN6Y8EFJUhXq7QRyZeTDis2pXUdDWiCNFZRd1c=; b=iICuuxhZGDm2Amm9IJr+DdShzgDkjew0u1rsbCoxfzU9UDaZn2sHBwlZs6fQ8jBR+1 7ISo4Z3oHxcmBCGGEAOfENL7Qlv1/D3ISBZVWF2pmPGfrbhi82x14r68a5AsLflbSlrg 5rzWv5Q5hJhXUks3ZncchGeqSMq07Ky1Dbwt93ybWB8XRO74HedKO2fBQRX2DAFz6fBt 7D+2EWg1i0+J9ycM0Kb0ovZ/POg5UAxOam6SuM1Ndv9V5EnDaKwnvCZZ+1LU/YaL9JGv EXsvDaoKrWRugWGSZgdsl5r7/O7a4yXK4xjgy+dvVHvGBo+91cAYQ9hZDbP8KhEgpVDM qJiQ== X-Gm-Message-State: ALoCoQmiiMfpc9ZaEnGNaKpjsAs2uBFYPuKHPmocf9ADxF7g/ucEXgg7B0oH1+qDHhyifv31W8WG X-Received: by 10.194.172.228 with SMTP id bf4mr7405544wjc.36.1376095953781; Fri, 09 Aug 2013 17:52:33 -0700 (PDT) Received: from [10.51.25.177] (86.20.90.92.rev.sfr.net. [92.90.20.86]) by mx.google.com with ESMTPSA id mb7sm5238100wic.10.2013.08.09.17.52.31 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 09 Aug 2013 17:52:32 -0700 (PDT) References: <8B53C542-5CC3-45E6-AA62-B9F52A735EE5@my.gd> Mime-Version: 1.0 (1.0) In-Reply-To: Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Message-Id: X-Mailer: iPhone Mail (10B144) From: Damien Fleuriot Subject: Re: how calculate the number of ip addresses in a range? Date: Sat, 10 Aug 2013 02:50:53 +0200 To: Kimmo Paasiala Cc: FreeBSD Net , s m , Peter Wemm X-BeenThere: freebsd-net@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Networking and TCP/IP with FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 10 Aug 2013 00:52:43 -0000 On 10 Aug 2013, at 01:13, Kimmo Paasiala wrote: > On Sat, Aug 10, 2013 at 2:07 AM, Kimmo Paasiala wrote= : >> On Sat, Aug 10, 2013 at 1:44 AM, Peter Wemm wrote: >>> On Fri, Aug 9, 2013 at 9:34 AM, Fleuriot Damien wrote: >>>>=20 >>>> On Aug 8, 2013, at 10:27 AM, Peter Wemm wrote: >>>>=20 >>>>> On Thu, Aug 8, 2013 at 12:04 AM, s m wrote: >>>>>> hello guys, >>>>>>=20 >>>>>> i have a question about ip addresses. i know my question is not relat= ed to >>>>>> freebsd but i googled a lot and found nothing useful and don't know w= here i >>>>>> should ask my question. >>>>>>=20 >>>>>> i want to know how can i calculate the number of ip addresses in a ra= nge? >>>>>> for example if i have 192.0.0.1 192.100.255.254 with mask 8, how many= ip >>>>>> addresses are available in this range? is there any formula to calcul= ate >>>>>> the number of ip addresses for any range? >>>>>>=20 >>>>>> i'm confusing about it. please help me to clear my mind. >>>>>> thanks in advance, >>>>>=20 >>>>> My immediate reaction is.. is this a homework / classwork / assignment= ? >>>>>=20 >>>>> Anyway, you can think of it by converting your start and end addresses= >>>>> to an integer. Over simplified: >>>>>=20 >>>>> $ cat homework.c >>>>> main() >>>>> { >>>>> int start =3D (192 << 24) | (0 << 16) | (0 << 8) | 1; >>>>> int end =3D (192 << 24) | (100 << 16) | (255 << 8) | 254; >>>>> printf("start %d end %d range %d\n", start, end, (end - start) + 1); >>>>> } >>>>> $ ./homework >>>>> start -1073741823 end -1067122690 range 6619134 >>>>>=20 >>>>> The +1 is correcting for base zero. 192.0.0.1 - 192.0.0.2 is two >>>>> usable addresses. >>>>>=20 >>>>> I'm not sure what you want to do with the mask of 8. >>>>>=20 >>>>> You can also do it with ntohl(inet_addr("address")) as well and a >>>>> multitude of other ways. >>>>=20 >>>>=20 >>>> Hold on a second, why would you correct the base zero ? >>>> It can be a valid IP address. >>>=20 >>> There is one usable address in a range of 10.0.0.1 - 10.0.0.1. >>> Converting to an integer and subtracting would be zero. Hence +1. >>>=20 >>> -- >>=20 >> To elaborate on this, for every subnet regardless of the address/mask >> combination there are two unusable addresses: The first address aka >> the "network address" and the last address aka the "broadcast >> address". There may be usable address in between the two that end in >> one of more zeros but those addresses are still valid. Some operating >> systems got this horribly wrong and marked any address ending with a >> single zero as invalid, windows 2000 was one of them. >>=20 >> -Kimmo >=20 > Of course I should have mentioned that there are special cases to > address/mask combinations, /31 and /32 masks have to be treated > specially or there are no usable addresses at all. >=20 > -Kimmo Fully agree, /31 and /32 are special cases indeed.=