Esempio n. 1
0
File: Peer.cpp Progetto: manut/ACE
int
Peer_Connector::open_connector (Peer_Handler *&peer_handler,
                                u_short port)
{
  // This object only gets allocated once and is just recycled
  // forever.
  ACE_NEW_RETURN (peer_handler,
                  Peer_Handler,
                  -1);

  ACE_INET_Addr addr (port,
                      Options::instance ()->connector_host ());

  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("connecting to %s:%d\n"),
              addr.get_host_name (),
              addr.get_port_number ()));

  if (this->connect (peer_handler, addr) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("%p\n"),
                       ACE_TEXT ("connect")),
                      -1);
  else
    ACE_DEBUG ((LM_DEBUG,
                ACE_TEXT ("connected to %C:%d\n"),
                addr.get_host_name (),
                addr.get_port_number ()));
  return 0;
}
Esempio n. 2
0
/// This method is called by a TcpConnection object that has been
/// created and opened by our acceptor_ as a result of passively
/// accepting a connection on our local address.  Ultimately, the connection
/// object needs to be paired with a DataLink object that is (or will be)
/// expecting this passive connection to be established.
void
TcpTransport::passive_connection(const ACE_INET_Addr& remote_address,
                                 const TcpConnection_rch& connection)
{
  DBG_ENTRY_LVL("TcpTransport", "passive_connection", 6);

  const PriorityKey key(connection->transport_priority(),
                        remote_address,
                        remote_address == tcp_config_->local_address_,
                        connection->is_connector());

  VDBG_LVL((LM_DEBUG, ACE_TEXT("(%P|%t) TcpTransport::passive_connection() - ")
            ACE_TEXT("established with %C:%d.\n"),
            remote_address.get_host_name(),
            remote_address.get_port_number()), 2);

  GuardType connection_guard(connections_lock_);
  TcpDataLink_rch link;
  {
    GuardType guard(links_lock_);
    links_.find(key, link);
  }

  if (!link.is_nil()) {
    connection_guard.release();

    if (connect_tcp_datalink(link, connection) == -1) {
      VDBG_LVL((LM_ERROR,
                ACE_TEXT("(%P|%t) ERROR: connect_tcp_datalink failed\n")), 5);
      GuardType guard(links_lock_);
      links_.unbind(key);

    } else {
      con_checker_->add(connection);
    }

    return;
  }

  // If we reach this point, this link was not in links_, so the
  // accept_datalink() call hasn't happened yet.  Store in connections_ for the
  // accept_datalink() method to find.
  VDBG_LVL((LM_DEBUG, "(%P|%t) # of bef connections: %d\n", connections_.size()), 5);
  const ConnectionMap::iterator where = connections_.find(key);

  if (where != connections_.end()) {
    ACE_ERROR((LM_ERROR,
               ACE_TEXT("(%P|%t) ERROR: TcpTransport::passive_connection() - ")
               ACE_TEXT("connection with %C:%d at priority %d already exists, ")
               ACE_TEXT("overwriting previously established connection.\n"),
               remote_address.get_host_name(),
               remote_address.get_port_number(),
               connection->transport_priority()));
  }

  connections_[key] = connection;
  VDBG_LVL((LM_DEBUG, "(%P|%t) # of after connections: %d\n", connections_.size()), 5);

  con_checker_->add(connection);
}
Esempio n. 3
0
int
UnidrawImportHandler::open (void *)
{
    ACE_INET_Addr addr;

    if (this->peer ().get_remote_addr (addr) == -1)
        return -1;
    else
    {
        ACE_OS::strncpy (this->peer_name_,
                         addr.get_host_name (),
                         MAXHOSTNAMELEN + 1);

        if (ComterpHandler::reactor_singleton()->register_handler
                (this, ACE_Event_Handler::READ_MASK) == -1)
            ACE_ERROR_RETURN ((LM_ERROR,
                               "(%P|%t) can't register with reactor\n"), -1);
#if 0
        else if (ComterpHandler::reactor_singleton()->schedule_timer
                 (this,
                  (const void *) this,
                  ACE_Time_Value (10),
                  ACE_Time_Value (10)) == -1)
            ACE_ERROR_RETURN ((LM_ERROR,
                               "can'(%P|%t) t register with reactor\n"), -1);
#endif
        else
            cerr << this->peer_name_ << " connected to import port\n";
        return 0;
    }
}
Esempio n. 4
0
int
TAO_SCIOP_Endpoint::set (const ACE_INET_Addr &addr,
                        int use_dotted_decimal_addresses)
{
  char tmp_host[MAXHOSTNAMELEN + 1];

  if (use_dotted_decimal_addresses
      || addr.get_host_name (tmp_host, sizeof (tmp_host)) != 0)
    {
      const char *tmp = addr.get_host_addr ();
      if (tmp == 0)
        {
          if (TAO_debug_level > 0)
            TAOLIB_DEBUG ((LM_DEBUG,
                        ACE_TEXT ("\n\nTAO (%P|%t) ")
                        ACE_TEXT ("SCIOP_Endpoint::set ")
                        ACE_TEXT ("- %p\n\n"),
                        ACE_TEXT ("cannot determine hostname")));
          return -1;
        }
      else
        this->host_ = tmp;
    }
  else
    this->host_ = CORBA::string_dup (tmp_host);

  this->port_ = addr.get_port_number();

  return 0;
}
int
Logging_Handler::open (void)
{
  ACE_INET_Addr addr;

  if (this->cli_stream_.get_remote_addr (addr) == -1)
    return -1;
  else
    {
      ACE_OS::strncpy (this->host_name_,
                       addr.get_host_name (),
                       MAXHOSTNAMELEN + 1);

      if (REACTOR::instance ()->register_handler (this, READ_MASK) == -1)
        ACE_ERROR_RETURN ((LM_ERROR,
                           "(%P|%t) can't register with reactor\n"),
                          -1);
      else if (REACTOR::instance ()->schedule_timer
               (this, (const void *) this,
                ACE_Time_Value (2),
                ACE_Time_Value (2)) == -1)
        ACE_ERROR_RETURN ((LM_ERROR,
                           "can'(%P|%t) t register with reactor\n"),
                          -1);
      else
        ACE_DEBUG ((LM_DEBUG,
                    "(%P|%t) connected with %s\n",
                    this->host_name_));
      return 0;
    }
}
Esempio n. 6
0
static void
handle_connections (ACE_SSL_SOCK_Acceptor &peer_acceptor,
                    size_t &n_handles)
{
  if (ACE_BIT_ENABLED (poll_array[0].revents, POLLIN))
    {
      ACE_SSL_SOCK_Stream new_stream;

      ACE_INET_Addr client;
      ACE_Time_Value nonblock (0, 0);

      // Handle all pending connection requests (note use of "polling"
      // feature that doesn't block).

      while (ACE_OS::poll (poll_array, 1, nonblock) > 0)
        if (peer_acceptor.accept (new_stream, &client) == -1)
          ACE_OS::perror ("accept");
        else
          {
            const char *s = client.get_host_name ();

            ACE_ASSERT (s != 0);
            ACE_DEBUG ((LM_DEBUG,
                        "(%P|%t) client %s\n",
                        s));
            poll_array[n_handles++].fd = new_stream.get_handle ();
          }
    }
}
Esempio n. 7
0
    bool Session_T<ACE_SYNCH_USE>::attach_connection (connection_type* connection)
      {
        INET_TRACE ("ACE_HTTP_Session::attach_connection");

        if (!connection->is_connected ())
          return false;

        this->close ();

        ACE_INET_Addr remote;
        connection->peer ().get_remote_addr (remote);
        this->host_ = remote.get_host_name ();
        this->port_ = remote.get_port_number ();

        this->connection_ = connection;
        this->connection_->add_reference ();

        ACE_NEW_NORETURN (this->sock_stream_,
                          sock_stream_type (this->connection_));

        if (this->sock_stream_)
          {
            this->keep_alive_ = true;
            this->keep_alive_timeout_ = ACE_Time_Value::zero;
            this->cannot_reconnect_ = true;
            return true;
          }
        else
          {
            this->close ();
            return false;
          }
      }
