From owner-freebsd-net@FreeBSD.ORG Sat Aug 10 00:58:29 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 590E294 for ; Sat, 10 Aug 2013 00:58:29 +0000 (UTC) (envelope-from ml@my.gd) Received: from mail-wg0-f41.google.com (mail-wg0-f41.google.com [74.125.82.41]) (using TLSv1 with cipher ECDHE-RSA-RC4-SHA (128/128 bits)) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id DB6F42E2A for ; Sat, 10 Aug 2013 00:58:28 +0000 (UTC) Received: by mail-wg0-f41.google.com with SMTP id l18so244748wgh.2 for ; Fri, 09 Aug 2013 17:58:21 -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=HVvf0wctHCuKIPmN/a1rEsNY3LUXFBZZtjKcUCCAP5o=; b=QCw+Mlyj7tHdFD5VEX77Af1Wi8eypel6xuRbiBhNzhLguMvqKctiKkHDQ7N7thm0A0 zwdeXEH3t2oAk3GAeYNhOFlbC604QXKFP9M5vyhuTI5fvlmH5KSY1j2cs2OD8HtWQUtM BMxThe98U9bMBWePNZWb7wp51oRVqZgco90Ovi6RA7DsCkXlf2uLegRqcgzBC//LixaH AYqoE33cS15xWHLM0WwgiWCmwwR4WcvCRedrkhW2hDcOyl7p2fD+n9EA6ObWsDXJN092 PMQkp6CXauhGzkO7Ccls7u1cHIx0UrTwkPCQNhSFupKPHo0e4irXbX+K63RR1gsnmrsA 3y3Q== X-Gm-Message-State: ALoCoQmkC8Cloo1qfHX3r31Lrg+oEqzFQAcBnNyX4mVJBkp0Wdxq6EnGAvEyBhYiLpUQkYWf+x5x X-Received: by 10.180.182.67 with SMTP id ec3mr1588303wic.1.1376095885517; Fri, 09 Aug 2013 17:51:25 -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 jf9sm5244977wic.5.2013.08.09.17.51.24 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 09 Aug 2013 17:51:24 -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:49:45 +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:58:29 -0000 On 10 Aug 2013, at 01:07, 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 relate= d to >>>>> freebsd but i googled a lot and found nothing useful and don't know wh= ere i >>>>> should ask my question. >>>>>=20 >>>>> i want to know how can i calculate the number of ip addresses in a ran= ge? >>>>> for example if i have 192.0.0.1 192.100.255.254 with mask 8, how many i= p >>>>> addresses are available in this range? is there any formula to calcula= te >>>>> 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 Kimmo, That is untrue regarding /31 netmasks where you theoretically have 2^1 -2 ad= dresses. With such a short netmask the only 2 addresses are usable.=