Commits

tuxik  committed 2b12cd2

50 percent

  • Participants
  • Parent commits ac01fed

Comments (0)

Files changed (5)

 # promenne prekladu
 
 
-NAME = webclient server
+NAME = client server
 CPP  = g++
 CC   = g++
 SCFLAGS = -g -pedantic -Wall -W -O2 -DINLINE
 
 ################################################################################
 # linkovani vysledneho souboru
-webclient: main.o parser.o socket.o
-	$(CC) -o $@ main.o parser.o socket.o
+client: client.o socket.o
+	$(CC) -o $@ client.o socket.o
 	
-server: server.o
-	$(CC) -o $@ server.o
+server: server.o socket.o
+	$(CC) -o $@ server.o socket.o -lpthread
 
 ################################################################################
 # kompilace hlavniho souboru
 	
 server.o: server.cpp
 	$(CC) -c server.cpp -o server.o $(CXXFLAGS)
+	
+client.o: client.cpp
+	$(CC) -c client.cpp -o client.o $(CXXFLAGS)
 
 ################################################################################
 # kompilace modulu
+#include <iostream>
+#include <unistd.h>
+#include <string>
+#include <cctype>
+#include <sstream>
+#include <cstdlib>
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netdb.h>
+#include <unistd.h>
+#include <arpa/inet.h>
+
+#include <pthread.h>
+
+#include <cstring>
+
+#include "socket.h"
+
+using namespace std;
+
+typedef struct params
+{
+  string h;
+  string p;
+  string l;
+  string u;
+  int L;
+  int U;
+  int G;
+  int N;
+  int H;
+  int S;
+  int counter;
+} struct_params;
+
+struct_params get_params(int, char**);
+
+int main(int argc, char** argv)
+{
+  cout << endl << endl;
+  
+  struct_params params = get_params(argc, argv);
+  
+  cout << params.u << params.l << endl;
+  cout << params.L << endl;
+  cout << params.U << endl;
+  cout << params.G << endl;
+  cout << params.N << endl;
+  cout << params.H << endl;
+  cout << params.S << endl;
+  
+  class_socket socket;
+  
+  cout << "Trying open socket on: " << params.h << " port: " << params.p << endl;
+  int tmp = socket.s_connect(params.h, params.p);
+  if ( tmp == 1)
+  {
+    fprintf(stderr, "Socket could not be opened\n");
+    exit(-1);
+  }
+  else if ( tmp == 2)
+  {
+    fprintf(stderr, "Failed to recover host name\n");
+    exit(-1);
+  }
+  
+  
+  
+  string request;
+  
+  for(int i=1;i<8;i++)
+  {
+    if(params.L == i)
+      request.append("1");
+    if(params.U == i)
+      request.append("2");
+    if(params.G == i)
+      request.append("3");
+    if(params.N == i)
+      request.append("4");
+    if(params.H == i)
+      request.append("5");
+    if(params.S == i)
+      request.append("6");
+  }
+  
+  for(int i=0;i<6-params.counter;i++)
+  {
+    request.append("0");
+  }
+  
+  if(params.u == "")
+    request.append("1"+params.l);
+  else
+    request.append("0"+params.u);
+  
+  
+  cout << "Sending: " << request << endl;
+  socket.s_write(request);
+  
+  cout << "Receiving!" << endl;
+  string text = socket.s_read();
+  
+  cout << text << endl;
+  
+  socket.s_disconnect();
+  return 0;
+}
+
+struct_params get_params(int argc, char** argv)
+{
+  struct_params par;
+  par.p = "";
+  par.u = "";
+  par.h = "";
+  par.l = "";
+  par.L = 0;
+  par.U = 0;
+  par.G = 0;
+  par.N = 0;
+  par.H = 0;
+  par.S = 0;
+  par.counter = 0;
+  
+  int c;
+  
+  while( (c = getopt(argc, (char**)argv, "p:u:h:l:LUGNHS")) != -1)
+  {
+    if(c == 'p' && optarg)
+    {
+      par.p = optarg;
+      
+      int pom;
+      istringstream stream(optarg);
+      stream >> pom;
+      
+      ostringstream o_stream;
+      o_stream << pom;
+      
+      if(o_stream.str().compare(par.p))
+      {
+        cerr << "Chyba: zadany port neni cislo!" << endl;
+        exit (-1);
+      }
+    }
+    else if(c == 'u' && optarg)
+    {
+      string s_pom;
+      s_pom = optarg;
+      
+      int pom;
+      istringstream stream(optarg);
+      stream >> pom;
+      
+      ostringstream o_stream;
+      o_stream << s_pom;
+      
+      if(o_stream.str().compare(s_pom))
+      {
+        cerr << "Chyba: zadane UID neni cislo!" << endl;
+        exit (-1);
+      }
+      
+      par.u.append(optarg);
+      
+      for( ;optind < argc && *argv[optind] != '-'; optind++)
+      {
+        par.u.append(" ");
+        
+        istringstream stream2(argv[optind]);
+        stream2 >> pom;
+        
+        ostringstream o_stream2;
+        o_stream2 << s_pom;
+        
+        if(o_stream2.str().compare(s_pom))
+        {
+          cerr << "Chyba: zadane UID neni cislo!" << endl;
+          exit (-1);
+        }
+        
+        par.u.append(argv[optind]);
+      }
+      
+    }
+    else if(c == 'h' && optarg)
+    {
+      par.h = optarg;
+    }
+    else if(c == 'l' && optarg)
+    {
+      par.l.append(optarg);
+      for( ;optind < argc && *argv[optind] != '-'; optind++)
+      {
+        par.l.append(" ");
+        par.l.append(argv[optind]);
+      }
+    }
+    else if(c == 'L')
+    {
+      par.counter++;
+      par.L = par.counter;
+    }
+    else if(c == 'U')
+    {
+      par.counter++;
+      par.U = par.counter;
+    }
+    else if(c == 'G')
+    {
+      par.counter++;
+      par.G = par.counter;
+    }
+    else if(c == 'N')
+    {
+      par.counter++;
+      par.N = par.counter;
+    }
+    else if(c == 'H')
+    {
+      par.counter++;
+      par.H = par.counter;
+    }
+    else if(c == 'S')
+    {
+      par.counter++;
+      par.S = par.counter;
+    }
+    else
+    {
+      cerr << "Chyba: neznamy parametr!" << endl;
+      exit(-1);
+    }
+  }
+  
+  if(par.l != "" && par.u != "")
+  {
+    cout << "UID and Login set!" << endl;
+    exit(1);
+  }
+  
+  if(par.p == "" || par.h == "" || (par.l == "" && par.u == ""))
+  {
+    cout << "client –h hostname –p port –l login ... –u uid ... –L –U –G –N –H –S" << endl;
+    exit(1);
+  }
+  
+  return par;
+}
+
 #include <iostream>
