Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <sstream>
- #include <cstring>
- #include <cstdio> //Old functions on std namespace
- #include <cstdlib> //Old functions on std namespace
- #include <vector>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #include <pthread.h>
- #include <string.h>
- #include <unistd.h>
- #include <time.h>
- #include <sys/stat.h>
- #include <stdbool.h>
- //#include <mysql/mysql.h>
- //#include "funcs.hpp"
- //#include "db.hpp"
- //#include "def.hpp"
- /**************
- * POP Server
- **************/
- #define SERVER "Warhead" //Servername
- #define RFC1123FMT "%a, %d %b %Y %H:%M:%S GMT" //Date format of returns
- #define PORT "110" //Socket port
- #define HTTP_THREADS 40 //Number of HTTP Threads
- using namespace std;
- //Anonymous namespace
- int socketfd; //Stores the socket
- //database db; //Database Class
- char *trimwhitespace(char *str)
- {
- char *end;
- // Trim leading space
- while(isspace(*str)) str++;
- if(*str == 0) // All spaces?
- return str;
- // Trim trailing space
- end = str + strlen(str) - 1;
- while(end > str && isspace(*end)) end--;
- // Write new null terminator
- *(end+1) = 0;
- return str;
- }
- ssize_t send_text(int new_sd, char *text) {
- int len;
- len = strlen(text);
- return send(new_sd, text, len, 0);
- }
- void process(int new_sd, void *threadid) {
- int tid = (long) threadid;
- ssize_t bytes_recieved;
- char incomming_data_buffer[1000];
- while (1) {
- bytes_recieved = recv(new_sd, incomming_data_buffer, 1000, 0);
- // if (bytes_recieved == 0) cout << "host shut down." << endl ;
- if (bytes_recieved == -1)cout << "Recieve Error! :(" << endl ;
- incomming_data_buffer[bytes_recieved] = '\0';
- trimwhitespace(incomming_data_buffer);
- cout << "|" << incomming_data_buffer << "|" << endl;
- if (!strcmp(incomming_data_buffer, "QUIT")) {
- close(new_sd);
- break;
- } else {
- send_text(new_sd, "OK \n");
- }
- }
- }
- void *accept_conn(void *threadid) {
- while (1) {
- int new_sd;
- struct sockaddr_storage their_addr;
- socklen_t addr_size = sizeof(their_addr);
- new_sd = accept(socketfd, (struct sockaddr *)&their_addr, &addr_size);
- if (new_sd == -1)
- {
- cout << "Listen Error" << endl ;
- }
- else
- {
- //cout << "Connection accepted. Using new socketfd : " << new_sd << endl;
- process(new_sd, threadid);
- }
- }
- return 0;
- }
- int main()
- {
- int status;
- int thread_started = 0;
- struct addrinfo host_info;
- struct addrinfo *host_info_list;
- cout << "POP Daemon 0.1" << endl;
- cout << "Starting...\n" << endl;
- memset(&host_info, 0, sizeof host_info);
- //Set the structs
- host_info.ai_family = AF_INET;
- host_info.ai_addr = INADDR_ANY;
- host_info.ai_socktype = SOCK_STREAM;
- host_info.ai_flags = AI_PASSIVE;
- status = getaddrinfo(NULL, PORT, &host_info, &host_info_list);
- if (status != 0) cout << "getaddrinfo error" << gai_strerror(status) ;
- //Creating a socket...
- socketfd = socket(host_info_list->ai_family, host_info_list->ai_socktype,
- host_info_list->ai_protocol);
- if (socketfd == -1) {
- perror("Socket Error");
- exit(-1);
- }
- //Binding socket...
- int yes = 1;
- status = setsockopt(socketfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
- status = bind(socketfd, host_info_list->ai_addr, host_info_list->ai_addrlen);
- if (status == -1) {
- perror("Bind Error");
- exit(-1);
- }
- //db.preconnect();
- cout << "POP server listening on port " << PORT << endl;
- cout << "Listening for connections..." << endl;
- status = listen(socketfd, 5);
- if (status == -1) perror("Listen Error");
- pthread_t threads[HTTP_THREADS];
- int rc;
- int i;
- void *it;
- for( i=0; i < HTTP_THREADS; i++ ){
- //cout << "main(): creating thread, " << i << endl;
- thread_started++;
- memcpy(&it, &i, sizeof(i)); //Safe convert the thread number to void*
- rc = f(&threads[i], NULL, accept_conn, it);
- if (rc){
- cout << "Error: unable to create thread, " << rc << endl;
- exit(-1);
- }
- }
- cout << "main(): threads created, " << thread_started << endl;
- cout << "\n";
- pthread_exit(NULL);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement