Example #1
0
    /**
     * Reads the peer's address. First a cookie has to be sent which has to match my own cookie,
     * otherwise the connection will be refused
     */
    private Address readPeerAddress(Socket client_sock) throws Exception {
      int timeout = client_sock.getSoTimeout();
      client_sock.setSoTimeout(peer_addr_read_timeout);

      try {
        // read the cookie first
        byte[] input_cookie = new byte[cookie.length];
        in.readFully(input_cookie, 0, input_cookie.length);
        if (!matchCookie(input_cookie))
          throw new SocketException(
              "ConnectionMap.Connection.readPeerAddress(): cookie read by "
                  + getLocalAddress()
                  + " does not match own cookie; terminating connection");
        // then read the version
        short version = in.readShort();

        if (!Version.isBinaryCompatible(version)) {
          if (log.isWarnEnabled())
            log.warn(
                new StringBuilder("packet from ")
                    .append(client_sock.getInetAddress())
                    .append(':')
                    .append(client_sock.getPort())
                    .append(" has different version (")
                    .append(Version.print(version))
                    .append(") from ours (")
                    .append(Version.printVersion())
                    .append("). This may cause problems")
                    .toString());
        }
        Address client_peer_addr = new IpAddress();
        client_peer_addr.readFrom(in);

        updateLastAccessed();
        return client_peer_addr;
      } finally {
        client_sock.setSoTimeout(timeout);
      }
    }
Example #2
0
  /**
   * Reads the peer's address. First a cookie has to be sent which has to match my own cookie,
   * otherwise the connection will be refused
   */
  protected Address readPeerAddress(Socket client_sock) throws Exception {
    int timeout = client_sock.getSoTimeout();
    client_sock.setSoTimeout(server.peerAddressReadTimeout());

    try {
      // read the cookie first
      byte[] input_cookie = new byte[cookie.length];
      in.readFully(input_cookie, 0, input_cookie.length);
      if (!Arrays.equals(cookie, input_cookie))
        throw new SocketException(
            "BaseServer.TcpConnection.readPeerAddress(): cookie read by "
                + server.localAddress()
                + " does not match own cookie; terminating connection");
      // then read the version
      short version = in.readShort();
      if (!Version.isBinaryCompatible(version))
        throw new IOException(
            "packet from "
                + client_sock.getInetAddress()
                + ":"
                + client_sock.getPort()
                + " has different version ("
                + Version.print(version)
                + ") from ours ("
                + Version.printVersion()
                + "); discarding it");
      short addr_len = in.readShort(); // only needed by NioConnection

      Address client_peer_addr = new IpAddress();
      client_peer_addr.readFrom(in);
      updateLastAccessed();
      return client_peer_addr;
    } finally {
      client_sock.setSoTimeout(timeout);
    }
  }