+#include <unistd.h>
+#include <string>
+#include <cctype>
+#include <sstream>
+#include <cstdlib>
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netdb.h>
+#include <unistd.h>
+#include <arpa/inet.h>
+
+#include <pthread.h>
+
+#include <cstring>
+#include <fstream>
+
+#include <vector>
+
+#include "socket.h"
+
+/*
+ * 0 0 0  0 0 0 0 tuxik pepa pavel
+ * L U G N H S ? DATA
+ */
 
 using namespace std;
 
-typedef struct params
+typedef struct struct_params
 {
-  
+  int p;
 } struct_params;
 
+struct_params get_params(int, char**);
+
 int main(int argc, char** argv)
 {
+  struct_params params = get_params(argc, argv);
+    
+  
+  
+  class_socket socket;
+  if(socket.s_listen(params.p) != 0)
+  {
+    cout << "Cannost listen!" << endl;
+    exit(-1);
+  }
+  
+  
+  
+  while(1) 
+  {
+    pid_t child_pid;
+    cout << "Server listening on port: " << htons(params.p) << endl;
+   
+    
+    sockaddr_in clientAddr;
+    socklen_t sin_size=sizeof(struct sockaddr_in);
+    class_socket client_sock;
+    client_sock.sock = accept(socket.sock, (struct sockaddr*)&clientAddr, &sin_size);
+    
+    cout << "Client connected! ID: " << client_sock.sock << endl;
+    
+    
+    
+    child_pid = fork();
+    
+    if (child_pid == 0) 
+    {
+      cout << "FORKED!" << endl;
+      
+      string received_string = client_sock.s_read();
+
+      cout << "Server received:  " << received_string << endl;
+      
+      string response_params;
+      bool uid = 0;
+      string response;
+      
+  
+      response_params = received_string.substr(0,6);
+      string data = received_string.substr(7,received_string.size()-7);
+      
+      vector<string> find;
+      unsigned r_start = 0;
+      unsigned r_end = data.size();
+      unsigned cmp = -1;
+      
+      cout << "START: " << r_start << " END: " << r_end << endl;
+      
+      data.append(" ");
+      while(r_start != cmp)
+      {
+        unsigned r_mezera = data.find(" ",r_start);
+        if (r_mezera != cmp)
+          find.push_back(data.substr(r_start, r_mezera - r_start));
+        else
+          break;
+        r_start = r_mezera+1;
+      }
+               
+      ifstream file;
+      file.open("/etc/passwd");
+      if(!file.good())
+      {
+        return 1; // exit if file not found
+      }    
+      
+      while (!file.eof())
+      {
+        bool parse = false;
+        string radek;
+        getline(file,radek);
+        
+        if(uid)
+        {
+          string s_uid;
+          unsigned uid_start = radek.find(":");
+          uid_start = radek.find(":",uid_start+1);
+          unsigned uid_end = radek.find(":",uid_start+1);
+          
+          for(int i=0;i<find.size();i++)
+          {
+            cout << "Comparing : " << s_uid << " with: " << find.at(i) << endl;
+            if(find.at(i) == s_uid)
+              parse = true;
+          }
+        }
+        else
+        {
+          string s_login;
+          unsigned login_start = 0;
+          unsigned login_end = radek.find(":",login_start);
+          
+          s_login = radek.substr(login_start,login_end-login_start); 
+          
+          for(int i=0;i<find.size();i++)
+          {
+            cout << "Comparing : " << s_login << " with: " << find.at(i) << endl;
+            if(find.at(i) == s_login)
+              parse = true;
+          }
+        }
+        
+        if(parse)
+        {
+          cout << "Parse: " << radek << endl;
+          // login_name:passwd:UID:GID:user_name:directory:shell
+          //     L             U   G    N         H      S    
+          //     1              2  3     4         5      6
+          
+          // LOGIN_NAME
+          unsigned parser_start = 0;
+          unsigned parser_end = radek.find(":");
+          unsigned data_end = radek.size();
+          vector<string> parsed; 
+          parsed.push_back(""); // 0
+          parsed.push_back(radek.substr(parser_start,parser_end-parser_start)); // 1
+          
+          // PASSWORD
+          parser_start = parser_end;
+          parser_end = radek.find(":",parser_start+1);
+          
+          // UID
+          parser_start = parser_end;
+          parser_end = radek.find(":",parser_start+1);
+          parsed.push_back(radek.substr(parser_start+1,parser_end-parser_start-1)); // 2
+          
+          // GID
+          parser_start = parser_end;
+          parser_end = radek.find(":",parser_start+1);
+          parsed.push_back(radek.substr(parser_start+1,parser_end-parser_start-1)); // 3
+          
+          // USER_NAME
+          parser_start = parser_end;
+          parser_end = radek.find(":",parser_start+1);
+          parsed.push_back(radek.substr(parser_start+1,parser_end-parser_start-1)); // 4
+          
+          // DIRECTORY
+          parser_start = parser_end;
+          parser_end = radek.find(":",parser_start+1);
+          parsed.push_back(radek.substr(parser_start+1,parser_end-parser_start-1)); // 5
+          
+          // SHELL
+          parser_start = parser_end;
+          parsed.push_back(radek.substr(parser_start+1,data_end-2)); // 6
+          
+          for(int i=0;i<6;i++)
+          {
+            int num = response_params.at(i);
+            num = num - '0';
+            response.append(parsed.at(num));
+            response.append(" ");
+          }
+        }        
+      }
+      
+      cout << "Sending!" << endl << response << endl;
+      client_sock.s_write(response);
+      
+      cout << "DONE" << endl;
+      
+      cout << endl << uid << endl;
+      
+      client_sock.s_disconnect();
+    }
+  }
   
   return 0;
 }
 
