2016-07-18 00:11:41 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 - EATON
|
2011-09-29 18:14:46 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2016-07-18 00:11:41 +00:00
|
|
|
/*! \file nutscan-ip.c
|
|
|
|
\brief iterator for IPv4 or IPv6 addresses
|
|
|
|
\author Frederic Bohe <fredericbohe@eaton.com>
|
|
|
|
*/
|
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
#include "config.h" /* must be first */
|
|
|
|
|
2011-09-29 18:14:46 +00:00
|
|
|
#include "nutscan-ip.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "common.h"
|
2012-01-24 10:22:33 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netdb.h>
|
2011-09-29 18:14:46 +00:00
|
|
|
|
|
|
|
static void increment_IPv6(struct in6_addr * addr)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
for (i = 15 ; i >= 0 ; i--) {
|
2011-09-29 18:14:46 +00:00
|
|
|
addr->s6_addr[i]++;
|
2022-06-29 10:37:36 +00:00
|
|
|
if (addr->s6_addr[i] != 0) {
|
2011-09-29 18:14:46 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void invert_IPv6(struct in6_addr * addr1, struct in6_addr * addr2)
|
|
|
|
{
|
|
|
|
struct in6_addr addr;
|
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
memcpy(addr.s6_addr, addr1->s6_addr, sizeof(addr.s6_addr));
|
|
|
|
memcpy(addr1->s6_addr, addr2->s6_addr, sizeof(addr.s6_addr));
|
|
|
|
memcpy(addr2->s6_addr, addr.s6_addr, sizeof(addr.s6_addr));
|
2011-09-29 18:14:46 +00:00
|
|
|
}
|
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
static int ntop(struct in_addr * ip, char * host, GETNAMEINFO_TYPE_ARG46 host_size)
|
2012-01-24 10:22:33 +00:00
|
|
|
{
|
|
|
|
struct sockaddr_in in;
|
2022-06-29 10:37:36 +00:00
|
|
|
memset(&in, 0, sizeof(struct sockaddr_in));
|
2012-01-24 10:22:33 +00:00
|
|
|
in.sin_addr = *ip;
|
|
|
|
in.sin_family = AF_INET;
|
2022-06-29 10:37:36 +00:00
|
|
|
return getnameinfo(
|
|
|
|
(struct sockaddr *)&in,
|
|
|
|
sizeof(struct sockaddr_in),
|
|
|
|
host, host_size, NULL, 0, NI_NUMERICHOST);
|
2012-01-24 10:22:33 +00:00
|
|
|
}
|
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
static int ntop6(struct in6_addr * ip, char * host, GETNAMEINFO_TYPE_ARG46 host_size)
|
2012-01-24 10:22:33 +00:00
|
|
|
{
|
|
|
|
struct sockaddr_in6 in6;
|
2022-06-29 10:37:36 +00:00
|
|
|
memset(&in6, 0, sizeof(struct sockaddr_in6));
|
|
|
|
memcpy(&in6.sin6_addr, ip, sizeof(struct in6_addr));
|
2012-01-24 10:22:33 +00:00
|
|
|
in6.sin6_family = AF_INET6;
|
2022-06-29 10:37:36 +00:00
|
|
|
return getnameinfo(
|
|
|
|
(struct sockaddr *)&in6,
|
|
|
|
sizeof(struct sockaddr_in6),
|
|
|
|
host, host_size, NULL, 0, NI_NUMERICHOST);
|
2012-01-24 10:22:33 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 18:14:46 +00:00
|
|
|
/* Return the first ip or NULL if error */
|
|
|
|
char * nutscan_ip_iter_init(nutscan_ip_iter_t * ip, const char * startIP, const char * stopIP)
|
|
|
|
{
|
2022-06-29 10:37:36 +00:00
|
|
|
uint32_t addr; /* 32-bit IPv4 address */
|
2011-09-29 18:14:46 +00:00
|
|
|
int i;
|
2012-01-24 10:22:33 +00:00
|
|
|
struct addrinfo hints;
|
|
|
|
struct addrinfo *res;
|
|
|
|
struct sockaddr_in * s_in;
|
|
|
|
struct sockaddr_in6 * s_in6;
|
|
|
|
char host[SMALLBUF];
|
2011-09-29 18:14:46 +00:00
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
if (startIP == NULL) {
|
2011-09-29 18:14:46 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
if (stopIP == NULL) {
|
2011-09-29 18:14:46 +00:00
|
|
|
stopIP = startIP;
|
|
|
|
}
|
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
memset(&hints, 0, sizeof(struct addrinfo));
|
2012-01-24 10:22:33 +00:00
|
|
|
hints.ai_family = AF_INET;
|
|
|
|
|
2011-09-29 18:14:46 +00:00
|
|
|
ip->type = IPv4;
|
|
|
|
/* Detecting IPv4 vs IPv6 */
|
2022-06-29 10:37:36 +00:00
|
|
|
if (getaddrinfo(startIP, NULL, &hints, &res) != 0) {
|
2011-09-29 18:14:46 +00:00
|
|
|
/*Try IPv6 detection */
|
|
|
|
ip->type = IPv6;
|
2012-01-24 10:22:33 +00:00
|
|
|
hints.ai_family = AF_INET6;
|
2022-06-29 10:37:36 +00:00
|
|
|
if (getaddrinfo(startIP, NULL, &hints, &res) != 0) {
|
|
|
|
fprintf(stderr, "Invalid address : %s\n", startIP);
|
2011-09-29 18:14:46 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2022-06-29 10:37:36 +00:00
|
|
|
|
|
|
|
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_CAST_ALIGN)
|
|
|
|
# pragma GCC diagnostic push
|
|
|
|
# pragma GCC diagnostic ignored "-Wcast-align"
|
|
|
|
#endif
|
|
|
|
/* Note: we receive a pointer to res above, so have
|
|
|
|
* no control about alignment of its further data */
|
2012-01-24 10:22:33 +00:00
|
|
|
s_in6 = (struct sockaddr_in6 *)res->ai_addr;
|
2022-06-29 10:37:36 +00:00
|
|
|
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_CAST_ALIGN)
|
|
|
|
# pragma GCC diagnostic pop
|
|
|
|
#endif
|
|
|
|
memcpy(&ip->start6, &s_in6->sin6_addr, sizeof(struct in6_addr));
|
2012-01-24 10:22:33 +00:00
|
|
|
freeaddrinfo(res);
|
|
|
|
}
|
|
|
|
else {
|
2022-06-29 10:37:36 +00:00
|
|
|
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_CAST_ALIGN)
|
|
|
|
# pragma GCC diagnostic push
|
|
|
|
# pragma GCC diagnostic ignored "-Wcast-align"
|
|
|
|
#endif
|
|
|
|
/* Note: we receive a pointer to res above, so have
|
|
|
|
* no control about alignment of its further data */
|
2012-01-24 10:22:33 +00:00
|
|
|
s_in = (struct sockaddr_in *)res->ai_addr;
|
2022-06-29 10:37:36 +00:00
|
|
|
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_CAST_ALIGN)
|
|
|
|
# pragma GCC diagnostic pop
|
|
|
|
#endif
|
2012-01-24 10:22:33 +00:00
|
|
|
ip->start = s_in->sin_addr;
|
|
|
|
freeaddrinfo(res);
|
2011-09-29 18:14:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Compute stop IP */
|
2022-06-29 10:37:36 +00:00
|
|
|
if (ip->type == IPv4) {
|
2012-01-24 10:22:33 +00:00
|
|
|
hints.ai_family = AF_INET;
|
2022-06-29 10:37:36 +00:00
|
|
|
if (getaddrinfo(stopIP, NULL, &hints, &res) != 0) {
|
|
|
|
fprintf(stderr, "Invalid address : %s\n", stopIP);
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-01-24 10:22:33 +00:00
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_CAST_ALIGN)
|
|
|
|
# pragma GCC diagnostic push
|
|
|
|
# pragma GCC diagnostic ignored "-Wcast-align"
|
|
|
|
#endif
|
|
|
|
/* Note: we receive a pointer to res above, so have
|
|
|
|
* no control about alignment of its further data */
|
2012-01-24 10:22:33 +00:00
|
|
|
s_in = (struct sockaddr_in *)res->ai_addr;
|
2022-06-29 10:37:36 +00:00
|
|
|
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_CAST_ALIGN)
|
|
|
|
# pragma GCC diagnostic pop
|
|
|
|
#endif
|
2012-01-24 10:22:33 +00:00
|
|
|
ip->stop = s_in->sin_addr;
|
|
|
|
freeaddrinfo(res);
|
2022-06-29 10:37:36 +00:00
|
|
|
}
|
|
|
|
else {
|
2012-01-24 10:22:33 +00:00
|
|
|
hints.ai_family = AF_INET6;
|
2022-06-29 10:37:36 +00:00
|
|
|
if (getaddrinfo(stopIP, NULL, &hints, &res) != 0) {
|
|
|
|
fprintf(stderr, "Invalid address : %s\n", stopIP);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_CAST_ALIGN)
|
|
|
|
# pragma GCC diagnostic push
|
|
|
|
# pragma GCC diagnostic ignored "-Wcast-align"
|
|
|
|
#endif
|
|
|
|
/* Note: we receive a pointer to res above, so have
|
|
|
|
* no control about alignment of its further data */
|
2012-01-24 10:22:33 +00:00
|
|
|
s_in6 = (struct sockaddr_in6 *)res->ai_addr;
|
2022-06-29 10:37:36 +00:00
|
|
|
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_CAST_ALIGN)
|
|
|
|
# pragma GCC diagnostic pop
|
|
|
|
#endif
|
|
|
|
memcpy(&ip->stop6, &s_in6->sin6_addr, sizeof(struct in6_addr));
|
2012-01-24 10:22:33 +00:00
|
|
|
freeaddrinfo(res);
|
2022-06-29 10:37:36 +00:00
|
|
|
}
|
2011-09-29 18:14:46 +00:00
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
/* Make sure start IP is lesser than stop IP */
|
|
|
|
if (ip->type == IPv4) {
|
|
|
|
if (ntohl(ip->start.s_addr) > ntohl(ip->stop.s_addr)) {
|
|
|
|
addr = ip->start.s_addr;
|
|
|
|
ip->start.s_addr = ip->stop.s_addr;
|
|
|
|
ip->stop.s_addr = addr;
|
|
|
|
}
|
2012-01-24 10:22:33 +00:00
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
if (ntop(&ip->start, host, sizeof(host)) != 0) {
|
2012-01-24 10:22:33 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return strdup(host);
|
2022-06-29 10:37:36 +00:00
|
|
|
}
|
|
|
|
else { /* IPv6 */
|
|
|
|
for (i = 0; i < 16; i++) {
|
|
|
|
if (ip->start6.s6_addr[i] !=ip->stop6.s6_addr[i]) {
|
|
|
|
if (ip->start6.s6_addr[i] > ip->stop6.s6_addr[i]) {
|
|
|
|
invert_IPv6(&ip->start6, &ip->stop6);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ntop6(&ip->start6, host, sizeof(host)) != 0) {
|
2012-01-24 10:22:33 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return strdup(host);
|
2022-06-29 10:37:36 +00:00
|
|
|
}
|
2011-09-29 18:14:46 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return the next IP
|
|
|
|
return NULL if there is no more IP
|
|
|
|
*/
|
|
|
|
char * nutscan_ip_iter_inc(nutscan_ip_iter_t * ip)
|
|
|
|
{
|
2012-01-24 10:22:33 +00:00
|
|
|
char host[SMALLBUF];
|
2011-09-29 18:14:46 +00:00
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
if (ip->type == IPv4) {
|
2011-09-29 18:14:46 +00:00
|
|
|
/* Check if this is the last address to scan */
|
2022-06-29 10:37:36 +00:00
|
|
|
if (ip->start.s_addr == ip->stop.s_addr) {
|
2011-09-29 18:14:46 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* increment the address (need to pass address in host
|
|
|
|
byte order, then pass back in network byte order */
|
2022-06-29 10:37:36 +00:00
|
|
|
ip->start.s_addr = htonl((ntohl(ip->start.s_addr) + 1));
|
2011-09-29 18:14:46 +00:00
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
if (ntop(&ip->start, host, sizeof(host)) != 0) {
|
2012-01-24 10:22:33 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2022-06-29 10:37:36 +00:00
|
|
|
|
2012-01-24 10:22:33 +00:00
|
|
|
return strdup(host);
|
2011-09-29 18:14:46 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Check if this is the last address to scan */
|
2022-06-29 10:37:36 +00:00
|
|
|
if (memcmp(&ip->start6.s6_addr, &ip->stop6.s6_addr,
|
|
|
|
sizeof(ip->start6.s6_addr)) == 0) {
|
2011-09-29 18:14:46 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
increment_IPv6(&ip->start6);
|
2022-06-29 10:37:36 +00:00
|
|
|
if (ntop6(&ip->start6, host, sizeof(host)) != 0) {
|
2012-01-24 10:22:33 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2011-09-29 18:14:46 +00:00
|
|
|
|
2012-01-24 10:22:33 +00:00
|
|
|
return strdup(host);
|
2011-09-29 18:14:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int nutscan_cidr_to_ip(const char * cidr, char ** start_ip, char ** stop_ip)
|
|
|
|
{
|
|
|
|
char * cidr_tok;
|
|
|
|
char * first_ip;
|
|
|
|
char * mask;
|
|
|
|
char * saveptr = NULL;
|
|
|
|
nutscan_ip_iter_t ip;
|
|
|
|
int mask_val;
|
|
|
|
int mask_byte;
|
2022-06-29 10:37:36 +00:00
|
|
|
uint32_t mask_bit; /* 32-bit IPv4 address bitmask */
|
2012-01-24 10:22:33 +00:00
|
|
|
char host[SMALLBUF];
|
|
|
|
struct addrinfo hints;
|
|
|
|
struct addrinfo *res;
|
|
|
|
struct sockaddr_in * s_in;
|
|
|
|
struct sockaddr_in6 * s_in6;
|
2011-09-29 18:14:46 +00:00
|
|
|
|
|
|
|
*start_ip = NULL;
|
|
|
|
*stop_ip = NULL;
|
|
|
|
|
|
|
|
cidr_tok = strdup(cidr);
|
2022-06-29 10:37:36 +00:00
|
|
|
first_ip = strdup(strtok_r(cidr_tok, "/", &saveptr));
|
|
|
|
if (first_ip == NULL) {
|
|
|
|
upsdebugx(0, "WARNING: %s failed to parse first_ip from cidr=%s",
|
|
|
|
__func__, cidr);
|
|
|
|
free(cidr_tok);
|
2011-09-29 18:14:46 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2022-06-29 10:37:36 +00:00
|
|
|
mask = strtok_r(NULL, "/", &saveptr);
|
|
|
|
if (mask == NULL) {
|
|
|
|
upsdebugx(0, "WARNING: %s failed to parse mask from cidr=%s (first_ip=%s)",
|
|
|
|
__func__, cidr, first_ip);
|
|
|
|
free (first_ip);
|
|
|
|
free(cidr_tok);
|
2011-09-29 18:14:46 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2022-06-29 10:37:36 +00:00
|
|
|
upsdebugx(5, "%s: parsed cidr=%s into first_ip=%s and mask=%s",
|
|
|
|
__func__, cidr, first_ip, mask);
|
2011-09-29 18:14:46 +00:00
|
|
|
|
|
|
|
mask_val = atoi(mask);
|
2022-06-29 10:37:36 +00:00
|
|
|
upsdebugx(5, "%s: parsed mask value %d",
|
|
|
|
__func__, mask_val);
|
|
|
|
|
|
|
|
/* NOTE: Sanity-wise, some larger number also makes sense
|
|
|
|
* as the maximum subnet size we would scan. But at least,
|
|
|
|
* this helps avoid scanning the whole Internet just due
|
|
|
|
* to string-parsing errors.
|
|
|
|
*/
|
|
|
|
if (mask_val < 1) {
|
|
|
|
fatalx(EXIT_FAILURE, "Bad netmask: %s", mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Note: this freeing invalidates "mask" and "saveptr" pointer targets */
|
|
|
|
free(cidr_tok);
|
2011-09-29 18:14:46 +00:00
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
/* Detecting IPv4 vs IPv6 */
|
|
|
|
memset(&hints, 0, sizeof(struct addrinfo));
|
2012-01-24 10:22:33 +00:00
|
|
|
hints.ai_family = AF_INET;
|
|
|
|
|
2011-09-29 18:14:46 +00:00
|
|
|
ip.type = IPv4;
|
2012-01-24 10:22:33 +00:00
|
|
|
/* Detecting IPv4 vs IPv6 */
|
2022-06-29 10:37:36 +00:00
|
|
|
if (getaddrinfo(first_ip, NULL, &hints, &res) != 0) {
|
2012-01-24 10:22:33 +00:00
|
|
|
/*Try IPv6 detection */
|
|
|
|
ip.type = IPv6;
|
|
|
|
hints.ai_family = AF_INET6;
|
|
|
|
int ret;
|
2022-06-29 10:37:36 +00:00
|
|
|
if ((ret = getaddrinfo(first_ip, NULL, &hints, &res)) != 0) {
|
2011-09-29 18:14:46 +00:00
|
|
|
free(first_ip);
|
2012-01-24 10:22:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2022-06-29 10:37:36 +00:00
|
|
|
|
|
|
|
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_CAST_ALIGN)
|
|
|
|
# pragma GCC diagnostic push
|
|
|
|
# pragma GCC diagnostic ignored "-Wcast-align"
|
|
|
|
#endif
|
|
|
|
/* Note: we receive a pointer to res above, so have
|
|
|
|
* no control about alignment of its further data */
|
2012-01-24 10:22:33 +00:00
|
|
|
s_in6 = (struct sockaddr_in6 *)res->ai_addr;
|
2022-06-29 10:37:36 +00:00
|
|
|
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_CAST_ALIGN)
|
|
|
|
# pragma GCC diagnostic pop
|
|
|
|
#endif
|
|
|
|
memcpy(&ip.start6, &s_in6->sin6_addr, sizeof(struct in6_addr));
|
2012-01-24 10:22:33 +00:00
|
|
|
freeaddrinfo(res);
|
|
|
|
}
|
|
|
|
else {
|
2022-06-29 10:37:36 +00:00
|
|
|
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_CAST_ALIGN)
|
|
|
|
# pragma GCC diagnostic push
|
|
|
|
# pragma GCC diagnostic ignored "-Wcast-align"
|
|
|
|
#endif
|
|
|
|
/* Note: we receive a pointer to res above, so have
|
|
|
|
* no control about alignment of its further data */
|
2012-01-24 10:22:33 +00:00
|
|
|
s_in = (struct sockaddr_in *)res->ai_addr;
|
2022-06-29 10:37:36 +00:00
|
|
|
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_CAST_ALIGN)
|
|
|
|
# pragma GCC diagnostic pop
|
|
|
|
#endif
|
2012-01-24 10:22:33 +00:00
|
|
|
ip.start = s_in->sin_addr;
|
|
|
|
freeaddrinfo(res);
|
|
|
|
}
|
2011-09-29 18:14:46 +00:00
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
if (ip.type == IPv4) {
|
2011-09-29 18:14:46 +00:00
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
if (mask_val > 0) {
|
2011-09-29 18:14:46 +00:00
|
|
|
mask_val --;
|
|
|
|
mask_bit = 0x80000000;
|
|
|
|
mask_bit >>= mask_val;
|
|
|
|
mask_bit--;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mask_bit = 0xffffffff;
|
|
|
|
}
|
|
|
|
ip.stop.s_addr = htonl(ntohl(ip.start.s_addr)|mask_bit);
|
|
|
|
ip.start.s_addr = htonl(ntohl(ip.start.s_addr)&(~mask_bit));
|
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
if (ntop(&ip.start, host, sizeof(host)) != 0) {
|
2012-01-24 10:22:33 +00:00
|
|
|
*start_ip = NULL;
|
|
|
|
*stop_ip = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*start_ip = strdup(host);
|
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
if (ntop(&ip.stop, host, sizeof(host)) != 0) {
|
2012-01-24 10:22:33 +00:00
|
|
|
free(*start_ip);
|
|
|
|
*start_ip = NULL;
|
|
|
|
*stop_ip = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*stop_ip = strdup(host);
|
|
|
|
|
2011-09-29 18:14:46 +00:00
|
|
|
free(first_ip);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else {
|
2022-06-29 10:37:36 +00:00
|
|
|
if (getaddrinfo(first_ip, NULL, &hints, &res) != 0) {
|
2012-01-24 10:22:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2022-06-29 10:37:36 +00:00
|
|
|
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_CAST_ALIGN)
|
|
|
|
# pragma GCC diagnostic push
|
|
|
|
# pragma GCC diagnostic ignored "-Wcast-align"
|
|
|
|
#endif
|
|
|
|
/* Note: we receive a pointer to res above, so have
|
|
|
|
* no control about alignment of its further data */
|
2012-01-24 10:22:33 +00:00
|
|
|
s_in6 = (struct sockaddr_in6 *)res->ai_addr;
|
2022-06-29 10:37:36 +00:00
|
|
|
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_CAST_ALIGN)
|
|
|
|
# pragma GCC diagnostic pop
|
|
|
|
#endif
|
|
|
|
memcpy(&ip.stop6, &s_in6->sin6_addr, sizeof(struct in6_addr));
|
2012-01-24 10:22:33 +00:00
|
|
|
freeaddrinfo(res);
|
2011-09-29 18:14:46 +00:00
|
|
|
|
|
|
|
mask_byte = mask_val / 8;
|
2022-06-29 10:37:36 +00:00
|
|
|
if (mask_byte < 16 && mask_byte >= 0) {
|
|
|
|
memset(&(ip.stop6.s6_addr[mask_byte + 1]), 0xFF, 15 - (uint8_t)mask_byte);
|
|
|
|
memset(&(ip.start6.s6_addr[mask_byte + 1]), 0x00, 15 - (uint8_t)mask_byte);
|
2011-09-29 18:14:46 +00:00
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
mask_bit = (0x100 >> mask_val%8) - 1;
|
2011-09-29 18:14:46 +00:00
|
|
|
ip.stop6.s6_addr[mask_byte] |= mask_bit;
|
|
|
|
ip.start6.s6_addr[mask_byte] &= (~mask_bit);
|
|
|
|
}
|
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
if (ntop6(&ip.start6, host, sizeof(host)) != 0) {
|
2012-01-24 10:22:33 +00:00
|
|
|
*start_ip = NULL;
|
|
|
|
*stop_ip = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*start_ip = strdup(host);
|
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
if (ntop6(&ip.stop6, host, sizeof(host)) != 0) {
|
2012-01-24 10:22:33 +00:00
|
|
|
free(*start_ip);
|
|
|
|
*start_ip = NULL;
|
|
|
|
*stop_ip = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*stop_ip = strdup(host);
|
2011-09-29 18:14:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
free(first_ip);
|
|
|
|
return 1;
|
|
|
|
}
|