ip2cidr

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

ip2cidr.c
1/*
2 * Converts IPADDRESS to CIDR, or returns CIDR if already a valid one
3 * Copyright (C) 2018 z0noxz, <chris@noxz.tech>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18#include <stdlib.h>
19#include <stdio.h>
20#include <string.h>
21#include <unistd.h>
22#include "ip2cidr.h"
23
24const char *usage = "usage: ip2cidr <IP>";
25
26/* HAKMEM bit counter (item 169) */
27int
28bit_count(unsigned int u) {
29    unsigned int u_count;
30
31    u_count = u - ((u >> 1) & 0333333333333) - ((u >> 2) & 011111111111);
32    return ((u_count + (u_count >> 3)) & 030707070707) % 63;
33}
34
35int
36main(int argc, char **argv) {
37    int ip, cidr,
38        n1, n2, n3, n4, input_size;
39    int buffer_size = 80;
40    char buffer[buffer_size];
41    char *input;
42
43    /* validate input from STDIN */
44    if (!isatty(fileno(stdin)) && fgets(buffer, buffer_size, stdin) != NULL) {
45        input_size = strlen(buffer);
46        input = malloc(input_size);
47        input[0] = '\0';
48        strcat(input, buffer);
49
50    /* validate input from CLI */
51    } else if (argc == 2) {
52        input_size = strlen(argv[1]);
53        input = malloc(input_size);
54        input[0] = '\0';
55        strcat(input, argv[1]);
56
57    /* no valid input, so show usage */
58    } else {
59        fprintf(stderr, "%s\n", usage);
60        return 1;
61    }
62
63    /* validate ip address */
64    if (
65        sscanf(input, "%d.%d.%d.%d", &n1, &n2, &n3, &n4) == 4 &&
66        n1 >= 0 && n1 <= 255 &&
67        n2 >= 0 && n2 <= 255 &&
68        n3 >= 0 && n3 <= 255 &&
69        n4 >= 0 && n4 <= 255
70    ) {
71        ip      = n1 * 0x1000000u + n2 * 0x10000u + n3 * 0x100u + n4;
72        cidr    = bit_count(ip);
73
74        /* check if ip is a valid subnet mask (all ones are in succession) */
75        if (cidr < 1 || cidr > 32 || ip != (0xffffffffu >> (32-cidr) << (32-cidr))) {
76            fprintf(stderr, "error: not a subnet mask\n");
77            return 1;
78        }
79    } else if (sscanf(input, "%d", &cidr) == 1 && cidr >= 1 && cidr <= 32) {
80    } else {
81        fprintf(stderr, "error: not a correctly formed ip address\n");
82        return 1;
83    }
84
85    /* finally print the CIDR */
86    printf("%d\n", cidr);
87    return 0;
88}