Ejemplo n.º 1
0
  /**
   * Send standard response.
   *
   * @param code the response code to send
   * @param header the headers to include in the response
   * @throws IOException if an IO error occurs
   */
  public void sendResponse(int code, ObexByteBuffer header) throws IOException {
    int totalLength = 3;
    mData.reset();

    if (header != null) {
      totalLength += header.getLength();
      mData.write((byte) code);
      mData.write((byte) (totalLength >> 8));
      mData.write((byte) totalLength);
      mData.write(header);
    } else {
      mData.write((byte) code);
      mData.write((byte) 0x00);
      mData.write((byte) totalLength);
    }
    mData.read(mOutput);
    mOutput.flush();
  }
Ejemplo n.º 2
0
  /**
   * Handles a connect request from a client. This method will read the rest of the request from the
   * client. Assuming the request is valid, it will create a <code>HeaderSet</code> object to pass
   * to the <code>ServerRequestHandler</code> object. After the handler processes the request, this
   * method will create a reply message to send to the server with the response code provided.
   *
   * @throws IOException if an error occurred at the transport layer
   */
  private void handleConnectRequest() throws IOException {
    int packetLength;
    @SuppressWarnings("unused")
    int version;
    @SuppressWarnings("unused")
    int flags;
    int totalLength = 7;
    byte[] head = null;
    int code = -1;
    HeaderSet request = new HeaderSet();
    HeaderSet reply = new HeaderSet();

    /*
     * Read in the length of the OBEX packet, OBEX version, flags, and max
     * packet length
     */
    packetLength = mInput.read();
    packetLength = (packetLength << 8) + mInput.read();
    version = mInput.read();
    flags = mInput.read();
    mMaxPacketLength = mInput.read();
    mMaxPacketLength = (mMaxPacketLength << 8) + mInput.read();

    // should we check it?
    if (mMaxPacketLength > ObexHelper.MAX_PACKET_SIZE_INT) {
      mMaxPacketLength = ObexHelper.MAX_PACKET_SIZE_INT;
    }

    if (packetLength > ObexHelper.MAX_PACKET_SIZE_INT) {
      code = ResponseCodes.OBEX_HTTP_REQ_TOO_LARGE;
      totalLength = 7;
    } else {
      if (packetLength > 7) {
        mData.reset();
        mData.write(mInput, packetLength - 7);

        ObexHelper.updateHeaderSet(request, mData, null, mHeaderBuffer);
      }

      if (mListener.getConnectionId() != -1 && request.mConnectionID != null) {
        mListener.setConnectionId(ObexHelper.convertToLong(request.mConnectionID));
      } else {
        mListener.setConnectionId(1);
      }

      if (request.mAuthResp != null) {
        if (!handleAuthResp(request.mAuthResp)) {
          code = ResponseCodes.OBEX_HTTP_UNAUTHORIZED;
          mListener.onAuthenticationFailure(ObexHelper.getTagValue((byte) 0x01, request.mAuthResp));
        }
        request.mAuthResp = null;
      }

      if (code != ResponseCodes.OBEX_HTTP_UNAUTHORIZED) {
        if (request.mAuthChall != null) {
          handleAuthChall(request);
          reply.mAuthResp = new byte[request.mAuthResp.length];
          System.arraycopy(request.mAuthResp, 0, reply.mAuthResp, 0, reply.mAuthResp.length);
          request.mAuthChall = null;
          request.mAuthResp = null;
        }

        try {
          code = mListener.onConnect(request, reply);
          code = validateResponseCode(code);

          if (reply.nonce != null) {
            mChallengeDigest = new byte[16];
            System.arraycopy(reply.nonce, 0, mChallengeDigest, 0, 16);
          } else {
            mChallengeDigest = null;
          }
          long id = mListener.getConnectionId();
          if (id == -1) {
            reply.mConnectionID = null;
          } else {
            reply.mConnectionID = ObexHelper.convertToByteArray(id);
          }

          head = ObexHelper.createHeader(reply, false);
          totalLength += head.length;

          if (totalLength > mMaxPacketLength) {
            totalLength = 7;
            head = null;
            code = ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
          }
        } catch (Exception e) {
          e.printStackTrace();
          totalLength = 7;
          head = null;
          code = ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
        }
      }
    }

    // Compute Length of OBEX CONNECT packet
    byte[] length = ObexHelper.convertToByteArray(totalLength);

    /*
     * Write the OBEX CONNECT packet to the server. Byte 0: response code
     * Byte 1&2: Connect Packet Length Byte 3: OBEX Version Number
     * (Presently, 0x10) Byte 4: Flags (For TCP 0x00) Byte 5&6: Max OBEX
     * Packet Length (Defined in MAX_PACKET_SIZE) Byte 7 to n: headers
     */
    byte[] sendData = new byte[totalLength];
    sendData[0] = (byte) code;
    sendData[1] = length[2];
    sendData[2] = length[3];
    sendData[3] = (byte) 0x10;
    sendData[4] = (byte) 0x00;
    sendData[5] = (byte) (ObexHelper.MAX_PACKET_SIZE_INT >> 8);
    sendData[6] = (byte) (ObexHelper.MAX_PACKET_SIZE_INT & 0xFF);

    if (head != null) {
      System.arraycopy(head, 0, sendData, 7, head.length);
    }

    mOutput.write(sendData);
    mOutput.flush();
  }
