source: trash/old-modules/transport/protlib/connectionmap.cpp@ 5887

Last change on this file since 5887 was 5641, checked in by Christoph Mayer, 15 years ago
File size: 4.6 KB
Line 
1/// ----------------------------------------*- mode: C++; -*--
2/// @file connectionmap.cpp
3/// stores connection related data
4/// ----------------------------------------------------------
5/// $Id: connectionmap.cpp 2872 2008-02-18 10:58:03Z bless $
6/// $HeadURL: https://svn.ipv6.tm.uka.de/nsis/protlib/trunk/src/connectionmap.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.h"
30#include "logfile.h"
31
32namespace protlib {
33 using namespace log;
34
35/** @ingroup tptcp
36 * @ingroup network
37 * @{
38 */
39
40
41/***** class ConnectionMap *****/
42
43/** @class ConnectionMap connectionmap.h
44 The class ConnectionMap 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 */
56bool ConnectionMap::insert(AssocData* 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,"ConnectionMap","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 AssocData object or NULL.
80 * @param socketfd socket file descriptor
81 */
82
83AssocData* ConnectionMap::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
90AssocData* ConnectionMap::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 AssocData object or NULL.
98 * @param addr IP-adress + port
99 *
100 */
101AssocData* ConnectionMap::lookup(const appladdress& addr) const
102{
103 // hack: only search for ip addresses!
104 const char* addr_ip = addr.get_ip_str();
105 for (const_addr2data_it_t::const_iterator i=addr2data.begin(); i!=addr2data.end(); i++) {
106 const appladdress& map_addr = i->first;
107 const char* map_ip = map_addr.get_ip_str();
108 if (strcmp(map_ip, addr_ip)==0) return i->second;
109 }
110 return NULL;
111
112// const_addr2data_it_t hit= addr2data.find(addr);
113// if (hit!=addr2data.end()) return hit->second;
114// else return NULL;
115} // end lookup
116
117/** @returns true if the AssocData-object with socketfd
118 * could be deleted
119 * @param socketfd socket file descriptor
120 */
121bool ConnectionMap::erase(socketfd_t socketfd) {
122 bool res = true;
123 AssocData* d = lookup(socketfd);
124 if (d) {
125 if (!ass2data.erase(d->socketfd)) res = false;
126 if (!addr2data.erase(d->peer)) res = false;
127 delete d; // AssocData is deleted
128 return res;
129 } else return false;
130} // end erase
131
132/**
133 * could be deleted
134 * @param associd association id
135 * @returns true if the AssocData-object with associd
136 */
137bool ConnectionMap::erase(associd_t associd) {
138 bool res = true;
139 AssocData* d = lookup(associd);
140 if (d) {
141 if (!ass2data.erase(d->assoc)) res = false;
142 if (!addr2data.erase(d->peer)) res = false;
143 delete d; // AssocData is deleted
144 return res;
145 } else return false;
146} // end erase
147
148/*
149 *
150 */
151void ConnectionMap::clear() {
152 const_ass2data_it_t hit;
153 for (hit=ass2data.begin();hit!=ass2data.end();hit++) {
154 if (hit->second) delete hit->second;
155 } // end for hit
156 ass2data.clear();
157 addr2data.clear();
158} // end clear
159
160
161} // end namespace protlib
162/// @}
Note: See TracBrowser for help on using the repository browser.