Exemplo n.º 1
0
void
wtp_open (
    const char *version,                /*  WTP version                      */
    const char *protocol,               /*  Protocol from command-line       */
    const char *port)                   /*  Port from command-line           */
{
    ASSERT (streq (version, "WTP/1.0"));
    ASSERT (streq (protocol, "tcp"));

    sock_init ();
    ip_nonblock = FALSE;                /*  Use blocking socket i/o          */

    wtp_socket  = connect_TCP (NULL, port);
    if (wtp_socket == INVALID_SOCKET)
      {
        coprintf ("Cannot connect to port %s: %s (%s)\n",
                   port, connect_errlist [connect_error ()], sockmsg ());
        exit (EXIT_FAILURE);
      }
    socket_nodelay (wtp_socket);        /*  Disable Nagle's Algorithm        */
}
Exemplo n.º 2
0
void ClientConnection::WaitForRequests() {
    if (!ok) {
	 return;
    }

    fprintf(fd, "220 Service ready\n");

    while(!parar) {



      fscanf(fd, "%s", command);
      if (COMMAND("USER")) {
	      fscanf(fd, "%s", arg);
        if (strcmp(arg,"alu") == 0)
          fprintf(fd, "331 User name ok, need password.\n");
        else
          fprintf(fd, "332 Need account for login.\n");
      }
      else if (COMMAND("PWD")) {
        char aux[200];
        getcwd(aux,sizeof(aux)); //La funcion getcwd esta en la librería unistd.h
        fprintf(fd, "257 'PATHNAME' created  %s\n",aux);
      }
      else if (COMMAND("PASS")) {
        fscanf(fd, "%s", arg);
        printf("(PASS):%s\n", arg);
        if (!strcmp(arg,"1234"))
          fprintf(fd, "230 User logged in, proceed.\n");
        else
          fprintf(fd, "530 Not logged in.\n");
      }
      else if (COMMAND("PORT")) {
        int ip[4];
        int puertos[2];
        fscanf(fd, "%d,%d,%d,%d,%d,%d", &ip[0], &ip[1], &ip[2], &ip[3], &puertos[0], &puertos[1]);

        uint32_t ip_addr = ip[3]<<24 | ip[2]<<16 | ip[1]<<8 | ip[0];
        uint16_t port_v = puertos[0] << 8 | puertos[1];

        data_socket = connect_TCP(ip_addr,port_v);

        fprintf(fd, "200 Okey\n");
      }
      else if (COMMAND("PASV")) {
        struct sockaddr_in sin;
        socklen_t len = sizeof(sin);
        int s = define_socket_TCP2(0);

        getsockname(s, (struct sockaddr *) &sin, &len);

        uint16_t puerto = sin.sin_port;
        int p1 = puerto & 0xFF;
        int p2 = puerto >> 8;

        fprintf(fd, "227 entering passive mode (127,0,0,1,%d,%d)\n", p1, p2);
        fflush(fd);

        len = sizeof(sin);
        data_socket = accept(s, (struct sockaddr*) &sin, &len);
      }
      else if (COMMAND("CWD")) {
        char aux[200];
        getcwd(aux,sizeof(aux));
        if(chdir(aux) == 0)
			     fprintf(fd, "200 Working directory changed\n");
		    else
			     fprintf(fd, "431 No such directory\n");
      }
      else if (COMMAND("STOR") ) {
        fscanf(fd, "%s", arg);
        char buffer[MAX_BUFF];
        int file;
        int aux;

        file=open(arg, O_RDWR|O_CREAT,S_IRWXU);
        fprintf(fd, "150 File ok, creating connection\n");
        fflush(fd);
        if(file < 0)
        {
          fprintf(fd, "450 Requested file action not taken. File unavaible.\n");
        }
        else
        {
          do{
            aux = read(data_socket,buffer,sizeof(buffer));
            write(file,buffer,aux);
          }while(aux > 0);

          fprintf(fd,"250 Requested file action okay, completed.\n");
          close(file);
          close(data_socket);
        }
      }
      else if (COMMAND("SYST")) {
        fprintf(fd, "215 UNIX Type: L8.\n");
      }
      else if (COMMAND("TYPE")) {
        fprintf(fd, "200 TYPE OK\n");
      }
      else if (COMMAND("RETR")) {
        fscanf(fd, "%s", arg);
        FILE* file = fopen(arg,"rb");
        if (!file){
          fprintf(fd, "450 Requested file action not taken. File unavaible.\n");
          close(data_socket);
        }
        else{
          fprintf(fd, "150 File status okay; about to open data connection.\n");
          struct sockaddr_in sa;
          socklen_t sa_len = sizeof(sa);
          char buffer[MAX_BUFF];
          int aux;
          do{
            aux = fread(buffer, sizeof(char), MAX_BUFF, file);
            send(data_socket, buffer, aux, 0);
          }while(aux == MAX_BUFF);

          fprintf(fd,"226 Closing data connection. Requested file action successful.\n");
          fclose(file);
          close(data_socket);
        }
      }
      else if (COMMAND("QUIT")) {
        parar = true;
	      fprintf(fd, "221 Service closing control connection\n");
      }
      else if (COMMAND("LIST")) {
        std::cout << "hago el ls" << std::endl;
        fprintf(fd, "125 Data Transfer starting\n");
        struct sockaddr_in sa;
        socklen_t sa_len = sizeof(sa);
        char buffer[MAX_BUFF];
        std::string listado;
        std::string ls = "ls";
        FILE* file = popen(ls.c_str(), "r");
        if (!file){
          fprintf(fd, "450 File unavaible.\n");
          close(data_socket);
        }else{
          while (!feof(file))
            if (fgets(buffer, MAX_BUFF, file) != NULL)
              listado.append(buffer);

              send(data_socket, listado.c_str(), listado.size(), 0);
              fprintf(fd, "250 Requested file action successful.\n");
              pclose(file);
              close(data_socket);
        }
      }
      else  {
	      fprintf(fd, "502 Command not implemented.\n"); fflush(fd);
	      printf("Comando : %s %s\n", command, arg);
	      printf("Error interno del servidor\n");

      }

    }