Ejemplo n.º 3
0
  /**
   * Handles a disconnect request from a client. This method will read the rest of the request from
   * the client. Assuming the request is valid, it will create a <code>HeaderSet</code> object to
   * pass to the <code>ServerRequestHandler</code> object. After the handler processes the request,
   * this method will create a reply message to send to the server.
   *
   * @throws IOException if an error occurred at the transport layer
   */
  private void handleDisconnectRequest() throws IOException {
    int length;
    int code = ResponseCodes.OBEX_HTTP_OK;
    int totalLength = 3;
    byte[] head = null;
    HeaderSet request = new HeaderSet();
    HeaderSet reply = new HeaderSet();

    length = mInput.read();
    length = (length << 8) + mInput.read();

    if (length > ObexHelper.MAX_PACKET_SIZE_INT) {
      code = ResponseCodes.OBEX_HTTP_REQ_TOO_LARGE;
      totalLength = 3;
    } else {
      if (length > 3) {
        mData.reset();
        mData.write(mInput, length - 3);

        ObexHelper.updateHeaderSet(request, mData, null, mHeaderBuffer);
      }

      if (mListener.getConnectionId() != -1 && request.mConnectionID != null) {
        mListener.setConnectionId(ObexHelper.convertToLong(request.mConnectionID));
      } else {
        mListener.setConnectionId(1);
      }

      if (request.mAuthResp != null) {
        if (!handleAuthResp(request.mAuthResp)) {
          code = ResponseCodes.OBEX_HTTP_UNAUTHORIZED;
          mListener.onAuthenticationFailure(ObexHelper.getTagValue((byte) 0x01, request.mAuthResp));
        }
        request.mAuthResp = null;
      }

      if (code != ResponseCodes.OBEX_HTTP_UNAUTHORIZED) {

        if (request.mAuthChall != null) {
          handleAuthChall(request);
          request.mAuthChall = null;
        }

        try {
          mListener.onDisconnect(request, reply);
        } catch (Exception e) {
          sendResponse(ResponseCodes.OBEX_HTTP_INTERNAL_ERROR, null);
          return;
        }

        long id = mListener.getConnectionId();
        if (id == -1) {
          reply.mConnectionID = null;
        } else {
          reply.mConnectionID = ObexHelper.convertToByteArray(id);
        }

        head = ObexHelper.createHeader(reply, false);
        totalLength += head.length;

        if (totalLength > mMaxPacketLength) {
          totalLength = 3;
          head = null;
          code = ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
        }
      }
    }

    // Compute Length of OBEX CONNECT packet
    mData.reset();
    mData.write((byte) code);
    mData.write((byte) (totalLength >> 8));
    mData.write((byte) totalLength);
    if (head != null) {
      mData.write(head);
    }
    /*
     * Write the OBEX DISCONNECT packet to the server. Byte 0: response code
     * Byte 1&2: Connect Packet Length Byte 3 to n: headers
     */
    mData.read(mOutput);
    mOutput.flush();
  }
