From 53535cd94190dd320b40e0c97cf3f031b4442051 Mon Sep 17 00:00:00 2001 From: radkesvat <134321679+radkesvat@users.noreply.github.com> Date: Fri, 19 Apr 2024 06:55:36 +0000 Subject: [PATCH] remove ipcalc becuase gnu :( --- ww/ipcalc/COPYING | 339 ------ ww/ipcalc/deaggregate.c | 233 ----- ww/ipcalc/ipcalc-geoip.c | 266 ----- ww/ipcalc/ipcalc-maxmind.c | 201 ---- ww/ipcalc/ipcalc-reverse.c | 127 --- ww/ipcalc/ipcalc-utils.c | 83 -- ww/ipcalc/ipcalc.c | 1990 ------------------------------------ ww/ipcalc/ipcalc.h | 156 --- ww/ipcalc/ipv6.c | 81 -- ww/ipcalc/ipv6.h | 29 - ww/ipcalc/netsplit.c | 253 ----- 11 files changed, 3758 deletions(-) delete mode 100644 ww/ipcalc/COPYING delete mode 100644 ww/ipcalc/deaggregate.c delete mode 100644 ww/ipcalc/ipcalc-geoip.c delete mode 100644 ww/ipcalc/ipcalc-maxmind.c delete mode 100644 ww/ipcalc/ipcalc-reverse.c delete mode 100644 ww/ipcalc/ipcalc-utils.c delete mode 100644 ww/ipcalc/ipcalc.c delete mode 100644 ww/ipcalc/ipcalc.h delete mode 100644 ww/ipcalc/ipv6.c delete mode 100644 ww/ipcalc/ipv6.h delete mode 100644 ww/ipcalc/netsplit.c diff --git a/ww/ipcalc/COPYING b/ww/ipcalc/COPYING deleted file mode 100644 index d159169d..00000000 --- a/ww/ipcalc/COPYING +++ /dev/null @@ -1,339 +0,0 @@ - GNU GENERAL PUBLIC LICENSE - Version 2, June 1991 - - Copyright (C) 1989, 1991 Free Software Foundation, Inc., - 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - Everyone is permitted to copy and distribute verbatim copies - of this license document, but changing it is not allowed. - - Preamble - - The licenses for most software are designed to take away your -freedom to share and change it. By contrast, the GNU General Public -License is intended to guarantee your freedom to share and change free -software--to make sure the software is free for all its users. This -General Public License applies to most of the Free Software -Foundation's software and to any other program whose authors commit to -using it. (Some other Free Software Foundation software is covered by -the GNU Lesser General Public License instead.) You can apply it to -your programs, too. - - When we speak of free software, we are referring to freedom, not -price. Our General Public Licenses are designed to make sure that you -have the freedom to distribute copies of free software (and charge for -this service if you wish), that you receive source code or can get it -if you want it, that you can change the software or use pieces of it -in new free programs; and that you know you can do these things. - - To protect your rights, we need to make restrictions that forbid -anyone to deny you these rights or to ask you to surrender the rights. -These restrictions translate to certain responsibilities for you if you -distribute copies of the software, or if you modify it. - - For example, if you distribute copies of such a program, whether -gratis or for a fee, you must give the recipients all the rights that -you have. You must make sure that they, too, receive or can get the -source code. And you must show them these terms so they know their -rights. - - We protect your rights with two steps: (1) copyright the software, and -(2) offer you this license which gives you legal permission to copy, -distribute and/or modify the software. - - Also, for each author's protection and ours, we want to make certain -that everyone understands that there is no warranty for this free -software. If the software is modified by someone else and passed on, we -want its recipients to know that what they have is not the original, so -that any problems introduced by others will not reflect on the original -authors' reputations. - - Finally, any free program is threatened constantly by software -patents. We wish to avoid the danger that redistributors of a free -program will individually obtain patent licenses, in effect making the -program proprietary. To prevent this, we have made it clear that any -patent must be licensed for everyone's free use or not licensed at all. - - The precise terms and conditions for copying, distribution and -modification follow. - - GNU GENERAL PUBLIC LICENSE - TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION - - 0. This License applies to any program or other work which contains -a notice placed by the copyright holder saying it may be distributed -under the terms of this General Public License. The "Program", below, -refers to any such program or work, and a "work based on the Program" -means either the Program or any derivative work under copyright law: -that is to say, a work containing the Program or a portion of it, -either verbatim or with modifications and/or translated into another -language. (Hereinafter, translation is included without limitation in -the term "modification".) Each licensee is addressed as "you". - -Activities other than copying, distribution and modification are not -covered by this License; they are outside its scope. The act of -running the Program is not restricted, and the output from the Program -is covered only if its contents constitute a work based on the -Program (independent of having been made by running the Program). -Whether that is true depends on what the Program does. - - 1. You may copy and distribute verbatim copies of the Program's -source code as you receive it, in any medium, provided that you -conspicuously and appropriately publish on each copy an appropriate -copyright notice and disclaimer of warranty; keep intact all the -notices that refer to this License and to the absence of any warranty; -and give any other recipients of the Program a copy of this License -along with the Program. - -You may charge a fee for the physical act of transferring a copy, and -you may at your option offer warranty protection in exchange for a fee. - - 2. You may modify your copy or copies of the Program or any portion -of it, thus forming a work based on the Program, and copy and -distribute such modifications or work under the terms of Section 1 -above, provided that you also meet all of these conditions: - - a) You must cause the modified files to carry prominent notices - stating that you changed the files and the date of any change. - - b) You must cause any work that you distribute or publish, that in - whole or in part contains or is derived from the Program or any - part thereof, to be licensed as a whole at no charge to all third - parties under the terms of this License. - - c) If the modified program normally reads commands interactively - when run, you must cause it, when started running for such - interactive use in the most ordinary way, to print or display an - announcement including an appropriate copyright notice and a - notice that there is no warranty (or else, saying that you provide - a warranty) and that users may redistribute the program under - these conditions, and telling the user how to view a copy of this - License. (Exception: if the Program itself is interactive but - does not normally print such an announcement, your work based on - the Program is not required to print an announcement.) - -These requirements apply to the modified work as a whole. If -identifiable sections of that work are not derived from the Program, -and can be reasonably considered independent and separate works in -themselves, then this License, and its terms, do not apply to those -sections when you distribute them as separate works. But when you -distribute the same sections as part of a whole which is a work based -on the Program, the distribution of the whole must be on the terms of -this License, whose permissions for other licensees extend to the -entire whole, and thus to each and every part regardless of who wrote it. - -Thus, it is not the intent of this section to claim rights or contest -your rights to work written entirely by you; rather, the intent is to -exercise the right to control the distribution of derivative or -collective works based on the Program. - -In addition, mere aggregation of another work not based on the Program -with the Program (or with a work based on the Program) on a volume of -a storage or distribution medium does not bring the other work under -the scope of this License. - - 3. You may copy and distribute the Program (or a work based on it, -under Section 2) in object code or executable form under the terms of -Sections 1 and 2 above provided that you also do one of the following: - - a) Accompany it with the complete corresponding machine-readable - source code, which must be distributed under the terms of Sections - 1 and 2 above on a medium customarily used for software interchange; or, - - b) Accompany it with a written offer, valid for at least three - years, to give any third party, for a charge no more than your - cost of physically performing source distribution, a complete - machine-readable copy of the corresponding source code, to be - distributed under the terms of Sections 1 and 2 above on a medium - customarily used for software interchange; or, - - c) Accompany it with the information you received as to the offer - to distribute corresponding source code. (This alternative is - allowed only for noncommercial distribution and only if you - received the program in object code or executable form with such - an offer, in accord with Subsection b above.) - -The source code for a work means the preferred form of the work for -making modifications to it. For an executable work, complete source -code means all the source code for all modules it contains, plus any -associated interface definition files, plus the scripts used to -control compilation and installation of the executable. However, as a -special exception, the source code distributed need not include -anything that is normally distributed (in either source or binary -form) with the major components (compiler, kernel, and so on) of the -operating system on which the executable runs, unless that component -itself accompanies the executable. - -If distribution of executable or object code is made by offering -access to copy from a designated place, then offering equivalent -access to copy the source code from the same place counts as -distribution of the source code, even though third parties are not -compelled to copy the source along with the object code. - - 4. You may not copy, modify, sublicense, or distribute the Program -except as expressly provided under this License. Any attempt -otherwise to copy, modify, sublicense or distribute the Program is -void, and will automatically terminate your rights under this License. -However, parties who have received copies, or rights, from you under -this License will not have their licenses terminated so long as such -parties remain in full compliance. - - 5. You are not required to accept this License, since you have not -signed it. However, nothing else grants you permission to modify or -distribute the Program or its derivative works. These actions are -prohibited by law if you do not accept this License. Therefore, by -modifying or distributing the Program (or any work based on the -Program), you indicate your acceptance of this License to do so, and -all its terms and conditions for copying, distributing or modifying -the Program or works based on it. - - 6. Each time you redistribute the Program (or any work based on the -Program), the recipient automatically receives a license from the -original licensor to copy, distribute or modify the Program subject to -these terms and conditions. You may not impose any further -restrictions on the recipients' exercise of the rights granted herein. -You are not responsible for enforcing compliance by third parties to -this License. - - 7. If, as a consequence of a court judgment or allegation of patent -infringement or for any other reason (not limited to patent issues), -conditions are imposed on you (whether by court order, agreement or -otherwise) that contradict the conditions of this License, they do not -excuse you from the conditions of this License. If you cannot -distribute so as to satisfy simultaneously your obligations under this -License and any other pertinent obligations, then as a consequence you -may not distribute the Program at all. For example, if a patent -license would not permit royalty-free redistribution of the Program by -all those who receive copies directly or indirectly through you, then -the only way you could satisfy both it and this License would be to -refrain entirely from distribution of the Program. - -If any portion of this section is held invalid or unenforceable under -any particular circumstance, the balance of the section is intended to -apply and the section as a whole is intended to apply in other -circumstances. - -It is not the purpose of this section to induce you to infringe any -patents or other property right claims or to contest validity of any -such claims; this section has the sole purpose of protecting the -integrity of the free software distribution system, which is -implemented by public license practices. Many people have made -generous contributions to the wide range of software distributed -through that system in reliance on consistent application of that -system; it is up to the author/donor to decide if he or she is willing -to distribute software through any other system and a licensee cannot -impose that choice. - -This section is intended to make thoroughly clear what is believed to -be a consequence of the rest of this License. - - 8. If the distribution and/or use of the Program is restricted in -certain countries either by patents or by copyrighted interfaces, the -original copyright holder who places the Program under this License -may add an explicit geographical distribution limitation excluding -those countries, so that distribution is permitted only in or among -countries not thus excluded. In such case, this License incorporates -the limitation as if written in the body of this License. - - 9. The Free Software Foundation may publish revised and/or new versions -of the General Public License from time to time. Such new versions will -be similar in spirit to the present version, but may differ in detail to -address new problems or concerns. - -Each version is given a distinguishing version number. If the Program -specifies a version number of this License which applies to it and "any -later version", you have the option of following the terms and conditions -either of that version or of any later version published by the Free -Software Foundation. If the Program does not specify a version number of -this License, you may choose any version ever published by the Free Software -Foundation. - - 10. If you wish to incorporate parts of the Program into other free -programs whose distribution conditions are different, write to the author -to ask for permission. For software which is copyrighted by the Free -Software Foundation, write to the Free Software Foundation; we sometimes -make exceptions for this. Our decision will be guided by the two goals -of preserving the free status of all derivatives of our free software and -of promoting the sharing and reuse of software generally. - - NO WARRANTY - - 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY -FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN -OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES -PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED -OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF -MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS -TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE -PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, -REPAIR OR CORRECTION. - - 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING -WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR -REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, -INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING -OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED -TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY -YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER -PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE -POSSIBILITY OF SUCH DAMAGES. - - END OF TERMS AND CONDITIONS - - How to Apply These Terms to Your New Programs - - If you develop a new program, and you want it to be of the greatest -possible use to the public, the best way to achieve this is to make it -free software which everyone can redistribute and change under these terms. - - To do so, attach the following notices to the program. It is safest -to attach them to the start of each source file to most effectively -convey the exclusion of warranty; and each file should have at least -the "copyright" line and a pointer to where the full notice is found. - - - Copyright (C) - - 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., - 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - -Also add information on how to contact you by electronic and paper mail. - -If the program is interactive, make it output a short notice like this -when it starts in an interactive mode: - - Gnomovision version 69, Copyright (C) year name of author - Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. - This is free software, and you are welcome to redistribute it - under certain conditions; type `show c' for details. - -The hypothetical commands `show w' and `show c' should show the appropriate -parts of the General Public License. Of course, the commands you use may -be called something other than `show w' and `show c'; they could even be -mouse-clicks or menu items--whatever suits your program. - -You should also get your employer (if you work as a programmer) or your -school, if any, to sign a "copyright disclaimer" for the program, if -necessary. Here is a sample; alter the names: - - Yoyodyne, Inc., hereby disclaims all copyright interest in the program - `Gnomovision' (which makes passes at compilers) written by James Hacker. - - , 1 April 1989 - Ty Coon, President of Vice - -This General Public License does not permit incorporating your program into -proprietary programs. If your program is a subroutine library, you may -consider it more useful to permit linking proprietary applications with the -library. If this is what you want to do, use the GNU Lesser General -Public License instead of this License. diff --git a/ww/ipcalc/deaggregate.c b/ww/ipcalc/deaggregate.c deleted file mode 100644 index c6174f68..00000000 --- a/ww/ipcalc/deaggregate.c +++ /dev/null @@ -1,233 +0,0 @@ -/* - * Copyright (c) 2019 Nikos Mavrogiannopoulos - * - * 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, see . - */ - -#include -#include -#include -#include -#include -#include -#include -#include - -#include "ipcalc.h" -#include "ipv6.h" - -static void deaggregate_v4(const char *ip1s, const char *ip2s, unsigned flags); -static void deaggregate_v6(const char *ip1s, const char *ip2s, unsigned flags); - -static char *trim(char *str) -{ - int len, i; - char *out; - - len = strlen(str); - for (i=len-1;i>=0;i--) { - if (isspace(str[i])) - str[i] = 0; - else - break; - } - - out = str; - for (i=0;i end) { - if (!beSilent) - fprintf(stderr, "ipcalc: bad range\n"); - exit(1); - } - - output_start(&jsonchain); - array_start(&jsonchain, "Deaggregated networks", "DEAGGREGATEDNETWORK"); - - while (base <= end) { - step = 0; - while ((base | (1 << step)) != base) { - if ((base | (UINT32_MAX >> (31-step))) > end) - break; - step++; - } - - print_ipv4_net(&jsonchain, base, 32-step, flags); - base += (1 << step); - } - - array_stop(&jsonchain); - output_stop(&jsonchain); - - return; -} - -static void print_ipv6_net(unsigned *jsonchain, struct in6_addr *ip, unsigned prefix, unsigned flags) -{ - char namebuf[INET6_ADDRSTRLEN + 1] = {0}; - - if (inet_ntop(AF_INET6, ip, namebuf, sizeof(namebuf)) == NULL) { - fprintf(stderr, "inet_ntop failure at line %d\n", - __LINE__); - exit(1); - } - - default_printf(jsonchain, "Network:\t", NULL, "%s/%u", namebuf, prefix); -} - -static unsigned ipv6_base_ok(struct in6_addr *base, unsigned step) -{ - struct in6_addr b2; - memcpy(&b2, base, sizeof(b2)); - - ipv6_or1(&b2, step); - - return memcmp(base->s6_addr, &b2.s6_addr, 16); -} - -void deaggregate_v6(const char *ip1s, const char *ip2s, unsigned flags) -{ - struct in6_addr ip1, ip2; - unsigned step; - struct in6_addr base, end; - struct in6_addr tmp; - unsigned jsonchain; - - if (inet_pton(AF_INET6, ip1s, &ip1) <= 0) { - if (!beSilent) - fprintf(stderr, "ipcalc: bad IPv6 address: %s\n", - ip1s); - exit(1); - } - - if (inet_pton(AF_INET6, ip2s, &ip2) <= 0) { - if (!beSilent) - fprintf(stderr, "ipcalc: bad IPv6 address: %s\n", - ip2s); - exit(1); - } - - memcpy(&base, &ip1, sizeof(base)); - memcpy(&end, &ip2, sizeof(end)); - - if (ipv6_cmp(&base, &end) > 0) { - if (!beSilent) - fprintf(stderr, "ipcalc: bad IPv6 range\n"); - exit(1); - } - - output_start(&jsonchain); - array_start(&jsonchain, "Deaggregated networks", "DEAGGREGATEDNETWORK"); - - - while (ipv6_cmp(&base, &end) <= 0) { - step = 0; - while (ipv6_base_ok(&base, step)) { - memcpy(&tmp, &base, sizeof(tmp)); - ipv6_orm(&tmp, step+1); - if (ipv6_cmp(&tmp, &end) > 0) - break; - step++; - } - - print_ipv6_net(&jsonchain, &base, 128-step, flags); - memset(&tmp, 0, sizeof(tmp)); - ipv6_or1(&tmp, step); - - /* v6add */ - ipv6_add(&base, &tmp); - } - - array_stop(&jsonchain); - output_stop(&jsonchain); - - return; -} diff --git a/ww/ipcalc/ipcalc-geoip.c b/ww/ipcalc/ipcalc-geoip.c deleted file mode 100644 index 6f2b513d..00000000 --- a/ww/ipcalc/ipcalc-geoip.c +++ /dev/null @@ -1,266 +0,0 @@ -/* - * Copyright (c) 2015 Red Hat, Inc. All rights reserved. - * - * 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, see . - * - * Authors: - * Nikos Mavrogiannopoulos - */ - -#define _GNU_SOURCE /* asprintf */ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "ipcalc.h" - -#ifdef USE_GEOIP - -# include -# include - -#define GEOIP_SILENCE 16 /* fix libgeoip < 1.6.3 */ - -# ifdef USE_RUNTIME_LINKING -# include - -typedef void (*_GeoIP_setup_dbfilename_func)(void); -typedef GeoIP * (*GeoIP_open_type_func)(int type, int flags); -typedef const char * (*GeoIP_country_name_by_id_func)(GeoIP * gi, int id); -typedef void (*GeoIP_delete_func)(GeoIP * gi); -typedef GeoIPRecord * (*GeoIP_record_by_ipnum_func)(GeoIP * gi, unsigned long ipnum); -typedef int (*GeoIP_id_by_ipnum_func)(GeoIP * gi, unsigned long ipnum); -typedef int (*GeoIP_id_by_ipnum_v6_func)(GeoIP * gi, geoipv6_t ipnum); -typedef GeoIPRecord *(*GeoIP_record_by_ipnum_v6_func)(GeoIP * gi, geoipv6_t ipnum); -typedef const char *(*GeoIP_code_by_id_func)(int id); - -static _GeoIP_setup_dbfilename_func p_GeoIP_setup_dbfilename; -static GeoIP_open_type_func pGeoIP_open_type; -static GeoIP_country_name_by_id_func pGeoIP_country_name_by_id; -static GeoIP_code_by_id_func pGeoIP_code_by_id; -static GeoIP_delete_func pGeoIP_delete; -static GeoIP_record_by_ipnum_func pGeoIP_record_by_ipnum; -static GeoIP_id_by_ipnum_func pGeoIP_id_by_ipnum; -static GeoIP_id_by_ipnum_v6_func pGeoIP_id_by_ipnum_v6; -static GeoIP_record_by_ipnum_v6_func pGeoIP_record_by_ipnum_v6; - -#define LIBNAME LIBPATH"/libGeoIP.so.1" - -int geo_setup(void) -{ - static void *ld = NULL; - static int ret = 0; - static char err[256] = {0}; - - if (ld != NULL || ret != 0) { - if (!beSilent && err[0] != 0) { - fprintf(stderr, "%s", err); - } - return ret; - } - - ld = dlopen(LIBNAME, RTLD_LAZY); - if (ld == NULL) { - snprintf(err, sizeof(err), "ipcalc: could not open %s\n", LIBNAME); - ret = -1; - goto exit; - } - - p_GeoIP_setup_dbfilename = dlsym(ld, "_GeoIP_setup_dbfilename"); - - pGeoIP_open_type = dlsym(ld, "GeoIP_open_type"); - pGeoIP_country_name_by_id = dlsym(ld, "GeoIP_country_name_by_id"); - pGeoIP_delete = dlsym(ld, "GeoIP_delete"); - pGeoIP_record_by_ipnum = dlsym(ld, "GeoIP_record_by_ipnum"); - pGeoIP_id_by_ipnum = dlsym(ld, "GeoIP_id_by_ipnum"); - pGeoIP_id_by_ipnum_v6 = dlsym(ld, "GeoIP_id_by_ipnum_v6"); - pGeoIP_record_by_ipnum_v6 = dlsym(ld, "GeoIP_record_by_ipnum_v6"); - pGeoIP_code_by_id = dlsym(ld, "GeoIP_code_by_id"); - - if (pGeoIP_open_type == NULL || pGeoIP_country_name_by_id == NULL || - pGeoIP_delete == NULL || pGeoIP_record_by_ipnum == NULL || - pGeoIP_id_by_ipnum == NULL || pGeoIP_id_by_ipnum_v6 == NULL || - pGeoIP_record_by_ipnum_v6 == NULL) { - snprintf(err, sizeof(err), "ipcalc: could not find symbols in libGeoIP\n"); - ret = -1; - goto exit; - } - - ret = 0; - exit: - return ret; -} - -# else - -extern void _GeoIP_setup_dbfilename(void); -# define p_GeoIP_setup_dbfilename _GeoIP_setup_dbfilename -# define pGeoIP_open_type GeoIP_open_type -# define pGeoIP_country_name_by_id GeoIP_country_name_by_id -# define pGeoIP_delete GeoIP_delete -# define pGeoIP_record_by_ipnum GeoIP_record_by_ipnum -# define pGeoIP_id_by_ipnum GeoIP_id_by_ipnum -# define pGeoIP_id_by_ipnum_v6 GeoIP_id_by_ipnum_v6 -# define pGeoIP_record_by_ipnum_v6 GeoIP_record_by_ipnum_v6 -# define pGeoIP_code_by_id GeoIP_code_by_id -# endif - -static void geo_ipv4_lookup(struct in_addr ip, char **country, char **ccode, char **city, char **coord) -{ - GeoIP *gi; - GeoIPRecord *gir; - int country_id; - const char *p; - - if (geo_setup() != 0) - return; - - ip.s_addr = ntohl(ip.s_addr); - - p_GeoIP_setup_dbfilename(); - - gi = pGeoIP_open_type(GEOIP_COUNTRY_EDITION, GEOIP_STANDARD | GEOIP_SILENCE); - if (gi != NULL) { - gi->charset = GEOIP_CHARSET_UTF8; - - country_id = pGeoIP_id_by_ipnum(gi, ip.s_addr); - if (country_id < 0) { - return; - } - p = pGeoIP_country_name_by_id(gi, country_id); - if (p) - *country = safe_strdup(p); - - p = pGeoIP_code_by_id(country_id); - if (p) - *ccode = safe_strdup(p); - - pGeoIP_delete(gi); - } - - gi = pGeoIP_open_type(GEOIP_CITY_EDITION_REV1, GEOIP_STANDARD | GEOIP_SILENCE); - if (gi != NULL) { - gi->charset = GEOIP_CHARSET_UTF8; - - gir = pGeoIP_record_by_ipnum(gi, ip.s_addr); - - if (gir && gir->city) - *city = safe_strdup(gir->city); - - if (gir && gir->longitude != 0 && gir->longitude != 0) - safe_asprintf(coord, "%f,%f", gir->latitude, gir->longitude); - - pGeoIP_delete(gi); - } else { - gi = pGeoIP_open_type(GEOIP_CITY_EDITION_REV0, GEOIP_STANDARD | GEOIP_SILENCE); - if (gi != NULL) { - gi->charset = GEOIP_CHARSET_UTF8; - - gir = pGeoIP_record_by_ipnum(gi, ip.s_addr); - - if (gir && gir->city) - *city = safe_strdup(gir->city); - - if (gir && gir->longitude != 0 && gir->longitude != 0) - safe_asprintf(coord, "%f,%f", gir->latitude, gir->longitude); - - pGeoIP_delete(gi); - } - } - - return; -} - -static void geo_ipv6_lookup(struct in6_addr *ip, char **country, char **ccode, char **city, char **coord) -{ - GeoIP *gi; - GeoIPRecord *gir; - int country_id; - const char *p; - - if (geo_setup() != 0) - return; - - p_GeoIP_setup_dbfilename(); - - gi = pGeoIP_open_type(GEOIP_COUNTRY_EDITION_V6, GEOIP_STANDARD | GEOIP_SILENCE); - if (gi != NULL) { - gi->charset = GEOIP_CHARSET_UTF8; - - country_id = pGeoIP_id_by_ipnum_v6(gi, (geoipv6_t)*ip); - if (country_id < 0) { - return; - } - p = pGeoIP_country_name_by_id(gi, country_id); - if (p) - *country = safe_strdup(p); - - p = pGeoIP_code_by_id(country_id); - if (p) - *ccode = safe_strdup(p); - - pGeoIP_delete(gi); - } - - gi = pGeoIP_open_type(GEOIP_CITY_EDITION_REV1_V6, GEOIP_STANDARD | GEOIP_SILENCE); - if (gi != NULL) { - gi->charset = GEOIP_CHARSET_UTF8; - - gir = pGeoIP_record_by_ipnum_v6(gi, (geoipv6_t)*ip); - - if (gir && gir->city) - *city = safe_strdup(gir->city); - - if (gir && gir->longitude != 0 && gir->longitude != 0) - safe_asprintf(coord, "%f,%f", gir->latitude, gir->longitude); - - pGeoIP_delete(gi); - } else { - gi = pGeoIP_open_type(GEOIP_CITY_EDITION_REV0_V6, GEOIP_STANDARD | GEOIP_SILENCE); - if (gi != NULL) { - gi->charset = GEOIP_CHARSET_UTF8; - - gir = pGeoIP_record_by_ipnum_v6(gi, (geoipv6_t)*ip); - - if (gir && gir->city) - *city = safe_strdup(gir->city); - - if (gir && gir->longitude != 0 && gir->longitude != 0) - safe_asprintf(coord, "%f,%f", gir->latitude, gir->longitude); - - pGeoIP_delete(gi); - } - } - - return; -} - -void geo_ip_lookup(const char *ip, char **country, char **ccode, char **city, char **coord) -{ - struct in_addr ipv4; - struct in6_addr ipv6; - if (inet_pton(AF_INET, ip, &ipv4) == 1) { - geo_ipv4_lookup(ipv4, country, ccode, city, coord); - } else if (inet_pton(AF_INET6, ip, &ipv6) == 1) { - geo_ipv6_lookup(&ipv6, country, ccode, city, coord); - } - return; -} - -#endif diff --git a/ww/ipcalc/ipcalc-maxmind.c b/ww/ipcalc/ipcalc-maxmind.c deleted file mode 100644 index a2f86547..00000000 --- a/ww/ipcalc/ipcalc-maxmind.c +++ /dev/null @@ -1,201 +0,0 @@ -/* - * Copyright (c) 2018 Red Hat, Inc. All rights reserved. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License, version 2, - * as published by the Free Software Foundation. - * - * 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 . - * - * Authors: - * Martin Sehnoutka - */ - -#define _GNU_SOURCE -#include -#include -#include -#include -#include - -#include "ipcalc.h" - -#ifdef USE_MAXMIND -#include - -#ifndef MAXMINDDB_LOCATION_COUNTRY -#define MAXMINDDB_LOCATION_COUNTRY "/usr/share/GeoIP/GeoLite2-Country.mmdb" -#endif - -#ifndef MAXMINDDB_LOCATION_CITY -#define MAXMINDDB_LOCATION_CITY "/usr/share/GeoIP/GeoLite2-City.mmdb" -#endif - -#ifdef USE_RUNTIME_LINKING -# include -# define LIBNAME LIBPATH"/libmaxminddb.so.0" - -typedef int (*MMDB_open_fn) - (const char *const filename, - uint32_t flags, - MMDB_s *const mmdb); -typedef void (*MMDB_close_fn) - (MMDB_s *const mmdb); -typedef MMDB_lookup_result_s (*MMDB_lookup_string_fn) - (MMDB_s *const mmdb, - const char *const ipstr, - int *const gai_error, - int *const mmdb_error); -typedef int (*MMDB_get_value_fn) - (MMDB_entry_s *const start, - MMDB_entry_data_s *const entry_data, - ...); - -static MMDB_close_fn pMMDB_close; -static MMDB_get_value_fn pMMDB_get_value; -static MMDB_lookup_string_fn pMMDB_lookup_string; -static MMDB_open_fn pMMDB_open; - -int geo_setup(void) -{ - static void *ld = NULL; - static int ret = 0; - static char err[256] = {0}; - - if (ld != NULL || ret != 0) { - if (!beSilent && err[0] != 0) { - fprintf(stderr, "%s", err); - } - return ret; - } - - ld = dlopen(LIBNAME, RTLD_LAZY); - if (ld == NULL) { - snprintf(err, sizeof(err), "ipcalc: could not open %s\n", LIBNAME); - ret = -1; - goto exit; - } - - pMMDB_close = dlsym(ld, "MMDB_close"); - pMMDB_get_value = dlsym(ld, "MMDB_get_value"); - pMMDB_lookup_string = dlsym(ld, "MMDB_lookup_string"); - pMMDB_open = dlsym(ld, "MMDB_open"); - - if(pMMDB_close == NULL || - pMMDB_get_value == NULL || - pMMDB_lookup_string == NULL || - pMMDB_open == NULL) { - snprintf(err, sizeof(err), "ipcalc: could not find symbols in libmaxmind\n"); - ret = -1; - goto exit; - } - - ret = 0; - exit: - return ret; -} - -#else -#define pMMDB_close MMDB_close -#define pMMDB_get_value MMDB_get_value -#define pMMDB_lookup_string MMDB_lookup_string -#define pMMDB_open MMDB_open -#endif - -void process_result_from_mmdb_lookup(MMDB_entry_data_s *entry_data, int status, char **output) -{ - if (MMDB_SUCCESS == status) { - if (entry_data->has_data) { - if (entry_data->type == MMDB_DATA_TYPE_UTF8_STRING) { - *output = (char *) calloc(entry_data->data_size + 1, sizeof(char)); - if (NULL != *output) { - memcpy(*output, entry_data->utf8_string, entry_data->data_size); - } else { - fprintf(stderr, "Memory allocation failure line %d\n", __LINE__); - } - } - } - } - /* Else fail silently */ -} - -void geo_ip_lookup(const char *ip, char **country, char **ccode, char **city, char **coord) -{ - MMDB_s mmdb; - MMDB_entry_data_s entry_data; - int gai_error, mmdb_error, status, coordinates=0; - double latitude = 0, longitude = 0; - - if (geo_setup() != 0) - return; - - /* Open the system maxmind database with countries */ - status = pMMDB_open(MAXMINDDB_LOCATION_COUNTRY, MMDB_MODE_MMAP, &mmdb); - if (MMDB_SUCCESS == status) { - /* Lookup IP address in the database */ - MMDB_lookup_result_s result = pMMDB_lookup_string(&mmdb, ip, &gai_error, &mmdb_error); - if (MMDB_SUCCESS == mmdb_error) { - /* If the lookup was successfull and an entry was found */ - if (result.found_entry) { - memset(&entry_data, 0, sizeof(MMDB_entry_data_s)); - /* Travel the path in the tree like structure of the MMDB and store the value if found */ - status = pMMDB_get_value(&result.entry, &entry_data, "country", "names", "en", NULL); - process_result_from_mmdb_lookup(&entry_data, status, country); - memset(&entry_data, 0, sizeof(MMDB_entry_data_s)); - status = pMMDB_get_value(&result.entry, &entry_data, "country", "iso_code", NULL); - process_result_from_mmdb_lookup(&entry_data, status, ccode); - } - } - /* Else fail silently */ - pMMDB_close(&mmdb); - } - /* Else fail silently */ - - /* Open the system maxmind database with cities - which actually does not contain names of the cities */ - status = pMMDB_open(MAXMINDDB_LOCATION_CITY, MMDB_MODE_MMAP, &mmdb); - if (MMDB_SUCCESS == status) { - /* Lookup IP address in the database */ - MMDB_lookup_result_s result = pMMDB_lookup_string(&mmdb, ip, &gai_error, &mmdb_error); - if (MMDB_SUCCESS == mmdb_error) { - /* If the lookup was successfull and an entry was found */ - if (result.found_entry) { - memset(&entry_data, 0, sizeof(MMDB_entry_data_s)); - // NOTE: Information about the city is not available in the free database, so there is not way - // for me to implement this functionality right now, but it should be easy to add for anyone with - // access to the paid databases. - status = pMMDB_get_value(&result.entry, &entry_data, "location", "latitude", NULL); - if (MMDB_SUCCESS == status) { - if (entry_data.has_data) { - if (entry_data.type == MMDB_DATA_TYPE_DOUBLE) { - latitude = entry_data.double_value; - ++coordinates; - } - } - } - status = pMMDB_get_value(&result.entry, &entry_data, "location", "longitude", NULL); - if (MMDB_SUCCESS == status) { - if (entry_data.has_data) { - if (entry_data.type == MMDB_DATA_TYPE_DOUBLE) { - longitude = entry_data.double_value; - ++coordinates; - } - } - } - if (coordinates == 2) { - safe_asprintf(coord, "%f,%f", latitude, longitude); - } - } - } - /* Else fail silently */ - pMMDB_close(&mmdb); - } - /* Else fail silently */ -} - -#endif diff --git a/ww/ipcalc/ipcalc-reverse.c b/ww/ipcalc/ipcalc-reverse.c deleted file mode 100644 index 3863ef6f..00000000 --- a/ww/ipcalc/ipcalc-reverse.c +++ /dev/null @@ -1,127 +0,0 @@ -/* - * Copyright (c) 2015 Red Hat, Inc. All rights reserved. - * - * 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, see . - * - * Authors: - * Nikos Mavrogiannopoulos - */ - -#define _GNU_SOURCE /* asprintf */ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "ipcalc.h" - -/* draft-ietf-dnsop-rfc2317bis-00 replaces that legacy style - */ -#undef USE_RFC2317_STYLE - -char *calc_reverse_dns4(struct in_addr ip, unsigned prefix, struct in_addr network, struct in_addr broadcast) -{ - char *str = NULL; - int ret = -1; - - unsigned byte1 = (ntohl(ip.s_addr) >> 24) & 0xff; - unsigned byte2 = (ntohl(ip.s_addr) >> 16) & 0xff; - unsigned byte3 = (ntohl(ip.s_addr) >> 8) & 0xff; - unsigned byte4 = (ntohl(ip.s_addr)) & 0xff; - -#ifdef USE_RFC2317_STYLE - if (prefix == 32) { - ret = asprintf(&str, "%u.%u.%u.%u.in-addr.arpa.", byte4, byte3, byte2, byte1); - } else if (prefix == 24) { - ret = asprintf(&str, "%u.%u.%u.in-addr.arpa.", byte3, byte2, byte1); - } else if (prefix == 16) { - ret = asprintf(&str, "%u.%u.in-addr.arpa.", byte2, byte1); - } else if (prefix == 8) { - ret = asprintf(&str, "%u.in-addr.arpa.", byte1); - } else if (prefix > 24) { - ret = asprintf(&str, "%u/%u.%u.%u.%u.in-addr.arpa.", byte4, prefix, byte3, byte2, byte1); - } else if (prefix > 16) { - ret = asprintf(&str, "%u/%u.%u.%u.in-addr.arpa.", byte3, prefix, byte2, byte1); - } else if (prefix > 8) { - ret = asprintf(&str, "%u/%u.%u.in-addr.arpa.", byte2, prefix, byte1); - } -#else - if (prefix == 32) { - ret = asprintf(&str, "%u.%u.%u.%u.in-addr.arpa.", byte4, byte3, byte2, byte1); - } else if (prefix == 24) { - ret = asprintf(&str, "%u.%u.%u.in-addr.arpa.", byte3, byte2, byte1); - } else if (prefix == 16) { - ret = asprintf(&str, "%u.%u.in-addr.arpa.", byte2, byte1); - } else if (prefix == 8) { - ret = asprintf(&str, "%u.in-addr.arpa.", byte1); - } else if (prefix > 24) { - unsigned min = (ntohl(network.s_addr)) & 0xff; - unsigned max = (ntohl(broadcast.s_addr)) & 0xff; - ret = asprintf(&str, "%u-%u.%u.%u.%u.in-addr.arpa.", min, max, byte3, byte2, byte1); - } else if (prefix > 16) { - unsigned min = (ntohl(network.s_addr) >> 8) & 0xff; - unsigned max = (ntohl(broadcast.s_addr) >> 8) & 0xff; - ret = asprintf(&str, "%u-%u.%u.%u.in-addr.arpa.", min, max, byte2, byte1); - } else if (prefix > 8) { - unsigned min = (ntohl(network.s_addr) >> 16) & 0xff; - unsigned max = (ntohl(broadcast.s_addr) >> 16) & 0xff; - ret = asprintf(&str, "%u-%u.%u.in-addr.arpa.", min, max, byte1); - } -#endif - - if (ret == -1) - return NULL; - return str; -} - -static char hexchar(unsigned int val) -{ - if (val < 10) - return '0' + val; - if (val < 16) - return 'a' + val - 10; - abort(); -} - -char *calc_reverse_dns6(struct in6_addr *ip, unsigned prefix) -{ - unsigned i, j = 0; - char str[256]; - unsigned max = prefix/8; - - if (prefix % 4 != 0) - return NULL; - - if (prefix % 8 == 4) { - str[j++] = hexchar(ip->s6_addr[(prefix+4)/8-1] >> 4); - str[j++] = '.'; - } - - for (i=0;is6_addr[max-1-i] & 0xf); - str[j++] = '.'; - - str[j++] = hexchar(ip->s6_addr[max-1-i] >> 4); - str[j++] = '.'; - - } - - strcpy(&str[j], "ip6.arpa."); - - return strdup(str); -} diff --git a/ww/ipcalc/ipcalc-utils.c b/ww/ipcalc/ipcalc-utils.c deleted file mode 100644 index 026e0690..00000000 --- a/ww/ipcalc/ipcalc-utils.c +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Copyright (c) 2018 Red Hat, Inc. All rights reserved. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License, version 2, - * as published by the Free Software Foundation. - * - * 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 . - * - * Authors: - * Martin Sehnoutka - */ - -#define _GNU_SOURCE -#include -#include -#include -#include -#include - -int __attribute__((__format__(printf, 2, 3))) safe_asprintf(char **strp, const char *fmt, ...) -{ - int ret; - va_list args; - - va_start(args, fmt); - ret = vasprintf(&(*strp), fmt, args); - va_end(args); - if (ret < 0) { - fprintf(stderr, "Memory allocation failure\n"); - exit(1); - } - return ret; -} - -int safe_atoi(const char *s, int *ret_i) -{ - char *x = NULL; - long l; - - errno = 0; - l = strtol(s, &x, 0); - - if (!x || x == s || *x || errno) - return errno > 0 ? -errno : -EINVAL; - - if ((long)(int)l != l) - return -ERANGE; - - *ret_i = (int)l; - return 0; -} - -/*! - \fn char safe_strdup(const char *s) - \brief strdup(3) that checks memory allocation or fail - - This function does the same as strdup(3) with additional memory allocation - check. When check fails the function will cause program to exit. - - \param string to be duplicated - \return allocated duplicate -*/ -extern char __attribute__((warn_unused_result)) *safe_strdup(const char *str) -{ - char *ret; - - if (!str) - return NULL; - - ret = strdup(str); - if (!ret) { - fprintf(stderr, "Memory allocation failure\n"); - exit(1); - } - return ret; -} diff --git a/ww/ipcalc/ipcalc.c b/ww/ipcalc/ipcalc.c deleted file mode 100644 index 13a0fb74..00000000 --- a/ww/ipcalc/ipcalc.c +++ /dev/null @@ -1,1990 +0,0 @@ -/* - * Copyright (c) 1997-2015 Red Hat, Inc. All rights reserved. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License, version 2, - * as published by the Free Software Foundation. - * - * 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 . - * - * Authors: - * Nikos Mavrogiannopoulos - * Erik Troan - * Preston Brown - * David Cantrell - */ - -#define _GNU_SOURCE /* asprintf */ -#include -#include -#include -#include -#include -#include -#include -#include -#include /* open */ -#include /* open */ -#include /* read */ -#include -#include -#include -#include /* clock_gettime */ -#include "ipcalc.h" - -int beSilent = 0; -static unsigned colors = 0; -static unsigned flags = 0; - -/*! - \file ipcalc.c - \brief provides utilities for manipulating IP addresses. - - ipcalc provides utilities and a front-end command line interface for - manipulating IP addresses, and calculating various aspects of an ip - address/netmask/network address/prefix/etc. - - Functionality can be accessed from other languages from the library - interface, documented here. To use ipcalc from the shell, read the - ipcalc(1) manual page. - - When passing parameters to the various functions, take note of whether they - take host byte order or network byte order. Most take host byte order, and - return host byte order, but there are some exceptions. -*/ - -/*! - \fn uint32_t prefix2mask(int bits) - \brief creates a netmask from a specified number of bits - - This function converts a prefix length to a netmask. As CIDR (classless - internet domain internet domain routing) has taken off, more an more IP - addresses are being specified in the format address/prefix - (i.e. 192.168.2.3/24, with a corresponding netmask 255.255.255.0). If you - need to see what netmask corresponds to the prefix part of the address, this - is the function. See also \ref mask2prefix. - - \param prefix is the number of bits to create a mask for. - \return a network mask, in network byte order. -*/ -uint32_t prefix2mask(int prefix) -{ - struct in_addr mask; - memset(&mask, 0, sizeof(mask)); - if (prefix) { - return htonl(~((1 << (32 - prefix)) - 1)); - } else { - return htonl(0); - } -} - -/*! - \fn struct in_addr calc_broadcast(struct in_addr addr, int prefix) - - \brief calculate broadcast address given an IP address and a prefix length. - - \param addr an IP address in network byte order. - \param prefix a prefix length. - - \return the calculated broadcast address for the network, in network byte - order. -*/ -static struct in_addr calc_broadcast(struct in_addr addr, int prefix) -{ - struct in_addr mask; - struct in_addr broadcast; - - mask.s_addr = prefix2mask(prefix); - - memset(&broadcast, 0, sizeof(broadcast)); - - /* Follow RFC3021 and set the limited broadcast address on /31 */ - if (prefix == 31) - broadcast.s_addr = htonl(0xFFFFFFFF); - else - broadcast.s_addr = (addr.s_addr & mask.s_addr) | ~mask.s_addr; - - return broadcast; -} - -/*! - \fn struct in_addr calc_network(struct in_addr addr, int prefix) - \brief calculates the network address for a specified address and prefix. - - \param addr an IP address, in network byte order - \param prefix the network prefix - \return the base address of the network that addr is associated with, in - network byte order. -*/ -struct in_addr calc_network(struct in_addr addr, int prefix) -{ - struct in_addr mask; - struct in_addr network; - - mask.s_addr = prefix2mask(prefix); - - memset(&network, 0, sizeof(network)); - network.s_addr = addr.s_addr & mask.s_addr; - return network; -} - -/*! - \fn const char *get_hostname(int family, void *addr) - \brief returns the hostname associated with the specified IP address - - \param family the address family, either AF_INET or AF_INET6. - \param addr an IP address to find a hostname for, in network byte order, - should either be a pointer to a struct in_addr or a struct in6_addr. - - \return a hostname, or NULL if one cannot be determined. Hostname is stored - in an allocated buffer. -*/ -static char *get_hostname(int family, void *addr) -{ - static char hostname[NI_MAXHOST]; - int ret = -1; - struct sockaddr_in addr4; - struct sockaddr_in6 addr6; - - if (family == AF_INET) { - memset(&addr4, 0, sizeof(addr4)); - addr4.sin_family = AF_INET; - memcpy(&addr4.sin_addr, addr, sizeof(struct in_addr)); - ret = getnameinfo((struct sockaddr*)&addr4, sizeof(addr4), hostname, sizeof(hostname), NULL, 0, 0); - } else if (family == AF_INET6) { - memset(&addr6, 0, sizeof(addr6)); - addr6.sin6_family = AF_INET6; - memcpy(&addr6.sin6_addr, addr, sizeof(struct in6_addr)); - ret = getnameinfo((struct sockaddr*)&addr6, sizeof(addr6), hostname, sizeof(hostname), NULL, 0, 0); - } - - if (ret != 0) - return NULL; - - return safe_strdup(hostname); -} - -/*! - \fn const char *get_ip_address(int family, void *addr) - \brief returns the IP address associated with the specified hostname - - \param family the requested address family or AF_UNSPEC for any - \param host a hostname - - \return an IP address, or NULL if one cannot be determined. The IP is stored - in an allocated buffer. -*/ -static char *get_ip_address(int family, const char *host) -{ - struct addrinfo *res, *rp; - struct addrinfo hints; - int err; - static char ipname[64]; - void *addr; - - memset(&hints, 0, sizeof(hints)); - hints.ai_family = family; - - err = getaddrinfo(host, NULL, &hints, &res); - if (err != 0) - return NULL; - - for (rp=res;rp!=NULL;rp=rp->ai_next) { - if (rp->ai_family == AF_INET) - addr = (&((struct sockaddr_in *)(rp->ai_addr))->sin_addr); - else - addr = (&((struct sockaddr_in6 *)(rp->ai_addr))->sin6_addr); - - if (inet_ntop(rp->ai_family, addr, ipname, sizeof(ipname)) != NULL) { - freeaddrinfo(res); - return safe_strdup(ipname); - } - } - - freeaddrinfo(res); - return NULL; -} - -static int bit_count(uint32_t i) -{ - int c = 0; - - while (i > 0) { - if (i & 1) - c++; - i >>= 1; - } - - return c; -} - -/*! - \fn int mask2prefix(struct in_addr mask) - \brief calculates the number of bits masked off by a netmask. - - This function calculates the significant bits in an IP address as specified by - a netmask. See also \ref prefix2mask. - - \param mask is the netmask, specified as an struct in_addr in network byte order. - \return the number of significant bits. */ -static int mask2prefix(struct in_addr mask) -{ - int c = 0; - unsigned int seen_one = 0; - uint32_t i = ntohl(mask.s_addr); - - while (i > 0) { - if (i & 1) { - seen_one = 1; - c++; - } else { - if (seen_one) { - return -1; - } - } - i >>= 1; - } - - return c; -} - -static -int ipv4_mask_to_int(const char *prefix) -{ - int ret; - struct in_addr in; - - ret = inet_pton(AF_INET, prefix, &in); - if (ret == 0) - return -1; - - return mask2prefix(in); -} - -/* Returns powers of two in textual format */ -static const char *p2_table(unsigned pow) -{ - static const char *pow2[] = { - "1", - "2", - "4", - "8", - "16", - "32", - "64", - "128", - "256", - "512", - "1024", - "2048", - "4096", - "8192", - "16384", - "32768", - "65536", - "131072", - "262144", - "524288", - "1048576", - "2097152", - "4194304", - "8388608", - "16777216", - "33554432", - "67108864", - "134217728", - "268435456", - "536870912", - "1073741824", - "2147483648", - "4294967296", - "8589934592", - "17179869184", - "34359738368", - "68719476736", - "137438953472", - "274877906944", - "549755813888", - "1099511627776", - "2199023255552", - "4398046511104", - "8796093022208", - "17592186044416", - "35184372088832", - "70368744177664", - "140737488355328", - "281474976710656", - "562949953421312", - "1125899906842624", - "2251799813685248", - "4503599627370496", - "9007199254740992", - "18014398509481984", - "36028797018963968", - "72057594037927936", - "144115188075855872", - "288230376151711744", - "576460752303423488", - "1152921504606846976", - "2305843009213693952", - "4611686018427387904", - "9223372036854775808", - "18446744073709551616", - "36893488147419103232", - "73786976294838206464", - "147573952589676412928", - "295147905179352825856", - "590295810358705651712", - "1180591620717411303424", - "2361183241434822606848", - "4722366482869645213696", - "9444732965739290427392", - "18889465931478580854784", - "37778931862957161709568", - "75557863725914323419136", - "151115727451828646838272", - "302231454903657293676544", - "604462909807314587353088", - "1208925819614629174706176", - "2417851639229258349412352", - "4835703278458516698824704", - "9671406556917033397649408", - "19342813113834066795298816", - "38685626227668133590597632", - "77371252455336267181195264", - "154742504910672534362390528", - "309485009821345068724781056", - "618970019642690137449562112", - "1237940039285380274899124224", - "2475880078570760549798248448", - "4951760157141521099596496896", - "9903520314283042199192993792", - "19807040628566084398385987584", - "39614081257132168796771975168", - "79228162514264337593543950336", - "158456325028528675187087900672", - "316912650057057350374175801344", - "633825300114114700748351602688", - "1267650600228229401496703205376", - "2535301200456458802993406410752", - "5070602400912917605986812821504", - "10141204801825835211973625643008", - "20282409603651670423947251286016", - "40564819207303340847894502572032", - "81129638414606681695789005144064", - "162259276829213363391578010288128", - "324518553658426726783156020576256", - "649037107316853453566312041152512", - "1298074214633706907132624082305024", - "2596148429267413814265248164610048", - "5192296858534827628530496329220096", - "10384593717069655257060992658440192", - "20769187434139310514121985316880384", - "41538374868278621028243970633760768", - "83076749736557242056487941267521536", - "166153499473114484112975882535043072", - "332306998946228968225951765070086144", - "664613997892457936451903530140172288", - "1329227995784915872903807060280344576", - "2658455991569831745807614120560689152", - "5316911983139663491615228241121378304", - "10633823966279326983230456482242756608", - "21267647932558653966460912964485513216", - "42535295865117307932921825928971026432", - "85070591730234615865843651857942052864", - "170141183460469231731687303715884105728", - }; - if (pow <= 127) - return pow2[pow]; - return ""; -} - -static const char *ipv4_net_to_type(struct in_addr net) -{ - unsigned byte1 = (ntohl(net.s_addr) >> 24) & 0xff; - unsigned byte2 = (ntohl(net.s_addr) >> 16) & 0xff; - unsigned byte3 = (ntohl(net.s_addr) >> 8) & 0xff; - unsigned byte4 = (ntohl(net.s_addr)) & 0xff; - - /* based on IANA's iana-ipv4-special-registry and ipv4-address-space - * Updated: 2020-04-06 - */ - if (byte1 == 0) { - return "This host on this network"; - } - - if (byte1 == 10) { - return "Private Use"; - } - - if (byte1 == 100 && (byte2 & 0xc0) == 64) { - return "Shared Address Space"; - } - - if (byte1 == 127) { - return "Loopback"; - } - - if (byte1 == 169 && byte2 == 254) { - return "Link Local"; - } - - if (byte1 == 172 && (byte2 & 0xf0) == 16) { - return "Private Use"; - } - - if (byte1 == 192 && byte2 == 0 && byte3 == 0 && byte4 <= 7) { - return "IPv4 Service Continuity Prefix"; - } - - if (byte1 == 192 && byte2 == 0 && byte3 == 0 && byte4 == 8) { - return "IPv4 dummy address"; - } - - if (byte1 == 192 && byte2 == 0 && byte3 == 0 && byte4 == 9) { - return "Port Control Protocol Anycast"; - } - - if (byte1 == 192 && byte2 == 0 && byte3 == 0 && byte4 == 10) { - return "Traversal Using Relays around NAT Anycast"; - } - - if (byte1 == 192 && byte2 == 0 && byte3 == 0 && (byte4 == 170 || byte4 == 171)) { - return "NAT64/DNS64 Discovery"; - } - - if (byte1 == 192 && byte2 == 0 && byte3 == 0) { - return "IETF Protocol Assignments"; - } - - if (byte1 == 192 && byte2 == 0 && byte3 == 2) { - return "Documentation (TEST-NET-1)"; - } - - if (byte1 == 198 && byte2 == 51 && byte3 == 100) { - return "Documentation (TEST-NET-2)"; - } - - if (byte1 == 203 && byte2 == 0 && byte3 == 113) { - return "Documentation (TEST-NET-3)"; - } - - if (byte1 == 192 && byte2 == 88 && byte3 == 99) { - return "6 to 4 Relay Anycast (Deprecated)"; - } - - if (byte1 == 192 && byte2 == 31 && byte3 == 196) { - return "AS112-v4"; - } - - if (byte1 == 192 && byte2 == 52 && byte3 == 193) { - return "AMT"; - } - - if (byte1 == 192 && byte2 == 168) { - return "Private Use"; - } - - if (byte1 == 192 && byte2 == 175 && byte3 == 48) { - return "Direct Delegation AS112 Service"; - } - - if (byte1 == 255 && byte2 == 255 && byte3 == 255 && byte4 == 255) { - return "Limited Broadcast"; - } - - if (byte1 == 198 && (byte2 & 0xfe) == 18) { - return "Benchmarking"; - } - - if (byte1 >= 224 && byte1 <= 239) { - return "Multicast"; - } - - if ((byte1 & 0xf0) == 240) { - return "Reserved"; - } - - return "Internet"; -} - -static -const char *ipv4_net_to_class(struct in_addr net) -{ - unsigned byte1 = (ntohl(net.s_addr) >> 24) & 0xff; - - if (byte1 < 128) { - return "Class A"; - } - - if (byte1 >= 128 && byte1 < 192) { - return "Class B"; - } - - if (byte1 >= 192 && byte1 < 224) { - return "Class C"; - } - - if (byte1 >= 224 && byte1 < 239) { - return "Class D"; - } - - return "Class E"; -} - -static -unsigned default_ipv4_prefix(struct in_addr net) -{ - unsigned byte1 = (ntohl(net.s_addr) >> 24) & 0xff; - - if (byte1 < 128) { - return 8; - } - - if (byte1 >= 128 && byte1 < 192) { - return 16; - } - - if (byte1 >= 192 && byte1 < 224) { - return 24; - } - - return 24; -} - -char *ipv4_prefix_to_hosts(char *hosts, unsigned hosts_size, unsigned prefix) -{ - if (prefix >= 31) { - snprintf(hosts, hosts_size, "%s", p2_table(32 - prefix)); - } else { - unsigned tmp = (1 << (32 - prefix)) - 2; - snprintf(hosts, hosts_size, "%u", tmp); - } - return hosts; -} - -char *ipv6_prefix_to_hosts(char *hosts, unsigned hosts_size, unsigned prefix) -{ - snprintf(hosts, hosts_size, "%s", p2_table(128 - prefix)); - return hosts; -} - - -static -int get_ipv4_info(const char *ipStr, int prefix, ip_info_st * info, - unsigned flags) -{ - struct in_addr ip, netmask, network, broadcast, minhost, maxhost; - char namebuf[INET_ADDRSTRLEN + 1]; - char errBuf[250]; - - memset(info, 0, sizeof(*info)); - - if (inet_pton(AF_INET, ipStr, &ip) <= 0) { - if (!beSilent) - fprintf(stderr, "ipcalc: bad IPv4 address: %s\n", - ipStr); - return -1; - } - - /* Handle CIDR entries such as 172/8 */ - if (prefix >= 0) { - char *tmp = (char *)ipStr; - int i; - - for (i = 3; i > 0; i--) { - tmp = strchr(tmp, '.'); - if (!tmp) - break; - else - tmp++; - } - - tmp = NULL; - for (; i > 0; i--) { - if (asprintf(&tmp, "%s.0", ipStr) == -1) { - fprintf(stderr, - "Memory allocation failure line %d\n", - __LINE__); - abort(); - } - ipStr = tmp; - } - } else { /* assume good old days classful Internet */ - if (flags & FLAG_ASSUME_CLASS_PREFIX) - prefix = default_ipv4_prefix(ip); - else - prefix = 32; - } - - if (prefix > 32) { - if (!beSilent) - fprintf(stderr, "ipcalc: bad IPv4 prefix %d\n", prefix); - return -1; - } - - if (inet_ntop(AF_INET, &ip, namebuf, sizeof(namebuf)) == 0) { - if (!beSilent) - fprintf(stderr, - "ipcalc: error calculating the IPv4 network\n"); - return -1; - } - info->ip = safe_strdup(namebuf); - - netmask.s_addr = prefix2mask(prefix); - memset(namebuf, '\0', sizeof(namebuf)); - - if (inet_ntop(AF_INET, &netmask, namebuf, INET_ADDRSTRLEN) == NULL) { - fprintf(stderr, "inet_ntop failure at line %d\n", - __LINE__); - exit(1); - } - info->netmask = safe_strdup(namebuf); - info->prefix = prefix; - - broadcast = calc_broadcast(ip, prefix); - - memset(namebuf, '\0', sizeof(namebuf)); - if (inet_ntop(AF_INET, &broadcast, namebuf, INET_ADDRSTRLEN) == NULL) { - fprintf(stderr, "inet_ntop failure at line %d\n", - __LINE__); - exit(1); - } - info->broadcast = safe_strdup(namebuf); - - network = calc_network(ip, prefix); - - info->reverse_dns = calc_reverse_dns4(network, prefix, network, broadcast); - - memset(namebuf, '\0', sizeof(namebuf)); - if (inet_ntop(AF_INET, &network, namebuf, INET_ADDRSTRLEN) == NULL) { - fprintf(stderr, "inet_ntop failure at line %d\n", - __LINE__); - exit(1); - } - - info->network = safe_strdup(namebuf); - - info->type = ipv4_net_to_type(network); - info->class = ipv4_net_to_class(network); - - if (prefix < 32) { - memcpy(&minhost, &network, sizeof(minhost)); - - if (prefix <= 30) - minhost.s_addr = htonl(ntohl(minhost.s_addr) | 1); - if (inet_ntop(AF_INET, &minhost, namebuf, INET_ADDRSTRLEN) == - NULL) { - fprintf(stderr, "inet_ntop failure at line %d\n", - __LINE__); - exit(1); - } - info->hostmin = safe_strdup(namebuf); - - memcpy(&maxhost, &network, sizeof(minhost)); - maxhost.s_addr |= ~netmask.s_addr; - if (prefix <= 30) { - maxhost.s_addr = htonl(ntohl(maxhost.s_addr) - 1); - } - if (inet_ntop(AF_INET, &maxhost, namebuf, sizeof(namebuf)) == 0) { - if (!beSilent) - fprintf(stderr, - "ipcalc: error calculating the IPv4 network\n"); - return -1; - } - - info->hostmax = safe_strdup(namebuf); - } else { - info->hostmin = info->network; - info->hostmax = info->network; - } - - ipv4_prefix_to_hosts(info->hosts, sizeof(info->hosts), prefix); - -#if defined(USE_GEOIP) || defined(USE_MAXMIND) - if (flags & FLAG_GET_GEOIP) { - geo_ip_lookup(ipStr, &info->geoip_country, &info->geoip_ccode, &info->geoip_city, &info->geoip_coord); - } -#endif - - if (flags & FLAG_RESOLVE_HOST) { - info->hostname = get_hostname(AF_INET, &ip); - if (info->hostname == NULL) { - if (!beSilent) { - sprintf(errBuf, - "ipcalc: cannot find hostname for %s", - ipStr); - herror(errBuf); - } - return -1; - } - } - return 0; -} - -int ipv6_prefix_to_mask(unsigned prefix, struct in6_addr *mask) -{ - struct in6_addr in6; - int i, j; - - if (prefix > 128) - return -1; - - memset(&in6, 0x0, sizeof(in6)); - for (i = prefix, j = 0; i > 0; i -= 8, j++) { - if (i >= 8) { - in6.s6_addr[j] = 0xff; - } else { - in6.s6_addr[j] = (unsigned long)(0xffU << (8 - i)); - } - } - - memcpy(mask, &in6, sizeof(*mask)); - return 0; -} - -static char *ipv6_mask_to_str(const struct in6_addr *mask) -{ - char buf[128]; - - if (inet_ntop(AF_INET6, mask, buf, sizeof(buf)) == NULL) - return NULL; - - return safe_strdup(buf); -} - -static const char *ipv6_net_to_type(struct in6_addr *net, int prefix) -{ - uint16_t word1 = net->s6_addr[0] << 8 | net->s6_addr[1]; - uint16_t word2 = net->s6_addr[2] << 8 | net->s6_addr[3]; - - /* based on IANA's iana-ipv6-special-registry and ipv6-address-space - * Updated: 2019-09-13 - */ - if (prefix == 128 && memcmp - (net->s6_addr, - "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01", - 16) == 0) - return "Loopback Address"; - - if (prefix == 128 && memcmp - (net->s6_addr, - "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", - 16) == 0) - return "Unspecified Address"; - - if (prefix >= 96 && memcmp - (net->s6_addr, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff", - 12) == 0) - return "IPv4-mapped Address"; - - if (prefix >= 96 && memcmp - (net->s6_addr, "\x00\x64\xff\x9b\x00\x00\x00\x00\x00\x00\x00\x00", - 12) == 0) - return "IPv4-IPv6 Translat."; - - if (prefix >= 48 && memcmp - (net->s6_addr, "\x00\x64\xff\x9b\x00\x01", - 6) == 0) - return "IPv4-IPv6 Translat."; - - if (prefix >= 96 && memcmp - (net->s6_addr, "\x10\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", - 12) == 0) - return "Discard-Only Address Block"; - - if (prefix >= 32 && word1 == 0x2001 && word2 == 0) - return "TEREDO"; - - if (prefix == 128 && memcmp - (net->s6_addr, "\x20\x01\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01", - 16) == 0) - return "Port Control Protocol Anycast"; - - if (prefix == 128 && memcmp - (net->s6_addr, "\x20\x01\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02", - 16) == 0) - return "Traversal Using Relays around NAT Anycast"; - - if (prefix >= 48 && memcmp - (net->s6_addr, "\x20\x01\x00\x02\x00\x00", - 6) == 0) - return "Benchmarking"; - - if (prefix >= 32 && word1 == 0x2001 && word2 == 0x3) - return "AMT"; - - if (prefix >= 48 && memcmp - (net->s6_addr, "\x20\x01\x00\x04\x01\x12", - 6) == 0) - return "AS112-v6"; - - if (prefix >= 28 && word1 == 0x2001 && (word2 & 0xfff0) == 0x10) - return "Deprecated (previously ORCHID)"; - - if (prefix >= 28 && word1 == 0x2001 && (word2 & 0xfff0) == 0x20) - return "ORCHIDv2"; - - if (prefix >= 23 && word1 == 0x2001 && (word2 & 0xff00) <= 0x100) - return "IETF Protocol Assignments"; - - if (prefix >= 32 && word1 == 0x2001 && word2 == 0xdb8) - return "Documentation"; - - if (word1 == 0x2002) - return "6to4"; - - if (prefix >= 48 && memcmp - (net->s6_addr, "\x26\x20\x00\x4f\x80\x00", - 6) == 0) - return "Direct Delegation AS112 Service"; - - if ((word1 & 0xe000) == 0x2000) { - return "Global Unicast"; - } - - if (((net->s6_addr[0] & 0xfe) == 0xfc)) { - return "Unique Local Unicast"; - } - - if ((word1 & 0xffc0) == 0xfe80) { - return "Link-Scoped Unicast"; - } - - if ((net->s6_addr[0] & 0xff) == 0xff) { - return "Multicast"; - } - - return "Reserved"; -} - -static -char *expand_ipv6(struct in6_addr *ip6) -{ - char buf[128]; - char *p; - unsigned i; - - p = buf; - for (i = 0; i < 16; i++) { - sprintf(p, "%.2x", (unsigned)ip6->s6_addr[i]); - p += 2; - if (i % 2 != 0 && i != 15) { - *p = ':'; - p++; - } - } - *p = 0; - - return safe_strdup(buf); -} - -static -int get_ipv6_info(const char *ipStr, int prefix, ip_info_st * info, - unsigned flags) -{ - struct in6_addr ip6, mask, network; - char errBuf[250]; - unsigned i; - - memset(info, 0, sizeof(*info)); - - if (inet_pton(AF_INET6, ipStr, &ip6) <= 0) { - if (!beSilent) - fprintf(stderr, "ipcalc: bad IPv6 address: %s\n", - ipStr); - return -1; - } - - /* expand */ - info->expanded_ip = expand_ipv6(&ip6); - - if (inet_ntop(AF_INET6, &ip6, errBuf, sizeof(errBuf)) == 0) { - if (!beSilent) - fprintf(stderr, - "ipcalc: error calculating the IPv6 network\n"); - return -1; - } - - info->ip = safe_strdup(errBuf); - - if (prefix > 128) { - if (!beSilent) - fprintf(stderr, "ipcalc: bad IPv6 prefix: %d\n", - prefix); - return -1; - } else if (prefix < 0) { - prefix = 128; - } - - info->prefix = prefix; - - if (ipv6_prefix_to_mask(prefix, &mask) == -1) { - if (!beSilent) - fprintf(stderr, - "ipcalc: error converting IPv6 prefix: %d\n", - prefix); - return -1; - } - - info->netmask = ipv6_mask_to_str(&mask); - - for (i = 0; i < sizeof(struct in6_addr); i++) - network.s6_addr[i] = ip6.s6_addr[i] & mask.s6_addr[i]; - - if (inet_ntop(AF_INET6, &network, errBuf, sizeof(errBuf)) == 0) { - if (!beSilent) - fprintf(stderr, - "ipcalc: error calculating the IPv6 network\n"); - return -1; - } - - info->network = safe_strdup(errBuf); - - info->expanded_network = expand_ipv6(&network); - info->type = ipv6_net_to_type(&network, prefix); - - info->reverse_dns = calc_reverse_dns6(&network, prefix); - - if (prefix < 128) { - info->hostmin = safe_strdup(errBuf); - - for (i = 0; i < sizeof(struct in6_addr); i++) - network.s6_addr[i] |= ~mask.s6_addr[i]; - if (inet_ntop(AF_INET6, &network, errBuf, sizeof(errBuf)) == 0) { - if (!beSilent) - fprintf(stderr, - "ipcalc: error calculating the IPv6 network\n"); - return -1; - } - - info->hostmax = safe_strdup(errBuf); - } else { - info->hostmin = info->network; - info->hostmax = info->network; - } - - ipv6_prefix_to_hosts(info->hosts, sizeof(info->hosts), prefix); - -#if defined(USE_GEOIP) || defined(USE_MAXMIND) - if (flags & FLAG_GET_GEOIP) { - geo_ip_lookup(ipStr, &info->geoip_country, &info->geoip_ccode, &info->geoip_city, &info->geoip_coord); - } -#endif - - if (flags & FLAG_RESOLVE_HOST) { - info->hostname = get_hostname(AF_INET6, &ip6); - if (info->hostname == NULL) { - if (!beSilent) { - sprintf(errBuf, - "ipcalc: cannot find hostname for %s", - ipStr); - herror(errBuf); - } - return -1; - } - } - return 0; -} - -static int randomize(void *ptr, unsigned size) -{ - int fd, ret; - - fd = open("/dev/urandom", O_RDONLY); - if (fd < 0) - return -1; - - ret = read(fd, ptr, size); - close(fd); - - if (ret != size) { - return -1; - } - - return 0; -} - -static char *generate_ip_network(unsigned prefix, unsigned flags) -{ - struct timespec ts; - char ipbuf[64]; - char *p = NULL; - - if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts) < 0) - return NULL; - - if (flags & FLAG_IPV6) { - struct in6_addr net; - - net.s6_addr[0] = 0xfd; - if (randomize(&net.s6_addr[1], 15) < 0) - return NULL; - - if (inet_ntop(AF_INET6, &net, ipbuf, sizeof(ipbuf)) == NULL) - return NULL; - } else { - struct in_addr net; - unsigned c = ts.tv_nsec % 4; - uint8_t bytes[4]; - - if (randomize(bytes, 4) < 0) - return NULL; - - if (prefix >= 16 && c < 2) { - if (c == 1) { - bytes[0] = 192; - bytes[1] = 168; - } else { - bytes[0] = 172; - bytes[1] = 16 | ((ts.tv_nsec >> 4) & 0x0f); - } - } else { - bytes[0] = 10; - } - - memcpy(&net.s_addr, bytes, 4); - - if (inet_ntop(AF_INET, &net, ipbuf, sizeof(ipbuf)) == NULL) - return NULL; - } - - if (asprintf(&p, "%s/%u", ipbuf, prefix) == -1) - return NULL; - - return p; -} - -static -int str_to_prefix(unsigned *flags, const char *prefixStr, unsigned fix) -{ - int prefix = -1; - if (!((*flags) & FLAG_IPV6) && strchr(prefixStr, '.')) { /* prefix is 255.x.x.x */ - prefix = ipv4_mask_to_int(prefixStr); - } else { - int r = safe_atoi(prefixStr, &prefix); - if (r != 0) { - return -1; - } - } - - if (fix && (prefix > 32 && !((*flags) & FLAG_IPV6))) - *flags |= FLAG_IPV6; - - if (prefix < 0 || ((((*flags) & FLAG_IPV6) && prefix > 128) || (!((*flags) & FLAG_IPV6) && prefix > 32))) { - return -1; - } - return prefix; -} - -#define OPT_ALLINFO 1 -#define OPT_MINADDR 2 -#define OPT_MAXADDR 3 -#define OPT_ADDRESSES 4 -#define OPT_ADDRSPACE 5 -#define OPT_USAGE 6 -#define OPT_REVERSE 7 -#define OPT_CLASS_PREFIX 8 -#define OPT_NO_DECORATE 9 - -static const struct option long_options[] = { - {"check", 0, 0, 'c'}, - {"random-private", 1, 0, 'r'}, - {"split", 1, 0, 'S'}, - {"deaggregate", 1, 0, 'd'}, - {"info", 0, 0, 'i'}, - {"all-info", 0, 0, OPT_ALLINFO}, - {"ipv4", 0, 0, '4'}, - {"ipv6", 0, 0, '6'}, - {"address", 0, 0, 'a'}, - {"broadcast", 0, 0, 'b'}, - {"hostname", 0, 0, 'h'}, - {"lookup-host", 1, 0, 'o'}, - {"reverse-dns", 0, 0, OPT_REVERSE}, -#if defined(USE_GEOIP) || defined(USE_MAXMIND) - {"geoinfo", 0, 0, 'g'}, -#endif - {"netmask", 0, 0, 'm'}, - {"network", 0, 0, 'n'}, - {"prefix", 0, 0, 'p'}, - {"class-prefix", 0, 0, OPT_CLASS_PREFIX}, - {"minaddr", 0, 0, OPT_MINADDR}, - {"maxaddr", 0, 0, OPT_MAXADDR}, - {"addresses", 0, 0, OPT_ADDRESSES}, - {"addrspace", 0, 0, OPT_ADDRSPACE}, - {"silent", 0, 0, 's'}, - {"no-decorate", 0, 0, OPT_NO_DECORATE}, - {"json", 0, 0, 'j'}, - {"version", 0, 0, 'v'}, - {"help", 0, 0, '?'}, - {"usage", 0, 0, OPT_USAGE}, - {NULL, 0, 0, 0} -}; - -static -void usage(unsigned verbose) -{ - if (verbose) { - fprintf(stderr, "Usage: ipcalc [OPTION...]\n"); - fprintf(stderr, " -c, --check Validate IP address\n"); - fprintf(stderr, " -r, --random-private=PREFIX Generate a random private IP network using\n"); - fprintf(stderr, " the supplied prefix or mask.\n"); - fprintf(stderr, " -S, --split=PREFIX Split the provided network using the\n"); - fprintf(stderr, " provided prefix/netmask\n"); - fprintf(stderr, " -d, --deaggregate=IP1-IP2 Deaggregate the provided address range\n"); - fprintf(stderr, " -i, --info Print information on the provided IP address\n"); - fprintf(stderr, " (default)\n"); - fprintf(stderr, " --all-info Print verbose information on the provided IP\n"); - fprintf(stderr, " address\n"); - fprintf(stderr, "\n"); - fprintf(stderr, "Specific info options:\n"); - fprintf(stderr, " --reverse-dns Print network in a the reverse DNS format\n"); - fprintf(stderr, " -a, --address Display IP address\n"); - fprintf(stderr, " -b, --broadcast Display calculated broadcast address\n"); - fprintf(stderr, " -m, --netmask Display netmask for IP\n"); - fprintf(stderr, " -n, --network Display network address\n"); - fprintf(stderr, " -p, --prefix Display network prefix\n"); - fprintf(stderr, " --minaddr Display the minimum address in the network\n"); - fprintf(stderr, " --maxaddr Display the maximum address in the network\n"); - fprintf(stderr, " --addresses Display the maximum number of addresses in\n"); - fprintf(stderr, " the network\n"); - fprintf(stderr, " --addrspace Display the address space the network\n"); - fprintf(stderr, " resides on\n"); - fprintf(stderr, " -h, --hostname Show hostname determined via DNS\n"); - fprintf(stderr, " -o, --lookup-host=STRING Show IP as determined via DNS\n"); -#if defined(USE_GEOIP) || defined(USE_MAXMIND) - fprintf(stderr, " -g, --geoinfo Show Geographic information about the\n"); - fprintf(stderr, " provided IP\n"); -#endif - fprintf(stderr, "\n"); - fprintf(stderr, "Other options:\n"); - fprintf(stderr, " -4, --ipv4 Explicitly specify the IPv4 address family\n"); - fprintf(stderr, " -6, --ipv6 Explicitly specify the IPv6 address family\n"); - fprintf(stderr, " --class-prefix When specified the default prefix will be determined\n"); - fprintf(stderr, " by the IPv4 address class\n"); - fprintf(stderr, " --no-decorate Print only the requested information\n"); - fprintf(stderr, " -j, --json JSON output\n"); - fprintf(stderr, " -s, --silent Don't ever display error messages\n"); - fprintf(stderr, " -v, --version Display program version\n"); - fprintf(stderr, " -?, --help Show this help message\n"); - fprintf(stderr, " --usage Display brief usage message\n"); - } else { - fprintf(stderr, "Usage: ipcalc [-46sv?] [-c|--check] [-r|--random-private=STRING] [-i|--info]\n"); - fprintf(stderr, " [--all-info] [-4|--ipv4] [-6|--ipv6] [-a|--address] [-b|--broadcast]\n"); - fprintf(stderr, " [-h|--hostname] [-o|--lookup-host=STRING] [-g|--geoinfo]\n"); - fprintf(stderr, " [-m|--netmask] [-n|--network] [-p|--prefix] [--minaddr] [--maxaddr]\n"); - fprintf(stderr, " [--addresses] [--addrspace] [-j|--json] [-s|--silent] [-v|--version]\n"); - fprintf(stderr, " [--reverse-dns] [--class-prefix]\n"); - fprintf(stderr, " [-?|--help] [--usage]\n"); - } -} - -void output_start(unsigned * const jsonfirst) -{ - if (flags & FLAG_JSON) { - printf("{\n"); - } - - *jsonfirst = JSON_FIRST; -} - -void output_separate(unsigned * const jsonfirst) -{ - if (!(flags & FLAG_JSON)) { - printf("\n"); - } -} - -void output_stop(unsigned * const jsonfirst) -{ - if (flags & FLAG_JSON) { - printf("\n}\n"); - } -} - -void array_start(unsigned * const jsonfirst, const char *head, const char *json_head) -{ - if (flags & FLAG_JSON) { - if (*jsonfirst == JSON_NEXT) { - printf(",\n "); - } - - printf(" \"%s\":[\n ", json_head); - } else { - if (!(flags & FLAG_NO_DECORATE)) - printf("[%s]\n", head); - } - - *jsonfirst = JSON_ARRAY_FIRST; -} - -void array_stop(unsigned * const jsonfirst) -{ - if (flags & FLAG_JSON) { - printf("]"); - *jsonfirst = JSON_NEXT; - } -} - -void -__attribute__ ((format(printf, 3, 4))) -color_printf(const char *color, const char *title, const char *fmt, ...) -{ - va_list args; - - va_start(args, fmt); - va_color_printf(color, title, fmt, args); - va_end(args); - - return; -} - -void va_color_printf(const char *color, const char *title, const char *fmt, va_list varglist) -{ - int ret; - char *str = NULL; - - ret = vasprintf(&str, fmt, varglist); - - if (ret < 0) { - return; - } - - fputs(title, stdout); - if (colors) { - fputs(color, stdout); - } - fputs(str, stdout); - fputs("\n", stdout); - if (colors) { - fputs(KRESET, stdout); - } - free(str); - return; -} - -void -__attribute__ ((format(printf, 3, 4))) -json_printf(unsigned * const jsonfirst, const char *jsontitle, const char *fmt, ...) -{ - va_list args; - - va_start(args, fmt); - va_json_printf(jsonfirst, jsontitle, fmt, args); - va_end(args); - - return; -} -void va_json_printf(unsigned * const jsonfirst, const char *jsontitle, const char *fmt, va_list varglist) -{ - int ret; - char *str = NULL; - - ret = vasprintf(&str, fmt, varglist); - if (ret < 0) - return; - - if (*jsonfirst == JSON_ARRAY_NEXT) { - fprintf(stdout, ",\n "); - } else if (*jsonfirst == JSON_NEXT) { - fprintf(stdout, ",\n"); - } - - fprintf(stdout, " "); - if (jsontitle) - fprintf(stdout, "\"%s\":\"%s\"", jsontitle, str); - else - fprintf(stdout, "\"%s\"", str); - if (*jsonfirst == JSON_FIRST) - *jsonfirst = JSON_NEXT; - else if (*jsonfirst == JSON_ARRAY_FIRST) - *jsonfirst = JSON_ARRAY_NEXT; - - free(str); - return; -} - -/* Always prints "title: value", will not color if --no-decorate is given - */ -static void -__attribute__ ((format(printf, 4, 5))) -pretty_printf(unsigned * const jsonfirst, const char *title, const char *jsontitle, const char *fmt, ...) -{ - va_list args; - - va_start(args, fmt); - if (flags & FLAG_JSON) { - va_json_printf(jsonfirst, jsontitle, fmt, args); - } else if (flags & FLAG_NO_DECORATE) { - fputs(title, stdout); - vprintf(fmt, args); - fputs("\n", stdout); - } else { - va_color_printf(KBLUE, title, fmt, args); - } - va_end(args); - - return; -} - -/* Always prints "title: value", will not color if --no-decorate is given - * To be used for distinct values (e.g., a summary). - */ -static void -__attribute__ ((format(printf, 4, 5))) -pretty_dist_printf(unsigned * const jsonfirst, const char *title, const char *jsontitle, const char *fmt, ...) -{ - va_list args; - - va_start(args, fmt); - if (flags & FLAG_JSON) { - va_json_printf(jsonfirst, jsontitle, fmt, args); - } else if (flags & FLAG_NO_DECORATE) { - fputs(title, stdout); - vprintf(fmt, args); - fputs("\n", stdout); - } else { - va_color_printf(KMAG, title, fmt, args); - } - va_end(args); - - return; -} - -/* Prints "title: value", will only print value if --no-decorate is given - */ -void -__attribute__ ((format(printf, 4, 5))) -default_printf(unsigned * const jsonfirst, const char *title, const char *jsontitle, const char *fmt, ...) -{ - va_list args; - - va_start(args, fmt); - if (flags & FLAG_JSON) { - va_json_printf(jsonfirst, jsontitle, fmt, args); - } else if (flags & FLAG_NO_DECORATE) { - vprintf(fmt, args); - printf("\n"); - } else { - va_color_printf(KBLUE, title, fmt, args); - } - va_end(args); - - return; -} - -/* Prints "title: value", will only print value if --no-decorate is given. It - * prints a distinct value (e.g., to be used for a summary). - */ -void -__attribute__ ((format(printf, 4, 5))) -dist_printf(unsigned * const jsonfirst, const char *title, const char *jsontitle, const char *fmt, ...) -{ - va_list args; - - va_start(args, fmt); - if (flags & FLAG_JSON) { - va_json_printf(jsonfirst, jsontitle, fmt, args); - } - else if (flags & FLAG_NO_DECORATE) { - vprintf(fmt, args); - printf("\n"); - } else { - va_color_printf(KMAG, title, fmt, args); - } - va_end(args); - - return; -} - -/* Printed variables names */ -#define HOSTNAME_NAME "HOSTNAME" -#define FULL_ADDRESS_NAME "FULLADDRESS" -#define ADDRESS_NAME "ADDRESS" -#define FULL_NETWORK_NAME "FULLNETWORK" -#define NETWORK_NAME "NETWORK" -#define NETMASK_NAME "NETMASK" -#define PREFIX_NAME "PREFIX" -#define BROADCAST_NAME "BROADCAST" -#define REVERSEDNS_NAME "REVERSEDNS" -#define ADDRSPACE_NAME "ADDRSPACE" -#define ADDRCLASS_NAME "ADDRCLASS" -#define MINADDR_NAME "MINADDR" -#define MAXADDR_NAME "MAXADDR" -#define ADDRESSES_NAME "ADDRESSES" -#define COUNTRYCODE_NAME "COUNTRYCODE" -#define COUNTRY_NAME "COUNTRY" -#define CITY_NAME "CITY" -#define COORDINATES_NAME "COORDINATES" - -/*! - \fn main(int argc, const char **argv) - \brief wrapper program for ipcalc functions. - - This is a wrapper program for the functions that the ipcalc library provides. - It can be used from shell scripts or directly from the command line. - - For more information, please see the ipcalc(1) man page. -*/ -// int main(int argc, char **argv) -// { -// char *randomStr = NULL; -// char *hostname = NULL; -// char *splitStr = NULL; -// char *ipStr = NULL, *prefixStr = NULL, *netmaskStr = NULL, *chptr = NULL; -// int prefix = -1, splitPrefix = -1; -// ip_info_st info; -// int r = 0; -// unsigned jsonchain = JSON_FIRST; -// enum app_t app = 0; - -// while (1) { -// int c = getopt_long(argc, argv, "S:cr:i46abho:gmnpjsvd:", long_options, NULL); -// if (c == -1) -// break; - -// switch(c) { -// case 'c': -// app |= APP_CHECK_ADDRESS; -// break; -// case 'S': -// app |= APP_SPLIT; -// splitStr = safe_strdup(optarg); -// if (splitStr == NULL) exit(1); -// break; -// case 'd': -// app |= APP_DEAGGREGATE; -// ipStr = safe_strdup(optarg); -// if (ipStr == NULL) exit(1); -// break; -// case 'r': -// app |= APP_SHOW_INFO; -// flags |= FLAG_RANDOM; -// randomStr = safe_strdup(optarg); -// if (randomStr == NULL) exit(1); -// break; -// case 'i': -// app |= APP_SHOW_INFO; -// flags |= FLAG_SHOW_MODERN_INFO; -// break; -// case OPT_ALLINFO: -// app |= APP_SHOW_INFO; -// flags |= FLAG_SHOW_ALL_INFO|FLAG_SHOW_MODERN_INFO; -// break; -// case OPT_CLASS_PREFIX: -// flags |= FLAG_ASSUME_CLASS_PREFIX; -// break; -// case OPT_REVERSE: -// app |= APP_SHOW_INFO; -// flags |= FLAG_SHOW_REVERSE; -// break; -// case '4': -// flags |= FLAG_IPV4; -// break; -// case '6': -// flags |= FLAG_IPV6; -// break; -// case 'a': -// app |= APP_SHOW_INFO; -// flags |= FLAG_SHOW_ADDRESS; -// break; -// case 'b': -// app |= APP_SHOW_INFO; -// flags |= FLAG_SHOW_BROADCAST; -// break; -// case 'h': -// app |= APP_SHOW_INFO; -// flags |= FLAG_RESOLVE_HOST; -// break; -// case 'o': -// app |= APP_SHOW_INFO; -// flags |= FLAG_RESOLVE_IP; -// hostname = safe_strdup(optarg); -// if (hostname == NULL) exit(1); -// break; -// case 'g': -// app |= APP_SHOW_INFO; -// flags |= FLAG_SHOW_GEOIP; -// break; -// case 'm': -// app |= APP_SHOW_INFO; -// flags |= FLAG_SHOW_NETMASK; -// break; -// case 'n': -// app |= APP_SHOW_INFO; -// flags |= FLAG_SHOW_NETWORK; -// break; -// case 'p': -// app |= APP_SHOW_INFO; -// flags |= FLAG_SHOW_PREFIX; -// break; -// case OPT_MINADDR: -// app |= APP_SHOW_INFO; -// flags |= FLAG_SHOW_MINADDR; -// break; -// case OPT_MAXADDR: -// app |= APP_SHOW_INFO; -// flags |= FLAG_SHOW_MAXADDR; -// break; -// case OPT_ADDRESSES: -// app |= APP_SHOW_INFO; -// flags |= FLAG_SHOW_ADDRESSES; -// break; -// case OPT_ADDRSPACE: -// app |= APP_SHOW_INFO; -// flags |= FLAG_SHOW_ADDRSPACE; -// break; -// case OPT_NO_DECORATE: -// flags |= FLAG_NO_DECORATE; -// break; -// case 'j': -// flags |= FLAG_JSON; -// break; -// case 's': -// beSilent = 1; -// break; -// case 'v': -// app |= APP_VERSION; -// break; -// case OPT_USAGE: -// usage(0); -// exit(0); -// case '?': -// usage(1); -// exit(0); -// } -// } - -// if (optind < argc) { -// if (ipStr != NULL) { -// if (!beSilent) -// fprintf(stderr, -// "ipcalc: superfluous option given\n"); -// exit(1); -// } - -// ipStr = argv[optind++]; -// if (optind < argc) -// chptr = argv[optind++]; -// } - -// if ((flags & FLAG_JSON) && (flags & FLAG_NO_DECORATE)) { -// flags &= ~FLAG_NO_DECORATE; -// } - -// if (!(flags & FLAGS_TO_IGNORE_MASK)) -// flags |= FLAG_SHOW_MODERN_INFO; - -// /* Only the modern info flag results to JSON output */ -// if ((flags & ENV_INFO_MASK) && (flags & FLAG_JSON)) -// flags |= FLAG_SHOW_MODERN_INFO; - -// if (geo_setup() == 0 && (flags & FLAG_SHOW_ALL_INFO)) -// flags |= FLAG_GET_GEOIP; - -// if (bit_count(app) > 1) { -// if (!beSilent) -// fprintf(stderr, -// "ipcalc: you cannot mix these options\n"); -// return 1; -// } - -// if ((flags & FLAG_IPV6) && (flags & FLAG_IPV4)) { -// if (!beSilent) -// fprintf(stderr, -// "ipcalc: you cannot specify both IPv4 and IPv6\n"); -// return 1; -// } - -// /* if there is a : in the address, it is an IPv6 address. -// * Note that we allow -4, and -6 to be given explicitly, so -// * that the tool can be used to check for a valid IPv4 or IPv6 -// * address. -// */ -// if ((flags & FLAG_IPV4) == 0 && ipStr && strchr(ipStr, ':') != NULL) { -// flags |= FLAG_IPV6; -// } - -// switch (app) { -// case APP_VERSION: -// printf("ipcalc %s\n", VERSION); -// return 0; -// case APP_DEAGGREGATE: -// deaggregate(ipStr, flags); -// return 0; -// case APP_SPLIT: -// case APP_CHECK_ADDRESS: -// case APP_SHOW_INFO: -// /* These are handled lower into the info app */ -// break; -// } - -// /* The main application which displays information about an address or -// * a network. */ -// if (flags & FLAG_RANDOM) { -// if (ipStr) { -// if (!beSilent) -// fprintf(stderr, -// "ipcalc: provided superfluous parameter '%s'\n", ipStr); -// return 1; -// } - -// prefix = str_to_prefix(&flags, randomStr, 1); -// if (prefix < 0) { -// if (!beSilent) -// fprintf(stderr, -// "ipcalc: bad %s prefix: %s\n", (flags&FLAG_IPV6)?"IPv6":"IPv4", randomStr); -// return 1; -// } - -// ipStr = generate_ip_network(prefix, flags); -// if (ipStr == NULL) { -// if (!beSilent) -// fprintf(stderr, -// "ipcalc: cannot generate network with prefix: %u\n", -// prefix); -// return 1; -// } -// } - -// if (hostname == NULL && ipStr == NULL) { -// if (!beSilent) { -// fprintf(stderr, -// "ipcalc: ip address expected\n"); -// usage(1); -// } -// return 1; -// } - -// /* resolve IP address if a hostname was given */ -// if (hostname) { -// int family = AF_UNSPEC; -// if (flags & FLAG_IPV6) -// family = AF_INET6; -// else if (flags & FLAG_IPV4) -// family = AF_INET; - -// ipStr = get_ip_address(family, hostname); -// if (ipStr == NULL) { -// if (!beSilent) -// fprintf(stderr, -// "ipcalc: could not resolve %s\n", hostname); -// return 1; -// } - -// if ((flags & FLAG_IPV4) == 0 && strchr(ipStr, ':') != NULL) { -// flags |= FLAG_IPV6; -// } -// } - -// if (chptr) { -// if ((flags & FLAG_IPV6) == 0) { -// prefixStr = chptr; -// } else { -// if (!beSilent) { -// fprintf(stderr, "ipcalc: unexpected argument: %s\n", -// chptr); -// usage(1); -// } -// return 1; -// } -// } - -// if (prefixStr == NULL && strchr(ipStr, '/') != NULL) { -// prefixStr = strchr(ipStr, '/'); -// *prefixStr = '\0'; /* fix up ipStr */ -// prefixStr++; -// } - -// if (prefixStr != NULL) { -// prefix = str_to_prefix(&flags, prefixStr, 0); -// if (prefix < 0) { -// if (!beSilent) -// fprintf(stderr, -// "ipcalc: bad %s prefix: %s\n", (flags & FLAG_IPV6)?"IPv6":"IPv4", prefixStr); -// return 1; -// } -// } - -// if (flags & FLAG_IPV6) { -// r = get_ipv6_info(ipStr, prefix, &info, flags); -// } else { -// if ((flags & FLAG_SHOW_BROADCAST) || (flags & FLAG_SHOW_NETWORK) || (flags & FLAG_SHOW_PREFIX)) { -// if (netmaskStr && prefix >= 0) { -// if (!beSilent) { -// fprintf(stderr, -// "ipcalc: both netmask and prefix specified\n"); -// usage(1); -// } -// return 1; -// } -// } - -// if (prefix == -1 && netmaskStr) { -// prefix = ipv4_mask_to_int(netmaskStr); -// if (prefix < 0) { -// if (!beSilent) -// fprintf(stderr, -// "ipcalc: bad IPv4 prefix: %s\n", prefixStr); -// return 1; -// } -// } -// r = get_ipv4_info(ipStr, prefix, &info, flags); -// } - -// if (r < 0) { -// return 1; -// } - -// switch (app) { -// case APP_SPLIT: -// splitPrefix = str_to_prefix(&flags, splitStr, 1); -// if (splitPrefix < 0) { -// if (!beSilent) -// fprintf(stderr, -// "ipcalc: bad %s prefix: %s\n", (flags & FLAG_IPV6)?"IPv6":"IPv4", splitStr); -// return 1; -// } - -// if (flags & FLAG_IPV6) { -// show_split_networks_v6(splitPrefix, &info, flags); -// } else { -// show_split_networks_v4(splitPrefix, &info, flags); -// } -// return 0; -// case APP_CHECK_ADDRESS: -// return 0; -// default: -// break; -// } - -// if (isatty(STDOUT_FILENO) != 0) -// colors = 1; - -// /* we know what we want to display now, so display it. */ -// if (flags & FLAG_SHOW_MODERN_INFO) { -// unsigned single_host = 0; - -// if (((flags & FLAG_IPV6) && info.prefix == 128) || -// (!(flags & FLAG_IPV6) && info.prefix == 32)) { -// single_host = 1; -// } - -// output_start(&jsonchain); - -// if ((!randomStr || single_host) && -// (single_host || strcmp(info.network, info.ip) != 0)) { -// if (info.expanded_ip) { -// pretty_printf(&jsonchain,"Full Address:\t", FULL_ADDRESS_NAME, "%s", info.expanded_ip); -// } -// pretty_printf(&jsonchain, "Address:\t", ADDRESS_NAME, "%s", info.ip); -// } - -// if (single_host && info.hostname) -// pretty_printf(&jsonchain, "Hostname:\t", HOSTNAME_NAME, "%s", info.hostname); - -// if (!single_host || (flags & FLAG_JSON)) { -// if (! (flags & FLAG_JSON)) { -// if (info.expanded_network) { -// pretty_printf(&jsonchain, "Full Network:\t", FULL_NETWORK_NAME, "%s/%u", info.expanded_network, info.prefix); -// } -// pretty_printf(&jsonchain, "Network:\t", NETWORK_NAME, "%s/%u", info.network, info.prefix); -// pretty_printf(&jsonchain, "Netmask:\t", NETMASK_NAME, "%s = %u", info.netmask, info.prefix); -// } -// else { -// if (info.expanded_network) { -// pretty_printf(&jsonchain, "Full Network:\t", FULL_NETWORK_NAME, "%s", info.expanded_network); -// } -// pretty_printf(&jsonchain, "Network:\t", NETWORK_NAME, "%s", info.network); -// pretty_printf(&jsonchain, "Netmask:\t", NETMASK_NAME, "%s", info.netmask); -// pretty_printf(&jsonchain, "Prefix:\t", PREFIX_NAME, "%u", info.prefix); -// } - - -// if (info.broadcast) -// pretty_printf(&jsonchain, "Broadcast:\t", BROADCAST_NAME, "%s", info.broadcast); -// } - -// if ((flags & FLAG_SHOW_ALL_INFO) && info.reverse_dns) -// pretty_printf(&jsonchain, "Reverse DNS:\t", REVERSEDNS_NAME, "%s", info.reverse_dns); - -// if (!single_host || (flags & FLAG_JSON)) { -// output_separate(&jsonchain); - -// if (info.type) -// pretty_dist_printf(&jsonchain, "Address space:\t", ADDRSPACE_NAME, "%s", info.type); - -// if ((flags & FLAG_SHOW_ALL_INFO) && info.class) -// pretty_dist_printf(&jsonchain, "Address class:\t", ADDRCLASS_NAME, "%s", info.class); - -// if (info.hostmin) -// pretty_printf(&jsonchain, "HostMin:\t", MINADDR_NAME, "%s", info.hostmin); - -// if (info.hostmax) -// pretty_printf(&jsonchain, "HostMax:\t", MAXADDR_NAME, "%s", info.hostmax); - -// if ((flags & FLAG_IPV6) && info.prefix < 112 && !(flags & FLAG_JSON)) -// pretty_printf(&jsonchain, "Hosts/Net:\t", ADDRESSES_NAME, "2^(%u) = %s", 128-info.prefix, info.hosts); -// else -// pretty_printf(&jsonchain, "Hosts/Net:\t", ADDRESSES_NAME, "%s", info.hosts); - -// } else { - -// if (info.type) -// pretty_dist_printf(&jsonchain, "Address space:\t", ADDRSPACE_NAME, "%s", info.type); - -// if ((flags & FLAG_SHOW_ALL_INFO) && info.class) -// pretty_dist_printf(&jsonchain, "Address class:\t", ADDRCLASS_NAME, "%s", info.class); -// } - -// if (info.geoip_country || info.geoip_city || info.geoip_coord) { -// output_separate(&jsonchain); - -// if (info.geoip_ccode) -// pretty_dist_printf(&jsonchain, "Country code:\t", COUNTRYCODE_NAME, "%s", info.geoip_ccode); -// if (info.geoip_country) -// pretty_dist_printf(&jsonchain, "Country:\t", COUNTRY_NAME, "%s", info.geoip_country); -// if (info.geoip_city) -// pretty_dist_printf(&jsonchain, "City:\t\t", CITY_NAME, "%s", info.geoip_city); -// if (info.geoip_coord) -// pretty_dist_printf(&jsonchain, "Coordinates:\t", COORDINATES_NAME, "%s", info.geoip_coord); -// } - -// output_stop(&jsonchain); - -// } else if (!(flags & FLAG_SHOW_MODERN_INFO)) { - -// if (flags & FLAG_SHOW_ADDRESS) { -// if (! (flags & FLAG_NO_DECORATE)) { -// printf(ADDRESS_NAME"="); -// } -// printf("%s\n", info.ip); -// } - -// if (flags & FLAG_SHOW_NETMASK) { -// if (! (flags & FLAG_NO_DECORATE)) { -// printf(NETMASK_NAME"="); -// } -// printf("%s\n", info.netmask); -// } - -// if (flags & FLAG_SHOW_PREFIX) { -// if (! (flags & FLAG_NO_DECORATE)) { -// printf(PREFIX_NAME"="); -// } -// printf("%u\n", info.prefix); -// } - -// if ((flags & FLAG_SHOW_BROADCAST) && !(flags & FLAG_IPV6)) { -// if (! (flags & FLAG_NO_DECORATE)) { -// printf(BROADCAST_NAME"="); -// } -// printf("%s\n", info.broadcast); -// } - -// if (flags & FLAG_SHOW_NETWORK) { -// if (! (flags & FLAG_NO_DECORATE)) { -// printf(NETWORK_NAME"="); -// } -// printf("%s\n", info.network); -// } - -// if (flags & FLAG_SHOW_REVERSE) { -// if (! (flags & FLAG_NO_DECORATE)) { -// printf(REVERSEDNS_NAME"="); -// } -// printf("%s\n", info.reverse_dns); -// } - -// if ((flags & FLAG_SHOW_MINADDR) && info.hostmin) { -// if (! (flags & FLAG_NO_DECORATE)) { -// printf(MINADDR_NAME"="); -// } -// printf("%s\n", info.hostmin); -// } - -// if ((flags & FLAG_SHOW_MAXADDR) && info.hostmax) { -// if (! (flags & FLAG_NO_DECORATE)) { -// printf(MAXADDR_NAME"="); -// } -// printf("%s\n", info.hostmax); -// } - -// if ((flags & FLAG_SHOW_ADDRSPACE) && info.type) { -// if (! (flags & FLAG_NO_DECORATE)) { -// printf(ADDRSPACE_NAME"="); -// } -// if (strchr(info.type, ' ') != NULL) -// printf("\"%s\"\n", info.type); -// else -// printf("%s\n", info.type); -// } - -// if ((flags & FLAG_SHOW_ADDRESSES) && info.hosts[0]) { -// if (! (flags & FLAG_NO_DECORATE)) { -// printf(ADDRESSES_NAME"="); -// } -// if (strchr(info.hosts, ' ') != NULL) -// printf("\"%s\"\n", info.hosts); -// else -// printf("%s\n", info.hosts); -// } - -// if ((flags & FLAG_RESOLVE_HOST) && info.hostname) { -// if (! (flags & FLAG_NO_DECORATE)) { -// printf(HOSTNAME_NAME"="); -// } -// printf("%s\n", info.hostname); -// } - -// if (flags & FLAG_RESOLVE_IP) { -// if (! (flags & FLAG_NO_DECORATE)) { -// printf(ADDRESS_NAME"="); -// } -// printf("%s\n", ipStr); -// } - -// if ((flags & FLAG_SHOW_GEOIP) == FLAG_SHOW_GEOIP) { -// if (info.geoip_ccode) { -// if (! (flags & FLAG_NO_DECORATE)) { -// printf(COUNTRYCODE_NAME"="); -// } -// printf("%s\n", info.geoip_ccode); -// } -// if (info.geoip_country) { -// if (! (flags & FLAG_NO_DECORATE)) { -// printf(COUNTRY_NAME"="); -// } -// if (strchr(info.geoip_country, ' ') != NULL) -// printf("\"%s\"\n", info.geoip_country); -// else -// printf("%s\n", info.geoip_country); -// } -// if (info.geoip_city) { -// if (! (flags & FLAG_NO_DECORATE)) { -// printf(CITY_NAME"="); -// } -// if (strchr(info.geoip_city, ' ') != NULL) { -// printf("\"%s\"\n", info.geoip_city); -// } else { -// printf("%s\n", info.geoip_city); -// } -// } -// if (info.geoip_coord) { -// if (! (flags & FLAG_NO_DECORATE)) { -// printf(COORDINATES_NAME"="); -// } -// printf("\"%s\"\n", info.geoip_coord); -// } -// } -// } - -// return 0; -// } - diff --git a/ww/ipcalc/ipcalc.h b/ww/ipcalc/ipcalc.h deleted file mode 100644 index a220dc5b..00000000 --- a/ww/ipcalc/ipcalc.h +++ /dev/null @@ -1,156 +0,0 @@ -/* - * Copyright (c) 2016 Red Hat, Inc. All rights reserved. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License, version 2, - * as published by the Free Software Foundation. - * - * 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 . - * - * Authors: - * Nikos Mavrogiannopoulos - */ - -#ifndef _IPCALC_H -#define _IPCALC_H - -#include /* for va_list */ - -#if defined(USE_GEOIP) - void geo_ip_lookup(const char *ip, char **country, char **ccode, char **city, char **coord); - int geo_setup(void); -# ifndef USE_RUNTIME_LINKING -# define geo_setup() 0 -# endif -#elif defined(USE_MAXMIND) - void geo_ip_lookup(const char *ip, char **country, char **ccode, char **city, char **coord); - int geo_setup(void); -# ifndef USE_RUNTIME_LINKING -# define geo_setup() 0 -# endif -#else -# define geo_ipv4_lookup(x,y,z,w,a) -# define geo_ipv6_lookup(x,y,z,w,a) -# define geo_setup() -1 -#endif - -int __attribute__((__format__(printf, 2, 3))) safe_asprintf(char **strp, const char *fmt, ...); -char __attribute__((warn_unused_result)) *safe_strdup(const char *str); -int safe_atoi(const char *s, int *ret_i); - -char *calc_reverse_dns4(struct in_addr ip, unsigned prefix, struct in_addr net, struct in_addr bcast); -char *calc_reverse_dns6(struct in6_addr *ip, unsigned prefix); - -uint32_t prefix2mask(int prefix); -int ipv6_prefix_to_mask(unsigned prefix, struct in6_addr *mask); - -struct in_addr calc_network(struct in_addr addr, int prefix); - -char *ipv4_prefix_to_hosts(char *hosts, unsigned hosts_size, unsigned prefix); -char *ipv6_prefix_to_hosts(char *hosts, unsigned hosts_size, unsigned prefix); - -typedef struct ip_info_st { - char *ip; - char *expanded_ip; - char *expanded_network; - char *reverse_dns; - - char *network; - char *broadcast; /* ipv4 only */ - char *netmask; - char *hostname; - char *geoip_country; - char *geoip_ccode; - char *geoip_city; - char *geoip_coord; - char hosts[64]; /* number of hosts in text */ - unsigned prefix; - - char *hostmin; - char *hostmax; - const char *type; - const char *class; -} ip_info_st; - -enum app_t { - APP_VERSION=1, - APP_CHECK_ADDRESS=1<<1, - APP_SHOW_INFO=1<<2, - APP_SPLIT=1<<3, - APP_DEAGGREGATE=1<<4 -}; - -#define FLAG_IPV6 (1<<1) -#define FLAG_IPV4 (1<<2) -#define FLAG_SHOW_MODERN_INFO (1<<3) -#define FLAG_RESOLVE_IP (1<<4) -#define FLAG_RESOLVE_HOST (1<<5) -#define FLAG_SHOW_BROADCAST (1<<6) -#define FLAG_SHOW_NETMASK (1<<7) -#define FLAG_SHOW_NETWORK (1<<8) -#define FLAG_SHOW_PREFIX (1<<9) -#define FLAG_SHOW_MINADDR (1<<10) -#define FLAG_SHOW_MAXADDR (1<<11) -#define FLAG_SHOW_ADDRESSES (1<<12) -#define FLAG_SHOW_ADDRSPACE (1<<13) -#define FLAG_GET_GEOIP (1<<14) -#define FLAG_SHOW_GEOIP ((1<<15)|FLAG_GET_GEOIP) -#define FLAG_SHOW_ALL_INFO (1<<16) -#define FLAG_SHOW_REVERSE (1<<17) -#define FLAG_ASSUME_CLASS_PREFIX (1<<18) -#define FLAG_NO_DECORATE (1<<20) -#define FLAG_SHOW_ADDRESS (1<<21) -#define FLAG_JSON (1<<22) -#define FLAG_RANDOM (1<<23) - -/* Flags that are modifying an existing option */ -#define FLAGS_TO_IGNORE (FLAG_IPV6|FLAG_IPV4|FLAG_GET_GEOIP|FLAG_NO_DECORATE|FLAG_JSON|FLAG_ASSUME_CLASS_PREFIX|(1<<16)|FLAG_RANDOM) -#define FLAGS_TO_IGNORE_MASK (~FLAGS_TO_IGNORE) - -#define ENV_INFO_FLAGS (FLAG_SHOW_NETMASK|FLAG_SHOW_BROADCAST|FLAG_RESOLVE_IP|FLAG_RESOLVE_HOST|FLAG_SHOW_ADDRESS|FLAG_SHOW_REVERSE|FLAG_SHOW_GEOIP|FLAG_SHOW_ADDRSPACE|FLAG_SHOW_ADDRESSES|FLAG_SHOW_MAXADDR|FLAG_SHOW_MINADDR|FLAG_SHOW_PREFIX|FLAG_SHOW_NETWORK) -#define ENV_INFO_MASK (~ENV_INFO_FLAGS) - -void show_split_networks_v4(unsigned split_prefix, const struct ip_info_st *info, unsigned flags); -void show_split_networks_v6(unsigned split_prefix, const struct ip_info_st *info, unsigned flags); - -void deaggregate(char *str, unsigned flags); - -#define KBLUE "\x1B[34m" -#define KMAG "\x1B[35m" -#define KRESET "\033[0m" - -#define JSON_FIRST 0 -#define JSON_NEXT 1 -#define JSON_ARRAY_FIRST 2 -#define JSON_ARRAY_NEXT 3 - -void -__attribute__ ((format(printf, 3, 4))) -color_printf(const char *color, const char *title, const char *fmt, ...); -void -__attribute__ ((format(printf, 3, 4))) -json_printf(unsigned * const jsonfirst, const char *jsontitle, const char *fmt, ...); -void va_color_printf(const char *color, const char *title, const char *fmt, va_list varglist); -void va_json_printf(unsigned * const jsonfirst, const char *jsontitle, const char *fmt, va_list varglist); - -void -__attribute__ ((format(printf, 4, 5))) -default_printf(unsigned * const jsonfirst, const char *title, const char *jsontitle, const char *fmt, ...); -void -__attribute__ ((format(printf, 4, 5))) -dist_printf(unsigned * const jsonfirst, const char *title, const char *jsontitle, const char *fmt, ...); - -void array_start(unsigned * const jsonfirst, const char *head, const char *json_head); -void array_stop(unsigned * const jsonfirst); -void output_start(unsigned * const jsonfirst); -void output_stop(unsigned * const jsonfirst); - -extern int beSilent; - -#endif diff --git a/ww/ipcalc/ipv6.c b/ww/ipcalc/ipv6.c deleted file mode 100644 index 6fc58610..00000000 --- a/ww/ipcalc/ipv6.c +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright (c) 2019 Nikos Mavrogiannopoulos - * Copyright 1991-1997, 1999-2014 Free Software Foundation, Inc. - * - * 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, see . - */ - -#include -#include -#include -#include -#include -#include - -/* Adds the two IP addresses and places the output in ap. This is based - * on Niels Moeller's mini-gmp. */ -void ipv6_add(struct in6_addr *ap, const struct in6_addr *bp) -{ - int i; - uint8_t cy; - - for (i = 15, cy = 0; i >= 0; i--) { - uint8_t a, b, r; - a = ap->s6_addr[i]; - b = bp->s6_addr[i]; - r = a + cy; - cy = (r < cy); - r += b; - cy += (r < b); - ap->s6_addr[i] = r; - } - return; -} - -/* Sets the "bit" of the IPv6 address to one */ -void ipv6_or1(struct in6_addr *a, unsigned bit) -{ - unsigned byte = bit / 8; - unsigned shift = bit % 8; - - assert(bit < 128); - - a->s6_addr[15 - byte] |= 1 << shift; -} - -/* Sets all bits below "bits" to one */ -void ipv6_orm(struct in6_addr *a, unsigned bits) -{ - int i; - unsigned bytes = bits / 8; - - assert(bits < 128); - - for (i = 0; i < bytes; i++) - a->s6_addr[15 - i] |= 0xff; - - for (i = bytes * 8; i < bits; i++) - ipv6_or1(a, i); -} - -/* Returns 1 if IP a is greater than IP b, 0 if equal, -1 if less */ -int ipv6_cmp(struct in6_addr *a, struct in6_addr *b) -{ - unsigned i; - for (i = 0; i < 16; i++) { - if (a->s6_addr[i] != b->s6_addr[i]) - return a->s6_addr[i] > b->s6_addr[i] ? 1 : -1; - } - return 0; -} diff --git a/ww/ipcalc/ipv6.h b/ww/ipcalc/ipv6.h deleted file mode 100644 index b1ce8843..00000000 --- a/ww/ipcalc/ipv6.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (c) 2019 Nikos Mavrogiannopoulos - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License, version 2, - * as published by the Free Software Foundation. - * - * 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 . - * - */ - -#ifndef IPV6_H -#define IPV6_H - -#include -#include - -void ipv6_add(struct in6_addr *a, const struct in6_addr *b); -void ipv6_or1(struct in6_addr *a, unsigned bit); -void ipv6_orm(struct in6_addr *a, unsigned bit); -int ipv6_cmp(struct in6_addr *a, struct in6_addr *b); - -#endif diff --git a/ww/ipcalc/netsplit.c b/ww/ipcalc/netsplit.c deleted file mode 100644 index 05fb1fb6..00000000 --- a/ww/ipcalc/netsplit.c +++ /dev/null @@ -1,253 +0,0 @@ -/* - * Copyright (c) 2003-2016 Simon Ekstrand - * Copyright (c) 2010-2016 Joachim Nilsson - * Copyright (c) 2016 Nikos Mavrogiannopoulos - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include -#include -#include -#include -#include -#include - -#include "ipv6.h" -#include "ipcalc.h" - -static const char *numtoquad(uint32_t num) -{ - static char quad[64]; - - num = htonl(num); - return inet_ntop(AF_INET, &num, quad, sizeof(quad)); -} - -void show_split_networks_v4(unsigned split_prefix, const struct ip_info_st *info, unsigned flags) -{ - char buf[64]; - uint32_t diff, start, end; - size_t maxlen = 0; - unsigned count; - uint32_t splitmask = ntohl(prefix2mask(split_prefix)); - uint32_t nmask = ntohl(prefix2mask(info->prefix)); - struct in_addr net, broadcast; - unsigned jsonchain = JSON_FIRST; - - if (splitmask < nmask) { - if (!beSilent) - fprintf(stderr, "Cannot subnet to /%d with this base network, use a prefix > /%d\n", - split_prefix, info->prefix); - exit(1); - } - - output_start(&jsonchain); - array_start(&jsonchain, "Split networks", "SPLITNETWORK"); - - if (inet_pton(AF_INET, info->network, &net) <= 0) { - if (!beSilent) - fprintf(stderr, "ipcalc: bad IPv4 address: %s\n", info->network); - exit(1); - } - net.s_addr = ntohl(net.s_addr); - - if (inet_pton(AF_INET, info->broadcast, &broadcast) <= 0) { - if (!beSilent) - fprintf(stderr, "ipcalc: bad broadcast address: %s\n", info->broadcast); - exit(1); - } - broadcast.s_addr = ntohl(broadcast.s_addr); - - diff = 0xffffffff - splitmask + 1; - start = net.s_addr; - end = net.s_addr + diff - 1; - - /* broadcast here is used to get maximum hosts in network. Set it to start + 1 for /31. */ - if (info->prefix == 31) - broadcast.s_addr = start + 1; - - /* Figure out max width of a network string. */ - while (1) { - size_t len; - - len = snprintf(buf, sizeof(buf), "%s", numtoquad(start)); - if (len > maxlen) - maxlen = len; - - start += diff; - if (end == 0xffffffff || end >= broadcast.s_addr) - break; - end += diff; - } - - start = net.s_addr; - end = net.s_addr + diff - 1; - count = 0; - while (1) { - if (!(flags & FLAG_NO_DECORATE) || (flags & FLAG_JSON)) { - default_printf(&jsonchain, "Network:\t", NULL, "%s/%u", numtoquad(start), split_prefix); - } else { - printf("%s/%u\n", numtoquad(start), split_prefix); - } - - count++; - start += diff; - if (end == 0xffffffff || end >= broadcast.s_addr) - break; - end += diff; - } - - array_stop(&jsonchain); - - if ((!(flags & FLAG_NO_DECORATE)) || (flags & FLAG_JSON)) { - dist_printf(&jsonchain, "\nTotal: \t", "NETS", "%u", count); - dist_printf(&jsonchain, "Hosts/Net:\t", "ADDRESSES", "%s", ipv4_prefix_to_hosts(buf, sizeof(buf), split_prefix)); - } - - output_stop(&jsonchain); -} - -static const char *ipv6tostr(struct in6_addr *ip) -{ - static char str[64]; - - return inet_ntop(AF_INET6, ip, str, sizeof(str)); -} - -void show_split_networks_v6(unsigned split_prefix, const struct ip_info_st *info, unsigned flags) -{ - int i, j, k; - unsigned count; - struct in6_addr splitmask, net, netmask, sdiff, ediff, start, end, tmpaddr, netlast; - char buf[32]; - unsigned jsonchain = JSON_FIRST; - - if (inet_pton(AF_INET6, info->network, &net) <= 0) { - if (!beSilent) - fprintf(stderr, "ipcalc: bad IPv6 address: %s\n", info->network); - exit(1); - } - - if (inet_pton(AF_INET6, info->hostmax, &netlast) <= 0) { - if (!beSilent) - fprintf(stderr, "ipcalc: bad IPv6 address: %s\n", info->hostmax); - exit(1); - } - - if (inet_pton(AF_INET6, info->netmask, &netmask) <= 0) { - if (!beSilent) - fprintf(stderr, "ipcalc: bad IPv6 mask: %s\n", info->netmask); - exit(1); - } - - if (ipv6_prefix_to_mask(split_prefix, &splitmask) < 0) { - if (!beSilent) - fprintf(stderr, "ipcalc: IPv6 prefix: %d\n", split_prefix); - exit(1); - } - - i = 0; - j = 0; - do { - if (splitmask.s6_addr[i] > netmask.s6_addr[i]) - j = 1; - if (netmask.s6_addr[i] > splitmask.s6_addr[i]) - j = 2; - i++; - } while (i < 16 && !j); - - if (j == 2) { - if (!beSilent) - fprintf(stderr, "Cannot subnet to /%d with this base network, use a prefix > /%d\n", - split_prefix, info->prefix); - exit(1); - } - - memset(&sdiff, 0, sizeof(sdiff)); - memset(&ediff, 0, sizeof(ediff)); - - for (i = 0; i < 16; i++) { - sdiff.s6_addr[i] = 0xff - splitmask.s6_addr[i]; - end.s6_addr[i] = net.s6_addr[i] + sdiff.s6_addr[i]; - } - - memcpy(&start, &net, sizeof(net)); - memcpy(&ediff, &sdiff, sizeof(sdiff)); - - memset(&tmpaddr, 0, sizeof(tmpaddr)); - tmpaddr.s6_addr[15] = 1; - ipv6_add(&sdiff, &tmpaddr); - - output_start(&jsonchain); - - array_start(&jsonchain, "Split networks", "SPLITNETWORK"); - - i = count = 0; - while (!i) { - if (!(flags & FLAG_NO_DECORATE) || (flags & FLAG_JSON)) { - default_printf(&jsonchain, "Network:\t", NULL, "%s/%u", ipv6tostr(&start), split_prefix); - } else { - printf("%s/%u\n", ipv6tostr(&start), split_prefix); - } - - ipv6_add(&start, &sdiff); - - j = 0; - for (k = 0; k < 16; k+=2) - if (end.s6_addr[k] != 0xff && end.s6_addr[k+1] != 0xff) - j = 1; - if (!j) - i = 1; - - j = 0; - k = 0; - do { - if (end.s6_addr[k] > netlast.s6_addr[k]) - j = 1; - if (netlast.s6_addr[k] > end.s6_addr[k]) - j = 2; - k++; - } while (k < 16 && !j); - - if (!j || j == 1) - i = 1; - - memset(&end, 0, sizeof(end)); - - ipv6_add(&end, &start); - ipv6_add(&end, &ediff); - count++; - } - - array_stop(&jsonchain); - - if ((!(flags & FLAG_NO_DECORATE)) || (flags & FLAG_JSON)) { - dist_printf(&jsonchain, "\nTotal: \t", "NETS", "%u", count); - dist_printf(&jsonchain, "Hosts/Net:\t", "ADDRESSES", "%s", ipv6_prefix_to_hosts(buf, sizeof(buf), split_prefix)); - } - - output_stop(&jsonchain); - -} -