Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.73 KB | None | 0 0
  1. int init(int port) {
  2.     /**
  3.      * This function initialises the server
  4.      * It creates the initial sock, binds
  5.      * and then listens on the defined port
  6.      * Returns: Socket Descriptor (INT)
  7.      */
  8.     int httpd;
  9.      
  10.     struct sockaddr_in mySockAddr, remoteAddr;
  11.     socklen_t remoteAddr_len;
  12.  
  13.  
  14.     mySockAddr.sin_family = AF_INET;
  15.     mySockAddr.sin_port = htons(port);
  16.    
  17.     cout << "Initialising Server...";
  18.     logEvent(1, "[NOTICE] Initialising Server...", false);
  19.    
  20.     httpd = socket(AF_INET, SOCK_STREAM, 0);
  21.  
  22.     inet_aton("127.0.0.1", &mySockAddr.sin_addr);
  23.  
  24.     if(httpd == -1){
  25.         logEvent(1,("[ERROR] Creating Socket failed."));
  26.     }
  27.  
  28.     if(bind(httpd,(struct sockaddr*) &mySockAddr, sizeof(mySockAddr)) < 0) {
  29.         cout << errno << endl;
  30.        
  31.         logEvent(1,("[ERROR] Binding on port "+convertInt(port)+" failed."));
  32.     }
  33.  
  34.     if(listen(httpd, MAX_CLIENTS) == -1) {
  35.         logEvent(1,("[ERROR] listening on port "+convertInt(port)+" failed."));
  36.     } else {
  37.         cout << "\t[OK]" << endl;
  38.         logEvent(1, ("[NOTICE] Server started on port "+convertInt(port)), false);
  39.     }
  40.      return httpd;
  41. }
  42.  
  43.  
  44. int handle_connection(int ClientSocket) {
  45.     socklen_t len;
  46.     struct sockaddr_storage addr;
  47.     char ipstr[INET6_ADDRSTRLEN];
  48.     int port;
  49.     const char* buf;
  50.     const char* body;
  51.    
  52.     len = sizeof addr;
  53.     getpeername(ClientSocket, (struct sockaddr*)&addr, &len);
  54.  
  55.     // deal with both IPv4 and IPv6:
  56.     if (addr.ss_family == AF_INET) {
  57.         struct sockaddr_in *ClientSocket = (struct sockaddr_in *)&addr;
  58.         port = ntohs(ClientSocket->sin_port);
  59.         inet_ntop(AF_INET, &ClientSocket->sin_addr, ipstr, sizeof ipstr);
  60.     }
  61.     string IP;
  62.     IP.assign(ipstr);
  63.     logEvent(1, ("Connection Established with: "+IP));
  64.    
  65.    
  66.    
  67.     buf = "HTTP/1.0 200 OK\r\n";
  68.     send(ClientSocket, buf, strlen(buf), 0);
  69.    
  70.     buf = "Server: eoPanel Server\r\n";
  71.     send(ClientSocket, buf, strlen(buf), 0);
  72.    
  73.     buf = "Content-Type: text/html; charset=UTF-8\r\n";
  74.     send(ClientSocket, buf, strlen(buf), 0);
  75.    
  76.     buf = "Content-Length: 20\r\n";
  77.     send(ClientSocket, buf, strlen(buf), 0);
  78.    
  79.     buf = "\r\n";
  80.     send(ClientSocket, buf, strlen(buf), 0);
  81.     body = "<h2> Welcome </h2>";
  82.     send(ClientSocket, body, strlen(body), 0);
  83.    
  84.    
  85.     close(ClientSocket);
  86. }
  87.  
  88. string convertInt(int number){
  89.    stringstream ss;//create a stringstream
  90.    ss << number;//add number to the stream
  91.    return ss.str();//return a string with the contents of the stream
  92. }
  93.  
  94.  
  95.  
  96. int main(int argc, char* argv[]) {
  97.    
  98.     int port;
  99.     struct sockaddr_in remoteAddr;
  100.         socklen_t remoteAddr_len;
  101.    
  102.     if(argv[1] > 0) {
  103.         port = atoi(argv[1]);
  104.     } else {
  105.         port = DEFAULT_PORT;
  106.     }
  107.    
  108.     ServerSocket = init(port);
  109.    
  110.     if(ServerSocket == -1) {
  111.         logEvent(1,("[ERROR] Main did not receive a correct socketID from init();"));
  112.         exit(1);
  113.     }
  114.    
  115.     while(1) {
  116.         ClientSocket = accept(ServerSocket, (struct sockaddr*) &remoteAddr, &remoteAddr_len);
  117.    
  118.         handle_connection(ClientSocket);
  119.     }
  120.     return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement