コード例 #1
0
  @Override
  public void produceData(final IOSession iosession, final ClientState sessionState)
      throws IOException, SMTPProtocolException {
    Args.notNull(iosession, "IO session");
    Args.notNull(sessionState, "Session state");

    SessionOutputBuffer buf = this.iobuffers.getOutbuf();

    String myHelo = heloName;
    if (myHelo == null) {
      myHelo = AddressUtils.resolveLocalDomain(iosession.getLocalAddress());
    }
    switch (this.codecState) {
      case EHLO_READY:
        SMTPCommand ehlo = new SMTPCommand("EHLO", myHelo);
        this.writer.write(ehlo, buf);
        this.codecState = CodecState.EHLO_RESPONSE_EXPECTED;
        break;
      case HELO_READY:
        SMTPCommand helo = new SMTPCommand("HELO", myHelo);
        this.writer.write(helo, buf);
        this.codecState = CodecState.HELO_RESPONSE_EXPECTED;
        break;
    }

    if (buf.hasData()) {
      buf.flush(iosession.channel());
    }
    if (!buf.hasData()) {
      iosession.clearEvent(SelectionKey.OP_WRITE);
    }
  }
コード例 #2
0
  @Override
  public void produceData(final IOSession iosession, final ServerState sessionState)
      throws IOException, SMTPProtocolException {
    Args.notNull(iosession, "IO session");
    Args.notNull(sessionState, "Session state");

    SessionOutputBuffer buf = this.iobuffers.getOutbuf();

    synchronized (sessionState) {
      if (this.actionFuture != null) {
        SMTPReply reply = getReply(this.actionFuture);
        this.actionFuture = null;
        this.writer.write(reply, buf);
      }

      if (this.actionFuture == null) {
        while (!this.pendingActions.isEmpty()) {
          Action<ServerState> action = this.pendingActions.remove();
          Future<SMTPReply> future =
              action.execute(sessionState, new OutputTrigger<SMTPReply>(sessionState, iosession));
          if (future.isDone()) {
            SMTPReply reply = getReply(future);
            this.writer.write(reply, buf);
          } else {
            this.actionFuture = future;
            break;
          }
        }
      }

      if (buf.hasData()) {
        buf.flush(iosession.channel());
      }
      if (!buf.hasData()) {
        if (sessionState.getDataType() != null) {
          this.completed = true;
        }
        if (sessionState.isTerminated()) {
          iosession.close();
        } else {
          iosession.clearEvent(SelectionKey.OP_WRITE);
        }
      }
    }
  }
コード例 #3
0
  @Override
  public void reset(final IOSession iosession, final ClientState sessionState)
      throws IOException, SMTPProtocolException {
    this.parser.reset();
    this.writer.reset();
    this.codecState = CodecState.SERVICE_READY_EXPECTED;

    iosession.setEventMask(SelectionKey.OP_READ);
  }
コード例 #4
0
  @Override
  public void consumeData(final IOSession iosession, final ServerState sessionState)
      throws IOException, SMTPProtocolException {
    Args.notNull(iosession, "IO session");
    Args.notNull(sessionState, "Session state");

    SessionInputBuffer buf = this.iobuffers.getInbuf();

    synchronized (sessionState) {
      for (; ; ) {
        int bytesRead = buf.fill(iosession.channel());
        try {
          SMTPCommand command = this.parser.parse(buf, bytesRead == -1);
          if (command == null) {
            if (bytesRead == -1 && !sessionState.isTerminated() && this.pendingActions.isEmpty()) {
              throw new UnexpectedEndOfStreamException();
            } else {
              break;
            }
          }
          Action<ServerState> action = this.commandHandler.handle(command);
          this.pendingActions.add(action);
        } catch (SMTPErrorException ex) {
          SMTPReply reply = new SMTPReply(ex.getCode(), ex.getEnhancedCode(), ex.getMessage());
          this.pendingActions.add(new SimpleAction(reply));
        } catch (SMTPProtocolException ex) {
          SMTPReply reply =
              new SMTPReply(
                  SMTPCodes.ERR_PERM_SYNTAX_ERR_COMMAND, new SMTPCode(5, 3, 0), ex.getMessage());
          this.pendingActions.add(new SimpleAction(reply));
        }
      }

      if (!this.pendingActions.isEmpty()) {
        iosession.setEvent(SelectionKey.OP_WRITE);
      }
    }
  }
コード例 #5
0
 public DefaultNHttpServerConnection createConnection(final IOSession iosession) {
   org.siddhiesb.transport.http.conn.SSLContextDetails customSSL = null;
   if (sslByIPMap != null) {
     customSSL = sslByIPMap.get(iosession.getLocalAddress());
   }
   if (customSSL == null) {
     customSSL = ssl;
   }
   IOSession customSession;
   if (customSSL != null) {
     customSession =
         new SSLIOSession(
             iosession, SSLMode.SERVER, customSSL.getContext(), customSSL.getHandler());
     iosession.setAttribute(SSLIOSession.SESSION_KEY, customSession);
   } else {
     customSession = iosession;
   }
   DefaultNHttpServerConnection conn =
       LoggingUtils.createServerConnection(customSession, requestFactory, allocator, params);
   int timeout = HttpConnectionParams.getSoTimeout(params);
   conn.setSocketTimeout(timeout);
   return conn;
 }
