File: AprSocketContext.java

package info (click to toggle)
tomcat8 8.0.14-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 33,280 kB
  • sloc: java: 303,023; xml: 46,103; jsp: 3,182; sh: 1,358; perl: 269; makefile: 114
file content (1348 lines) | stat: -rw-r--r-- 46,474 bytes parent folder | download | duplicates (3)
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
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
/*
 *  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.IOException;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
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.Library;
import org.apache.tomcat.jni.OS;
import org.apache.tomcat.jni.Poll;
import org.apache.tomcat.jni.Pool;
import org.apache.tomcat.jni.SSL;
import org.apache.tomcat.jni.SSLContext;
import org.apache.tomcat.jni.SSLExt;
import org.apache.tomcat.jni.Socket;
import org.apache.tomcat.jni.Status;

public class AprSocketContext {
    /**
     * Called when a chunk of data is sent or received. This is very low
     * level, used mostly for debugging or stats.
     */
    public static interface RawDataHandler {
        public void rawData(AprSocket ch, boolean input, byte[] data, int pos,
                int len, int requested, boolean closed);
    }

    /**
     * Called in SSL mode after the handshake is completed.
     *
     * @see AprSocketContext#customVerification(TlsCertVerifier)
     */
    public static interface TlsCertVerifier {
        public void handshakeDone(AprSocket ch);
    }

    /**
     * Delegates loading of persistent info about a host - public certs,
     * tickets, config, persistent info etc.
     */
    public static interface HostInfoLoader {
        public HostInfo getHostInfo(String name, int port, boolean ssl);
    }

    private static final Logger log = Logger.getLogger("AprSocketCtx");

    // If interrupt() or thread-safe poll update are not supported - the
    // poll updates will happen after the poll() timeout.
    // The poll timeout with interrupt/thread safe updates can be much higher/
    private static final int FALLBACK_POLL_TIME = 2000;

    // It seems to send the ticket, get server helo / ChangeCipherSpec, but than
    // SSL3_GET_RECORD:decryption failed or bad record mac in s3_pkt.c:480:
    // Either bug in openssl, or some combination of ciphers - needs more debugging.
    // ( this can save a roundtrip and CPU on TLS handshake )
    boolean USE_TICKETS = false;

    private final AprSocket END = new AprSocket(this);

    private static final AtomicInteger contextNumber = new AtomicInteger();
    private int contextId;

    private final AtomicInteger threadNumber = new AtomicInteger();

    /**
     * For now - single acceptor thread per connector.
     */
    private AcceptorThread acceptor;
    private AcceptorDispatchThread acceptorDispatch;

    // APR/JNI is thread safe
    private boolean threadSafe = true;

    /**
     * Pollers.
     */
    private final List<AprPoller> pollers = new ArrayList<>();

    // Set on all accepted or connected sockets.
    // TODO: add the other properties
    boolean tcpNoDelay = true;

    protected boolean running = true;

    protected boolean sslMode;

    // onSocket() will be called in accept thread.
    // If false: use executor ( but that may choke the acceptor thread )
    private boolean nonBlockingAccept = false;

    private final BlockingQueue<AprSocket> acceptedQueue =
            new LinkedBlockingQueue<>();

    /**
     * Root APR memory pool.
     */
    private long rootPool = 0;

    /**
     * SSL context.
     */
    private volatile long sslCtx = 0;

    TlsCertVerifier tlsCertVerifier;

    //
    final int connectTimeout =  20000;
    final int defaultTimeout = 100000;
    // TODO: Use this
    final int keepAliveTimeout = 20000;

    final AtomicInteger open = new AtomicInteger();

    /**
     * Poll interval, in microseconds. If the platform doesn't support
     * poll interrupt - it'll take this time to stop the poller.
     *
     */
    private int pollTime = 5 * 1000000;

    private HostInfoLoader hostInfoLoader;

    final RawDataHandler rawDataHandler = null;

    // TODO: do we need this here ?
    private final Map<String, HostInfo> hosts = new HashMap<>();

    private String certFile;
    private String keyFile;

    private byte[] spdyNPN;

    private byte[] ticketKey;

    // For resolving DNS ( i.e. connect ), callbacks
    private ExecutorService threadPool;

    // Separate executor for connect/handshakes
    final ExecutorService connectExecutor;

    final boolean debugSSL = false;
    private boolean debugPoll = false;

    private boolean deferAccept = false;

    private int backlog = 100;

    private boolean useSendfile;

    private int sslProtocol = SSL.SSL_PROTOCOL_TLSV1 | SSL.SSL_PROTOCOL_SSLV3;

    /**
     * Max time spent in a callback ( will be longer for blocking )
     */
    final AtomicLong maxHandlerTime = new AtomicLong();
    final AtomicLong totalHandlerTime = new AtomicLong();
    final AtomicLong handlerCount = new AtomicLong();

    /**
     * Total connections handled ( accepted or connected ).
     */
    private final AtomicInteger connectionsCount = new AtomicInteger();


    public AprSocketContext() {
        connectExecutor =new ThreadPoolExecutor(0, 64, 5, TimeUnit.SECONDS,
                new LinkedBlockingQueue<Runnable>(), new RejectedExecutionHandler() {
                    @Override
                    public void rejectedExecution(Runnable r,
                            java.util.concurrent.ThreadPoolExecutor executor) {
                        AprSocket s = (AprSocket) r;
                        log.severe("Rejecting " + s);
                        s.reset();
                    }
                });
        contextId = contextNumber.incrementAndGet();
    }

    /**
     * Poller thread count.
     */
    private int pollerThreadCount = 4;
    public void setPollerThreadCount(int pollerThreadCount) { this.pollerThreadCount = pollerThreadCount; }
    public int getPollerThreadCount() { return pollerThreadCount; }

    // to test the limits - default should be lower
    private int maxConnections = 64 * 1024;
    public void setMaxconnections(int maxCon) {
        this.maxConnections = maxCon;
    }

    public void setBacklog(int backlog) { if (backlog > 0) this.backlog = backlog; }
    public int getBacklog() { return backlog; }

    /**
     * Defer accept.
     */
    public void setDeferAccept(boolean deferAccept) { this.deferAccept = deferAccept; }
    public boolean getDeferAccept() { return deferAccept; }

    /**
     * For client:
     *   - ClientHello will include the npn extension ( the ID == 0x3374)
     *   - if ServerHello includes a list of protocols - select one
     *   - send it after ChangeCipherSpec and before Finish
     *
     *  For server:
     *   - if ClientHello includes the npn extension
     *    -- will send this string as list of supported protocols in ServerHello
     *   - read the selection before Finish.
     * @param npn
     */
    public void setNpn(String npn) {
        byte[] data = npn.getBytes();
        byte[] npnB = new byte[data.length + 2];

        System.arraycopy(data, 0, npnB, 1, data.length);
        npnB[0] = (byte) data.length;
        npnB[npnB.length - 1] = 0;
        spdyNPN = npnB;

    }

    public void setNpn(byte[] data) {
        spdyNPN = data;
    }

    public void setHostLoader(HostInfoLoader handler) {
        this.hostInfoLoader = handler;
    }

    public boolean isServer() {
        return acceptor != null;
    }

    protected Executor getExecutor() {
        if (threadPool == null) {
            threadPool = Executors.newCachedThreadPool(new ThreadFactory( ) {
                @Override
                public Thread newThread(Runnable r) {
                    Thread t = new Thread(r, "AprThread-" + contextId + "-" +
                            threadNumber.incrementAndGet());
                    t.setDaemon(true);
                    return t;
                }
            });
        }
        return threadPool;
    }

    /**
     * All accepted/connected sockets will start handshake automatically.
     */
    public AprSocketContext setTls() {
        this.sslMode = true;
        return this;
    }

    public void setTcpNoDelay(boolean b) {
        tcpNoDelay = b;
    }

    public void setSslProtocol(String protocol) {
        protocol = protocol.trim();
        if ("SSLv2".equalsIgnoreCase(protocol)) {
            sslProtocol = SSL.SSL_PROTOCOL_SSLV2;
        } else if ("SSLv3".equalsIgnoreCase(protocol)) {
            sslProtocol = SSL.SSL_PROTOCOL_SSLV3;
        } else if ("TLSv1".equalsIgnoreCase(protocol)) {
            sslProtocol = SSL.SSL_PROTOCOL_TLSV1;
        } else if ("all".equalsIgnoreCase(protocol)) {
            sslProtocol = SSL.SSL_PROTOCOL_ALL;
        }
    }

    public void setTicketKey(byte[] key48Bytes) {
        if(key48Bytes.length != 48) {
            throw new RuntimeException("Key must be 48 bytes");
        }
        this.ticketKey = key48Bytes;
    }

    public void customVerification(TlsCertVerifier verifier) {
        tlsCertVerifier = verifier;
    }

    // TODO: should have a separate method for switching to tls later.
    /**
     * Set certificate, will also enable TLS mode.
     */
    public AprSocketContext setKeys(String certPemFile, String keyDerFile) {
        this.sslMode = true;
        setTls();
        certFile = certPemFile;
        keyFile = keyDerFile;
        return this;
    }

    /**
     * SSL cipher suite.
     */
    private String SSLCipherSuite = "ALL";
    public String getSSLCipherSuite() { return SSLCipherSuite; }
    public void setSSLCipherSuite(String SSLCipherSuite) { this.SSLCipherSuite = SSLCipherSuite; }

    /**
     * Override or use hostInfoLoader to implement persistent/memcache storage.
     */
    public HostInfo getHostInfo(String host, int port, boolean ssl) {
        if (hostInfoLoader != null) {
            return hostInfoLoader.getHostInfo(host, port, ssl);
        }
        // Use local cache
        String key = host + ":" + port;
        HostInfo pi = hosts.get(key);
        if (pi != null) {
            return pi;
        }
        pi = new HostInfo(host, port, ssl);
        hosts.put(key, pi);
        return pi;
    }

    protected void rawData(AprSocket ch, boolean inp, byte[] data, int pos,
            int len, int requested, boolean closed) {
        if (rawDataHandler != null) {
            rawDataHandler.rawData(ch, inp, data, pos, len, requested, closed);
        }
    }

    public void listen(final int port) throws IOException {
        if (acceptor != null) {
            throw new IOException("Already accepting on " + acceptor.port);
        }
        if (sslMode && certFile == null) {
            throw new IOException("Missing certificates for server");
        }
        if (sslMode || !nonBlockingAccept) {
            acceptorDispatch = new AcceptorDispatchThread();
            acceptorDispatch.setName("AprAcceptorDispatch-" + port);
            acceptorDispatch.start();
        }

        acceptor = new AcceptorThread(port);
        acceptor.prepare();
        acceptor.setName("AprAcceptor-" + port);
        acceptor.start();


    }

    /**
     * Get a socket for connectiong to host:port.
     */
    public AprSocket socket(String host, int port, boolean ssl) {
        HostInfo hi = getHostInfo(host, port, ssl);
        return socket(hi);
    }

    public AprSocket socket(HostInfo hi) {
        AprSocket sock = newSocket(this);
        sock.setHost(hi);
        return sock;
    }

    public AprSocket socket(long socket) {
        AprSocket sock = newSocket(this);
        // Tomcat doesn't set this
        SSLExt.sslSetMode(socket, SSLExt.SSL_MODE_ENABLE_PARTIAL_WRITE |
                SSLExt.SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
        sock.setStatus(AprSocket.ACCEPTED);
        sock.socket = socket;
        return sock;
    }


    void destroySocket(AprSocket socket) {
        // TODO: does it need to be done in io thread ?
        synchronized (socket) {
            if (socket.socket != 0) {
                long s = socket.socket;
                socket.socket = 0;
                log.info("DESTROY: " + Long.toHexString(s));
                Socket.destroy(s);
            }
        }
    }

    protected void connectBlocking(AprSocket apr) throws IOException {
        try {
            if (!running) {
                throw new IOException("Stopped");
            }
            HostInfo hi = apr.getHost();

            long clientSockP;
            synchronized (pollers) {
                long socketpool = Pool.create(getRootPool());

                int family = Socket.APR_INET;

                clientSockP = Socket.create(family,
                        Socket.SOCK_STREAM,
                        Socket.APR_PROTO_TCP, socketpool); // or rootPool ?
            }
            Socket.timeoutSet(clientSockP, connectTimeout * 1000);
            if (OS.IS_UNIX) {
                Socket.optSet(clientSockP, Socket.APR_SO_REUSEADDR, 1);
            }

            Socket.optSet(clientSockP, Socket.APR_SO_KEEPALIVE, 1);

            // Blocking
            // TODO: use socket pool
            // TODO: cache it ( and TTL ) in hi
            long inetAddress = Address.info(hi.host, Socket.APR_INET,
                  hi.port, 0, rootPool);
            // this may take a long time - stop/destroy must wait
            // at least connect timeout
            int rc = Socket.connect(clientSockP, inetAddress);

            if (rc != 0) {
                synchronized (pollers) {
                    Socket.close(clientSockP);
                    Socket.destroy(clientSockP);
                }
                /////Pool.destroy(socketpool);
                throw new IOException("Socket.connect(): " + rc + " " + Error.strerror(rc) + " " + connectTimeout);
            }
            if (!running) {
                throw new IOException("Stopped");
            }

            connectionsCount.incrementAndGet();
            if (tcpNoDelay) {
                Socket.optSet(clientSockP, Socket.APR_TCP_NODELAY, 1);
            }

            Socket.timeoutSet(clientSockP, defaultTimeout * 1000);

            apr.socket = clientSockP;

            apr.afterConnect();
        } catch (IOException e) {
            apr.reset();
            throw e;
        } catch (Throwable e) {
            apr.reset();
            e.printStackTrace();
            throw new IOException(e);
        }
    }

    AprSocket newSocket(AprSocketContext context) {
        return new AprSocket(context);
    }

    /**
     * To clean the pools - we could track if all channels are
     * closed, but this seems simpler and safer.
     */
    @Override
    protected void finalize() throws Throwable {
        if (rootPool != 0) {
            log.warning(this + " GC without stop()");
            try {
                stop();
            } catch (Exception e) {
                //TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        super.finalize();
    }


    public void stop() {
        synchronized (pollers) {
            if (!running) {
                return;
            }
            running = false;
        }

        if (rootPool != 0) {
            if (acceptor != null) {
                try {
                    acceptor.unblock();
                    acceptor.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            if (acceptorDispatch != null) {
                acceptedQueue.add(END);
                try {
                    acceptorDispatch.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            if (threadPool != null) {
                threadPool.shutdownNow();
            }

            log.info("Stopping pollers " + contextId);

            while (true) {
                AprPoller a;
                synchronized (pollers) {
                    if (pollers.size() == 0) {
                        break;
                    }
                    a = pollers.remove(0);
                }
                a.interruptPoll();
                try {
                    a.join();
                    log.info("Poller " + a.id + " done ");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    // Called when the last poller has been destroyed.
    void destroy() {
        synchronized (pollers) {
            if (pollers.size() != 0) {
                return;
            }

            if (rootPool == 0) {
                return;
            }
            log.info("Destroy root pool " + rootPool);
            //Pool.destroy(rootPool);
            //rootPool = 0;
        }
    }

    private static IOException noApr;
    static {

        try {
            Library.initialize(null);
            SSL.initialize(null);
        } catch (Exception e) {
            noApr = new IOException("APR not present", e);
        }

    }

    private long getRootPool() throws IOException {
        if (rootPool == 0) {
            if (noApr != null) {
                throw noApr;
            }
            // Create the root APR memory pool
            rootPool = Pool.create(0);

            // Adjust poller sizes
            if ((OS.IS_WIN32 || OS.IS_WIN64) && (maxConnections > 1024)) {
                // The maximum per poller to get reasonable performance is 1024
                pollerThreadCount = maxConnections / 1024;
                // Adjust poller size so that it won't reach the limit
                maxConnections = maxConnections - (maxConnections % 1024);
            }
        }
        return rootPool;
    }

    long getSslCtx() throws Exception {
        if (sslCtx == 0) {
            synchronized (AprSocketContext.class) {
                if (sslCtx == 0) {
                    boolean serverMode = acceptor != null;
                    sslCtx = SSLContext.make(getRootPool(),
                            sslProtocol,
                            serverMode ? SSL.SSL_MODE_SERVER : SSL.SSL_MODE_CLIENT);


                    // SSL.SSL_OP_NO_SSLv3
                    int opts = SSL.SSL_OP_NO_SSLv2 |
                        SSL.SSL_OP_SINGLE_DH_USE;

                    if (!USE_TICKETS || serverMode && ticketKey == null) {
                        opts |= SSL.SSL_OP_NO_TICKET;
                    }

                    SSLContext.setOptions(sslCtx, opts);
                    // Set revocation
                    //        SSLContext.setCARevocation(sslContext, SSLCARevocationFile, SSLCARevocationPath);

                    // Client certificate verification - maybe make it option
                    try {
                        SSLContext.setCipherSuite(sslCtx, SSLCipherSuite);


                        if (serverMode) {
                            if (ticketKey != null) {
                                //SSLExt.setTicketKeys(sslCtx, ticketKey, ticketKey.length);
                            }
                            if (certFile != null) {
                                boolean rc = SSLContext.setCertificate(sslCtx,
                                        certFile,
                                        keyFile, null, SSL.SSL_AIDX_DSA);
                                if (!rc) {
                                    throw new IOException("Can't set keys");
                                }
                            }
                            SSLContext.setVerify(sslCtx, SSL.SSL_CVERIFY_NONE, 10);

                            if (spdyNPN != null) {
                                SSLExt.setNPN(sslCtx, spdyNPN, spdyNPN.length);
                            }
                        } else {
                            if (tlsCertVerifier != null) {
                                // NONE ?
                                SSLContext.setVerify(sslCtx,
                                        SSL.SSL_CVERIFY_NONE, 10);
                            } else {
                                SSLContext.setCACertificate(sslCtx,
                                        "/etc/ssl/certs/ca-certificates.crt",
                                        "/etc/ssl/certs");
                                SSLContext.setVerify(sslCtx,
                                        SSL.SSL_CVERIFY_REQUIRE, 10);
                            }

                            if (spdyNPN != null) {
                                SSLExt.setNPN(sslCtx, spdyNPN, spdyNPN.length);
                            }
                        }
                    } catch (IOException e) {
                        throw e;
                    } catch (Exception e) {
                        throw new IOException(e);
                    }
                }
            // TODO: try release buffers
            }
        }
        return sslCtx;
    }

    void findPollerAndAdd(AprSocket ch) throws IOException {
        if (ch.poller != null) {
            ch.poller.requestUpdate(ch);
            return;
        }
        assignPoller(ch);
    }

    void assignPoller(AprSocket ch) throws IOException {
        AprPoller target = null;
        synchronized (pollers) {
            // Make sure we have min number of pollers
            int needPollers = pollerThreadCount - pollers.size();
            if (needPollers > 0) {
                for (int i = needPollers; i > 0; i--) {
                    pollers.add(allocatePoller());
                }
            }
            int max = 0;
            for (AprPoller poller: pollers) {
                int rem = poller.remaining();
                if (rem > max) {
                    target = poller;
                    max = rem;
                }
            }
        }
        if (target != null && target.add(ch)) {
            return;
        }

        // can't be added - add a new poller
        synchronized (pollers) {
            AprPoller poller = allocatePoller();
            poller.add(ch);
            pollers.add(poller);
        }
    }

    /**
     * Called on each accepted socket (for servers) or after connection (client)
     * after handshake.
     */
    protected void onSocket(@SuppressWarnings("unused") AprSocket s) {
        // Defaults to NO-OP. Parameter is used by sub-classes.
    }

    private class AcceptorThread extends Thread {
        private final int port;
        private long serverSockPool = 0;
        private long serverSock = 0;

        private long inetAddress;

        AcceptorThread(int port) {
            this.port = port;
            setDaemon(true);
        }

        void prepare() throws IOException {
            try {
                // Create the pool for the server socket
                serverSockPool = Pool.create(getRootPool());

                int family = Socket.APR_INET;
                inetAddress =
                        Address.info(null, family, port, 0, serverSockPool);

                // Create the APR server socket
                serverSock = Socket.create(family,
                        Socket.SOCK_STREAM,
                        Socket.APR_PROTO_TCP, serverSockPool);

                if (OS.IS_UNIX) {
                    Socket.optSet(serverSock, Socket.APR_SO_REUSEADDR, 1);
                }
                // Deal with the firewalls that tend to drop the inactive sockets
                Socket.optSet(serverSock, Socket.APR_SO_KEEPALIVE, 1);
                // Bind the server socket
                int ret = Socket.bind(serverSock, inetAddress);
                if (ret != 0) {
                    throw new IOException("Socket.bind " + ret + " " +
                            Error.strerror(ret) + " port=" + port);
                }
                // Start listening on the server socket
                ret = Socket.listen(serverSock, backlog );
                if (ret != 0) {
                    throw new IOException("endpoint.init.listen"
                            + ret + " " + Error.strerror(ret));
                }
                if (OS.IS_WIN32 || OS.IS_WIN64) {
                    // On Windows set the reuseaddr flag after the bind/listen
                    Socket.optSet(serverSock, Socket.APR_SO_REUSEADDR, 1);
                }

                // Sendfile usage on systems which don't support it cause major problems
                if (useSendfile && !Library.APR_HAS_SENDFILE) {
                    useSendfile = false;
                }

                // Delay accepting of new connections until data is available
                // Only Linux kernels 2.4 + have that implemented
                // on other platforms this call is noop and will return APR_ENOTIMPL.
                if (deferAccept) {
                    if (Socket.optSet(serverSock, Socket.APR_TCP_DEFER_ACCEPT, 1) == Status.APR_ENOTIMPL) {
                        deferAccept = false;
                    }
                }
            } catch (Throwable t) {
                throw new IOException(t);
            }
        }

        void unblock() {
            try (java.net.Socket sock = new java.net.Socket()) {
                // Easiest ( maybe safest ) way to interrupt accept
                // we could have it in non-blocking mode, etc
                sock.connect(new InetSocketAddress("127.0.0.1", port));
            } catch (Exception ex) {
                // ignore - the acceptor may have shut down by itself.
            }
        }

        @Override
        public void run() {
            while (running) {
                try {
                    // each socket has a pool.
                    final AprSocket ch = newSocket(AprSocketContext.this);
                    ch.setStatus(AprSocket.ACCEPTED);

                    ch.socket = Socket.accept(serverSock);
                    if (!running) {
                        break;
                    }
                    connectionsCount.incrementAndGet();
                    if (connectionsCount.get() % 1000 == 0) {
                        System.err.println("Accepted: " + connectionsCount.get());
                    }

                    if (nonBlockingAccept && !sslMode) {
                        ch.setStatus(AprSocket.CONNECTED);
                        // TODO: SSL really needs a thread.
                        onSocket(ch);
                    } else {
                        acceptedQueue.add(ch);
                    }
                } catch (Throwable e) {
                    e.printStackTrace();
                }
            }
            Socket.close(serverSock);
        }
    }

    private class AcceptorDispatchThread extends Thread {

        AcceptorDispatchThread() {
            setDaemon(true);
        }

        @Override
        public void run() {
            while(running) {
                try {
                    AprSocket ch = acceptedQueue.take();
                    if (ch == END) {
                        return;
                    }
                    connectExecutor.execute(ch);
                } catch (InterruptedException e) {
                }
            }
        }
    }

    /**
     * Create the poller. With some versions of APR, the maximum poller size will
     * be 62 (recompiling APR is necessary to remove this limitation).
     * @throws IOException
     */
    AprPoller allocatePoller() throws IOException {
        long pool = Pool.create(getRootPool());
        int size = maxConnections / pollerThreadCount;

        long serverPollset = allocatePoller(size, pool);

        if (serverPollset == 0 && size > 1024) {
            log.severe("Falling back to 1024-sized poll, won't scale");
            size = 1024;
            serverPollset = allocatePoller(size, pool);
        }
        if (serverPollset == 0) {
            log.severe("Falling back to 62-sized poll, won't scale");
            size = 62;
            serverPollset = allocatePoller(size, pool);
        }

        AprPoller res = new AprPoller();
        res.pool = pool;
        res.serverPollset = serverPollset;
        res.desc = new long[size * 2];
        res.size = size;
        res.id = contextId++;
        res.setDaemon(true);
        res.setName("AprPoller-" + res.id);
        res.start();
        if (debugPoll && !sizeLogged) {
            sizeLogged = true;
            log.info("Poller size " + (res.desc.length / 2));
        }
        return res;
    }

    // Removed the 'thread safe' updates for now, to simplify the code
    // last test shows a small improvement, can switch later.
    private static boolean sizeLogged = false;

    protected long allocatePoller(int size, long pool) {
        int flag = threadSafe ? Poll.APR_POLLSET_THREADSAFE: 0;
        for (int i = 0; i < 2; i++) {
            try {
                //  timeout must be -1 - or ttl will take effect, strange results.
                return Poll.create(size, pool, flag, -1);
            } catch (Error e) {
                e.printStackTrace();
                if (Status.APR_STATUS_IS_EINVAL(e.getError())) {
                    log.info(" endpoint.poll.limitedpollsize " + size);
                    return 0;
                } else if (Status.APR_STATUS_IS_ENOTIMPL(e.getError())) {
                    // thread safe not supported
                    log.severe("THREAD SAFE NOT SUPPORTED" + e);
                    threadSafe = false;
                    // try again without the flags
                    continue;
                } else {
                    log.severe("endpoint.poll.initfail" + e);
                    return 0;
                }
            }
        }
        log.severe("Unexpected ENOTIMPL with flag==0");
        return 0;
    }

    class AprPoller extends Thread {

        private int id;
        private int size;
        private long serverPollset = 0;
        private long pool = 0;
        private long[] desc;

        private long lastPoll;
        private long lastPollTime;
        private final AtomicBoolean inPoll = new AtomicBoolean(false);

        // Should be replaced with socket data.
        // used only to lookup by socket
        private final Map<Long, AprSocket> channels = new HashMap<>();

        // Active + pending, must be < desc.length / 2
        // The channel will also have poller=this when active or pending
        // How many sockets have poller == this
        private final AtomicInteger keepAliveCount = new AtomicInteger();
        // Tracks desc, how many sockets are actively polled
        private final AtomicInteger polledCount = new AtomicInteger();

        private final AtomicInteger pollCount = new AtomicInteger();

        private final List<AprSocket> updates = new ArrayList<>();

        @Override
        public void run() {
            if (!running) {
                return;
            }
            if (debugPoll) {
                log.info("Starting poller " + id + " " + (isServer() ? "SRV ": "CLI "));
            }
            long t0 = System.currentTimeMillis();
            while (running) {
                try {
                    updates();

                    // Pool for the specified interval. Remove signaled sockets
                    synchronized (this) {
                        inPoll.set(true);
                    }
                    // if updates are added after updates and poll - interrupt will have still
                    // work

                    int rv = Poll.poll(serverPollset, pollTime, desc, true);
                    synchronized (this) {
                        inPoll.set(false);
                        if (!running) {
                            break;
                        }
                    }

                    pollCount.incrementAndGet();
                    lastPoll = System.currentTimeMillis();
                    lastPollTime = lastPoll - t0;

                    if (rv > 0) {
                        if (debugPoll) {
                            log.info(" Poll() id=" + id + " rv=" + rv + " keepAliveCount=" + keepAliveCount +
                                    " polled = " + polledCount.get()
                                    + " time=" + lastPollTime);
                        }
                        polledCount.addAndGet(-rv);
                        for (int pollIdx = 0; pollIdx < rv; pollIdx++) {
                            long sock = desc[pollIdx * 2 + 1];
                            AprSocket ch;
                            boolean blocking = false;

                            synchronized (channels) {
                                ch = channels.get(Long.valueOf(sock));
                                if (ch != null) {
                                    blocking = ch.isBlocking();
                                } else {
                                    log.severe("Polled socket not found !!!!!" + Long.toHexString(sock));
                                    // TODO: destroy/close the raw socket
                                    continue;
                                }
                            }
                            // was removed from polling
                            ch.clearStatus(AprSocket.POLL);

                            // We just removed it ( see last param to poll()).
                            // Check for failed sockets and hand this socket off to a worker
                            long mask = desc[pollIdx * 2];

                            boolean err = ((mask & Poll.APR_POLLERR) == Poll.APR_POLLERR);
                            boolean nval = ((mask & Poll.APR_POLLNVAL) != 0);
                            if (err || nval) {
                                System.err.println("ERR " + err + " NVAL " + nval);
                            }

                            boolean out = (mask & Poll.APR_POLLOUT) == Poll.APR_POLLOUT;
                            boolean in = (mask & Poll.APR_POLLIN) == Poll.APR_POLLIN;
                            if (debugPoll) {
                                log.info(" Poll channel: " + Long.toHexString(mask) +
                                        (out ? " OUT" :"") +
                                        (in ? " IN": "") +
                                        (err ? " ERR" : "") +
                                        " Ch: " + ch);
                            }

                            // will be set again in process(), if all read/write is done
                            ch.clearStatus(AprSocket.POLLOUT);
                            ch.clearStatus(AprSocket.POLLIN);

                            // try to send if needed
                            if (blocking) {
                                synchronized (ch) {
                                    ch.notifyAll();
                                }
                                getExecutor().execute(ch);
                            } else {
                                ((AprSocketContext.NonBlockingPollHandler) ch.handler).process(ch, in, out, false);

                                // Update polling for the channel (in IO thread, safe)
                                updateIOThread(ch);
                            }
                        }
                    } else if (rv < 0) {
                        int errn = -rv;
                        if (errn == Status.TIMEUP) {
                            // to or interrupt
//                            if (debugPoll) {
//                                log.info(" Poll() timeup" + " keepAliveCount=" + keepAliveCount +
//                                        " polled = " + polledCount.get()
//                                        + " time=" + lastPollTime);
//                            }
                        } else if (errn == Status.EINTR) {
                            // interrupt - no need to log
                        } else {
                            if (debugPoll) {
                                log.info(" Poll() rv=" + rv + " keepAliveCount=" + keepAliveCount +
                                        " polled = " + polledCount.get()
                                        + " time=" + lastPollTime);
                            }
                            /* Any non timeup or interrupted error is critical */
                            if (errn >  Status.APR_OS_START_USERERR) {
                                errn -=  Status.APR_OS_START_USERERR;
                            }
                            log.severe("endpoint.poll.fail " + errn + " " + Error.strerror(errn));
                            // Handle poll critical failure
                            synchronized (this) {
                                destroyPoller(); // will close all sockets
                            }
                            continue;
                        }
                    }
                    // TODO: timeouts
                } catch (Throwable t) {
                    log.log(Level.SEVERE, "endpoint.poll.error", t);
                }

            }
            if (!running) {
                destroyPoller();
            }
        }

        /**
                 * Destroy the poller.
                 */
        protected void destroyPoller() {
            synchronized (pollers) {
                pollers.remove(this);
            }
            log.info("Poller stopped after cnt=" +
                    pollCount.get() +
                    " sockets=" + channels.size() +
                    " lastPoll=" + lastPoll);

            // Close all sockets
            synchronized (this)  {
                if (serverPollset == 0) {
                    return;
                }

//                for (AprSocket ch: channels.values()) {
//                    ch.poller = null;
//                    ch.reset();
//                }
                keepAliveCount.set(0);
                log.warning("Destroy pollset");
                //serverPollset = 0;
            }
            Pool.destroy(pool);
            pool = 0;
            synchronized (pollers) {
                // Now we can destroy the root pool
                if (pollers.size() == 0 && !running) {
                    log.info("Destroy server context");
//                    AprSocketContext.this.destroy();
                }
            }
        }

        /**
         * Called only in poller thread, only used if not thread safe
         * @throws IOException
         */
        protected void updates() throws IOException {
            synchronized (this) {
                for (AprSocket up: updates) {
                    updateIOThread(up);
                }
                updates.clear();
            }
        }

        void interruptPoll() {
            try {
                int rc = Status.APR_SUCCESS;
                synchronized (this) {
                    if (serverPollset != 0) {
                        rc = Poll.interrupt(serverPollset);
                    } else {
                        log.severe("Interrupt with closed pollset");
                    }
                }
                if (rc != Status.APR_SUCCESS) {
                    log.severe("Failed interrupt and not thread safe");
                }
            } catch (Throwable t) {
                t.printStackTrace();
                if (pollTime > FALLBACK_POLL_TIME) {
                    pollTime = FALLBACK_POLL_TIME;
                }
            }
        }


        int remaining() {
            synchronized (channels) {
                return (desc.length - channels.size() * 2);
            }
        }



        /**
         * Called from any thread, return true if we could add it
         * to pending.
         */
        boolean add(AprSocket ch) throws IOException {
            synchronized (this) {
                if (!running) {
                    return false;
                }
                if (keepAliveCount.get() >= size) {
                    return false;
                }
                keepAliveCount.incrementAndGet();
                ch.poller = this;
            }

            requestUpdate(ch);

            return true;
        }

        /**
         * May be called outside of IOThread.
         */
        protected void requestUpdate(AprSocket ch) throws IOException {
            synchronized (this) {
                if (!running) {
                    return;
                }
            }
            if (isPollerThread()) {
                updateIOThread(ch);
            } else {
                synchronized (this) {
                    if (!updates.contains(ch)) {
                        updates.add(ch);
                    }
                    interruptPoll();
                }
                if (debugPoll) {
                    log.info("Poll: requestUpdate " + id + " " + ch);
                }
            }
        }

        private void updateIOThread(AprSocket ch) throws IOException {
            if (!running || ch.socket == 0) {
                return;
            }
            // called from IO thread, either in 'updates' or after
            // poll.
            //synchronized (ch)
            boolean polling = ch.checkPreConnect(AprSocket.POLL);

            int requested = ch.requestedPolling();
            if (requested == 0) {
                if (polling) {
                    removeSafe(ch);
                }
                if (ch.isClosed()) {
                    synchronized (channels) {
                        ch.poller = null;
                        channels.remove(Long.valueOf(ch.socket));
                    }
                    keepAliveCount.decrementAndGet();
                    ch.reset();
                }
            } else {
                if (polling) {
                    removeSafe(ch);
                }
                // will close if error
                pollAdd(ch, requested);
            }
            if (debugPoll) {
                log.info("Poll: updated=" + id + " " + ch);
            }
        }

        /**
         * Called only from IO thread
         */
        private void pollAdd(AprSocket up, int req) throws IOException {
            boolean failed = false;
            int rv;
            synchronized (channels) {
                if (up.isClosed()) {
                    return;
                }
                rv = Poll.add(serverPollset, up.socket, req);
                if (rv != Status.APR_SUCCESS) {
                    up.poller = null;
                    keepAliveCount.decrementAndGet();
                    failed = true;
                } else {
                    polledCount.incrementAndGet();
                    channels.put(Long.valueOf(up.socket), up);
                    up.setStatus(AprSocket.POLL);
                }
            }
            if (failed) {
                up.reset();
                throw new IOException("poll add error " +  rv + " " + up + " " + Error.strerror(rv));
            }
        }

        /**
         * Called only from IO thread. Remove from Poll and channels,
         * set POLL bit to false.
         */
        private void removeSafe(AprSocket up) {
            int rv = Status.APR_EGENERAL;
            if (running && serverPollset != 0 && up.socket != 0
                    && !up.isClosed()) {
                rv = Poll.remove(serverPollset, up.socket);
            }
            up.clearStatus(AprSocket.POLL);

            if (rv != Status.APR_SUCCESS) {
                log.severe("poll remove error " +  Error.strerror(rv) + " " + up);
            } else {
                polledCount.decrementAndGet();
            }
        }


        public boolean isPollerThread() {
            return Thread.currentThread() == this;
        }

    }

    /**
     * Callback for poll events, will be invoked in a thread pool.
     *
     */
    public static interface BlockingPollHandler {

        /**
         * Called when the socket has been polled for in, out or closed.
         *
         *
         */
        public void process(AprSocket ch, boolean in, boolean out, boolean close);


        /**
         *  Called just before the socket is destroyed
         */
        public void closed(AprSocket ch);
    }

    /**
     *  Additional callbacks for non-blocking.
     *  This can be much faster - but it's harder to code, should be used only
     *  for low-level protocol implementation, proxies, etc.
     *
     *  The model is restricted in many ways to avoid complexity and bugs:
     *
     *  - read can only happen in the IO thread associated with the poller
     *  - user doesn't control poll interest - it is set automatically based
     *  on read/write results
     *  - it is only possible to suspend read, for TCP flow control - also
     *  only from the IO thread. Resume can happen from any thread.
     *  - it is also possible to call write() from any thread
     */
    public static interface NonBlockingPollHandler extends BlockingPollHandler {

        /**
         * Called after connection is established, in a thread pool.
         * Process will be called next.
         */
        public void connected(AprSocket ch);

        /**
         * Before close, if an exception happens.
         */
        public void error(AprSocket ch, Throwable t);
    }

}