From owner-freebsd-net@FreeBSD.ORG Thu Aug 8 08:27:12 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 229194D2 for ; Thu, 8 Aug 2013 08:27:12 +0000 (UTC) (envelope-from peter@wemm.org) Received: from mail-qa0-x22c.google.com (mail-qa0-x22c.google.com [IPv6:2607:f8b0:400d:c00::22c]) (using TLSv1 with cipher ECDHE-RSA-RC4-SHA (128/128 bits)) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id D5A5B2E5E for ; Thu, 8 Aug 2013 08:27:11 +0000 (UTC) Received: by mail-qa0-f44.google.com with SMTP id hu16so154187qab.3 for ; Thu, 08 Aug 2013 01:27:11 -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=IhR3esDGbOpo0etRqvsQptgRJ7B2aIhZ1RFqH31IQcs=; b=tGYL5yyEhRFYDh+hPSkqkpG+VQhYriOUImpbhtGRHjcJtLV6rQr8DJJwh5HQHiPV0S VUifwEaXoMWMSeJIYdJwPpzoHp9l/bqbvkwxYMZFqaMsDMLszxKBwNQ6QPvbWXYhUaKY ddk6ZEGLY5wHXBPy+SLszU3D8wf3lYilu/lSQ= 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=IhR3esDGbOpo0etRqvsQptgRJ7B2aIhZ1RFqH31IQcs=; b=OdZZhRBaz4z+1niImEmEItvLzEk1e9FW+9iHbYLlT0+7AGvgkUfQQtD6/Sg3jEiTXK tWFEv+wNsUDSfwOdSqtF3Fd77MxxemaE4az8YCMqF41ad2dZY17Ypw/edezv+VCoMHgJ W/oIIPSW/uMwCO2YAP4R+/VGXTDEOL8A48Ht7CBdfYlu1pTW632/E/G5cVBuyqqqoz16 0RlN3hoask1dvI1CGkn01pFnQ706r8yWvzEGdtDJaY5mLAuKRAEc+KwxFu/MxPRswep0 t/ZvX0M4/lUAPydgb1yz5uVRBpelqwI4BcT4qeRJEhnZSXoLII1RJfZXeA62uPOUWlum pxqg== X-Gm-Message-State: ALoCoQmyOdwxW72Sx2H/Ovw2TcTrbxpz+Sypam/5kzoFHw2a7ydrya2wVfDHo/OInAE8uOC5DKqh MIME-Version: 1.0 X-Received: by 10.224.137.73 with SMTP id v9mr2984791qat.16.1375950430956; Thu, 08 Aug 2013 01:27:10 -0700 (PDT) Received: by 10.49.36.5 with HTTP; Thu, 8 Aug 2013 01:27:10 -0700 (PDT) In-Reply-To: References: Date: Thu, 8 Aug 2013 01:27:10 -0700 Message-ID: Subject: Re: how calculate the number of ip addresses in a range? From: Peter Wemm To: s m Content-Type: text/plain; charset=ISO-8859-1 Cc: 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: Thu, 08 Aug 2013 08:27:12 -0000 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. -- 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"