Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <errno.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <netdb.h>
- #include <string.h>
- #include <dirent.h> //HEADER for ls command
- /*
- 1. cd ..
- 2. args and ports
- 3. memory check
- 4. loop proper
- 5. if client withdraws
- */
- #define PORT 5000
- #define MAXMSG 1025
- typedef struct client {
- int sockid;
- char path[1024];
- char inbuffer[1024];
- struct client * next;
- struct client * prev;
- } client_t;
- client_t * listHead = NULL;
- client_t * listTail = NULL;
- client_t * new_client( _sock )
- {
- client_t * p = malloc( sizeof( client_t ) );
- p->sockid = _sock;
- p->path[0] = '\0';
- p->next = NULL;
- p->prev = NULL;
- return p;
- }
- void list_addClient( client_t * _client, int add_to_head )
- {
- if ( listHead == NULL ) {
- listHead = listTail = _client;
- return;
- }
- if ( add_to_head != 0 ) {
- _client->next = listHead;
- listHead->prev = _client;
- listHead = _client;
- } else {
- _client->prev = listTail;
- listTail->next = _client;
- listTail = _client;
- }
- }
- void list_remClient( client_t * _client )
- {
- if ( listHead == _client ) {
- listHead = _client->next;
- }
- if ( listTail == _client ) {
- listTail = _client->prev;
- }
- if ( _client->prev != NULL ) {
- _client->prev->next = _client->next;
- }
- if ( _client->next != NULL ) {
- _client->next->prev = _client->prev;
- }
- }
- int read_from_client( client_t * _client )
- {
- int nbytes;
- memset(_client->inbuffer, '\0' ,sizeof(_client->inbuffer));
- nbytes = read( _client->sockid, _client->inbuffer, 512 );
- if ( nbytes > 0 ) {
- _client->inbuffer[nbytes] = '\0';
- //getcwd(_client->path, sizeof(_client->path)); //reading path of current dir to PATH array
- return 1;
- } else if ( nbytes == 0 ) {
- return 0;
- } else if ( errno == EAGAIN ) {
- return 1;
- } else {
- return 0;
- }
- }
- int main( void )
- {
- //extern int make_socket (uint16_t port);
- int sock;
- fd_set active_fd_set, read_fd_set;
- struct sockaddr_in clientname;
- ;
- //size_t size;
- unsigned int size;
- /* Create the socket and set it up to accept connections. */
- //sock = make_socket (PORT);
- if ( ( sock = socket( AF_INET, SOCK_STREAM, 0 ) ) == -1 ) {
- perror( "Server-socket() error!" );
- /*just exit!*/
- exit( 1 );
- }
- printf( "Server-socket() is OK...\n" );
- /*"address already in use" error message */
- int yes = 1;
- if ( setsockopt( sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof( int ) ) == -1 ) {
- perror( "Server-setsockopt() error!" );
- exit( 1 );
- }
- printf( "Server-setsockopt() is OK...\n" );
- /* bind */
- clientname.sin_family = AF_INET;
- clientname.sin_addr.s_addr = htonl( INADDR_ANY );
- clientname.sin_port = htons( 5000 );
- //memset(clientname.sin_zero, '\0', 8);
- if ( bind( sock, ( struct sockaddr * )&clientname, sizeof( clientname ) ) == -1 ) {
- perror( "Server-bind() error lol!" );
- exit( 1 );
- }
- printf( "Server-bind() is OK...\n" );
- if ( listen( sock, 1 ) < 0 ) {
- perror( "listen" );
- exit( EXIT_FAILURE );
- }
- /* Initialize the set of active sockets. */
- FD_ZERO( &active_fd_set );
- FD_SET( sock, &active_fd_set );
- while ( 1 ) {
- /* Block until input arrives on one or more active sockets. */
- read_fd_set = active_fd_set;
- if ( select( FD_SETSIZE, &read_fd_set, NULL, NULL, NULL ) < 0 ) {
- perror( "select" );
- exit( EXIT_FAILURE );
- }
- //check for incomming connections
- if ( FD_ISSET( sock, &read_fd_set ) ) {
- int sockid;
- size = sizeof( clientname );
- sockid = accept( sock, ( struct sockaddr * ) &clientname, & size );
- if ( sockid < 0 ) {
- perror( "accept failure: abandoning this client" );
- } else {
- client_t * newSock = new_client( sockid );
- newSock->path[0] = 0; //SETS zero for later test whether sockid->path array is empty
- list_addClient( newSock, 1 );
- fprintf( stderr,
- "Server: new connection from host %s, port %hd.\n",
- inet_ntoa( clientname.sin_addr ),
- ntohs( clientname.sin_port ) );
- FD_SET( sockid, &active_fd_set );
- }
- }
- //process all connected clients
- client_t * check = listHead;
- client_t * check_next = listHead;
- while ( check != NULL ) {
- check_next = check->next; //needed because we might delete clients while navigating list;
- if ( FD_ISSET( check->sockid, &read_fd_set ) )
- {
- //TODO: read data from client
- if ( read_from_client(check) == 0) {
- list_remClient( check );
- FD_CLR(check->sockid,&active_fd_set);
- free(check);
- }
- else
- { //memset(dir->d_name, '\0', sizeof(dir->d_name));
- //lists files in the current directory (ls)
- if (check->inbuffer[0] == '1')
- {
- if (check->path[0] == 0)
- getcwd(check->path, sizeof(check->path)); //reading path of current dir to PATH array
- DIR *d;
- struct dirent *dir;
- d=opendir(check->path);
- if (d)
- {dir = readdir (d);
- dir = readdir (d); dir = readdir (d); dir = readdir (d); dir = readdir (d);
- while ((dir = readdir (d)) != NULL)
- {
- //printf("%s\n", dir->d_name);
- write(check->sockid, dir->d_name, strlen(dir->d_name));
- }
- closedir(d);
- }
- }
- else if (check->inbuffer[0] == '2')//Prints current directory (pwd)
- {
- if (check->path[0] == 0)
- getcwd(check->path, sizeof(check->path)); //reading path of current dir to PATH array
- //fprintf(stdout, "Current working dir: %s\n", check->path);
- write(check->sockid, check->path, strlen(check->path));
- }
- else if (check->inbuffer[0] == '.')//change current directory (cd)
- {
- if (check->path[0] == 0)
- getcwd(check->path, sizeof(check->path));//reading path of current dir to PATH array
- int i;
- int pos=0;
- for (i=0; i < strlen(check->path); i++)
- {
- if (check->path[i] == '/')
- {
- pos=i;
- }
- }
- for (i=pos; i<strlen(check->path); i++)
- {
- check->path[i]='\0';
- }
- fprintf(stdout, "Current working dir: %s\n", check->path);
- write(check->sockid, check->path, strlen(check->path));
- }
- else if (check->inbuffer[0] == '/')// a new absolute directory (cd /)
- {
- if (check->path[0] == 0)
- getcwd(check->path, sizeof(check->path));//reading path of current dir to PATH array
- memset( check->path, '\0' ,sizeof(check->path));
- check->path[0] = '/';
- fprintf(stdout, "Current working dir: %s\n", check->path);
- write(check->sockid, check->path, strlen(check->path));
- }
- else if (check->inbuffer[0] == ' ')//a new directory relative to the current position (cd )
- {
- char *home_dir = getenv("HOME");
- if (home_dir)
- {
- strcpy(check->path, home_dir);
- printf("Directory relative to the current position: %s\n", check->path);
- }
- else
- printf("Cannot print out realtive directory.\n");
- write(check->sockid, check->path, strlen(check->path));
- }
- //fprintf(stdout, "Current working dir: %s\n", check->path);
- fprintf( stderr, "Server: got message: `%s'\n", check->inbuffer );
- }
- }
- check = check_next;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement