35 #include <sys/socket.h>
36 #include <arpa/inet.h>
38 #include <netinet/in.h>
44 #include <sys/prctl.h>
53 #include <sys/types.h>
61 generic_server::generic_server(
void)
71 verify_client =
false;
73 max_log_size = 10*1000*1000;
77 log_file_mutex = CreateMutex( NULL, FALSE, NULL);
78 thread_cnt_mutex = CreateMutex( NULL, FALSE, NULL);
79 thread_sync_mutex = CreateMutex( NULL, FALSE, NULL);
81 pthread_mutex_init(&log_file_mutex,NULL);
82 pthread_mutex_init(&thread_cnt_mutex,NULL);
83 pthread_mutex_init(&thread_sync_mutex,NULL);
98 return(syslog_msg(debug_level,verbose_level,data));
99 unsigned int msk = 0xFFFFFFFF;
101 get_log_file_mutex();
102 debug_level = debug_level & ~msk;
103 if(verbose_level >= debug_level)
105 if(log_file.is_open())
106 log_file << get_cur_time() << data << endl;
108 cout << get_cur_time() << data << endl;
110 rel_log_file_mutex();
118 return(syslog_msg(debug_level,verbose_level,data));
119 unsigned int msk = 0xFFFFFFFF;
122 get_log_file_mutex();
123 debug_level = debug_level & ~msk;
124 if(verbose_level >= debug_level)
126 if(log_file.is_open())
127 log_file << get_cur_time() << data << endl;
129 cout << get_cur_time() << data << endl;
131 rel_log_file_mutex();
136 int generic_server::get_log_file_mutex(
void)
140 dwWaitResult = WaitForSingleObject(log_file_mutex,INFINITE);
142 pthread_mutex_lock(&log_file_mutex);
152 string file_name,fname2;
153 struct _stat stFileInfo;
154 size = get_log_size();
155 map<int,string> log_files;
156 map<int,string>::iterator itr;
158 if(size > max_log_size)
160 log(LOG_LOW,LOG_LOW,oss.str());
161 get_log_file_mutex();
162 if(log_file.is_open())
164 for(i = 0;i <= max_logs_saved; i++)
168 oss << logname <<
"." << i;
171 file_name = oss.str();
172 if(!_stat(file_name.c_str(),&stFileInfo))
173 log_files[i] = file_name;
175 if(log_files.size() == max_logs_saved+1)
177 itr = log_files.end();
179 log_files.erase(itr);
181 if(log_files.size() == 1)
183 fname2 = logname +
".1";
184 rename(log_files[0].c_str(),fname2.c_str());
188 for(i = log_files.size()-1; i >= 0;i--)
191 oss << logname <<
"." << (i+1);
193 rename(log_files[i].c_str(),fname2.c_str());
196 log_file.open(logname.c_str());
197 rel_log_file_mutex();
203 int generic_server::rel_log_file_mutex(
void)
206 ReleaseMutex(log_file_mutex);
208 pthread_mutex_unlock(&log_file_mutex);
214 int generic_server::set_conf_file(
char *cfile)
221 int generic_server::set_process_name(
char *pname)
223 process_name = pname;
228 int generic_server::get_thread_sync_mutex(
void)
232 dwWaitResult = WaitForSingleObject(thread_sync_mutex,INFINITE);
234 pthread_mutex_lock(&thread_sync_mutex);
240 int generic_server::rel_thread_sync_mutex(
void)
243 ReleaseMutex(thread_sync_mutex);
245 pthread_mutex_unlock(&thread_sync_mutex);
251 int generic_server::get_command_port(
void)
253 return(command_port);
257 bool generic_server::get_verify_client(
void)
259 return(verify_client);
263 int generic_server::check_allocated_port(
string in_plugin_id,
string in_plugin_port)
265 vector <GENERIC_PLUGIN *>::iterator it;
267 for(it = get_start_iterator(); it != get_end_iterator();it++)
269 if((*it)->get_plugin_name() == in_plugin_id && (*it)->get_port() == atoi(in_plugin_port.c_str()))
276 int generic_server::check_enabled_plugin(
string in_plugin_type)
279 vector <GENERIC_PLUGIN *>::iterator it;
281 for(cnt=0,it = get_start_iterator(); it != get_end_iterator();it++)
283 if((*it)->get_plugin() == in_plugin_type)
293 vector <GENERIC_PLUGIN *>::iterator it;
295 for(it = get_start_iterator(); it != get_end_iterator();it++)
297 if( (*it)->get_plugin_name() != plugin_parms.at(PLUGIN_NAME) &&
298 (*it)->get_plugin() == plugin_parms.at(PLUGIN_TYPE) &&
299 (*it)->get_plugin_path() == plugin_parms.at(SHARED_LIB_PATH) &&
300 (*it)->get_port() == atoi(plugin_parms.at(PORT).c_str()) &&
301 (*it)->get_conf_file() == plugin_parms.at(PLUGIN_CONF) &&
302 (*it)->get_tls_flag() == atoi(plugin_parms.at(TLS_FLAG).c_str())
305 if(!(*it)->find_plugin_alias(plugin_parms.at(PLUGIN_NAME)))
306 (*it)->add_plugin_alias(plugin_parms.at(PLUGIN_NAME));
310 if( (*it)->get_plugin_name() != plugin_parms.at(PLUGIN_NAME) &&
311 (*it)->get_plugin() == plugin_parms.at(PLUGIN_TYPE) &&
312 (*it)->get_port() == atoi(plugin_parms.at(PORT).c_str()) &&
313 (*it)->get_tls_flag() == atoi(plugin_parms.at(TLS_FLAG).c_str())
316 if( (*it)->get_conf_file() != plugin_parms.at(PLUGIN_CONF) ||
317 (*it)->get_plugin_path() != plugin_parms.at(SHARED_LIB_PATH) )
319 oss <<
" Ignoring attempted plugin alias for: " << (*it)->get_plugin_name() <<
". check alias configuration.";
320 log(LOG_LOW,LOG_LOW,oss.str());
331 vector <GENERIC_PLUGIN *>::iterator it;
332 map <string,int>::iterator ports_itr;
336 for(pos=0,it = get_start_iterator(); it != get_end_iterator();it++,pos++)
338 ports_itr = new_ports.find((*it)->get_plugin_name());
339 if(new_ports.empty() || ports_itr == new_ports.end())
341 if((*it)->aliases_count())
343 new_pluginid = (*it)->pop_alias();
344 (*it)->set_plugin_name(new_pluginid);
348 shutdown((*it)->get_socket(),2);
350 closesocket((*it)->get_socket());
352 close((*it)->get_socket());
354 if( (check_enabled_plugin((*it)->get_plugin())) == 1)
355 (*it)->server_shutdown();
356 unload_shared_lib((*it)->get_plugin());
357 v.erase(get_start_iterator()+pos);
359 it = get_start_iterator();
368 int generic_server::remove_all_aliases()
370 vector <GENERIC_PLUGIN *>::iterator it;
372 for(it = get_start_iterator(); it != get_end_iterator();it++)
373 (*it)->clear_aliases();
378 int generic_server::get_server_params(
string line,
int start_type)
382 if(line.size() > 9 && line.compare(0,9,
"LOG_FILE=") == 0)
384 logname = line.substr(9);
385 if(logname.compare(0,6,
"SYSLOG") != 0)
389 log_file.open(logname.c_str(),ios_base::app);
394 openlog(process_name.c_str(),LOG_NDELAY,LOG_LOCAL0);
399 log_file.open(logname.c_str(),ios_base::app);
404 el.Init(trim(process_name).c_str());
407 if(start_type == INIT)
409 os <<
" Starting GENERIC_SERVER as: " << trim(process_name) <<
" with conf file: " << trim(conf_file);
410 log(LOG_LOW,LOG_LOW,os.str());
414 if(line.size() > 13 && line.compare(0,13,
"COMMAND_PORT=") == 0)
416 command_port = atoi(line.substr(13).c_str());
419 if(line.size() > 19 && line.compare(0,19,
"MAX_LOG_SIZE_IN_MB=") == 0)
421 max_log_size = atoi(line.substr(19).c_str());
422 max_log_size = max_log_size*1000*1000;
425 if(line.size() > 15 && line.compare(0,15,
"MAX_LOGS_SAVED=") == 0)
427 max_logs_saved = atoi(line.substr(15).c_str());
430 if (line.size() > 16 && line.compare(0,16,
"TIMEOUT_SECONDS=") == 0)
432 timeout_seconds = atoi(line.substr(16).c_str());
435 if (line.size() > 12 && line.compare(0,12,
"MAX_THREADS=") == 0)
437 max_threads = atoi(line.substr(12).c_str());
440 if (line.size() > 19 && line.compare(0,19,
"SERVER_CERTIFICATE=") == 0)
442 server_cert = line.substr(19);
445 if (line.size() > 16 && line.compare(0,16,
"RSA_PRIVATE_KEY=") == 0)
447 rsa_private_key = line.substr(16);
450 if (line.size() > 15 && line.compare(0,15,
"CA_CERTIFICATE=") == 0)
452 ca_certificate = line.substr(15);
455 if (line.size() > 14 && line.compare(0,14,
"VERIFY_CLIENT=") == 0)
457 if(atoi(line.substr(14).c_str()))
458 verify_client =
true;
460 verify_client =
false;
463 if (line.size() > 13 && line.compare(0,13,
"PLUGINS_PATH=") == 0)
465 plugins_path = line.substr(13);
476 vector<string> plugin_parms;
477 string line,delim,library_image;
484 cout <<
"Can't init socket library! " << endl;
487 TCHAR filebuf[MAX_SZ];
488 get_exe_directory(filebuf,MAX_SZ);
489 SetCurrentDirectory(filebuf);
493 ifstream inf(conf_file.c_str());
497 cout <<
" Can't Open file: " << conf_file << endl;
500 while(getline(inf, line))
503 if(line.length() == 0 || line.at(0) ==
'#')
505 if(!get_server_params(line,INIT))
507 plugin_parms = split(line,delim,
true);
508 if(plugin_parms.size() != 7)
510 set_plugins_path(plugin_parms);
513 if(check_plugin_aliases(plugin_parms))
515 plugin = load_shared_lib(plugin_parms.at(SHARED_LIB_PATH),plugin_parms.at(PLUGIN_TYPE));
518 os <<
" Thr.ID:MAIN load_library failed: " << plugin_parms.at(SHARED_LIB_PATH) <<
" PLUGIN_TYPE: " << plugin_parms.at(PLUGIN_TYPE) << endl;
519 log(LOG_LOW,LOG_LOW,os.str());
522 plugin = initialize_plugin_object(plugin_parms,plugin);
528 if(!tls_initialize())
537 if(rsa_private_key !=
"" && server_cert !=
"" && ca_certificate !=
"")
541 os <<
" Thr.ID:MAIN TLS_init failed.";
542 log(LOG_LOW,LOG_LOW,os.str());
546 else if(tls_setup_reqd)
548 os <<
" Thr.ID:MAIN TLS not correctly setup in CONF file. Please check.";
549 log(LOG_LOW,LOG_LOW,os.str());
560 sslserver.set_cert_file(server_cert);
561 sslserver.set_priv_key(rsa_private_key);
562 sslserver.set_ca_cert(ca_certificate);
563 sslserver.set_verify_client(verify_client);
564 if(!sslserver.tls_init())
566 os <<
" Thr.ID:MAIN Unable to initiliaze TLS.";
567 log(LOG_LOW,LOG_LOW,(
char *)os.str().c_str());
570 if(!sslserver.CreateCTX())
572 os <<
" Thr.ID:MAIN Unable to set TLS context.";
573 log(LOG_LOW,LOG_LOW,(
char *)os.str().c_str());
576 if(!sslserver.LoadCerts())
578 os <<
" Thr.ID:MAIN Unable to load TLS certificates.";
579 log(LOG_LOW,LOG_LOW,(
char *)os.str().c_str());
582 os <<
" Thr.ID:MAIN successfully initialized TLS.";
583 log(LOG_LOW,LOG_LOW,(
char *)os.str().c_str());
588 int generic_server::unload_shared_lib(
string plugin_type)
591 FreeLibrary((HMODULE)dll_handles[plugin_type]);
593 dlclose(dll_handles[plugin_type]);
605 void* handle = dlopen(library_image.c_str(),RTLD_LAZY);
608 os <<
" Thr.ID:MAIN Can't load DLL: " <<
" " << dlerror();
609 log(LOG_LOW,LOG_LOW,os.str());
612 factory[plugin_type] = (
create_fp) dlsym(handle,
"create_instance");
613 dll_handles[plugin_type] = (
void *) handle;
616 handle = LoadLibrary(library_image.c_str());
621 os <<
" Thr.ID:MAIN Error loading: " << library_image <<
" " << GetLastError();
622 log(LOG_LOW,LOG_LOW,os.str());
625 factory[plugin_type] = (
create_fp) GetProcAddress(handle,
"create_instance");
626 dll_handles[plugin_type] = (
void *) handle;
628 if(factory[plugin_type] == NULL)
630 os <<
" Thr.ID:MAIN Can't instantiate PLUGIN_TYPE: " << plugin_type;
631 log(LOG_LOW,LOG_LOW,os.str());
634 plugin_p = (factory[plugin_type])();
635 os <<
" Thr.ID:MAIN loaded plugin version: " << plugin_p->get_plugin_version() <<
" for PLUGIN_TYPE: " << plugin_type;
636 log(LOG_LOW,LOG_LOW,os.str());
647 iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
655 int generic_server::socket_close(SOCKET sock)
659 iResult = shutdown(sock, 2);
665 if (iResult == SOCKET_ERROR)
676 int generic_server::plugin_read_conf_file(
GENERIC_PLUGIN *plugin,
int thread_no)
679 string line,delim,conf_file;
680 conf_file = plugin->plugin_conf_file;
683 ifstream inf(conf_file.c_str());
684 if(!inf.rdbuf()->is_open())
686 os <<
" Thr.ID:" << thread_no <<
" Plugin:" << plugin->get_plugin_name() <<
" 11 cannot read: " << conf_file;
687 log(LOG_LOW,LOG_LOW,os.str());
690 while(getline(inf, line))
693 if(line.length() == 0 || line.at(0) ==
'#')
703 string generic_server::trim(
const std::string& s)
707 size_t b = s.find_first_not_of(
" \t\r\n");
708 size_t e = s.find_last_not_of(
" \t\r\n");
711 return string(s, b, e - b + 1);
715 string generic_server::get_cur_time(
void)
723 localtime_r(&tim,&now);
725 localtime_s(&now,&tim);
727 os << setw(2) << setfill(
'0') << now.tm_mon+1 <<
"/" ;
728 os << setw(2) << setfill(
'0') << now.tm_mday <<
"/";
729 os << setw(2) << setfill(
'0') << now.tm_year+1900 <<
" ";
730 os << setw(2) << setfill(
'0') << now.tm_hour <<
":";
731 os << setw(2) << setfill(
'0') << now.tm_min <<
":";
732 os << setw(2) << setfill(
'0') << now.tm_sec;
737 int generic_server::get_thread_id()
740 int new_thread_id,thread_cnt;
743 dwWaitResult = WaitForSingleObject(thread_cnt_mutex,INFINITE);
745 pthread_mutex_lock(&thread_cnt_mutex);
747 map<int,THREAD_INFO>::iterator it;
749 for(thread_cnt = 0,it = thread_ids.begin(); it != thread_ids.end();it++,thread_cnt++)
752 if(th_info.state == THREAD_FREE)
755 th_info.state = THREAD_ALLOCATED;
756 thread_ids[thread_cnt] = th_info;
757 new_thread_id = (*it).first;
761 if(it == thread_ids.end())
763 th_info.state = THREAD_ALLOCATED;
764 thread_ids[thread_cnt] = th_info;
765 new_thread_id = thread_cnt;
768 ReleaseMutex(thread_cnt_mutex);
770 pthread_mutex_unlock(&thread_cnt_mutex);
772 return(new_thread_id);
776 int generic_server::rel_thread_id(
int thread_id)
781 dwWaitResult = WaitForSingleObject(thread_cnt_mutex,INFINITE);
783 pthread_mutex_lock(&thread_cnt_mutex);
786 if(thread_ids.find(thread_id) != thread_ids.end())
788 thread_ids[thread_id].state = THREAD_COMPLETED;
794 ReleaseMutex(thread_cnt_mutex);
796 pthread_mutex_unlock(&thread_cnt_mutex);
802 int generic_server::close_thread_ids(
void)
808 dwWaitResult = WaitForSingleObject(thread_cnt_mutex,INFINITE);
810 pthread_mutex_lock(&thread_cnt_mutex);
812 map<int,THREAD_INFO>::iterator it;
814 for(thread_cnt = 0,it = thread_ids.begin(); it != thread_ids.end();it++,thread_cnt++)
817 if(thread_ids[thread_cnt].state == THREAD_COMPLETED)
820 CloseHandle(th_info.th);
822 thread_ids[thread_cnt].state = THREAD_FREE;
826 ReleaseMutex(thread_cnt_mutex);
828 pthread_mutex_unlock(&thread_cnt_mutex);
834 vector <GENERIC_PLUGIN *>::iterator generic_server::get_start_iterator(
void)
836 vector <GENERIC_PLUGIN *>::iterator it = v.begin();
841 vector <GENERIC_PLUGIN *>::iterator generic_server::get_end_iterator(
void)
843 vector <GENERIC_PLUGIN *>::iterator it = v.end();
848 int generic_server::print_key_message()
850 vector <GENERIC_PLUGIN *>::iterator it;
854 log(LOG_LOW,LOG_LOW,(
char *)
" Thr.ID:MAIN thread. Doing PLUGIN_WALK: ");
855 for(it = get_start_iterator(); it != get_end_iterator();it++)
860 log(LOG_LOW,LOG_LOW,os.str());
864 os <<
" RSA: " << rsa_private_key;
865 os <<
" CERT: " << server_cert;
866 os <<
" CA-CERT: " << ca_certificate;
867 log(LOG_LOW,LOG_LOW,os.str());
870 os <<
" GENERIC_SERVER framework version: " << get_framework_version() <<
" using GEN_PLUGIN version: ";
871 os << tmp_plugin.generic_plugin_version() <<
" ready.";
872 log(LOG_LOW,LOG_LOW,os.str());
878 int generic_server::set_access_control(
char daemon_flag)
882 if(getuid() != 0 && !daemon_flag)
884 os <<
" Thr.ID:MAIN thread. Only 'root'' user can start generic_server..";
885 log(LOG_LOW,LOG_LOW,os.str());
886 cout <<
" Thr.ID:MAIN thread. Only 'root' user can start generic_server.." << endl;
896 unsigned char buff[25];
898 memcpy(key,buff+8,8);
899 memcpy(key+8,buff,8);
907 struct timeval timeout;
912 memset(sav_buffer,0,
sizeof(sav_buffer));
913 memset(str,0,MAX_SZ);
919 FD_SET(conn_s,&socks);
921 timeout.tv_usec = 50000;
922 readsock = select((
int)conn_s+1,&socks,NULL,NULL,&timeout);
923 if(bytes_recvd && readsock <= 0)
925 if(FD_ISSET(conn_s,&socks))
927 if ((t = recv(conn_s,str,
sizeof(str), 0)) > 0)
929 memcpy(sav_buffer+bytes_recvd,str,t);
931 if(bytes_recvd >= length)
946 struct timeval timeout;
952 memset(sav_buffer,0,
sizeof(sav_buffer));
953 memset(str,0,MAX_SZ);
958 FD_SET(conn_s,&socks);
959 timeout.tv_sec = secs;
961 readsock = select((
int)conn_s+1,&socks,NULL,NULL,&timeout);
964 if(FD_ISSET(conn_s,&socks))
966 if ((t = recv(conn_s,str,
sizeof(str), 0)) > 0)
968 memcpy(sav_buffer,str,t);
970 if(bytes_recvd >= length)
980 int generic_server::get_status(
unsigned char *buff)
982 return(((buff[0]-48)*10)+(buff[1]-48));
986 int generic_server::close_log(
void)
988 if(log_file.is_open())
994 int generic_server::get_log_size()
997 struct _stat fileStat;
999 struct stat fileStat;
1004 err = _stat(logname.c_str(),&fileStat);
1006 err = stat(logname.c_str(),&fileStat);
1010 return fileStat.st_size;
1014 vector<string> generic_server::split(
const string& s,
const string& delim,
const bool keep_empty =
true)
1016 vector<string> result;
1019 result.push_back(s);
1022 string::const_iterator substart = s.begin(), subend;
1025 subend = search(substart, s.end(), delim.begin(), delim.end());
1026 string temp(substart, subend);
1027 if (keep_empty || !temp.empty())
1028 result.push_back(temp);
1029 if (subend == s.end())
1031 substart = subend + delim.size();
1037 BOOL generic_server::get_exe_directory(TCHAR *obuf,
size_t osize)
1039 if(!GetModuleFileName(0, obuf, osize))
1047 TCHAR*lastslash = 0;
1048 for(; *obuf; obuf++)
1050 if (*obuf ==
'\\' || *obuf ==
'/')
1060 int generic_server::reload_conf_file(
void)
1063 vector <string> plugin_parms;
1064 map <string,int> new_conf_ports;
1065 string line,delim,library_image;
1067 vector <GENERIC_PLUGIN *>::iterator it;
1069 log(LOG_LOW,LOG_LOW,(
char *)
" Thr.ID:MAIN thread. SIGNAL received to re-read conf file..");
1071 ifstream inf(conf_file.c_str(),ifstream::in);
1075 remove_all_aliases();
1076 while(getline(inf, line))
1079 if(line.length() == 0 || line.at(0) ==
'#')
1081 if(!get_server_params(line,RELOAD))
1083 if(command_port == 0)
1085 os <<
" Thr.ID:MAIN thread. COMMAND_PORT not defined in CONF file." << endl;
1086 log(LOG_LOW,LOG_LOW,os.str());
1087 command_port = 10000;
1090 plugin_parms = split(line,delim,
true);
1091 set_plugins_path(plugin_parms);
1092 new_conf_ports[plugin_parms.at(PLUGIN_NAME)] = atoi(plugin_parms.at(PORT).c_str());
1093 if(check_allocated_port(plugin_parms.at(PLUGIN_NAME),plugin_parms.at(PORT)))
1097 if(check_plugin_aliases(plugin_parms))
1099 plugin = load_shared_lib(plugin_parms.at(SHARED_LIB_PATH),plugin_parms.at(PLUGIN_TYPE));
1104 os <<
" Thr.ID:MAIN thread. load_library failed for: " << plugin_parms.at(SHARED_LIB_PATH) << endl;
1105 log(LOG_LOW,LOG_LOW,os.str());
1108 plugin = initialize_plugin_object(plugin_parms,plugin);
1111 v.push_back(plugin);
1114 remove_plugins(new_conf_ports);
1115 print_key_message();
1120 int generic_server::set_plugins_path(vector<string> &plugin_parms)
1122 if(plugins_path !=
"")
1124 plugin_parms.at(SHARED_LIB_PATH) = plugins_path +
"//" + plugin_parms.at(SHARED_LIB_PATH);
1125 if(plugin_parms.size() > PLUGIN_CONF)
1126 plugin_parms.at(PLUGIN_CONF) = plugins_path +
"//" + plugin_parms.at(PLUGIN_CONF);
1132 string generic_server::get_plugins_path()
1134 return(plugins_path);
1142 SOCKET ListenSocket;
1143 string bootstrap_name;;
1145 plugin->set_port(atoi(plugin_parms.at(PORT).c_str()));
1146 plugin->set_plugin(plugin_parms.at(PLUGIN_TYPE));
1147 plugin->set_plugin_name(plugin_parms.at(PLUGIN_NAME));
1148 plugin->set_plugin_number(plugin_parms.at(PLUGIN_NUMBER));
1149 plugin->set_plugin_path(plugin_parms.at(SHARED_LIB_PATH));
1150 plugin->set_tls_flag(atoi(plugin_parms.at(TLS_FLAG).c_str()));
1151 memcpy(&plugin->
pinstance,&pinstance,4);
1154 plugin->set_socket(ListenSocket);
1155 if(plugin->get_tls_flag())
1157 if(plugin_parms.size() > PLUGIN_CONF)
1159 plugin->set_conf_file(plugin_parms.at(PLUGIN_CONF));
1160 ret = plugin_read_conf_file(plugin,0);
1163 os <<
" Thr.ID:MAIN plugin_init failed for: " << plugin->get_plugin_name() << endl;
1164 log(LOG_LOW,LOG_LOW,os.str());
1169 map<string,bool>::iterator it = bootstrap_jobs.find(bootstrap_name);
1170 if(it == bootstrap_jobs.end() && bootstrap_name !=
"")
1172 os <<
" Thr.ID:MAIN Invoking bootstrap: " << bootstrap_name <<
" on plug-in: " << plugin->get_plugin_name();
1173 log(LOG_LOW,LOG_LOW,os.str());
1178 os <<
" Thr.ID:MAIN failed to bootstrap: " << bootstrap_name <<
" on plug-in: " << plugin->get_plugin_name();
1179 log(LOG_LOW,LOG_LOW,os.str());
1182 bootstrap_jobs[bootstrap_name] =
true;
1183 os <<
" Thr.ID:MAIN Successfully bootstrapped: " << bootstrap_name <<
" on plug-in: " << plugin->get_plugin_name();
1184 log(LOG_LOW,LOG_LOW,os.str());
1188 if(!check_enabled_plugin(plugin_parms.at(PLUGIN_TYPE)))
1192 os <<
" Thr.ID:MAIN thread. server_init failed for: " << plugin_parms.at(PLUGIN_TYPE) << endl;
1193 log(LOG_LOW,LOG_LOW,os.str());
1197 setnonblocking(ListenSocket);
1202 int generic_server::setnonblocking(SOCKET sock)
1207 iResult = ioctlsocket(sock, FIONBIO, &iMode);
1208 if (iResult != NO_ERROR)
1212 int opts = fcntl(sock,F_GETFL);
1215 opts = (opts | O_NONBLOCK);
1216 if (fcntl(sock,F_SETFL,opts) < 0)
1223 int generic_server::setblocking(SOCKET sock)
1228 iResult = ioctlsocket(sock, FIONBIO, &iMode);
1229 if (iResult != NO_ERROR)
1233 int opts = fcntl(sock,F_GETFL);
1236 opts = (opts | ~O_NONBLOCK);
1237 if (fcntl(sock,F_SETFL,opts) < 0)
1246 SOCKET ListenSocket;
1247 struct addrinfo *result = NULL;
1248 struct sockaddr_in local_address;
1249 struct addrinfo hints;
1254 memset(&local_address,0,
sizeof(local_address));
1255 memset(&hints, 0,
sizeof(hints));
1256 hints.ai_family = AF_INET;
1257 hints.ai_socktype = SOCK_STREAM;
1258 hints.ai_protocol = IPPROTO_TCP;
1259 hints.ai_flags = AI_PASSIVE;
1260 iResult = getaddrinfo(NULL, os.str().c_str(), &hints, &result);
1265 os <<
" Thr.ID:MAIN getaddrinfo failed.";
1266 log(LOG_LOW,LOG_LOW,os.str());
1268 ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
1269 if (ListenSocket == INVALID_SOCKET)
1273 os <<
" Thr.ID:MAIN socket creation failed.";
1274 log(LOG_LOW,LOG_LOW,os.str());
1275 freeaddrinfo(result);
1278 local_address.sin_family = AF_INET;
1279 local_address.sin_addr.s_addr = inet_addr(
"127.0.0.1");
1280 local_address.sin_port = htons(port);
1281 iResult = bind(ListenSocket, (sockaddr *) &local_address, (
int)result->ai_addrlen);
1286 os <<
" Thr.ID:MAIN socket bind failed for port: " << port;
1287 log(LOG_LOW,LOG_LOW,os.str());
1288 freeaddrinfo(result);
1289 shutdown(ListenSocket,2);
1291 closesocket(ListenSocket);
1293 close(ListenSocket);
1297 iResult = listen(ListenSocket, SOMAXCONN);
1298 if (iResult == SOCKET_ERROR)
1302 os <<
" Thr.ID:MAIN socket listen failed for port: " << port;
1303 log(LOG_LOW,LOG_LOW,os.str());
1304 shutdown(ListenSocket,2);
1306 closesocket(ListenSocket);
1308 close(ListenSocket);
1312 setnonblocking(ListenSocket);
1313 return(ListenSocket);
1317 int generic_server::syslog_msg(
unsigned int debug_level,
unsigned int verbose_level,
string data)
1319 unsigned int msk = 0xFFFFFFFF;
1321 debug_level = debug_level & ~msk;
1322 if (verbose_level >= debug_level)
1328 syslog(LOG_INFO,
"%s",trim(data).c_str());
1331 syslog(LOG_NOTICE,
"%s",trim(data).c_str());
1334 syslog(LOG_WARNING,
"%s",trim(data).c_str());
1338 el.Write(EVENTLOG_INFORMATION_TYPE, trim(data).c_str());
1341 el.Write(EVENTLOG_INFORMATION_TYPE, trim(data).c_str());
1344 el.Write(EVENTLOG_WARNING_TYPE, trim(data).c_str());
1354 int generic_server::ssl_async_write(SSL *ssl,
char *buf,
int size)
1356 signed int status,status1,ssl_response;
1359 while(ssl_response == 1)
1363 status = SSL_write((SSL *)ssl,buf,size);
1366 status1 = SSL_get_error((SSL *)ssl,status);
1369 case SSL_ERROR_WANT_READ:
1370 oss <<
" AWRITE: ERROR: SSL_ERROR_WANT_READ ";
1373 case SSL_ERROR_WANT_WRITE:
1374 oss <<
" AWRITE: ERROR: SSL_ERROR_WANT_WRITE ";
1377 case SSL_ERROR_WANT_CONNECT:
1378 oss <<
" AWRITE: ERROR: SSL_ERROR_WANT_CONNECT ";
1381 case SSL_ERROR_WANT_ACCEPT:
1382 oss <<
" AWRITE: ERROR: SSL_ERROR_WANT_ACCEPT ";
1385 case SSL_ERROR_SYSCALL:
1386 oss <<
" AWRITE: ERROR: SSL_ERROR_SYSCALL ";
1390 case SSL_ERROR_NONE:
1391 oss <<
" AWRITE: ERROR: SSL_ERROR_NONE ";
1396 oss <<
" AWRITE: ERROR: SSL_ERROR_SSL ";
1400 case SSL_ERROR_ZERO_RETURN:
1401 oss <<
" AWRITE ERROR: SSL_ERROR_ZERO_RETURN ";
1406 if(ssl_response < 0)
1409 return(ssl_response);
1411 if(ssl_response == 0)
1420 int generic_server::ssl_async_read(SSL *ssl,
char *buf,
int size)
1422 signed int status,status1,ssl_response;
1425 while(ssl_response == 1)
1429 status = SSL_read((SSL *)ssl,buf,size);
1432 status1 = SSL_get_error((SSL *)ssl,status);
1435 case SSL_ERROR_WANT_READ:
1436 oss <<
" AREAD: ERROR: SSL_ERROR_WANT_READ ";
1439 case SSL_ERROR_WANT_WRITE:
1440 oss <<
" AREAD: ERROR: SSL_ERROR_WANT_WRITE ";
1443 case SSL_ERROR_WANT_CONNECT:
1444 oss <<
" AREAD: ERROR: SSL_ERROR_WANT_CONNECT ";
1447 case SSL_ERROR_WANT_ACCEPT:
1448 oss <<
" AREAD: ERROR: SSL_ERROR_WANT_ACCEPT ";
1451 case SSL_ERROR_SYSCALL:
1452 oss <<
" AREAD: ERROR: SSL_ERROR_SYSCALL ";
1456 case SSL_ERROR_NONE:
1457 oss <<
" AREAD: ERROR: SSL_ERROR_NONE ";
1462 oss <<
" AREAD: ERROR: SSL_ERROR_SSL ";
1466 case SSL_ERROR_ZERO_RETURN:
1467 oss <<
" AREAD: ERROR: SSL_ERROR_ZERO_RETURN ";
1472 if(ssl_response < 0)
1475 return(ssl_response);
1477 if(ssl_response == 0)
1486 int generic_server::ssl_async_accept(SSL *ssl)
1488 signed int status,status1,ssl_response;
1491 while(ssl_response == 1)
1495 status = SSL_accept((SSL *)ssl);
1498 status1 = SSL_get_error((SSL *)ssl,status);
1501 case SSL_ERROR_WANT_READ:
1502 oss <<
" ACCEPT: ERROR: SSL_ERROR_WANT_READ ";
1505 case SSL_ERROR_WANT_WRITE:
1506 oss <<
" ACCEPT: ERROR: SSL_ERROR_WANT_WRITE ";
1509 case SSL_ERROR_WANT_X509_LOOKUP:
1510 oss <<
" ACCEPT: ERROR: SSL_ERROR_WANT_CONNECT ";
1513 case SSL_ERROR_SYSCALL:
1514 oss <<
" ACCEPT: ERROR: SSL_ERROR_SYSCALL ";
1518 case SSL_ERROR_NONE:
1519 oss <<
" ACCEPT: ERROR: SSL_ERROR_NONE ";
1524 oss <<
" ACCEPT: ERROR: SSL_ERROR_SSL ";
1528 case SSL_ERROR_ZERO_RETURN:
1529 oss <<
" ACCEPT: ERROR: SSL_ERROR_ZERO_RETURN ";
1534 if(ssl_response <= 0)
1536 log(LOG_LOW,LOG_LOW,oss.str());
GENERIC_PLUGIN * load_shared_lib(string library_image, string)
virtual string bootstrap_name(void)
int log(unsigned int, unsigned int, char *)
enables logging to framework managed log file.
static generic_server * pinstance
Pointer to singleton framework instance.
Singleton class to manage framework state and provide utility functions. This class stores master Vec...
int rollover_logs(void)
Rolls over log files, configured in Master configuation file.
bool check_plugin_aliases(vector< string > &alias)
This is a singleton class and provides framework functionality.
virtual bool bootstrap_init(string)
This component provides functionality that are common across plug-ins. Framework would instantiate an...
OS agnostic thread handle.
int sock_init(void)
Really useful only for Windows, just invokes WSAStartup API.
int swap_bytes(unsigned char *key)
A simple utility to swap upper / lower 8 bytes of a 16 bytes buffer.
struct thread_info THREAD_INFO
OS agnostic thread handle.
GENERIC_PLUGIN *(* create_fp)()
Every plug-in should have a function with following prototype. This is the factory method: 'create_in...
int tls_initialize()
Validates generic_server framework and CA certificates, calls tls_setup().
This is the base class for all plugins. All plug-ins should derive from this class. This class has a bunch of virtual functions that all plug-in could/should implement. In addition to virtual functions, this class also provides a lot of utility functions for all plug-ins.
int remove_plugins(map< string, int > &new_ports)
This is invoked when any plug-in entry gets removed from Master configuration file and framework gets...
GENERIC_PLUGIN * initialize_plugin_object(vector< string > plugin_parms, GENERIC_PLUGIN *plugin)
SOCKET create_socket(int)
int bb_getline(char *sav_buffer, size_t length, size_t conn_s)
SOCKET initialize_socket(string port)
Binds socket to TCP port, set options on new socket and do socket listen.
unsigned long pinstance
Pointer to 'framework' object.
int tls_setup(void)
Initializes TLS.
virtual int server_init(void)
Utility class for TLS. Please refer excellent documentation at: http://www.openssl.org/docs/ssl/ssl.html.
static generic_server * instance(void)
Instantiate tis singleton class only once and return pointer to instantiated object later on...
virtual int get_plugin_params(string line)