From owner-freebsd-net@FreeBSD.ORG Sun Aug 11 19:30:32 2013 Return-Path: Delivered-To: freebsd-net@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTP id 5A39AA17 for ; Sun, 11 Aug 2013 19:30:32 +0000 (UTC) (envelope-from ml@my.gd) Received: from mail-we0-f173.google.com (mail-we0-f173.google.com [74.125.82.173]) (using TLSv1 with cipher ECDHE-RSA-RC4-SHA (128/128 bits)) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id D03592415 for ; Sun, 11 Aug 2013 19:30:31 +0000 (UTC) Received: by mail-we0-f173.google.com with SMTP id x55so4851368wes.18 for ; Sun, 11 Aug 2013 12:30:24 -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=OOWzzlgofC2+Up1cf7y/yLWvW80lcVxAkC2QN+cbw1Q=; b=BbvEFo1+VffBr4c+JcCfWL13oDsdXcMxWShQvMTkLfskUpmWLU9YsfD8zSvE2gFDgD Opu0Dyo0cI1/4tRDhFUUNHbuqylHLm0YWNrKr+p+W7V4gkJnwuIw9JhsW5piDAPFTWs1 NFEaRAp7qyGc0wfT5XC2HTA46NejjYePLZiobWS4dvvUJaY3E9KyR878AcbTGAVneDLX vraEykpN3VifnGi3GWI+vydk488rzL8h5ZTh6/nlEmlry3MIEQ2rknL5JAnq9tYM0hAo ivxCYvcsPtoqT84zcma+p/AWjFzeiGgmaca7qHfChezAqmz5WBwVN/G80HXDPwVCZOSz Z+bw== X-Gm-Message-State: ALoCoQlO4+OVLb5vW2nzakx6fGHyIabIgLep36ZyxNhOH4aEVMJPE3n6yp+13CsgXQGjSpHef/oj X-Received: by 10.194.201.202 with SMTP id kc10mr10968208wjc.1.1376248941167; Sun, 11 Aug 2013 12:22:21 -0700 (PDT) Received: from [10.51.222.45] (86.20.90.92.rev.sfr.net. [92.90.20.86]) by mx.google.com with ESMTPSA id jf9sm12667338wic.5.2013.08.11.12.22.19 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Sun, 11 Aug 2013 12:22:20 -0700 (PDT) References: <8B53C542-5CC3-45E6-AA62-B9F52A735EE5@my.gd> Mime-Version: 1.0 (1.0) In-Reply-To: Message-Id: X-Mailer: iPhone Mail (10B144) From: Damien Fleuriot Subject: Re: how calculate the number of ip addresses in a range? Date: Sun, 11 Aug 2013 21:20:40 +0200 To: saeedeh motlagh Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable X-Content-Filtered-By: Mailman/MimeDel 2.1.14 Cc: Kimmo Paasiala , s m , Peter Wemm , FreeBSD Net 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: Sun, 11 Aug 2013 19:30:32 -0000 Yup. Your available addresses are: (2 ^ (32 - netmask) ) - 2 Example for a 29: 2^3 - 2 Example for a /28: 2^4 - 2 On 11 Aug 2013, at 06:39, saeedeh motlagh wrote:= > thank you all guys for your answers. > Peter, of course it's not my homework!!!!!! in fact, i have a program whic= h manages dhcp. i want to limit the number of ip address which can be assign= ed by dhcp server. in order to do that, i should know how many ip addresses a= re available in the range that is defined for server and if the number of av= ailable ip addresses are greater than valid threshold, it's error. so as you= said, i should know the math for calculate this number. >=20 > thank you again guys for your answers but they do not solve my problem. an= y body knows what is the formula to calculate the valid ip addresses for any= desired ranges? > Thanks=20 >=20 >=20 > On Sat, Aug 10, 2013 at 5:19 AM, Damien Fleuriot wrote: >>=20 >>=20 >> On 10 Aug 2013, at 01:07, Kimmo Paasiala wrote: >>=20 >> > On Sat, Aug 10, 2013 at 1:44 AM, Peter Wemm wrote: >> >> On Fri, Aug 9, 2013 at 9:34 AM, Fleuriot Damien wrote: >> >>> >> >>> On Aug 8, 2013, at 10:27 AM, Peter Wemm wrote: >> >>> >> >>>> On Thu, Aug 8, 2013 at 12:04 AM, s m wrote: >> >>>>> hello guys, >> >>>>> >> >>>>> i have a question about ip addresses. i know my question is not rel= ated to >> >>>>> freebsd but i googled a lot and found nothing useful and don't know= where i >> >>>>> should ask my question. >> >>>>> >> >>>>> i want to know how can i calculate the number of ip addresses in a r= ange? >> >>>>> for example if i have 192.0.0.1 192.100.255.254 with mask 8, how ma= ny ip >> >>>>> addresses are available in this range? is there any formula to calc= ulate >> >>>>> the number of ip addresses for any range? >> >>>>> >> >>>>> i'm confusing about it. please help me to clear my mind. >> >>>>> thanks in advance, >> >>>> >> >>>> My immediate reaction is.. is this a homework / classwork / assignme= nt? >> >>>> >> >>>> Anyway, you can think of it by converting your start and end address= es >> >>>> to an integer. Over simplified: >> >>>> >> >>>> $ 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 >> >>>> >> >>>> The +1 is correcting for base zero. 192.0.0.1 - 192.0.0.2 is two >> >>>> usable addresses. >> >>>> >> >>>> I'm not sure what you want to do with the mask of 8. >> >>>> >> >>>> You can also do it with ntohl(inet_addr("address")) as well and a >> >>>> multitude of other ways. >> >>> >> >>> >> >>> Hold on a second, why would you correct the base zero ? >> >>> It can be a valid IP address. >> >> >> >> 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. >> >> >> >> -- >> > >> > 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. >> > >> > -Kimmo >>=20 >>=20 >> Kimmo, >>=20 >> That is untrue regarding /31 netmasks where you theoretically have 2^1 -2= addresses. >>=20 >> With such a short netmask the only 2 addresses are usable. >> _______________________________________________ >> freebsd-net@freebsd.org mailing list >> http://lists.freebsd.org/mailman/listinfo/freebsd-net >> To unsubscribe, send any mail to "freebsd-net-unsubscribe@freebsd.org" >=20 >=20 >=20 > --=20 > Sa.M