PageRenderTime 58ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/nsock/include/nsock.h

https://gitlab.com/g10h4ck/nmap-gsoc2015
C Header | 680 lines | 188 code | 103 blank | 389 comment | 0 complexity | e3b32276bebc92389fe836cb6c8023d6 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, Apache-2.0, LGPL-2.0, LGPL-2.1, MIT
  1. /***************************************************************************
  2. * nsock.h -- public interface definitions for the nsock parallel socket *
  3. * event library *
  4. * *
  5. ***********************IMPORTANT NSOCK LICENSE TERMS***********************
  6. * *
  7. * The nsock parallel socket event library is (C) 1999-2015 Insecure.Com *
  8. * LLC This library is free software; you may redistribute and/or *
  9. * modify it under the terms of the GNU General Public License as *
  10. * published by the Free Software Foundation; Version 2. This guarantees *
  11. * your right to use, modify, and redistribute this software under certain *
  12. * conditions. If this license is unacceptable to you, Insecure.Com LLC *
  13. * may be willing to sell alternative licenses (contact *
  14. * sales@insecure.com ). *
  15. * *
  16. * As a special exception to the GPL terms, Insecure.Com LLC grants *
  17. * permission to link the code of this program with any version of the *
  18. * OpenSSL library which is distributed under a license identical to that *
  19. * listed in the included docs/licenses/OpenSSL.txt file, and distribute *
  20. * linked combinations including the two. You must obey the GNU GPL in all *
  21. * respects for all of the code used other than OpenSSL. If you modify *
  22. * this file, you may extend this exception to your version of the file, *
  23. * but you are not obligated to do so. *
  24. * *
  25. * If you received these files with a written license agreement stating *
  26. * terms other than the (GPL) terms above, then that alternative license *
  27. * agreement takes precedence over this comment. *
  28. * *
  29. * Source is provided to this software because we believe users have a *
  30. * right to know exactly what a program is going to do before they run it. *
  31. * This also allows you to audit the software for security holes. *
  32. * *
  33. * Source code also allows you to port Nmap to new platforms, fix bugs, *
  34. * and add new features. You are highly encouraged to send your changes *
  35. * to the dev@nmap.org mailing list for possible incorporation into the *
  36. * main distribution. By sending these changes to Fyodor or one of the *
  37. * Insecure.Org development mailing lists, or checking them into the Nmap *
  38. * source code repository, it is understood (unless you specify otherwise) *
  39. * that you are offering the Nmap Project (Insecure.Com LLC) the *
  40. * unlimited, non-exclusive right to reuse, modify, and relicense the *
  41. * code. Nmap will always be available Open Source, but this is important *
  42. * because the inability to relicense code has caused devastating problems *
  43. * for other Free Software projects (such as KDE and NASM). We also *
  44. * occasionally relicense the code to third parties as discussed above. *
  45. * If you wish to specify special license conditions of your *
  46. * contributions, just say so when you send them. *
  47. * *
  48. * This program is distributed in the hope that it will be useful, but *
  49. * WITHOUT ANY WARRANTY; without even the implied warranty of *
  50. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
  51. * General Public License v2.0 for more details *
  52. * (http://www.gnu.org/licenses/gpl-2.0.html). *
  53. * *
  54. ***************************************************************************/
  55. /* $Id$ */
  56. #ifndef NSOCK_H
  57. #define NSOCK_H
  58. /* Keep assert() defined for security reasons */
  59. #undef NDEBUG
  60. #ifndef WIN32
  61. #include "nsock_config.h"
  62. #else
  63. #include "nsock_winconfig.h"
  64. #endif
  65. #include <stdio.h>
  66. #include <sys/types.h>
  67. #ifndef WIN32
  68. #include <sys/socket.h>
  69. #include <netinet/in.h>
  70. #include <arpa/inet.h>
  71. #include <sys/time.h>
  72. #else
  73. #include <winsock2.h> /* for struct timeval... */
  74. #endif
  75. #if HAVE_SYS_UN_H
  76. #include <sys/un.h>
  77. #ifndef SUN_LEN
  78. #include <string.h>
  79. #define SUN_LEN(ptr) ((sizeof(*(ptr)) - sizeof((ptr)->sun_path)) \
  80. + strlen((ptr)->sun_path))
  81. #endif
  82. #endif /* HAVE_SYS_UN_H */
  83. #ifdef __cplusplus
  84. extern "C" {
  85. #endif
  86. /* The read calls will generally return after reading at least this
  87. * much data so that the caller can process it and so that the
  88. * connection spewing data doesn't monopolize resources. The caller
  89. * can always initiate another read request to ask for more. */
  90. #define NSOCK_READ_CHUNK_SIZE 0x8FFFF
  91. struct npool;
  92. struct niod;
  93. struct nevent;
  94. struct proxy_chain;
  95. /* ------------------- TYPEDEFS ------------------- */
  96. /* nsock_pool, nsock_iod, and nsock_event are opaque objects that should
  97. * only be accessed using the appropriate accessor functions (described below). */
  98. /* An nsock_pool aggregates and manages events and i/o descriptors */
  99. typedef struct npool *nsock_pool;
  100. /* nsock_iod is an I/O descriptor -- you create it and then use it to
  101. * make calls to do connect()s, read()s, write()s, etc. A single IOD can handle
  102. * multiple event calls, but only one at a time. Also the event calls must be in
  103. * a "reasonable" order. For example, you might start with nsock_connect_tcp()
  104. * followed by a bunch of nsock_read* and nsock_write* calls. Then you either
  105. * destroy the iod for good with nsock_iod_delete() and allocate a new one via
  106. * nsock_iod_new for your next connection. */
  107. typedef struct niod *nsock_iod;
  108. /* An event is created when you do various calls (for reading, writing,
  109. * connecting, timers, etc) and is provided back to you in the callback when the
  110. * call completes/fails. It is automatically destroyed after the callback */
  111. typedef struct nevent *nsock_event;
  112. /* Provided by calls which (internally) create an nsock_event. This allows you
  113. * to cancel the event */
  114. typedef unsigned long nsock_event_id;
  115. /* This is used to save SSL sessionids between SSL connections */
  116. typedef void *nsock_ssl_session;
  117. typedef void *nsock_ssl_ctx;
  118. typedef void *nsock_ssl;
  119. typedef struct proxy_chain *nsock_proxychain;
  120. /* Logging-related data structures */
  121. typedef enum {
  122. /* --
  123. * Actual message priority values */
  124. NSOCK_LOG_DBG_ALL,
  125. NSOCK_LOG_DBG,
  126. NSOCK_LOG_INFO,
  127. NSOCK_LOG_ERROR,
  128. /* --
  129. * No messages are issued by nsock with this value.
  130. * Users can therefore set loglevel to NSOCK_LOG_NONE
  131. * to disable logging */
  132. NSOCK_LOG_NONE
  133. } nsock_loglevel_t;
  134. struct nsock_log_rec {
  135. /* Message emission time */
  136. struct timeval time;
  137. /* Message log level */
  138. nsock_loglevel_t level;
  139. /* Source file */
  140. const char *file;
  141. /* Statement line in nsock source */
  142. int line;
  143. /* Function that emitted the message */
  144. const char *func;
  145. /* Actual log message */
  146. char *msg;
  147. };
  148. /* Nsock logging function. This function receives all nsock log records whose
  149. * level is greater than or equal to nsp loglevel. The rec structure is
  150. * allocated and freed by nsock. */
  151. typedef void (*nsock_logger_t)(const struct nsock_log_rec *rec);
  152. /* ------------------- PROTOTYPES ------------------- */
  153. /* Here is the all important looping function that tells the event
  154. * engine to start up and begin processing events. It will continue until all
  155. * events have been delivered (including new ones started from event handlers),
  156. * or the msec_timeout is reached, or a major error has occurred. Use -1 if you
  157. * don't want to set a maximum time for it to run. A timeout of 0 will return
  158. * after 1 non-blocking loop. The nsock loop can be restarted again after it
  159. * returns. For example you could do a series of 15 second runs, allowing you
  160. * to do other stuff between them. Or you could just schedule a timer to call
  161. * you back every 15 seconds. */
  162. enum nsock_loopstatus {
  163. NSOCK_LOOP_NOEVENTS = 2,
  164. NSOCK_LOOP_TIMEOUT,
  165. NSOCK_LOOP_ERROR,
  166. NSOCK_LOOP_QUIT
  167. };
  168. enum nsock_loopstatus nsock_loop(nsock_pool nsp, int msec_timeout);
  169. /* Calling this function will cause nsock_loop to quit on its next iteration
  170. * with a return value of NSOCK_LOOP_QUIT. */
  171. void nsock_loop_quit(nsock_pool nsp);
  172. /* This next function returns the errno style error code -- which is only valid
  173. * if the status is NSOCK_LOOP_ERROR was returned by nsock_loop() */
  174. int nsock_pool_get_error(nsock_pool nsp);
  175. nsock_ssl nsock_iod_get_ssl(nsock_iod nsockiod);
  176. /* Note that nsock_iod_get_ssl_session will increment the usage count of the
  177. * SSL_SESSION if inc_ref is not zero, since nsock does a free when the IOD
  178. * is destroyed. It's up to any calling function/etc to do a SSL_SESSION_free()
  179. * on it. Passing in inc_ref=0 doesn't increment, and is for informational
  180. * purposes only. */
  181. nsock_ssl_session nsock_iod_get_ssl_session(nsock_iod nsockiod, int inc_ref);
  182. /* Sometimes it is useful to store a pointer to information inside the NSP so
  183. * you can retrieve it during a callback. */
  184. void nsock_pool_set_udata(nsock_pool nsp, void *data);
  185. /* And the function above wouldn't make much sense if we didn't have a way to
  186. * retrieve that data ... */
  187. void *nsock_pool_get_udata(nsock_pool nsp);
  188. /* Turns on or off broadcast support on new sockets. Default is off (0, false)
  189. * set in nsock_pool_new(). Any non-zero (true) value sets SO_BROADCAST on all
  190. * new sockets (value of optval will be used directly in the setsockopt() call). */
  191. void nsock_pool_set_broadcast(nsock_pool nsp, int optval);
  192. /* Sets the name of the interface for new sockets to bind to. */
  193. void nsock_pool_set_device(nsock_pool nsp, const char *device);
  194. /* Initializes an Nsock pool to create SSL connections. This sets an internal
  195. * SSL_CTX, which is like a template that sets options for all connections that
  196. * are made from it. Returns the SSL_CTX so you can set your own options.
  197. *
  198. * Use the NSOCK_SSL_MAX_SPEED to emphasize speed over security.
  199. * Insecure ciphers are used when they are faster and no certificate
  200. * verification is done.
  201. *
  202. * Returns the SSL_CTX so you can set your own options.
  203. * By default, do no server certificate verification. To enable it, do
  204. * something like:
  205. * SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL);
  206. *
  207. * on the SSL_CTX returned. If you do, it is then up to the application to
  208. * load trusted certificates with SSL_CTX_load_verify_locations or
  209. * SSL_CTX_set_default_verify_paths, or else every connection will fail. It
  210. * is also up to the application to do any further checks such as domain name
  211. * validation. */
  212. #define NSOCK_SSL_MAX_SPEED (1 << 0)
  213. nsock_ssl_ctx nsock_pool_ssl_init(nsock_pool ms_pool, int flags);
  214. /* Enforce use of a given IO engine.
  215. * The engine parameter is a zero-terminated string that will be
  216. * strup()'ed by the library. No validity check is performed by this function,
  217. * beware nsock_pool_new() will fatal() if an invalid/unavailable engine name was
  218. * supplied before.
  219. * Pass NULL to reset to default (use most efficient engine available).
  220. *
  221. * Function returns 0 on success and -1 on error. */
  222. int nsock_set_default_engine(char *engine);
  223. /* Get a comma-separated list of available engines. */
  224. const char *nsock_list_engines(void);
  225. /* And here is how you create an nsock_pool. This allocates, initializes, and
  226. * returns an nsock_pool event aggregator. In the case of error, NULL will be
  227. * returned. If you do not wish to immediately associate any userdata, pass in
  228. * NULL. */
  229. nsock_pool nsock_pool_new(void *udata);
  230. /* If nsock_pool_new returned success, you must free the nsp when you are done with it
  231. * to conserve memory (and in some cases, sockets). After this call, nsp may no
  232. * longer be used. Any pending events are sent an NSE_STATUS_KILL callback and
  233. * all outstanding iods are deleted. */
  234. void nsock_pool_delete(nsock_pool nsp);
  235. /* Logging subsystem: set custom logging function.
  236. * A NULL logger will reset the default (stderr) logger.
  237. * (See nsock_logger_t type definition). */
  238. void nsock_set_log_function(nsock_logger_t logger);
  239. nsock_loglevel_t nsock_get_loglevel(void);
  240. void nsock_set_loglevel(nsock_loglevel_t loglevel);
  241. /* Parse a proxy chain description string and build a nsock_proxychain object
  242. * accordingly. If the optional nsock_pool parameter is passed in, it gets
  243. * associated to the chain object. The alternative is to pass nsp=NULL and call
  244. * nsock_pool_set_proxychain() manually. Whatever is done, the chain object has
  245. * to be deleted by the caller, using proxychain_delete(). */
  246. int nsock_proxychain_new(const char *proxystr, nsock_proxychain *chain, nsock_pool nspool);
  247. /* If nsock_proxychain_new() returned success, caller has to free the chain
  248. * object using this function. */
  249. void nsock_proxychain_delete(nsock_proxychain chain);
  250. /* Assign a previously created proxychain object to a nsock pool. After this,
  251. * new connections requests will be issued through the chain of proxies (if
  252. * possible). */
  253. int nsock_pool_set_proxychain(nsock_pool nspool, nsock_proxychain chain);
  254. /* nsock_event handles a single event. Its ID is generally returned when the
  255. * event is created, and the event itself is included in callbacks
  256. *
  257. * ---------------------------------------------------------------------------
  258. * IF YOU ADD NEW NSE_TYPES YOU MUST INCREASE TYPE_CODE_NUM_BITS SO THAT IT IS
  259. * ALWAYS log2(maximum_nse_type_value + 1)
  260. * --------------------------------------------------------------------------- */
  261. #define TYPE_CODE_NUM_BITS 3
  262. enum nse_type {
  263. NSE_TYPE_CONNECT = 0,
  264. NSE_TYPE_CONNECT_SSL = 1,
  265. NSE_TYPE_READ = 2,
  266. NSE_TYPE_WRITE = 3,
  267. NSE_TYPE_TIMER = 4,
  268. NSE_TYPE_PCAP_READ = 5,
  269. NSE_TYPE_MAX = 6,
  270. }; /* At some point I was considering a NSE_TYPE_START and NSE_TYPE_CUSTOM */
  271. /* Find the type of an event that spawned a callback */
  272. enum nse_type nse_type(nsock_event nse);
  273. /* Takes an nse_type (as returned by nse_type()) and returns a static string name
  274. * that you can use for printing, etc. */
  275. const char *nse_type2str(enum nse_type type);
  276. /* Did the event succeed? What is the status? */
  277. enum nse_status {
  278. NSE_STATUS_NONE = 0, /* User should never see this */
  279. NSE_STATUS_SUCCESS, /* Everything went A-OK! */
  280. NSE_STATUS_ERROR, /* Uh-oh! Problem, check the nse_errorcode() */
  281. NSE_STATUS_TIMEOUT, /* The async call surpassed the timeout you specified */
  282. NSE_STATUS_CANCELLED, /* Someone cancelled the event. (by calling nsock_event_cancel()). */
  283. NSE_STATUS_KILL, /* The event has been killed, this generally means the
  284. nspool is being deleted -- you should free up any
  285. resources you have allocated and exit. Don't you
  286. dare make any more async nsock calls! */
  287. NSE_STATUS_EOF, /* We got EOF and NO DATA -- if we got data first,
  288. SUCCESS is reported (see nse_eof()). */
  289. NSE_STATUS_PROXYERROR
  290. };
  291. enum nse_status nse_status(nsock_event nse);
  292. /* Takes an nse_status (as returned by nse_status() and returns a static string
  293. * name that you can use for printing, etc. */
  294. const char *nse_status2str(enum nse_status status);
  295. /* This next function tells whether we received an EOF when we were reading. It
  296. * is generally a better way to check for EOF than looking at the status because
  297. * sometimes we read some data before getting the EOF, in which SUCCESS is
  298. * returned (although another read attempt would return a status of EOF).
  299. * nse_eof returns nonzero if we have reached EOF, zero if we have NOT reach
  300. * EOF. */
  301. int nse_eof(nsock_event nse);
  302. /* This next function returns the errno style error code -- which is only valid
  303. * if the status is NSE_STATUS_ERROR (this is a normal errno style error code). */
  304. int nse_errorcode(nsock_event nse);
  305. /* Every event has an ID which will be unique throughout the program's execution
  306. * (for a given nsock_pool) unless you blow through 500,000,000 of them */
  307. nsock_event_id nse_id(nsock_event nse);
  308. /* If you did a read request, and the result was STATUS_SUCCESS, this function
  309. * provides the buffer that was read in as well as the number of chars read.
  310. * The buffer should not be modified or free'd . It is not guaranteed to be
  311. * NUL-terminated and it may even contain nuls */
  312. char *nse_readbuf(nsock_event nse, int *nbytes);
  313. /* Obtains the nsock_iod (see below) associated with the event. Note that some
  314. * events (such as timers) don't have an nsock_iod associated with them */
  315. nsock_iod nse_iod(nsock_event nse);
  316. /* nsock_iod is like a "file descriptor" for the nsock library. You use it to
  317. * request events. And here is how you create an nsock_iod. nsock_iod_new
  318. * returns NULL if the iod cannot be allocated. Pass NULL as udata if you
  319. * don't want to immediately associate any user data with the IOD. */
  320. nsock_iod nsock_iod_new(nsock_pool nsockp, void *udata);
  321. /* This version allows you to associate an existing sd with the msi so that you
  322. * can read/write it using the nsock infrastructure. For example, you may want
  323. * to watch for data from STDIN_FILENO at the same time as you read/write
  324. * various sockets. STDIN_FILENO is a special case, however. Any other sd is
  325. * dup()ed, so you may close or otherwise manipulate your copy. The duped copy
  326. * will be destroyed when the IOD is destroyed */
  327. nsock_iod nsock_iod_new2(nsock_pool nsockp, int sd, void *udata);
  328. /* If nsock_iod_new returned success, you must free the iod when you are done
  329. * with it to conserve memory (and in some cases, sockets). After this call,
  330. * nsockiod may no longer be used -- you need to create a new one with
  331. * nsock_iod_new(). pending_response tells what to do with any events that are
  332. * pending on this nsock_iod. This can be NSOCK_PENDING_NOTIFY (send a KILL
  333. * notification to each event), NSOCK_PENDING_SILENT (do not send notification
  334. * to the killed events), or NSOCK_PENDING_ERROR (print an error message and
  335. * quit the program) */
  336. enum nsock_del_mode {
  337. NSOCK_PENDING_NOTIFY,
  338. NSOCK_PENDING_SILENT,
  339. NSOCK_PENDING_ERROR,
  340. };
  341. void nsock_iod_delete(nsock_iod iod, enum nsock_del_mode pending_response);
  342. /* Sometimes it is useful to store a pointer to information inside
  343. * the nsiod so you can retrieve it during a callback. */
  344. void nsock_iod_set_udata(nsock_iod iod, void *udata);
  345. /* And the function above wouldn't make much sense if we didn't have a way to
  346. * retrieve that data ... */
  347. void *nsock_iod_get_udata(nsock_iod iod);
  348. /* I didn't want to do this. Its an ugly hack, but I suspect it will be
  349. * necessary. I certainly can't reproduce in nsock EVERYTHING you might want
  350. * to do with a socket. So I'm offering you this function to obtain the socket
  351. * descriptor which is (usually) wrapped in a nsock_iod). You can do
  352. * "reasonable" things with it, like setting socket receive buffers. But don't
  353. * create havok by closing the descriptor! If the descriptor you get back is
  354. * -1, the iod does not currently possess a valid descriptor */
  355. int nsock_iod_get_sd(nsock_iod iod);
  356. /* Returns the ID of an nsock_iod . This ID is always unique amongst ids for a
  357. * given nspool (unless you blow through billions of them). */
  358. unsigned long nsock_iod_id(nsock_iod iod);
  359. /* Returns Packets received in bytes */
  360. unsigned long nsock_iod_get_read_count(nsock_iod iod);
  361. /* Returns Packets sent in bytes */
  362. unsigned long nsock_iod_get_write_count(nsock_iod iod);
  363. /* Returns 1 if an NSI is communicating via SSL, 0 otherwise */
  364. int nsock_iod_check_ssl(nsock_iod iod);
  365. /* Returns the remote peer port (or -1 if unavailable). Note the return value
  366. * is a whole int so that -1 can be distinguished from 65535. Port is returned
  367. * in host byte order. */
  368. int nsock_iod_get_peerport(nsock_iod iod);
  369. /* Sets the local address to bind to before connect() */
  370. int nsock_iod_set_localaddr(nsock_iod iod, struct sockaddr_storage *ss, size_t sslen);
  371. /* Sets IPv4 options to apply before connect(). It makes a copy of the options,
  372. * so you can free() yours if necessary. This copy is freed when the iod is
  373. * destroyed */
  374. int nsock_iod_set_ipoptions(nsock_iod iod, void *ipopts, size_t ipoptslen);
  375. /* Returns that host/port/protocol information for the last communication (or
  376. * comm. attempt) this nsi has been involved with. By "involved" with I mean
  377. * interactions like establishing (or trying to) a connection or sending a UDP
  378. * datagram through an unconnected nsock_iod. AF is the address family (AF_INET
  379. * or AF_INET6), Protocol is IPPROTO_TCP or IPPROTO_UDP. Pass NULL for
  380. * information you do not need. If ANY of the information you requested is not
  381. * available, 0 will be returned and the unavailable sockets are zeroed. If
  382. * protocol or af is requested but not available, it will be set to -1 (and 0
  383. * returned). The pointers you pass in must be NULL or point to allocated
  384. * address space. The sockaddr members should actually be sockaddr_storage,
  385. * sockaddr_in6, or sockaddr_in with the socklen of them set appropriately (eg
  386. * sizeof(sockaddr_storage) if that is what you are passing). */
  387. int nsock_iod_get_communication_info(nsock_iod iod, int *protocol, int *af,
  388. struct sockaddr *local,
  389. struct sockaddr *remote, size_t socklen);
  390. /* Set the hostname of the remote host, for when that matters. This is currently
  391. * only used for Server Name Indication in SSL connections. */
  392. int nsock_iod_set_hostname(nsock_iod iod, const char *hostname);
  393. /* EVENT CREATION FUNCTIONS
  394. * ---
  395. * These functions request asynchronous
  396. * notification of completion of an event. The handler will never be
  397. * synchronously called back during the event creation call (that causes too
  398. * many hard to debug errors and plus we don't want people to have to deal with
  399. * callbacks until they actually call nsock_loop). */
  400. /* These functions generally take a common 5 initial parameters:
  401. *
  402. * nsock_pool mst:
  403. * The is the nsock_pool describing the events you have scheduled, etc
  404. *
  405. * nsock_iod nsiod:
  406. * The I/O Descriptor that should be used in the request. Note that timer
  407. * events don't have this argument since they don't use an iod. You can
  408. * obtain it in the callback from the nsock_event.
  409. *
  410. * nsock_ev_handler handler:
  411. * This is the function you want the system to call when your event is
  412. * triggered (or times out, or hits an error, etc.). The function should be
  413. * of this form: void funcname(nsock_pool nsp, nsock_event nse, void *userdata)
  414. *
  415. * int timeout_msecs:
  416. * The timeout for the request in milliseconds. If the request hasn't
  417. * completed (or in a few cases started) within the timeout specified, the
  418. * handler will be called with a TIMEOUT status and the request will be
  419. * aborted.
  420. *
  421. * void *userdata:
  422. * The nsock_event that comes back can optionally have a pointer associated
  423. * with it. You can set that pointer here. If you don't want one, just
  424. * pass NULL.
  425. *
  426. * These functions return an nsock_event_id which can be used to cancel the
  427. * event if necessary.
  428. */
  429. typedef void (*nsock_ev_handler)(nsock_pool, nsock_event, void *);
  430. /* Initialize an unconnected UDP socket. */
  431. int nsock_setup_udp(nsock_pool nsp, nsock_iod ms_iod, int af);
  432. #if HAVE_SYS_UN_H
  433. /* Request a UNIX domain sockets connection to the same system (by path to socket).
  434. * This function connects to the socket of type SOCK_STREAM. ss should be a
  435. * sockaddr_storage, sockaddr_un as appropriate (just like what you would pass to
  436. * connect). sslen should be the sizeof the structure you are passing in. */
  437. nsock_event_id nsock_connect_unixsock_stream(nsock_pool nsp, nsock_iod nsiod, nsock_ev_handler handler,
  438. int timeout_msecs, void *userdata, struct sockaddr *ss,
  439. size_t sslen);
  440. /* Request a UNIX domain sockets connection to the same system (by path to socket).
  441. * This function connects to the socket of type SOCK_DGRAM. ss should be a
  442. * sockaddr_storage, sockaddr_un as appropriate (just like what you would pass to
  443. * connect). sslen should be the sizeof the structure you are passing in. */
  444. nsock_event_id nsock_connect_unixsock_datagram(nsock_pool nsp, nsock_iod nsiod, nsock_ev_handler handler,
  445. void *userdata, struct sockaddr *ss, size_t sslen);
  446. #endif /* HAVE_SYS_UN_H */
  447. /* Request a TCP connection to another system (by IP address). The in_addr is
  448. * normal network byte order, but the port number should be given in HOST BYTE
  449. * ORDER. ss should be a sockaddr_storage, sockaddr_in6, or sockaddr_in as
  450. * appropriate (just like what you would pass to connect). sslen should be the
  451. * sizeof the structure you are passing in. */
  452. nsock_event_id nsock_connect_tcp(nsock_pool nsp, nsock_iod nsiod, nsock_ev_handler handler, int timeout_msecs,
  453. void *userdata, struct sockaddr *ss, size_t sslen, unsigned short port);
  454. nsock_event_id nsock_connect_tcp_direct(nsock_pool nsp, nsock_iod nsiod, nsock_ev_handler handler,
  455. int timeout_msecs, void *userdata, struct sockaddr *ss,
  456. size_t sslen, unsigned short port);
  457. /* Request an SCTP association to another system (by IP address). The in_addr is
  458. * normal network byte order, but the port number should be given in HOST BYTE
  459. * ORDER. ss should be a sockaddr_storage, sockaddr_in6, or sockaddr_in as
  460. * appropriate (just like what you would pass to connect). sslen should be the
  461. * sizeof the structure you are passing in. */
  462. nsock_event_id nsock_connect_sctp(nsock_pool nsp, nsock_iod nsiod, nsock_ev_handler handler, int timeout_msecs,
  463. void *userdata, struct sockaddr *ss, size_t sslen, unsigned short port);
  464. /* Request a UDP "connection" to another system (by IP address). The in_addr is
  465. * normal network byte order, but the port number should be given in HOST BYTE
  466. * ORDER. Since this is UDP, no packets are actually sent. The destination IP
  467. * and port are just associated with the nsiod (an actual OS connect() call is
  468. * made). You can then use the normal nsock write calls on the socket. There
  469. * is no timeout since this call always calls your callback at the next
  470. * opportunity. The advantages to having a connected UDP socket (as opposed to
  471. * just specifying an address with sendto()) are that we can now use a consistent
  472. * set of write/read calls for TCP/UDP, received packets from the non-partner
  473. * are automatically dropped by the OS, and the OS can provide asynchronous
  474. * errors (see Unix Network Programming pp224). ss should be a
  475. * sockaddr_storage, sockaddr_in6, or sockaddr_in as appropriate (just like what
  476. * you would pass to connect). sslen should be the sizeof the structure you are
  477. * passing in. */
  478. nsock_event_id nsock_connect_udp(nsock_pool nsp, nsock_iod nsiod, nsock_ev_handler handler, void *userdata,
  479. struct sockaddr *ss, size_t sslen, unsigned short port);
  480. /* Request an SSL over TCP/SCTP connection to another system (by IP address).
  481. * The in_addr is normal network byte order, but the port number should be given
  482. * in HOST BYTE ORDER. This function will call back only after it has made the
  483. * connection AND done the initial SSL negotiation. From that point on, you use
  484. * the normal read/write calls and decryption will happen transparently. ss
  485. * should be a sockaddr_storage, sockaddr_in6, or sockaddr_in as appropriate
  486. * (just like what you would pass to connect). sslen should be the sizeof the
  487. * structure you are passing in. */
  488. nsock_event_id nsock_connect_ssl(nsock_pool nsp, nsock_iod nsiod, nsock_ev_handler handler, int timeout_msecs,
  489. void *userdata, struct sockaddr *ss, size_t sslen, int proto, unsigned short port, nsock_ssl_session ssl_session);
  490. /* Request ssl connection over already established TCP/SCTP connection. nsiod
  491. * must be socket that is already connected to target using nsock_connect_tcp or
  492. * nsock_connect_sctp. All parameters have the same meaning as in
  493. * 'nsock_connect_ssl' */
  494. nsock_event_id nsock_reconnect_ssl(nsock_pool nsp, nsock_iod nsiod,
  495. nsock_ev_handler handler, int timeout_msecs, void *userdata, nsock_ssl_session ssl_session);
  496. /* Read up to nlines lines (terminated with \n, which of course inclues \r\n),
  497. * or until EOF, or until the timeout, whichever comes first. Note that
  498. * NSE_STATUS_SUCCESS will be returned in the case of EOF or timeout if at least
  499. * 1 char has been read. Also note that you may get more than 'nlines' back --
  500. * we just stop once "at least" 'nlines' is read */
  501. nsock_event_id nsock_readlines(nsock_pool nsp, nsock_iod nsiod,
  502. nsock_ev_handler handler, int timeout_msecs, void *userdata, int nlines);
  503. /* Same as above, except it tries to read at least 'nbytes' instead of 'nlines'. */
  504. nsock_event_id nsock_readbytes(nsock_pool nsp, nsock_iod nsiod,
  505. nsock_ev_handler handler, int timeout_msecs, void *userdata, int nbytes);
  506. /* The simplest read function -- returns NSE_STATUS_SUCCESS when it reads
  507. * anything, otherwise it returns timeout, eof, or error as appropriate */
  508. nsock_event_id nsock_read(nsock_pool nsp, nsock_iod nsiod, nsock_ev_handler handler, int timeout_msecs, void *userdata);
  509. /* Write some data to the socket. If the write is not COMPLETED within
  510. * timeout_msecs , NSE_STATUS_TIMEOUT will be returned. If you are supplying
  511. * NUL-terminated data, you can optionally pass -1 for datalen and nsock_write
  512. * will figure out the length itself */
  513. nsock_event_id nsock_write(nsock_pool nsp, nsock_iod nsiod,
  514. nsock_ev_handler handler, int timeout_msecs, void *userdata, const char *data, int datalen);
  515. nsock_event_id nsock_sendto(nsock_pool ms_pool, nsock_iod ms_iod, nsock_ev_handler handler, int timeout_msecs,
  516. void *userdata, struct sockaddr *saddr, size_t sslen, unsigned short port, const char *data, int datalen);
  517. /* Same as nsock_write except you can use a printf-style format and you can only
  518. * use this for ASCII strings */
  519. nsock_event_id nsock_printf(nsock_pool nsp, nsock_iod nsiod,
  520. nsock_ev_handler handler, int timeout_msecs, void *userdata, char *format, ... );
  521. /* Send back an NSE_TYPE_TIMER after the number of milliseconds specified. Of
  522. * course it can also return due to error, cancellation, etc. */
  523. nsock_event_id nsock_timer_create(nsock_pool nsp, nsock_ev_handler handler, int timeout_msecs, void *userdata);
  524. /* Cancel an event (such as a timer or read request). If notify is nonzero, the
  525. * requester will be sent an event CANCELLED status back to the given handler.
  526. * But in some cases there is no need to do this (like if the function deleting
  527. * it is the one which created it), in which case 0 can be passed to skip the
  528. * step. This function returns zero if the event is not found, nonzero
  529. * otherwise */
  530. int nsock_event_cancel(nsock_pool ms_pool, nsock_event_id id, int notify );
  531. /* Grab the latest time as recorded by the nsock library, which does so at least
  532. * once per event loop (in main_loop). Not only does this function (generally)
  533. * avoid a system call, but in many circumstances it is better to use nsock's
  534. * time rather than the system time. If nsock has never obtained the time when
  535. * you call it, it will do so before returning */
  536. const struct timeval *nsock_gettimeofday();
  537. #ifdef HAVE_PCAP
  538. /* Open pcap device and connect it to nsp. Other parameters have the
  539. * same meaning as for pcap_open_live in pcap(3).
  540. *
  541. * device: pcap-style device name
  542. * snaplen: size of packet to be copied to handler
  543. * promisc: whether to open device in promiscuous mode
  544. * bpf_fmt: berkeley filter
  545. *
  546. * return value: 0 if everything was okay, or error code if error occurred.
  547. * */
  548. int nsock_pcap_open(nsock_pool nsp, nsock_iod nsiod, const char *pcap_device,
  549. int snaplen, int promisc, const char *bpf_fmt, ...);
  550. /* Requests exactly one packet to be captured.from pcap.
  551. * See nsock_read() for parameters description. */
  552. nsock_event_id nsock_pcap_read_packet(nsock_pool nsp, nsock_iod nsiod,
  553. nsock_ev_handler handler,
  554. int timeout_msecs, void *userdata);
  555. /* Gets packet data. This should be called after successful receipt of packet
  556. * to get packet. If you're not interested in some values, just pass NULL
  557. * instead of valid pointer.
  558. * l3_data is just after l2_data in buffer. Feel free to treat l2_data as one
  559. * buffer with size of (l2_len + l3_len).
  560. * Ts time is fixed for systems that don't support proper timing, like Windows.
  561. * So TS is pointing to time when packet was received or to the time _after_.
  562. * As a result you'll get longer times than you should, but it's safer to
  563. * think that host is a bit further.
  564. * */
  565. void nse_readpcap(nsock_event nsee, const unsigned char **l2_data,
  566. size_t *l2_len, const unsigned char **l3_data, size_t *l3_len,
  567. size_t *packet_len, struct timeval *ts);
  568. /* Well. Just pcap-style datalink.
  569. * Like DLT_EN10MB or DLT_SLIP. Check in pcap(3) manpage. */
  570. int nsock_iod_linktype(nsock_iod iod);
  571. /* Is this nsiod a pcap descriptor? */
  572. int nsock_iod_is_pcap(nsock_iod iod);
  573. #endif /* HAVE_PCAP */
  574. #ifdef __cplusplus
  575. } /* End of 'extern "C"' */
  576. #endif
  577. #endif /* NSOCK_H */