1 /***************************************************************************
2  * nbase_winunix.h -- Misc. compatibility routines that generally try to   *
3  * reproduce UNIX-centric concepts on Windows.                             *
4  *                                                                         *
5  ***********************IMPORTANT NMAP LICENSE TERMS************************
6  *                                                                         *
7  * The Nmap Security Scanner is (C) 1996-2020 Insecure.Com LLC ("The Nmap  *
8  * Project"). Nmap is also a registered trademark of the Nmap Project.     *
9  *                                                                         *
10  * This program is distributed under the terms of the Nmap Public Source   *
11  * License (NPSL). The exact license text applying to a particular Nmap    *
12  * release or source code control revision is contained in the LICENSE     *
13  * file distributed with that version of Nmap or source code control       *
14  * revision. More Nmap copyright/legal information is available from       *
15  * https://nmap.org/book/man-legal.html, and further information on the    *
16  * NPSL license itself can be found at https://nmap.org/npsl. This header  *
17  * summarizes some key points from the Nmap license, but is no substitute  *
18  * for the actual license text.                                            *
19  *                                                                         *
20  * Nmap is generally free for end users to download and use themselves,    *
21  * including commercial use. It is available from https://nmap.org.        *
22  *                                                                         *
23  * The Nmap license generally prohibits companies from using and           *
24  * redistributing Nmap in commercial products, but we sell a special Nmap  *
25  * OEM Edition with a more permissive license and special features for     *
26  * this purpose. See https://nmap.org/oem                                  *
27  *                                                                         *
28  * If you have received a written Nmap license agreement or contract       *
29  * stating terms other than these (such as an Nmap OEM license), you may   *
30  * choose to use and redistribute Nmap under those terms instead.          *
31  *                                                                         *
32  * The official Nmap Windows builds include the Npcap software             *
33  * (https://npcap.org) for packet capture and transmission. It is under    *
34  * separate license terms which forbid redistribution without special      *
35  * permission. So the official Nmap Windows builds may not be              *
36  * redistributed without special permission (such as an Nmap OEM           *
37  * license).                                                               *
38  *                                                                         *
39  * Source is provided to this software because we believe users have a     *
40  * right to know exactly what a program is going to do before they run it. *
41  * This also allows you to audit the software for security holes.          *
42  *                                                                         *
43  * Source code also allows you to port Nmap to new platforms, fix bugs,    *
44  * and add new features.  You are highly encouraged to submit your         *
45  * changes as a Github PR or by email to the dev@nmap.org mailing list     *
46  * for possible incorporation into the main distribution. Unless you       *
47  * specify otherwise, it is understood that you are offering us very       *
48  * broad rights to use your submissions as described in the Nmap Public    *
49  * Source License Contributor Agreement. This is important because we      *
50  * fund the project by selling licenses with various terms, and also       *
51  * because the inability to relicense code has caused devastating          *
52  * problems for other Free Software projects (such as KDE and NASM).       *
53  *                                                                         *
54  * The free version of Nmap is distributed in the hope that it will be     *
55  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of  *
56  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Warranties,        *
57  * indemnification and commercial support are all available through the    *
58  * Npcap OEM program--see https://nmap.org/oem.                            *
59  *                                                                         *
60  ***************************************************************************/
61 
62 /* $Id: nbase_winunix.h 38078 2020-10-02 16:12:22Z dmiller $ */
63 
64 #ifndef NBASE_WINUNIX_H
65 #define NBASE_WINUNIX_H
66 
67 
68 #include "nbase_winconfig.h"
69 
70 /* Winsock defines its own error codes that are analogous to but
71    different from those in <errno.h>. The error macros have similar
72    names, for example
73      EINTR -> WSAEINTR
74      ECONNREFUSED -> WSAECONNREFUSED
75    But the values are different. The errno codes are small integers,
76    while the Winsock codes start at 10000 or so.
77    http://msdn.microsoft.com/en-us/library/ms737828
78 
79    Later in this file there is a block of code that defines the errno
80    names to their Winsock equivalents, so that you can write code using
81    the errno names only, and have it still work on Windows. However this
82    causes some problems that are worked around in the following few
83    lines. First, we prohibit the inclusion of <errno.h>, so that the
84    only error codes visible are those we explicitly define in this file.
85    This will cause a compilation error if someone uses a code we're not
86    yet aware of instead of using an incompatible value at runtime.
87    Second, because <errno.h> is not defined, the C++0x header
88    <system_error> doesn't compile, so we pretend not to have C++0x to
89    avoid it. */
90 #if _MSC_VER < 1600 /* Breaks on VS2010 and later */
91 #define _INC_ERRNO  /* suppress errno.h */
92 #define _ERRNO_H_ /* Also for errno.h suppression */
93 #define _SYSTEM_ERROR_
94 #undef _HAS_CPP0X
95 #define _HAS_CPP0X 0
96 #else
97 /* VS2013: we include errno.h, then redefine the constants we want.
98  * This may work in other versions, but haven't tested (since the other method
99  * has been working just fine). */
100 #include <errno.h>
101 #endif
102 
103 /* Suppress winsock.h */
104 #define _WINSOCKAPI_
105 #define WIN32_LEAN_AND_MEAN
106 
107 #include <windows.h>
108 #include <winsock2.h>
109 #include <ws2tcpip.h> /* IPv6 stuff */
110 #if HAVE_WSPIAPI_H
111 /* <wspiapi.h> is necessary for getaddrinfo before Windows XP, but it isn't
112    available on some platforms like MinGW. */
113 #include <wspiapi.h>
114 #endif
115 #include <time.h>
116 #include <iptypes.h>
117 #include <stdlib.h>
118 #include <malloc.h>
119 #include <io.h>
120 #include <fcntl.h>
121 #include <sys/stat.h>
122 #include <sys/types.h>
123 #include <process.h>
124 #include <limits.h>
125 #include <WINCRYPT.H>
126 #include <math.h>
127 
128 
129 #define SIOCGIFCONF     0x8912          /* get iface list */
130 
131 #ifndef GLOBALS
132 #define GLOBALS 1
133 
134 #endif
135 
136 #define munmap(ptr, len) win32_munmap(ptr, len)
137 
138 /* Windows error message names */
139 #undef  ECONNABORTED
140 #define ECONNABORTED    WSAECONNABORTED
141 #undef ECONNRESET
142 #define ECONNRESET      WSAECONNRESET
143 #undef ECONNREFUSED
144 #define ECONNREFUSED    WSAECONNREFUSED
145 #undef  EAGAIN
146 #define EAGAIN		WSAEWOULDBLOCK
147 #undef EWOULDBLOCK
148 #define EWOULDBLOCK	WSAEWOULDBLOCK
149 #undef EHOSTUNREACH
150 #define EHOSTUNREACH	WSAEHOSTUNREACH
151 #undef ENETDOWN
152 #define ENETDOWN	WSAENETDOWN
153 #undef ENETUNREACH
154 #define ENETUNREACH	WSAENETUNREACH
155 #undef ENETRESET
156 #define ENETRESET	WSAENETRESET
157 #undef ETIMEDOUT
158 #define ETIMEDOUT	WSAETIMEDOUT
159 #undef EHOSTDOWN
160 #define EHOSTDOWN	WSAEHOSTDOWN
161 #undef EINPROGRESS
162 #define EINPROGRESS	WSAEINPROGRESS
163 #undef  EINVAL
164 #define EINVAL          WSAEINVAL      /* Invalid argument */
165 #undef  EPERM
166 #define EPERM           WSAEACCES      /* Operation not permitted */
167 #undef  EACCES
168 #define EACCES          WSAEACCES     /* Operation not permitted */
169 #undef  EINTR
170 #define EINTR           WSAEINTR      /* Interrupted system call */
171 #undef ENOBUFS
172 #define ENOBUFS         WSAENOBUFS     /* No buffer space available */
173 #undef EMSGSIZE
174 #define EMSGSIZE        WSAEMSGSIZE    /* Message too long */
175 #undef  ENOMEM
176 #define ENOMEM          WSAENOBUFS
177 #undef  ENOTSOCK
178 #define ENOTSOCK        WSAENOTSOCK
179 #undef  EOPNOTSUPP
180 #define EOPNOTSUPP      WSAEOPNOTSUPP
181 #undef  EIO
182 #define EIO             WSASYSCALLFAILURE
183 
184 /*
185 This is not used by our network code, and causes problems in programs using
186 Nbase that legitimately use ENOENT for file operations.
187 #undef  ENOENT
188 #define ENOENT          WSAENOENT
189 */
190 
191 #define close(x) closesocket(x)
192 
193 typedef unsigned short u_short_t;
194 
195 int win_stdin_start_thread(void);
196 int win_stdin_ready(void);
197 
198 #endif /* NBASE_WINUNIX_H */
199