Inital commit
This commit is contained in:
commit
97b64e7d46
3
Dockerfile
Normal file
3
Dockerfile
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
FROM scratch
|
||||||
|
COPY subnetcalc /subnetcalc
|
||||||
|
ENTRYPOINT ["/subnetcalc"]
|
16
LICENSE
Normal file
16
LICENSE
Normal 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
7
Makefile
Normal 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
32
README.md
Normal 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.
|
||||||
|
|
||||||
|

|
||||||
|

|
||||||
|

|
||||||
|

|
||||||
|

|
||||||
|
|
||||||
|
## 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
BIN
subnetcalc
Executable file
Binary file not shown.
38
subnetcalc.c
Normal file
38
subnetcalc.c
Normal 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;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user