Add.
[shishi.git] / src / resume.c
blobde8d344bfd87357355d33cb2776864a58ba42bfe
1 /* resume.c --- Handle the details of TLS session resumption.
2 * Copyright (C) 2002, 2003, 2007 Simon Josefsson
4 * This file is part of Shishi.
6 * Shishi is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * Shishi is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Shishi; if not, see http://www.gnu.org/licenses or write
18 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
19 * Floor, Boston, MA 02110-1301, USA
23 /* Note: only use syslog to report errors in this file. */
25 /* Get Shishid stuff. */
26 #include "kdc.h"
28 typedef struct
30 char *id;
31 size_t id_size;
32 char *data;
33 size_t data_size;
34 } CACHE;
36 static CACHE *cache_db;
37 static size_t cache_db_ptr = 0;
38 static size_t cache_db_size = 0;
40 int
41 resume_db_store (void *dbf, gnutls_datum key, gnutls_datum data)
43 if (cache_db_size == 0)
44 return -1;
46 cache_db[cache_db_ptr].id = xrealloc (cache_db[cache_db_ptr].id, key.size);
47 memcpy (cache_db[cache_db_ptr].id, key.data, key.size);
48 cache_db[cache_db_ptr].id_size = key.size;
50 cache_db[cache_db_ptr].data = xrealloc (cache_db[cache_db_ptr].data,
51 data.size);
52 memcpy (cache_db[cache_db_ptr].data, data.data, data.size);
53 cache_db[cache_db_ptr].data_size = data.size;
55 cache_db_ptr++;
56 cache_db_ptr %= cache_db_size;
58 return 0;
61 gnutls_datum
62 resume_db_fetch (void *dbf, gnutls_datum key)
64 gnutls_datum res = { NULL, 0 };
65 size_t i;
67 for (i = 0; i < cache_db_size; i++)
68 if (key.size == cache_db[i].id_size &&
69 memcmp (key.data, cache_db[i].id, key.size) == 0)
71 res.size = cache_db[i].data_size;
73 res.data = gnutls_malloc (res.size);
74 if (res.data == NULL)
75 return res;
77 memcpy (res.data, cache_db[i].data, res.size);
79 return res;
82 return res;
85 int
86 resume_db_delete (void *dbf, gnutls_datum key)
88 size_t i;
90 for (i = 0; i < cache_db_size; i++)
91 if (key.size == cache_db[i].id_size &&
92 memcmp (key.data, cache_db[i].id, key.size) == 0)
94 cache_db[i].id_size = 0;
95 cache_db[i].data_size = 0;
97 return 0;
100 return -1;
103 void
104 resume_db_init (size_t nconnections)
106 resume_db_done ();
107 cache_db = xcalloc (nconnections, sizeof (*cache_db));
108 cache_db_size = nconnections;
111 void
112 resume_db_done (void)
114 size_t i;
116 for (i = 0; i < cache_db_size; i++)
118 if (cache_db[i].id)
119 free (cache_db[i].id);
120 if (cache_db[i].data)
121 free (cache_db[i].data);
124 if (cache_db)
125 free (cache_db);