cidr2ip

Small and simple program for converting a CIDR into a netmask
git clone git://git.noxz.tech/cidr2ip
Log | Files | Refs | README | LICENSE

commit a808568045b05c24347b8a26e523b00833d841dd
parent c2b6c7fffe0902896cf7e5d3188a14909b80acf6
Author: z0noxz <chris@noxz.tech>
Date:   Wed,  8 Aug 2018 20:46:45 +0200

port everything to c instead of POSIX sh

Diffstat:
AMakefile | 33+++++++++++++++++++++++++++++++++
MREADME.md | 23++++++++++++++++-------
Dcidr2ip | 72------------------------------------------------------------------------
Acidr2ip.c | 65+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aconfig.mk | 15+++++++++++++++
5 files changed, 129 insertions(+), 79 deletions(-)

diff --git a/Makefile b/Makefile @@ -0,0 +1,33 @@ +.POSIX: + +include config.mk + +SRC = cidr2ip.c +OBJ = $(SRC:.c=.o) + +all: options cidr2ip + +options: + @echo cidr2ip build options: + @echo "CC = $(CC)" + +.c.o: + $(CC) $(STCFLAGS) -c $< + +$(OBJ): config.mk + +cidr2ip: $(OBJ) + $(CC) -g -o $@ $(OBJ) $(STLDFLAGS) + +clean: + rm -f cidr2ip $(OBJ) + +install: cidr2ip + mkdir -p /usr/local/bin + cp -f cidr2ip /usr/local/bin + chmod 755 /usr/local/bin/cidr2ip + +uninstall: + rm -f /usr/local/bin/cidr2ip + +.PHONY: all options clean install uninstall diff --git a/README.md b/README.md @@ -1,9 +1,18 @@ -# cidr2ip -Converts CIDR to IPADDRESS +cidr2ip +====== +`cidr2ip` is a small and simple program for converting a CIDR into ip (mask). -install using `cp ./cidr2ip /usr/local/bin` or similar. +Installation +------------ +Edit config.mk to match your local setup (cidr2ip is installed into the +/usr/local namespace by default), then simply enter the following command to +install (if necessary as root): + + make clean install + +Example usage of cidr2ip +------------------------ +Print a complete CIDR to ip conversion table + + for i in {1..32}; do printf '\\%-5s' $i; cidr2ip $i;done -## print complete table -``` -for i in {1..32}; do printf '\\%-5s' $i; cidr2ip $i;done -``` diff --git a/cidr2ip b/cidr2ip @@ -1,72 +0,0 @@ -#!/bin/sh -license=' -Converts CIDR to IPADDRESS -Copyright (C) 2018 z0noxz, <chris@noxz.tech> - -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 3 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, see <http://www.gnu.org/licenses/>. -' - -do_cidr2ip() { - - # variable declaration - cidr=$1 - cidr_invert=$((32-cidr)) - mask=$((4294967295 >> cidr_invert << cidr_invert)) - result="" - base=256 - i=0 - - # base convert decimal mask to base-256 - while [ "$mask" -ne 0 ]; do - result=$(printf '%-4s%s' $((mask % base)) "$result") - mask=$((mask / base)) - done - - # output as an ip - for item in $result; do - printf '%d' "$item" - i=$((i+1)) - if [ "$i" -lt 4 ]; then - printf '.' - else - printf '\n' - fi - done - - # success - return 0 -} - -# read from stdin if applicable -x="$1" -if ! [ -t 0 ]; then - while read -r line; do - x="$line" - done -fi - -# check if input is a CIDR, then process it -if [ "$x" -eq "$x" ] 2> /dev/null && [ "$x" -le 32 ] && [ "$x" -gt 0 ]; then - do_cidr2ip "$x" && exit 0 -fi - -# if all fails print usage -program_name="${0##*/}" -printf 'Usage: %s CIDR - -LICENSE NOTICE -%s -' "$program_name" "$license" >&2 - -exit 1 diff --git a/cidr2ip.c b/cidr2ip.c @@ -0,0 +1,65 @@ +/* + * Converts CIDR to IPADDRESS + * Copyright (C) 2018 z0noxz, <chris@noxz.tech> + * + * 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 3 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, see <http://www.gnu.org/licenses/>. + */ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +int +main(int argc, char **argv) { + int ip, cidr, input_size; + int buffer_size = 80; + unsigned char mask[4]; + char buffer[buffer_size]; + char *input; + + /* validate input from STDIN */ + if (!isatty(fileno(stdin)) && fgets(buffer, buffer_size, stdin) != NULL) { + input_size = strlen(buffer); + input = malloc(input_size); + input[0] = '\0'; + strcat(input, buffer); + + /* validate input from CLI */ + } else if (argc == 2) { + input_size = strlen(argv[1]); + input = malloc(input_size); + input[0] = '\0'; + strcat(input, argv[1]); + + /* no valid input, so show usage */ + } else { + fprintf(stderr, "usage: cidr2ip CIDR\n"); + return 1; + } + + if (sscanf(input, "%d", &cidr) == 1 && cidr >= 1 && cidr <= 32) { + ip = 4294967295 >> (32-cidr) << (32-cidr); + mask[0] = (ip >> 0) & 0xff; + mask[1] = (ip >> 8) & 0xff; + mask[2] = (ip >> 16) & 0xff; + mask[3] = (ip >> 24) & 0xff; + } else { + fprintf(stderr, "error: not a CIDR\n"); + return 1; + } + + /* finally print the mask */ + printf("%d.%d.%d.%d\n", mask[3], mask[2], mask[1], mask[0]); + return 0; +} diff --git a/config.mk b/config.mk @@ -0,0 +1,15 @@ +# ztatus version +VERSION = 0.1.1 + +# paths +PREFIX = /usr/local +MANPREFIX = $(PREFIX)/share/man + +# flags +CFLAGS = -Wall -O3 -pedantic +CPPFLAGS = -DVERSION=\"$(VERSION)\" -D_DEFAULT_SOURCE -D_XOPEN_SOURCE=600 +STCFLAGS = $(CPPFLAGS) $(CFLAGS) +STLDFLAGS = $(LDFLAGS) + +# compiler and linker +CC = gcc