int chromatic_handler::open ( void* accept )
{
  
	  ACE_Trace _( ACE_TEXT( "chromatic_handler::open" ) , __LINE__ );
	  output_boiler_plate();
	  
	  map_commands();
	  silent( false );

	  acceptor ( (chromatic_imap_acceptor_ptr) accept);

	  m_ptrImap = new chromatic_imap_mgr(  acceptor()->Data()->default_host() ,
										   acceptor()->Data()->dn()	,
										   this );
      
	
	  string strConcurrency;

	  //check for timeout every 30 seconds
	  ACE_Time_Value reschedule( m_maximumTimeToWait.sec() / 60 );

	  m_dwTimerToken = reactor()->schedule_timer( this , (void*) timer_id , m_maximumTimeToWait , reschedule );

	  if ( concurrency () == concurrency_t::thread_per_connection_ )
	  {
		return activate ( THR_DETACHED );
	  }

	  reactor ( acceptor()->reactor () );

	  ACE_INET_Addr addr;
  
	  if ( peer ().get_remote_addr (addr) == -1 )
	  {
		return ( -1 );
	  }  

	  if ( reactor ()->register_handler ( this,
										  REGISTER_MASK ) == -1 )
	  {
		ACE_ERROR_RETURN ((LM_ERROR,
						   "(%P|%t) can't register with reactor\n"),
						  -1);
	  }  
  
  
	  ACE_DEBUG ((LM_DEBUG,
				  "(%P|%t) connected with %s\n",
				  addr.get_host_name ()));  

	  

	  return ( 0L );  
	  
}
Esempio n. 9
0
int
TAO_IIOP_Endpoint::set (const ACE_INET_Addr &addr,
                        int use_dotted_decimal_addresses)
{
  char tmp_host[MAXHOSTNAMELEN + 1];

#if defined (ACE_HAS_IPV6)
  this->is_ipv6_decimal_ = false; // Reset
#endif /* ACE_HAS_IPV6 */

  if (use_dotted_decimal_addresses
      || addr.get_host_name (tmp_host, sizeof (tmp_host)) != 0)
    {
      if (use_dotted_decimal_addresses == 0 && TAO_debug_level > 5)
        {
          TAOLIB_DEBUG ((LM_DEBUG,
                      ACE_TEXT ("TAO (%P|%t) - IIOP_Endpoint::set, ")
                      ACE_TEXT ("%p\n"),
                      ACE_TEXT ("cannot determine hostname")));
        }

      const char *tmp = addr.get_host_addr ();
      if (tmp == 0)
        {
          if (TAO_debug_level > 0)
            {
              TAOLIB_ERROR ((LM_ERROR,
                          ACE_TEXT ("TAO (%P|%t) - IIOP_Endpoint::set, ")
                          ACE_TEXT ("%p\n"),
                          ACE_TEXT ("cannot determine hostname and hostaddr")));
            }
          return -1;
        }
      else
        {
          this->host_ = tmp;
#if defined (ACE_HAS_IPV6)
          if (addr.get_type () == PF_INET6)
            this->is_ipv6_decimal_ = true;
#endif /* ACE_HAS_IPV6 */
        }
    }
  else
    this->host_ = CORBA::string_dup (tmp_host);

  this->port_ = addr.get_port_number();

  return 0;
}
Esempio n. 10
0
int
TAO_AV_SCTP_SEQ_Acceptor::open_default (TAO_Base_StreamEndPoint *endpoint,
                                        TAO_AV_Core *av_core,
                                        TAO_FlowSpec_Entry *entry,
                                        TAO_AV_Flow_Protocol_Factory *factory,
                                        TAO_AV_Core::Flow_Component flow_comp)
{
    this->flow_protocol_factory_ = factory;
    this->av_core_ = av_core;
    this->endpoint_ = endpoint;
    this->entry_ = entry;
    if (flow_comp == TAO_AV_Core::TAO_AV_CONTROL)
        this->flowname_ = TAO_AV_Core::get_control_flowname (entry->flowname());
    else
        this->flowname_ = entry->flowname ();

    ACE_INET_Addr *address;
    ACE_NEW_RETURN (address,
                    ACE_INET_Addr ("0"),
                    -1);

    int result = this->acceptor_.acceptor_open (this,
                 av_core->reactor (),
                 *address,
                 entry);


    if (result < 0)
        ORBSVCS_ERROR_RETURN ((LM_ERROR,
                               "TAO_AV_SCTP_SEQ_Acceptor::open failed"),
                              -1);

    this->acceptor_.acceptor ().get_local_addr (*address);

    address->set (address->get_port_number (),
                  address->get_host_name ());

    char buf[BUFSIZ];
    address->addr_to_string (buf,BUFSIZ);

    if (TAO_debug_level > 0)
        ORBSVCS_DEBUG ((LM_DEBUG,
                        "TAO_AV_SCTP_SEQ_Acceptor::open_default: %s\n",
                        buf));

    entry->set_local_addr (address);

    return 0;
}
Esempio n. 11
0
    bool Session_T<ACE_SYNCH_USE>::connect (connection_type* connection)
      {
        INET_TRACE ("ACE_FTP_Session::connect(connection)");

        this->close ();

        if (connection->is_connected ())
          {
            ACE_INET_Addr remote;
            connection->peer ().get_remote_addr (remote);
            this->host_ = remote.get_host_name ();
            this->port_ = remote.get_port_number ();
          }
        else
          {
            typedef ACE_Connector<connection_type, ACE_SOCK_CONNECTOR> connector_type;

            connector_type connector;
            if (connector.connect (connection,
                                   ACE_INET_Addr (this->host_.c_str (),
                                                  this->port_)) == -1)
              {
                INET_ERROR (1, (LM_ERROR, DLINFO
                                ACE_TEXT ("(%d) ACE_FTP_Session::connect(connection) - ")
                                ACE_TEXT ("failed to connect; host=%C, port=%d"),
                                ACE_OS::last_error (), this->host_.c_str (), this->port_));
                return false;
              }
          }

        this->connection_ = connection;
        this->connection_->add_reference ();

        ACE_NEW_NORETURN (this->sock_stream_,
                          sock_stream_type (this->connection_));

        if (this->sock_stream_)
          {
            this->new_connect_ = true;
            this->cannot_reconnect_ = true;
            return true;
          }
        else
          {
            this->close ();
            return false;
          }
      }
