Inital commit

This commit is contained in:
Jack-Benny Persson 2020-05-02 15:23:22 +02:00
commit 97b64e7d46
6 changed files with 96 additions and 0 deletions

3
Dockerfile Normal file
View File

@ -0,0 +1,3 @@
FROM scratch
COPY subnetcalc /subnetcalc
ENTRYPOINT ["/subnetcalc"]

16
LICENSE Normal file
View File

@ -0,0 +1,16 @@
Copyright (C) 2020 Jack-Benny Persson <jack-benny@cyberinfo.se>
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

7
Makefile Normal file
View File

@ -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

32
README.md Normal file
View File

@ -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

BIN
subnetcalc Executable file

Binary file not shown.

38
subnetcalc.c Normal file
View File

@ -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 <stdio.h>
#include <math.h>
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;
}