Ejemplo n.º 4
0
  /**
   * Handles a SETPATH request from a client. This method will read the rest of the request from the
   * client. Assuming the request is valid, it will create a <code>HeaderSet</code> object to pass
   * to the <code>ServerRequestHandler</code> object. After the handler processes the request, this
   * method will create a reply message to send to the server with the response code provided.
   *
   * @throws IOException if an error occurred at the transport layer
   */
  private void handleSetPathRequest() throws IOException {
    int length;
    int flags;
    @SuppressWarnings("unused")
    int constants;
    int totalLength = 3;
    byte[] head = null;
    int code = -1;
    HeaderSet request = new HeaderSet();
    HeaderSet reply = new HeaderSet();

    length = mInput.read();
    length = (length << 8) + mInput.read();
    flags = mInput.read();
    constants = mInput.read();

    if (length > ObexHelper.MAX_PACKET_SIZE_INT) {
      code = ResponseCodes.OBEX_HTTP_REQ_TOO_LARGE;
      totalLength = 3;
    } else {
      if (length > 5) {
        mData.reset();
        mData.write(mInput, length - 5);

        ObexHelper.updateHeaderSet(request, mData, null, mHeaderBuffer);

        if (mListener.getConnectionId() != -1 && request.mConnectionID != null) {
          mListener.setConnectionId(ObexHelper.convertToLong(request.mConnectionID));
        } else {
          mListener.setConnectionId(1);
        }
        // the Auth chan is initiated by the server, client sent back the authResp .
        if (request.mAuthResp != null) {
          if (!handleAuthResp(request.mAuthResp)) {
            code = ResponseCodes.OBEX_HTTP_UNAUTHORIZED;
            mListener.onAuthenticationFailure(
                ObexHelper.getTagValue((byte) 0x01, request.mAuthResp));
          }
          request.mAuthResp = null;
        }
      }

      if (code != ResponseCodes.OBEX_HTTP_UNAUTHORIZED) {
        // the Auth challenge is initiated by the client
        // the server will send back the authResp to the client
        if (request.mAuthChall != null) {
          handleAuthChall(request);
          reply.mAuthResp = new byte[request.mAuthResp.length];
          System.arraycopy(request.mAuthResp, 0, reply.mAuthResp, 0, reply.mAuthResp.length);
          request.mAuthChall = null;
          request.mAuthResp = null;
        }
        boolean backup = false;
        boolean create = true;
        if (!((flags & 1) == 0)) {
          backup = true;
        }
        if (!((flags & 2) == 0)) {
          create = false;
        }

        try {
          code = mListener.onSetPath(request, reply, backup, create);
        } catch (Exception e) {
          sendResponse(ResponseCodes.OBEX_HTTP_INTERNAL_ERROR, null);
          return;
        }

        code = validateResponseCode(code);

        if (reply.nonce != null) {
          mChallengeDigest = new byte[16];
          System.arraycopy(reply.nonce, 0, mChallengeDigest, 0, 16);
        } else {
          mChallengeDigest = null;
        }

        long id = mListener.getConnectionId();
        if (id == -1) {
          reply.mConnectionID = null;
        } else {
          reply.mConnectionID = ObexHelper.convertToByteArray(id);
        }

        head = ObexHelper.createHeader(reply, false);
        totalLength += head.length;

        if (totalLength > mMaxPacketLength) {
          totalLength = 3;
          head = null;
          code = ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
        }
      }
    }

    // Compute Length of OBEX SETPATH packet
    mData.reset();
    mData.write((byte) code);
    mData.write((byte) (totalLength >> 8));
    mData.write((byte) totalLength);
    if (head != null) {
      mData.write(head);
    }
    /*
     * Write the OBEX SETPATH packet to the server. Byte 0: response code
     * Byte 1&2: Connect Packet Length Byte 3 to n: headers
     */
    mData.read(mOutput);
    mOutput.flush();
  }