001: // @(#)ProxyCipherMsg.java 1.3 "@(#)ProxyCipherMsg.java 1.3 99/09/23 Sun Microsystems"
002:
003: package com.sun.portal.netlet.econnection;
004:
005: import java.io.ByteArrayInputStream;
006: import java.io.ByteArrayOutputStream;
007: import java.io.DataInputStream;
008: import java.io.DataOutputStream;
009: import java.io.IOException;
010:
011: public class ProxyCipherMsg extends CipherMsg implements
012: ProxyMsgConstants, SizeConstants {
013:
014: protected int srcPort;
015:
016: protected int hostNameLen;
017:
018: protected byte[] hostName;
019:
020: protected final int MAX_HOSTNAME_LEN = 255;
021:
022: protected final int MAX_PROXY_LEN = MAX_HOSTNAME_LEN + 8;
023:
024: /*
025: * Added for RFE - 4645450 @ added bshankar@sun.com
026: */
027: protected int dstPortLen;
028:
029: protected byte[] dstPortList;
030:
031: public ProxyCipherMsg() {
032: super (VERSION, PROXY_MSG);
033: }
034:
035: /*
036: * Modified for RFE 4645450 ( Lihue Netlet PRD 2.2) @ modified
037: * bshankar@sun.com
038: */
039: public ProxyCipherMsg(int sp, byte[] dp, int dpl, byte[] hn, int hnl) {
040: super (VERSION, PROXY_MSG);
041: if (hnl <= MAX_HOSTNAME_LEN) {
042: srcPort = sp;
043:
044: dstPortLen = dpl;
045: dstPortList = new byte[dpl];
046: System.arraycopy(dp, 0, dstPortList, 0, dpl);
047:
048: hostNameLen = hnl;
049: hostName = new byte[hnl];
050: msgLen = (2 * INT) + INT + hnl + dpl;
051: System.arraycopy(hn, 0, hostName, 0, hnl);
052: }
053: }
054:
055: public int readMsg(DataInputStream in) {
056: int rc = 0;
057: DataInputStream db_in;
058:
059: rc = readHeader(in);
060: if (rc == 0) {
061: if ((msgLen > 0) && (msgLen <= MAX_PROXY_LEN)) {
062: byte[] buffer = new byte[msgLen];
063: try {
064: // now, parse rest of config message...
065: in.readFully(buffer, 0, msgLen);
066: db_in = new DataInputStream(
067: new ByteArrayInputStream(buffer));
068: // get source port...port that netlet listens on
069: srcPort = db_in.readInt();
070:
071: dstPortLen = db_in.readInt();
072: dstPortList = new byte[dstPortLen];
073:
074: // get destination hostname length...
075: hostNameLen = db_in.readInt();
076: if (hostNameLen > MAXHOSTNAMELEN) {
077: rc = -1;
078: } else {
079: hostName = new byte[hostNameLen];
080: }
081:
082: // get destination hostname...hostname that service resides
083: // on
084: db_in.readFully(dstPortList, 0, dstPortLen);
085: db_in.readFully(hostName, 0, hostNameLen);
086: } catch (IOException e) {
087: System.out
088: .println("ProxyMsg: IOE reading proxy info: "
089: + e);
090: rc = -1;
091: }
092: } else {
093: rc = -1;
094: }
095: }
096:
097: return (rc);
098: }
099:
100: public int writeMsg(DataOutputStream out) {
101: int rc = 0;
102: int oldmsgLen;
103:
104: if ((hostNameLen > 0) && (hostNameLen < MAX_HOSTNAME_LEN)) {
105: try {
106: ByteArrayOutputStream d_out = new ByteArrayOutputStream(
107: msgLen);
108: DataOutputStream dd_out = new DataOutputStream(d_out);
109: dd_out.writeInt(srcPort);
110: dd_out.writeInt(dstPortLen);
111: dd_out.writeInt(hostNameLen);
112: d_out.write(dstPortList, 0, dstPortLen);
113: d_out.write(hostName, 0, hostNameLen);
114: // byte[] buffer = new byte[msgLen + 8]; // pad
115: byte[] buffer = new byte[msgLen]; // removed Padding
116: System.arraycopy(d_out.toByteArray(), 0, buffer, 0,
117: msgLen);
118: ByteArrayOutputStream b_out = new ByteArrayOutputStream(
119: HEADER_LEN + MAX_PROXY_LEN);
120: oldmsgLen = msgLen;
121: writeHeaderToByteArray(b_out);
122: b_out.write(buffer, 0, msgLen);
123: b_out.writeTo(out);
124: out.flush();
125: msgLen = oldmsgLen;
126: buffer = null;
127: } catch (IOException e) {
128: System.out.println("ProxyMsg: IOE writing proxy info: "
129: + e);
130: rc = -1;
131: }
132: } else {
133: rc = -1;
134: }
135: return (rc);
136: }
137:
138: public byte[] getToDataStream() {
139: ByteArrayOutputStream b_out = new ByteArrayOutputStream(
140: MAX_PROXY_LEN);
141: DataOutputStream db_out = new DataOutputStream(b_out);
142: try {
143: db_out.writeInt(srcPort);
144: db_out.writeInt(dstPortLen);
145: db_out.writeInt(hostNameLen);
146: b_out.write(dstPortList, 0, dstPortLen);
147: b_out.write(hostName, 0, hostNameLen);
148: } catch (IOException e) {
149: System.out
150: .println("ProxyMsg: IOE writing proxy info to data stream: "
151: + e);
152: return (null);
153: }
154: return (b_out.toByteArray());
155: }
156:
157: public int setFromDataStream(byte[] buffer, int buffer_len) {
158: int rc = 0;
159: DataInputStream db_in;
160:
161: if ((buffer_len > 0) && (buffer_len <= MAX_PROXY_LEN)) {
162: try {
163: // now, parse rest of config message...
164: db_in = new DataInputStream(new ByteArrayInputStream(
165: buffer));
166:
167: msgLen = buffer_len;
168:
169: // get source port...port that netlet listens on
170: srcPort = db_in.readInt();
171: // System.out.println("ProxyMsg: srcPort = " + srcPort);
172:
173: dstPortLen = db_in.readInt();
174: dstPortList = new byte[dstPortLen];
175:
176: // System.out.println("ProxyMsg: dstPort = " + dstPort);
177:
178: // get destination hostname length...
179: hostNameLen = db_in.readInt();
180: if (hostNameLen > MAXHOSTNAMELEN) {
181: rc = -1;
182: } else {
183: hostName = new byte[hostNameLen];
184: }
185: // System.out.println("ProxyMsg: hostNameLen = " + hostNameLen);
186:
187: // get destination portlist...
188: db_in.readFully(dstPortList, 0, dstPortLen);
189: // get destination hostname...hostname that service resides on
190: db_in.readFully(hostName, 0, hostNameLen);
191: // System.out.println("ProxyMsg: hostName = " + new
192: // String(hostName));
193: } catch (IOException e) {
194: System.out
195: .println("ProxyMsg: IOE reading proxy info from data stream: "
196: + e);
197: rc = -1;
198: }
199: } else {
200: rc = -1;
201: }
202: return (rc);
203: }
204:
205: public int getSrcPort() {
206: return (srcPort);
207: }
208:
209: public void setSrcPort(int sp) {
210: srcPort = sp;
211: }
212:
213: /*
214: * public int getDstPort() { return(dstPort); }
215: *
216: * public void setDstPort(int dp) { dstPort = dp; }
217: */
218:
219: /*
220: * Added for RFE 4645450 ( Lihue Netlet PRD 2.2) @ added bshankar@sun.com
221: */
222: public String getDstPort() {
223: return (new String(dstPortList));
224: }
225:
226: public String getHostName() {
227: return (new String(hostName));
228: }
229:
230: public void setHostName(String hn) {
231: hostNameLen = 0; // once set, even if fails, null out hostname
232: if (hostName != null) {
233: hostName = null;
234: }
235: if (hn.length() <= MAX_HOSTNAME_LEN) {
236: hostNameLen = hn.length();
237: hostName = new byte[hostNameLen];
238: System.arraycopy(hn, 0, hostName, 0, hostNameLen);
239: }
240: }
241:
242: }
|