Esempio n. 12
0
int
Mem_Map_Stream::open (STRAT_CONNECTOR *connector,
                      const ACE_INET_Addr &addr)
{
  svc_handler_ = 0;

  // Connect to the server at <addr>. If the handler has to be
  // connected to the server again, the Caching strategy takes care
  // and uses the same connection.
  if (connector->connect (svc_handler_,
                          addr) == -1)
    {

      ACE_ERROR_RETURN ((LM_ERROR,
                         "%p %s %d\n",
                         "Connect failed",
                         addr.get_host_name (),
                         addr.get_port_number ()),
                        -1);
    }
  // Create a temporary filename.
  ACE_FILE_Addr file (ACE_sap_any_cast (ACE_FILE_Addr &));

  // Create the temporary file via the <ACE_Mem_Map> class API.
  if (this->mem_map_.open (file.get_path_name (),
                          O_RDWR | O_CREAT | O_APPEND,
                          ACE_DEFAULT_FILE_PERMS) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "open"),
                      -1);
  // Make sure to unlink this right away so that if this process
  // crashes these files will be removed automatically.
#if 0
  else if (ACE_OS::unlink (file.get_path_name ()) == -1)
  ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "unlink"),
                      -1);
  else
#endif
    // Initialize all the position pointers to 0.
    this->rewind ();

  return 0;
}
Esempio n. 13
0
	int accept_connections(){
		if (peer_acceptor_.get_local_addr(server_addr_) == -1){
			ACE_ERROR_RETURN((LM_ERROR, "%p\n", "Error in get_local_addr"), 1);
		}
		ACE_DEBUG((LM_DEBUG, "Starting server at port %d\n", server_addr_.get_port_number()));
		while (1){
			ACE_Time_Value timeout(ACE_DEFAULT_TIMEOUT);
			if (peer_acceptor_.accept(new_stream_, &client_addr_, &timeout)){
				ACE_ERROR((LM_ERROR, "%p\n", "accept"));
				continue;
			}
			else{
				ACE_DEBUG((LM_DEBUG, "Connection established with remote %s:%d\n",
					client_addr_.get_host_name(), client_addr_.get_port_number()));
				handle_connection();
			}

		}
	}
