home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / nmap254b.zip / utils.h < prev   
C/C++ Source or Header  |  2002-02-25  |  7KB  |  190 lines

  1. /***********************************************************************/
  2. /* utils.c -- Various miscellaneous utility functions which defy       */
  3. /* categorization :)                                                   */
  4. /*                                                                     */
  5. /***********************************************************************/
  6. /*  The Nmap Security Scanner is (C) 1995-2001 Insecure.Com LLC. This  */
  7. /*  program is free software; you can redistribute it and/or modify    */
  8. /*  it under the terms of the GNU General Public License as published  */
  9. /*  by the Free Software Foundation; Version 2.  This guarantees your  */
  10. /*  right to use, modify, and redistribute this software under certain */
  11. /*  conditions.  If this license is unacceptable to you, we may be     */
  12. /*  willing to sell alternative licenses (contact sales@insecure.com). */
  13. /*                                                                     */
  14. /*  If you received these files with a written license agreement       */
  15. /*  stating terms other than the (GPL) terms above, then that          */
  16. /*  alternative license agreement takes precendence over this comment. */
  17. /*                                                                     */
  18. /*  Source is provided to this software because we believe users have  */
  19. /*  a right to know exactly what a program is going to do before they  */
  20. /*  run it.  This also allows you to audit the software for security   */
  21. /*  holes (none have been found so far).                               */
  22. /*                                                                     */
  23. /*  Source code also allows you to port Nmap to new platforms, fix     */
  24. /*  bugs, and add new features.  You are highly encouraged to send     */
  25. /*  your changes to fyodor@insecure.org for possible incorporation     */
  26. /*  into the main distribution.  By sending these changes to Fyodor or */
  27. /*  one the insecure.org development mailing lists, it is assumed that */
  28. /*  you are offering Fyodor the unlimited, non-exclusive right to      */
  29. /*  reuse, modify, and relicense the code.  This is important because  */
  30. /*  the inability to relicense code has caused devastating problems    */
  31. /*  for other Free Software projects (such as KDE and NASM).  Nmap     */
  32. /*  will always be available Open Source.  If you wish to specify      */
  33. /*  special license conditions of your contributions, just say so      */
  34. /*  when you send them.                                                */
  35. /*                                                                     */
  36. /*  This program is distributed in the hope that it will be useful,    */
  37. /*  but WITHOUT ANY WARRANTY; without even the implied warranty of     */
  38. /*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  */
  39. /*  General Public License for more details (                          */
  40. /*  http://www.gnu.org/copyleft/gpl.html ).                            */
  41. /*                                                                     */
  42. /***********************************************************************/
  43.  
  44. /* $Id: utils.h,v 1.30 2001/08/23 05:48:22 fyodor Exp $ */
  45.  
  46. #ifndef UTILS_H
  47. #define UTILS_H
  48.  
  49. #ifdef WIN32
  50. #include "mswin32\winclude.h"
  51. #else
  52. #include <stdio.h>
  53. #include <stdlib.h>
  54. #include <stdarg.h>
  55. #include <string.h>
  56. #include <errno.h>
  57. #include <ctype.h>
  58. #include <sys/types.h>
  59.  
  60. #if HAVE_NETINET_IN_H
  61. #include <netinet/in.h>
  62. #endif
  63.  
  64. #include <sys/time.h>
  65. #include <assert.h>
  66. #ifndef __EMX__
  67. #include <sys/mman.h>
  68. #endif
  69. #include "config.h"
  70. #endif
  71.  
  72. #if HAVAE_UNISTD_H
  73. #include <unistd.h>
  74. #endif
  75.  
  76. #if TIME_WITH_SYS_TIME
  77. # include <sys/time.h>
  78. # include <time.h>
  79. #else
  80. # if HAVE_SYS_TIME_H
  81. #  include <sys/time.h>
  82. # else
  83. #  include <time.h>
  84. # endif
  85. #endif
  86.  
  87. #include "nmap_error.h"
  88. #include "nmap.h"
  89. #include "global_structures.h"
  90.  
  91. #ifndef MAX
  92. #define MAX(x,y) (((x)>(y))?(x):(y))
  93. #endif
  94. #ifndef MIN
  95. #define MIN(x,y) (((x)<(y))?(x):(y))
  96. #endif
  97. #ifndef ABS
  98. #define ABS(x) (((x) >= 0)?(x):(-x)) 
  99. #endif
  100. #ifndef MOD_DIFF
  101. #define MOD_DIFF(a,b) ((unsigned long) (MIN((unsigned long)(a) - (unsigned long ) (b), (unsigned long )(b) - (unsigned long) (a))))
  102. #endif
  103. #ifndef MOD_DIFF_USHORT
  104. #define MOD_DIFF_USHORT(a,b) ((MIN((unsigned short)((unsigned short)(a) - (unsigned short ) (b)), (unsigned short) ((unsigned short )(b) - (unsigned short) (a)))))
  105. #endif
  106. #ifndef FALSE
  107. #define FALSE 0
  108. #endif
  109. #ifndef TRUE
  110. #define TRUE 1
  111. #endif
  112.  
  113. #define NIPQUAD(addr) \
  114.         (((addr) >> 0)  & 0xff), \
  115.         (((addr) >> 8)  & 0xff), \
  116.         (((addr) >> 16) & 0xff), \
  117.         (((addr) >> 24) & 0xff)
  118.  
  119. #define MAX_PARSE_ARGS 254 /* +1 for integrity checking + 1 for null term */
  120.  
  121. /* Timeval subtraction in microseconds */
  122. #define TIMEVAL_SUBTRACT(a,b) (((a).tv_sec - (b).tv_sec) * 1000000 + (a).tv_usec - (b).tv_usec)
  123. /* Timeval subtract in milliseconds */
  124. #define TIMEVAL_MSEC_SUBTRACT(a,b) ((((a).tv_sec - (b).tv_sec) * 1000) + ((a).tv_usec - (b).tv_usec) / 1000)
  125. /* Timeval subtract in seconds; truncate towards zero */
  126. #define TIMEVAL_SEC_SUBTRACT(a,b) ((a).tv_sec - (b).tv_sec + (((a).tv_usec < (b).tv_usec) ? - 1 : 0))
  127.  
  128. /* assign one timeval to another timeval plus some msecs: a = b + msecs */
  129. #define TIMEVAL_MSEC_ADD(a, b, msecs) (a).tv_sec = (b).tv_sec + ((msecs) / 1000); (a).tv_usec = (b).tv_usec + ((msecs) % 1000) * 1000; (a).tv_sec += (a).tv_usec / 1000000; (a).tv_usec %= 1000000
  130.  
  131.  
  132. void *safe_malloc(int size);
  133. void hdump(unsigned char *packet, unsigned int len);
  134. void lamont_hdump(unsigned char *bp, unsigned int length);
  135. int get_random_bytes(void *buf, int numbytes);
  136. int get_random_int();
  137. unsigned short get_random_ushort();
  138. unsigned int get_random_uint();
  139. u32 get_random_u32();
  140. u8 get_random_u8();
  141.  
  142. /* Scramble the contents of an array*/
  143. void genfry(unsigned char *arr, int elem_sz, int num_elem);
  144. void shortfry(unsigned short *arr, int num_elem);
  145. /* Like the perl equivialent -- It removes the terminating newline from string
  146.    IF one exists.  It then returns the POSSIBLY MODIFIED string */
  147. char *chomp(char *string);
  148. ssize_t Write(int fd, const void *buf, size_t count);
  149.  
  150. unsigned long gcd_ulong(unsigned long a, unsigned long b);
  151. unsigned int gcd_uint(unsigned int a, unsigned int b);
  152. unsigned long gcd_n_ulong(long nvals, unsigned long *val);
  153. unsigned int gcd_n_uint(int nvals, unsigned int *val);
  154.  
  155. int arg_parse(const char *command, char ***argv);
  156. void arg_parse_free(char **argv);
  157.  
  158. #ifndef HAVE_USLEEP
  159. #ifdef HAVE_NANOSLEEP
  160. void usleep(unsigned long usec);
  161. #endif
  162. #endif
  163.  
  164. #ifndef HAVE_STRERROR
  165. char *strerror(int errnum);
  166. #endif
  167.  
  168.  
  169. /* mmap() an entire file into the address space.  Returns a pointer
  170.    to the beginning of the file.  The mmap'ed length is returned
  171.    inside the length parameter.  If there is a problem, NULL is
  172.    returned, the value of length is undefined, and errno is set to
  173.    something appropriate.  The user is responsible for doing
  174.    an munmap(ptr, length) when finished with it.  openflags should 
  175.    be O_RDONLY or O_RDWR, or O_WRONLY
  176. */
  177. char *mmapfile(char *fname, int *length, int openflags);
  178.  
  179. #ifdef WIN32
  180. #define PROT_READ       0x1             /* page can be read */
  181. #define PROT_WRITE      0x2             /* page can be written */
  182. #define PROT_EXEC       0x4             /* page can be executed */
  183. #define PROT_NONE       0x0             /* page can not be accessed */
  184.  
  185. #define MAP_SHARED      0x01            /* Share changes */
  186.  
  187. #endif /* WIN32 */
  188.  
  189. #endif /* UTILS_H */
  190.