nn.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*
  2. Copyright (c) 2012-2014 Martin Sustrik All rights reserved.
  3. Copyright (c) 2013 GoPivotal, Inc. All rights reserved.
  4. Copyright 2016 Garrett D'Amore <garrett@damore.org>
  5. Copyright (c) 2015-2016 Jack R. Dunaway. All rights reserved.
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"),
  8. to deal in the Software without restriction, including without limitation
  9. the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. and/or sell copies of the Software, and to permit persons to whom
  11. the Software is furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included
  13. in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. IN THE SOFTWARE.
  21. */
  22. #ifndef NN_H_INCLUDED
  23. #define NN_H_INCLUDED
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. #include <errno.h>
  28. #include <stddef.h>
  29. #include <stdint.h>
  30. /* Handle DSO symbol visibility. */
  31. #if !defined(NN_EXPORT)
  32. # if defined(_WIN32) && !defined(NN_STATIC_LIB)
  33. # if defined NN_SHARED_LIB
  34. # define NN_EXPORT __declspec(dllexport)
  35. # else
  36. # define NN_EXPORT __declspec(dllimport)
  37. # endif
  38. # else
  39. # define NN_EXPORT extern
  40. # endif
  41. #endif
  42. /******************************************************************************/
  43. /* ABI versioning support. */
  44. /******************************************************************************/
  45. /* Don't change this unless you know exactly what you're doing and have */
  46. /* read and understand the following documents: */
  47. /* www.gnu.org/software/libtool/manual/html_node/Libtool-versioning.html */
  48. /* www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html */
  49. /* The current interface version. */
  50. #define NN_VERSION_CURRENT 5
  51. /* The latest revision of the current interface. */
  52. #define NN_VERSION_REVISION 0
  53. /* How many past interface versions are still supported. */
  54. #define NN_VERSION_AGE 0
  55. /******************************************************************************/
  56. /* Errors. */
  57. /******************************************************************************/
  58. /* A number random enough not to collide with different errno ranges on */
  59. /* different OSes. The assumption is that error_t is at least 32-bit type. */
  60. #define NN_HAUSNUMERO 156384712
  61. /* On some platforms some standard POSIX errnos are not defined. */
  62. #ifndef ENOTSUP
  63. #define ENOTSUP (NN_HAUSNUMERO + 1)
  64. #endif
  65. #ifndef EPROTONOSUPPORT
  66. #define EPROTONOSUPPORT (NN_HAUSNUMERO + 2)
  67. #endif
  68. #ifndef ENOBUFS
  69. #define ENOBUFS (NN_HAUSNUMERO + 3)
  70. #endif
  71. #ifndef ENETDOWN
  72. #define ENETDOWN (NN_HAUSNUMERO + 4)
  73. #endif
  74. #ifndef EADDRINUSE
  75. #define EADDRINUSE (NN_HAUSNUMERO + 5)
  76. #endif
  77. #ifndef EADDRNOTAVAIL
  78. #define EADDRNOTAVAIL (NN_HAUSNUMERO + 6)
  79. #endif
  80. #ifndef ECONNREFUSED
  81. #define ECONNREFUSED (NN_HAUSNUMERO + 7)
  82. #endif
  83. #ifndef EINPROGRESS
  84. #define EINPROGRESS (NN_HAUSNUMERO + 8)
  85. #endif
  86. #ifndef ENOTSOCK
  87. #define ENOTSOCK (NN_HAUSNUMERO + 9)
  88. #endif
  89. #ifndef EAFNOSUPPORT
  90. #define EAFNOSUPPORT (NN_HAUSNUMERO + 10)
  91. #endif
  92. #ifndef EPROTO
  93. #define EPROTO (NN_HAUSNUMERO + 11)
  94. #endif
  95. #ifndef EAGAIN
  96. #define EAGAIN (NN_HAUSNUMERO + 12)
  97. #endif
  98. #ifndef EBADF
  99. #define EBADF (NN_HAUSNUMERO + 13)
  100. #endif
  101. #ifndef EINVAL
  102. #define EINVAL (NN_HAUSNUMERO + 14)
  103. #endif
  104. #ifndef EMFILE
  105. #define EMFILE (NN_HAUSNUMERO + 15)
  106. #endif
  107. #ifndef EFAULT
  108. #define EFAULT (NN_HAUSNUMERO + 16)
  109. #endif
  110. #ifndef EACCES
  111. #define EACCES (NN_HAUSNUMERO + 17)
  112. #endif
  113. #ifndef EACCESS
  114. #define EACCESS (EACCES)
  115. #endif
  116. #ifndef ENETRESET
  117. #define ENETRESET (NN_HAUSNUMERO + 18)
  118. #endif
  119. #ifndef ENETUNREACH
  120. #define ENETUNREACH (NN_HAUSNUMERO + 19)
  121. #endif
  122. #ifndef EHOSTUNREACH
  123. #define EHOSTUNREACH (NN_HAUSNUMERO + 20)
  124. #endif
  125. #ifndef ENOTCONN
  126. #define ENOTCONN (NN_HAUSNUMERO + 21)
  127. #endif
  128. #ifndef EMSGSIZE
  129. #define EMSGSIZE (NN_HAUSNUMERO + 22)
  130. #endif
  131. #ifndef ETIMEDOUT
  132. #define ETIMEDOUT (NN_HAUSNUMERO + 23)
  133. #endif
  134. #ifndef ECONNABORTED
  135. #define ECONNABORTED (NN_HAUSNUMERO + 24)
  136. #endif
  137. #ifndef ECONNRESET
  138. #define ECONNRESET (NN_HAUSNUMERO + 25)
  139. #endif
  140. #ifndef ENOPROTOOPT
  141. #define ENOPROTOOPT (NN_HAUSNUMERO + 26)
  142. #endif
  143. #ifndef EISCONN
  144. #define EISCONN (NN_HAUSNUMERO + 27)
  145. #define NN_EISCONN_DEFINED
  146. #endif
  147. #ifndef ESOCKTNOSUPPORT
  148. #define ESOCKTNOSUPPORT (NN_HAUSNUMERO + 28)
  149. #endif
  150. /* Native nanomsg error codes. */
  151. #ifndef ETERM
  152. #define ETERM (NN_HAUSNUMERO + 53)
  153. #endif
  154. #ifndef EFSM
  155. #define EFSM (NN_HAUSNUMERO + 54)
  156. #endif
  157. /* This function retrieves the errno as it is known to the library. */
  158. /* The goal of this function is to make the code 100% portable, including */
  159. /* where the library is compiled with certain CRT library (on Windows) and */
  160. /* linked to an application that uses different CRT library. */
  161. NN_EXPORT int nn_errno (void);
  162. /* Resolves system errors and native errors to human-readable string. */
  163. NN_EXPORT const char *nn_strerror (int errnum);
  164. /* Returns the symbol name (e.g. "NN_REQ") and value at a specified index. */
  165. /* If the index is out-of-range, returns NULL and sets errno to EINVAL */
  166. /* General usage is to start at i=0 and iterate until NULL is returned. */
  167. NN_EXPORT const char *nn_symbol (int i, int *value);
  168. /* Constants that are returned in `ns` member of nn_symbol_properties */
  169. #define NN_NS_NAMESPACE 0
  170. #define NN_NS_VERSION 1
  171. #define NN_NS_DOMAIN 2
  172. #define NN_NS_TRANSPORT 3
  173. #define NN_NS_PROTOCOL 4
  174. #define NN_NS_OPTION_LEVEL 5
  175. #define NN_NS_SOCKET_OPTION 6
  176. #define NN_NS_TRANSPORT_OPTION 7
  177. #define NN_NS_OPTION_TYPE 8
  178. #define NN_NS_OPTION_UNIT 9
  179. #define NN_NS_FLAG 10
  180. #define NN_NS_ERROR 11
  181. #define NN_NS_LIMIT 12
  182. #define NN_NS_EVENT 13
  183. #define NN_NS_STATISTIC 14
  184. /* Constants that are returned in `type` member of nn_symbol_properties */
  185. #define NN_TYPE_NONE 0
  186. #define NN_TYPE_INT 1
  187. #define NN_TYPE_STR 2
  188. /* Constants that are returned in the `unit` member of nn_symbol_properties */
  189. #define NN_UNIT_NONE 0
  190. #define NN_UNIT_BYTES 1
  191. #define NN_UNIT_MILLISECONDS 2
  192. #define NN_UNIT_PRIORITY 3
  193. #define NN_UNIT_BOOLEAN 4
  194. #define NN_UNIT_MESSAGES 5
  195. #define NN_UNIT_COUNTER 6
  196. /* Structure that is returned from nn_symbol */
  197. struct nn_symbol_properties {
  198. /* The constant value */
  199. int value;
  200. /* The constant name */
  201. const char* name;
  202. /* The constant namespace, or zero for namespaces themselves */
  203. int ns;
  204. /* The option type for socket option constants */
  205. int type;
  206. /* The unit for the option value for socket option constants */
  207. int unit;
  208. };
  209. /* Fills in nn_symbol_properties structure and returns it's length */
  210. /* If the index is out-of-range, returns 0 */
  211. /* General usage is to start at i=0 and iterate until zero is returned. */
  212. NN_EXPORT int nn_symbol_info (int i,
  213. struct nn_symbol_properties *buf, int buflen);
  214. /******************************************************************************/
  215. /* Helper function for shutting down multi-threaded applications. */
  216. /******************************************************************************/
  217. NN_EXPORT void nn_term (void);
  218. /******************************************************************************/
  219. /* Zero-copy support. */
  220. /******************************************************************************/
  221. #define NN_MSG ((size_t) -1)
  222. NN_EXPORT void *nn_allocmsg (size_t size, int type);
  223. NN_EXPORT void *nn_reallocmsg (void *msg, size_t size);
  224. NN_EXPORT int nn_freemsg (void *msg);
  225. /******************************************************************************/
  226. /* Socket definition. */
  227. /******************************************************************************/
  228. struct nn_iovec {
  229. void *iov_base;
  230. size_t iov_len;
  231. };
  232. struct nn_msghdr {
  233. struct nn_iovec *msg_iov;
  234. int msg_iovlen;
  235. void *msg_control;
  236. size_t msg_controllen;
  237. };
  238. struct nn_cmsghdr {
  239. size_t cmsg_len;
  240. int cmsg_level;
  241. int cmsg_type;
  242. };
  243. /* Internal stuff. Not to be used directly. */
  244. NN_EXPORT struct nn_cmsghdr *nn_cmsg_nxthdr_ (
  245. const struct nn_msghdr *mhdr,
  246. const struct nn_cmsghdr *cmsg);
  247. #define NN_CMSG_ALIGN_(len) \
  248. (((len) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1))
  249. /* POSIX-defined msghdr manipulation. */
  250. #define NN_CMSG_FIRSTHDR(mhdr) \
  251. nn_cmsg_nxthdr_ ((struct nn_msghdr*) (mhdr), NULL)
  252. #define NN_CMSG_NXTHDR(mhdr, cmsg) \
  253. nn_cmsg_nxthdr_ ((struct nn_msghdr*) (mhdr), (struct nn_cmsghdr*) (cmsg))
  254. #define NN_CMSG_DATA(cmsg) \
  255. ((unsigned char*) (((struct nn_cmsghdr*) (cmsg)) + 1))
  256. /* Extensions to POSIX defined by RFC 3542. */
  257. #define NN_CMSG_SPACE(len) \
  258. (NN_CMSG_ALIGN_ (len) + NN_CMSG_ALIGN_ (sizeof (struct nn_cmsghdr)))
  259. #define NN_CMSG_LEN(len) \
  260. (NN_CMSG_ALIGN_ (sizeof (struct nn_cmsghdr)) + (len))
  261. /* SP address families. */
  262. #define AF_SP 1
  263. #define AF_SP_RAW 2
  264. /* Max size of an SP address. */
  265. #define NN_SOCKADDR_MAX 128
  266. /* Socket option levels: Negative numbers are reserved for transports,
  267. positive for socket types. */
  268. #define NN_SOL_SOCKET 0
  269. /* Generic socket options (NN_SOL_SOCKET level). */
  270. #define NN_LINGER 1
  271. #define NN_SNDBUF 2
  272. #define NN_RCVBUF 3
  273. #define NN_SNDTIMEO 4
  274. #define NN_RCVTIMEO 5
  275. #define NN_RECONNECT_IVL 6
  276. #define NN_RECONNECT_IVL_MAX 7
  277. #define NN_SNDPRIO 8
  278. #define NN_RCVPRIO 9
  279. #define NN_SNDFD 10
  280. #define NN_RCVFD 11
  281. #define NN_DOMAIN 12
  282. #define NN_PROTOCOL 13
  283. #define NN_IPV4ONLY 14
  284. #define NN_SOCKET_NAME 15
  285. #define NN_RCVMAXSIZE 16
  286. #define NN_MAXTTL 17
  287. /* Send/recv options. */
  288. #define NN_DONTWAIT 1
  289. /* Ancillary data. */
  290. #define PROTO_SP 1
  291. #define SP_HDR 1
  292. NN_EXPORT int nn_socket (int domain, int protocol);
  293. NN_EXPORT int nn_close (int s);
  294. NN_EXPORT int nn_setsockopt (int s, int level, int option, const void *optval,
  295. size_t optvallen);
  296. NN_EXPORT int nn_getsockopt (int s, int level, int option, void *optval,
  297. size_t *optvallen);
  298. NN_EXPORT int nn_bind (int s, const char *addr);
  299. NN_EXPORT int nn_connect (int s, const char *addr);
  300. NN_EXPORT int nn_shutdown (int s, int how);
  301. NN_EXPORT int nn_send (int s, const void *buf, size_t len, int flags);
  302. NN_EXPORT int nn_recv (int s, void *buf, size_t len, int flags);
  303. NN_EXPORT int nn_sendmsg (int s, const struct nn_msghdr *msghdr, int flags);
  304. NN_EXPORT int nn_recvmsg (int s, struct nn_msghdr *msghdr, int flags);
  305. /******************************************************************************/
  306. /* Socket mutliplexing support. */
  307. /******************************************************************************/
  308. #define NN_POLLIN 1
  309. #define NN_POLLOUT 2
  310. struct nn_pollfd {
  311. int fd;
  312. short events;
  313. short revents;
  314. };
  315. NN_EXPORT int nn_poll (struct nn_pollfd *fds, int nfds, int timeout);
  316. /******************************************************************************/
  317. /* Built-in support for devices. */
  318. /******************************************************************************/
  319. NN_EXPORT int nn_device (int s1, int s2);
  320. /******************************************************************************/
  321. /* Statistics. */
  322. /******************************************************************************/
  323. /* Transport statistics */
  324. #define NN_STAT_ESTABLISHED_CONNECTIONS 101
  325. #define NN_STAT_ACCEPTED_CONNECTIONS 102
  326. #define NN_STAT_DROPPED_CONNECTIONS 103
  327. #define NN_STAT_BROKEN_CONNECTIONS 104
  328. #define NN_STAT_CONNECT_ERRORS 105
  329. #define NN_STAT_BIND_ERRORS 106
  330. #define NN_STAT_ACCEPT_ERRORS 107
  331. #define NN_STAT_CURRENT_CONNECTIONS 201
  332. #define NN_STAT_INPROGRESS_CONNECTIONS 202
  333. #define NN_STAT_CURRENT_EP_ERRORS 203
  334. /* The socket-internal statistics */
  335. #define NN_STAT_MESSAGES_SENT 301
  336. #define NN_STAT_MESSAGES_RECEIVED 302
  337. #define NN_STAT_BYTES_SENT 303
  338. #define NN_STAT_BYTES_RECEIVED 304
  339. /* Protocol statistics */
  340. #define NN_STAT_CURRENT_SND_PRIORITY 401
  341. NN_EXPORT uint64_t nn_get_statistic (int s, int stat);
  342. #ifdef __cplusplus
  343. }
  344. #endif
  345. #endif