public static void processPacket(Client c, int packetType, int packetSize) {
   if (packetType == -1) {
     return;
   }
   PacketType p = packetId[packetType];
   if (p != null) {
     try {
       // System.out.println("packet: " + packetType);
       p.processPacket(c, packetType, packetSize);
     } catch (Exception e) {
       e.printStackTrace();
     }
   } else {
     System.out.println("Unhandled packet type: " + packetType + " - size: " + packetSize);
   }
 }
 public static void processPacket(Client c, int packetType, int packetSize) {
   if (packetType == -1) {
     return;
   }
   PacketType p = packetId[packetType];
   if (p != null) {
     try {
       // System.out.println("packet: " + packetType);
       p.processPacket(c, packetType, packetSize);
     } catch (Exception e) {
       e.printStackTrace();
       c.disconnected = true; // this is for when the isaac gets out of wack the player will d
     }
   } else {
     System.out.println("Unhandled packet type: " + packetType + " - size: " + packetSize);
   }
 }
Beispiel #3
0
 @Deprecated
 /**
  * @deprecated not really, just a reminder to set a static description to all packets so the log
  *     can be read.
  */
 public synchronized void sendPacket(PacketType packetType, ByteBuffer payload)
     throws DespotifyException {
   sendPacket(packetType, payload, "anonymous " + packetType.toString() + " command packet");
 }
  public void encodeMessage(byte[] data) {

    rawMessage = data;

    packetType = PacketType.UNKNOWN;
    packetId = data[1];

    for (PacketType pt : PacketType.values()) {
      if (pt.toByte() == data[1]) {
        packetType = pt;
        break;
      }
    }

    subType = data[2];
    seqNbr = data[3];
    id1 = data[4];

    if (data.length > 5) {
      id2 = data[5];
    }
  }
Beispiel #5
0
  /* Send command with payload (will be encrypted with stream cipher). */
  public synchronized void sendPacket(
      PacketType packetType, ByteBuffer payload, String packetDescription)
      throws DespotifyException {
    ByteBuffer buffer = ByteBuffer.allocate(1 + 2 + payload.remaining());

    /* Set IV. */
    this.session.shannonSend.nonce(IntegerUtilities.toBytes(this.session.keySendIv));

    /* Build packet. */
    buffer.put(packetType.getByteValue());
    buffer.putShort((short) payload.remaining());
    buffer.put(payload);

    byte[] bytes = buffer.array();
    byte[] mac = new byte[4];

    if (log.isInfoEnabled()) {
      byte[] arr = buffer.array();
      log.info(
          "sending " + packetDescription + ", " + arr.length + " bytes:\n" + Hex.log(arr, log));
    }

    /* Encrypt packet and get MAC. */
    this.session.shannonSend.encrypt(bytes);
    this.session.shannonSend.finish(mac);

    buffer = ByteBuffer.allocate(buffer.position() + 4);
    buffer.put(bytes);
    buffer.put(mac);
    buffer.flip();

    /* Send encrypted packet. */
    this.send(buffer, null);

    /* Increment IV. */
    this.session.keySendIv++;
  }
Beispiel #6
0
  /* Receive a packet (will be decrypted with stream cipher). */
  public void receivePacket() throws DespotifyException {
    byte[] header = new byte[3];
    PacketType packetType;
    int payloadLength, headerLength = 3, macLength = 4;

    /* Read header. */
    int received =
        this.receive(header, headerLength, log.isDebugEnabled() ? "packet header" : null);
    if (received != headerLength) {
      // todo reconnect?
      throw new RecievedInvalidHeaderException(
          "Failed to read header. Expected "
              + headerLength
              + " bytes, received "
              + received
              + ". todo try to reconnect once?");
    }

    /* Set IV. */
    this.session.shannonRecv.nonce(IntegerUtilities.toBytes(this.session.keyRecvIv));

    /* Decrypt header. */
    this.session.shannonRecv.decrypt(header);

    /* Get command and payload length from header. */
    ByteBuffer headerBuffer = ByteBuffer.wrap(header);

    byte commandByte = headerBuffer.get();
    packetType = PacketType.valueOf(commandByte & 0xff);

    if (packetType == null) {
      log.info(
          "Unknown command in received packet: "
              + packetType
              + " 0x"
              + Hex.toHex(new byte[] {commandByte}));
      // todo should we just ignore these?
    }

    payloadLength = headerBuffer.getShort() & 0xffff;

    /* Allocate buffer. Account for MAC. */
    byte[] bytes = new byte[payloadLength + macLength];
    ByteBuffer buffer = ByteBuffer.wrap(bytes);

    /* Limit buffer to payload length, so we can read the payload. */
    buffer.limit(payloadLength);

    try {
      for (int n = payloadLength, r; n > 0 && (r = this.channel.read(buffer)) > 0; n -= r) ;
    } catch (IOException e) {
      throw new DespotifyException("Failed to read payload: " + e.getMessage());
    }

    /* Extend it again to payload and mac length. */
    buffer.limit(payloadLength + macLength);

    try {
      for (int n = macLength, r; n > 0 && (r = this.channel.read(buffer)) > 0; n -= r) ;
    } catch (IOException e) {
      throw new DespotifyException("Failed to read MAC: " + e.getMessage());
    }

    /* Decrypt payload. */
    this.session.shannonRecv.decrypt(bytes);

    /* Get payload bytes from buffer (throw away MAC). */
    byte[] payload = new byte[payloadLength];

    buffer.flip();
    buffer.get(payload);

    /* Increment IV. */
    this.session.keyRecvIv++;

    if (log.isInfoEnabled()) {
      log.info(
          "received "
              + packetType
              + "-command packet payload, "
              + payloadLength
              + " bytes:\n"
              + Hex.log(payload, log));
    }

    /* Fire events. */
    for (CommandListener listener : this.listeners) {
      listener.commandReceived(packetType, payload);
    }
  }
Beispiel #7
0
 public Type getType() {
   return packetType.getType(type);
 }