00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00037 #include <netdb.h>
00038 #include <pwd.h>
00039 #include <netinet/in.h>
00040 #include <cerrno>
00041
00042 #include "threadsafe_db.h"
00043 #include "cleanuphandler.h"
00044 #include "logfile.h"
00045
00046 namespace protlib {
00047
00052 using namespace log;
00053
00054 bool tsdb::is_init = false;
00055 bool tsdb::resolvenames = true;
00056 pthread_mutex_t tsdb::mutex =
00057 #ifdef _DEBUG
00058 PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
00059 #else
00060 PTHREAD_MUTEX_INITIALIZER;
00061 #endif
00062
00063
00064 uint32 tsdb::id32 = 1;
00065 uint64 tsdb::id64 = 1;
00066
00067 protocol_t tsdb::udp_id= 17;
00068 protocol_t tsdb::tcp_id= 6;
00069 protocol_t tsdb::sctp_id= 132;
00070
00071
00072 void
00073 tsdb::init(bool noresolving)
00074 {
00075 if (is_init)
00076 {
00077 Log(ERROR_LOG,LOG_NORMAL, "Threadsafe_DB", "Tried to initialize tstdb although already initialized.");
00078 } else
00079 {
00080 pthread_mutex_init(&mutex,NULL);
00081 is_init = true;
00082
00083
00084 udp_id= tsdb::getprotobyname("udp");
00085 tcp_id= tsdb::getprotobyname("tcp");
00086 sctp_id= tsdb::getprotobyname("sctp");
00087
00088 resolvenames=!noresolving;
00089 if (!resolvenames)
00090 Log(INFO_LOG,LOG_NORMAL,"Threadsafe_DB"," ** Disabled reverse name lookups - addresses will not be resolved to names **");
00091 }
00092 }
00093
00094 void tsdb::end() {
00095 if (is_init) {
00096 is_init = false;
00097 pthread_mutex_destroy(&mutex);
00098 } else {
00099 Log(ERROR_LOG,LOG_NORMAL, "Threadsafe_DB", "Tried to end tstdb although not initialized.");
00100 }
00101 }
00102
00103 uint32 tsdb::get_new_id32() {
00104 uint32 res = 0;
00105 if (is_init) {
00106 pthread_mutex_lock(&mutex);
00107 res = id32++;
00108 pthread_mutex_unlock(&mutex);
00109 } else {
00110 Log(ERROR_LOG,LOG_NORMAL, "Threadsafe_DB", "Tried to access tsdb although not initialized.");
00111 }
00112 return res;
00113 }
00114
00115 uint64 tsdb::get_new_id64() {
00116 uint64 res = 0;
00117 if (is_init) {
00118 pthread_mutex_lock(&mutex);
00119 res = id64++;
00120 pthread_mutex_unlock(&mutex);
00121 } else {
00122 Log(ERROR_LOG,LOG_NORMAL, "Threadsafe_DB", "Tried to access tsdb although not initialized.");
00123 }
00124 return res;
00125 }
00126
00127 string tsdb::getprotobynumber(protocol_t proto, bool *res) {
00128 string str;
00129 if (is_init)
00130 {
00131 pthread_mutex_lock(&mutex);
00132 struct protoent* entry = ::getprotobynumber(proto);
00133
00134 if (res) *res = (entry!=NULL);
00135 if (entry)
00136 str = entry->p_name;
00137 else
00138 str = "UNKNOWN";
00139
00140 pthread_mutex_unlock(&mutex);
00141 }
00142 else
00143 {
00144 Log(ERROR_LOG,LOG_NORMAL, "Threadsafe_DB", "Tried to access tsdb although not initialized.");
00145 if (res) *res = false;
00146 str = "";
00147 }
00148 return str;
00149 }
00150
00151 protocol_t tsdb::getprotobyname(const string &pname, bool *res) {
00152 return getprotobyname(pname.c_str(),res);
00153 }
00154
00155 protocol_t tsdb::getprotobyname(const char* pname, bool *res) {
00156 register protocol_t pnum;
00157 struct protoent* entry = NULL;
00158 if (is_init) {
00159 pthread_mutex_lock(&mutex);
00160 if (pname) entry = ::getprotobyname(pname);
00161 if (res) *res = (entry!=NULL);
00162 if (entry) pnum = entry->p_proto;
00163 else pnum = 0;
00164 pthread_mutex_unlock(&mutex);
00165 } else {
00166 Log(ERROR_LOG,LOG_NORMAL, "Threadsafe_DB", "Tried to access tsdb although not initialized.");
00167 if (res) *res = false;
00168 pnum = 0;
00169 }
00170 return pnum;
00171 }
00172
00173 string tsdb::get_username(uid_t uid, bool *res) {
00174 string str;
00175 if (is_init) {
00176 pthread_mutex_lock(&mutex);
00177 struct passwd* entry = ::getpwuid(uid);
00178 if (res) *res = (entry!=NULL);
00179 if (entry) str = entry->pw_name;
00180 else str = "UNKNOWN";
00181 pthread_mutex_unlock(&mutex);
00182 } else {
00183 Log(ERROR_LOG,LOG_NORMAL, "Threadsafe_DB", "Tried to access tsdb although not initialized.");
00184 if (res) *res = false;
00185 str = "";
00186 }
00187 return str;
00188 }
00189
00190 uid_t tsdb::get_userid(const char* uname, bool *res) {
00191 register uid_t uid;
00192 struct passwd* entry = NULL;
00193 if (is_init) {
00194 pthread_mutex_lock(&mutex);
00195 if (uname) entry = ::getpwnam(uname);
00196 if (res) *res = (entry!=NULL);
00197 if (entry) uid = entry->pw_uid;
00198 else uid = 0;
00199 pthread_mutex_unlock(&mutex);
00200 } else {
00201 Log(ERROR_LOG,LOG_NORMAL, "Threadsafe_DB", "Tried to access tsdb although not initialized.");
00202 if (res) *res = false;
00203 uid = 0;
00204 }
00205 return uid;
00206 }
00207
00208 uid_t tsdb::get_userid(const string& uname, bool *res) {
00209 return get_userid(uname.c_str(),res);
00210 }
00211
00212 string tsdb::get_portname(port_t port, protocol_t prot, bool *res) {
00213 string str;
00214 if (is_init) {
00215 bool tmpres = true;
00216 string pname = getprotobynumber(prot,&tmpres);
00217 if (tmpres) {
00218 pthread_mutex_lock(&mutex);
00219 struct servent* entry = ::getservbyport(htons(port),pname.c_str());
00220 if (res) *res = (entry!=NULL);
00221 if (entry) str = entry->s_name;
00222 else str = "UNKNOWN";
00223 pthread_mutex_unlock(&mutex);
00224 } else {
00225 if (res) *res = false;
00226 str = "UNKNOWN";
00227 }
00228 } else {
00229 Log(ERROR_LOG,LOG_NORMAL, "Threadsafe_DB", "Tried to access tsdb although not initialized.");
00230 if (res) *res = false;
00231 str = "";
00232 }
00233 return str;
00234 }
00235
00236 port_t tsdb::get_portnumber(const char* pname, protocol_t prot, bool *res) {
00237 register port_t pnum;
00238 struct servent* entry = NULL;
00239 if (is_init) {
00240 bool tmpres = true;
00241 string protoname = getprotobynumber(prot,&tmpres);
00242 if (tmpres) {
00243 pthread_mutex_lock(&mutex);
00244 if (pname) entry = ::getservbyname(pname,protoname.c_str());
00245 if (res) *res = (entry!=NULL);
00246 if (entry) pnum = ntohs(entry->s_port);
00247 else pnum = 0;
00248 pthread_mutex_unlock(&mutex);
00249 } else {
00250 if (res) *res = false;
00251 pnum = 0;
00252 }
00253 } else {
00254 Log(ERROR_LOG,LOG_NORMAL, "Threadsafe_DB", "Tried to access tsdb although not initialized.");
00255 if (res) *res = false;
00256 pnum = 0;
00257 }
00258 return pnum;
00259 }
00260
00261 port_t tsdb::get_portnumber(const string& pname, protocol_t prot, bool *res) {
00262 return get_portnumber(pname.c_str(),prot,res);
00263 }
00264
00265
00266 string
00267 tsdb::get_hostname(const struct sockaddr* sa, bool *res)
00268 {
00269 string str;
00270 static char tmpbuf[NI_MAXHOST];
00271
00272 if (is_init)
00273 {
00274 pthread_mutex_lock(&mutex);
00275 if (resolvenames)
00276 {
00277 int resultval= getnameinfo(sa,sizeof(struct sockaddr),
00278 tmpbuf,sizeof(tmpbuf),
00279 0,0,
00280 0);
00281
00282 if (res) *res = (resultval==0);
00283 if (resultval==0)
00284 {
00285 str= tmpbuf;
00286 }
00287 else
00288 {
00289 str = "UNKNOWN";
00290 if (resultval==EAI_AGAIN || errno==EAI_AGAIN)
00291 {
00292 Log(INFO_LOG,LOG_NORMAL, "Threadsafe_DB", "Temporary failure in name lookup. Try again later.");
00293 }
00294 else
00295 Log(INFO_LOG,LOG_NORMAL, "Threadsafe_DB", "Name lookup failed -" << strerror(errno));
00296
00297 if (res) *res= false;
00298 }
00299 }
00300 else
00301 {
00302 str= "disabled";
00303 if (res) *res= false;
00304 }
00305 pthread_mutex_unlock(&mutex);
00306 }
00307 else
00308 {
00309 Log(ERROR_LOG,LOG_NORMAL, "Threadsafe_DB", "Tried to access tsdb although not initialized.");
00310 if (res) *res = false;
00311 str = "";
00312 }
00313 return str;
00314 }
00315
00316
00317
00325 string
00326 tsdb::get_hostname(const in_addr& in, bool *res)
00327 {
00328 struct sockaddr_in sa={
00329 AF_INET,
00330 0,
00331 in
00332 };
00333 return get_hostname(reinterpret_cast<const sockaddr*>(&sa),res);
00334 }
00335
00343 string
00344 tsdb::get_hostname(const in6_addr& in, bool *res)
00345 {
00346 struct sockaddr_in6 sa={
00347 AF_INET6,
00348 0,
00349 0,
00350 in,
00351 0
00352 };
00353 return get_hostname(reinterpret_cast<const sockaddr*>(&sa),res);
00354 }
00355
00357
00358 }