コード例 #6
0
  public void verify(final IOSession iosession, final SSLSession sslsession) throws SSLException {
    SocketAddress remoteAddress = iosession.getRemoteAddress();
    String address;
    if (remoteAddress instanceof InetSocketAddress) {
      address = ((InetSocketAddress) remoteAddress).getHostName();
    } else {
      address = remoteAddress.toString();
    }

    if (verificationManager != null) {
      try {
        verificationManager.verifyRevocationStatus(sslsession.getPeerCertificateChain());
      } catch (CertificateVerificationException e) {
        throw new SSLException("Certificate Chain Validation failed for host : " + address, e);
      }
    }
  }
コード例 #7
0
ファイル: SSLIOSession.java プロジェクト: bugvm/bugvm
  /**
   * Creates new instance of {@code SSLIOSession} class.
   *
   * @param session I/O session to be decorated with the TLS/SSL capabilities.
   * @param sslMode SSL mode (client or server)
   * @param host original host (applicable in client mode only)
   * @param sslContext SSL context to use for this I/O session.
   * @param handler optional SSL setup handler. May be {@code null}.
   * @param bufferManagementStrategy buffer management strategy
   */
  public SSLIOSession(
      final IOSession session,
      final SSLMode sslMode,
      final HttpHost host,
      final SSLContext sslContext,
      final SSLSetupHandler handler,
      final SSLBufferManagementStrategy bufferManagementStrategy) {
    super();
    Args.notNull(session, "IO session");
    Args.notNull(sslContext, "SSL context");
    Args.notNull(bufferManagementStrategy, "Buffer management strategy");
    this.session = session;
    this.sslMode = sslMode;
    this.appEventMask = session.getEventMask();
    this.channel = new InternalByteChannel();
    this.handler = handler;

    // Override the status buffer interface
    this.session.setBufferStatus(this);

    if (this.sslMode == SSLMode.CLIENT && host != null) {
      this.sslEngine = sslContext.createSSLEngine(host.getHostName(), host.getPort());
    } else {
      this.sslEngine = sslContext.createSSLEngine();
    }

    // Allocate buffers for network (encrypted) data
    final int netBuffersize = this.sslEngine.getSession().getPacketBufferSize();
    this.inEncrypted = bufferManagementStrategy.constructBuffer(netBuffersize);
    this.outEncrypted = bufferManagementStrategy.constructBuffer(netBuffersize);

    // Allocate buffers for application (unencrypted) data
    final int appBuffersize = this.sslEngine.getSession().getApplicationBufferSize();
    this.inPlain = bufferManagementStrategy.constructBuffer(appBuffersize);
    this.outPlain = bufferManagementStrategy.constructBuffer(appBuffersize);
  }
コード例 #8
0
 public void verify(final IOSession iosession, final SSLSession sslsession) throws SSLException {
   InetSocketAddress address = (InetSocketAddress) iosession.getRemoteAddress();
   hostnameVerifier.verify(address.getHostName(), sslsession);
 }
コード例 #9
0
  @Override
  public void consumeData(final IOSession iosession, final ClientState sessionState)
      throws IOException, SMTPProtocolException {
    Args.notNull(iosession, "IO session");
    Args.notNull(sessionState, "Session state");

    SessionInputBuffer buf = this.iobuffers.getInbuf();

    int bytesRead = buf.fill(iosession.channel());
    SMTPReply reply = this.parser.parse(buf, bytesRead == -1);

    if (reply != null) {
      switch (this.codecState) {
        case SERVICE_READY_EXPECTED:
          if (reply.getCode() == SMTPCodes.SERVICE_READY) {
            this.codecState = CodecState.EHLO_READY;
            iosession.setEvent(SelectionKey.OP_WRITE);
          } else {
            this.codecState = CodecState.COMPLETED;
            sessionState.setReply(reply);
          }
          break;
        case EHLO_RESPONSE_EXPECTED:
          if (reply.getCode() == SMTPCodes.OK) {

            Set<String> extensions = sessionState.getExtensions();

            List<String> lines = reply.getLines();
            if (lines.size() > 1) {
              for (int i = 1; i < lines.size(); i++) {
                String line = lines.get(i);
                extensions.add(line.toUpperCase(Locale.US));
              }
            }
            this.codecState = CodecState.COMPLETED;
            sessionState.setReply(reply);
          } else if (reply.getCode() == SMTPCodes.ERR_PERM_SYNTAX_ERR_COMMAND) {
            this.codecState = CodecState.HELO_READY;
            iosession.setEvent(SelectionKey.OP_WRITE);
          } else {
            this.codecState = CodecState.COMPLETED;
            sessionState.setReply(reply);
          }
          break;
        case HELO_RESPONSE_EXPECTED:
          this.codecState = CodecState.COMPLETED;
          sessionState.setReply(reply);
          break;
        default:
          if (reply.getCode() == SMTPCodes.ERR_TRANS_SERVICE_NOT_AVAILABLE) {
            sessionState.setReply(reply);
            this.codecState = CodecState.COMPLETED;
          } else {
            throw new SMTPProtocolException("Unexpected reply: " + reply);
          }
      }
    } else {
      if (bytesRead == -1 && !sessionState.isTerminated()) {
        throw new UnexpectedEndOfStreamException();
      }
    }
  }