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