Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.29 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <netinet/in.h>
  8. #include <arpa/inet.h>
  9. #include <netdb.h>
  10. #include <string.h>
  11. #include <dirent.h> //HEADER for ls command
  12.  
  13. /*
  14.  1. cd ..
  15.  2. args and ports
  16.  3. memory check
  17.  4. loop proper
  18.  5. if client withdraws
  19.  */
  20. #define PORT    5000
  21. #define MAXMSG  1025
  22.  
  23. typedef struct client {
  24.     int sockid;
  25.     char path[1024];
  26.     char inbuffer[1024];
  27.    
  28.     struct client * next;
  29.     struct client * prev;
  30. } client_t;
  31.  
  32. client_t * listHead = NULL;
  33. client_t * listTail = NULL;
  34.  
  35.  
  36. client_t * new_client( _sock )
  37. {
  38.     client_t * p = malloc( sizeof( client_t ) );
  39.     p->sockid = _sock;
  40.     p->path[0] = '\0';
  41.     p->next = NULL;
  42.     p->prev = NULL;
  43.     return p;
  44. }
  45.  
  46. void list_addClient( client_t * _client, int add_to_head )
  47. {
  48.     if ( listHead == NULL ) {
  49.         listHead = listTail = _client;
  50.         return;
  51.     }
  52.    
  53.     if ( add_to_head != 0 ) {
  54.         _client->next = listHead;
  55.         listHead->prev = _client;
  56.         listHead = _client;
  57.     } else {
  58.         _client->prev = listTail;
  59.         listTail->next = _client;
  60.         listTail = _client;
  61.     }
  62. }
  63.  
  64. void list_remClient( client_t * _client )
  65. {
  66.    
  67.     if ( listHead == _client ) {
  68.         listHead = _client->next;
  69.     }
  70.    
  71.     if ( listTail == _client ) {
  72.         listTail = _client->prev;
  73.     }
  74.    
  75.     if ( _client->prev != NULL ) {
  76.         _client->prev->next = _client->next;
  77.     }
  78.    
  79.     if ( _client->next != NULL ) {
  80.         _client->next->prev = _client->prev;
  81.     }
  82.    
  83. }
  84.  
  85.  
  86.  
  87.  
  88. int read_from_client( client_t * _client )
  89. {
  90.     int nbytes;
  91.     memset(_client->inbuffer, '\0' ,sizeof(_client->inbuffer));
  92.     nbytes = read( _client->sockid, _client->inbuffer, 512 );
  93.    
  94.     if ( nbytes > 0 ) {
  95.        
  96.         _client->inbuffer[nbytes] = '\0';
  97.         //getcwd(_client->path, sizeof(_client->path)); //reading path of current dir to PATH array
  98.         return 1;
  99.        
  100.     } else if ( nbytes == 0 ) {
  101.         return 0;
  102.     } else if ( errno == EAGAIN ) {
  103.         return 1;
  104.     } else {
  105.         return 0;
  106.     }
  107. }
  108.  
  109.  
  110. int     main( void )
  111. {
  112.     //extern int make_socket (uint16_t port);
  113.     int sock;
  114.     fd_set active_fd_set, read_fd_set;
  115.     struct sockaddr_in clientname;
  116.     ;
  117.     //size_t size;
  118.     unsigned int size;
  119.    
  120.     /* Create the socket and set it up to accept connections. */
  121.     //sock = make_socket (PORT);
  122.     if ( ( sock = socket( AF_INET, SOCK_STREAM, 0 ) ) == -1 ) {
  123.         perror( "Server-socket() error!" );
  124.         /*just exit!*/
  125.         exit( 1 );
  126.     }
  127.    
  128.     printf( "Server-socket() is OK...\n" );
  129.    
  130.    
  131.    
  132.     /*"address already in use" error message */
  133.     int yes = 1;
  134.    
  135.     if ( setsockopt( sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof( int ) ) == -1 ) {
  136.         perror( "Server-setsockopt() error!" );
  137.         exit( 1 );
  138.     }
  139.    
  140.     printf( "Server-setsockopt() is OK...\n" );
  141.    
  142.    
  143.     /* bind */
  144.     clientname.sin_family = AF_INET;
  145.     clientname.sin_addr.s_addr = htonl( INADDR_ANY );
  146.     clientname.sin_port = htons( 5000 );
  147.     //memset(clientname.sin_zero, '\0', 8);
  148.    
  149.     if ( bind( sock, ( struct sockaddr * )&clientname, sizeof( clientname ) ) == -1 ) {
  150.         perror( "Server-bind() error lol!" );
  151.         exit( 1 );
  152.     }
  153.    
  154.     printf( "Server-bind() is OK...\n" );
  155.    
  156.    
  157.     if ( listen( sock, 1 ) < 0 ) {
  158.         perror( "listen" );
  159.         exit( EXIT_FAILURE );
  160.     }
  161.    
  162.     /* Initialize the set of active sockets. */
  163.     FD_ZERO( &active_fd_set );
  164.     FD_SET( sock, &active_fd_set );
  165.    
  166.     while ( 1 ) {
  167.         /* Block until input arrives on one or more active sockets. */
  168.         read_fd_set = active_fd_set;
  169.        
  170.         if ( select( FD_SETSIZE, &read_fd_set, NULL, NULL, NULL ) < 0 ) {
  171.        perror( "select" );
  172.        exit( EXIT_FAILURE );
  173.         }
  174.        
  175.         //check for incomming connections
  176.         if ( FD_ISSET( sock, &read_fd_set ) ) {
  177.        int sockid;
  178.        size = sizeof( clientname );
  179.        sockid = accept( sock, ( struct sockaddr * ) &clientname, & size );
  180.        
  181.        if ( sockid < 0 ) {
  182.            perror( "accept failure: abandoning this client" );
  183.        } else {
  184.            
  185.            client_t * newSock = new_client( sockid );
  186.            
  187.            newSock->path[0] = 0; //SETS zero for later test whether sockid->path array is empty
  188.            
  189.            list_addClient( newSock, 1 );
  190.            
  191.            fprintf( stderr,
  192.               "Server: new connection from host %s, port %hd.\n",
  193.               inet_ntoa( clientname.sin_addr ),
  194.               ntohs( clientname.sin_port ) );
  195.            FD_SET( sockid, &active_fd_set );
  196.        }
  197.         }
  198.        
  199.         //process all connected clients
  200.         client_t * check = listHead;
  201.         client_t * check_next = listHead;
  202.        
  203.         while ( check != NULL ) {
  204.        check_next = check->next; //needed because we might delete clients while navigating list;
  205.        
  206.        if ( FD_ISSET( check->sockid, &read_fd_set ) )
  207.            {
  208.            //TODO: read data from client
  209.            
  210.            if ( read_from_client(check) == 0) {
  211.           list_remClient( check );
  212.           FD_CLR(check->sockid,&active_fd_set);
  213.           free(check);
  214.            }
  215.            else
  216.                 {   //memset(dir->d_name, '\0', sizeof(dir->d_name));
  217.                     //lists files in the current directory (ls)
  218.                     if (check->inbuffer[0] == '1')
  219.                         {
  220.                         if (check->path[0] == 0)
  221.                        getcwd(check->path, sizeof(check->path)); //reading path of current dir to PATH array
  222.                         DIR *d;
  223.                         struct  dirent *dir;
  224.                         d=opendir(check->path);
  225.                         if (d)
  226.                        {dir = readdir (d);
  227.                            dir = readdir (d); dir = readdir (d); dir = readdir (d); dir = readdir (d);
  228.                            while ((dir = readdir (d)) != NULL)
  229.                           {
  230.                                 //printf("%s\n", dir->d_name);
  231.                                 write(check->sockid, dir->d_name, strlen(dir->d_name));
  232.                                
  233.                           }
  234.                            closedir(d);
  235.                        }
  236.                        
  237.                         }
  238.                     else if (check->inbuffer[0] == '2')//Prints current directory (pwd)
  239.                         {
  240.                         if (check->path[0] == 0)
  241.                        getcwd(check->path, sizeof(check->path)); //reading path of current dir to PATH array
  242.                        
  243.                         //fprintf(stdout, "Current working dir: %s\n", check->path);
  244.                         write(check->sockid, check->path, strlen(check->path));
  245.                         }
  246.                     else if (check->inbuffer[0] == '.')//change current directory (cd)
  247.                         {
  248.                         if (check->path[0] == 0)
  249.                        getcwd(check->path, sizeof(check->path));//reading path of current dir to PATH array
  250.                         int i;
  251.                         int pos=0;
  252.                         for (i=0; i < strlen(check->path); i++)
  253.                        {
  254.                        if (check->path[i] == '/')
  255.                            {
  256.                            pos=i;
  257.                            }
  258.                        }
  259.                         for (i=pos; i<strlen(check->path); i++)
  260.                        {
  261.                        check->path[i]='\0';
  262.                        }
  263.                         fprintf(stdout, "Current working dir: %s\n", check->path);
  264.                         write(check->sockid, check->path, strlen(check->path));
  265.                         }
  266.                     else if (check->inbuffer[0] == '/')// a new absolute directory (cd /)
  267.                         {
  268.                        if (check->path[0] == 0)
  269.                            getcwd(check->path, sizeof(check->path));//reading path of current dir to PATH array
  270.                        memset(  check->path, '\0' ,sizeof(check->path));
  271.                        check->path[0] = '/';
  272.                        fprintf(stdout, "Current working dir: %s\n", check->path);
  273.                        write(check->sockid, check->path, strlen(check->path));
  274.                         }
  275.                     else if (check->inbuffer[0] == ' ')//a new directory relative to the current position (cd  )
  276.                         {
  277.                         char *home_dir = getenv("HOME");
  278.                        
  279.                         if (home_dir)
  280.                        {
  281.                        strcpy(check->path, home_dir);                      
  282.                        printf("Directory relative to the current position: %s\n", check->path);
  283.                        }
  284.                         else
  285.                        printf("Cannot print out realtive directory.\n");
  286.                         write(check->sockid, check->path, strlen(check->path));
  287.                         }
  288.                    
  289.                     //fprintf(stdout, "Current working dir: %s\n", check->path);
  290.                    
  291.                     fprintf( stderr, "Server: got message: `%s'\n", check->inbuffer );
  292.                    
  293.                    
  294.                    
  295.                    
  296.                 }
  297.            }
  298.        check = check_next;
  299.         }
  300.        
  301.     }
  302. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement