001: //
002: // Copyright %G% Sun Microsystems, Inc. All Rights Reserved.
003: //
004: package com.sun.portal.netlet.client.common;
005:
006: import com.sun.portal.netlet.econnection.KeyConstants;
007: import com.sun.portal.netlet.econnection.ReaderWriterDecrypt;
008:
009: import java.io.InputStream;
010: import java.io.OutputStream;
011:
012: public class ReaderWriterDecryptExchange extends ReaderWriterDecrypt {
013: private String srvhost;
014: private SClientMgr scm;
015:
016: private final int length_byte_pos = 8; // starting from 0
017: private final int port_bytes_pos = 136;
018: private byte port_first_byte;
019: private int num_bytes_read = 0;
020: private int current_packet_size = 0;
021:
022: public ReaderWriterDecryptExchange(RWGroupCrypt rwc,
023: InputStream inStream, OutputStream outStream,
024: String srvhost, SClientMgr scm) {
025:
026: super (rwc, inStream, outStream);
027: this .srvhost = srvhost;
028: this .scm = scm;
029: System.out.println("Netlet Exchange");
030: }
031:
032: public int FindDynamicPort(byte[] buffer, int len) {
033: //System.out.println("Netlet Exchange Port");
034: if (len <= 0) {
035: return 0;
036: }
037:
038: if (current_packet_size == 0) {
039: int length_byte_index;
040:
041: length_byte_index = length_byte_pos - num_bytes_read;
042: if (length_byte_index >= len || length_byte_index < 0) {
043: num_bytes_read += len;
044: return 0;
045: }
046: current_packet_size = byte_to_int(buffer[length_byte_index]);
047: System.out.println("rpc packet size is "
048: + current_packet_size);
049: }
050:
051: int port_bytes_index;
052:
053: port_bytes_index = port_bytes_pos - num_bytes_read;
054: if (port_bytes_index >= 0 && port_bytes_index < len) {
055: if (port_bytes_index == len - 1) {
056: // we get only first byte here.
057: port_first_byte = buffer[port_bytes_index];
058: } else {
059: int sport;
060: sport = byte_to_int(buffer[port_bytes_index]) * 256
061: + byte_to_int(buffer[port_bytes_index + 1]);
062:
063: String tname = "atprox_" + sport + "_"
064: + ClientConfig.getDestPort() + "_"
065: + ClientConfig.getDestHost();
066: System.out.println("Netlet Exchange Dynamic Port: "
067: + sport);
068:
069: scm.addproxy("", sport, new String("" + sport),
070: srvhost, tname, false,
071: KeyConstants.DEFAULT_CIPHER,
072: KeyConstants.DEFAULT_KEYLEN);
073: }
074: } else if (port_bytes_index == -1) {
075: int sport;
076:
077: sport = byte_to_int(port_first_byte) * 256
078: + byte_to_int(buffer[0]);
079:
080: String tname = "atprox_" + sport + "_"
081: + ClientConfig.getDestPort() + "_"
082: + ClientConfig.getDestHost();
083:
084: scm.addproxy("", sport, new String("" + sport), srvhost,
085: tname, false, KeyConstants.DEFAULT_CIPHER,
086: KeyConstants.DEFAULT_KEYLEN);
087: }
088:
089: num_bytes_read += len;
090:
091: if (num_bytes_read < current_packet_size) {
092: return 0;
093: }
094:
095: num_bytes_read = 0;
096: current_packet_size = 0;
097:
098: if (num_bytes_read == current_packet_size) {
099: return 0;
100: }
101:
102: int extra_len = num_bytes_read - current_packet_size;
103:
104: byte[] tmp_buffer = new byte[extra_len];
105:
106: System.arraycopy(buffer, len - extra_len, tmp_buffer, 0,
107: extra_len);
108:
109: FindDynamicPort(tmp_buffer, extra_len);
110: return 0;
111: }
112:
113: public int byte_to_int(byte b) {
114: if (b >= 0) {
115: return (int) b;
116: }
117: return ((int) b) + 256;
118: }
119: }
|