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