Advertisement
Guest User

Mail Server

a guest
Jul 20th, 2013
1,686
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include <cstring>
  4. #include <cstdio>       //Old functions on std namespace
  5. #include <cstdlib>      //Old functions on std namespace
  6. #include <vector>
  7. #include <sys/socket.h>
  8. #include <netinet/in.h>
  9. #include <netdb.h>
  10. #include <pthread.h>
  11. #include <string.h>
  12. #include <unistd.h>
  13. #include <time.h>
  14. #include <sys/stat.h>
  15. #include <stdbool.h>
  16. //#include <mysql/mysql.h>
  17. //#include "funcs.hpp"
  18. //#include "db.hpp"
  19. //#include "def.hpp"
  20.  
  21. /**************
  22.  * POP Server
  23.  **************/
  24. #define SERVER          "Warhead"                 //Servername
  25. #define RFC1123FMT      "%a, %d %b %Y %H:%M:%S GMT"   //Date format of returns
  26. #define PORT            "110"                        //Socket port
  27. #define HTTP_THREADS    40                             //Number of HTTP Threads
  28.  
  29.  
  30. using namespace std;
  31.  
  32. //Anonymous namespace
  33. int socketfd;   //Stores the socket
  34. //database db;    //Database Class
  35.  
  36. char *trimwhitespace(char *str)
  37. {
  38.     char *end;
  39.    
  40.     // Trim leading space
  41.     while(isspace(*str)) str++;
  42.    
  43.     if(*str == 0)  // All spaces?
  44.         return str;
  45.    
  46.     // Trim trailing space
  47.     end = str + strlen(str) - 1;
  48.     while(end > str && isspace(*end)) end--;
  49.    
  50.     // Write new null terminator
  51.     *(end+1) = 0;
  52.    
  53.     return str;
  54. }
  55.  
  56. ssize_t send_text(int new_sd, char *text) {
  57.     int len;
  58.     len = strlen(text);
  59.    
  60.     return send(new_sd, text, len, 0);
  61. }
  62.  
  63.  
  64. void process(int new_sd, void *threadid) {
  65.     int tid = (long) threadid;
  66.    
  67.     ssize_t bytes_recieved;
  68.     char incomming_data_buffer[1000];
  69.    
  70.     while (1) {
  71.         bytes_recieved = recv(new_sd, incomming_data_buffer, 1000, 0);
  72.    
  73.         //    if (bytes_recieved == 0) cout << "host shut down." << endl ;
  74.         if (bytes_recieved == -1)cout << "Recieve Error! :(" << endl ;
  75.         incomming_data_buffer[bytes_recieved] = '\0';
  76.    
  77.         trimwhitespace(incomming_data_buffer);
  78.    
  79.         cout << "|" << incomming_data_buffer << "|" << endl;
  80.    
  81.         if (!strcmp(incomming_data_buffer, "QUIT")) {
  82.             close(new_sd);
  83.             break;
  84.         } else {
  85.             send_text(new_sd, "OK \n");
  86.         }
  87.     }
  88. }
  89.  
  90. void *accept_conn(void *threadid) {
  91.     while (1) {
  92.         int new_sd;
  93.         struct sockaddr_storage their_addr;
  94.         socklen_t addr_size = sizeof(their_addr);
  95.        
  96.         new_sd = accept(socketfd, (struct sockaddr *)&their_addr, &addr_size);
  97.         if (new_sd == -1)
  98.         {
  99.             cout << "Listen Error" << endl ;
  100.         }
  101.         else
  102.         {
  103.             //cout << "Connection accepted. Using new socketfd : "  <<  new_sd << endl;
  104.             process(new_sd, threadid);
  105.         }
  106.     }
  107.    
  108.     return 0;
  109. }
  110.  
  111. int main()
  112. {
  113.     int status;
  114.     int thread_started = 0;
  115.    
  116.     struct addrinfo host_info;
  117.     struct addrinfo *host_info_list;
  118.    
  119.     cout << "POP Daemon 0.1" << endl;
  120.     cout << "Starting...\n" << endl;
  121.    
  122.     memset(&host_info, 0, sizeof host_info);
  123.    
  124.     //Set the structs
  125.     host_info.ai_family   = AF_INET;
  126.     host_info.ai_addr     = INADDR_ANY;
  127.     host_info.ai_socktype = SOCK_STREAM;
  128.     host_info.ai_flags    = AI_PASSIVE;
  129.    
  130.     status = getaddrinfo(NULL, PORT, &host_info, &host_info_list);
  131.     if (status != 0)  cout << "getaddrinfo error" << gai_strerror(status) ;
  132.    
  133.    
  134.     //Creating a socket...
  135.     socketfd = socket(host_info_list->ai_family, host_info_list->ai_socktype,
  136.                       host_info_list->ai_protocol);
  137.     if (socketfd == -1) {
  138.         perror("Socket Error");
  139.         exit(-1);
  140.     }
  141.    
  142.     //Binding socket...
  143.     int yes = 1;
  144.     status = setsockopt(socketfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
  145.     status = bind(socketfd, host_info_list->ai_addr, host_info_list->ai_addrlen);
  146.     if (status == -1) {
  147.         perror("Bind Error");
  148.         exit(-1);
  149.     }
  150.    
  151.     //db.preconnect();
  152.    
  153.     cout << "POP server listening on port " << PORT << endl;
  154.     cout << "Listening for connections..."  << endl;
  155.     status =  listen(socketfd, 5);
  156.     if (status == -1)  perror("Listen Error");
  157.    
  158.     pthread_t threads[HTTP_THREADS];
  159.     int rc;
  160.     int i;
  161.     void *it;
  162.    
  163.     for( i=0; i < HTTP_THREADS; i++ ){
  164.         //cout << "main(): creating thread, " << i << endl;
  165.         thread_started++;
  166.        
  167.         memcpy(&it, &i, sizeof(i)); //Safe convert the thread number to void*
  168.         rc = f(&threads[i], NULL, accept_conn, it);
  169.        
  170.         if (rc){
  171.             cout << "Error: unable to create thread, " << rc << endl;
  172.             exit(-1);
  173.         }
  174.     }
  175.    
  176.     cout << "main(): threads created, " << thread_started << endl;
  177.     cout << "\n";
  178.    
  179.     pthread_exit(NULL);
  180.    
  181.     return 0;
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement