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

/tc6.0.x/tags/TOMCAT_6_0_10/java/org/apache/tomcat/jni/Socket.java

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