mbah_bejo

servergerry.c

Jun 17th, 2021
1,624
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <sys/socket.h>
  6. #include <sys/wait.h>
  7. #include <netinet/in.h>
  8. #include <unistd.h>
  9. #define PORT 8080
  10.  
  11. void exit_on_failed_fork(pid_t child_id) {
  12.     if (child_id < 0)
  13.         exit(EXIT_FAILURE);
  14. }
  15.  
  16. void create_directory(char destination_dir[100]) {
  17.  
  18.     //create dir
  19.     char *argv[] = {"mkdir", "-p", destination_dir, NULL};
  20.     execv("/bin/mkdir", argv);
  21. }
  22.  
  23. int main(int argc, char const *argv[]) {
  24.     /*SOCKET CONFIGURATION*/
  25.     int server_fd, new_socket, valread;
  26.     struct sockaddr_in address;
  27.     int opt = 1;
  28.     int addrlen = sizeof(address);
  29.     char *hello = "Hello from server";
  30.  
  31.     if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
  32.         perror("socket failed");
  33.         exit(EXIT_FAILURE);
  34.     }
  35.  
  36.     /*if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {*/
  37.         /*perror("setsockopt");*/
  38.         /*exit(EXIT_FAILURE);*/
  39.     /*}*/
  40.  
  41.     address.sin_family = AF_INET;
  42.     address.sin_addr.s_addr = INADDR_ANY;
  43.     address.sin_port = htons( PORT );
  44.  
  45.     if (bind(server_fd, (struct sockaddr *)&address, sizeof(address))<0) {
  46.         perror("bind failed");
  47.         exit(EXIT_FAILURE);
  48.     }
  49.  
  50.     if (listen(server_fd, 3) < 0) {
  51.         perror("listen");
  52.         exit(EXIT_FAILURE);
  53.     }
  54.  
  55.     if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0) {
  56.         perror("accept");
  57.         exit(EXIT_FAILURE);
  58.     }
  59.  
  60.     /*SOCKET CONFIGURATION END */
  61.  
  62.     while(1){
  63.         char all_input[10][200];
  64.         char buffer[1024] = {0};
  65.         valread = read(new_socket, buffer, 1024);
  66.  
  67.         char * splitted_input = strtok(buffer, " ");
  68.  
  69.         printf(" full %s\n",buffer);
  70.         printf(" splitted\n");
  71.  
  72.         int counter = 0;
  73.         while(splitted_input != NULL){
  74.             printf(" %s\n",splitted_input);
  75.  
  76.             strcpy(all_input[counter], splitted_input);
  77.             splitted_input = strtok(NULL, " ");
  78.             counter ++;
  79.         }
  80.  
  81.         printf(" udah split\n");
  82.         printf("%s -> %ld \n", all_input[0], strlen(all_input[0]));
  83.         printf("%s\n", all_input[1]);
  84.  
  85.         int execute_child_id = fork();
  86.         exit_on_failed_fork(execute_child_id);
  87.  
  88.         char database_path[100] = "./databases";
  89.  
  90.         if(execute_child_id == 0){
  91.             if(strcmp(all_input[0], "create_db") == 0){
  92.                 strcat(database_path, "/");
  93.                 strcat(database_path,all_input[1]);
  94.                 create_directory(database_path);
  95.             }
  96.         }
  97.     }
  98.     return 0;
  99. }
RAW Paste Data