2 * @file module_sslid_hash_map.cpp
3 * @brief sslid hashmap container.
4 * @brief this module provide session persistence by SSL session ID.
6 * L7VSD: Linux Virtual Server for Layer7 Load Balancing
7 * Copyright (C) 2008 NTT COMWARE Corporation.
8 * Copyright (C) 2009 Shinya TAKEBAYASHI
9 * Copyright (C) 2009 NTT Resonant Inc. O.Nakayama, T.Motoda.
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
26 **********************************************************************/
32 #include <tr1/unordered_map>
34 #include "l7vs_dest.h"
35 #include "protomod_sslid.h"
36 #include "module_sslid_hash_map.h"
39 CSSLIDMap::CSSLIDMap()
45 CSSLIDMap::~CSSLIDMap()
50 void CSSLIDMap::allocate(unsigned int size)
58 void CSSLIDMap::destroy(void)
72 void CSSLIDMap::add(const std::string key, struct l7vs_dest data)
74 if (m->size() >= listsize) {
75 m->erase(*(l->begin()));
80 m->insert(IDMAP::value_type(key, data));
85 bool CSSLIDMap::search(const std::string key, l7vs_dest **data)
89 IDMAP::const_iterator it = m->find(key);
95 *data = (struct l7vs_dest*)&it->second;
103 void *CSSLIDMap::getIDMAP(void)
109 void *CSSLIDMap::getIDLIST(void)
114 int CSSLIDMap::getLISTSIZE(void)
120 void CSSLIDMap::setPointer(const void *keymap, const void *keylist, unsigned int size)
122 m = static_cast<IDMAP *> (const_cast<void*> (keymap));
123 l = static_cast<IDLIST *> (const_cast<void*> (keylist));
129 * Convert SSL session ID (hex to binary) // 2009.4.9 T.Motoda@NTTR
130 * @param[out] id SSL session ID
131 * @param[out] id_len a pointer to SSL session ID length
132 * @param[in] buf hex string
134 static void str_c_id(char *id, int *id_len, const char* buf) {
136 char hexbuf[3] = {0,0,0};
139 memset(id, 0, SSLID_LENGTH);
141 for (i = 0; i < SSLID_LENGTH; i++) {
142 if ((hexbuf[0] = buf[i * 2 + 0]) > 0) {
143 hexbuf[1] = buf[i * 2 + 1];
144 id[i] = strtol(hexbuf, NULL, 16);
153 void CSSLIDMap::construct_sessionlist(struct l7vs_sslid_service* sslid_service)
156 IDMAP::const_iterator it;
158 for (it = m->begin(); it != m->end(); it++, pick++) {
159 str_c_id((sslid_service->session + pick)->id, &((sslid_service->session + pick)->id_len), it->first.c_str()); // 2009.4.9 T.Motoda@NTTR
160 memcpy((sslid_service->session + pick)->id, &(it->first), 32);
161 memcpy(&(sslid_service->session + pick)->dest, &(it->second), sizeof(struct l7vs_dest));
167 void CSSLIDMap::rebuild_sessionlist(struct l7vs_sslid_service* sslid_service)
170 struct ssl_session tmp_session;
175 for (pick = 0; pick < sslid_service->maxlist; pick++) {
176 memset(&tmp_session, 0, sizeof (struct ssl_session));
177 tmp_session.id_len = (sslid_service->session + pick)->id_len; // added by 2009.4.9 T.Motoda@NTTR
178 memcpy(&(tmp_session.id), &(sslid_service->session + pick)->id, 32);
179 memcpy(&(tmp_session.dest), &(sslid_service->session + pick)->dest, sizeof(struct l7vs_dest));
180 add(tmp_session.id, tmp_session.dest);