commit 97b64e7d46e99153ac8369bbb97df1217a217875 Author: Jack-Benny Persson Date: Sat May 2 15:23:22 2020 +0200 Inital commit diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..f2a6b67 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,3 @@ +FROM scratch +COPY subnetcalc /subnetcalc +ENTRYPOINT ["/subnetcalc"] diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..0a10f21 --- /dev/null +++ b/LICENSE @@ -0,0 +1,16 @@ +Copyright (C) 2020 Jack-Benny Persson + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e42377f --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +subnetcalc: subnetcalc.c + gcc -Wall -pedantic subnetcalc.c -lm -static -o subnetcalc + docker build . -t subnetcalc + +clean: subnetcalc + rm subnetcalc + docker image rm subnetcalc diff --git a/README.md b/README.md new file mode 100644 index 0000000..8d8aed6 --- /dev/null +++ b/README.md @@ -0,0 +1,32 @@ +# subnetcalc # +A simple subnet calculator written in C, statically compiled, and +then Dockerized. The *Dockerized* version was created as a demonstration +on how to create really small Docker images from scratch. + +![Docker Image Size (latest by date)](https://img.shields.io/docker/image-size/jackbenny/subnetcalc?sort=date) +![Docker Pulls](https://img.shields.io/docker/pulls/jackbenny/subnetcalc) +![Docker Stars](https://img.shields.io/docker/stars/jackbenny/subnetcalc) +![Docker Cloud Automated build](https://img.shields.io/docker/cloud/automated/jackbenny/subnetcalc) +![Docker Cloud Build Status](https://img.shields.io/docker/cloud/build/jackbenny/subnetcalc) + +## Manual build +If you would like to build it yourself, instead of running the +pre-compiled image from Docker Hub, simple type `make`. This will +create a image called *subnetcalc*. If you get a *"is up to date"* message, +instead run `make --always-make`. + +## Usage +Run it (remove jackbenny/ if you would like to run it from your local build) +and enter the subnet mask in slash notation -- but without the slash. + +For example enter 24 for a 24-bit subnet mask and the +program will output the total number of addresses in the range aswell +as the total number of usable addresses for hosts. + + $> docker run -it --rm jackbenny/subnetcalc + Enter netmask in slash-notation without the slash: 24 + Netmask bit: 24 + + 256 total addresses + 254 usable addresses for hosts + diff --git a/subnetcalc b/subnetcalc new file mode 100755 index 0000000..80a20ad Binary files /dev/null and b/subnetcalc differ diff --git a/subnetcalc.c b/subnetcalc.c new file mode 100644 index 0000000..68135c9 --- /dev/null +++ b/subnetcalc.c @@ -0,0 +1,38 @@ +/* A very simple subnet calculator in C, written by + * Jack-Benny Persson (jack-benny@cyberinfo.se). + * Released under GNU GPLv2. +*/ + +#include +#include + +int main(void) +{ + int net; + double hosts; + double addr; + printf("Enter netmask in slash-notation without the slash: "); + scanf("%2d", &net); + + if (net >= 0 && net <= 32) // Sanity check the user input + { + printf("Netmask bit: %d\n\n", net); + addr = pow(2, 32-net); // Calculate number of addresses + printf("%.0lf total addresses\n", addr); + + hosts = addr-2; + if (hosts < 0) // Check if number of usable hosts is a negative value + { + hosts = 0; // Set usable hosts to zero if it was negative + } + printf("%.0lf usable addresses for hosts\n", hosts); + return 0; + } + else // If the user entered anything else than 0-32 + { + printf("Only values between 0-32 are accepted\n"); + return 1; + } + + return 1; +}