Esempio n. 14
0
TAO_Trading_Loader::TAO_Trading_Loader (void)
  : federate_ (0),
    ior_output_file_ (0),
    bootstrapper_ (0)
{
  char *trader_name = CORBA::string_alloc (MAXHOSTNAMELEN + 10);

  if (trader_name != 0)
    {
      // The trader name is the concatenation of the local host name
      // and the server's process id.
      char host_name[MAXHOSTNAMELEN + 1];
      ACE_INET_Addr localhost ((u_short) 0);
      if (localhost.get_host_name (host_name, sizeof (host_name)) != 0)
        {
          const char *tmp = localhost.get_host_addr ();
          if (tmp == 0)
            ORBSVCS_DEBUG ((LM_DEBUG,
                        ACE_TEXT ("\n\nTAO Trading Service (%P|%t) ")
                        ACE_TEXT ("TAO_Trading_Loader ")
                        ACE_TEXT ("- %p\n\n"),
                        ACE_TEXT ("cannot determine hostname")));
          else
            ACE_OS::strcpy (host_name, tmp);
        }

      ACE_OS::sprintf (trader_name,
                       "%s_%ld",
                       host_name,
                       static_cast<long> (ACE_OS::getpid ()));

      for (char *dot = 0;
           (dot = ACE_OS::strchr (trader_name, '.')) != 0;
           *dot = '_')
        continue;

      ORBSVCS_DEBUG ((LM_DEBUG,
                  "*** Trading Service %C initializing.\n",
                  trader_name));

      this->name_ = trader_name;
    }
}
Esempio n. 15
0
bool
TcpTransport::configure_i(TransportInst* config)
{
  DBG_ENTRY_LVL("TcpTransport", "configure_i", 6);

  // Downcast the config argument to a TcpInst*
  TcpInst* tcp_config =
    static_cast<TcpInst*>(config);

  if (tcp_config == 0) {
    // The downcast failed.
    ACE_ERROR_RETURN((LM_ERROR,
                      "(%P|%t) ERROR: Failed downcast from TransportInst "
                      "to TcpInst.\n"),
                     false);
  }

  this->create_reactor_task();

  // Ask our base class for a "copy" of the reference to the reactor task.
  this->reactor_task_ = reactor_task();

  connector_.open(reactor_task_->get_reactor());

  // Make a "copy" of the reference for ourselves.
  tcp_config->_add_ref();
  this->tcp_config_ = tcp_config;

  // Open the reconnect task
  if (this->con_checker_->open()) {
    ACE_ERROR_RETURN((LM_ERROR,
                      ACE_TEXT("(%P|%t) ERROR: connection checker failed to open : %p\n"),
                      ACE_TEXT("open")),
                     false);
  }

  // Open our acceptor object so that we can accept passive connections
  // on our this->tcp_config_->local_address_.

  if (this->acceptor_->open(this->tcp_config_->local_address_,
                            this->reactor_task_->get_reactor()) != 0) {
    // Remember to drop our reference to the tcp_config_ object since
    // we are about to return -1 here, which means we are supposed to
    // keep a copy after all.
    TcpInst_rch cfg = this->tcp_config_._retn();

    ACE_ERROR_RETURN((LM_ERROR,
                      ACE_TEXT("(%P|%t) ERROR: Acceptor failed to open %C:%d: %p\n"),
                      cfg->local_address_.get_host_addr(),
                      cfg->local_address_.get_port_number(),
                      ACE_TEXT("open")),
                     false);
  }

  // update the port number (incase port zero was given).
  ACE_INET_Addr address;

  if (this->acceptor_->acceptor().get_local_addr(address) != 0) {
    ACE_ERROR((LM_ERROR,
               ACE_TEXT("(%P|%t) ERROR: TcpTransport::configure_i ")
               ACE_TEXT("- %p"),
               ACE_TEXT("cannot get local addr\n")));
  }

  VDBG_LVL((LM_DEBUG,
            ACE_TEXT("(%P|%t) TcpTransport::configure_i listening on %C:%hu\n"),
            address.get_host_name(), address.get_port_number()), 2);

  unsigned short port = address.get_port_number();
  std::stringstream out;
  out << port;

  // As default, the acceptor will be listening on INADDR_ANY but advertise with the fully
  // qualified hostname and actual listening port number.
  if (tcp_config_->local_address_.is_any()) {
    std::string hostname = get_fully_qualified_hostname();

    this->tcp_config_->local_address_.set(port, hostname.c_str());
    this->tcp_config_->local_address_str_ = hostname;
    this->tcp_config_->local_address_str_ += ':' + out.str();
  }

  // Now we got the actual listening port. Update the port nnmber in the configuration
  // if it's 0 originally.
  else if (tcp_config_->local_address_.get_port_number() == 0) {
    this->tcp_config_->local_address_.set_port_number(port);

    if (this->tcp_config_->local_address_str_.length() > 0) {
      size_t pos = this->tcp_config_->local_address_str_.find_last_of(
                     ":]", std::string::npos, 2);
      std::string str = this->tcp_config_->local_address_str_.substr(0, pos + 1);

      if (this->tcp_config_->local_address_str_[pos] == ']') {
        str += ":";
      }

      str += out.str();
      this->tcp_config_->local_address_str_ = str;
    }
  }

  // Ahhh...  The sweet smell of success!
  return true;
}
Esempio n. 16
0
static ACE_THR_FUNC_RETURN
process(void* arg){
  ACE_INET_Addr addr;
  ACE_SOCK_Stream stream;
  ACE_HANDLE handle = (ACE_HANDLE) (intptr_t) arg;
  stream.set_handle(handle);
  /*make sure we're not in non-blocking mode.*/
  if(stream.disable(ACE_NONBLOCK) == -1){
    ACE_ERROR_RETURN ((LM_ERROR,
		       "%p\n",
		       "disable"),
		      0);
  }
  else if(stream.get_remote_addr(addr) == -1){
    ACE_ERROR_RETURN ((LM_ERROR,
		       "%p\n",
		       "get_remote_addr"),
		      0);
  }
  ACE_DEBUG ((LM_INFO,
	      "(%P|%t:%l) client %s connected from %d\n",
	      addr.get_host_name (),
	      addr.get_port_number ()));
  
  int r_bytes = 0;
  char buf[SIZE];
  ACE_CString cs;
  do{
    r_bytes = stream.recv(buf, SIZE);

    if(r_bytes == 0 || r_bytes == -1){
      ACE_DEBUG((LM_INFO,
		 "(%P|%t:%l) r_bytes = %d, exit from the loop\n", r_bytes));
      break;
    }
    for(int i=0; i< r_bytes; i++){
      cs += buf[i];
    }
  }while(true);
  stream.close_reader();
  /*
  ACE_DEBUG((LM_INFO,
	     "%s\n", cs.c_str()));
  */
  /*the layout of the message would be:
   * ^^pq||step1$$
   * 1st step: get the d by pq
   * 2nd calculate
   * 3rd send back the digest
   */
  int p0, p1;
  int len = cs.length();
  p0 = 2;
  p1 = cs.find("||");
  ACE_CString pq = cs.substr(2, p1 - 2);
  ACE_CString step1 = cs.substr(p1 + 2, len - p1 -4);
  /*
  ACE_DEBUG((LM_INFO,
	     "pq = %s\n step1 = %s\n", pq.c_str(), step1.c_str()));
  */
  //get the d
  ACE_CString sql = "select d,textid from player0 where pq='";
  sql += pq;
  sql += "'";
  /*
  ACE_DEBUG((LM_INFO,
	     "sql = %s\n", sql.c_str()));
*/
  PGconn* con;
  con = PQconnectdb("host=45.33.3.188 port=5432 dbname=nv user=dec");
  PGresult* res;
  if(PQstatus(con)!= CONNECTION_OK){
    ACE_DEBUG((LM_INFO,
	       "Connection to database failed:%s\n",
	       PQerrorMessage(con)));
    reclaim_conn(con);
  }
  res = PQexec(con, sql.c_str());
  int n = PQntuples(res);
  if(n != 1){
    ACE_ERROR((LM_ERROR,
	       "there is significant error: %d\n", n));
    //    return 0;
  }
  ACE_CString d = "";
  d += PQgetvalue(res, 0, 0);
  /*
  ACE_DEBUG((LM_INFO,
	     "d = %s\n", d.c_str()));
*/
  bn* pq_ = from_hex(&pq);
  bn* d_ = from_hex(&d);
  bn* step1_ = from_hex(&step1);
  bn* step2_ = npmod(step1_ , d_ , pq_ );
  ACE_CString* step2 = step2_->to_hex();
  
  //check if the pq is taking the ownnership?

  /* if pq is 'centralbank', bypass the check, otherwise check the payer whether has the ownnership of the note;
   * select count(*) from ownership0 o, player0 p where o.owner = p.textid  and o.note='0x12345678' and p.pq='0x12345678';
   */
  bn* e_ = new bn(1);
  e_->addat(0,0x10001);
  bn* step3_ = npmod(step2_ , e_ , pq_ );
  ACE_CString* rawMsg = encode(step3_);
  ACE_DEBUG((LM_INFO,
	     "%T :%l step3 = %s\n", rawMsg->c_str()));
  if(verify_ownership(rawMsg, pq)){
    //update the ownership;
    int pos1 = rawMsg->find("->");
    ACE_CString note = rawMsg->substr(2, pos1 -2);
    int rawlen = rawMsg->length();
    ACE_CString id = rawMsg->substr(pos1 + 2, rawlen - pos1 - 6);
    update_ownership(note, id);
    /*insert the log entry*/
    insert_logentry(pq, *step2, *rawMsg, note, id);
    stream.send( step2->c_str(), step2->length());
    /*
    ACE_DEBUG((LM_INFO,
	       "%T :%l the step2 message is:\n%s\n", step2->c_str()));
    */
  }else{
    ACE_DEBUG((LM_INFO,
	       "%T :%l invalid transfer\n"));
    ACE_CString reply ="invalid transfer\n";
    stream.send( reply.c_str(), reply.length());
  }
  delete step2 ;
  delete pq_;
  delete d_;
  delete step1_;
  delete step2_;
  delete e_;
  delete step3_;
  delete rawMsg;
  /*
  stream.close_writer();
  */
  stream.close();
  ACE_DEBUG((LM_INFO,
	     "%T :%l closed the stream\n"));

}
int
ACE_Service_Manager::handle_input (ACE_HANDLE)
{
  ACE_TRACE ("ACE_Service_Manager::handle_input");

  // Try to find out if the implementation of the reactor that we are
  // using requires us to reset the event association for the newly
  // created handle. This is because the newly created handle will
  // inherit the properties of the listen handle, including its event
  // associations.
  int reset_new_handle =
    ACE_Reactor::instance ()->uses_event_associations ();

  if (this->acceptor_.accept (this->client_stream_, // stream
                              0, // remote address
                              0, // timeout
                              1, // restart
                              reset_new_handle  // reset new handler
                              ) == -1)
    return -1;

  if (this->debug_)
    {
      ACE_DEBUG ((LM_DEBUG,
                  ACE_LIB_TEXT ("client_stream fd = %d\n"),
                 this->client_stream_.get_handle ()));
      ACE_INET_Addr sa;
      if (this->client_stream_.get_remote_addr (sa) == -1)
        return -1;

      ACE_DEBUG ((LM_DEBUG,
                  ACE_LIB_TEXT ("accepted from host %s at port %d\n"),
                  ACE_TEXT_CHAR_TO_TCHAR (sa.get_host_name ()),
                  sa.get_port_number ()));
    }

  ACE_TCHAR request[BUFSIZ];
  ACE_TCHAR* offset = request;
  ssize_t remaining = sizeof (request);

  // Read service request from client.

  ssize_t result;

  // Keep looping until we actually get the request.  Note that Win32
  // sets the socket into non-blocking mode, so we may need to loop if
  // the system is heavily loaded.  Read bytes into the buffer until a
  // '\n' or '\r' is found in the buffer, otherwise the buffer
  // contains an incomplete string.

  int error;
  do
    {
      result = client_stream_.recv (offset, remaining);
      error = errno;
      if (result == 0 && error != EWOULDBLOCK)
        remaining = 0;

      if (result >= 0)
        {
          if ((remaining -= result) <= 0)
            {
              ACE_DEBUG ((LM_ERROR,
                          ACE_LIB_TEXT ("Request buffer overflow.\n")));
              result = 0;
              break;
            }

          offset += result;
          *offset = 0;

          if (ACE_OS::strchr (request, '\r') != 0
              || ACE_OS::strchr (request, '\n') != 0)
            remaining = 0;
        }
    }
  while (result == -1 && error == EWOULDBLOCK || remaining > 0);

  switch (result)
    {
    case -1:
      if (this->debug_)
        ACE_DEBUG ((LM_ERROR,
                    ACE_LIB_TEXT ("%p\n"),
                    ACE_LIB_TEXT ("recv")));
      break;
    case 0:
      return 0;
      /* NOTREACHED */
    default:
      {
        ACE_Event_Handler *old_signal_handler = 0;
        ACE_Reactor::instance ()->register_handler (SIGPIPE,
                                                    this,
                                                    0,
                                                    &old_signal_handler);

        this->process_request (request);

        // Restore existing SIGPIPE handler
        ACE_Reactor::instance ()->register_handler (SIGPIPE,
                                                    old_signal_handler);
      }
    }

  if (this->client_stream_.close () == -1 && this->debug_)
    ACE_DEBUG ((LM_ERROR,
                ACE_LIB_TEXT ("%p\n"),
                ACE_LIB_TEXT ("close")));
  return 0;
}
Esempio n. 18
0
/* Our goal here is to develop a client that can send a datagram to a
   server running on a known host.  We'll use a command-line argument
   to specify the hostname instead of hard-coding it.  */
