|  | 1 | /// ----------------------------------------*- mode: C++; -*-- | 
|---|
|  | 2 | /// @file connectionmap_uds.cpp | 
|---|
|  | 3 | /// stores network connection related data | 
|---|
|  | 4 | /// ---------------------------------------------------------- | 
|---|
|  | 5 | /// $Id: connectionmap_uds.cpp 2872 2008-02-18 10:58:03Z bless $ | 
|---|
|  | 6 | /// $HeadURL: https://svn.ipv6.tm.uka.de/nsis/protlib/trunk/src/connectionmap_uds.cpp $ | 
|---|
|  | 7 | // =========================================================== | 
|---|
|  | 8 | // | 
|---|
|  | 9 | // Copyright (C) 2005-2007, all rights reserved by | 
|---|
|  | 10 | // - Institute of Telematics, Universitaet Karlsruhe (TH) | 
|---|
|  | 11 | // | 
|---|
|  | 12 | // More information and contact: | 
|---|
|  | 13 | // https://projekte.tm.uka.de/trac/NSIS | 
|---|
|  | 14 | // | 
|---|
|  | 15 | // This program is free software; you can redistribute it and/or modify | 
|---|
|  | 16 | // it under the terms of the GNU General Public License as published by | 
|---|
|  | 17 | // the Free Software Foundation; version 2 of the License | 
|---|
|  | 18 | // | 
|---|
|  | 19 | // This program is distributed in the hope that it will be useful, | 
|---|
|  | 20 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
|  | 21 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
|  | 22 | // GNU General Public License for more details. | 
|---|
|  | 23 | // | 
|---|
|  | 24 | // You should have received a copy of the GNU General Public License along | 
|---|
|  | 25 | // with this program; if not, write to the Free Software Foundation, Inc., | 
|---|
|  | 26 | // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | 
|---|
|  | 27 | // | 
|---|
|  | 28 | // =========================================================== | 
|---|
|  | 29 | #include "connectionmap_uds.h" | 
|---|
|  | 30 | #include "logfile.h" | 
|---|
|  | 31 |  | 
|---|
|  | 32 | namespace protlib { | 
|---|
|  | 33 | using namespace log; | 
|---|
|  | 34 |  | 
|---|
|  | 35 | /** @ingroup tptcp | 
|---|
|  | 36 | * @ingroup network | 
|---|
|  | 37 | * @{ | 
|---|
|  | 38 | */ | 
|---|
|  | 39 |  | 
|---|
|  | 40 |  | 
|---|
|  | 41 | /***** class ConnectionMapUDS *****/ | 
|---|
|  | 42 |  | 
|---|
|  | 43 | /** @class ConnectionMapUDS connectionmap.h | 
|---|
|  | 44 | The class ConnectionMapUDS saves all required information about | 
|---|
|  | 45 | currently existing connections. Its methods allow to check for | 
|---|
|  | 46 | connections and return, if possible, the data associated | 
|---|
|  | 47 | with the connection. | 
|---|
|  | 48 | */ | 
|---|
|  | 49 |  | 
|---|
|  | 50 |  | 
|---|
|  | 51 | /** @returns true if data is not NULL, if there is not already | 
|---|
|  | 52 | * an entry for data and if | 
|---|
|  | 53 | * it is inserted into the internal hash maps. | 
|---|
|  | 54 | * | 
|---|
|  | 55 | */ | 
|---|
|  | 56 | bool ConnectionMapUDS::insert(AssocDataUDS* assoc) | 
|---|
|  | 57 | { | 
|---|
|  | 58 | if (assoc) | 
|---|
|  | 59 | { | 
|---|
|  | 60 | if ( (assoc->socketfd && lookup(assoc->socketfd)) || | 
|---|
|  | 61 | (assoc->assoc && lookup(assoc->assoc)) || | 
|---|
|  | 62 | lookup(assoc->peer)) return false; | 
|---|
|  | 63 | else | 
|---|
|  | 64 | { | 
|---|
|  | 65 | if (assoc->socketfd) | 
|---|
|  | 66 | ass2data[assoc->socketfd] = assoc; | 
|---|
|  | 67 | else | 
|---|
|  | 68 | if (assoc->assoc) | 
|---|
|  | 69 | ass2data[assoc->assoc] = assoc; | 
|---|
|  | 70 | else | 
|---|
|  | 71 | Log(ERROR_LOG,LOG_NORMAL,"ConnectionMapUDS","insertion failed, both socketfd and associd are 0"); | 
|---|
|  | 72 |  | 
|---|
|  | 73 | addr2data[assoc->peer] = assoc; | 
|---|
|  | 74 | return true; | 
|---|
|  | 75 | } // end if already in map | 
|---|
|  | 76 | } else return false; | 
|---|
|  | 77 | } // end insert | 
|---|
|  | 78 |  | 
|---|
|  | 79 | /** @returns a pointer to the AssocDataUDS object or NULL. | 
|---|
|  | 80 | * @param socketfd socket file descriptor | 
|---|
|  | 81 | */ | 
|---|
|  | 82 |  | 
|---|
|  | 83 | AssocDataUDS* ConnectionMapUDS::lookup(socketfd_t socketfd) const | 
|---|
|  | 84 | { | 
|---|
|  | 85 | const_ass2data_it_t hit= ass2data.find(socketfd); | 
|---|
|  | 86 | if (hit!=ass2data.end()) return hit->second; | 
|---|
|  | 87 | else return NULL; | 
|---|
|  | 88 | } // end lookup | 
|---|
|  | 89 |  | 
|---|
|  | 90 | AssocDataUDS* ConnectionMapUDS::lookup(associd_t associd) const | 
|---|
|  | 91 | { | 
|---|
|  | 92 | const_ass2data_it_t hit= ass2data.find(associd); | 
|---|
|  | 93 | if (hit!=ass2data.end()) return hit->second; | 
|---|
|  | 94 | else return NULL; | 
|---|
|  | 95 | } // end lookup | 
|---|
|  | 96 |  | 
|---|
|  | 97 | /** @returns a pointer to the AssocDataUDS object or NULL. | 
|---|
|  | 98 | * @param addr IP-adress + port | 
|---|
|  | 99 | * | 
|---|
|  | 100 | */ | 
|---|
|  | 101 | AssocDataUDS* ConnectionMapUDS::lookup(const udsaddress& addr) const | 
|---|
|  | 102 | { | 
|---|
|  | 103 | const_addr2data_it_t hit= addr2data.find(addr); | 
|---|
|  | 104 | if (hit!=addr2data.end()) return hit->second; | 
|---|
|  | 105 | else return NULL; | 
|---|
|  | 106 | } // end lookup | 
|---|
|  | 107 |  | 
|---|
|  | 108 | /** @returns true if the AssocDataUDS-object with socketfd | 
|---|
|  | 109 | * could be deleted | 
|---|
|  | 110 | * @param socketfd socket file descriptor | 
|---|
|  | 111 | */ | 
|---|
|  | 112 | bool ConnectionMapUDS::erase(socketfd_t socketfd) { | 
|---|
|  | 113 | bool res = true; | 
|---|
|  | 114 | AssocDataUDS* d = lookup(socketfd); | 
|---|
|  | 115 | if (d) { | 
|---|
|  | 116 | if (!ass2data.erase(d->socketfd)) res = false; | 
|---|
|  | 117 | if (!addr2data.erase(d->peer)) res = false; | 
|---|
|  | 118 | delete d; // AssocDataUDS is deleted | 
|---|
|  | 119 | return res; | 
|---|
|  | 120 | } else return false; | 
|---|
|  | 121 | } // end erase | 
|---|
|  | 122 |  | 
|---|
|  | 123 | /** | 
|---|
|  | 124 | * erase an association | 
|---|
|  | 125 | * @param associd - association identifier | 
|---|
|  | 126 | * @returns true if the AssocDataUDS-object with associd could be deleted | 
|---|
|  | 127 | */ | 
|---|
|  | 128 | bool ConnectionMapUDS::erase(associd_t associd) { | 
|---|
|  | 129 | bool res = true; | 
|---|
|  | 130 | AssocDataUDS* d = lookup(associd); | 
|---|
|  | 131 | if (d) { | 
|---|
|  | 132 | if (!ass2data.erase(d->assoc)) res = false; | 
|---|
|  | 133 | if (!addr2data.erase(d->peer)) res = false; | 
|---|
|  | 134 | delete d; // AssocDataUDS is deleted | 
|---|
|  | 135 | return res; | 
|---|
|  | 136 | } else return false; | 
|---|
|  | 137 | } // end erase | 
|---|
|  | 138 |  | 
|---|
|  | 139 | /* | 
|---|
|  | 140 | * | 
|---|
|  | 141 | */ | 
|---|
|  | 142 | void ConnectionMapUDS::clear() { | 
|---|
|  | 143 | const_ass2data_it_t hit; | 
|---|
|  | 144 | for (hit=ass2data.begin();hit!=ass2data.end();hit++) { | 
|---|
|  | 145 | if (hit->second) delete hit->second; | 
|---|
|  | 146 | } // end for hit | 
|---|
|  | 147 | ass2data.clear(); | 
|---|
|  | 148 | addr2data.clear(); | 
|---|
|  | 149 | } // end clear | 
|---|
|  | 150 |  | 
|---|
|  | 151 |  | 
|---|
|  | 152 | } // end namespace protlib | 
|---|
|  | 153 | /// @} | 
|---|