From owner-freebsd-net@FreeBSD.ORG Fri Aug 9 16:34:51 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 50DB5737 for ; Fri, 9 Aug 2013 16:34:51 +0000 (UTC) (envelope-from ml@my.gd) Received: from mail-wi0-f175.google.com (mail-wi0-f175.google.com [209.85.212.175]) (using TLSv1 with cipher ECDHE-RSA-RC4-SHA (128/128 bits)) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id D82B5227D for ; Fri, 9 Aug 2013 16:34:50 +0000 (UTC) Received: by mail-wi0-f175.google.com with SMTP id hq12so1834140wib.2 for ; Fri, 09 Aug 2013 09:34:43 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-gm-message-state:content-type:mime-version:subject:from :in-reply-to:date:cc:content-transfer-encoding:message-id:references :to; bh=G0uf3vNF5JLj/0LNoYML7yxC6w6NTSQ5QSpAiJPMQSE=; b=gwYIWMOjF/OGB1gt4G38R2vz4dHi6Ps/mSWzH5vS0TR1MkFeDxBbxFx9Ab1RjYaHM8 0KXtWKX+B6lcWWIiyvEWdepA+uuOGly5onS3asICSwaXVPgVPy6/TOpGsie8KI7alJVS PBI+uI+SpCI6rRbgJncyjcFXytIihkAkxKT7uZPwzVDZGKp9jkv8Otu81B+qjaSG5J17 mzkkyfTMhYADjmHb08jYgh3u7z1qP1mRSYl6b6rp6ApIERXqjknIE4p/GTxaVxt+cEy3 q7VyXHFbU3z8Vk3XtZwuPtqc1ASa/9T+mKsvisWnLWy4pdUSjOGm3Oy1hXakVgvjnFLz mI5w== X-Gm-Message-State: ALoCoQmCK+sTpacmnhg0IeOiJEQAygilPaxzaVauBWV3YZdb6gqts95M5lIpZW7mPT0veKVtFztF X-Received: by 10.180.13.210 with SMTP id j18mr796364wic.51.1376066083182; Fri, 09 Aug 2013 09:34:43 -0700 (PDT) Received: from dfleuriot.paris.hi-media-techno.com ([83.167.62.196]) by mx.google.com with ESMTPSA id l5sm3730508wia.6.2013.08.09.09.34.42 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 09 Aug 2013 09:34:42 -0700 (PDT) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 6.5 \(1508\)) Subject: Re: how calculate the number of ip addresses in a range? From: Fleuriot Damien In-Reply-To: Date: Fri, 9 Aug 2013 18:34:41 +0200 Content-Transfer-Encoding: quoted-printable Message-Id: <8B53C542-5CC3-45E6-AA62-B9F52A735EE5@my.gd> References: To: Peter Wemm X-Mailer: Apple Mail (2.1508) Cc: FreeBSD Net , s m 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: Fri, 09 Aug 2013 16:34:51 -0000 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, >>=20 >> i have a question about ip addresses. i know my question is not = related to >> freebsd but i googled a lot and found nothing useful and don't know = where i >> should ask my question. >>=20 >> i want to know how can i calculate the number of ip addresses in a = range? >> 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 = calculate >> 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. Hold on a second, why would you correct the base zero ? It can be a valid IP address. = https://labs.ripe.net/Members/stephane_bortzmeyer/all-ip-addresses-are-equ= al-dot-zero-addresses-are-less-equal