Example #1
0
int main(int argc, char** argv) {

    if (argc != 3) {
        usage();
    }

    //general variables
    PracticaCaso::TcpClient *client = new PracticaCaso::TcpClient();
    string dnsName, ipAddressAndPort, msg, ip, aux;
    int port, length;
    vector<string> commandVec;
    bool exit = false, badCommand = true;



    //////////////NameServer Part/////////////////////
    client->connect("127.0.0.1", atoi(argv[1])); //connect with the name server

    // Lookup in NameServer(send the domain that we want)
    dnsName = argv[2];
    client->send(dnsName);

    //receive the response
    ipAddressAndPort = client->receive();
    client->close();

    if (ipAddressAndPort.find("ERROR") == 0) {
        cout << "The DNS name " << dnsName << " could not be resolved." << endl;
    }
    else
    {
        //connect to the proposal server
        ip = ipAddressAndPort.substr(0, ipAddressAndPort.find(":"));
        port = atoi((ipAddressAndPort.substr(ipAddressAndPort.find(":")+1)).c_str());
        client->connect(ip,port);
        //////////////////Server Part/////////////////////
        while(!exit)
        {
            //reset bad command boolean
            badCommand = true;

            while(badCommand) //if the command is bad then loop until is a good constructed command
            {
                //insert command
                cout << "Insert your command: ";
                getline(cin, msg);

                //copy the string to have the original msg intact, the strtoken brokens
                strcpy((char *)aux.c_str(),(char *)msg.c_str());
                aux =  string(aux.c_str(), aux.size());
                //split command
                commandVec = extractCommandAndArgs(aux);

                if ((commandVec.size() >= 2) || (commandVec[0].find("quit") == 0))
                    badCommand = false;
                else if(commandVec[0].find("help") == 0) //prompt help
                {
                    helpCommands();
                }
            }

            //if is the command then ecript
            if (commandVec[0].find("pass") == 0)
            {
                //encrypt password
                msg = encryptMsg(commandVec[1]);
                msg = "pass " + msg;
            } else if(commandVec[0].find("quit") == 0) //we want to quit, we send to the server and wait response to close connection
                exit = true;

            //send and wait response
            client->send(msg);
            msg = client->receive();

            cout << msg << endl;

            if(msg.find("[QUIT]") == 0)
                exit = true;
            else if((msg.find("[USER OK]") == 0)) //automate password asking
            {
                msg = "[PASS ERROR]"; //reset the msg to enter the while loop

                while( (msg.find("[PASS ERROR]") == 0) && (msg.find("[QUIT]") != 0) )
                {
                    //insert command
                    cout << "Insert your password: "******"pass " + msg;
                    client->send(msg);
                    msg = client->receive();
                    cout << msg << endl;
                }
            }
        }
        //finish
        client->close();
    }

    delete client;

}
Example #2
0
File: main.c Project: moikop/ab
int processData(tdns* dns,char** argv,char* cmd,FILE* logf) {

    tdomain data;
    tdomain td;
    char ip_origen[IP_MAX];
    char ip_destino[IP_MAX];
    char msg[300];

    if(strcmp(argv[1],CMD_SEND)==0) {
        if(urlExists(*dns,argv[2])!=RES_OK) return RES_ERROR;
        if(urlExists(*dns,argv[3])!=RES_OK) return RES_ERROR;
        getValue(dns,argv[2],&data);
        strcpy(ip_origen,data.ip);
        getValue(dns,argv[3],&data);
        strcpy(ip_destino,data.ip);
        encryptMsg(msg,data.offset);
        log(logf,CMD_SEND,argv[2],ip_origen,argv[3],ip_destino,argv[3],msg);
        printf("Mensaje encriptado: %s\n",msg);
    }
    else if(strcmp(argv[1],CMD_GETIP)==0) {
            printf("Entré en  getip\n");
        if(urlExists(*dns,argv[2])!=RES_OK) return RES_ERROR;
        if(urlExists(*dns,argv[3])!=RES_OK) return RES_ERROR;
        getValue(dns,argv[2],&data);
        strcpy(ip_origen,data.ip);
        getValue(dns,argv[3],&data);
        strcpy(ip_destino,data.ip);
        log(logf,CMD_GETIP,argv[2],ip_origen,argv[3],ip_destino,"","");
        printf("Origen: %s %s\nDestino: %s %s\n",argv[2],ip_origen,argv[3],ip_destino);
    }
    else if(strcmp(argv[1],CMD_ADDDOMAIN)==0) {
        if(urlExists(*dns,argv[2])==RES_OK) {
            printf("Ya existe %s.\n",argv[2]);
            return RES_ERROR;
        }
        strcpy(td.domain,argv[2]);
        strcpy(td.ip,argv[3]);
        genoffset(td.domain);
        getoffset(td.domain,&(td.offset));
        AB_Crear(&(td.subab),sizeof(tdomain));
        if(addDomain(dns,argv[2],&td)!=RES_OK) return RES_ERROR;
        log(logf,CMD_ADDDOMAIN,argv[2],argv[3],"","","","");
        printf("Se agrego %s con direccion ip %s.\n",argv[2],argv[3]);
    }
    else if(strcmp(argv[1],CMD_DELETEDOMAIN) == 0) {
        if(urlExists(*dns,argv[2])!=RES_OK) {
            printf("No existe %s para eliminar.\n",argv[2]);
            return RES_ERROR;
        }
        getValue(dns,argv[2],&data);
        printf("Dato obtenido: %s %s.\n",data.domain,data.ip);
        strcpy(ip_origen,data.ip);
        deleteDomain(dns,argv[2]);
        log(logf,CMD_DELETEDOMAIN,argv[2],ip_origen,"","","","");
        printf("Se elimino a %s con ip %s.\n",argv[2],ip_origen);
    } else {
        printf("Comando equivocado.\n");
        showHelp(argv[0]);
        return RES_ERROR;
    }
    return RES_OK;
}