Exemplo n.º 1
0
/* LOGOUT command */
int ImapSession::processLogout(ImapCommand *command) {
    size_t crlfPos = incomingData_.find(CRLF);
    string line = incomingData_.substr(0, crlfPos);

    int ret = crlfPos + 1; /* last position in command */

    /* Check command syntax */
    if (line.length() != command->tag.length() + 1 /* whitespace */ +
            command->name.length()) {
        rejectUnknownCommand(command);
        return ret;
    }

    service_->onLogout();

    ostringstream oss;
    oss << "* BYE IMAP4rev1 Server logging out" << CRLF << command->tag
            << " OK " << command->name << " completed" << CRLF;
    answersData_.append(oss.str());
    writeAnswers();
    switchState(ImapSessionState::EXIT);
    return ret;
}
Exemplo n.º 2
0
int main (int argc,char* argv[])
{
    TPDU parameters;
    int stcp,sudp;
    char data[BUFLEN];
    struct hostent *hp;
    int fd;
    int protocol;
    sigset_t mask;
    int ret;
    int efiport;
    int addrlen;
    struct sockaddr_in myaddr_in;
    int fp;
    char titulo[50]="";

    //Comprobracion de parametros de entrada
    if(argc != 4)
    {
        fprintf(stderr,"\nError! How to use it: %s [hostname] [protocol TCP|UDP] [ordenes.txt|ordenes1.txt|ordenes2.txt]\n",argv[0]);
    }
    else
    {
        if(strcmp(argv[2],"UDP") != 0 && strcmp(argv[2],"TCP") != 0)
        {
            fprintf(stderr,"\nError! The protocol has to be TCP or UDP\n");
        }

        if(strcmp(argv[3],"ordenes.txt") != 0 && strcmp(argv[3],"ordenes1.txt") != 0 && strcmp(argv[3],"ordenes2.txt") != 0 )
        {
            fprintf(stderr,"\nError! The archive has to be ordenes.txt or ordenes1.txt or ordenes2.txt \n");
        }
    }

    //Comprobamos que protocolo es
	if(strcmp(argv[2],"UDP") == 0)
	{
        protocol = IPPROTO_UDP;
	}
	else
	{
	    protocol = IPPROTO_TCP;
	}

	memset ((char *)&myaddr_in, 0, sizeof(struct sockaddr_in));

    //Obtenemos los datos del host
    hp = gethostbyname (argv[1]);
    if (hp == NULL)
    {
        fprintf(stderr, "%s not found in /etc/hosts\n",argv[1]);
        return 1;
    }

    //Rellenamos los parametros de configuracion
    if(protocol == IPPROTO_UDP)
    {
        parameters.protocol = IPPROTO_UDP;
    }
    else
    {
        parameters.protocol = IPPROTO_TCP;
    }

    parameters.port = htons(PORT);
    parameters.server = *((struct in_addr *)(hp->h_addr));

    //creamos el socket dependiendo del protocolo
    if(protocol == IPPROTO_UDP)
    {
        //creamos el socket UDP
        sudp = crearSocket(parameters,CLIENT);
		if(sudp == -1)
		{
		    return 1;
		}

        //aqui obtenemos la informacion del socket, buscamos el puerto efimero
		addrlen = sizeof(struct sockaddr_in);
        if(getsockname(sudp, (struct sockaddr *)&myaddr_in, &addrlen) == -1)
        {
            fprintf(stderr, "%s: unable to read socket address\n", argv[0]);
            exit(1);
        }
        efiport = myaddr_in.sin_port;
    }
    else
    {
        //creamos el socket TCP
		stcp = crearSocket(parameters,CLIENT);
		if(stcp == -1)
		{
		    return 1;
		}

		//aqui obtenemos la informacion del socket, buscamos el puerto efimero
		addrlen = sizeof(struct sockaddr_in);
        if(getsockname(stcp, (struct sockaddr *)&myaddr_in, &addrlen) == -1)
        {
            fprintf(stderr, "%s: unable to read socket address\n", argv[0]);
            exit(1);
        }
        efiport = myaddr_in.sin_port;
    }

    //abrimos el fichero que hemos pasado por parametros para lectura
    fd = fopen(argv[3],"r");
    if(fd==NULL)
    {
        fprintf(stderr,"\nCan't open text file\n");
        return 1;
    }

    //nombramos al fichero con el nombre del puerto efimero
    sprintf(titulo,"%d.txt",efiport);

    //abrimos el fichero para el log
    fp = fopen(titulo, "a");
    if(fp==NULL)
    {
        fprintf(stderr,"\nCan't open log file\n");
        return 1;
    }

    //recorrer el fichero e ir enviado y recibiendo los datos contenidos en el
    while(1)
    {
        //obtenemos una frase del fichero
        fgets(data,BUFLEN,fd);

        //protocolo UDP
        if(protocol == IPPROTO_UDP)
        {
            //enviamos la frase al servidor
            ret =Enviar(sudp,data,sizeof(data),parameters);

            //comprobamos si hay problemas en el envio
            if(ret < 0)
            {
                writeAnswers(fp,"UDP",hp->h_name,ntohs(parameters.port),ntohs(efiport),inet_ntoa(parameters.server),1,0,data);
            }
            else
            {
                //comprobamos si enviamos la palabra FIN para finalizar el cliente
                if(strcmp(data,"FIN\n") == 0 || strcmp(data,"FIN") == 0)
                {
                    close(sudp);
                    close(fd);
                    close(fp);
                    return 0;
                }

                writeAnswers(fp,"UDP",hp->h_name,ntohs(parameters.port),ntohs(efiport),inet_ntoa(parameters.server),0,0,data);

                //recibimos los datos
                ret =Recibir(sudp,&data,sizeof(data),&parameters);

                //comprobamos si se ha producido algun error en el envio
                if(ret < 0)
                {
                    writeAnswers(fp,"UDP",hp->h_name,ntohs(parameters.port),ntohs(efiport),inet_ntoa(parameters.server),1,1,data);

                }
                else
                {
                    writeAnswers(fp,"UDP",hp->h_name,ntohs(parameters.port),ntohs(efiport),inet_ntoa(parameters.server),0,1,data);
                }
            }
        }

        //protocolo TCP
        if(protocol == IPPROTO_TCP)
        {
            //enviamos los datos
            ret =Enviar(stcp,data,sizeof(data),parameters);

            //comprobamos si se ha producido algun error en el envio
            if(ret < 0)
            {
                writeAnswers(fp,"TCP",hp->h_name,ntohs(parameters.port),ntohs(efiport),inet_ntoa(parameters.server),1,0,data);
            }
            else
            {
                //comprobamos si enviamos FIN para finalizar el cliente
                if(strcmp(data,"FIN\n") == 0 || strcmp(data,"FIN") == 0)
                {
                    close(sudp);
                    close(fd);//fichero del cual obtenemos los datos
                    close(fp);//fichero del log
                    return 0;
                }

                writeAnswers(fp,"TCP",hp->h_name,ntohs(parameters.port),ntohs(efiport),inet_ntoa(parameters.server),0,0,data);

                //recibimos los datos
                ret =Recibir(stcp,&data,sizeof(data),&parameters);

                //comprobamos si se ha producido algun error en la recepcion
                if(ret < 0)
                {
                    writeAnswers(fp,"TCP",hp->h_name,ntohs(parameters.port),ntohs(efiport),inet_ntoa(parameters.server),1,1,data);
                }
                else
                {
                    writeAnswers(fp,"TCP",hp->h_name,ntohs(parameters.port),ntohs(efiport),inet_ntoa(parameters.server),0,1,data);
                }
            }
        }
    }
}
Exemplo n.º 3
0
void ImapSession::processData() {
    lock_guard<mutex> lock(sessionLock_);

    string data = socket_->readAll();
    incomingData_.append(data);

    bool flag = true;
    while(flag) {
        size_t crlfPos = incomingData_.find(CRLF);

        // Checking for valid line
        if (crlfPos == string::npos)
            break;

        /* Getting line and parse her. Here we only do rough parsing,
         * detailed parsing goes in process<command> methods.*/
        string line = incomingData_.substr(0, crlfPos);
        string *tag, *commandName;


        tag = getCommandTag(line);
        commandName = getCommandName(line);

        if (!tag || !commandName) {
            ImapCommand com {line, ""};
            rejectBad(&com, "Missing command");

            incomingData_.erase(0, crlfPos + 2); // deleting wrong line from buffer
            if (tag) delete tag;
            if (commandName) delete commandName;
            continue;
        }

        ImapCommand *command = new ImapCommand();
        command->tag = *tag;
        command->name = *commandName;

        stringToUpper(command->name);

        IMAP_LOG_LVL(DEBUG, "Received command: {" << command->tag << "," << command->name << "}");

        int commandEnd;

        if (parserFunctions.count(command->name)) {
            CommandParserFunction func =  parserFunctions.at(command->name);
            commandEnd = (this->*func)(command);
            if (commandEnd >= 0) {
                incomingData_.erase(0, commandEnd + 1);
            } else {
                /* Incomplete command. break */
                flag = false;
            }
        } else {
            rejectUnknownCommand(command);
            incomingData_.erase(0, crlfPos + 2); // deleting wrong line from buffer
        }

        writeAnswers();

        if (tag) delete tag;
        if (commandName) delete commandName;

        if (state_ == ImapSessionState::EXIT) {
            break;
        }
    }
}