From owner-freebsd-net@FreeBSD.ORG Fri Aug 9 22:44:53 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 A65B1E73 for ; Fri, 9 Aug 2013 22:44:53 +0000 (UTC) (envelope-from peter@wemm.org) Received: from mail-vc0-x234.google.com (mail-vc0-x234.google.com [IPv6:2607:f8b0:400c:c03::234]) (using TLSv1 with cipher ECDHE-RSA-RC4-SHA (128/128 bits)) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 5DA542729 for ; Fri, 9 Aug 2013 22:44:53 +0000 (UTC) Received: by mail-vc0-f180.google.com with SMTP id gf11so1443232vcb.25 for ; Fri, 09 Aug 2013 15:44:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=wemm.org; s=google; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=qu3G/DyEl43WM+FIo79IrRDvdEyqxWtK7beZ2kavskM=; b=MVUv8eE7/6lnSiVuQVisLL8+x74dzFbmB8M1+sMbFLuNVgQOcNWqTkpwg5eJQyP39L 2Ol98Aq/JIofBNb17C5QoRi0dtADAbiIEV+WoHZ2pB0JIyK0gnS9dsaOuu2CH5eK/cMM 4454IohQkfV/1H7NX6WrAa+O9rnrCeohV0UPA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type; bh=qu3G/DyEl43WM+FIo79IrRDvdEyqxWtK7beZ2kavskM=; b=f0pc0uNxBOJaF0JVeS8YMGnHvpWw2fa5qs7wSFEPZTqN30VVzPfHh8PmEKi3RR6bXb wZiLFxpAippk5SAD1N624rZiUJjVH4eN14l5Rh/lIueKdVHrvO8YqCqDQg1Xwe8OjiRH XllAoJSI4sUNDirFMRTTDS5Fc/TomsMuuh7qqgUS850+SG3Jd2evUXEnPgiXOcsWCD8d m/LWoWmcmXfTqbcOI/WSgis+ghjf3MjJE8xdRRd81twVFH7xlQYYj4l5H05SD1+JrKmV FHzus01B27SIQEaX8mmnyaZWSUkupG+rd0MaF/0JUE9xPV2QXvh+34qwBUQZUTqs95ud BsNw== X-Gm-Message-State: ALoCoQmrm5M01tjC2sKQDjQqpZECY4MrOUKhgds9oJUOWE9I4qi2XJEgcJ3k3Tqvs1tEtwwPfXcA MIME-Version: 1.0 X-Received: by 10.52.30.18 with SMTP id o18mr1398019vdh.114.1376088292161; Fri, 09 Aug 2013 15:44:52 -0700 (PDT) Received: by 10.220.167.74 with HTTP; Fri, 9 Aug 2013 15:44:52 -0700 (PDT) In-Reply-To: <8B53C542-5CC3-45E6-AA62-B9F52A735EE5@my.gd> References: <8B53C542-5CC3-45E6-AA62-B9F52A735EE5@my.gd> Date: Fri, 9 Aug 2013 15:44:52 -0700 Message-ID: Subject: Re: how calculate the number of ip addresses in a range? From: Peter Wemm To: Fleuriot Damien Content-Type: text/plain; charset=ISO-8859-1 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 22:44:53 -0000 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 related 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 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? >>> >>> i'm confusing about it. please help me to clear my mind. >>> thanks in advance, >> >> My immediate reaction is.. is this a homework / classwork / assignment? >> >> Anyway, you can think of it by converting your start and end addresses >> to an integer. Over simplified: >> >> $ cat homework.c >> main() >> { >> int start = (192 << 24) | (0 << 16) | (0 << 8) | 1; >> int end = (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. -- Peter Wemm - peter@wemm.org; peter@FreeBSD.org; peter@yahoo-inc.com; KI6FJV UTF-8: for when a ' just won\342\200\231t do. ZFS must be the bacon of file systems. "everything's better with ZFS"