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 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
|
/*
* 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.socket;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.tomcat.jni.Address;
import org.apache.tomcat.jni.Error;
import org.apache.tomcat.jni.Poll;
import org.apache.tomcat.jni.SSL;
import org.apache.tomcat.jni.SSLExt;
import org.apache.tomcat.jni.SSLSocket;
import org.apache.tomcat.jni.Sockaddr;
import org.apache.tomcat.jni.Socket;
import org.apache.tomcat.jni.Status;
import org.apache.tomcat.jni.socket.AprSocketContext.AprPoller;
import org.apache.tomcat.jni.socket.AprSocketContext.BlockingPollHandler;
/**
* Native socket, using JNI + APR + openssl.
*
* The socket is non-blocking - you can register either a blocking or non
* blocking callback.
*
* There is no explicit method to register/unregister poll interest -
* it is done automatically, when read/write methods return 0.
*
* To keep the socket polling you must read all the available data, until
* read() returns 0. If you want to pause - don't read all input. To resume -
* read again until it returns 0.
*
* Same for write - when write() returns 0 the socket is registered for
* write interest.
*
* You can also use the blocking read/write methods.
*/
public class AprSocket implements Runnable {
private static final Logger log =
Logger.getLogger("org.apache.tomcat.jni.socket.AprSocket");
private static final byte[][] NO_CERTS = new byte[0][];
static final int CONNECTING = 0x1;
static final int CONNECTED = 0x2;
// Current ( real ) poll status
static final int POLLIN_ACTIVE = 0x4;
static final int POLLOUT_ACTIVE = 0x8;
static final int POLL = 0x10;
static final int SSL_ATTACHED = 0x40;
// Requested poll status. Set by read/write when needed.
// Cleared when polled
static final int POLLIN = 0x80;
static final int POLLOUT = 0x100;
static final int ACCEPTED = 0x200;
static final int ERROR = 0x400;
static final int CLOSED = 0x800;
static final int READING = 0x1000;
static final int WRITING = 0x2000;
// Not null
private final AprSocketContext context;
// only one - to save per/socket memory - context has similar callbacks.
BlockingPollHandler handler;
// Set while it's associated with a poller - it'll stay associated after
// connect until close. Destroy will happen in the poller.
// POLL bit indicates if the socket is actually polling.
AprPoller poller;
// Bit field indicating the status and socket should only be accessed with
// socketLock protection
private int status;
long socket;
//long to = 10000;
// Persistent info about the peer ( SSL, etc )
private HostInfo hostInfo;
AprSocket(AprSocketContext context) {
this.context = context;
}
public void recycle() {
status = 0;
hostInfo = null;
handler = null;
socket = 0;
poller = null;
}
@Override
public String toString() {
return (context.isServer() ? "AprSrv-" : "AprCli-") +
Long.toHexString(socket) + " " + Integer.toHexString(status);
}
public void setHandler(BlockingPollHandler l) {
handler = l;
}
private void setNonBlocking() {
if (socket != 0 && context.running) {
Socket.optSet(socket, Socket.APR_SO_NONBLOCK, 1);
Socket.timeoutSet(socket, 0);
}
}
/**
* Check if the socket is currently registered with a poller.
*/
public boolean isPolling() {
synchronized (this) {
return (status & POLL) != 0;
}
}
public BlockingPollHandler getHandler() {
return handler;
}
public AprSocketContext getContext() {
return context;
}
AprSocket setHost(HostInfo hi) {
hostInfo = hi;
return this;
}
/**
*/
public void connect() throws IOException {
if (isBlocking()) {
// will call handleConnected() at the end.
context.connectBlocking(this);
} else {
synchronized(this) {
if ((status & CONNECTING) != 0) {
return;
}
status |= CONNECTING;
}
context.connectExecutor.execute(this);
}
}
// after connection is done, called from a thread pool ( not IO thread )
// may block for handshake.
void afterConnect() throws IOException {
if (hostInfo.secure) {
blockingStartTLS();
}
setNonBlocking(); // call again, to set the bits ( connect was blocking )
setStatus(CONNECTED);
clearStatus(CONNECTING);
notifyConnected(false);
}
public HostInfo getHost() {
return hostInfo;
}
/**
* Write as much data as possible to the socket.
*
* @param data
* @param off
* @param len
* @return For both blocking and non-blocking, returns the number of bytes
* written. If no data can be written (e.g. if the buffers are
* full) 0 will be returned.
* @throws IOException
*/
public int write(byte[] data, int off, int len, long to) throws IOException {
long max = System.currentTimeMillis() + to;
while (true) {
int rc = writeInternal(data, off, len);
if (rc < 0) {
throw new IOException("Write error " + rc);
} else if (rc == 0) {
// need poll out - do we need to update polling ?
context.findPollerAndAdd(this);
} else {
return rc;
}
try {
long waitTime = max - System.currentTimeMillis();
if (waitTime <= 0) {
return 0;
}
wait(waitTime);
} catch (InterruptedException e) {
return 0;
}
}
}
public int write(byte[] data, int off, int len) throws IOException {
// In SSL mode, read/write can't be called at the same time.
int rc = writeInternal(data, off, len);
if (rc < 0) {
throw new IOException("Write error " + rc);
} else if (rc == 0) {
// need poll out - do we need to update polling ?
synchronized (this) {
context.findPollerAndAdd(this);
}
}
return rc;
}
private int writeInternal(byte[] data, int off, int len) throws IOException {
int rt = 0;
int sent = 0;
synchronized(this) {
if ((status & CLOSED) != 0
|| socket == 0
|| !context.running) {
throw new IOException("Closed");
}
if ((status & WRITING) != 0) {
throw new IOException("Write from 2 threads not allowed");
}
status |= WRITING;
while (len > 0) {
sent = Socket.send(socket, data, off, len);
if (sent <= 0) {
break;
}
off += sent;
len -= sent;
}
status &= ~WRITING;
}
if (context.rawDataHandler != null) {
context.rawData(this, false, data, off, sent, len, false);
}
if (sent <= 0) {
if (sent == -Status.TIMEUP || sent == -Status.EAGAIN || sent == 0) {
setStatus(POLLOUT);
updatePolling();
return rt;
}
log.warning("apr.send(): Failed to send, closing " + sent);
reset();
throw new IOException("Error sending " + sent + " " + Error.strerror(-sent));
} else {
off += sent;
len -= sent;
rt += sent;
return sent;
}
}
public int read(byte[] data, int off, int len, long to) throws IOException {
int rd = readNB(data, off, len);
if (rd == 0) {
synchronized(this) {
try {
wait(to);
} catch (InterruptedException e) {
return 0;
}
}
rd = readNB(data, off, len);
}
return processReadResult(data, off, len, rd);
}
public int read(byte[] data, int off, int len) throws IOException {
return readNB(data, off, len);
}
private int processReadResult(byte[] data, int off, int len, int read)
throws IOException {
if (context.rawDataHandler != null) {
context.rawData(this, true, data, off, read, len, false);
}
if (read > 0) {
return read;
}
if (read == 0 || read == -Status.TIMEUP || read == -Status.ETIMEDOUT
|| read == -Status.EAGAIN) {
read = 0;
setStatus(POLLIN);
updatePolling();
return 0;
}
if (read == -Status.APR_EOF || read == -1) {
close();
return -1;
}
// abrupt close
reset();
throw new IOException("apr.read(): " + read + " " + Error.strerror(-read));
}
public int readNB(byte[] data, int off, int len) throws IOException {
int read;
synchronized(this) {
if ((status & CLOSED) != 0
|| socket == 0
|| !context.running) {
return -1;
}
if ((status & READING) != 0) {
throw new IOException("Read from 2 threads not allowed");
}
status |= READING;
read = Socket.recv(socket, data, off, len);
status &= ~READING;
}
return processReadResult(data, off, len, read);
}
/*
No support for shutdownOutput: SSL is quite tricky.
Use close() instead - no read/write will be allowed after.
*/
public void close() {
synchronized (this) {
if ((status & CLOSED) != 0 || socket == 0) {
return;
}
status |= CLOSED;
status &= ~POLLIN;
status &= ~POLLOUT;
}
if (context.rawDataHandler != null) {
context.rawDataHandler.rawData(this, false, null, 0, 0, 0, true);
}
Socket.close(socket);
if (poller == null) {
maybeDestroy();
} else {
try {
poller.requestUpdate(this);
} catch (IOException e) {
e.printStackTrace();
}
}
}
void maybeDestroy() {
synchronized(this) {
if (socket == 0 ||
(status & CONNECTING) != 0 || !context.running) {
// closed or operation in progress
// if context stopped, pool will be destroyed and close
// all sockets automatically.
return;
}
if ((status & CLOSED) == 0) {
return; // not closed
}
if ((status & (WRITING | READING)) != 0) {
return; // not closed
}
if (context.rawDataHandler != null) {
context.rawDataHandler.rawData(this, false, null, -1, -1, -1, true);
}
if (log.isLoggable(Level.FINE)) {
log.info("closing: context.open=" + context.open.get() + " " + this);
}
context.open.decrementAndGet();
if (socket != 0 && (status & CLOSED) == 0) {
Socket.close(socket);
status |= CLOSED;
}
if (handler != null) {
if (isBlocking()) {
context.getExecutor().execute(this);
} else {
handler.closed(this);
}
}
context.destroySocket(this);
}
}
/**
* Close input and output, potentially sending RST, than close the socket.
*
* The proper way to close when gracefully done is by calling writeEnd() and
* reading all remaining input until -1 (EOF) is received.
*
* If EOF is received, the proper way to close is send whatever is remaining and
* call writeEnd();
*/
public void reset() {
setStatus(ERROR);
close();
}
/**
*/
public boolean isClosed() {
synchronized(this) {
if ((status & CLOSED) != 0 || socket == 0 || !context.running) {
return true;
}
return false;
}
}
public long getIOTimeout() throws IOException {
if (socket != 0 && context.running) {
try {
return Socket.timeoutGet(socket) / 1000;
} catch (Exception e) {
throw new IOException(e);
}
} else {
throw new IOException("Socket is closed");
}
}
// Cert is in DER format
// Called after handshake
public byte[][] getPeerCert(boolean check) throws IOException {
getHost();
if (hostInfo.certs != null && hostInfo.certs != NO_CERTS && !check) {
return hostInfo.certs;
}
if (!checkBitAndSocket(SSL_ATTACHED)) {
return NO_CERTS;
}
try {
int certLength = SSLSocket.getInfoI(socket,
SSL.SSL_INFO_CLIENT_CERT_CHAIN);
// TODO: if resumed, old certs are good.
// If not - warn if certs changed, remember first cert, etc.
if (certLength <= 0) {
// Can also happen on session resume - keep the old
if (hostInfo.certs == null) {
hostInfo.certs = NO_CERTS;
}
return hostInfo.certs;
}
hostInfo.certs = new byte[certLength + 1][];
hostInfo.certs[0] = SSLSocket.getInfoB(socket,
SSL.SSL_INFO_CLIENT_CERT);
for (int i = 0; i < certLength; i++) {
hostInfo.certs[i + 1] = SSLSocket.getInfoB(socket,
SSL.SSL_INFO_CLIENT_CERT_CHAIN + i);
}
return hostInfo.certs;
} catch (Exception e) {
throw new IOException(e);
}
}
public X509Certificate[] getPeerX509Cert() throws IOException {
byte[][] certs = getPeerCert(false);
X509Certificate[] xcerts = new X509Certificate[certs.length];
if (certs.length == 0) {
return xcerts;
}
try {
CertificateFactory cf = CertificateFactory.getInstance("X.509");
for (int i = 0; i < certs.length; i++) {
if (certs[i] != null) {
ByteArrayInputStream bis = new ByteArrayInputStream(
certs[i]);
xcerts[i] = (X509Certificate) cf.generateCertificate(bis);
bis.close();
}
}
} catch (CertificateException ex) {
throw new IOException(ex);
}
return xcerts;
}
public String getCipherSuite() throws IOException {
if (checkBitAndSocket(SSL_ATTACHED)) {
return null;
}
try {
return SSLSocket.getInfoS(socket, SSL.SSL_INFO_CIPHER);
} catch (Exception e) {
throw new IOException(e);
}
}
public int getKeySize() throws IOException {
if (checkBitAndSocket(SSL_ATTACHED)) {
return -1;
}
try {
return SSLSocket.getInfoI(socket, SSL.SSL_INFO_CIPHER_USEKEYSIZE);
} catch (Exception e) {
throw new IOException(e);
}
}
public int getRemotePort() throws IOException {
if (socket != 0 && context.running) {
try {
long sa = Address.get(Socket.APR_REMOTE, socket);
Sockaddr addr = Address.getInfo(sa);
return addr.port;
} catch (Exception ex) {
throw new IOException(ex);
}
}
throw new IOException("Socket closed");
}
public String getRemoteAddress() throws IOException {
if (socket != 0 && context.running) {
try {
long sa = Address.get(Socket.APR_REMOTE, socket);
return Address.getip(sa);
} catch (Exception ex) {
throw new IOException(ex);
}
}
throw new IOException("Socket closed");
}
public String getRemoteHostname() throws IOException {
if (socket != 0 && context.running) {
try {
long sa = Address.get(Socket.APR_REMOTE, socket);
String remoteHost = Address.getnameinfo(sa, 0);
if (remoteHost == null) {
remoteHost = Address.getip(sa);
}
return remoteHost;
} catch (Exception ex) {
throw new IOException(ex);
}
}
throw new IOException("Socket closed");
}
public int getLocalPort() throws IOException {
if (socket != 0 && context.running) {
try {
long sa = Address.get(Socket.APR_LOCAL, socket);
Sockaddr addr = Address.getInfo(sa);
return addr.port;
} catch (Exception ex) {
throw new IOException(ex);
}
}
throw new IOException("Socket closed");
}
public String getLocalAddress() throws IOException {
if (socket != 0 && context.running) {
try {
long sa = Address.get(Socket.APR_LOCAL, socket);
return Address.getip(sa);
} catch (Exception ex) {
throw new IOException(ex);
}
}
throw new IOException("Socket closed");
}
public String getLocalHostname() throws IOException {
if (socket != 0 && context.running) {
try {
long sa = Address.get(Socket.APR_LOCAL, socket);
return Address.getnameinfo(sa, 0);
} catch (Exception ex) {
throw new IOException(ex);
}
}
throw new IOException("Socket closed");
}
public boolean isBlocking() {
return ! (handler instanceof AprSocketContext.NonBlockingPollHandler);
}
public boolean isError() {
return checkPreConnect(ERROR);
}
void notifyError(Throwable err, boolean needsThread) {
if (handler instanceof AprSocketContext.NonBlockingPollHandler) {
if (err != null) {
((AprSocketContext.NonBlockingPollHandler) handler).error(this, err);
}
} else {
// poller destroyed, etc
if (needsThread) {
context.getExecutor().execute(this);
} else {
try {
notifyIO();
} catch (IOException e) {
log.log(Level.SEVERE, this + " error ", e);
}
}
}
}
// Called after connect and from poller.
void notifyIO() throws IOException {
long t0 = System.currentTimeMillis();
try {
if (handler != null) {
handler.process(this, true, false, false);
}
} catch (Throwable t) {
throw new IOException(t);
} finally {
long t1 = System.currentTimeMillis();
t1 -= t0;
if (t1 > context.maxHandlerTime.get()) {
context.maxHandlerTime.set(t1);
}
context.totalHandlerTime.addAndGet(t1);
context.handlerCount.incrementAndGet();
}
}
private void notifyConnected(boolean server) throws IOException {
// Will set the handler on the channel for accepted
context.onSocket(this);
if (handler instanceof AprSocketContext.NonBlockingPollHandler) {
((AprSocketContext.NonBlockingPollHandler) handler).connected(this);
((AprSocketContext.NonBlockingPollHandler) handler).process(this, true, true, false);
// Now register for polling - unless process() set suspendRead and
// doesn't need out notifications
updatePolling();
} else {
if (server) {
// client will block in connect().
// Server: call process();
notifyIO();
}
}
}
private void updatePolling() throws IOException {
synchronized (this) {
if ((status & CLOSED) != 0) {
maybeDestroy();
return;
}
}
context.findPollerAndAdd(this);
}
@Override
public void run() {
if (!context.running) {
return;
}
if (checkPreConnect(CLOSED)) {
if (handler != null) {
handler.closed(this);
}
return;
}
if (!checkPreConnect(CONNECTED)) {
if (checkBitAndSocket(ACCEPTED)) {
try {
context.open.incrementAndGet();
if (log.isLoggable(Level.FINE)) {
log.info("Accept: " + context.open.get() + " " + this + " " +
getRemotePort());
}
if (context.tcpNoDelay) {
Socket.optSet(socket, Socket.APR_TCP_NODELAY, 1);
}
setStatus(CONNECTED);
if (context.sslMode) {
Socket.timeoutSet(socket,
context.connectTimeout * 1000L);
blockingStartTLS();
}
setNonBlocking(); // call again, to set the bits ( connect was blocking )
notifyConnected(true);
return;
} catch (Throwable t) {
t.printStackTrace(); // no error handler yet
reset();
notifyError(t, false);
return;
}
}
if (checkPreConnect(CONNECTING)) {
// Non-blocking connect - will call 'afterConnection' at the end.
try {
context.connectBlocking(this);
} catch (IOException t) {
reset(); // also sets status ERROR
if (handler instanceof AprSocketContext.NonBlockingPollHandler) {
((AprSocketContext.NonBlockingPollHandler) handler).process(this, false, false, true);
}
notifyError(t, false);
}
}
} else {
if (handler != null) {
try {
notifyIO();
} catch (Throwable e) {
log.log(Level.SEVERE, this + " error ", e);
reset();
// no notifyIO - just did it.
}
}
}
}
/**
* This is a blocking call ! ( can be made non-blocking, but too complex )
*
* Will be called automatically after connect() or accept if 'secure' is
* true.
*
* Can be called manually to upgrade the channel
* @throws IOException
*/
public void blockingStartTLS() throws IOException {
synchronized(this) {
if (socket == 0 || !context.running) {
return;
}
if ((status & SSL_ATTACHED) != 0) {
return;
}
status |= SSL_ATTACHED;
}
try {
if (log.isLoggable(Level.FINE)) {
log.info(this + " StartSSL");
}
AprSocketContext aprCon = context;
SSLSocket.attach(aprCon.getSslCtx(), socket);
if (context.debugSSL) {
SSLExt.debug(socket);
}
if (!getContext().isServer()) {
if (context.USE_TICKETS && hostInfo.ticketLen > 0) {
SSLExt.setTicket(socket, hostInfo.ticket,
hostInfo.ticketLen);
} else if (hostInfo.sessDer != null) {
SSLExt.setSessionData(socket, hostInfo.sessDer,
hostInfo.sessDer.length);
}
}
SSLExt.sslSetMode(socket, SSLExt.SSL_MODE_ENABLE_PARTIAL_WRITE |
SSLExt.SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
int rc = SSLSocket.handshake(socket);
// At this point we have the session ID, remote certs, etc
// we can lookup host info
if (hostInfo == null) {
hostInfo = new HostInfo();
}
if (rc != Status.APR_SUCCESS) {
throw new IOException(this + " Handshake failed " + rc + " "
+ Error.strerror(rc) + " SSLL "
+ SSL.getLastError());
} else { // SUCCESS
handshakeDone();
}
} catch (IOException e) {
throw e;
} catch (Exception e) {
throw new IOException(e);
}
}
private void handshakeDone() throws IOException {
getHost();
if (socket == 0 || !context.running) {
throw new IOException("Socket closed");
}
if (context.USE_TICKETS && ! context.isServer()) {
if (hostInfo.ticket == null) {
hostInfo.ticket = new byte[2048];
}
int ticketLen = SSLExt.getTicket(socket, hostInfo.ticket);
if (ticketLen > 0) {
hostInfo.ticketLen = ticketLen;
if (log.isLoggable(Level.FINE)) {
log.info("Received ticket: " + ticketLen);
}
}
}
// TODO: if the ticket, session id or session changed - callback to
// save the session again
try {
hostInfo.sessDer = SSLExt.getSessionData(socket);
getPeerCert(true);
hostInfo.sessionId = SSLSocket.getInfoS(socket,
SSL.SSL_INFO_SESSION_ID);
} catch (Exception e) {
throw new IOException(e);
}
hostInfo.npn = new byte[32];
hostInfo.npnLen = SSLExt.getNPN(socket, hostInfo.npn);
// If custom verification is used - should check the certificates
if (context.tlsCertVerifier != null) {
context.tlsCertVerifier.handshakeDone(this);
}
}
int requestedPolling() {
synchronized(this) {
if (socket == 0 || ((status & CLOSED) != 0)) {
return 0;
}
// Implicit:
//Poll.APR_POLLNVAL | Poll.APR_POLLHUP | Poll.APR_POLLERR |
int res = 0;
if ((status & POLLIN) != 0) {
res = Poll.APR_POLLIN;
}
if ((status & POLLOUT) != 0) {
res |= Poll.APR_POLLOUT;
}
return res;
}
}
boolean checkBitAndSocket(int bit) {
synchronized (this) {
return ((status & bit) != 0 && socket != 0 &&
(status & CLOSED) == 0 && context.running);
}
}
boolean checkPreConnect(int bit) {
synchronized (this) {
return ((status & bit) != 0);
}
}
void clearStatus(int bit) {
synchronized (this) {
status &= ~bit;
}
}
boolean setStatus(int bit) {
synchronized (this) {
int old = status & bit;
status |= bit;
return old != 0;
}
}
}
|