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 | using boost::unordered_map; |
---|
35 | |
---|
36 | /** @ingroup tptcp |
---|
37 | * @ingroup network |
---|
38 | * @{ |
---|
39 | */ |
---|
40 | |
---|
41 | |
---|
42 | /***** class ConnectionMapUDS *****/ |
---|
43 | |
---|
44 | /** @class ConnectionMapUDS connectionmap.h |
---|
45 | The class ConnectionMapUDS saves all required information about |
---|
46 | currently existing connections. Its methods allow to check for |
---|
47 | connections and return, if possible, the data associated |
---|
48 | with the connection. |
---|
49 | */ |
---|
50 | |
---|
51 | |
---|
52 | /** @returns true if data is not NULL, if there is not already |
---|
53 | * an entry for data and if |
---|
54 | * it is inserted into the internal hash maps. |
---|
55 | * |
---|
56 | */ |
---|
57 | bool ConnectionMapUDS::insert(AssocDataUDS* assoc) |
---|
58 | { |
---|
59 | if (assoc) |
---|
60 | { |
---|
61 | if ( (assoc->socketfd && lookup(assoc->socketfd)) || |
---|
62 | (assoc->assoc && lookup(assoc->assoc)) || |
---|
63 | lookup(assoc->peer)) return false; |
---|
64 | else |
---|
65 | { |
---|
66 | if (assoc->socketfd) |
---|
67 | ass2data[assoc->socketfd] = assoc; |
---|
68 | else |
---|
69 | if (assoc->assoc) |
---|
70 | ass2data[assoc->assoc] = assoc; |
---|
71 | else |
---|
72 | Log(ERROR_LOG,LOG_NORMAL,"ConnectionMapUDS","insertion failed, both socketfd and associd are 0"); |
---|
73 | |
---|
74 | addr2data[assoc->peer] = assoc; |
---|
75 | return true; |
---|
76 | } // end if already in map |
---|
77 | } else return false; |
---|
78 | } // end insert |
---|
79 | |
---|
80 | /** @returns a pointer to the AssocDataUDS object or NULL. |
---|
81 | * @param socketfd socket file descriptor |
---|
82 | */ |
---|
83 | |
---|
84 | AssocDataUDS* ConnectionMapUDS::lookup(socketfd_t socketfd) const |
---|
85 | { |
---|
86 | const_ass2data_it_t hit= ass2data.find(socketfd); |
---|
87 | if (hit!=ass2data.end()) return hit->second; |
---|
88 | else return NULL; |
---|
89 | } // end lookup |
---|
90 | |
---|
91 | AssocDataUDS* ConnectionMapUDS::lookup(associd_t associd) const |
---|
92 | { |
---|
93 | const_ass2data_it_t hit= ass2data.find(associd); |
---|
94 | if (hit!=ass2data.end()) return hit->second; |
---|
95 | else return NULL; |
---|
96 | } // end lookup |
---|
97 | |
---|
98 | /** @returns a pointer to the AssocDataUDS object or NULL. |
---|
99 | * @param addr IP-adress + port |
---|
100 | * |
---|
101 | */ |
---|
102 | AssocDataUDS* ConnectionMapUDS::lookup(const udsaddress& addr) const |
---|
103 | { |
---|
104 | const_addr2data_it_t hit= addr2data.find(addr); |
---|
105 | if (hit!=addr2data.end()) return hit->second; |
---|
106 | else return NULL; |
---|
107 | } // end lookup |
---|
108 | |
---|
109 | /** @returns true if the AssocDataUDS-object with socketfd |
---|
110 | * could be deleted |
---|
111 | * @param socketfd socket file descriptor |
---|
112 | */ |
---|
113 | bool ConnectionMapUDS::erase(socketfd_t socketfd) { |
---|
114 | bool res = true; |
---|
115 | AssocDataUDS* d = lookup(socketfd); |
---|
116 | if (d) { |
---|
117 | if (!ass2data.erase(d->socketfd)) res = false; |
---|
118 | if (!addr2data.erase(d->peer)) res = false; |
---|
119 | delete d; // AssocDataUDS is deleted |
---|
120 | return res; |
---|
121 | } else return false; |
---|
122 | } // end erase |
---|
123 | |
---|
124 | /** |
---|
125 | * erase an association |
---|
126 | * @param associd - association identifier |
---|
127 | * @returns true if the AssocDataUDS-object with associd could be deleted |
---|
128 | */ |
---|
129 | bool ConnectionMapUDS::erase(associd_t associd) { |
---|
130 | bool res = true; |
---|
131 | AssocDataUDS* d = lookup(associd); |
---|
132 | if (d) { |
---|
133 | if (!ass2data.erase(d->assoc)) res = false; |
---|
134 | if (!addr2data.erase(d->peer)) res = false; |
---|
135 | delete d; // AssocDataUDS is deleted |
---|
136 | return res; |
---|
137 | } else return false; |
---|
138 | } // end erase |
---|
139 | |
---|
140 | /* |
---|
141 | * |
---|
142 | */ |
---|
143 | void ConnectionMapUDS::clear() { |
---|
144 | const_ass2data_it_t hit; |
---|
145 | for (hit=ass2data.begin();hit!=ass2data.end();hit++) { |
---|
146 | if (hit->second) delete hit->second; |
---|
147 | } // end for hit |
---|
148 | ass2data.clear(); |
---|
149 | addr2data.clear(); |
---|
150 | } // end clear |
---|
151 | |
---|
152 | |
---|
153 | } // end namespace protlib |
---|
154 | /// @} |
---|