Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 1 | /* |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | * contributor license agreements. See the NOTICE file distributed with |
| 4 | * this work for additional information regarding copyright ownership. |
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | * (the "License"); you may not use this file except in compliance with |
| 7 | * the License. You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 17 | package org.apache.felix.shell.remote; |
| 18 | |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 19 | import java.io.IOException; |
| 20 | import java.io.PrintStream; |
Felix Meschberger | 3590103 | 2009-01-30 22:43:37 +0000 | [diff] [blame] | 21 | import java.net.InetAddress; |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 22 | import java.net.ServerSocket; |
| 23 | import java.net.Socket; |
| 24 | import java.net.SocketException; |
Felix Meschberger | 5182570 | 2009-02-02 07:07:38 +0000 | [diff] [blame] | 25 | import java.net.SocketTimeoutException; |
Felix Meschberger | 73bf123 | 2009-02-02 08:28:42 +0000 | [diff] [blame] | 26 | import java.util.HashSet; |
| 27 | import java.util.Set; |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 28 | |
Felix Meschberger | d7c324d | 2008-08-19 14:10:48 +0000 | [diff] [blame] | 29 | import org.osgi.framework.BundleContext; |
| 30 | |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 31 | /** |
| 32 | * Implements a simple listener that will accept a single connection. |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 33 | */ |
| 34 | class Listener |
| 35 | { |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 36 | private final int m_port; |
| 37 | private final String m_ip; |
| 38 | private final Thread m_listenerThread; |
| 39 | private final Acceptor m_acceptor; |
| 40 | private final AtomicInteger m_useCounter; |
| 41 | private final int m_maxConnections; |
| 42 | private final int m_soTimeout; |
| 43 | private final Set m_connections; |
| 44 | private final ServiceMediator m_services; |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 45 | |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 46 | /** |
| 47 | * Activates this listener on a listener thread (telnetconsole.Listener). |
| 48 | */ |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 49 | public Listener(BundleContext context, ServiceMediator services) throws IOException |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 50 | { |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 51 | m_services = services; |
Felix Meschberger | d7c324d | 2008-08-19 14:10:48 +0000 | [diff] [blame] | 52 | //configure from framework property |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 53 | m_ip = getProperty(context, "osgi.shell.telnet.ip", "127.0.0.1"); |
| 54 | m_port = getProperty(context, "osgi.shell.telnet.port", 6666); |
| 55 | m_soTimeout = getProperty(context, "osgi.shell.telnet.socketTimeout", 0); |
| 56 | m_maxConnections = getProperty(context, "osgi.shell.telnet.maxconn", 2); |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 57 | m_useCounter = new AtomicInteger(0); |
Felix Meschberger | 73bf123 | 2009-02-02 08:28:42 +0000 | [diff] [blame] | 58 | m_connections = new HashSet(); |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 59 | m_acceptor = new Acceptor(); |
| 60 | m_listenerThread = new Thread(m_acceptor, "telnetconsole.Listener"); |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 61 | m_listenerThread.start(); |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 62 | }//activate |
Richard S. Hall | 8a277a7 | 2010-08-24 14:11:47 +0000 | [diff] [blame] | 63 | |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 64 | public ServiceMediator getServices() |
| 65 | { |
| 66 | return m_services; |
| 67 | } |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 68 | |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 69 | /** |
| 70 | * Deactivates this listener. |
| 71 | * <p/> |
| 72 | * The listener's socket will be closed, which should cause an interrupt in the |
| 73 | * listener thread and allow for it to return. The calling thread joins the listener |
| 74 | * thread until it returns (to ensure a clean stop). |
| 75 | */ |
| 76 | public void deactivate() |
| 77 | { |
| 78 | try |
| 79 | { |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 80 | //wait for the listener thread |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 81 | m_acceptor.close(); |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 82 | m_listenerThread.join(); |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 83 | } |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 84 | catch (Exception ex) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 85 | { |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 86 | m_services.error("Listener::deactivate()", ex); |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 87 | } |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 88 | |
Felix Meschberger | 73bf123 | 2009-02-02 08:28:42 +0000 | [diff] [blame] | 89 | // get the active connections (and clear the list) |
| 90 | // we have to work on a copy, since stopping any active connection |
| 91 | // will try to remove itself from the set, which might cause a |
| 92 | // ConcurrentModificationException if we would iterate over the list |
| 93 | Shell[] connections; |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 94 | synchronized (m_connections) |
Felix Meschberger | 73bf123 | 2009-02-02 08:28:42 +0000 | [diff] [blame] | 95 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 96 | connections = (Shell[]) m_connections.toArray(new Shell[m_connections.size()]); |
Felix Meschberger | 73bf123 | 2009-02-02 08:28:42 +0000 | [diff] [blame] | 97 | m_connections.clear(); |
| 98 | } |
| 99 | |
| 100 | // now terminate all active connections |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 101 | for (int i = 0; i < connections.length; i++) |
Felix Meschberger | 73bf123 | 2009-02-02 08:28:42 +0000 | [diff] [blame] | 102 | { |
| 103 | connections[i].terminate(); |
| 104 | } |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 105 | }//deactivate |
| 106 | |
| 107 | /** |
| 108 | * Class that implements the listener's accept logic as a <tt>Runnable</tt>. |
| 109 | */ |
| 110 | private class Acceptor implements Runnable |
| 111 | { |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 112 | private volatile boolean m_stop = false; |
| 113 | private final ServerSocket m_serverSocket; |
| 114 | |
| 115 | Acceptor() throws IOException |
| 116 | { |
| 117 | m_serverSocket = new ServerSocket(m_port, 1, InetAddress.getByName(m_ip)); |
| 118 | m_serverSocket.setSoTimeout(m_soTimeout); |
| 119 | } |
| 120 | |
| 121 | public void close() throws IOException |
| 122 | { |
| 123 | m_stop = true; |
| 124 | m_serverSocket.close(); |
| 125 | } |
| 126 | |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 127 | /** |
| 128 | * Listens constantly to a server socket and handles incoming connections. |
| 129 | * One connection will be accepted and routed into the shell, all others will |
| 130 | * be notified and closed. |
| 131 | * <p/> |
| 132 | * The mechanism that should allow the thread to unblock from the ServerSocket.accept() call |
| 133 | * is currently closing the ServerSocket from another thread. When the stop flag is set, |
| 134 | * this should cause the thread to return and stop. |
| 135 | */ |
| 136 | public void run() |
| 137 | { |
| 138 | try |
| 139 | { |
| 140 | /* |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 141 | A server socket is opened with a connectivity queue of a size specified |
| 142 | in int floodProtection. Concurrent login handling under normal circumstances |
| 143 | should be handled properly, but denial of service attacks via massive parallel |
| 144 | program logins should be prevented with this. |
| 145 | */ |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 146 | do |
| 147 | { |
| 148 | try |
| 149 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 150 | Socket s = m_serverSocket.accept(); |
| 151 | if (m_useCounter.get() >= m_maxConnections) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 152 | { |
| 153 | //reject with message |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 154 | PrintStream out = new PrintStream(s.getOutputStream()); |
| 155 | out.print(INUSE_MESSAGE); |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 156 | out.flush(); |
| 157 | //close |
| 158 | out.close(); |
| 159 | s.close(); |
| 160 | } |
| 161 | else |
| 162 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 163 | m_useCounter.increment(); |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 164 | //run on the connection thread |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 165 | Thread connectionThread = new Thread(new Shell(Listener.this, s, m_useCounter)); |
| 166 | connectionThread.setName("telnetconsole.shell remote=" + s.getRemoteSocketAddress()); |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 167 | connectionThread.start(); |
| 168 | } |
| 169 | } |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 170 | catch (SocketException ex) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 171 | { |
| 172 | } |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 173 | catch (SocketTimeoutException ste) |
| 174 | { |
Felix Meschberger | 5182570 | 2009-02-02 07:07:38 +0000 | [diff] [blame] | 175 | // Caught socket timeout exception. continue |
| 176 | } |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 177 | } |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 178 | while (!m_stop); |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 179 | |
| 180 | } |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 181 | catch (IOException e) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 182 | { |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 183 | m_services.error("Listener.Acceptor::run()", e); |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 184 | } |
| 185 | }//run |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 186 | }//inner class Acceptor |
| 187 | |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 188 | private static final String INUSE_MESSAGE = "Connection refused.\r\n" + "All possible connections are currently being used.\r\n"; |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 189 | |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 190 | private int getProperty(BundleContext bundleContext, String propName, int defaultValue) |
Felix Meschberger | d7c324d | 2008-08-19 14:10:48 +0000 | [diff] [blame] | 191 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 192 | String propValue = bundleContext.getProperty(propName); |
| 193 | if (propValue != null) |
Felix Meschberger | d7c324d | 2008-08-19 14:10:48 +0000 | [diff] [blame] | 194 | { |
| 195 | try |
| 196 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 197 | return Integer.parseInt(propValue); |
Felix Meschberger | d7c324d | 2008-08-19 14:10:48 +0000 | [diff] [blame] | 198 | } |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 199 | catch (NumberFormatException ex) |
Felix Meschberger | d7c324d | 2008-08-19 14:10:48 +0000 | [diff] [blame] | 200 | { |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 201 | m_services.error("Listener::activate()", ex); |
Felix Meschberger | d7c324d | 2008-08-19 14:10:48 +0000 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
| 205 | return defaultValue; |
| 206 | } |
| 207 | |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 208 | private String getProperty(BundleContext bundleContext, String propName, String defaultValue) |
Felix Meschberger | 3590103 | 2009-01-30 22:43:37 +0000 | [diff] [blame] | 209 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 210 | String propValue = bundleContext.getProperty(propName); |
| 211 | if (propValue != null) |
Felix Meschberger | 3590103 | 2009-01-30 22:43:37 +0000 | [diff] [blame] | 212 | { |
Felix Meschberger | 73bf123 | 2009-02-02 08:28:42 +0000 | [diff] [blame] | 213 | return propValue; |
Felix Meschberger | 3590103 | 2009-01-30 22:43:37 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | return defaultValue; |
| 217 | } |
| 218 | |
Felix Meschberger | 73bf123 | 2009-02-02 08:28:42 +0000 | [diff] [blame] | 219 | /** |
| 220 | * Registers the given {@link Shell} instance handling a remote connection |
| 221 | * to this listener. |
| 222 | * <p> |
| 223 | * This method is called by the {@link Shell#run()} method to register the |
| 224 | * remote connection for it to be terminated in case this listener is |
| 225 | * {@link #deactivate() deactivated} before the remote connection is |
| 226 | * terminated. |
| 227 | * |
| 228 | * @param connection The {@link Shell} connection to register |
| 229 | */ |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 230 | void registerConnection(Shell connection) |
Felix Meschberger | 73bf123 | 2009-02-02 08:28:42 +0000 | [diff] [blame] | 231 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 232 | synchronized (m_connections) |
Felix Meschberger | 73bf123 | 2009-02-02 08:28:42 +0000 | [diff] [blame] | 233 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 234 | m_connections.add(connection); |
Felix Meschberger | 73bf123 | 2009-02-02 08:28:42 +0000 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Unregisters the given {@link Shell} instance handling a remote connection |
| 240 | * from this listener. |
| 241 | * <p> |
| 242 | * This method is called when the {@link Shell#run()} method terminates to |
| 243 | * inform this listener instance that the remote connection has ended and |
| 244 | * thus does not need to be cleaned up when this listener terminates. |
| 245 | * |
| 246 | * @param connection The {@link Shell} connection to unregister |
| 247 | */ |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 248 | void unregisterConnection(Shell connection) |
Felix Meschberger | 73bf123 | 2009-02-02 08:28:42 +0000 | [diff] [blame] | 249 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 250 | synchronized (m_connections) |
Felix Meschberger | 73bf123 | 2009-02-02 08:28:42 +0000 | [diff] [blame] | 251 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 252 | m_connections.remove(connection); |
Felix Meschberger | 73bf123 | 2009-02-02 08:28:42 +0000 | [diff] [blame] | 253 | } |
| 254 | } |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 255 | }//class Listener |