001: //
002: // Copyright 02/09/00 Sun Microsystems, Inc. All Rights Reserved.
003: //
004: package com.sun.portal.netlet.client.common;
005:
006: import com.sun.portal.netlet.econnection.DataCipherMsg;
007: import com.sun.portal.netlet.econnection.KeyConstants;
008: import com.sun.portal.netlet.econnection.ReaderWriterEncrypt;
009:
010: import java.io.InputStream;
011: import java.io.InterruptedIOException;
012: import java.io.OutputStream;
013: import java.net.Socket;
014: import java.util.StringTokenizer;
015:
016: public class ReaderWriterEncryptFtp extends ReaderWriterEncrypt {
017: private SClientMgr scm;
018:
019: private final byte byteCR = 13;
020: private final byte byteLF = 10;
021:
022: public ReaderWriterEncryptFtp(RWGroupCrypt rwc,
023: InputStream inStream, OutputStream outStream,
024: int netletKeepAliveInterval, SClientMgr scm) {
025: super (rwc, inStream, outStream, netletKeepAliveInterval);
026: this .scm = scm;
027:
028: System.out.println("Netlet Ftp");
029: }
030:
031: public void run() {
032: //System.out.println("ReaderWriterEncrypt running");
033: try {
034: int numBytes = 0;
035: byte[] buffer = new byte[MAXBUFFERSIZE];
036: int startIndex = 0;
037: int cmdBufferSize = MAXBUFFERSIZE;
038: byte[] cmdBuffer = new byte[cmdBufferSize];
039: int cmdSize = 0;
040: int newCmdSize = 0;
041: int oldCmdSize = 0;
042: byte[] tmpBuffer;
043: boolean cmdComplete = false;
044: boolean needLF = false;
045: int i;
046:
047: DataCipherMsg dMsg = new DataCipherMsg();
048:
049: while (go) {
050: try {
051: numBytes = in.read(buffer, 0, MAXBUFFERSIZE);
052: startIndex = 0;
053:
054: if (numBytes > 0) {
055: while (startIndex < numBytes) {
056: for (i = startIndex; i < numBytes; i++) {
057: if (i == 0 && needLF) {
058: if (buffer[0] == byteLF) {
059: newCmdSize = 1;
060: cmdComplete = true;
061: break;
062: } else {
063: needLF = false;
064: }
065: }
066: if (buffer[i] == byteCR) {
067: if (i + 1 < numBytes) {
068: if (buffer[i + 1] == byteLF) {
069: newCmdSize = i + 2
070: - startIndex;
071: cmdComplete = true;
072: break;
073: } else {
074: continue;
075: }
076: } else {
077: needLF = true;
078: }
079: }
080: }
081: if (i == numBytes) {
082: newCmdSize = numBytes - startIndex;
083: }
084:
085: oldCmdSize = cmdSize;
086: cmdSize += newCmdSize;
087:
088: if (cmdSize > cmdBufferSize - 8) {
089: cmdBufferSize = cmdSize + 8;
090: tmpBuffer = new byte[cmdBufferSize];
091: System.arraycopy(cmdBuffer, 0,
092: tmpBuffer, 0, oldCmdSize);
093: cmdBuffer = tmpBuffer;
094: }
095: System.arraycopy(buffer, startIndex,
096: cmdBuffer, oldCmdSize, newCmdSize);
097:
098: if (cmdComplete) {
099: cmdSize = checkCommand(cmdBuffer,
100: cmdSize);
101: dMsg.setDataByRef(cmdBuffer, cmdSize);
102: if (dMsg.writeMsg(out) == 0) {
103: sent = true;
104: }
105: cmdComplete = false;
106: cmdSize = 0;
107: }
108: startIndex += newCmdSize;
109: }
110: } else if (numBytes == 0) {
111: System.out
112: .println("ReaderWriterEncryptFtp received 0 bytes");
113: } else {
114: go = false;
115: return;
116: }
117: } catch (InterruptedIOException iioe) {
118: // System.out.println("ReaderWriterEncryptFtp ioexception " + iioe);
119: int rc = writeDummyMsg(out);
120: if (rc == -1) {
121: go = false;
122: return;
123: }
124: } catch (Exception e) {
125: // System.out.println("ReaderWriterEncryptFtp caught exception:");
126: e.printStackTrace();
127: go = false;
128: return;
129: }
130: }
131: } finally {
132: stop();
133: rwLock.notifyFinished(this );
134: rwLock = null;
135: }
136: }
137:
138: private int checkCommand(byte[] cmdBuffer, int cmdSize) {
139: String cmd = new String(cmdBuffer, 0, cmdSize);
140: System.out.println("FTP: " + cmd);
141: StringTokenizer st = new StringTokenizer(cmd);
142: if (st.countTokens() != 2) {
143: return cmdSize;
144: }
145:
146: String cmdCode = st.nextToken();
147: if (!cmdCode.equalsIgnoreCase("PORT")) {
148: return cmdSize;
149: }
150:
151: StringTokenizer stz = new StringTokenizer(st.nextToken(), ",");
152: if (stz.countTokens() != 6) {
153: return cmdSize;
154: }
155:
156: String strIP = stz.nextToken() + "." + stz.nextToken() + "."
157: + stz.nextToken() + "." + stz.nextToken();
158:
159: int dataPort;
160: try {
161: dataPort = Integer.parseInt(stz.nextToken()) * 256
162: + Integer.parseInt(stz.nextToken());
163: } catch (Exception ex) {
164: System.out.println("Unable to get FTP data port. " + ex);
165: return cmdSize;
166: }
167:
168: int sport = -1;
169:
170: ProxySConn sconn = new ProxySConn(dataPort, strIP, true);
171: Socket toClient = sconn.getconn();
172:
173: if (toClient == null) {
174: return cmdSize;
175: }
176:
177: //System.out.println(strIP + ":" + dataPort + " connected.");
178:
179: RWGroupCrypt rwg = new RWGroupCrypt(toClient,
180: ClientConfig.NORMAL, sport, -1, "dummy", scm, null,
181: null, KeyConstants.DEFAULT_KEYLEN);
182:
183: cmd = "PORT " + rwg.newFtpAddrPort;
184: int cmdLength = cmd.length();
185: System.arraycopy(cmd.getBytes(), 0, cmdBuffer, 0, cmdLength);
186: cmdBuffer[cmdLength] = byteCR;
187: cmdBuffer[cmdLength + 1] = byteLF;
188: return (cmdLength + 2);
189: }
190: }
|