Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 30e145259f37e4b5cec04264bc735b5c2c288ef8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
 * Copyright (c) 2011, 2012, 2015 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Teerawat Chaiyakijpichet (No Magic Asia Ltd.) - initial API and implementation
 *    Caspar De Groot (No Magic Asia Ltd.) - initial API and implementation
 */
package org.eclipse.net4j.internal.tcp.ssl;

import org.eclipse.net4j.buffer.BufferState;
import org.eclipse.net4j.buffer.IBuffer;
import org.eclipse.net4j.buffer.IBufferProvider;
import org.eclipse.net4j.internal.tcp.bundle.OM;
import org.eclipse.net4j.util.StringUtil;
import org.eclipse.net4j.util.om.trace.ContextTracer;

import org.eclipse.internal.net4j.buffer.Buffer;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.SocketChannel;

/**
 * All source code same as org.eclipse.internal.net4j.buffer.Buffer except adding SSLEngineManager to constructor and
 * overriding startGetting and write method in order to attach the SSL functional.
 *
 * @author Teerawat Chaiyakijpichet (No Magic Asia Ltd.)
 * @author Caspar De Groot (No Magic Asia Ltd.)
 * @since 4.0
 */
public class SSLBuffer extends Buffer
{
  private static final ContextTracer TRACER = new ContextTracer(OM.DEBUG_SSLBUFFER, SSLBuffer.class);

  private SSLEngineManager sslEngineManager;

  public SSLBuffer(IBufferProvider provider, short capacity, SSLEngineManager sslEngineManager)
  {
    super(provider, capacity);
    this.sslEngineManager = sslEngineManager;
  }

  @Override
  public ByteBuffer startGetting(SocketChannel socketChannel) throws IOException
  {
    BufferState state = getState();
    if (state != BufferState.INITIAL && state != BufferState.READING_HEADER && state != BufferState.READING_BODY)
    {
      throw new IllegalStateException(toString());
    }

    int readSize = 0;

    if (sslEngineManager.getAppRecvBuf().position() > 0)
    {
      readSize = sslEngineManager.getAppRecvBuf().position();
    }
    else
    {
      readSize = sslEngineManager.read(socketChannel);
    }

    if (readSize > 0)
    {
      ByteBuffer buf = sslEngineManager.getAppRecvBuf();
      buf.flip();

      int limit = buf.limit();
      ByteBuffer byteBuffer = getByteBuffer();

      int capacity = byteBuffer.capacity();
      limit = limit > capacity ? capacity : limit;

      byteBuffer.put(buf.array(), 0, limit);
      buf.position(limit);
      buf.compact();
      byteBuffer.flip();

      setChannelID(byteBuffer.getShort());
      short payloadSize = byteBuffer.getShort();

      if (payloadSize < 0)
      {
        setEOS(true);
        payloadSize = (short)-payloadSize;
      }

      payloadSize -= MAKE_PAYLOAD_SIZE_NON_ZERO;

      byteBuffer.position(IBuffer.HEADER_SIZE);
      setState(BufferState.READING_HEADER);

      byteBuffer.compact();
      byteBuffer.limit(payloadSize);
      setState(BufferState.READING_BODY);

      byteBuffer.flip();
      setState(BufferState.GETTING);

      return byteBuffer;
    }
    else if (readSize < 0)
    {
      throw new ClosedChannelException();
    }

    return null;
  }

  /**
   * @return <code>true</code> if the buffer has been completely written, <code>false</code> otherwise.
   */
  @Override
  public boolean write(SocketChannel socketChannel) throws IOException
  {
    try
    {

      if (sslEngineManager.getPacketSendBuf().position() > 0)
      {
        sslEngineManager.handleWrite(socketChannel);

        if (sslEngineManager.getPacketSendBuf().position() > 0)
        {
          clear();
          return false;
        }

        clear();
        return true;
      }

      BufferState state = getState();
      if (state != BufferState.PUTTING && state != BufferState.WRITING)
      {
        throw new IllegalStateException(toString());
      }

      ByteBuffer byteBuffer = getByteBuffer();
      if (state == BufferState.PUTTING)
      {
        if (getChannelID() == NO_CHANNEL)
        {
          throw new IllegalStateException("channelID == NO_CHANNEL"); //$NON-NLS-1$
        }

        int payloadSize = byteBuffer.position() - IBuffer.HEADER_SIZE + MAKE_PAYLOAD_SIZE_NON_ZERO;
        if (isEOS())
        {
          payloadSize = -payloadSize;
        }

        if (TRACER.isEnabled())
        {
          TRACER.trace("Writing " + (Math.abs(payloadSize) - 1) + " bytes" //$NON-NLS-1$ //$NON-NLS-2$
              + (isEOS() ? " (EOS)" : "") + StringUtil.NL + formatContent(false)); //$NON-NLS-1$ //$NON-NLS-2$
        }

        byteBuffer.flip();
        byteBuffer.putShort(getChannelID());
        byteBuffer.putShort((short)payloadSize);
        byteBuffer.position(0);
        setState(BufferState.WRITING);
      }

      sslEngineManager.getAppSendBuf().put(byteBuffer);
      sslEngineManager.write(socketChannel);

      if (sslEngineManager.getPacketSendBuf().position() > 0)
      {
        clear();
        return false;
      }

      clear();
      return true;
    }
    catch (IOException ex)
    {
      handleError(ex);
      throw ex;
    }
    catch (RuntimeException ex)
    {
      handleError(ex);
      throw ex;
    }
    catch (Error ex)
    {
      handleError(ex);
      throw ex;
    }
  }

  @Override
  public void dispose()
  {
    sslEngineManager = null;
    super.dispose();
  }
}

Back to the top