Add.
[shishi.git] / src / resume.c
blob0ad192665dc43813caa2104a596e563f1653d711
1 /* resume.c --- Handle the details of TLS session resumption.
2 * Copyright (C) 2002, 2003 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 2 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, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 /* Note: only use syslog to report errors in this file. */
24 /* Get Shishid stuff. */
25 #include "kdc.h"
27 typedef struct
29 char *id;
30 size_t id_size;
31 char *data;
32 size_t data_size;
33 } CACHE;
35 static CACHE *cache_db;
36 static size_t cache_db_ptr = 0;
37 static size_t cache_db_size = 0;
39 int
40 resume_db_store (void *dbf, gnutls_datum key, gnutls_datum data)
42 if (cache_db_size == 0)
43 return -1;
45 cache_db[cache_db_ptr].id = xrealloc (cache_db[cache_db_ptr].id, key.size);
46 memcpy (cache_db[cache_db_ptr].id, key.data, key.size);
47 cache_db[cache_db_ptr].id_size = key.size;
49 cache_db[cache_db_ptr].data = xrealloc (cache_db[cache_db_ptr].data,
50 data.size);
51 memcpy (cache_db[cache_db_ptr].data, data.data, data.size);
52 cache_db[cache_db_ptr].data_size = data.size;
54 cache_db_ptr++;
55 cache_db_ptr %= cache_db_size;
57 return 0;
60 gnutls_datum
61 resume_db_fetch (void *dbf, gnutls_datum key)
63 gnutls_datum res = { NULL, 0 };
64 size_t i;
66 for (i = 0; i < cache_db_size; i++)
67 if (key.size == cache_db[i].id_size &&
68 memcmp (key.data, cache_db[i].id, key.size) == 0)
70 res.size = cache_db[i].data_size;
72 res.data = gnutls_malloc (res.size);
73 if (res.data == NULL)
74 return res;
76 memcpy (res.data, cache_db[i].data, res.size);
78 return res;
81 return res;
84 int
85 resume_db_delete (void *dbf, gnutls_datum key)
87 size_t i;
89 for (i = 0; i < cache_db_size; i++)
90 if (key.size == cache_db[i].id_size &&
91 memcmp (key.data, cache_db[i].id, key.size) == 0)
93 cache_db[i].id_size = 0;
94 cache_db[i].data_size = 0;
96 return 0;
99 return -1;
102 void
103 resume_db_init (size_t nconnections)
105 resume_db_done ();
106 cache_db = xcalloc (nconnections, sizeof (*cache_db));
107 cache_db_size = nconnections;
110 void
111 resume_db_done (void)
113 size_t i;
115 for (i = 0; i < cache_db_size; i++)
117 if (cache_db[i].id)
118 free (cache_db[i].id);
119 if (cache_db[i].data)
120 free (cache_db[i].data);
123 if (cache_db)
124 free (cache_db);