OSDN Git Service

Corresponded to 1-32 bytes SSL Session IDs. / [Ultramonkey-l7-develop 413] by Osamu...
[ultramonkey-l7/ultramonkey-l7-v2.git] / module / protocol / module_sslid_hash_map.cpp
1 /*
2  * @file  module_sslid_hash_map.cpp
3  * @brief sslid hashmap container.
4  * @brief this module provide session persistence by SSL session ID.
5  *
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.
10  *
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.
15  *
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.
20  *
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
24  * 02110-1301 USA
25  *
26  **********************************************************************/
27
28 #include <iostream>
29 #include <sstream>
30 #include <list>
31 #include <string>
32 #include <tr1/unordered_map>
33 #include <string.h>
34 #include "l7vs_dest.h"
35 #include "protomod_sslid.h"
36 #include "module_sslid_hash_map.h"
37
38
39 CSSLIDMap::CSSLIDMap()
40 {
41   listsize = 0;
42 }
43
44
45 CSSLIDMap::~CSSLIDMap()
46 {
47 }
48
49
50 void CSSLIDMap::allocate(unsigned int size)
51 {
52   m = new IDMAP;
53   l = new IDLIST;
54   listsize = size;
55 }
56
57
58 void CSSLIDMap::destroy(void)
59 {
60   if (m) {
61     delete m;
62     m = NULL;
63   }
64
65   if (l) {
66     delete l;
67     l = NULL;
68   }
69 }
70
71
72 void CSSLIDMap::add(const std::string key, struct l7vs_dest data)
73 {
74   if (m->size() >= listsize) {
75     m->erase(*(l->begin()));
76     l->pop_front();
77
78   }
79
80   m->insert(IDMAP::value_type(key, data));
81   l->push_back(key);
82 }
83
84
85 bool CSSLIDMap::search(const std::string key, l7vs_dest **data)
86 {
87   bool retval;
88
89   IDMAP::const_iterator it = m->find(key);
90
91   if (it == m->end()) {
92     retval = false;
93
94   } else {
95     *data = (struct l7vs_dest*)&it->second;
96     retval = true;
97   }
98
99   return retval;
100 }
101
102
103 void *CSSLIDMap::getIDMAP(void)
104 {
105   return (void *)m;
106 }
107
108
109 void *CSSLIDMap::getIDLIST(void)
110 {
111   return (void *)l;
112 }
113
114 int CSSLIDMap::getLISTSIZE(void)
115 {
116   return listsize;
117 }
118
119
120 void CSSLIDMap::setPointer(const void *keymap, const void *keylist, unsigned int size)
121 {
122   m = static_cast<IDMAP *> (const_cast<void*> (keymap));
123   l = static_cast<IDLIST *> (const_cast<void*> (keylist));
124   listsize = size;
125 }
126
127
128 /*!
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
133  */
134 static void str_c_id(char *id, int *id_len, const char* buf) {
135         int i;
136         char hexbuf[3] = {0,0,0};
137         (*id_len) = 0;
138         if (buf == NULL) {
139                 memset(id, 0, SSLID_LENGTH);
140         } else {
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);
145                                 (*id_len) ++;
146                         } else {
147                                 break;
148                         }
149                 }
150         }
151 }
152
153 void CSSLIDMap::construct_sessionlist(struct l7vs_sslid_service* sslid_service)
154 {
155   int pick = 0;
156   IDMAP::const_iterator it;
157   
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));
162
163   }
164 }
165
166
167 void CSSLIDMap::rebuild_sessionlist(struct l7vs_sslid_service* sslid_service)
168 {
169   int pick;
170   struct ssl_session tmp_session;
171   
172   m->clear();
173   l->clear();
174
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);
181
182   }
183 }