int
main (int argc,char *argv[])
{
  /* All datagrams must have a point of origin.  Since we intend to
    transmit instead of receive, we initialize an address with zero
    and let the OS choose a port for us.  We could have chosen our own
    value between 1025 and 65535 as long as it isn't already in use.

    The biggest difference between client and server when datagrams
    are used is the fact that servers tend to have a known/fixed
    address at which they listen and clients tend to have arbitrary
    addresses assigned by the OS.  */
  ACE_INET_Addr local((u_short) 0);

  /* And here is our datagram object.  */
  ACE_SOCK_Dgram dgram;

  /* Notice that this looks a lot like the server application.
    There's no difference in creating server datagrams an client
    datagrams.  You can even use a zero-constructed address for your
    server datagram as long as you tell the client where you're
    listening (eg -- by writting into a file or some such).  */
  if (dgram.open (local) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "datagram open"),
                      -1);

  /* Yep.  We've seen this before too...  */
  char buf[BUFSIZ];

  /* Ok, now we're doing something different.  */
  sprintf (buf, "Hello World!");

  /* Just like sending a telegram, we have to address our datagram.
    Here, we create an address object at the desired port on the
    chosen host.  To keep us from crashing, we'll provide a default
    host name if we aren't given one.  */
  ACE_INET_Addr remote (PORT,
                        argc > 1 ? argv[1] : "localhost");

  ACE_DEBUG ((LM_DEBUG,
              "(%P|%t) Sending (%s) to the server.\n",
              buf));
  /* Now we send our buffer of stuff to the remote address.  This is
    just exactly what the server did after receiving a client message.
    Datagrams are rather orthogonal that way: they don't generally
    make much of a fuss about being either client or server.  */
  if (dgram.send (buf,
                  ACE_OS::strlen (buf) + 1,
                  remote) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "send"),
                      -1);

  /* Now we've turned around and put ourselves into "server mode" by
    invoking the recv() method.  We know our server is going to send
    us something, so we hang out here and wait for it.  Because we
    know datagrams are unreliable, there is a chance that the server
    will respond but we won't hear.  You might consider providing a
    timeout on the recv() in that case.  If recv() fails due to
    timeout it will return -1 and you can then resend your query and
    attempt the recv() again.

    Like the server application, we have to give the recv() an
    uninitialized addr object so that we can find out who is talking
    back to us.  */
  if (dgram.recv (buf,
                  sizeof (buf),
                  remote) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "recv"),
                      -1);

  /* Find out what the server had to say.  */
  ACE_DEBUG ((LM_DEBUG,
              "(%P|%t) The server said:  %s\n",
              buf));

  /* Using the "remote" object instance, find out where the server
    lives.  We could then save this address and use directed datagrams
    to chat with the server for a while.  */
  ACE_DEBUG ((LM_DEBUG,
              "(%P|%t) The server can be found at:  (%s:%d)\n",
              remote.get_host_name(),
              PORT));

  return 0;
}
Esempio n. 19
0
// receive the heartbeat information, and return back the enquened messages;
static ACE_THR_FUNC_RETURN
commu(void* arg){
  ACE_INET_Addr addr;
  ACE_SOCK_Stream stream;
  ACE_HANDLE handle =(ACE_HANDLE)(intptr_t) arg;
  stream.set_handle(handle);
  if(stream.disable(ACE_NONBLOCK) == -1){
    ACE_ERROR_RETURN((LM_ERROR,
		      "%p\n","disable"),0);
  }
  else if(stream.get_remote_addr(addr)== -1){
    ACE_ERROR_RETURN((LM_ERROR,
		      "%p\n","get_remote_addr"),0);
  }
  ACE_DEBUG((LM_INFO,
	     "(%P|%t) client %s connected from %d\n",
	     addr.get_host_name(),addr.get_port_number()));
  ACE_CString str ;
  char* ch = new char[128];  
  do{
    int bytes = stream.recv(ch, 128);
    if(bytes == -1){
      ACE_ERROR((LM_ERROR,
		 "%p\n","recv"));
      break;
    }
    if(bytes == 0){
      ACE_DEBUG((LM_INFO,
		 "(%P|%t) reached end of input, connection closed by client\n"));
      break;
    }
    for(int i = 0; i< bytes; i++){
      str += ch[i];
    }
  }while(true);
  delete[] ch;
  ACE_DEBUG((LM_INFO,
	     "received message:%s\n", str.c_str()));
  
  /*
    the layout of the heartbeat message:
    ^^hb->senderid$$
   */
  /*if got a heartbeat message*/
  int pos = str.find("^^hb->");
  if( pos >= 0 ){
    int p2 = str.find("$$");
    ACE_CString id= str.substr(6, p2 -6);
    //    delete str;
    ACE_CString sql = "select pqr, v2,txn_id from step2 where recipient='"+ id +"' and transmitted='false' ";
    ACE_DEBUG((LM_DEBUG,
	       "%s\n", sql.c_str()));
    PGconn* con;
    PGresult* res;
    con = PQconnectdb("dbname=pq");
    if(PQstatus(con)!= CONNECTION_OK){
      ACE_DEBUG((LM_INFO,
		 "Connection to database failed:%s\n",
		 PQerrorMessage(con)));
      reclaim_conn(con);
    }
    res = PQexec(con, sql.c_str());
    int n = PQntuples(res);
    if(n == 0){
      /*no pending messages at all, exit immediately*/
      ACE_DEBUG((LM_INFO,
		 "(%P|%t) no pending messages at all\n"));
      PQclear(res);
      reclaim_conn(con);
      stream.close();
      //      delete str;
      return 0;
  }
    /*there are pending messages*/
    else{
      /* the reply message is in format "^^pqr1->v1->txn_id1||pqr2->v2->txn_id2||pqr3->v3->txn_id3||$$"*/
      ACE_CString reply = ACE_CString("^^");
      for(int i=0; i < n ; i++){
	reply += PQgetvalue(res, i, 0);
	reply += "->";
	reply += PQgetvalue(res, i, 1);
	reply += "->";
	reply += PQgetvalue(res, i, 2);
	reply += "||";
      }
      reply += "$$";
      PQclear(res);
      /*    reclaim_conn(con);
       */
      stream.send(reply.c_str(), reply.length());
      ACE_DEBUG((LM_INFO,
		 "(%P|%t:%l) %s\n", reply.c_str()));
      ACE_DEBUG((LM_INFO,
		 "(%P|%t) close the writer\n"));
      stream.close();
      //      delete str;
      return 0;
    }
  }
  
  /*if got an ack message
    '^^ack->txn_id2||ack->txn_id2||$$'
   */
  int p = str.find("^^ack->");
  if( p >= 0){
    PGconn* con;
    PGresult* res;
    con = PQconnectdb("dbname=pq");
    if(PQstatus(con)!= CONNECTION_OK){
      ACE_DEBUG((LM_INFO,
		 "Connection to database failed:%s\n",
		 PQerrorMessage(con)));
      reclaim_conn(con);
    }
    do{
      p += 7;
      int p2 = str.find("||",p);
      if(p2< 0){
	ACE_DEBUG((LM_INFO,
		   "reached the end of the txn_ids\n"));
	break;
      }
      ACE_CString txn_id = str.substr(pos, p2 - p);
      ACE_CString sql = ACE_CString("update step2 set transmitted=TRUE where txn_id='");
      sql += txn_id;
      sql += "'";
      ACE_DEBUG((LM_INFO,
	       "(%P|%t:%l) sql:%s\n" , sql.c_str()));
      res = PQexec(con, sql.c_str());
      PQclear(res);
      p = p2;
    }while(true);
    reclaim_conn(con);
    stream.close();
    //    delete str;
    return 0;
  }
  return 0;
}
Esempio n. 20
0
int
ACE_MEM_Connector::connect (ACE_MEM_Stream &new_stream,
                            const ACE_INET_Addr &remote_sap,
                            ACE_Time_Value *timeout,
                            const ACE_Addr &local_sap,
                            int reuse_addr,
                            int flags,
                            int perms)
{
    ACE_TRACE ("ACE_MEM_Connector::connect");

    if (!this->address_.same_host (remote_sap))
        ACE_ERROR_RETURN ((LM_ERROR,
                           ACE_TEXT ("(%P|%t) MEM_Connector can't connect ")
                           ACE_TEXT ("to %C:%d which is not a local endpoint ")
                           ACE_TEXT ("(local address is %C:%d)\n"),
                           remote_sap.get_host_name (),
                           remote_sap.get_port_number (),
                           this->address_.get_host_name (),
                           this->address_.get_port_number ()),
                          -1);
    else
        this->address_.set_port_number (remote_sap.get_port_number ());

    ACE_SOCK_Stream temp_stream;

    if (ACE_SOCK_Connector::connect (temp_stream,
                                     this->address_.get_local_addr (),
                                     timeout, local_sap,
                                     reuse_addr, flags, perms) == -1)
        ACE_ERROR_RETURN ((LM_DEBUG,
                           ACE_TEXT ("%p\n"),
                           ACE_TEXT ("ACE_MEM_Connector::connect")),
                          -1);

    ACE_HANDLE new_handle = temp_stream.get_handle ();
    new_stream.set_handle (new_handle);
    new_stream.disable (ACE_NONBLOCK);
    // Do not close the handle.

    // now we should setup the mmap malloc.
    ACE_TCHAR buf[MAXPATHLEN];

    // @@ Need to handle timeout here.
    ACE_INT16 server_strategy = ACE_MEM_IO::Reactive;
    // Receive the signaling strategy theserver support.
    if (ACE::recv (new_handle, &server_strategy,
                   sizeof (ACE_INT16)) == -1)
        ACE_ERROR_RETURN ((LM_DEBUG,
                           ACE_TEXT ("ACE_MEM_Connector::connect error receiving strategy\n")),
                          -1);

    // If either side don't support MT, we will not use it.
#if defined (ACE_WIN32) || !defined (_ACE_USE_SV_SEM)
    if (! (this->preferred_strategy_ == ACE_MEM_IO::MT &&
            server_strategy == ACE_MEM_IO::MT))
#endif /* ACE_WIN32 || !_ACE_USE_SV_SEM */
        server_strategy = ACE_MEM_IO::Reactive;

    if (ACE::send (new_handle, &server_strategy,
                   sizeof (ACE_INT16)) == -1)
        ACE_ERROR_RETURN ((LM_DEBUG,
                           ACE_TEXT ("ACE_MEM_Connector::connect error sending strategy\n")),
                          -1);

    ACE_INT16 buf_len;
    // Byte-order is not a problem for this read.
    if (ACE::recv (new_handle, &buf_len, sizeof (buf_len)) == -1)
        ACE_ERROR_RETURN ((LM_DEBUG,
                           ACE_TEXT ("ACE_MEM_Connector::connect error receiving shm filename length\n")),
                          -1);

    if (ACE::recv (new_handle, buf, buf_len) == -1)
        ACE_ERROR_RETURN ((LM_DEBUG,
                           ACE_TEXT ("ACE_MEM_Connector::connect error receiving shm filename.\n")),
                          -1);

    if (new_stream.init (buf,
                         static_cast<ACE_MEM_IO::Signal_Strategy> (server_strategy),
                         &this->malloc_options_) == -1)
        return -1;

    return 0;
}
Esempio n. 21
0
int
ACE_TMAIN (int argc, ACE_TCHAR *argv[])
{
  u_short port = argc > 1 ? ACE_OS::atoi (argv[1]) : ACE_DEFAULT_SERVER_PORT;
  ACE_Time_Value timeout (argc > 2 ? ACE_OS::atoi (argv[2]) : ACE_DEFAULT_TIMEOUT);

  // Create a server address.
  ACE_INET_Addr addr (port);

  // Create a server, reuse the addr.
  ACE_TLI_Acceptor peer_acceptor;

  // Not sure why but reuse_addr set to true/1 causes problems for
  // FORE/XTI/ATM - this is now handled in ACE_TLI_Acceptor::open()
  if (peer_acceptor.open (addr, 1) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "open"),
                      -1);

  ACE_TLI_Stream new_stream;

  ACE_DEBUG ((LM_DEBUG,
              "starting server at host %s\n",
              addr.get_host_name ()));

  // Performs the iterative server activities

  for (;;)
    {
      char buf[BUFSIZ];

      // Create a new ACE_TLI_Stream endpoint (note automatic restart
      // if errno == EINTR).
      if (peer_acceptor.accept (new_stream,
				&addr,
                                &timeout) == -1)
	{
	  ACE_ERROR ((LM_ERROR,
                      "%p\n",
                      "accept"));
	  continue;
	}

      ACE_DEBUG ((LM_DEBUG,
                  "client %s connected\n",
                  addr.get_host_name ()));

      // Read data from client (terminate on error).

      for (int r_bytes;
	   (r_bytes = new_stream.recv (buf, sizeof buf)) > 0; )
        if (ACE_OS::write (ACE_STDOUT,
                           buf,
                           r_bytes) != r_bytes)
	  ACE_ERROR ((LM_ERROR,
                      "%p\n",
                      "ACE::send_n"));

	// Close new endpoint (listening endpoint stays open).
      if (new_stream.close () == -1)
	ACE_ERROR ((LM_ERROR,
                    "%p\n",
                    "close"));

    }
  /* NOTREACHED */
  return 0;
}
Esempio n. 22
0
int run_main (int, ACE_TCHAR *[])
{
  ACE_START_TEST (ACE_TEXT ("INET_Addr_Test"));

  int status = 0;     // Innocent until proven guilty

  // Try to set up known IP and port.
  u_short port (80);
  ACE_UINT32 const ia_any = INADDR_ANY;
  ACE_INET_Addr local_addr(port, ia_any);
  status |= check_type_consistency (local_addr);
  if (local_addr.get_port_number () != 80)
    {
      ACE_ERROR ((LM_ERROR,
                  ACE_TEXT ("Got port %d, expecting 80\n"),
                  (int)(local_addr.get_port_number ())));
      status = 1;
    }
  if (local_addr.get_ip_address () != ia_any)
    {
      ACE_ERROR ((LM_ERROR, ACE_TEXT ("Mismatch on local IP addr\n")));
      status = 1;
    }

  // Assignment constructor
  ACE_INET_Addr local_addr2 (local_addr);
  status |= check_type_consistency (local_addr2);
  if (local_addr2.get_port_number () != 80)
    {
      ACE_ERROR ((LM_ERROR,
                  ACE_TEXT ("Copy got port %d, expecting 80\n"),
                  (int)(local_addr2.get_port_number ())));
      status = 1;
    }
  if (local_addr2.get_ip_address () != ia_any)
    {
      ACE_ERROR ((LM_ERROR, ACE_TEXT ("Mismatch on copy local IP addr\n")));
      status = 1;
    }
  if (local_addr != local_addr2)
    {
      ACE_ERROR ((LM_ERROR, ACE_TEXT ("Copy local addr mismatch\n")));
      status = 1;
    }

  // Try to parse out a simple address:port string. Intentionally reuse
  // the ACE_INET_Addr to ensure resetting an address works.
  const char *addr_ports[] =
    {
      "127.0.0.1:80", "www.dre.vanderbilt.edu:80", 0
    };
  ACE_INET_Addr addr_port;
  for (int i = 0; addr_ports[i] != 0; ++i)
    {
      if (addr_port.set (addr_ports[i]) == 0)
        {
          status |= check_type_consistency (addr_port);
          if (addr_port.get_port_number () != 80)
            {
              ACE_ERROR ((LM_ERROR,
                          ACE_TEXT ("Got port %d from %s\n"),
                          (int)(addr_port.get_port_number ()),
                          addr_ports[i]));
              status = 1;
            }
          ACE_INET_Addr check (addr_ports[i]);
          if (addr_port != check)
            {
              ACE_ERROR ((LM_ERROR, ACE_TEXT ("Reset on iter %d failed\n"), i));
              status = 1;
            }
        }
      else
        {
          // Sometimes this fails because the run-time host lacks the capability to
          // resolve a name. But it shouldn't fail on the first one, 127.0.0.1.
          if (i == 0)
            {
              ACE_ERROR ((LM_ERROR,
                          ACE_TEXT ("%C: %p\n"),
                          addr_ports[i],
                          ACE_TEXT ("lookup")));
              status = 1;
            }
          else
            {
              ACE_ERROR ((LM_WARNING,
                          ACE_TEXT ("%C: %p\n"),
                          addr_ports[i],
                          ACE_TEXT ("lookup")));
            }
        }
    }

  const char *ipv4_addresses[] =
    {
      "127.0.0.1", "138.38.180.251", "64.219.54.121", "192.0.0.1", "10.0.0.1", 0
    };

  ACE_INET_Addr addr;
  status |= check_type_consistency (addr);
  char hostaddr[1024];

  for (int i=0; ipv4_addresses[i] != 0; i++)
    {
      struct in_addr addrv4;
      ACE_OS::memset ((void *) &addrv4, 0, sizeof addrv4);
      ACE_UINT32 addr32;

      ACE_OS::inet_pton (AF_INET, ipv4_addresses[i], &addrv4);

      ACE_OS::memcpy (&addr32, &addrv4, sizeof (addr32));

      status |= !(addr.set (80, ipv4_addresses[i]) == 0);
      status |= check_type_consistency (addr);

      /*
      ** Now check to make sure get_ip_address matches and get_host_addr
      ** matches.
      */
      if (addr.get_ip_address () != ACE_HTONL (addr32))
        {
          ACE_ERROR ((LM_ERROR,
                      ACE_TEXT ("Error: %C failed get_ip_address() check\n")
                      ACE_TEXT ("0x%x != 0x%x\n"),
                      ipv4_addresses[i],
                      addr.get_ip_address (),
                      ACE_HTONL (addr32)));
          status = 1;
        }

      if (addr.get_host_addr () != 0 &&
          ACE_OS::strcmp (addr.get_host_addr(), ipv4_addresses[i]) != 0)
        {
          ACE_ERROR ((LM_ERROR,
                      ACE_TEXT ("%C failed get_host_addr() check\n")
                      ACE_TEXT ("%C != %C\n"),
                      ipv4_addresses[i],
                      addr.get_host_addr (),
                      ipv4_addresses[i]));
          status = 1;
        }

      // Now we check the operation of get_host_addr(char*,int)
      const char* haddr = addr.get_host_addr (&hostaddr[0], sizeof(hostaddr));
      if (haddr != 0 &&
          ACE_OS::strcmp (&hostaddr[0], haddr) != 0)
        {
          ACE_ERROR ((LM_ERROR,
                      ACE_TEXT ("%C failed get_host_addr(char* buf,int) check\n")
                      ACE_TEXT ("buf ['%C'] != return value ['%C']\n"),
                      ipv4_addresses[i],
                      &hostaddr[0],
                      haddr));
          status = 1;
        }
      if (ACE_OS::strcmp (&hostaddr[0], ipv4_addresses[i]) != 0)
        {
          ACE_ERROR ((LM_ERROR,
                      ACE_TEXT ("%C failed get_host_addr(char*,int) check\n")
                      ACE_TEXT ("buf ['%C'] != expected value ['%C']\n"),
                      ipv4_addresses[i],
                      &hostaddr[0],
                      ipv4_addresses[i]));
          status = 1;
        }

      // Clear out the address by setting it to 1 and check
      addr.set (0, ACE_UINT32 (1), 1);
      status |= check_type_consistency (addr);
      if (addr.get_ip_address () != 1)
        {
          ACE_ERROR ((LM_ERROR, ACE_TEXT ("Failed to set address to 1\n")));
          status = 1;
        }

      // Now set the address using a 32 bit number and check that we get
      // the right string out of get_host_addr().
      addr.set (80, addr32, 0); // addr32 is already in network byte order
      status |= check_type_consistency(addr);

      if (addr.get_host_addr () != 0 &&
          ACE_OS::strcmp (addr.get_host_addr (), ipv4_addresses[i]) != 0)
        {
          ACE_ERROR ((LM_ERROR,
                      ACE_TEXT ("%C failed second get_host_addr() check\n")
                      ACE_TEXT ("return value ['%C'] != expected value ['%C']\n"),
                      ipv4_addresses[i],
                      addr.get_host_addr (),
                      ipv4_addresses[i]));
          status = 1;
        }

      // Test for ACE_INET_Addr::set_addr().
      struct sockaddr_in sa4;
      sa4.sin_family = AF_INET;
      sa4.sin_addr = addrv4;
      sa4.sin_port = ACE_HTONS (8080);

      addr.set (0, ACE_UINT32 (1), 1);
      addr.set_addr (&sa4, sizeof(sa4));
      status |= check_type_consistency (addr);

      if (addr.get_port_number () != 8080)
        {
          ACE_ERROR ((LM_ERROR,
                      ACE_TEXT ("ACE_INET_Addr::set_addr() ")
                      ACE_TEXT ("failed to update port number.\n")));
          status = 1;
        }

      if (addr.get_ip_address () != ACE_HTONL (addr32))
        {
          ACE_ERROR ((LM_ERROR,
                      ACE_TEXT ("ACE_INET_Addr::set_addr() ")
                      ACE_TEXT ("failed to update address.\n")));
          status = 1;
        }

    }

#if defined (ACE_HAS_IPV6)
  if (ACE::ipv6_enabled ())
    {
      const char *ipv6_addresses[] = {
        "1080::8:800:200c:417a", // unicast address
        "ff01::101",             // multicast address
        "::1",                   // loopback address
        "::",                    // unspecified addresses
        0
      };

      for (int i=0; ipv6_addresses[i] != 0; i++)
        {
          ACE_INET_Addr addr (80, ipv6_addresses[i]);
          status |= check_type_consistency (addr);

          if (0 != ACE_OS::strcmp (addr.get_host_addr (), ipv6_addresses[i]))
            {
              ACE_ERROR ((LM_ERROR,
                          ACE_TEXT ("IPv6 get_host_addr failed: %C != %C\n"),
                          addr.get_host_addr (),
                          ipv6_addresses[i]));
              status = 1;
            }
        }

      const char *ipv6_names[] = {
        "naboo.dre.vanderbilt.edu",
        "v6.ipv6-test.com",
        0
      };
      for (int i=0; ipv6_names[i] != 0; i++)
        {
          ACE_INET_Addr addr (80, ipv6_names[i]);
          status |= check_type_consistency (addr);

          if (0 != ACE_OS::strcmp (addr.get_host_name (), ipv6_names[i]))
            {
              // Alias? Check lookup on the reverse.
              ACE_INET_Addr alias_check;
              if (alias_check.set (80, addr.get_host_name ()) == 0)
                {
                  if (addr != alias_check)
                    ACE_ERROR ((LM_WARNING,
                                ACE_TEXT ("IPv6 name mismatch: %s (%s) != %s\n"),
                                addr.get_host_name (),
                                addr.get_host_addr (),
                                ipv6_names[i]));
                }
              else
                {
                  ACE_ERROR ((LM_WARNING,
                              ACE_TEXT ("IPv6 reverse lookup mismatch: %s (%s) != %s\n"),
                              addr.get_host_name (),
                              addr.get_host_addr (),
                              ipv6_names[i]));
                }
            }
        }
    }
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("IPv6 tests done\n")));
#else
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("ACE_HAS_IPV6 not set; no IPv6 tests run\n")));
#endif

  struct Address loopback_addresses[] =
    { {"127.0.0.1", true}, {"127.1.2.3", true}
      , {"127.0.0.0", true}, {"127.255.255.255", true}
      , {"126.255.255.255", false}, {"128.0.0.0", false}, {0, true}
    };

  for (int i=0; loopback_addresses[i].name != 0; i++)
    {
      struct in_addr addrv4;
      ACE_UINT32 addr32 = 0;

      ACE_OS::inet_pton (AF_INET, loopback_addresses[i].name, &addrv4);

      ACE_OS::memcpy (&addr32, &addrv4, sizeof (addr32));

      addr.set (80, loopback_addresses[i].name);

      if (addr.is_loopback() != loopback_addresses[i].loopback)
        {
          ACE_ERROR ((LM_ERROR,
                      ACE_TEXT ("ACE_INET_Addr::is_loopback() ")
                      ACE_TEXT ("failed to distinguish loopback address. %C\n")
                      , loopback_addresses[i].name));
          status = 1;
        }
    }

  if (addr.string_to_addr ("127.0.0.1:72000", AF_INET) != -1)
    {
      ACE_ERROR ((LM_ERROR,
                  ACE_TEXT ("ACE_INET_Addr::string_to_addr() ")
                  ACE_TEXT ("failed to detect port number overflow\n")));
      status = 1;
    }

  if (!test_tao_use ())
    status = 1;

  if (!test_multiple ())
    status = 1;

  if (!test_port_assignment ())
    status = 1;

  ACE_INET_Addr a1 (80, "127.0.0.1");
  ACE_INET_Addr a2 = a1;
  if (a1 != a2)
    {
      ACE_ERROR ((LM_ERROR,
                  ACE_TEXT ("Address equality check failed after assignment\n")));
      status = 1;
    }

  ACE_END_TEST;

  return status;
}