# HG changeset patch # User kono # Date 1223173564 -32400 # Node ID 7107faaf3febb53ce2c305dfc8268b927b7b7879 # Parent 511376c066db420aa84b8a715afc3fd30c651c8b *** empty log message *** diff -r 511376c066db -r 7107faaf3feb rep/REPCommandPacker.java --- a/rep/REPCommandPacker.java Sun Oct 05 11:01:58 2008 +0900 +++ b/rep/REPCommandPacker.java Sun Oct 05 11:26:04 2008 +0900 @@ -28,18 +28,22 @@ public class REPCommandPacker implements REPPack { + private static final int TEXTSIZELIMIT = 0x7000000; // JIS/S-JIS = 2, UTF-8 = 3, UTF-?? = 5 private final int HEADER_SIZE = 24; final int CHAR_ORDER = 5; + Charset charset = Charset.forName("UTF-8"); + CharsetEncoder encoder = charset.newEncoder(); + CharsetDecoder decoder = charset.newDecoder(); /* (non-Javadoc) * @see rep.REPPack#packUConv(rep.REPCommand) */ public ByteBuffer packUConv(REPCommand command){ - //System.out.println("send command byUTF8: " + command.toString()); + System.out.println("send command byUTF8: " + command.toString()); if(command.string == null){ - command.setString("test"); + command.setString(""); } ByteBuffer buffer = ByteBuffer.allocateDirect(HEADER_SIZE+(command.string.length()*CHAR_ORDER)); buffer.clear(); // position = 0 @@ -51,25 +55,20 @@ int pos = buffer.position(); buffer.putInt(0); + int pos1 = buffer.position(); //Encode to UTF8 CharBuffer cb = CharBuffer.wrap(command.string); - Charset charset = Charset.forName("UTF-8"); - CharsetEncoder encoder = charset.newEncoder(); try { encoder.encode(cb, buffer, true); } catch (IllegalStateException e) { - e.printStackTrace(); + buffer.position(pos1); } //Encoded string length set - int length = (buffer.position() -pos) -4; - //System.out.println("UTF-8: Set REPComand textlen(Byte) : " + (buffer.position() - pos-4)); - if(length < 0) { - length = 0; - } + int length = buffer.position() -pos1 ; + // System.out.println("UTF-8: Set REPComand textlen(Byte) : " + (buffer.position() - pos-4)); buffer.putInt(pos, length); - buffer.limit(HEADER_SIZE+length); buffer.rewind(); @@ -80,14 +79,7 @@ public REPCommand unpackUConv(SocketChannel sc) throws IOException { ByteBuffer header = ByteBuffer.allocateDirect(HEADER_SIZE); header.clear(); - /* - len = sc.read(header); - if(len <= 0){ - return null; - } - if (len !=HEADER_SIZE) { - throw new IOException(); - } 下のwhileループで OK ? */ + while(header.remaining()>0){ if (sc.read(header)<0) throw new IOException(); } // 壊れたパケットに対してはブロックする可能性あり まぁTCPだしいいか? @@ -101,38 +93,29 @@ int lineno = header.getInt(); int textsiz = header.getInt(); + if (textsiz>TEXTSIZELIMIT||textsiz<0) { + // corrupted packet + throw new IOException(); + } ByteBuffer textBuffer = ByteBuffer.allocateDirect(textsiz); - /* - len = sc.read(textBuffer); - if(len <= 0){ - return null; - } - if (len != textsiz) { - throw new IOException(); - }*/ while(textBuffer.remaining()>0){ if (sc.read(textBuffer)<0) throw new IOException(); } textBuffer.rewind(); //Decode UTF-8 to System Encoding(UTF-16) - Charset charset = Charset.forName("UTF-8"); - CharsetDecoder decoder = charset.newDecoder(); CharBuffer cb = null; + String string; try { cb = decoder.decode(textBuffer); + cb.rewind(); + string = cb.toString(); } catch (CharacterCodingException e) { - e.printStackTrace(); + string = ""; } - cb.rewind(); - - String string = cb.toString(); - textsiz = string.length(); - REPCommand repcommand = new REPCommand(cmd, sid, eid, seqid, lineno, textsiz, string); - return repcommand; } diff -r 511376c066db -r 7107faaf3feb test/ServerSample.java --- a/test/ServerSample.java Sun Oct 05 11:01:58 2008 +0900 +++ b/test/ServerSample.java Sun Oct 05 11:26:04 2008 +0900 @@ -104,6 +104,9 @@ System.out.print("EEE: " + cmd); // クライアントへメッセージを送信 + // copy or do not modify after the write + // In the simulation, object is directly passed + // to the client cmd = new REPCommand(cmd); cmd.setString("This is the answer."); socketChannel.write(cmd); diff -r 511376c066db -r 7107faaf3feb test/sematest/TestSessionManager.java --- a/test/sematest/TestSessionManager.java Sun Oct 05 11:01:58 2008 +0900 +++ b/test/sematest/TestSessionManager.java Sun Oct 05 11:26:04 2008 +0900 @@ -105,7 +105,7 @@ * isSimulation=true thread base simulation for PathFinder * isSimulation=false socket based communication mode */ - REPServerSocketChannel.isSimulation = true; + REPServerSocketChannel.isSimulation = false; TestSessionManager test = new TestSessionManager(1, 0, 2); logger.setLogLevel(5); test.startTest();