javanio.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /* javanio.h -- reference implementation of native functions.
  2. Copyright (C) 2006 Free Software Foundation, Inc.
  3. This file is a part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or (at
  7. your option) any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
  15. USA
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. #ifndef __JAVANIO_H__
  32. #define __JAVANIO_H__
  33. #include <sys/time.h>
  34. /**
  35. * This header defines functions that are called by our JNI reference
  36. * implementation of java.nio.*. In our reference implementation, these
  37. * functions map exactly to their counterparts in POSIX; in implementations
  38. * that can't use these functions directly (such as systems that use user-land
  39. * threads, and thus can't call blocking system calls directly) can provide
  40. * their own implementations suitable for their system.
  41. */
  42. /**
  43. * This macro is used in all function prototypes below; if any additional
  44. * keywords need to be added to a prototype, declare them in this macro.
  45. */
  46. #define CPNIO_EXPORT static inline
  47. /**
  48. * Read bytes from the given file descriptor into the given memory address, which
  49. * has sufficient space for NBYTES bytes.
  50. *
  51. * \param fd The file descriptor to read from.
  52. * \param buf The memory address to read bytes into.
  53. * \param nbytes The number of bytes available to store in BUF.
  54. * \return The number of bytes read, possibly zero, on success; return -1 on failure,
  55. * and set ERRNO to an appropriate value.
  56. * \see read(2)
  57. *
  58. * Allowed errno values:
  59. * [EBADF] If FD is not a valid file descriptor, or is not open for reading.
  60. * [EFAULT] If BUF points outside the process's address space.
  61. * [EIO] An I/O error occurrs.
  62. * [EINTR] If the read is interrupted by a signal.
  63. * [EINVAL] If FD is negative.
  64. * [EAGAIN] If FD was marked for non-blocking I/O, and no data were ready to
  65. * be read.
  66. */
  67. CPNIO_EXPORT ssize_t cpnio_read (int fd, void *buf, size_t nbytes);
  68. /*
  69. * Read bytes from a file descriptor into a sequence of IO buffers.
  70. *
  71. * The iovec structure is defined as:
  72. *
  73. * struct iovec {
  74. * char *iov_base;
  75. * size_t iov_len;
  76. * };
  77. *
  78. * The call to _cp_readv should do a scattering read, where for each struct iovec
  79. * in the supplied list, up to IOV_LEN bytes are read into IOV_BASE. The function
  80. * returns the total number of bytes read into all supplied buffers.
  81. *
  82. * \param fd The file descriptor.
  83. * \param iov A pointer to the head of a list of iovec structures.
  84. * \param iovcnt The number of iovec structures pointed to by IOV.
  85. * \return The total number of bytes read accross all buffers, possibly zero. On
  86. * error, -1 is returned and ERRNO is set.
  87. * \see readv(2)
  88. *
  89. * Allowed ERRNO values include all of those listed for _cp_read, as well as the
  90. * following:
  91. * [EINVAL] If IOVCNT overflows the maximum number of iovec structures
  92. * this platform supports (usually 16), if any IOV_LEN value
  93. * is negative, or if the sum of all IOV_LEN values is too
  94. * large to be stored in a ssize_t (usually a 32-bit integer).
  95. * [EFAULT] If part of IOV points outside the process's address space.
  96. */
  97. CPNIO_EXPORT ssize_t cpnio_readv (int fd, const struct iovec *iov, int iovcnt);
  98. /*
  99. * Write NBYTES bytes from BUF to the file descriptor FD, returning the number
  100. * of bytes successfully written.
  101. *
  102. * \param fd The file descriptor.
  103. * \param buf A pointer to the bytes to write.
  104. * \param nbytes The maximum number of bytes to write.
  105. * \return The number of bytes written to the file descriptor, possibly zero. -1
  106. * is returned if an error is encountered, and ERRNO will be set.
  107. * \see write(2)
  108. *
  109. * Allowed ERRNO values:
  110. * [EBADF] If FD is not a valid file descriptor or is not open for writing.
  111. * [EPIPE] If FD is a pipe, when the other side is disconnected; if FD is a
  112. * socket, when the peer is not connected.
  113. * [EFBIG] When FD is a file, and writing to it overflows the process's
  114. * or the system's maximim file size.
  115. * [EFAULT] If the buffer to write points outside the process's address
  116. * space.
  117. * [EINVAL] If the descriptor FD is negative.
  118. * [ENOSPC] If FD is a file, and there is insufficient space on the
  119. * filesystem.
  120. * [EDQUOT] If FD is a file, and the user's disk quota has been exceeded.
  121. * [EIO] If an I/O error occurs.
  122. * [EINTR] If the call is interrupted by a signal.
  123. * [EAGAIN] If FD is in non-blocking mode, and no bytes could be immediately
  124. * written.
  125. */
  126. CPNIO_EXPORT ssize_t cpnio_write (int fd, const void *buf, size_t nbytes);
  127. /*
  128. * Write data from a sequence of IOVCNT buffers IOV to a file descriptor FD.
  129. *
  130. * \param fd The file descriptor.
  131. * \param iov The list of buffers to write.
  132. * \param iovcnt The number of iovec structures pointed to by IOV.
  133. * \return The total number of bytes written from the given buffers, possibly
  134. * zero. -1 if an error occurs, and ERRNO will be set.
  135. * \see writev(2)
  136. *
  137. * Allowed ERRNO values include those mentioned in _cp_write, as well as:
  138. * [EDESTADDRREQ] If the descriptor is a datagram socket, and the peer is
  139. * no longer available.
  140. * [EINVAL] If IOVCNT is out of range, if any IOV_LEN value is
  141. * negative, or if the sum of all IOVCNT IOV_LEN values
  142. * will overflow a ssize_t.
  143. * [ENOBUFS] If the mbuf pool is exhausted (???).
  144. */
  145. CPNIO_EXPORT ssize_t cpnio_writev (int fd, const struct iovec *iov, size_t iovcnt);
  146. /**
  147. * Open a new, unbound and unconnected socket.
  148. *
  149. * \param domain The socket domain. Implementations need only handle AF_INET.
  150. * \param type The socket type; implementations need only handle types
  151. * SOCK_STREAM (for streaming sockets) and SOCK_DGRAM (for datagram sockets).
  152. * \param protocol This should always be 0. It can be ignored.
  153. * \return A new file descriptor pointing to a newly created socket, or -1 on
  154. * error, and ERRNO set.
  155. *
  156. * Allowed ERRNO values:
  157. * [EPROTONOSUPPORT] If TYPE is unrecognized.
  158. * [EMFILE] If a new file descriptor cannot be allocated, because
  159. * the process's descriptor table is full.
  160. * [ENFILE] Likewise, but when the system table is full.
  161. * [EACCES] If this operation is not allowed.
  162. * [ENOBUFS] If there is not enough buffer space available for the
  163. * new socket.
  164. */
  165. CPNIO_EXPORT int cpnio_socket (int domain, int type, int protocol);
  166. /**
  167. * Connect a socket to a remote address.
  168. *
  169. * \param fd The file descriptor of the socket to connect.
  170. * \param addr The address to connect to. In practice, this should be
  171. * either a `struct sockaddr_in' or a `struct sockaddr_in6'.
  172. * \param addrlen The size of the address structure passed by ADDR.
  173. * \return Zero if the connect succeeds. -1 on error, and ERRNO should be set.
  174. *
  175. * Allowed ERRNO values:
  176. * [EBADF] If FD is not a valid file descriptor.
  177. * [ENOTSOCK] If FD is not a socket descriptor.
  178. * [EADDRNOTAVAIL] If ADDR is not available for use to this process.
  179. * [EAFNOSUPPORT] If the address family of ADDR is not supported.
  180. * [EISCONN] If the socket is already connected.
  181. * [ETIMEDOUT] If the connection could not be made in a reasonable
  182. * amount of time.
  183. * [ECONNREFUSED] If the connection attempt was rejected.
  184. * [ENETUNREACH] If the network ADDR is on is unreachable.
  185. * [EADDRINUSE] If the address is already in use.
  186. * [EFAULT] If ADDR points outside the addressable space.
  187. * [EINPROGRESS] If FD is in non-blocking mode, and the connection could
  188. * not be completed immediately.
  189. * [EALREADY] If FD is in non-blocking mode, and a connection attempt
  190. * is still pending.
  191. * [EACCESS] If ADDR is the broadcast address, and the socket option
  192. * SO_BROADCAST is not set.
  193. */
  194. CPNIO_EXPORT int cpnio_connect (int fd, const struct sockaddr *addr, socklen_t addrlen);
  195. /**
  196. * Accept an incoming connection on a socket, returning a new socket for
  197. * the connection, and storing the peer address in ADDR.
  198. *
  199. * \param fd The socket file descriptor.
  200. * \param addr The structure to store the peer address in.
  201. * \param addrlen The size of the data available in ADDR; upon return, the
  202. * number of bytes stored in ADDR will be placed here.
  203. * \return The new socket file descriptor, or -1 on error, and ERRNO set.
  204. *
  205. * Allowed ERRNO values:
  206. * [EBADF] If FD is not a valid file descriptor.
  207. * [ENOTSOCK] If FD in not a socket descriptor.
  208. * [EOPNOTSUPP] If the socket is not a SOCK_STREAM socket.
  209. * [EFAULT] If ADDR points outside the process's addressable space.
  210. * [EWOULDBLOCK] If the socket is in non-blocking mode, and no connection
  211. * attempt is currently ready.
  212. * [EMFILE] If the process's descriptor table is full.
  213. * [ENFILE] If the system's descriptor table is full.
  214. */
  215. CPNIO_EXPORT int cpnio_accept (int fd, struct sockaddr *addr, socklen_t *addrlen);
  216. /**
  217. * Send a datagram to the given address.
  218. *
  219. * \param fd The socket file descriptor.
  220. * \param msg A pointer to the message to send.
  221. * \param len The size of the message to send.
  222. * \param flags Flags for sending.
  223. * \param to The remote address to send the message to.
  224. * \param tolen The size of the TO address structure.
  225. * \return The number of bytes written, possibly zero, on success. Returns
  226. * -1 on failure, and sets ERRNO.
  227. * \see sendto(2)
  228. *
  229. * Allowed ERRNO values:
  230. * [EBADF]
  231. * [ENOTSOCK]
  232. * [EFAULT]
  233. * [EMSGSIZE]
  234. * [EAGAIN]
  235. * [ENOBUFS]
  236. * [EACCES]
  237. * [EHOSTUNREACH]
  238. */
  239. CPNIO_EXPORT ssize_t cpnio_sendto (int fd, const void *msg, size_t len, int flags,
  240. const struct sockaddr *to, socklen_t tolen);
  241. /**
  242. * Receive a message on a socket, storing the remote host's address in
  243. * FROM.
  244. *
  245. * \param fd The socket file descriptor.
  246. * \param buf The buffer to store received bytes in.
  247. * \param flags Flags to control the receive.
  248. * \param from Where to store the remote address.
  249. * \param fromlen Pointer to the size of FROM; on return, it will contain the
  250. * size of the structure placed in FROM.
  251. * \return The number of bytes received on success. -1 on error, and ERRNO will
  252. * be set.
  253. * \see recvfrom(2)
  254. *
  255. * Allewed ERRNO values:
  256. * [EBADF] FD is not a valid file descriptor.
  257. * [ENOTCONN] If the socket is stream-oriented, and no prior call to
  258. * connect(2) was made.
  259. * [ENOTSOCK] FD is not a socket.
  260. * [EAGAIN] FD is in non-blocking mode, and no message was
  261. * immediately available.
  262. * [EINTR] The system call was interrupted by a signal.
  263. * [EFAULT] BUF, FROM, or FROMLEN lie outside the process's address
  264. * space.
  265. */
  266. CPNIO_EXPORT ssize_t cpnio_recvfrom (int fd, void *buf, size_t len, int flags,
  267. struct sockaddr *from, socklen_t *fromlen);
  268. /**
  269. * Control file descriptor properties.
  270. *
  271. * \param fd The file descriptor to control.
  272. * \param cmd The command to execute.
  273. * \param arg The command argument.
  274. * \return A value other than -1, specific to CMD. On error, -1 is
  275. * returned, and ERRNO is set.
  276. *
  277. * Allowed ERRNO values:
  278. * FIXME
  279. */
  280. CPNIO_EXPORT int cpnio_fcntl (int fd, int cmd, long arg);
  281. /**
  282. * Select from one of the given file descriptor sets a descriptor that
  283. * is ready for the given operation (read, write, etc.).
  284. *
  285. * \param nfds A value one larger than the largest file
  286. * descriptor.
  287. * \param readfds A set of file descriptors to select for
  288. * readability.
  289. * \param writefds A set of file descriptors to select for
  290. * writability.
  291. * \param exceptfds A set of file descriptors to select for
  292. * exceptional conditions.
  293. * \param tm The selection timeout.
  294. * \return The number of file descriptors selected, possibly zero, or
  295. * -1 on error (and with ERRNO set).
  296. */
  297. CPNIO_EXPORT int cpnio_select (int nfds, fd_set *readfds, fd_set *writefds,
  298. fd_set *exceptfds, struct timeval *tm);
  299. /*
  300. * We include the implementation file here, because our reference
  301. * implementation is trivial, and the functions are declared extern
  302. * inline.
  303. *
  304. * Implementations that need different implementations of these functions
  305. * SHOULD remove this line, and compile javanio.c as a separate unit.
  306. */
  307. #include "javanio.c"
  308. #endif /* __JAVANIO_H__ */