"Fossies" - the Fresh Open Source Software Archive

Member "nmap-7.91/nsock/src/nsock_engines.c" (9 Oct 2020, 7505 Bytes) of package /linux/misc/nmap-7.91.tgz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "nsock_engines.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 7.90_vs_7.91.

    1 /***************************************************************************
    2  * nsock_engines.c -- This contains the functions and definitions to       *
    3  * manage the list of available IO engines.  Each IO engine leverages a    *
    4  * specific IO notification function to wait for events.  Nsock will try   *
    5  * to use the most efficient engine for your system.                       *
    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_engines.c 38094 2020-10-09 22:10:17Z dmiller $ */
   59 
   60 #ifdef HAVE_CONFIG_H
   61 #include "nsock_config.h"
   62 #endif
   63 
   64 #include "nsock_internal.h"
   65 
   66 #if HAVE_IOCP
   67   extern struct io_engine engine_iocp;
   68   #define ENGINE_IOCP &engine_iocp,
   69 #else
   70   #define ENGINE_IOCP
   71 #endif /* HAVE_IOCP */
   72 
   73 #if HAVE_EPOLL
   74   extern struct io_engine engine_epoll;
   75   #define ENGINE_EPOLL &engine_epoll,
   76 #else
   77   #define ENGINE_EPOLL
   78 #endif /* HAVE_EPOLL */
   79 
   80 #if HAVE_KQUEUE
   81   extern struct io_engine engine_kqueue;
   82   #define ENGINE_KQUEUE &engine_kqueue,
   83 #else
   84   #define ENGINE_KQUEUE
   85 #endif /* HAVE_KQUEUE */
   86 
   87 #if HAVE_POLL
   88   extern struct io_engine engine_poll;
   89   #define ENGINE_POLL &engine_poll,
   90 #else
   91   #define ENGINE_POLL
   92 #endif /* HAVE_POLL */
   93 
   94 /* select() based engine is the fallback engine, we assume it's always available */
   95 extern struct io_engine engine_select;
   96 #define ENGINE_SELECT &engine_select,
   97 
   98 /* Available IO engines. This depends on which IO management interfaces are
   99  * available on your system. Engines must be sorted by order of preference */
  100 static struct io_engine *available_engines[] = {
  101   ENGINE_EPOLL
  102   ENGINE_KQUEUE
  103   ENGINE_POLL
  104   ENGINE_IOCP
  105   ENGINE_SELECT
  106   NULL
  107 };
  108 
  109 static char *engine_hint;
  110 
  111 int posix_iod_connect(struct npool *nsp, int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
  112   return connect(sockfd, addr, addrlen);
  113 }
  114 
  115 int posix_iod_read(struct npool *nsp, int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen) {
  116   return recvfrom(sockfd, (char *)buf, len, flags, src_addr, addrlen);
  117 }
  118 
  119 int posix_iod_write(struct npool *nsp, int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen) {
  120   struct sockaddr_storage *dest = (struct sockaddr_storage *)dest_addr;
  121   if (dest->ss_family == AF_UNSPEC)
  122     return send(sockfd, (char *)buf, len, flags);
  123   else
  124     return sendto(sockfd, (char *)buf, len, flags, dest_addr, addrlen);
  125 }
  126 
  127 struct io_operations posix_io_operations = {
  128   posix_iod_connect,
  129   posix_iod_read,
  130   posix_iod_write
  131 };
  132 
  133 struct io_engine *get_io_engine(void) {
  134   struct io_engine *engine = NULL;
  135   int i;
  136 
  137   if (!engine_hint) {
  138     engine = available_engines[0];
  139   } else {
  140     for (i = 0; available_engines[i] != NULL; i++)
  141       if (strcmp(engine_hint, available_engines[i]->name) == 0) {
  142         engine = available_engines[i];
  143         break;
  144       }
  145   }
  146 
  147   if (!engine)
  148     fatal("No suitable IO engine found! (%s)\n",
  149           engine_hint ? engine_hint : "no hint");
  150 
  151   return engine;
  152 }
  153 
  154 int nsock_set_default_engine(char *engine) {
  155   if (engine_hint)
  156     free(engine_hint);
  157 
  158   if (engine) {
  159     int i;
  160 
  161     for (i = 0; available_engines[i] != NULL; i++) {
  162       if (strcmp(engine, available_engines[i]->name) == 0) {
  163         engine_hint = strdup(engine);
  164         return 0;
  165       }
  166     }
  167     return -1;
  168   }
  169   /* having engine = NULL is fine. This is actually the
  170    * way to tell nsock to use the default engine again. */
  171   engine_hint = NULL;
  172   return 0;
  173 }
  174 
  175 const char *nsock_list_engines(void) {
  176   return
  177 #if HAVE_IOCP
  178   "iocp "
  179 #endif
  180 #if HAVE_EPOLL
  181   "epoll "
  182 #endif
  183 #if HAVE_KQUEUE
  184   "kqueue "
  185 #endif
  186 #if HAVE_POLL
  187   "poll "
  188 #endif
  189   "select";
  190 }
  191