/** * there is a socket listening on the port for this packet. process if it is a new connection rdp * packet */ public void processNewConnection(RDPServerSocket serverSocket, RDPPacket packet) { if (Log.loggingNet) Log.net( "processNewConnection: RDPPACKET (localport=" + serverSocket.getPort() + "): " + packet); // int localPort = serverSocket.getPort(); InetAddress remoteAddr = packet.getInetAddress(); int remotePort = packet.getPort(); if (!packet.isSyn()) { // the client is not attemping to start a new connection // send a reset and forget about it Log.debug("socket got non-syn packet, replying with reset: packet=" + packet); RDPPacket rstPacket = RDPPacket.makeRstPacket(); rstPacket.setPort(remotePort); rstPacket.setInetAddress(remoteAddr); RDPServer.sendPacket(serverSocket.getDatagramChannel(), rstPacket); return; } // it is a syn packet, lets make a new connection for it RDPConnection con = new RDPConnection(); DatagramChannel dc = serverSocket.getDatagramChannel(); con.initConnection(dc, packet); // add new connection to allConnectionMap registerConnection(con, dc); // ack it with a syn RDPPacket synPacket = RDPPacket.makeSynPacket(con); con.sendPacketImmediate(synPacket, false); }
/** * a DatagramChannel has data ready - process all the pending packets, whether its for a * rdpserversocket or rdpconnection. */ void processActiveChannel(DatagramChannel dc) throws ClosedChannelException { RDPPacket packet; int count = 0; // read in the packet try { Set<RDPConnection> needsAckConnections = new HashSet<RDPConnection>(); while ((packet = RDPServer.receivePacket(dc)) != null) { if (Log.loggingNet) Log.net( "RDPServer.processActiveChannel: Starting iteration with count of " + count + " packets"); // see if there is a connection already for this packet InetAddress remoteAddr = packet.getInetAddress(); int remotePort = packet.getPort(); int localPort = dc.socket().getLocalPort(); ConnectionInfo conInfo = new ConnectionInfo(remoteAddr, remotePort, localPort); RDPConnection con = RDPServer.getConnection(dc, conInfo); if (con != null) { if (Log.loggingNet) Log.net("RDPServer.processActiveChannel: found an existing connection: " + con); count++; if (processExistingConnection(con, packet)) needsAckConnections.add(con); // Prevent this from blocking getActiveChannels by // putting an upper bound on the number of packets // processed if (count >= 20) break; continue; } else { Log.net("RDPServer.processActiveChannel: did not find an existing connection"); } // there is no connection, // see if there is a socket listening for new connection RDPServerSocket rdpSocket = RDPServer.getRDPSocket(dc); if (rdpSocket != null) { count++; processNewConnection(rdpSocket, packet); return; } return; } // Finally, send out the acks for (RDPConnection con : needsAckConnections) { RDPPacket replyPacket = new RDPPacket(con); con.sendPacketImmediate(replyPacket, false); } } catch (ClosedChannelException ex) { Log.error("RDPServer.processActiveChannel: ClosedChannel " + dc.socket()); throw ex; } finally { if (Log.loggingNet) Log.net("RDPServer.processActiveChannel: Returning after processing " + count + " packets"); } }
/** make sure the packet as the remote address and remote port set */ static void sendPacket(DatagramChannel dc, RDPPacket packet) { sendMeter.add(); // allocate a buffer int bufSize = 100 + (packet.numEacks() * 4); if (packet.getData() != null) { bufSize += packet.getData().length; sendDataMeter.add(); } MVByteBuffer buf = new MVByteBuffer(bufSize); packet.toByteBuffer(buf); // function flips the buffer int remotePort = packet.getPort(); InetAddress remoteAddr = packet.getInetAddress(); if ((remotePort < 0) || (remoteAddr == null)) { throw new MVRuntimeException("RDPServer.sendPacket: remotePort or addr is null"); } try { int bytes = dc.send(buf.getNioBuf(), new InetSocketAddress(remoteAddr, remotePort)); if (bytes == 0) { Log.error("RDPServer.sendPacket: could not send packet, size=" + bufSize); } if (Log.loggingNet) Log.net( "RDPServer.sendPacket: remoteAddr=" + remoteAddr + ", remotePort=" + remotePort + ", numbytes sent=" + bytes); } catch (java.io.IOException e) { Log.exception( "RDPServer.sendPacket: remoteAddr=" + remoteAddr + ", remotePort=" + remotePort + ", got exception", e); throw new MVRuntimeException("RDPServer.sendPacket", e); } }