PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/nsock/src/engine_select.c

https://gitlab.com/g10h4ck/nmap-gsoc2015
C | 393 lines | 222 code | 81 blank | 90 comment | 58 complexity | 1e24cde125ef3a606b7f16d7d97d8386 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, Apache-2.0, LGPL-2.0, LGPL-2.1, MIT
  1. /***************************************************************************
  2. * engine_select.c -- select(2) based IO engine. *
  3. * *
  4. ***********************IMPORTANT NSOCK LICENSE TERMS***********************
  5. * *
  6. * The nsock parallel socket event library is (C) 1999-2015 Insecure.Com *
  7. * LLC This library is free software; you may redistribute and/or *
  8. * modify it under the terms of the GNU General Public License as *
  9. * published by the Free Software Foundation; Version 2. This guarantees *
  10. * your right to use, modify, and redistribute this software under certain *
  11. * conditions. If this license is unacceptable to you, Insecure.Com LLC *
  12. * may be willing to sell alternative licenses (contact *
  13. * sales@insecure.com ). *
  14. * *
  15. * As a special exception to the GPL terms, Insecure.Com LLC grants *
  16. * permission to link the code of this program with any version of the *
  17. * OpenSSL library which is distributed under a license identical to that *
  18. * listed in the included docs/licenses/OpenSSL.txt file, and distribute *
  19. * linked combinations including the two. You must obey the GNU GPL in all *
  20. * respects for all of the code used other than OpenSSL. If you modify *
  21. * this file, you may extend this exception to your version of the file, *
  22. * but you are not obligated to do so. *
  23. * *
  24. * If you received these files with a written license agreement stating *
  25. * terms other than the (GPL) terms above, then that alternative license *
  26. * agreement takes precedence over this comment. *
  27. * *
  28. * Source is provided to this software because we believe users have a *
  29. * right to know exactly what a program is going to do before they run it. *
  30. * This also allows you to audit the software for security holes. *
  31. * *
  32. * Source code also allows you to port Nmap to new platforms, fix bugs, *
  33. * and add new features. You are highly encouraged to send your changes *
  34. * to the dev@nmap.org mailing list for possible incorporation into the *
  35. * main distribution. By sending these changes to Fyodor or one of the *
  36. * Insecure.Org development mailing lists, or checking them into the Nmap *
  37. * source code repository, it is understood (unless you specify otherwise) *
  38. * that you are offering the Nmap Project (Insecure.Com LLC) the *
  39. * unlimited, non-exclusive right to reuse, modify, and relicense the *
  40. * code. Nmap will always be available Open Source, but this is important *
  41. * because the inability to relicense code has caused devastating problems *
  42. * for other Free Software projects (such as KDE and NASM). We also *
  43. * occasionally relicense the code to third parties as discussed above. *
  44. * If you wish to specify special license conditions of your *
  45. * contributions, just say so when you send them. *
  46. * *
  47. * This program is distributed in the hope that it will be useful, but *
  48. * WITHOUT ANY WARRANTY; without even the implied warranty of *
  49. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
  50. * General Public License v2.0 for more details *
  51. * (http://www.gnu.org/licenses/gpl-2.0.html). *
  52. * *
  53. ***************************************************************************/
  54. /* $Id$ */
  55. #ifndef WIN32
  56. #include <sys/select.h>
  57. #endif
  58. #include <errno.h>
  59. #include "nsock_internal.h"
  60. #include "nsock_log.h"
  61. #if HAVE_PCAP
  62. #include "nsock_pcap.h"
  63. #endif
  64. /* --- ENGINE INTERFACE PROTOTYPES --- */
  65. static int select_init(struct npool *nsp);
  66. static void select_destroy(struct npool *nsp);
  67. static int select_iod_register(struct npool *nsp, struct niod *iod, int ev);
  68. static int select_iod_unregister(struct npool *nsp, struct niod *iod);
  69. static int select_iod_modify(struct npool *nsp, struct niod *iod, int ev_set, int ev_clr);
  70. static int select_loop(struct npool *nsp, int msec_timeout);
  71. /* ---- ENGINE DEFINITION ---- */
  72. struct io_engine engine_select = {
  73. "select",
  74. select_init,
  75. select_destroy,
  76. select_iod_register,
  77. select_iod_unregister,
  78. select_iod_modify,
  79. select_loop
  80. };
  81. /* --- INTERNAL PROTOTYPES --- */
  82. static void iterate_through_event_lists(struct npool *nsp);
  83. /* defined in nsock_core.c */
  84. void process_event(struct npool *nsp, gh_list_t *evlist, struct nevent *nse, int ev);
  85. void process_iod_events(struct npool *nsp, struct niod *nsi, int ev);
  86. void process_expired_events(struct npool *nsp);
  87. #if HAVE_PCAP
  88. #ifndef PCAP_CAN_DO_SELECT
  89. int pcap_read_on_nonselect(struct npool *nsp);
  90. #endif
  91. #endif
  92. /* defined in nsock_event.c */
  93. void update_first_events(struct nevent *nse);
  94. extern struct timeval nsock_tod;
  95. /*
  96. * Engine specific data structure
  97. */
  98. struct select_engine_info {
  99. /* Descriptors which have pending READ events */
  100. fd_set fds_master_r;
  101. /* Descriptors we are trying to WRITE to */
  102. fd_set fds_master_w;
  103. /* Looking for exceptional events -- used with connect */
  104. fd_set fds_master_x;
  105. /* For keeping track of the select results */
  106. fd_set fds_results_r, fds_results_w, fds_results_x;
  107. /* The highest sd we have set in any of our fd_set's (max_sd + 1 is used in
  108. * select() calls). Note that it can be -1, when there are no valid sockets */
  109. int max_sd;
  110. };
  111. int select_init(struct npool *nsp) {
  112. struct select_engine_info *sinfo;
  113. sinfo = (struct select_engine_info *)safe_malloc(sizeof(struct select_engine_info));
  114. FD_ZERO(&sinfo->fds_master_r);
  115. FD_ZERO(&sinfo->fds_master_w);
  116. FD_ZERO(&sinfo->fds_master_x);
  117. sinfo->max_sd = -1;
  118. nsp->engine_data = (void *)sinfo;
  119. return 1;
  120. }
  121. void select_destroy(struct npool *nsp) {
  122. assert(nsp->engine_data != NULL);
  123. free(nsp->engine_data);
  124. }
  125. int select_iod_register(struct npool *nsp, struct niod *iod, int ev) {
  126. assert(!IOD_PROPGET(iod, IOD_REGISTERED));
  127. iod->watched_events = ev;
  128. select_iod_modify(nsp, iod, ev, EV_NONE);
  129. IOD_PROPSET(iod, IOD_REGISTERED);
  130. return 1;
  131. }
  132. int select_iod_unregister(struct npool *nsp, struct niod *iod) {
  133. struct select_engine_info *sinfo = (struct select_engine_info *)nsp->engine_data;
  134. iod->watched_events = EV_NONE;
  135. /* some IODs can be unregistered here if they're associated to an event that was
  136. * immediately completed */
  137. if (IOD_PROPGET(iod, IOD_REGISTERED)) {
  138. #if HAVE_PCAP
  139. if (iod->pcap) {
  140. int sd = ((mspcap *)iod->pcap)->pcap_desc;
  141. if (sd >= 0) {
  142. checked_fd_clr(sd, &sinfo->fds_master_r);
  143. checked_fd_clr(sd, &sinfo->fds_results_r);
  144. }
  145. } else
  146. #endif
  147. {
  148. checked_fd_clr(iod->sd, &sinfo->fds_master_r);
  149. checked_fd_clr(iod->sd, &sinfo->fds_master_w);
  150. checked_fd_clr(iod->sd, &sinfo->fds_master_x);
  151. checked_fd_clr(iod->sd, &sinfo->fds_results_r);
  152. checked_fd_clr(iod->sd, &sinfo->fds_results_w);
  153. checked_fd_clr(iod->sd, &sinfo->fds_results_x);
  154. }
  155. if (sinfo->max_sd == iod->sd)
  156. sinfo->max_sd--;
  157. IOD_PROPCLR(iod, IOD_REGISTERED);
  158. }
  159. return 1;
  160. }
  161. int select_iod_modify(struct npool *nsp, struct niod *iod, int ev_set, int ev_clr) {
  162. int sd;
  163. struct select_engine_info *sinfo = (struct select_engine_info *)nsp->engine_data;
  164. assert((ev_set & ev_clr) == 0);
  165. iod->watched_events |= ev_set;
  166. iod->watched_events &= ~ev_clr;
  167. sd = nsock_iod_get_sd(iod);
  168. /* -- set events -- */
  169. if (ev_set & EV_READ)
  170. checked_fd_set(sd, &sinfo->fds_master_r);
  171. if (ev_set & EV_WRITE)
  172. checked_fd_set(sd, &sinfo->fds_master_w);
  173. if (ev_set & EV_EXCEPT)
  174. checked_fd_set(sd, &sinfo->fds_master_x);
  175. /* -- clear events -- */
  176. if (ev_clr & EV_READ)
  177. checked_fd_clr(sd, &sinfo->fds_master_r);
  178. if (ev_clr & EV_WRITE)
  179. checked_fd_clr(sd, &sinfo->fds_master_w);
  180. if (ev_clr & EV_EXCEPT)
  181. checked_fd_clr(sd, &sinfo->fds_master_x);
  182. /* -- update max_sd -- */
  183. if (ev_set != EV_NONE)
  184. sinfo->max_sd = MAX(sinfo->max_sd,sd);
  185. else if (ev_clr != EV_NONE && iod->events_pending == 1 && (sinfo->max_sd == sd))
  186. sinfo->max_sd--;
  187. return 1;
  188. }
  189. int select_loop(struct npool *nsp, int msec_timeout) {
  190. int results_left = 0;
  191. int event_msecs; /* msecs before an event goes off */
  192. int combined_msecs;
  193. int sock_err = 0;
  194. struct timeval select_tv;
  195. struct timeval *select_tv_p;
  196. struct select_engine_info *sinfo = (struct select_engine_info *)nsp->engine_data;
  197. assert(msec_timeout >= -1);
  198. if (nsp->events_pending == 0)
  199. return 0; /* No need to wait on 0 events ... */
  200. do {
  201. struct nevent *nse;
  202. nsock_log_debug_all("wait for events");
  203. nse = next_expirable_event(nsp);
  204. if (!nse)
  205. event_msecs = -1; /* None of the events specified a timeout */
  206. else
  207. event_msecs = MAX(0, TIMEVAL_MSEC_SUBTRACT(nse->timeout, nsock_tod));
  208. #if HAVE_PCAP
  209. #ifndef PCAP_CAN_DO_SELECT
  210. /* Force a low timeout when capturing packets on systems where
  211. * the pcap descriptor is not select()able. */
  212. if (gh_list_count(&nsp->pcap_read_events))
  213. if (event_msecs > PCAP_POLL_INTERVAL)
  214. event_msecs = PCAP_POLL_INTERVAL;
  215. #endif
  216. #endif
  217. /* We cast to unsigned because we want -1 to be very high (since it means no
  218. * timeout) */
  219. combined_msecs = MIN((unsigned)event_msecs, (unsigned)msec_timeout);
  220. /* Set up the timeval pointer we will give to select() */
  221. memset(&select_tv, 0, sizeof(select_tv));
  222. if (combined_msecs > 0) {
  223. select_tv.tv_sec = combined_msecs / 1000;
  224. select_tv.tv_usec = (combined_msecs % 1000) * 1000;
  225. select_tv_p = &select_tv;
  226. } else if (combined_msecs == 0) {
  227. /* we want the tv_sec and tv_usec to be zero but they already are from bzero */
  228. select_tv_p = &select_tv;
  229. } else {
  230. assert(combined_msecs == -1);
  231. select_tv_p = NULL;
  232. }
  233. #if HAVE_PCAP
  234. #ifndef PCAP_CAN_DO_SELECT
  235. /* do non-blocking read on pcap devices that doesn't support select()
  236. * If there is anything read, just leave this loop. */
  237. if (pcap_read_on_nonselect(nsp)) {
  238. /* okay, something was read. */
  239. } else
  240. #endif
  241. #endif
  242. {
  243. /* Set up the descriptors for select */
  244. sinfo->fds_results_r = sinfo->fds_master_r;
  245. sinfo->fds_results_w = sinfo->fds_master_w;
  246. sinfo->fds_results_x = sinfo->fds_master_x;
  247. results_left = fselect(sinfo->max_sd + 1, &sinfo->fds_results_r,
  248. &sinfo->fds_results_w, &sinfo->fds_results_x, select_tv_p);
  249. if (results_left == -1)
  250. sock_err = socket_errno();
  251. }
  252. gettimeofday(&nsock_tod, NULL); /* Due to select delay */
  253. } while (results_left == -1 && sock_err == EINTR); /* repeat only if signal occurred */
  254. if (results_left == -1 && sock_err != EINTR) {
  255. nsock_log_error("nsock_loop error %d: %s", sock_err, socket_strerror(sock_err));
  256. nsp->errnum = sock_err;
  257. return -1;
  258. }
  259. iterate_through_event_lists(nsp);
  260. return 1;
  261. }
  262. /* ---- INTERNAL FUNCTIONS ---- */
  263. static inline int get_evmask(const struct npool *nsp, const struct niod *nsi) {
  264. struct select_engine_info *sinfo = (struct select_engine_info *)nsp->engine_data;
  265. int sd, evmask;
  266. evmask = EV_NONE;
  267. #if HAVE_PCAP
  268. #ifndef PCAP_CAN_DO_SELECT
  269. if (nsi->pcap) {
  270. /* Always assume readable for a non-blocking read. We can't check checked_fd_isset
  271. because we don't have a pcap_desc. */
  272. evmask |= EV_READ;
  273. return evmask;
  274. }
  275. #endif
  276. #endif
  277. #if HAVE_PCAP
  278. if (nsi->pcap)
  279. sd = ((mspcap *)nsi->pcap)->pcap_desc;
  280. else
  281. #endif
  282. sd = nsi->sd;
  283. assert(sd >= 0);
  284. if (checked_fd_isset(sd, &sinfo->fds_results_r))
  285. evmask |= EV_READ;
  286. if (checked_fd_isset(sd, &sinfo->fds_results_w))
  287. evmask |= EV_WRITE;
  288. if (checked_fd_isset(sd, &sinfo->fds_results_x))
  289. evmask |= EV_EXCEPT;
  290. return evmask;
  291. }
  292. /* Iterate through all the event lists (such as connect_events, read_events,
  293. * timer_events, etc) and take action for those that have completed (due to
  294. * timeout, i/o, etc) */
  295. void iterate_through_event_lists(struct npool *nsp) {
  296. gh_lnode_t *current, *next, *last;
  297. last = gh_list_last_elem(&nsp->active_iods);
  298. for (current = gh_list_first_elem(&nsp->active_iods);
  299. current != NULL && gh_lnode_prev(current) != last;
  300. current = next) {
  301. struct niod *nsi = container_of(current, struct niod, nodeq);
  302. if (nsi->state != NSIOD_STATE_DELETED && nsi->events_pending)
  303. process_iod_events(nsp, nsi, get_evmask(nsp, nsi));
  304. next = gh_lnode_next(current);
  305. if (nsi->state == NSIOD_STATE_DELETED) {
  306. gh_list_remove(&nsp->active_iods, current);
  307. gh_list_prepend(&nsp->free_iods, current);
  308. }
  309. }
  310. /* iterate through timers and expired events */
  311. process_expired_events(nsp);
  312. }