Example #1
0
/* establish all to all connections. */
void
do_dance(const int port)
{
    int i, cc;
    int fd;

    if (comm_rank != comm_size - 1) {
	fd = passive_open(port);
	if (fd < 0) {
	    fprintf(stderr, "[%d] Failed passive open.\n", comm_rank);
	    exit(1);
	}
    }
    MPI_Barrier(MPI_COMM_WORLD);

    for (i = 0; i < comm_rank; i++) {
	if (active_open(i, port) < 0) {
	    fprintf(stderr, "[%d] Failed active open.\n", comm_rank);
	    exit(1);
	}
    }

    for (i = 0; i < comm_size - comm_rank - 1;) {
	fd_set afds;

	FD_ZERO(&afds);
	FD_SET(fd, &afds);
	cc = select(fd + 1, &afds, NULL, NULL, NULL);
	if (cc == -1 && (errno == EAGAIN || errno == EWOULDBLOCK))
	    continue;

	if (FD_ISSET(fd, &afds)) {
	    struct sockaddr_in sa;
	    socklen_t salen = sizeof(sa);
	    int sock;
	    int peer;

	    sock = accept(fd, (struct sockaddr *)&sa, &salen);
	    if (sock < 0)
		perror_exit("accept", 1);

	    peer = get_peer_rank_by_sock(sock);
	    if (peer < 0) {
		fprintf(stderr, "Failed get peer rank.\n");
		exit(1);
	    }
	    hosts[peer].sock = sock;
	}
	i++;
    }
}
int command_interpreter(FTPinfo * FTP_information)
{
    char        pure_command[MAXLINE]; 
    char        arg_opt[MAXLINE];
    int         fail;         // this variable make program code readable.
    char        buf[MAXLINE]; // This will be used for multiple purpose below.
    char*       userpath;     // user relative path.

    
    strcpy(pure_command, FTP_information->RAW_command); //unsafe.
    fail = command_parser(FTP_information->RAW_command, pure_command, arg_opt);
    /*  
        for example,
        command_parser("USER localhost -arg -opt", buffer-pointer, buffer-pointer2);
        buffer-pointer  -> USER
        buffer-pointer2 -> localhost -arg -opt
    */
    
    /*
    if(fail) // fail to parse command due to invalid command.
    {
        perror("parse error");
        return -1;
    }
    */
    
    /*        interpret command.        */
    if( strcmp(pure_command, "USER") == 0)
    {
        //strcpy(FTP_information->response_code, "331");
        strcpy(FTP_information->response_code, "331 Please specify the password.\r\n");
        strcpy(FTP_information->loginID, arg_opt);
        write(FTP_information->controlsockfd, FTP_information->response_code,
              strlen(FTP_information->response_code)); // send response.
        return 331;
    }
    else if( strcmp(pure_command, "PASS") == 0)
    {
        chdir("/Users/tracking/Desktop/old/REAL_FTP/REAL_FTP"); // for xcode debug. you should delete
        
        
        fail = checkID(FTP_information->loginID, arg_opt);
        if(fail)
        {
            strcpy(FTP_information->response_code, "530 Login incorrect.\r\n");
            return 530;
        }
        
        
        
        // if there not exist user directory, then the program need to make it.
        if( access(FTP_information->loginID,F_OK) == -1)
            mkdir(FTP_information->loginID, 0777); // you'd better correct permission.

                // configure user directory.
        chdir(FTP_information->loginID);
        getcwd(FTP_information->root_directory, BUFSIZE);
        getcwd(FTP_information->working_directory, BUFSIZE);
        
        strcpy(FTP_information->response_code, "230- Welcome to my FTP world.\r\n");
        strcat(FTP_information->response_code, "230-\r\n");
        strcat(FTP_information->response_code, "230- This system is for authorized users only. All access is logged.\r\n");
        strcat(FTP_information->response_code, "230 Login successful.\r\n");
        write(FTP_information->controlsockfd, FTP_information->response_code,
              strlen(FTP_information->response_code)); // send response.
        return 230;
    }
    else if( strcmp(pure_command, "SYST") == 0)
    {
        strcpy(FTP_information->response_code, "215 MACOS Type: L8\r\n");
        write(FTP_information->controlsockfd, FTP_information->response_code,
              strlen(FTP_information->response_code)); // send response.
        return 215;
    }
    else if( strcmp(pure_command, "FEAT") == 0)
    {
        strcpy(FTP_information->response_code, "211 \r\n");
        write(FTP_information->controlsockfd, FTP_information->response_code,
              strlen(FTP_information->response_code)); // send response.
        return 211;
    }
    else if( strcmp(pure_command, "PWD") == 0)
    {

//        strcpy(buf, FTP_information->working_directory);
        userpath    =   Generate_userpath(FTP_information->working_directory, FTP_information->root_directory);
        if( *userpath == 0) // NULL is user-root directory
        {
            buf[0] = '/';
            buf[1] = 0;
            userpath = buf;
        }
        sprintf(FTP_information->response_code, "257 \"%s\"\r\n", userpath);
        write(FTP_information->controlsockfd, FTP_information->response_code,
              strlen(FTP_information->response_code)); // send response.
        return 257;
    }
    else if( strcmp(pure_command, "CWD") == 0)
    {
        
        if(access(arg_opt, F_OK	) == -1)
        {
            sprintf(FTP_information->response_code, "550 %s: No such file or directory\r\n", arg_opt);
            return 550;
        }
        chdir(arg_opt);
        getcwd(buf, MAXLINE);
        // since user can't move upper directory than user direcory!
        if( strlen(buf) < strlen(FTP_information->root_directory))
            chdir(FTP_information->root_directory);
        getcwd(FTP_information->working_directory, BUFSIZE);
        strcpy(FTP_information->response_code, "250 CWD command successful.\r\n");
        write(FTP_information->controlsockfd, FTP_information->response_code,
              strlen(FTP_information->response_code)); // send response.
        return 250;
        
    }
    else if( strcmp(pure_command, "QUIT") == 0)
    {
        sprintf(FTP_information->response_code, "221 Goodbye \r\n");
        close(FTP_information->controlsockfd);
        write(FTP_information->controlsockfd, FTP_information->response_code,
              strlen(FTP_information->response_code)); // send response.
        return 221;
    }
    else if( strcmp(pure_command, "PORT") == 0)
    {
        /* previous code.
        Get_dataportandIP(FTP_information,arg_opt);
        strcpy(FTP_information->response_code, "200 PORT command successful.\r\n");
        strcpy(FTP_information->RAW_command, pure_command);
        */
        
        Get_dataportandIP(FTP_information,arg_opt);
        strcpy(FTP_information->response_code, "200 PORT command successful.\r\n");
        write(FTP_information->controlsockfd, FTP_information->response_code,
              strlen(FTP_information->response_code));
        
        active_open(FTP_information);
        strcpy(FTP_information->response_code, "150 Data Connection OK\r\n");
        write(FTP_information->controlsockfd, FTP_information->response_code,
              strlen(FTP_information->response_code));
        return 200;
    }
    else if( strcmp(pure_command, "NOOP") == 0)
    {
        strcpy(FTP_information->response_code, "200 NOOP command successful.\r\n");
        write(FTP_information->controlsockfd, FTP_information->response_code,
              strlen(FTP_information->response_code)); // send response.
        return 200;
    }
    else if( strcmp(pure_command, "PASV") == 0)
    {
        passive_open(FTP_information);
        
        // on failure, I would send the message "cannot open connection.".
        strcpy(FTP_information->response_code, "150 Data Connection OK\r\n"); // not need.
        write(FTP_information->controlsockfd, FTP_information->response_code,
              strlen(FTP_information->response_code)); // send response.
        
        return 227;
    }
    /*
    else if( strcmp(pure_command, "LIST") == 0)
    {
        active_open(FTP_information); // needs to return -1 or 0. for exception proces
        strcpy(FTP_information->response_code, "125 Data Connection OK\r\n");
        write(FTP_information->controlsockfd, FTP_information->response_code,
              strlen(FTP_information->response_code)); // send response.
        return 125;
    }
    */
    else if( strcmp(pure_command, "TYPE") == 0)
    {
        strcpy(FTP_information->response_code,"200 TYPE Command OK\r\n");
        write(FTP_information->controlsockfd, FTP_information->response_code,
              strlen(FTP_information->response_code)); // send response.
        return 200;
    }
    else if( strcmp(pure_command, "MKD") == 0)
    {
//        strcpy(FTP_information->response_code, ")
    
        fail = mkdir(arg_opt, 0644);
        
        if(fail)
        {
            perror("directory error : ");
            strcpy(FTP_information->response_code, "503 Bad sequence of commands.\r\n");
            return 503;
        }
        else
        {
            
            strcpy(FTP_information->response_code, "257 Directory Command succesful\r\n");
            return 257;
        }
        write(FTP_information->controlsockfd, FTP_information->response_code,
              strlen(FTP_information->response_code)); // send response.
    }
    else
    {
        fail = Data_gateway(FTP_information); // maybe data transfer command.
        if(fail)
        {
            sprintf(FTP_information->response_code, "502 Command not implemented.\r\n");
            write(FTP_information->controlsockfd, FTP_information->response_code,
                  strlen(FTP_information->response_code));
            return 502;
        }
        else
        {
            close(FTP_information->datasockfd); // transfer complete.
            write(FTP_information->controlsockfd, FTP_information->response_code,
                  strlen(FTP_information->response_code));
            return 226;
        }
    }
    return 0;
}
Example #3
0
int
OpenDDS::DCPS::TcpConnection::open(void* arg)
{
  DBG_ENTRY_LVL("TcpConnection","open",6);

  if (is_connector_) {

    VDBG_LVL((LM_DEBUG, "(%P|%t) DBG:   TcpConnection::open active.\n"), 2);
    // Take over the refcount from TcpTransport::connect_datalink().
    const TcpConnection_rch self(this);
    const TcpTransport_rch transport = link_->get_transport_impl();

    const bool is_loop(local_address_ == remote_address_);
    const PriorityKey key(transport_priority_, remote_address_,
                          is_loop, false /* !active */);

    int active_open_ = active_open();

    int connect_tcp_datalink_ = transport->connect_tcp_datalink(link_, self);

    if (active_open_ == -1 || connect_tcp_datalink_ == -1) {
      // if (active_open() == -1 ||
      //       transport->connect_tcp_datalink(link_, self) == -1) {

      transport->async_connect_failed(key);

      return -1;
    }

    return 0;
  }

  // The passed-in arg is really the acceptor object that created this
  // TcpConnection object, and is also the caller of this open()
  // method.  We need to cast the arg to the TcpAcceptor* type.
  TcpAcceptor* acceptor = static_cast<TcpAcceptor*>(arg);

  if (acceptor == 0) {
    // The cast failed.
    ACE_ERROR_RETURN((LM_ERROR,
                      ACE_TEXT("(%P|%t) ERROR: TcpConnection::open() - ")
                      ACE_TEXT("failed to cast void* arg to ")
                      ACE_TEXT("TcpAcceptor* type.\n")),
                     -1);
  }

  // Now we need to ask the TcpAcceptor object to provide us with
  // a pointer to the TcpTransport object that "owns" the acceptor.
  TcpTransport_rch transport = acceptor->transport();

  if (transport.is_nil()) {
    // The acceptor gave us a nil transport (smart) pointer.
    ACE_ERROR_RETURN((LM_ERROR,
                      ACE_TEXT("(%P|%t) ERROR: TcpConnection::open() - ")
                      ACE_TEXT("acceptor's transport is nil.\n")),
                     -1);
  }

  TcpInst* tcp_config = acceptor->get_configuration();

  // Keep a "copy" of the reference to TcpInst object
  // for ourselves.
  tcp_config->_add_ref();
  tcp_config_ = tcp_config;
  local_address_ = tcp_config_->local_address_;

  set_sock_options(tcp_config_.in());

  // We expect that the active side of the connection (the remote side
  // in this case) will supply its listening ACE_INET_Addr as the first
  // message it sends to the socket.  This is a one-way connection
  // establishment protocol message.
  passive_setup_ = true;
  transport_during_setup_ = transport;
  passive_setup_buffer_.size(sizeof(ACE_UINT32));

  if (reactor()->register_handler(this, READ_MASK) == -1) {
    ACE_ERROR_RETURN((LM_ERROR,
                      ACE_TEXT("(%P|%t) ERROR: TcpConnection::open() - ")
                      ACE_TEXT("unable to register with the reactor.%p\n"),
                      ACE_TEXT("register_handler")),
                     -1);
  }

  VDBG_LVL((LM_DEBUG, "(%P|%t) DBG:   TcpConnection::open passive handle=%d.\n",
            int(get_handle())), 2);

  return 0;
}