/java/org/apache/tomcat/jni/Socket.java

https://github.com/enson16855/tomcat · Java · 584 lines · 96 code · 45 blank · 443 comment · 0 complexity · f2026de4877f2f28afffebb215de3127 MD5 · raw file

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.tomcat.jni;
  18. /* Import needed classes */
  19. import java.nio.ByteBuffer;
  20. /** Socket
  21. *
  22. * @author Mladen Turk
  23. */
  24. public class Socket {
  25. /* Standard socket defines */
  26. public static final int SOCK_STREAM = 0;
  27. public static final int SOCK_DGRAM = 1;
  28. /*
  29. * apr_sockopt Socket option definitions
  30. */
  31. public static final int APR_SO_LINGER = 1; /** Linger */
  32. public static final int APR_SO_KEEPALIVE = 2; /** Keepalive */
  33. public static final int APR_SO_DEBUG = 4; /** Debug */
  34. public static final int APR_SO_NONBLOCK = 8; /** Non-blocking IO */
  35. public static final int APR_SO_REUSEADDR = 16; /** Reuse addresses */
  36. public static final int APR_SO_SNDBUF = 64; /** Send buffer */
  37. public static final int APR_SO_RCVBUF = 128; /** Receive buffer */
  38. public static final int APR_SO_DISCONNECTED = 256; /** Disconnected */
  39. /** For SCTP sockets, this is mapped to STCP_NODELAY internally. */
  40. public static final int APR_TCP_NODELAY = 512;
  41. public static final int APR_TCP_NOPUSH = 1024; /** No push */
  42. /** This flag is ONLY set internally when we set APR_TCP_NOPUSH with
  43. * APR_TCP_NODELAY set to tell us that APR_TCP_NODELAY should be turned on
  44. * again when NOPUSH is turned off
  45. */
  46. public static final int APR_RESET_NODELAY = 2048;
  47. /** Set on non-blocking sockets (timeout != 0) on which the
  48. * previous read() did not fill a buffer completely. the next
  49. * apr_socket_recv() will first call select()/poll() rather than
  50. * going straight into read(). (Can also be set by an application to
  51. * force a select()/poll() call before the next read, in cases where
  52. * the app expects that an immediate read would fail.)
  53. */
  54. public static final int APR_INCOMPLETE_READ = 4096;
  55. /** like APR_INCOMPLETE_READ, but for write
  56. */
  57. public static final int APR_INCOMPLETE_WRITE = 8192;
  58. /** Don't accept IPv4 connections on an IPv6 listening socket.
  59. */
  60. public static final int APR_IPV6_V6ONLY = 16384;
  61. /** Delay accepting of new connections until data is available.
  62. */
  63. public static final int APR_TCP_DEFER_ACCEPT = 32768;
  64. /** Define what type of socket shutdown should occur.
  65. * apr_shutdown_how_e enum
  66. */
  67. public static final int APR_SHUTDOWN_READ = 0; /** no longer allow read request */
  68. public static final int APR_SHUTDOWN_WRITE = 1; /** no longer allow write requests */
  69. public static final int APR_SHUTDOWN_READWRITE = 2; /** no longer allow read or write requests */
  70. public static final int APR_IPV4_ADDR_OK = 0x01;
  71. public static final int APR_IPV6_ADDR_OK = 0x02;
  72. /* TODO: Missing:
  73. * APR_INET
  74. * APR_UNSPEC
  75. * APR_INET6
  76. */
  77. public static final int APR_UNSPEC = 0;
  78. public static final int APR_INET = 1;
  79. public static final int APR_INET6 = 2;
  80. public static final int APR_PROTO_TCP = 6; /** TCP */
  81. public static final int APR_PROTO_UDP = 17; /** UDP */
  82. public static final int APR_PROTO_SCTP = 132; /** SCTP */
  83. /**
  84. * Enum to tell us if we're interested in remote or local socket
  85. * apr_interface_e
  86. */
  87. public static final int APR_LOCAL = 0;
  88. public static final int APR_REMOTE = 1;
  89. /* Socket.get types */
  90. public static final int SOCKET_GET_POOL = 0;
  91. public static final int SOCKET_GET_IMPL = 1;
  92. public static final int SOCKET_GET_APRS = 2;
  93. public static final int SOCKET_GET_TYPE = 3;
  94. /**
  95. * Create a socket.
  96. * @param family The address family of the socket (e.g., APR_INET).
  97. * @param type The type of the socket (e.g., SOCK_STREAM).
  98. * @param protocol The protocol of the socket (e.g., APR_PROTO_TCP).
  99. * @param cont The parent pool to use
  100. * @return The new socket that has been set up.
  101. */
  102. public static native long create(int family, int type,
  103. int protocol, long cont)
  104. throws Exception;
  105. /**
  106. * Shutdown either reading, writing, or both sides of a socket.
  107. * <br />
  108. * This does not actually close the socket descriptor, it just
  109. * controls which calls are still valid on the socket.
  110. * @param thesocket The socket to close
  111. * @param how How to shutdown the socket. One of:
  112. * <PRE>
  113. * APR_SHUTDOWN_READ no longer allow read requests
  114. * APR_SHUTDOWN_WRITE no longer allow write requests
  115. * APR_SHUTDOWN_READWRITE no longer allow read or write requests
  116. * </PRE>
  117. */
  118. public static native int shutdown(long thesocket, int how);
  119. /**
  120. * Close a socket.
  121. * @param thesocket The socket to close
  122. */
  123. public static native int close(long thesocket);
  124. /**
  125. * Destroy a pool associated with socket
  126. * @param thesocket The destroy
  127. */
  128. public static native void destroy(long thesocket);
  129. /**
  130. * Bind the socket to its associated port
  131. * @param sock The socket to bind
  132. * @param sa The socket address to bind to
  133. * This may be where we will find out if there is any other process
  134. * using the selected port.
  135. */
  136. public static native int bind(long sock, long sa);
  137. /**
  138. * Listen to a bound socket for connections.
  139. * @param sock The socket to listen on
  140. * @param backlog The number of outstanding connections allowed in the sockets
  141. * listen queue. If this value is less than zero, the listen
  142. * queue size is set to zero.
  143. */
  144. public static native int listen(long sock, int backlog);
  145. /**
  146. * Accept a new connection request
  147. * @param sock The socket we are listening on.
  148. * @param pool The pool for the new socket.
  149. * @return A copy of the socket that is connected to the socket that
  150. * made the connection request. This is the socket which should
  151. * be used for all future communication.
  152. */
  153. public static native long acceptx(long sock, long pool)
  154. throws Exception;
  155. /**
  156. * Accept a new connection request
  157. * @param sock The socket we are listening on.
  158. * @return A copy of the socket that is connected to the socket that
  159. * made the connection request. This is the socket which should
  160. * be used for all future communication.
  161. */
  162. public static native long accept(long sock)
  163. throws Exception;
  164. /**
  165. * Set an OS level accept filter.
  166. * @param sock The socket to put the accept filter on.
  167. * @param name The accept filter
  168. * @param args Any extra args to the accept filter. Passing NULL here removes
  169. * the accept filter.
  170. */
  171. public static native int acceptfilter(long sock, String name, String args);
  172. /**
  173. * Query the specified socket if at the OOB/Urgent data mark
  174. * @param sock The socket to query
  175. * @return True if socket is at the OOB/urgent mark,
  176. * otherwise return false.
  177. */
  178. public static native boolean atmark(long sock);
  179. /**
  180. * Issue a connection request to a socket either on the same machine
  181. * or a different one.
  182. * @param sock The socket we wish to use for our side of the connection
  183. * @param sa The address of the machine we wish to connect to.
  184. */
  185. public static native int connect(long sock, long sa);
  186. /**
  187. * Send data over a network.
  188. * <PRE>
  189. * This functions acts like a blocking write by default. To change
  190. * this behavior, use apr_socket_timeout_set() or the APR_SO_NONBLOCK
  191. * socket option.
  192. *
  193. * It is possible for both bytes to be sent and an error to be returned.
  194. *
  195. * APR_EINTR is never returned.
  196. * </PRE>
  197. * @param sock The socket to send the data over.
  198. * @param buf The buffer which contains the data to be sent.
  199. * @param offset Offset in the byte buffer.
  200. * @param len The number of bytes to write; (-1) for full array.
  201. * @return The number of bytes send.
  202. *
  203. */
  204. public static native int send(long sock, byte[] buf, int offset, int len);
  205. /**
  206. * Send data over a network.
  207. * <PRE>
  208. * This functions acts like a blocking write by default. To change
  209. * this behavior, use apr_socket_timeout_set() or the APR_SO_NONBLOCK
  210. * socket option.
  211. *
  212. * It is possible for both bytes to be sent and an error to be returned.
  213. *
  214. * APR_EINTR is never returned.
  215. * </PRE>
  216. * @param sock The socket to send the data over.
  217. * @param buf The Byte buffer which contains the data to be sent.
  218. * @param offset The offset within the buffer array of the first buffer from
  219. * which bytes are to be retrieved; must be non-negative
  220. * and no larger than buf.length
  221. * @param len The maximum number of buffers to be accessed; must be non-negative
  222. * and no larger than buf.length - offset
  223. * @return The number of bytes send.
  224. *
  225. */
  226. public static native int sendb(long sock, ByteBuffer buf,
  227. int offset, int len);
  228. /**
  229. * Send data over a network without retry
  230. * <PRE>
  231. * This functions acts like a blocking write by default. To change
  232. * this behavior, use apr_socket_timeout_set() or the APR_SO_NONBLOCK
  233. * socket option.
  234. *
  235. * It is possible for both bytes to be sent and an error to be returned.
  236. *
  237. * </PRE>
  238. * @param sock The socket to send the data over.
  239. * @param buf The Byte buffer which contains the data to be sent.
  240. * @param offset The offset within the buffer array of the first buffer from
  241. * which bytes are to be retrieved; must be non-negative
  242. * and no larger than buf.length
  243. * @param len The maximum number of buffers to be accessed; must be non-negative
  244. * and no larger than buf.length - offset
  245. * @return The number of bytes send.
  246. *
  247. */
  248. public static native int sendib(long sock, ByteBuffer buf,
  249. int offset, int len);
  250. /**
  251. * Send data over a network using internally set ByteBuffer
  252. */
  253. public static native int sendbb(long sock,
  254. int offset, int len);
  255. /**
  256. * Send data over a network using internally set ByteBuffer
  257. * without internal retry.
  258. */
  259. public static native int sendibb(long sock,
  260. int offset, int len);
  261. /**
  262. * Send multiple packets of data over a network.
  263. * <PRE>
  264. * This functions acts like a blocking write by default. To change
  265. * this behavior, use apr_socket_timeout_set() or the APR_SO_NONBLOCK
  266. * socket option.
  267. * The number of bytes actually sent is stored in argument 3.
  268. *
  269. * It is possible for both bytes to be sent and an error to be returned.
  270. *
  271. * APR_EINTR is never returned.
  272. * </PRE>
  273. * @param sock The socket to send the data over.
  274. * @param vec The array from which to get the data to send.
  275. *
  276. */
  277. public static native int sendv(long sock, byte[][] vec);
  278. /**
  279. * @param sock The socket to send from
  280. * @param where The apr_sockaddr_t describing where to send the data
  281. * @param flags The flags to use
  282. * @param buf The data to send
  283. * @param offset Offset in the byte buffer.
  284. * @param len The length of the data to send
  285. */
  286. public static native int sendto(long sock, long where, int flags,
  287. byte[] buf, int offset, int len);
  288. /**
  289. * Read data from a network.
  290. *
  291. * <PRE>
  292. * This functions acts like a blocking read by default. To change
  293. * this behavior, use apr_socket_timeout_set() or the APR_SO_NONBLOCK
  294. * socket option.
  295. * The number of bytes actually received is stored in argument 3.
  296. *
  297. * It is possible for both bytes to be received and an APR_EOF or
  298. * other error to be returned.
  299. *
  300. * APR_EINTR is never returned.
  301. * </PRE>
  302. * @param sock The socket to read the data from.
  303. * @param buf The buffer to store the data in.
  304. * @param offset Offset in the byte buffer.
  305. * @param nbytes The number of bytes to read (-1) for full array.
  306. * @return the number of bytes received.
  307. */
  308. public static native int recv(long sock, byte[] buf, int offset, int nbytes);
  309. /**
  310. * Read data from a network with timeout.
  311. *
  312. * <PRE>
  313. * This functions acts like a blocking read by default. To change
  314. * this behavior, use apr_socket_timeout_set() or the APR_SO_NONBLOCK
  315. * socket option.
  316. * The number of bytes actually received is stored in argument 3.
  317. *
  318. * It is possible for both bytes to be received and an APR_EOF or
  319. * other error to be returned.
  320. *
  321. * APR_EINTR is never returned.
  322. * </PRE>
  323. * @param sock The socket to read the data from.
  324. * @param buf The buffer to store the data in.
  325. * @param offset Offset in the byte buffer.
  326. * @param nbytes The number of bytes to read (-1) for full array.
  327. * @param timeout The socket timeout in microseconds.
  328. * @return the number of bytes received.
  329. */
  330. public static native int recvt(long sock, byte[] buf, int offset,
  331. int nbytes, long timeout);
  332. /**
  333. * Read data from a network.
  334. *
  335. * <PRE>
  336. * This functions acts like a blocking read by default. To change
  337. * this behavior, use apr_socket_timeout_set() or the APR_SO_NONBLOCK
  338. * socket option.
  339. * The number of bytes actually received is stored in argument 3.
  340. *
  341. * It is possible for both bytes to be received and an APR_EOF or
  342. * other error to be returned.
  343. *
  344. * APR_EINTR is never returned.
  345. * </PRE>
  346. * @param sock The socket to read the data from.
  347. * @param buf The buffer to store the data in.
  348. * @param offset Offset in the byte buffer.
  349. * @param nbytes The number of bytes to read (-1) for full array.
  350. * @return the number of bytes received.
  351. */
  352. public static native int recvb(long sock, ByteBuffer buf,
  353. int offset, int nbytes);
  354. /**
  355. * Read data from a network using internally set ByteBuffer
  356. */
  357. public static native int recvbb(long sock,
  358. int offset, int nbytes);
  359. /**
  360. * Read data from a network with timeout.
  361. *
  362. * <PRE>
  363. * This functions acts like a blocking read by default. To change
  364. * this behavior, use apr_socket_timeout_set() or the APR_SO_NONBLOCK
  365. * socket option.
  366. * The number of bytes actually received is stored in argument 3.
  367. *
  368. * It is possible for both bytes to be received and an APR_EOF or
  369. * other error to be returned.
  370. *
  371. * APR_EINTR is never returned.
  372. * </PRE>
  373. * @param sock The socket to read the data from.
  374. * @param buf The buffer to store the data in.
  375. * @param offset Offset in the byte buffer.
  376. * @param nbytes The number of bytes to read (-1) for full array.
  377. * @param timeout The socket timeout in microseconds.
  378. * @return the number of bytes received.
  379. */
  380. public static native int recvbt(long sock, ByteBuffer buf,
  381. int offset, int nbytes, long timeout);
  382. /**
  383. * Read data from a network with timeout using internally set ByteBuffer
  384. */
  385. public static native int recvbbt(long sock,
  386. int offset, int nbytes, long timeout);
  387. /**
  388. * @param from The apr_sockaddr_t to fill in the recipient info
  389. * @param sock The socket to use
  390. * @param flags The flags to use
  391. * @param buf The buffer to use
  392. * @param offset Offset in the byte buffer.
  393. * @param nbytes The number of bytes to read (-1) for full array.
  394. * @return the number of bytes received.
  395. */
  396. public static native int recvfrom(long from, long sock, int flags,
  397. byte[] buf, int offset, int nbytes);
  398. /**
  399. * Setup socket options for the specified socket
  400. * @param sock The socket to set up.
  401. * @param opt The option we would like to configure. One of:
  402. * <PRE>
  403. * APR_SO_DEBUG -- turn on debugging information
  404. * APR_SO_KEEPALIVE -- keep connections active
  405. * APR_SO_LINGER -- lingers on close if data is present
  406. * APR_SO_NONBLOCK -- Turns blocking on/off for socket
  407. * When this option is enabled, use
  408. * the APR_STATUS_IS_EAGAIN() macro to
  409. * see if a send or receive function
  410. * could not transfer data without
  411. * blocking.
  412. * APR_SO_REUSEADDR -- The rules used in validating addresses
  413. * supplied to bind should allow reuse
  414. * of local addresses.
  415. * APR_SO_SNDBUF -- Set the SendBufferSize
  416. * APR_SO_RCVBUF -- Set the ReceiveBufferSize
  417. * </PRE>
  418. * @param on Value for the option.
  419. */
  420. public static native int optSet(long sock, int opt, int on);
  421. /**
  422. * Query socket options for the specified socket
  423. * @param sock The socket to query
  424. * @param opt The option we would like to query. One of:
  425. * <PRE>
  426. * APR_SO_DEBUG -- turn on debugging information
  427. * APR_SO_KEEPALIVE -- keep connections active
  428. * APR_SO_LINGER -- lingers on close if data is present
  429. * APR_SO_NONBLOCK -- Turns blocking on/off for socket
  430. * APR_SO_REUSEADDR -- The rules used in validating addresses
  431. * supplied to bind should allow reuse
  432. * of local addresses.
  433. * APR_SO_SNDBUF -- Set the SendBufferSize
  434. * APR_SO_RCVBUF -- Set the ReceiveBufferSize
  435. * APR_SO_DISCONNECTED -- Query the disconnected state of the socket.
  436. * (Currently only used on Windows)
  437. * </PRE>
  438. * @return Socket option returned on the call.
  439. */
  440. public static native int optGet(long sock, int opt)
  441. throws Exception;
  442. /**
  443. * Setup socket timeout for the specified socket
  444. * @param sock The socket to set up.
  445. * @param t Value for the timeout in microseconds.
  446. * <PRE>
  447. * t > 0 -- read and write calls return APR_TIMEUP if specified time
  448. * elapses with no data read or written
  449. * t == 0 -- read and write calls never block
  450. * t < 0 -- read and write calls block
  451. * </PRE>
  452. */
  453. public static native int timeoutSet(long sock, long t);
  454. /**
  455. * Query socket timeout for the specified socket
  456. * @param sock The socket to query
  457. * @return Socket timeout returned from the query.
  458. */
  459. public static native long timeoutGet(long sock)
  460. throws Exception;
  461. /**
  462. * Send a file from an open file descriptor to a socket, along with
  463. * optional headers and trailers.
  464. * <br />
  465. * This functions acts like a blocking write by default. To change
  466. * this behavior, use apr_socket_timeout_set() or the
  467. * APR_SO_NONBLOCK socket option.
  468. * The number of bytes actually sent is stored in the len parameter.
  469. * The offset parameter is passed by reference for no reason; its
  470. * value will never be modified by the apr_socket_sendfile() function.
  471. * @param sock The socket to which we're writing
  472. * @param file The open file from which to read
  473. * @param headers Array containing the headers to send
  474. * @param trailers Array containing the trailers to send
  475. * @param offset Offset into the file where we should begin writing
  476. * @param len Number of bytes to send from the file
  477. * @param flags APR flags that are mapped to OS specific flags
  478. * @return Number of bytes actually sent, including headers,
  479. * file, and trailers
  480. *
  481. */
  482. public static native long sendfile(long sock, long file, byte [][] headers,
  483. byte[][] trailers, long offset,
  484. long len, int flags);
  485. /**
  486. * Send a file without header and trailer arrays.
  487. */
  488. public static native long sendfilen(long sock, long file, long offset,
  489. long len, int flags);
  490. /**
  491. * Create a child pool from associated socket pool.
  492. * @param thesocket The socket to use
  493. */
  494. public static native long pool(long thesocket)
  495. throws Exception;
  496. /**
  497. * Private method for getting the socket struct members
  498. * @param socket The socket to use
  499. * @param what Struct member to obtain
  500. * <PRE>
  501. * SOCKET_GET_POOL - The socket pool
  502. * SOCKET_GET_IMPL - The socket implementation object
  503. * SOCKET_GET_APRS - APR socket
  504. * SOCKET_GET_TYPE - Socket type
  505. * </PRE>
  506. * @return The structure member address
  507. */
  508. private static native long get(long socket, int what);
  509. /**
  510. * Set internal send ByteBuffer.
  511. * This function will preset internal Java ByteBuffer for
  512. * consecutive sendbb calls.
  513. * @param sock The socket to use
  514. * @param buf The ByteBuffer
  515. */
  516. public static native void setsbb(long sock, ByteBuffer buf);
  517. /**
  518. * Set internal receive ByteBuffer.
  519. * This function will preset internal Java ByteBuffer for
  520. * consecutive revcvbb/recvbbt calls.
  521. * @param sock The socket to use
  522. * @param buf The ByteBuffer
  523. */
  524. public static native void setrbb(long sock, ByteBuffer buf);
  525. /**
  526. * Set the data associated with the current socket.
  527. * @param sock The currently open socket.
  528. * @param data The user data to associate with the socket.
  529. * @param key The key to associate with the data.
  530. */
  531. public static native int dataSet(long sock, String key, Object data);
  532. /**
  533. * Return the data associated with the current socket
  534. * @param key The key to associate with the user data.
  535. * @param sock The currently open socket.
  536. * @return Data or null in case of error.
  537. */
  538. public static native Object dataGet(long sock, String key);
  539. }