Advertisement
Guest User

cl3.c

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