Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 118 lines (94 sloc) 2.529 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
/*
* Scan IP ranges for DNS servers and for servers
* supporting recursion.
*
* Copyright (c) 2009-2013 Michael Santos <michael.santos@gmail.com>
*
* 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/>.
*
*/

/*
* Parse an IP address and return a range.
*/
#include "iprange.h"

in_addr_t quad2hl(char *buf);
in_addr_t mask2hl(char *buf);

    int
parseip (char *buf, in_addr_t *network, in_addr_t *broadcast)
{
    char *ip = NULL;
    char *mask = NULL;
    char *p = NULL;

    in_addr_t addr = 0;
    in_addr_t netmask = 0;

    if ( (ip = strdup(buf)) == NULL)
        err(EXIT_FAILURE, "strdup");

    p = strchr(ip, '/');
    if (p == NULL)
        mask = "32";
    else {
        *p++ = '\0';
        mask = p;
    }

    /* Determine the base IP address */
    if ( (addr = quad2hl(ip)) == 0) {
        warnx("Invalid IP address: %s", ip);
        return (-1);
    }

    /* Get the mask */
    if ( (netmask = mask2hl(mask)) == 0)
        netmask = quad2hl(mask);

    if (netmask == 0) {
        warnx("Invalid netmask: %s", mask);
        return (-1);
    }

    *network = addr & netmask;
    *broadcast = addr | ~netmask;

    return (0);
}

    in_addr_t
quad2hl(char *buf)
{
    in_addr_t addr = 0;
    u_int32_t byte[4];
    int i = 0;

    if (buf == NULL)
        return (0);

    if (sscanf(buf, "%u.%u.%u.%u", &byte[0], &byte[1],
                &byte[2], &byte[3]) != 4)
        return (0);

    for (i = 0; i < 4; i++)
        if (byte[i] > 255)
            return (0);

    for (i = 0; i < 4; i++)
        addr += byte[i] << (24 - i * 8);

    return (addr);
}

    in_addr_t
mask2hl(char *buf)
{
    u_int32_t byte;
    in_addr_t mask = 0;

    if (buf == NULL)
        return (0);

    if (sscanf(buf, "%u", &byte) != 1)
        return (0);

    if (byte > 32 || byte == 0)
        return (0);

    while (byte > 0) {
        mask = (mask >> 1) + 0x80000000;
        byte--;
    }

    return (mask);
}
Something went wrong with that request. Please try again.