Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "common.h"
- #include "line_parser.h"
- #include <string.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netdb.h>
- #define BUFF_SIZE 2048
- #define BUFF_SIZE_DOWN 1024
- #define TCP_PORT "2018"
- client_state *curr_client;
- int d_flag = 0;
- void debug_print(char *msg){
- if(d_flag == 1){
- fprintf(stderr, "%s | Log: %s\n", curr_client->server_addr, msg);
- }
- }
- //int after_recv(char* buff, int sockfd, char* server_address){
- //}
- int exec(char* cmd, char* const args[], int args_len){
- if(!strncmp(cmd, "conn", strlen("conn"))){
- if(curr_client->conn_state == IDLE){
- int status, sockfd = -1;
- struct addrinfo *hints = (struct addrinfo*) malloc(sizeof(struct addrinfo));
- hints->ai_family = AF_UNSPEC;
- hints->ai_socktype = SOCK_STREAM;
- struct addrinfo *res;
- if((status = getaddrinfo(args[1], TCP_PORT, hints, &res)) == 0){
- while(res){
- if((sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1){
- res = res->ai_next;
- continue;
- }
- else{
- break;
- }
- }
- if(!res){
- perror("no address to connect");
- return -1;
- }
- if((status = connect(sockfd, res->ai_addr, res->ai_addrlen)) == -1){
- perror("connect");
- return -1;
- }
- else{
- const char *msg = "Hello";
- int len = strlen(msg);
- send(sockfd, msg, len, 0);
- char buff[BUFF_SIZE];
- if(0 == recv(sockfd, buff, BUFF_SIZE, 0)){
- perror("server was closed");
- return -1;
- }
- else{
- if(strncmp(buff, "nok", strlen("nok"))){
- debug_print(buff);
- curr_client->conn_state = CONNECTED;
- curr_client->sock_fd = sockfd;
- char new_address[strlen(args[1])];
- strcpy(new_address, args[1]);
- curr_client->server_addr = new_address;
- int i = 0;
- while(buff[i++] != ' ');
- curr_client->client_id = (buff + i);
- return 1;
- }
- else{
- int i = 0;
- while(buff[i++] != ' ');
- fprintf(stderr, "Server Error: %s\n", (buff + i));
- return -1;
- }
- }
- }
- }
- else{
- perror("can't get addrinfo");
- return -1;
- }
- free(hints);
- }
- else{
- printf("can't connect if not idle\n");
- return -2;
- }
- }
- else if(!strncmp(cmd, "bye", strlen("bye"))){
- if(curr_client->conn_state != CONNECTED){
- printf("can't disconnect if not connected\n");
- return -2;
- }
- else{
- const char *msg = "bye";
- int len = strlen(msg);
- send(curr_client->sock_fd, msg, len, 0);
- close(curr_client->sock_fd);
- curr_client->client_id = 0;
- curr_client->conn_state = IDLE;
- curr_client->sock_fd = -1;
- curr_client->server_addr = "nil";
- return 1;
- }
- }
- else if (!strncmp(cmd, "ls", strlen("ls"))){
- if(curr_client->conn_state != CONNECTED){
- return -2;
- }
- else{
- const char *msg = "ls";
- int len = strlen(msg);
- send(curr_client->sock_fd, msg, len, 0);
- char buff[BUFF_SIZE];
- memset(buff, 0, BUFF_SIZE);
- recv(curr_client->sock_fd, buff, strlen("ok"), 0);
- if(!strncmp(buff, "ok", strlen("ok"))){
- memset(buff, 0, BUFF_SIZE);
- recv(curr_client->sock_fd, buff, BUFF_SIZE, 0);
- printf("%s\n", buff);
- return 1;
- }
- else if(!strncmp(buff, "nok", strlen("nok"))){
- const char *goodbye = "bye";
- int len = strlen(goodbye);
- send(curr_client->sock_fd, goodbye, len, 0);
- close(curr_client->sock_fd);
- curr_client->client_id = 0;
- curr_client->conn_state = IDLE;
- curr_client->sock_fd = -1;
- curr_client->server_addr = "nil";
- int i = 0;
- while(buff[i++] != ' ');
- fprintf(stderr, "Server Error: %s\n", (buff + i));
- return -1;
- }
- }
- }
- else if(!strncmp(cmd, "get", strlen("get"))){
- if(curr_client->conn_state != CONNECTED){
- return -2;
- }
- char buff[BUFF_SIZE_DOWN];
- memset(buff, 0, BUFF_SIZE_DOWN);
- sprintf(buff, "get %s", args[1]);
- send(curr_client->sock_fd, buff, strlen(buff), 0);
- memset(buff, 0, BUFF_SIZE_DOWN);
- recv(curr_client->sock_fd, buff, BUFF_SIZE_DOWN, 0);
- if(!strncmp(buff, "ok", strlen("ok"))){
- curr_client->conn_state = DOWNLOADING;
- int i = 0;
- int len = 0;
- while(buff[i++] != ' ');
- int file_s = atoi(buff + i);
- FILE *fd;
- char file_name[BUFF_SIZE_DOWN];
- memset(file_name, 0, BUFF_SIZE_DOWN);
- sprintf(file_name, "%s.tmp", args[1]);
- fd = fopen(file_name, "w");
- while(file_s > 0){
- memset(buff, 0, BUFF_SIZE_DOWN);
- len = recv(curr_client->sock_fd, buff, BUFF_SIZE_DOWN, 0);
- printf("%d\n", len);
- if(!strncmp(buff, "nok", strlen("nok"))){
- printf("Error while downloading file %s\n",file_name);
- fclose(fd);
- remove(file_name);
- break;
- }
- else{
- fwrite(buff, sizeof(char), len, fd);
- file_s = file_s - len;
- }
- }
- if(file_s <= 0){
- fclose(fd);
- send(curr_client->sock_fd, "done", strlen("done"), 0);
- memset(buff, 0, BUFF_SIZE_DOWN);
- recv(curr_client->sock_fd, buff, BUFF_SIZE_DOWN, 0);
- if(!strncmp(buff, "ok", strlen("ok"))){
- rename(file_name, args[1]);
- curr_client->conn_state = CONNECTED;
- }
- }
- }
- }
- return 0;
- }
- int main(int argc, char const *argv[]) {
- curr_client = (client_state*) malloc(sizeof(client_state));
- curr_client->server_addr = "nil";
- curr_client->client_id = 0;
- curr_client->sock_fd = -1;
- curr_client->conn_state = IDLE;
- if(argc >= 2){
- if(strcmp(argv[1],"-d")){
- d_flag = 1;
- }
- }
- char buffer[BUFF_SIZE];
- cmd_line *cmd;
- while(1){
- printf("server: %s> ", curr_client->server_addr);
- fgets(buffer, BUFF_SIZE, stdin);
- cmd = parse_cmd_lines(buffer);
- if(!strcmp("quit", cmd->arguments[0])){
- return 0;
- }
- exec(cmd->arguments[0], cmd->arguments, cmd->arg_count);
- free_cmd_lines(cmd);
- }
- free(curr_client);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement