Advertisement
Guest User

srv3.c

a guest
Sep 4th, 2010
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.69 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <errno.h>
  6. #include <sys/types.h>
  7. #include <sys/wait.h>
  8. #include <sys/socket.h>
  9. #include <arpa/inet.h>
  10.  
  11. #define DFL_PORT 8006
  12. #define BACKLOG 10
  13. #define BUFLEN 120
  14.  
  15. char buf[BUFLEN];
  16.  
  17.  
  18. void make_server_addr(struct sockaddr_in * addr, unsigned short int port) {
  19.   addr->sin_family = AF_INET;
  20.   addr->sin_addr.s_addr = htonl(INADDR_ANY);
  21.   addr->sin_port = htons(port);
  22.   memset(&(addr->sin_zero), '\0', 8);
  23.  
  24. }
  25.  
  26. void out(int client_fd, char * buf, size_t len) {
  27.   size_t sent;
  28.  
  29.   while (len > 0) {
  30.     sent = write(client_fd, buf, len);
  31.     if (sent < 0) {
  32.       perror("out: write");
  33.       close(client_fd);
  34.       exit(EXIT_FAILURE);
  35.     }
  36.     len -= sent;
  37.   }
  38.  
  39. }
  40.  
  41. void ls_cmd(int client_fd){
  42.   pid_t pid;
  43.   char ls_cmd[] = "/bin/ls";
  44.   char * ls_args[] = {"ls", "-lh", 0};
  45.   int retval;
  46.   printf("ls_cmd buf %s\n", buf);
  47.   if((pid = fork()) < 0){
  48.     perror("ls_cmd: fork");
  49.     close(client_fd);
  50.     exit(EXIT_FAILURE);
  51.   }else if(!pid){
  52.     /* child */
  53.     if(dup2(client_fd, 1) < 0 ){
  54.       perror("ls_cmd: dup2");
  55.       close(client_fd);
  56.       exit(EXIT_FAILURE);
  57.     }
  58.  
  59.     if(execv(ls_cmd, ls_args)){
  60.       perror("ls_cmd: execv");
  61.       close(client_fd);
  62.       exit(EXIT_FAILURE);
  63.     }
  64.  
  65.   }else{
  66.     waitpid(pid, &retval, 0);
  67.     if(retval != EXIT_SUCCESS){
  68.       out(client_fd, "Internal server error.\n", 24);
  69.       close(client_fd);
  70.       exit(EXIT_FAILURE);
  71.     }
  72.   }
  73.  
  74. }
  75.  
  76.  
  77. void get_cmd(int client_fd){
  78.  
  79.     int isfileopen = 0;
  80.     int filesize;
  81.     int readcount;
  82.     char* filename = malloc(120);
  83.     filename = strtok(buf, " ");
  84.     filename = strtok(NULL, " ");
  85. //     filename[strlen(filename)-1] = '\0';
  86.     char* string = malloc(BUFLEN);
  87.     printf("file name %s_\n", filename);
  88.    
  89.     FILE *fp;
  90.     fp = fopen(filename, "rb");
  91.     if(!fp){
  92.         string = "Error opening file\n";
  93.         out(client_fd, string, strlen(string)+1);
  94.         perror("get_cmd file open");
  95.     }else{
  96.         isfileopen = 1;
  97.  
  98.         while(1){
  99.             if(feof(fp)){
  100.                 printf("done sending file, last slice length: %d\n", readcount);
  101.                 break;
  102.             }
  103.  
  104.             readcount = fread(string, 1, BUFLEN, fp);
  105.             out(client_fd, string, readcount);
  106.            
  107.         }
  108.     }
  109.     if(isfileopen){
  110.         fclose(fp);
  111.     }
  112.  
  113. }
  114.  
  115.  
  116. void serve(int client_fd) {
  117.     char* error = malloc(120);
  118.     size_t readcount;
  119.     while ( (readcount = read(client_fd, buf, BUFLEN)) > 0) {
  120.             printf("buf%c_ %s strlen(buf) %d_ \n", buf[0] , buf, readcount);
  121.  
  122.         if(buf[0] == 'g'){
  123.             printf("serve: file %s_\n", buf);
  124.             get_cmd(client_fd);
  125.         }else if(buf[0] == 'l'){
  126.             puts("Serve: ls");
  127.             ls_cmd(client_fd);
  128.         }else{
  129.             int i;
  130.  
  131.             printf(error, "Unknown command: %c, %d, %s_\n", buf[0], buf[0], buf);
  132.             sprintf(error, "Unknown command: %c, %d, %s_\n", buf[0], buf[0], buf);
  133.             out(client_fd, error, strlen(error));
  134.         }
  135.  
  136.     }
  137.     close(client_fd);
  138.    
  139. }
  140.  
  141. int main(int argc, char ** argv) {
  142.  
  143.   int listen_fd;
  144.   struct sockaddr_in client_addr;
  145.   socklen_t client_addr_len = sizeof(struct sockaddr_in);
  146.   struct sockaddr_in local_address;
  147.   int on = 1;
  148.   int client_fd;
  149.   pid_t pid;
  150.  
  151.  
  152.   /* make a listening socket */
  153.   listen_fd = socket(PF_INET, SOCK_STREAM, 0);
  154.   if ( listen_fd < 0 ) {
  155.     perror("main: socket");
  156.     exit(EXIT_FAILURE);
  157.   }
  158.  
  159.   make_server_addr(&local_address, DFL_PORT);
  160.  
  161.   if( bind(listen_fd, (struct sockaddr *)&local_address,
  162.                                     sizeof(local_address)) ){
  163.     perror("main: bind");
  164.     exit(EXIT_FAILURE);
  165.   }
  166.  
  167.  
  168.   if( listen(listen_fd, BACKLOG) ){
  169.     perror("main: listen");
  170.     exit(EXIT_FAILURE);
  171.   }
  172.  
  173.  
  174.   if( setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) ){
  175.     perror("main: setsockopt:");
  176.     exit(EXIT_FAILURE);
  177.   }
  178.  
  179.   puts("Started listening");
  180.  
  181.   while(1){
  182.      
  183.     client_fd = accept(listen_fd, (struct sockaddr *)&client_addr,
  184.                                                     &client_addr_len );
  185.     if (client_fd < 0 ) {
  186.       perror("main: accept");
  187.       exit(EXIT_FAILURE);
  188.     }
  189.  
  190.     printf("Client connected!\n");
  191.  
  192.     if( (pid = fork()) < 0 ) {
  193.       perror("myserver: fork");
  194.       exit(EXIT_FAILURE);
  195.     } else if(!pid) {
  196.       /* child */
  197.  
  198.       close(listen_fd);
  199.  
  200.       serve(client_fd);
  201.  
  202.       printf("Client disconnected!\n");
  203.       exit(EXIT_SUCCESS);
  204.  
  205.     } else {
  206.       /* parent*/
  207.       close(client_fd);
  208.     }
  209.  
  210.  
  211.   }
  212.  
  213.   close(listen_fd);
  214.   exit(EXIT_SUCCESS);
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement