1 /***************************************************************************
2  * nsock_pool.c -- This contains the functions that deal with creating,    *
3  * destroying, and otherwise manipulating nsock_pools (and their internal  *
4  * struct npool representation).  An nsock_pool aggregates and manages events    *
5  * and i/o descriptors                                                     *
6  *                                                                         *
7  ***********************IMPORTANT NSOCK LICENSE TERMS***********************
8  *                                                                         *
9  * The nsock parallel socket event library is (C) 1999-2020 Insecure.Com   *
10  * LLC This library is free software; you may redistribute and/or          *
11  * modify it under the terms of the GNU General Public License as          *
12  * published by the Free Software Foundation; Version 2.  This guarantees  *
13  * your right to use, modify, and redistribute this software under certain *
14  * conditions.  If this license is unacceptable to you, Insecure.Com LLC   *
15  * may be willing to sell alternative licenses (contact                    *
16  * sales@insecure.com ).                                                   *
17  *                                                                         *
18  * As a special exception to the GPL terms, Insecure.Com LLC grants        *
19  * permission to link the code of this program with any version of the     *
20  * OpenSSL library which is distributed under a license identical to that  *
21  * listed in the included docs/licenses/OpenSSL.txt file, and distribute   *
22  * linked combinations including the two. You must obey the GNU GPL in all *
23  * respects for all of the code used other than OpenSSL.  If you modify    *
24  * this file, you may extend this exception to your version of the file,   *
25  * but you are not obligated to do so.                                     *
26  *                                                                         *
27  * If you received these files with a written license agreement stating    *
28  * terms other than the (GPL) terms above, then that alternative license   *
29  * agreement takes precedence over this comment.                           *
30  *                                                                         *
31  * Source is provided to this software because we believe users have a     *
32  * right to know exactly what a program is going to do before they run it. *
33  * This also allows you to audit the software for security holes.          *
34  *                                                                         *
35  * Source code also allows you to port Nmap to new platforms, fix bugs,    *
36  * and add new features.  You are highly encouraged to send your changes   *
37  * to the dev@nmap.org mailing list for possible incorporation into the    *
38  * main distribution.  By sending these changes to Fyodor or one of the    *
39  * Insecure.Org development mailing lists, or checking them into the Nmap  *
40  * source code repository, it is understood (unless you specify otherwise) *
41  * that you are offering the Nmap Project (Insecure.Com LLC) the           *
42  * unlimited, non-exclusive right to reuse, modify, and relicense the      *
43  * code.  Nmap will always be available Open Source, but this is important *
44  * because the inability to relicense code has caused devastating problems *
45  * for other Free Software projects (such as KDE and NASM).  We also       *
46  * occasionally relicense the code to third parties as discussed above.    *
47  * If you wish to specify special license conditions of your               *
48  * contributions, just say so when you send them.                          *
49  *                                                                         *
50  * This program is distributed in the hope that it will be useful, but     *
51  * WITHOUT ANY WARRANTY; without even the implied warranty of              *
52  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU       *
53  * General Public License v2.0 for more details                            *
54  * (http://www.gnu.org/licenses/gpl-2.0.html).                             *
55  *                                                                         *
56  ***************************************************************************/
57 
58 /* $Id: nsock_pool.c 38071 2020-10-02 05:02:05Z fyodor $ */
59 
60 #include "nsock_internal.h"
61 #include "nsock_log.h"
62 #include "gh_list.h"
63 #include "netutils.h"
64 
65 #include <string.h>
66 #ifdef HAVE_SYS_TIME_H
67 #include <sys/time.h>
68 #endif
69 #ifdef HAVE_UNISTD_H
70 #include <unistd.h>
71 #endif
72 #include <signal.h>
73 
74 #if HAVE_SYS_RESOURCE_H
75 #include <sys/resource.h>
76 #endif
77 
78 extern struct timeval nsock_tod;
79 
80 /* To use this library, the first thing they must do is create a pool
81  * so we do the initialization during the first pool creation */
82 static int nsocklib_initialized = 0;
83 
84 
85 /* defined in nsock_engines.h */
86 struct io_engine *get_io_engine(void);
87 
88 /* ---- INTERNAL FUNCTIONS PROTOTYPES ---- */
89 static void nsock_library_initialize(void);
90 /* --------------------------------------- */
91 
92 
93 /* This next function returns the errno style error code -- which is only
94  * valid if the status NSOCK_LOOP_ERROR was returned by nsock_loop() */
nsock_pool_get_error(nsock_pool nsp)95 int nsock_pool_get_error(nsock_pool nsp) {
96   struct npool *mt = (struct npool *)nsp;
97   return mt->errnum;
98 }
99 
100 /* Sometimes it is useful to store a pointer to information inside
101  * the NSP so you can retrieve it during a callback. */
nsock_pool_set_udata(nsock_pool nsp,void * data)102 void nsock_pool_set_udata(nsock_pool nsp, void *data) {
103   struct npool *mt = (struct npool *)nsp;
104   mt->userdata = data;
105 }
106 
107 /* And the define above wouldn't make much sense if we didn't have a way
108  * to retrieve that data ... */
nsock_pool_get_udata(nsock_pool nsp)109 void *nsock_pool_get_udata(nsock_pool nsp) {
110   struct npool *mt = (struct npool *)nsp;
111   return mt->userdata;
112 }
113 
114 /* Turns on or off broadcast support on new sockets. Default is off (0, false)
115  * set in nsock_pool_new(). Any non-zero (true) value sets SO_BROADCAST on all new
116  * sockets (value of optval will be used directly in the setsockopt() call */
nsock_pool_set_broadcast(nsock_pool nsp,int optval)117 void nsock_pool_set_broadcast(nsock_pool nsp, int optval) {
118   struct npool *mt = (struct npool *)nsp;
119   mt->broadcast = optval;
120 }
121 
122 /* Sets the name of the interface for new sockets to bind to. */
nsock_pool_set_device(nsock_pool nsp,const char * device)123 void nsock_pool_set_device(nsock_pool nsp, const char *device) {
124   struct npool *mt = (struct npool *)nsp;
125   mt->device = device;
126 }
127 
expirable_cmp(gh_hnode_t * n1,gh_hnode_t * n2)128 static int expirable_cmp(gh_hnode_t *n1, gh_hnode_t *n2) {
129   struct nevent *nse1;
130   struct nevent *nse2;
131 
132   nse1 = container_of(n1, struct nevent, expire);
133   nse2 = container_of(n2, struct nevent, expire);
134 
135   return (TIMEVAL_BEFORE(nse1->timeout, nse2->timeout)) ? 1 : 0;
136 }
137 
138 /* And here is how you create an nsock_pool.  This allocates, initializes, and
139  * returns an nsock_pool event aggregator.  In the case of error, NULL will be
140  * returned.  If you do not wish to immediately associate any userdata, pass in
141  * NULL. */
nsock_pool_new(void * userdata)142 nsock_pool nsock_pool_new(void *userdata) {
143   struct npool *nsp;
144 
145   /* initialize the library in not already done */
146   if (!nsocklib_initialized) {
147     nsock_library_initialize();
148     nsocklib_initialized = 1;
149   }
150 
151   nsp = (struct npool *)safe_malloc(sizeof(*nsp));
152   memset(nsp, 0, sizeof(*nsp));
153 
154   gettimeofday(&nsock_tod, NULL);
155 
156   nsp->userdata = userdata;
157 
158   nsp->engine = get_io_engine();
159   nsock_engine_init(nsp);
160 
161   /* initialize IO events lists */
162   gh_list_init(&nsp->connect_events);
163   gh_list_init(&nsp->read_events);
164   gh_list_init(&nsp->write_events);
165 #if HAVE_PCAP
166   gh_list_init(&nsp->pcap_read_events);
167 #endif
168 
169   /* initialize timer heap */
170   gh_heap_init(&nsp->expirables, expirable_cmp);
171 
172   /* initialize the list of IODs */
173   gh_list_init(&nsp->active_iods);
174 
175   /* initialize caches */
176   gh_list_init(&nsp->free_iods);
177   gh_list_init(&nsp->free_events);
178 
179   nsp->next_event_serial = 1;
180 
181   nsp->device = NULL;
182 
183 #if HAVE_OPENSSL
184   nsp->sslctx = NULL;
185 #endif
186 
187   nsp->px_chain = NULL;
188 
189   return (nsock_pool)nsp;
190 }
191 
192 /* If nsock_pool_new returned success, you must free the nsp when you are done with it
193  * to conserve memory (and in some cases, sockets).  After this call, nsp may no
194  * longer be used.  Any pending events are sent an NSE_STATUS_KILL callback and
195  * all outstanding iods are deleted. */
nsock_pool_delete(nsock_pool ms_pool)196 void nsock_pool_delete(nsock_pool ms_pool) {
197   struct npool *nsp = (struct npool *)ms_pool;
198   struct nevent *nse;
199   struct niod *nsi;
200   int i;
201   gh_lnode_t *current, *next;
202   gh_list_t *event_lists[] = {
203     &nsp->connect_events,
204     &nsp->read_events,
205     &nsp->write_events,
206 #if HAVE_PCAP
207     &nsp->pcap_read_events,
208 #endif
209     NULL
210   };
211 
212   assert(nsp);
213 
214   /* First I go through all the events sending NSE_STATUS_KILL */
215   for (i = 0; event_lists[i] != NULL; i++) {
216     while (gh_list_count(event_lists[i]) > 0) {
217       gh_lnode_t *lnode = gh_list_pop(event_lists[i]);
218 
219       assert(lnode);
220 
221 #if HAVE_PCAP
222       if (event_lists[i] == &nsp->pcap_read_events)
223         nse = lnode_nevent2(lnode);
224       else
225 #endif
226         nse = lnode_nevent(lnode);
227 
228       assert(nse);
229 
230       nse->status = NSE_STATUS_KILL;
231       nsock_trace_handler_callback(nsp, nse);
232       nse->handler(nsp, nse, nse->userdata);
233 
234       if (nse->iod) {
235         nse->iod->events_pending--;
236         assert(nse->iod->events_pending >= 0);
237       }
238       event_delete(nsp, nse);
239     }
240     gh_list_free(event_lists[i]);
241   }
242 
243   /* Kill timers too, they're not in event lists */
244   while (gh_heap_count(&nsp->expirables) > 0) {
245     gh_hnode_t *hnode;
246 
247     hnode = gh_heap_pop(&nsp->expirables);
248     nse = container_of(hnode, struct nevent, expire);
249 
250     if (nse->type == NSE_TYPE_TIMER) {
251       nse->status = NSE_STATUS_KILL;
252       nsock_trace_handler_callback(nsp, nse);
253       nse->handler(nsp, nse, nse->userdata);
254       event_delete(nsp, nse);
255       gh_list_append(&nsp->free_events, &nse->nodeq_io);
256     }
257   }
258 
259   gh_heap_free(&nsp->expirables);
260 
261   /* foreach struct niod */
262   for (current = gh_list_first_elem(&nsp->active_iods);
263        current != NULL;
264        current = next) {
265     next = gh_lnode_next(current);
266     nsi = container_of(current, struct niod, nodeq);
267 
268     nsock_iod_delete(nsi, NSOCK_PENDING_ERROR);
269 
270     gh_list_remove(&nsp->active_iods, current);
271     gh_list_prepend(&nsp->free_iods, &nsi->nodeq);
272   }
273 
274   /* Now we free all the memory in the free iod list */
275   while ((current = gh_list_pop(&nsp->free_iods))) {
276     nsi = container_of(current, struct niod, nodeq);
277     free(nsi);
278   }
279 
280   while ((current = gh_list_pop(&nsp->free_events))) {
281     nse = lnode_nevent(current);
282     free(nse);
283   }
284 
285   gh_list_free(&nsp->active_iods);
286   gh_list_free(&nsp->free_iods);
287   gh_list_free(&nsp->free_events);
288 
289   nsock_engine_destroy(nsp);
290 
291 #if HAVE_OPENSSL
292   if (nsp->sslctx != NULL)
293     SSL_CTX_free(nsp->sslctx);
294 #endif
295 
296   free(nsp);
297 }
298 
nsock_library_initialize(void)299 void nsock_library_initialize(void) {
300 #ifndef WIN32
301   rlim_t res;
302 
303   /* We want to make darn sure the evil SIGPIPE is ignored */
304   signal(SIGPIPE, SIG_IGN);
305 
306   /* And we're gonna need sockets -- LOTS of sockets ... */
307   res = maximize_fdlimit();
308   assert(res > 7);
309 #endif
310   return;
311 }
312 
313