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.h" 00030 #include "logfile.h" 00031 00032 namespace protlib { 00033 using namespace log; 00034 00041 /***** class ConnectionMap *****/ 00042 00056 bool ConnectionMap::insert(AssocData* assoc) 00057 { 00058 if (assoc) 00059 { 00060 if ( (assoc->socketfd && lookup(assoc->socketfd)) || 00061 (assoc->assoc && lookup(assoc->assoc)) || 00062 lookup(assoc->peer)) return false; 00063 else 00064 { 00065 if (assoc->socketfd) 00066 ass2data[assoc->socketfd] = assoc; 00067 else 00068 if (assoc->assoc) 00069 ass2data[assoc->assoc] = assoc; 00070 else 00071 Log(ERROR_LOG,LOG_NORMAL,"ConnectionMap","insertion failed, both socketfd and associd are 0"); 00072 00073 addr2data[assoc->peer] = assoc; 00074 return true; 00075 } // end if already in map 00076 } else return false; 00077 } // end insert 00078 00083 AssocData* ConnectionMap::lookup(socketfd_t socketfd) const 00084 { 00085 const_ass2data_it_t hit= ass2data.find(socketfd); 00086 if (hit!=ass2data.end()) return hit->second; 00087 else return NULL; 00088 } // end lookup 00089 00090 AssocData* ConnectionMap::lookup(associd_t associd) const 00091 { 00092 const_ass2data_it_t hit= ass2data.find(associd); 00093 if (hit!=ass2data.end()) return hit->second; 00094 else return NULL; 00095 } // end lookup 00096 00101 AssocData* ConnectionMap::lookup(const appladdress& addr) const 00102 { 00103 const_addr2data_it_t hit= addr2data.find(addr); 00104 if (hit!=addr2data.end()) return hit->second; 00105 else return NULL; 00106 } // end lookup 00107 00112 bool ConnectionMap::erase(socketfd_t socketfd) { 00113 bool res = true; 00114 AssocData* d = lookup(socketfd); 00115 if (d) { 00116 if (!ass2data.erase(d->socketfd)) res = false; 00117 if (!addr2data.erase(d->peer)) res = false; 00118 delete d; // AssocData is deleted 00119 return res; 00120 } else return false; 00121 } // end erase 00122 00128 bool ConnectionMap::erase(associd_t associd) { 00129 bool res = true; 00130 AssocData* d = lookup(associd); 00131 if (d) { 00132 if (!ass2data.erase(d->assoc)) res = false; 00133 if (!addr2data.erase(d->peer)) res = false; 00134 delete d; // AssocData is deleted 00135 return res; 00136 } else return false; 00137 } // end erase 00138 00139 /* 00140 * 00141 */ 00142 void ConnectionMap::clear() { 00143 const_ass2data_it_t hit; 00144 for (hit=ass2data.begin();hit!=ass2data.end();hit++) { 00145 if (hit->second) delete hit->second; 00146 } // end for hit 00147 ass2data.clear(); 00148 addr2data.clear(); 00149 } // end clear 00150 00151 00152 } // end namespace protlib