-
+struct_params get_params(int argc, char** argv)
+{
+  struct_params par;
+  par.p = -1;
+  
+  int c;
+  
+  while( (c = getopt(argc, (char**)argv, "p:?")) != -1)
+  {
+    if(c == 'p' && optarg)
+    {
+      istringstream stream(optarg);
+      stream >> par.p;
+      
+      ostringstream o_stream;
+      o_stream << par.p;
+      
+      if(o_stream.str().compare(optarg))
+      {
+        cerr << "Chyba: zadany port neni cislo!" << endl;
+        exit (-1);
+      }
+    }
+    else
+    {
+      cerr << "Chyba: neznamy parametr!" << endl;
+      exit(-1);
+    }
+  }
+  
+  if(par.p < 0)
+  {
+    cout << "server -p port" << endl;
+    exit(1);
+  }
+  
+  return par;
+}
+#include "socket.h"
+
+class_socket::class_socket()
+{
+  sock = socket(AF_INET, SOCK_STREAM, 0);
+  if(sock < 0) 
+  {
+    perror("ERROR: Cannot create socket!");
+    exit(-1);
+  }
+}
+
+int class_socket::s_connect(string host, string port)
+{
+    struct addrinfo hints, *res, *tmp;
+    int status;
+    
+    memset(&hints, 0, sizeof hints);
+    hints.ai_family = AF_INET;
+    hints.ai_socktype = SOCK_STREAM;
+
+    if ((status = getaddrinfo(host.c_str(), port.c_str(), &hints, &res)) != 0) 
+      return 2;
+
+    for(tmp = res;tmp != NULL; tmp = tmp->ai_next) 
+    {
+      if (connect(sock, tmp->ai_addr, tmp->ai_addrlen) < 0) 
+	continue;
+      else
+      {
+	freeaddrinfo(res); 
+	return 0;
+      }
+    } 
+    freeaddrinfo(res);
+    return 1;
+}
+
+int class_socket::s_disconnect()
+{
+    close(sock);
+    return 0;
+}
+
+int class_socket::s_write(string query)
+{
+  unsigned int sent = 0;
+  int tmp = 0;
+  while(sent < query.size())
+  {
+    tmp = send(sock, query.c_str()+sent, query.size()-sent, 0);
+    if(tmp == -1)
+      return -1;
+    sent = sent + tmp;
+  }
+  return 0;
+}
+
+string class_socket::s_read()
+{
+  char buf[BUFF_SIZE+1];
+  int tmpres = 0;
+  string text;
+    
+  while((tmpres = recv(sock, buf, BUFF_SIZE, 0)) > 0)
+  {
+    for (int i=0;i<tmpres;i++)
+    {
+      text.push_back(buf[i]);
+    }
+    memset(buf, 0, sizeof(buf));
+  }
+  
+  return text;
+}
+
+int class_socket::s_listen(int port)
+{
+  sock = socket(AF_INET, SOCK_STREAM, 0);
+
+  sockaddr_in serverAddr;
+  serverAddr.sin_family = AF_INET;
+  serverAddr.sin_port = htons(port);
+  serverAddr.sin_addr.s_addr = INADDR_ANY;
+
+  int ret = bind(sock, (struct sockaddr*)&serverAddr, sizeof(struct sockaddr));
+  if(ret != 0)
+    return ret;
+  
+  ret = listen(sock, 10);
+  
+  return ret;
+}
+#ifndef CLASS_SOCKET_H
+#define CLASS_SOCKET_H
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <netdb.h>
+#include <netinet/in.h>
+#include <sys/socket.h>
+#include <string>
+#include <iostream>
+#include <sstream>
+
+#define BUFF_SIZE 1024
+
+using namespace std;
+
+class class_socket
+{
+      private:
+	
+      public:
+	class_socket();
+	
+	int sock;
+	struct hostent *server;
+	int s_connect(string host,string port);
+	int s_disconnect();
+	string s_read();
+	int s_write(string);
+        int s_listen(int port);
+};
+ 
+#endif // CLASS_SOCKET_H
+