Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.48 KB | None | 0 0
  1. #include "common.h"
  2. #include "line_parser.h"
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <unistd.h>
  7.  
  8. #include <sys/types.h>
  9. #include <sys/socket.h>
  10. #include <netdb.h>
  11.  
  12. #define BUFF_SIZE 2048
  13. #define BUFF_SIZE_DOWN 1024
  14. #define TCP_PORT "2018"
  15.  
  16. client_state *curr_client;
  17. int d_flag = 0;
  18.  
  19. void debug_print(char *msg){
  20. if(d_flag == 1){
  21. fprintf(stderr, "%s | Log: %s\n", curr_client->server_addr, msg);
  22. }
  23. }
  24.  
  25. //int after_recv(char* buff, int sockfd, char* server_address){
  26. //}
  27.  
  28. int exec(char* cmd, char* const args[], int args_len){
  29. if(!strncmp(cmd, "conn", strlen("conn"))){
  30. if(curr_client->conn_state == IDLE){
  31. int status, sockfd = -1;
  32. struct addrinfo *hints = (struct addrinfo*) malloc(sizeof(struct addrinfo));
  33. hints->ai_family = AF_UNSPEC;
  34. hints->ai_socktype = SOCK_STREAM;
  35.  
  36. struct addrinfo *res;
  37. if((status = getaddrinfo(args[1], TCP_PORT, hints, &res)) == 0){
  38. while(res){
  39. if((sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1){
  40. res = res->ai_next;
  41. continue;
  42. }
  43. else{
  44. break;
  45. }
  46. }
  47. if(!res){
  48. perror("no address to connect");
  49. return -1;
  50. }
  51. if((status = connect(sockfd, res->ai_addr, res->ai_addrlen)) == -1){
  52. perror("connect");
  53. return -1;
  54. }
  55. else{
  56. const char *msg = "Hello";
  57. int len = strlen(msg);
  58. send(sockfd, msg, len, 0);
  59. char buff[BUFF_SIZE];
  60. if(0 == recv(sockfd, buff, BUFF_SIZE, 0)){
  61. perror("server was closed");
  62. return -1;
  63. }
  64. else{
  65. if(strncmp(buff, "nok", strlen("nok"))){
  66. debug_print(buff);
  67. curr_client->conn_state = CONNECTED;
  68. curr_client->sock_fd = sockfd;
  69. char new_address[strlen(args[1])];
  70. strcpy(new_address, args[1]);
  71. curr_client->server_addr = new_address;
  72. int i = 0;
  73. while(buff[i++] != ' ');
  74. curr_client->client_id = (buff + i);
  75. return 1;
  76. }
  77. else{
  78. int i = 0;
  79. while(buff[i++] != ' ');
  80. fprintf(stderr, "Server Error: %s\n", (buff + i));
  81. return -1;
  82. }
  83. }
  84. }
  85. }
  86. else{
  87. perror("can't get addrinfo");
  88. return -1;
  89. }
  90. free(hints);
  91. }
  92. else{
  93. printf("can't connect if not idle\n");
  94. return -2;
  95. }
  96. }
  97. else if(!strncmp(cmd, "bye", strlen("bye"))){
  98. if(curr_client->conn_state != CONNECTED){
  99. printf("can't disconnect if not connected\n");
  100. return -2;
  101. }
  102. else{
  103. const char *msg = "bye";
  104. int len = strlen(msg);
  105. send(curr_client->sock_fd, msg, len, 0);
  106. close(curr_client->sock_fd);
  107. curr_client->client_id = 0;
  108. curr_client->conn_state = IDLE;
  109. curr_client->sock_fd = -1;
  110. curr_client->server_addr = "nil";
  111. return 1;
  112. }
  113. }
  114. else if (!strncmp(cmd, "ls", strlen("ls"))){
  115. if(curr_client->conn_state != CONNECTED){
  116. return -2;
  117. }
  118. else{
  119. const char *msg = "ls";
  120. int len = strlen(msg);
  121. send(curr_client->sock_fd, msg, len, 0);
  122. char buff[BUFF_SIZE];
  123. memset(buff, 0, BUFF_SIZE);
  124. recv(curr_client->sock_fd, buff, strlen("ok"), 0);
  125. if(!strncmp(buff, "ok", strlen("ok"))){
  126. memset(buff, 0, BUFF_SIZE);
  127. recv(curr_client->sock_fd, buff, BUFF_SIZE, 0);
  128. printf("%s\n", buff);
  129. return 1;
  130. }
  131. else if(!strncmp(buff, "nok", strlen("nok"))){
  132. const char *goodbye = "bye";
  133. int len = strlen(goodbye);
  134. send(curr_client->sock_fd, goodbye, len, 0);
  135. close(curr_client->sock_fd);
  136. curr_client->client_id = 0;
  137. curr_client->conn_state = IDLE;
  138. curr_client->sock_fd = -1;
  139. curr_client->server_addr = "nil";
  140. int i = 0;
  141. while(buff[i++] != ' ');
  142. fprintf(stderr, "Server Error: %s\n", (buff + i));
  143. return -1;
  144. }
  145. }
  146. }
  147. else if(!strncmp(cmd, "get", strlen("get"))){
  148. if(curr_client->conn_state != CONNECTED){
  149. return -2;
  150. }
  151. char buff[BUFF_SIZE_DOWN];
  152. memset(buff, 0, BUFF_SIZE_DOWN);
  153. sprintf(buff, "get %s", args[1]);
  154. send(curr_client->sock_fd, buff, strlen(buff), 0);
  155. memset(buff, 0, BUFF_SIZE_DOWN);
  156. recv(curr_client->sock_fd, buff, BUFF_SIZE_DOWN, 0);
  157. if(!strncmp(buff, "ok", strlen("ok"))){
  158. curr_client->conn_state = DOWNLOADING;
  159. int i = 0;
  160. int len = 0;
  161. while(buff[i++] != ' ');
  162. int file_s = atoi(buff + i);
  163. FILE *fd;
  164. char file_name[BUFF_SIZE_DOWN];
  165. memset(file_name, 0, BUFF_SIZE_DOWN);
  166. sprintf(file_name, "%s.tmp", args[1]);
  167. fd = fopen(file_name, "w");
  168. while(file_s > 0){
  169. memset(buff, 0, BUFF_SIZE_DOWN);
  170. len = recv(curr_client->sock_fd, buff, BUFF_SIZE_DOWN, 0);
  171. printf("%d\n", len);
  172. if(!strncmp(buff, "nok", strlen("nok"))){
  173. printf("Error while downloading file %s\n",file_name);
  174. fclose(fd);
  175. remove(file_name);
  176. break;
  177. }
  178. else{
  179. fwrite(buff, sizeof(char), len, fd);
  180. file_s = file_s - len;
  181. }
  182. }
  183. if(file_s <= 0){
  184. fclose(fd);
  185. send(curr_client->sock_fd, "done", strlen("done"), 0);
  186. memset(buff, 0, BUFF_SIZE_DOWN);
  187. recv(curr_client->sock_fd, buff, BUFF_SIZE_DOWN, 0);
  188. if(!strncmp(buff, "ok", strlen("ok"))){
  189. rename(file_name, args[1]);
  190. curr_client->conn_state = CONNECTED;
  191. }
  192. }
  193.  
  194. }
  195. }
  196. return 0;
  197. }
  198.  
  199. int main(int argc, char const *argv[]) {
  200. curr_client = (client_state*) malloc(sizeof(client_state));
  201. curr_client->server_addr = "nil";
  202. curr_client->client_id = 0;
  203. curr_client->sock_fd = -1;
  204. curr_client->conn_state = IDLE;
  205.  
  206. if(argc >= 2){
  207. if(strcmp(argv[1],"-d")){
  208. d_flag = 1;
  209. }
  210. }
  211.  
  212. char buffer[BUFF_SIZE];
  213. cmd_line *cmd;
  214. while(1){
  215. printf("server: %s> ", curr_client->server_addr);
  216. fgets(buffer, BUFF_SIZE, stdin);
  217. cmd = parse_cmd_lines(buffer);
  218. if(!strcmp("quit", cmd->arguments[0])){
  219. return 0;
  220. }
  221. exec(cmd->arguments[0], cmd->arguments, cmd->arg_count);
  222. free_cmd_lines(cmd);
  223. }
  224. free(curr_client);
  225. return 0;
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement