Package ch.ethz.inf.vs.elements

Examples of ch.ethz.inf.vs.elements.RawData


  public RawData serialize(Request request) {
    byte[] bytes = request.getBytes();
    if (bytes == null)
      bytes = new DataSerializer().serializeRequest(request);
    request.setBytes(bytes);
    return new RawData(bytes, request.getDestination(), request.getDestinationPort());
  }
View Full Code Here


  public RawData serialize(Response response) {
    byte[] bytes = response.getBytes();
    if (bytes == null)
      bytes = new DataSerializer().serializeResponse(response);
    response.setBytes(bytes);
    return new RawData(bytes, response.getDestination(), response.getDestinationPort());
  }
View Full Code Here

  public RawData serialize(EmptyMessage message) {
    byte[] bytes = message.getBytes();
    if (bytes == null)
      bytes = new DataSerializer().serializeEmptyMessage(message);
    message.setBytes(bytes);
    return new RawData(bytes, message.getDestination(), message.getDestinationPort());
  }
View Full Code Here

      List<Record> records = Record.fromByteArray(data);

      for (Record record : records) {
        record.setSession(session);

        RawData raw = null;

        ContentType contentType = record.getType();
        LOGGER.finest(" => contentType: "+contentType);
        DTLSFlight flight = null;
        switch (contentType) {
        case APPLICATION_DATA:
          if (session == null) {
            // There is no session available, so no application data
            // should be received, discard it
            LOGGER.info("Discarded unexpected application data message from " + peerAddress.toString());
            return null;
          }
          // at this point, the current handshaker is not needed
          // anymore, remove it
          handshakers.remove(addressToKey(peerAddress));

          ApplicationMessage applicationData = (ApplicationMessage) record.getFragment();
          raw = new RawData(applicationData.getData());
          break;

        case ALERT:
          AlertMessage alert = (AlertMessage) record.getFragment();
          switch (alert.getDescription()) {
          case CLOSE_NOTIFY:
            session.setActive(false);
           
            LOGGER.fine("Received CLOSE_NOTIFY from " + peerAddress.toString());
           
            // server must reply with CLOSE_NOTIFY
            if (!session.isClient()) {
              DTLSMessage closeNotify = new AlertMessage(AlertLevel.WARNING, AlertDescription.CLOSE_NOTIFY);
              flight = new DTLSFlight();
              flight.addMessage(new Record(ContentType.ALERT, session.getWriteEpoch(), session.getSequenceNumber(), closeNotify, session));
              flight.setRetransmissionNeeded(false);
            }
           
            if (dtlsSessions.remove(addressToKey(peerAddress))!=null) {
              LOGGER.info("Closed session with peer: " + peerAddress.toString());
            } else {
              LOGGER.warning("Session to close not found: " + peerAddress.toString());
            }
           
            break;
         
          // remote implementation might use any alert (e.g., against padding oracle attack)
          default:
            LOGGER.warning(alert.getDescription() + " with " + peerAddress.toString());

            // cleaning up
            cancelPreviousFlight(peerAddress);
            dtlsSessions.remove(addressToKey(peerAddress));
            handshakers.remove(addressToKey(peerAddress));
            break;
           
            //TODO somehow tell CoAP endpoint to cancel
          }
          break;
        case CHANGE_CIPHER_SPEC:
        case HANDSHAKE:
          LOGGER.finest(" => handshaker: "+handshaker);
          if (handshaker == null) {
            /*
             * A handshake message received, but no handshaker
             * available: this must mean that we either received
             * a HelloRequest (from server) or a ClientHello
             * (from client) => initialize appropriate
             * handshaker type
             */

            HandshakeMessage handshake = (HandshakeMessage) record.getFragment();

            switch (handshake.getMessageType()) {
            case HELLO_REQUEST:
              /*
               * Client side: server desires a re-handshake
               */
              if (session == null) {
                // create new session
                session = new DTLSSession(peerAddress, true);
                // store session according to peer address
                dtlsSessions.put(addressToKey(peerAddress), session);

                LOGGER.info("Created new session as client with peer: " + peerAddress.toString());
              }
              handshaker = new ClientHandshaker(peerAddress, null, session);
              handshakers.put(addressToKey(peerAddress), handshaker);
              LOGGER.finest("Stored re-handshaker: " + handshaker.toString() + " for " + peerAddress.toString());
              break;

            case CLIENT_HELLO:
              /*
               * Server side: server received a client hello:
               * check first if client wants to resume a
               * session (message must contain session
               * identifier) and then check if particular
               * session still available, otherwise conduct
               * full handshake with fresh session.
               */

              if (!(handshake instanceof FragmentedHandshakeMessage)) {
                // check if session identifier set
                ClientHello clientHello = (ClientHello) handshake;
                session = getSessionByIdentifier(clientHello.getSessionId().getSessionId());
              }
             
              if (session == null) {
                // create new session
                session = new DTLSSession(peerAddress, false);
                // store session according to peer address
                dtlsSessions.put(addressToKey(peerAddress), session);

                LOGGER.info("Created new session as server with peer: " + peerAddress.toString());
                handshaker = new ServerHandshaker(peerAddress, session);
              } else {
                handshaker = new ResumingServerHandshaker(peerAddress, session);
              }
              handshakers.put(addressToKey(peerAddress), handshaker);
              LOGGER.finest("Stored handshaker: " + handshaker.toString() + " for " + peerAddress.toString());
              break;

            default:
              LOGGER.severe("Received unexpected first handshake message (type="+handshake.getMessageType()+") from " + peerAddress.toString() + ":\n" + handshake.toString());
              break;
            }
          }
          flight = handshaker.processMessage(record);
          break;

        default:
          LOGGER.severe("Received unknown DTLS record from " + peerAddress.toString() + ":\n" + ByteArrayUtils.toHexString(data));
          break;
        }

        if (flight != null) {
          cancelPreviousFlight(peerAddress);

          flight.setPeerAddress(peerAddress);
          flight.setSession(session);

          if (flight.isRetransmissionNeeded()) {
            flights.put(addressToKey(peerAddress), flight);
            scheduleRetransmission(flight);
          }

          sendFlight(flight);
        }

        if (raw != null) {

          raw.setAddress(packet.getAddress());
          raw.setPort(packet.getPort());

          return raw;
        }
      }

View Full Code Here

        throw new NullPointerException();
      if (raw.getPort() == 0)
        throw new NullPointerException();
     
      System.out.println(new String(raw.getBytes()));
      connector.send(new RawData("ACK".getBytes(), raw.getAddress(), raw.getPort()));
    }
View Full Code Here

  }
 
  public void test() {
    try {
      dtlsConnector.start();
      dtlsConnector.send(new RawData("HELLO WORLD".getBytes(), InetAddress.getByName("localhost") , DEFAULT_PORT));
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
View Full Code Here

        request.setType(Type.NON);
        request.setToken(new byte[0]);
        request.setMID(mid);
        request.setURI(targetURI);

        RawData raw = serializer.serialize(request);
        raw.setAddress(source);
        raw.setPort(port);
        array.add(raw);
        count++;
      }
    }
View Full Code Here

    return counter < amount;
  }

  @Override
  public RawData next() {
    RawData raw = new RawData(
        messages.get(ptr_message), address,ports[ptr_port]);
    if (++ptr_message >= 1 << 16) {
      ptr_message = 0;
      ptr_port++;
    }
View Full Code Here

   
  public void sendRequests() {
    try {
      System.out.println("Send requests to "+destination+":"+PORT);
      while (running) {
        RawData data = producer.next();
        DatagramPacket p = new DatagramPacket(data.getBytes(), data.getSize());
        p.setAddress(destination);
        p.setPort(PORT);
        socket.send(p);
        meter.requested();
      }
View Full Code Here

TOP

Related Classes of ch.ethz.inf.vs.elements.RawData

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.