/*************************************************************************** * utils.cc -- Miscellaneous utils that didn't fit into any of the other * * source files. * * * ***********************IMPORTANT NMAP LICENSE TERMS************************ * * * The Nmap Security Scanner is (C) 1996-2022 Nmap Software LLC ("The Nmap * * Project"). Nmap is also a registered trademark of the Nmap Project. * * * * This program is distributed under the terms of the Nmap Public Source * * License (NPSL). The exact license text applying to a particular Nmap * * release or source code control revision is contained in the LICENSE * * file distributed with that version of Nmap or source code control * * revision. More Nmap copyright/legal information is available from * * https://nmap.org/book/man-legal.html, and further information on the * * NPSL license itself can be found at https://nmap.org/npsl/ . This * * header summarizes some key points from the Nmap license, but is no * * substitute for the actual license text. * * * * Nmap is generally free for end users to download and use themselves, * * including commercial use. It is available from https://nmap.org. * * * * The Nmap license generally prohibits companies from using and * * redistributing Nmap in commercial products, but we sell a special Nmap * * OEM Edition with a more permissive license and special features for * * this purpose. See https://nmap.org/oem/ * * * * If you have received a written Nmap license agreement or contract * * stating terms other than these (such as an Nmap OEM license), you may * * choose to use and redistribute Nmap under those terms instead. * * * * The official Nmap Windows builds include the Npcap software * * (https://npcap.com) for packet capture and transmission. It is under * * separate license terms which forbid redistribution without special * * permission. So the official Nmap Windows builds may not be * * redistributed without special permission (such as an Nmap OEM * * license). * * * * Source is provided to this software because we believe users have a * * right to know exactly what a program is going to do before they run it. * * This also allows you to audit the software for security holes. * * * * Source code also allows you to port Nmap to new platforms, fix bugs, * * and add new features. You are highly encouraged to submit your * * changes as a Github PR or by email to the dev@nmap.org mailing list * * for possible incorporation into the main distribution. Unless you * * specify otherwise, it is understood that you are offering us very * * broad rights to use your submissions as described in the Nmap Public * * Source License Contributor Agreement. This is important because we * * fund the project by selling licenses with various terms, and also * * because the inability to relicense code has caused devastating * * problems for other Free Software projects (such as KDE and NASM). * * * * The free version of Nmap 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. Warranties, * * indemnification and commercial support are all available through the * * Npcap OEM program--see https://nmap.org/oem/ * * * ***************************************************************************/ #include "nping.h" #include "utils.h" #include "NpingOps.h" #include "global_structures.h" #include "output.h" #include "nbase.h" #include "pcap.h" #include "dnet.h" #include #include extern NpingOps o; /** Returns true if "source" contains at least one instance of "substring" */ bool contains(const char *source, const char *substring){ if(source==NULL || substring==NULL ) nping_fatal(QT_3,"contains(): NULL value received."); if( strcasestr(source, substring) ) return true; else return false; } /* End of contains() */ /** Returns true if the supplied string matches "rand" or "random" (not case * sensitive)*/ bool meansRandom(const char *source){ if(source==NULL) nping_fatal(QT_3,"meansRandom(): NULL value received."); if( !strcasecmp(source, "rand") || !strcasecmp(source, "random") ) return true; else return false; } /* End of meansRandom() */ /** Returns true if source contains the representation of a number >= min and * <= max in the given base (with nothing following). */ static bool isNumber_range(const char *source, int base, unsigned long min, unsigned long max){ unsigned long ul; char *tail; errno = 0; ul = strtoul(source, &tail, base); if (errno != 0 || tail == source || *tail != '\0') return false; return ul >= min && ul <= max; } /** Returns true if "source" is a number in the supplied base that can fit * into a 8-bit var */ bool isNumber_u8(const char *source, int base){ return isNumber_range(source, base, 0UL, 0xFFUL); } /** Returns true if "source" is a number in the supplied base that can fit * into a 16-bit var */ bool isNumber_u16(const char *source, int base){ return isNumber_range(source, base, 0UL, 0xFFFFUL); } /** Returns true if "source" is a number in the supplied base that can fit * into a 32-bit var */ bool isNumber_u32(const char *source, int base){ return isNumber_range(source, base, 0UL, 0xFFFFFFFFUL); } /** Returns a buffer that contains the binary equivalent to the supplied * hex spec or NULL in case of error. * @warning Returned pointer points to a static buffer that subsequent calls * will overwrite. */ u8 *parseBufferSpec(char *str, size_t *outlen){ char auxbuff[1024]; static u8 dst[16384]; size_t dstlen=16384; unsigned int i=0, j=0; char *start=NULL; if(str==NULL || outlen==NULL) return NULL; /* This catches the empty string possibility "" */ if(strlen(str) == 0) return NULL; else memset(auxbuff,0,1024); /* String should be treated as a hex number in this format: 0xAABBCCDDEE... * We process it the way it is specified, we don't perform byte order * conversions so if the users says 0x00AA we write dst[0]=0x00, dst[1]==0xAA * no matter the endianness of the host system. */ if( !strncmp("0x", str, 2) ){ /* This catches the case of an empty "0x" */ if(strlen(str) == 2) return NULL; start=str+2; } /* String should be treated as list of hex char in this format: \x00\xFF\x0A*/ else if( !strncmp("\\x", str, 2) ){ /* This catches the case of an empty "\x" */ if(strlen(str) == 2) return NULL; /* Copy all interesting bytes to an aux array, discard "\x" */ for(i=0; i= min && result <= max) { *dst = result; return OP_SUCCESS; } else { return OP_FAILURE; } } /* End of parse_number() */ /** Takes a string representing an 8-bit number and converts it into an * actual integer. The result is stored in memory area pointed by * "dstbuff". Returns OP_SUCCESS if conversion was successful or * OP_FAILURE in case of error.*/ int parse_u8(const char *str, u8 *dst){ unsigned long ul; int ret; ret = parse_unsigned_number(str, 0UL, 0xffUL, &ul); if (ret == OP_SUCCESS) *dst = ul; return ret; } /** Takes a string representing a 16-bit number and converts it into an * actual integer. The result is stored in memory area pointed by * "dstbuff". Returns OP_SUCCESS if conversion was successful or * OP_FAILURE in case of error.*/ int parse_u16(const char *str, u16 *dst){ unsigned long ul; int ret; ret = parse_unsigned_number(str, 0UL, 0xffffUL, &ul); if (ret == OP_SUCCESS) *dst = ul; return ret; } /** Takes a string representing a 32-bit number and converts it into an * actual integer. The result is stored in memory area pointed by * "dstbuff". Returns OP_SUCCESS if conversion was successful or * OP_FAILURE in case of error.*/ int parse_u32(const char *str, u32 *dst){ unsigned long ul; int ret; ret = parse_unsigned_number(str, 0UL, 0xffffffffUL, &ul); if (ret == OP_SUCCESS) *dst = ul; return ret; } /** Prints the hexadecimal dump of the supplied buffer to standard output */ int print_hexdump(int level, const u8 *cp, u32 length){ char *str = hexdump(cp, length); if(str==NULL) return OP_FAILURE; nping_print(level, "%s", str); free(str); return OP_SUCCESS; } /* End of print_hexdump() */