diff --git a/Makefile b/Makefile index 1c228040e0c221c3a971f6096d9815b4ae848864..3dfc5690e6dcd239daee5e47220b356c75ada1c7 100644 --- a/Makefile +++ b/Makefile @@ -8,8 +8,8 @@ SRC=./src OBJ=./obj FLAGS = -w -main: connectionRawSocket messages submessages - $(CC) $(SRC)/main.cpp $(OBJ)/connectionRawSocket.o $(OBJ)/messages.o $(OBJ)/submessages.o $(FLAGS) -I$(INCLUDE) -L$(LIB) -o connection +main: connectionRawSocket messages miniShell submessages + $(CC) $(SRC)/main.cpp $(OBJ)/connectionRawSocket.o $(OBJ)/messages.o $(OBJ)/miniShell.o $(OBJ)/submessages.o $(FLAGS) -I$(INCLUDE) -L$(LIB) -o connection connectionRawSocket: $(CC) -c $(SRC)/connectionRawSocket.cpp $(FLAGS) -I$(INCLUDE) -o $(OBJ)/connectionRawSocket.o @@ -19,6 +19,10 @@ messages: $(CC) -c $(SRC)/messages.cpp $(FLAGS) -I$(INCLUDE) -o $(OBJ)/messages.o ar -cru $(LIB)/messages.a $(OBJ)/messages.o +miniShell: + $(CC) -c $(SRC)/miniShell.cpp $(FLAGS) -I$(INCLUDE) -o $(OBJ)/miniShell.o + ar -cru $(LIB)/miniShell.a $(OBJ)/miniShell.o + submessages: $(CC) -c $(SRC)/submessages.cpp $(FLAGS) -I$(INCLUDE) -o $(OBJ)/submessages.o ar -cru $(LIB)/submessages.a $(OBJ)/submessages.o diff --git a/include/connectionRawSocket.h b/include/connectionRawSocket.h index 587d95723853ff1d9c44d26b4e3be5b6e81e7b07..0c902198414d0c2003882bb8b87e31460e64716b 100644 --- a/include/connectionRawSocket.h +++ b/include/connectionRawSocket.h @@ -27,6 +27,8 @@ #include <unistd.h> #include <sys/stat.h> #include <sys/statvfs.h> +#include <cmath> +#include <vector> using namespace std; diff --git a/include/miniShell.h b/include/miniShell.h new file mode 100644 index 0000000000000000000000000000000000000000..d0c143bc87f7f3bec24415cf2a6718c3cba1825e --- /dev/null +++ b/include/miniShell.h @@ -0,0 +1,10 @@ +// Implementado por: Eduardo Machado e Victor Perszel +// 2015 + +#include "connectionRawSocket.h" + +using namespace std; + +int lsParameter(string parameter); +int lsMini(string parameter, char* currentPath); +int cdMini(string intendedDirectory); diff --git a/miniShellSrc/runMiniShell.cpp b/miniShellSrc/runMiniShell.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3bbb1dd1f30db58577d3ef9677ebef3a98c7970b --- /dev/null +++ b/miniShellSrc/runMiniShell.cpp @@ -0,0 +1,30 @@ +/* Programa criado apenas para fazer testes da biblioteca miniShell */ + +#include "miniShell.cpp" + +int main(int argc, char const *argv[]) { + char *buff, *currentPath; + long size; + string command, parameter; + + while(1){ + /* Escreve onde você está. */ + size = pathconf(".", _PC_PATH_MAX); + if ((buff = (char *)malloc((size_t)size)) != NULL) + currentPath = getcwd(buff, (size_t)size); + printf("%s $ ", currentPath); + + /* Executa o comando escolhido. */ + cin >> command; + if(command == "ls"){ + parameter = ""; + cin >> parameter; + lsMini(parameter, currentPath); + } + else if(command == "cd"){ + cin >> parameter; + cdMini(parameter); + } + } + return 0; +} diff --git a/src/connectionRawSocket.cpp b/src/connectionRawSocket.cpp index 7451c5794fb8e2bd9f2768740175915582b1337c..04da20ee33d49ad0341fe73a6e9462ead7626c9b 100644 --- a/src/connectionRawSocket.cpp +++ b/src/connectionRawSocket.cpp @@ -1,13 +1,4 @@ -#include <sys/types.h> -#include <sys/socket.h> -#include <sys/ioctl.h> -#include <net/ethernet.h> -#include <arpa/inet.h> -#include <linux/if_packet.h> -#include <linux/if.h> -#include <stdlib.h> -#include <string.h> -#include <stdio.h> +#include "connectionRawSocket.h" int openRawSocket(char *device) { diff --git a/src/main.cpp b/src/main.cpp index 728f51fac8a291a53e26c76f0a5e3d3dc46f3e14..d94713d2d1226176e67dabcefa56977d6f3c24e3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,83 +3,83 @@ #include "connectionRawSocket.h" #include "messages.h" +#include "miniShell.h" using namespace std; int soquete; -int main(int argc, char *argv[]) -{ - unsigned char dados[64] = "mensagem de teste"; - unsigned char *saida; - SubMessage mesg; - int i; +int main(int argc, char *argv[]) { + Message mainMessage; + SubMessage serverReceive, resposta; + string service, message, command, parameter; + char *buff, *currentPath, *receiptMessage = (char*)malloc(sizeof(char*)*68); + int socket, sendSize, receiptSize; + long size; - //42 = 101010 / 21 = 010101 / 6 = 0110 - mesg.setData(dados, 21, 6); - saida = mesg.objToString(); - for(i=0;i<22;i++){ - std::bitset<8> x(saida[i]); - std::cout << x << " - " << saida[i] << endl; + if(argc != 2){ // Testa o número de parâmetros de estrada do programa. + cout << "Erro na entrada de parametros." << endl; // Caso houver mais ou menos parâmetros + return 0; // o programa não continua a execução. } - return 0; -} - - -// int main(int argc, char *argv[]) { -// string service, message; -// char *receiptMessage = (char*)malloc(sizeof(char*)*64); -// int socket, sendSize, receiptSize; -// if(argc != 2){ // Testa o número de parâmetros de estrada do programa. -// cout << "Erro na entrada de parametros." << endl; // Caso houver mais ou menos parâmetros -// return 0; // o programa não continua a execução. -// } + service = argv[1]; -// service = argv[1]; + // Estabelece a conexão + socket = openRawSocket("eth0"); + // Verifica se a conexão socket deu certo + if(socket == -1){ + cout << "Problemas com o Socket." << endl; + return 0; + } -// // Estabelece a conexão -// socket = openRawSocket("eth0"); -// // Verifica se a conexão socket deu certo -// if(socket == -1){ -// cout << "Problemas com o Socket." << endl; -// return 0; -// } + if(service == "client"){ + cout << "Execução em modo cliente iniciada." << endl; + cout << "==================================" << endl; + while(1){ + /* Escreve onde você está. */ + size = pathconf(".", _PC_PATH_MAX); + if ((buff = (char *)malloc((size_t)size)) != NULL) + currentPath = getcwd(buff, (size_t)size); + printf("%s (C) ", currentPath); -// if(service == "client"){ -// cout << "Execução em modo cliente iniciada." << endl; -// cout << "==================================" << endl; -// while(1){ -// cin.clear(); -// cin >> message; -// // Envia a mensagem. -// sendSize = send(socket, (char*)message.c_str(), message.length()+1, 0); -// // Verifica se há algum problema com o socket. -// if(sendSize == -1){ -// cout << "Problemas com o envio da mensagem: \n" << message << endl; -// }else{ -// cout << "Mensagem: " << message << endl; -// cout << "Tamanho: " << sendSize << endl; -// cout << "Enviado." << endl; -// } -// } -// }else if(service == "server"){ -// cout << "Execução em modo servidor iniciada." << endl; -// cout << "===================================" << endl; -// while(1){ -// // Recebe a mensagem. -// receiptSize = recv(socket, receiptMessage, 64*sizeof(char), 0); -// // Passa o valor do *char para uma string. -// message = receiptMessage; -// if (receiptSize == -1){ -// cout << "Problemas com o recebimento da mensagem." << endl; -// } -// else{ -// cout << "Mensagem: " << message << endl; -// cout << "Tamanho: " << receiptSize << endl; -// cout << "Recebido." << endl; -// } -// } -// } -// return 0; -// } + /* Executa o comando escolhido. */ + cin >> command; + if(command == "ls"){ + parameter = ""; + cin >> parameter; + lsMini(parameter, currentPath); + } + else if(command == "cd"){ + cin >> parameter; + cdMini(parameter); + } else if(command == "rcd"){ + cin >> parameter; + mainMessage.sendCD((unsigned char*)(parameter.c_str())); + } + } + }else if(service == "server"){ + cout << "Execução em modo servidor iniciada." << endl; + cout << "===================================" << endl; + while(1){ + recv(soquete, receiptMessage, 68*sizeof(char), 0); + serverReceive.stringToObj((unsigned char*)receiptMessage); + // Lê começo da mensagem e vê se tem o delimitador de início + if(serverReceive.getStartMessage() == 0x7E){ // 0x7E = 01111110 + if(serverReceive.getType() == 3){ + if(serverReceive.checkParity()){ // Se a paridade estiver errada + resposta.setData((unsigned char*)'0',0,0); // Manda um Nack + send(soquete,resposta.objToString(),resposta.getSize()+4, 0); + } else { + mainMessage.receiveCD(serverReceive.getData()); + /* Escreve onde você está. */ + size = pathconf(".", _PC_PATH_MAX); + if ((buff = (char *)malloc((size_t)size)) != NULL) + currentPath = getcwd(buff, (size_t)size); + printf("%s (S) ", currentPath); + } + } + } + } + } + return 0; +} diff --git a/src/messages.cpp b/src/messages.cpp index cc938869e79f0faa03cbe3bba48a7b0459a0b02a..76fe10fa97cb843ed5950bedd03febcd73a0aa08 100644 --- a/src/messages.cpp +++ b/src/messages.cpp @@ -7,7 +7,7 @@ int Message::sendCD(unsigned char* dirName){ int error, success = 0, garbage, rv; - char *receiptMessage = (char*)malloc(sizeof(char*)*64); + char *receiptMessage = (char*)malloc(sizeof(char*)*68); struct pollfd ufds[1]; // usado para timeout em recv SubMessage changeDir, answer; @@ -67,7 +67,7 @@ int Message::sendCD(unsigned char* dirName){ int Message::sendLS(unsigned char* options){ int error, success = 0, garbage, rv; - char *receiptMessage = (char*)malloc(sizeof(char*)*64); + char *receiptMessage = (char*)malloc(sizeof(char*)*68); struct pollfd ufds[1]; // usado para timeout em recv char* listagem; SubMessage listDir, answer, ack, nack; @@ -158,7 +158,7 @@ int Message::sendLS(unsigned char* options){ int Message::sendPUT(unsigned char* fileName){ int error, success = 0, garbage, rv; - char *receiptMessage = (char*)malloc(sizeof(char*)*64); + char *receiptMessage = (char*)malloc(sizeof(char*)*68); string temp, fileData; struct pollfd ufds[1]; // usado para timeout em recv SubMessage put, answer, size; @@ -248,7 +248,7 @@ int Message::sendPUT(unsigned char* fileName){ int Message::sendGET(unsigned char* fileName, char* currentPath){ int error, success = 0, garbage, rv, size, availableSize; - char *receiptMessage = (char*)malloc(sizeof(char*)*64); + char *receiptMessage = (char*)malloc(sizeof(char*)*68); string temp, fileData; struct pollfd ufds[1]; // usado para timeout em recv SubMessage get, answer, ack, nack, err, ok; @@ -334,7 +334,7 @@ int Message::sendData(unsigned char* stringMessage){ int numberOfMessages, sizeLastMessage, size=0; int i, j, k, garbage, error, rv, success = 0; unsigned char* subData; // a parte da mensagem que vai em cada sequencia - char *receiptMessage = (char*)malloc(sizeof(char*)*64); + char *receiptMessage = (char*)malloc(sizeof(char*)*68); char *erro1, *erro2, *sucesso, *buffer; struct pollfd ufds[1]; // usado para timeout em recv SubMessage answer, end; @@ -669,7 +669,7 @@ int Message::receiveGET(unsigned char* fileName){ int Message::receivePUT(unsigned char* fileName, char* currentPath){ int success = 0, rv, size, availableSize; - char *receiptMessage = (char*)malloc(sizeof(char*)*64); + char *receiptMessage = (char*)malloc(sizeof(char*)*68); SubMessage resposta, answer; struct statvfs *buff; struct pollfd ufds[1]; @@ -729,7 +729,7 @@ int Message::receiveData(unsigned char* fileName, int size, int save){ int numberOfMessages, messagesCounter = 0, seqCounter; int i, garbage, error, rv, success = 0, greaterSeq = 0; unsigned char *subData; - char *receiptMessage = (char*)malloc(sizeof(char*)*64); + char *receiptMessage = (char*)malloc(sizeof(char*)*68); struct pollfd ufds[1]; SubMessage nack, ack, answer; ofstream fileOut; diff --git a/src/miniShell.cpp b/src/miniShell.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f20d3cb5fc91ab6f99eef08ada46619cf31b7ed2 --- /dev/null +++ b/src/miniShell.cpp @@ -0,0 +1,64 @@ +/* Implementado por: Eduardo Machado e Victor Perszel */ +/* 2015 */ + +#include "miniShell.h" + +int lsParameter(string parameter){ + if(parameter == ".") return 1; + else if(parameter == "-a") return 2; + else if(parameter == "-l") return 3; + else if(parameter == "-la") return 4; + else return -1; +} + +int lsMini(string parameter, char* currentPath){ + string mostra, temp, path, lsAux, fileName; + ifstream lsFile; + + path = currentPath; + /* Para dar um ls apenas, é necessário escrever "ls ." */ + /* Precisamos pensar em um jeito de concertar isso. */ + switch(lsParameter(parameter)){ + case 1: + lsAux = "ls > " + path + "/ls.txt"; + system(lsAux.c_str()); + break; + case 2: + lsAux = "ls -a > " + path + "/ls.txt"; + system(lsAux.c_str()); + break; + case 3: + lsAux = "ls -l > " + path + "/ls.txt"; + system(lsAux.c_str()); + break; + case 4: + lsAux = "ls -la > " + path + "/ls.txt"; + system(lsAux.c_str()); + break; + case -1: + puts("Parâmetro de ls inválido."); + return -1; + break; + } + printf("\n"); + + // Abertura do arquivo de entrada. + fileName = path + "/ls.txt"; + lsFile.open(fileName.c_str(), ios::in); + lsFile.seekg (0); + // Leitura do arquivo. + while(getline(lsFile, temp)) { + mostra += temp + "\n"; + } + // Remove arquivo ls.txt + remove(fileName.c_str()); + + cout << "mostra:" << mostra << endl; + return 0; +} + +int cdMini(string intendedDirectory){ + int errorTest; + errorTest = chdir(intendedDirectory.c_str()); + return errorTest; +}