00001 00002 00003 00004 00005 00006 00007 // =========================================================== 00008 // 00009 // Copyright (C) 2005-2007, all rights reserved by 00010 // - Institute of Telematics, Universitaet Karlsruhe (TH) 00011 // 00012 // More information and contact: 00013 // https://projekte.tm.uka.de/trac/NSIS 00014 // 00015 // This program is free software; you can redistribute it and/or modify 00016 // it under the terms of the GNU General Public License as published by 00017 // the Free Software Foundation; version 2 of the License 00018 // 00019 // This program is distributed in the hope that it will be useful, 00020 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00021 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00022 // GNU General Public License for more details. 00023 // 00024 // You should have received a copy of the GNU General Public License along 00025 // with this program; if not, write to the Free Software Foundation, Inc., 00026 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 00027 // 00028 // =========================================================== 00029 #include "connectionmap_uds.h" 00030 #include "logfile.h" 00031 00032 namespace protlib { 00033 using namespace log; 00034 using boost::unordered_map; 00035 00042 /***** class ConnectionMapUDS *****/ 00043 00057 bool ConnectionMapUDS::insert(AssocDataUDS* assoc) 00058 { 00059 if (assoc) 00060 { 00061 if ( (assoc->socketfd && lookup(assoc->socketfd)) || 00062 (assoc->assoc && lookup(assoc->assoc)) || 00063 lookup(assoc->peer)) return false; 00064 else 00065 { 00066 if (assoc->socketfd) 00067 ass2data[assoc->socketfd] = assoc; 00068 else 00069 if (assoc->assoc) 00070 ass2data[assoc->assoc] = assoc; 00071 else 00072 Log(ERROR_LOG,LOG_NORMAL,"ConnectionMapUDS","insertion failed, both socketfd and associd are 0"); 00073 00074 addr2data[assoc->peer] = assoc; 00075 return true; 00076 } // end if already in map 00077 } else return false; 00078 } // end insert 00079 00084 AssocDataUDS* ConnectionMapUDS::lookup(socketfd_t socketfd) const 00085 { 00086 const_ass2data_it_t hit= ass2data.find(socketfd); 00087 if (hit!=ass2data.end()) return hit->second; 00088 else return NULL; 00089 } // end lookup 00090 00091 AssocDataUDS* ConnectionMapUDS::lookup(associd_t associd) const 00092 { 00093 const_ass2data_it_t hit= ass2data.find(associd); 00094 if (hit!=ass2data.end()) return hit->second; 00095 else return NULL; 00096 } // end lookup 00097 00102 AssocDataUDS* ConnectionMapUDS::lookup(const udsaddress& addr) const 00103 { 00104 const_addr2data_it_t hit= addr2data.find(addr); 00105 if (hit!=addr2data.end()) return hit->second; 00106 else return NULL; 00107 } // end lookup 00108 00113 bool ConnectionMapUDS::erase(socketfd_t socketfd) { 00114 bool res = true; 00115 AssocDataUDS* d = lookup(socketfd); 00116 if (d) { 00117 if (!ass2data.erase(d->socketfd)) res = false; 00118 if (!addr2data.erase(d->peer)) res = false; 00119 delete d; // AssocDataUDS is deleted 00120 return res; 00121 } else return false; 00122 } // end erase 00123 00129 bool ConnectionMapUDS::erase(associd_t associd) { 00130 bool res = true; 00131 AssocDataUDS* d = lookup(associd); 00132 if (d) { 00133 if (!ass2data.erase(d->assoc)) res = false; 00134 if (!addr2data.erase(d->peer)) res = false; 00135 delete d; // AssocDataUDS is deleted 00136 return res; 00137 } else return false; 00138 } // end erase 00139 00140 /* 00141 * 00142 */ 00143 void ConnectionMapUDS::clear() { 00144 const_ass2data_it_t hit; 00145 for (hit=ass2data.begin();hit!=ass2data.end();hit++) { 00146 if (hit->second) delete hit->second; 00147 } // end for hit 00148 ass2data.clear(); 00149 addr2data.clear(); 00150 } // end clear 00151 00152 00153 } // end namespace protlib