예제 #1
0
int RestfulApi::check_login_form_submission(struct mg_connection *conn)
  {

    char name[100], password[100], ssid[100], expire[100], expire_epoch[100];

     mg_get_var(conn, "name", name, sizeof(name));
     mg_get_var(conn, "password", password, sizeof(password));

     //To do: routine that validate users (maybe passprhase?)
     if (AuthenticateUser(name,password)){
       // Generate expiry date
       time_t t = time(NULL) + 3600;  // Valid for 1 hour
       snprintf(expire_epoch, sizeof(expire_epoch), "%lu", (unsigned long) t);
       strftime(expire, sizeof(expire), "%a, %d %b %Y %H:%M:%S GMT", gmtime(&t));

       generate_ssid(name, expire_epoch, ssid, sizeof(ssid));
       // Set "session id" cookie, there could be some data encoded in it.
       mg_printf(conn,
                 "HTTP/1.1 302 Moved\r\n"
                 "Set-Cookie: ssid=%s; expire=\"%s\"; http-only; HttpOnly;\r\n"
                 "Location: /\r\n\r\n",
                 ssid, expire);
      fprintf(stderr,"\nAutenticated successfully\n");

      return MG_FALSE;
     }
      mg_printf(conn, "HTTP/1.1 302 Moved\r\nLocation: %s\r\n\r\n", s_error_uri);
     return MG_FALSE;
   }
void Phase_1()
{
        int iWelSock, iNewSock, bytes_recieved , true = 1;  
        char send_data [1024] , recv_data[1024];

        struct sockaddr_in addAucServer, addClient;    
        int sin_size;
        
        int iFlagAddInfo;
        struct addrinfo Hints, *Serverinfo;
        int iSockOpt = 1;
        
        int iAucPort = 1933;                        
        
        //Read Registration File and populate the Users with their data
        ReadRegistrationFile("Registration.txt");
        
        memset(&Hints, 0, sizeof(Hints));
        Hints.ai_family = AF_INET;
        Hints.ai_socktype = SOCK_STREAM;        
        
        if((iFlagAddInfo = getaddrinfo("localhost", "1933", &Hints, &Serverinfo)) != 0)
        {
            perror("getaddrinfo ");            
        }
        
        void *pAddr;
        struct sockaddr_in *pIP = (struct sockaddr_in *) Serverinfo->ai_addr;
        
        pAddr = &pIP->sin_addr;
        inet_ntop(Serverinfo->ai_family, pAddr, szAucServIP, sizeof(szAucServIP));                        
        
        //Create Welcoming Socket
        if ((iWelSock = socket(AF_INET, SOCK_STREAM, 0)) == -1) 
        {
            perror("Socket");
            exit(1);
        }             
        
        if (setsockopt(iWelSock, SOL_SOCKET,SO_REUSEADDR, &iSockOpt, sizeof(int)) == -1) 
        {
            perror("Setsockopt");
            exit(1);        
        }
        struct hostent *host;
        host = gethostbyname("localhost");
        
        // Configuring the Welcoming Socket
        memset(&addAucServer, 0 ,sizeof(addAucServer));
        addAucServer.sin_family = AF_INET;         
        addAucServer.sin_port = htons(iAucPort);     
        addAucServer.sin_addr = *((struct in_addr *)host->h_addr);                    

        if (bind(iWelSock, (struct sockaddr *)&addAucServer, sizeof(struct sockaddr)) == -1) 
        {
            perror("Unable to bind");
            exit(1);
        }

        if (listen(iWelSock, 10) == -1) 
        {
            perror("Listen");
            exit(1);
        }
		
        printf("\nPhase 1: Auction Server has TCP port number %d and IP address %s" , iAucPort, szAucServIP);
        fflush(stdout);
        int i = 0;
        
        for(i = 0;i<4;i++)
        {  
                sin_size = sizeof(struct sockaddr_in);

                iNewSock = accept(iWelSock, (struct sockaddr *)&addClient, &sin_size);
                
                //if(!fork())
                //{
                        //close(iWelSock);
                        AuthenticateUser(iNewSock, inet_ntoa(addClient.sin_addr));                        
                        close(iNewSock);
                //}
               // close(iNewSock);
                fflush(stdout);
        }
                
        close(iWelSock);
}