PageRenderTime 39ms CodeModel.GetById 2ms app.highlight 30ms RepoModel.GetById 1ms app.codeStats 0ms

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