summaryrefslogtreecommitdiffstats
path: root/src/global/scache_single.c
blob: 2a3864b9bd52fdfc79747340f608045b2faf9c79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*++
/* NAME
/*	scache_single 3
/* SUMMARY
/*	single-item session cache
/* SYNOPSIS
/*	#include <scache.h>
/* DESCRIPTION
/*	SCACHE *scache_single_create()
/* DESCRIPTION
/*	This module implements an in-memory, single-session cache.
/*
/*	scache_single_create() creates a session cache instance
/*	that stores a single session.
/* DIAGNOSTICS
/*	Fatal error: memory allocation problem;
/*	panic: internal consistency failure.
/* SEE ALSO
/*	scache(3), generic session cache API
/* LICENSE
/* .ad
/* .fi
/*	The Secure Mailer license must be distributed with this software.
/* AUTHOR(S)
/*	Wietse Venema
/*	IBM T.J. Watson Research
/*	P.O. Box 704
/*	Yorktown Heights, NY 10598, USA
/*--*/

/* System library. */

#include <sys_defs.h>
#include <unistd.h>
#include <string.h>

/* Utility library. */

#include <msg.h>
#include <vstring.h>
#include <mymalloc.h>
#include <events.h>

/*#define msg_verbose 1*/

/* Global library. */

#include <scache.h>

/* Application-specific. */

 /*
  * Data structure for one saved connection. It is left up to the application
  * to serialize attributes upon passivation, and to de-serialize them upon
  * re-activation.
  */
typedef struct {
    VSTRING *endp_label;		/* physical endpoint name */
    VSTRING *endp_prop;			/* endpoint properties, serialized */
    int     fd;				/* the session */
} SCACHE_SINGLE_ENDP;

 /*
  * Data structure for a logical name to physical endpoint binding. It is
  * left up to the application to serialize attributes upon passivation, and
  * to de-serialize then upon re-activation.
  */
typedef struct {
    VSTRING *dest_label;		/* logical destination name */
    VSTRING *dest_prop;			/* binding properties, serialized */
    VSTRING *endp_label;		/* physical endpoint name */
} SCACHE_SINGLE_DEST;

 /*
  * SCACHE_SINGLE is a derived type from the SCACHE super-class.
  */
typedef struct {
    SCACHE  scache[1];			/* super-class */
    SCACHE_SINGLE_ENDP endp;		/* one cached session */
    SCACHE_SINGLE_DEST dest;		/* one cached binding */
} SCACHE_SINGLE;

static void scache_single_expire_endp(int, void *);
static void scache_single_expire_dest(int, void *);

#define SCACHE_SINGLE_ENDP_BUSY(sp)	(VSTRING_LEN(sp->endp.endp_label) > 0)
#define SCACHE_SINGLE_DEST_BUSY(sp)	(VSTRING_LEN(sp->dest.dest_label) > 0)

#define STR(x) vstring_str(x)

/* scache_single_free_endp - discard endpoint */

static void scache_single_free_endp(SCACHE_SINGLE *sp)
{
    const char *myname = "scache_single_free_endp";

    if (msg_verbose)
	msg_info("%s: %s", myname, STR(sp->endp.endp_label));

    event_cancel_timer(scache_single_expire_endp, (void *) sp);
    if (sp->endp.fd >= 0 && close(sp->endp.fd) < 0)
	msg_warn("close session endpoint %s: %m", STR(sp->endp.endp_label));
    VSTRING_RESET(sp->endp.endp_label);
    VSTRING_TERMINATE(sp->endp.endp_label);
    VSTRING_RESET(sp->endp.endp_prop);
    VSTRING_TERMINATE(sp->endp.endp_prop);
    sp->endp.fd = -1;
}

/* scache_single_expire_endp - discard expired session */

static void scache_single_expire_endp(int unused_event, void *context)
{
    SCACHE_SINGLE *sp = (SCACHE_SINGLE *) context;

    scache_single_free_endp(sp);
}

/* scache_single_save_endp - save endpoint */

static void scache_single_save_endp(SCACHE *scache, int endp_ttl,
				            const char *endp_label,
				            const char *endp_prop, int fd)
{
    SCACHE_SINGLE *sp = (SCACHE_SINGLE *) scache;
    const char *myname = "scache_single_save_endp";

    if (endp_ttl <= 0)
	msg_panic("%s: bad endp_ttl: %d", myname, endp_ttl);

    if (SCACHE_SINGLE_ENDP_BUSY(sp))
	scache_single_free_endp(sp);		/* dump the cached fd */

    vstring_strcpy(sp->endp.endp_label, endp_label);
    vstring_strcpy(sp->endp.endp_prop, endp_prop);
    sp->endp.fd = fd;
    event_request_timer(scache_single_expire_endp, (void *) sp, endp_ttl);

    if (msg_verbose)
	msg_info("%s: %s fd=%d", myname, endp_label, fd);
}

/* scache_single_find_endp - look up cached session */

static int scache_single_find_endp(SCACHE *scache, const char *endp_label,
				           VSTRING *endp_prop)
{
    SCACHE_SINGLE *sp = (SCACHE_SINGLE *) scache;
    const char *myname = "scache_single_find_endp";
    int     fd;

    if (!SCACHE_SINGLE_ENDP_BUSY(sp)) {
	if (msg_verbose)
	    msg_info("%s: no endpoint cache: %s", myname, endp_label);
	return (-1);
    }
    if (strcmp(STR(sp->endp.endp_label), endp_label) == 0) {
	vstring_strcpy(endp_prop, STR(sp->endp.endp_prop));
	fd = sp->endp.fd;
	sp->endp.fd = -1;
	scache_single_free_endp(sp);
	if (msg_verbose)
	    msg_info("%s: found: %s fd=%d", myname, endp_label, fd);
	return (fd);
    }
    if (msg_verbose)
	msg_info("%s: not found: %s", myname, endp_label);
    return (-1);
}

/* scache_single_free_dest - discard destination/endpoint association */

static void scache_single_free_dest(SCACHE_SINGLE *sp)
{
    const char *myname = "scache_single_free_dest";

    if (msg_verbose)
	msg_info("%s: %s -> %s", myname, STR(sp->dest.dest_label),
		 STR(sp->dest.endp_label));

    event_cancel_timer(scache_single_expire_dest, (void *) sp);
    VSTRING_RESET(sp->dest.dest_label);
    VSTRING_TERMINATE(sp->dest.dest_label);
    VSTRING_RESET(sp->dest.dest_prop);
    VSTRING_TERMINATE(sp->dest.dest_prop);
    VSTRING_RESET(sp->dest.endp_label);
    VSTRING_TERMINATE(sp->dest.endp_label);
}

/* scache_single_expire_dest - discard expired destination/endpoint binding */

static void scache_single_expire_dest(int unused_event, void *context)
{
    SCACHE_SINGLE *sp = (SCACHE_SINGLE *) context;

    scache_single_free_dest(sp);
}

/* scache_single_save_dest - create destination/endpoint association */

static void scache_single_save_dest(SCACHE *scache, int dest_ttl,
				            const char *dest_label,
				            const char *dest_prop,
				            const char *endp_label)
{
    SCACHE_SINGLE *sp = (SCACHE_SINGLE *) scache;
    const char *myname = "scache_single_save_dest";
    int     refresh;

    if (dest_ttl <= 0)
	msg_panic("%s: bad dest_ttl: %d", myname, dest_ttl);

    /*
     * Optimize: reset timer only, if nothing has changed.
     */
    refresh =
	(SCACHE_SINGLE_DEST_BUSY(sp)
	 && strcmp(STR(sp->dest.dest_label), dest_label) == 0
	 && strcmp(STR(sp->dest.dest_prop), dest_prop) == 0
	 && strcmp(STR(sp->dest.endp_label), endp_label) == 0);

    if (refresh == 0) {
	vstring_strcpy(sp->dest.dest_label, dest_label);
	vstring_strcpy(sp->dest.dest_prop, dest_prop);
	vstring_strcpy(sp->dest.endp_label, endp_label);
    }
    event_request_timer(scache_single_expire_dest, (void *) sp, dest_ttl);

    if (msg_verbose)
	msg_info("%s: %s -> %s%s", myname, dest_label, endp_label,
		 refresh ? " (refreshed)" : "");
}

/* scache_single_find_dest - look up cached session */

static int scache_single_find_dest(SCACHE *scache, const char *dest_label,
			             VSTRING *dest_prop, VSTRING *endp_prop)
{
    SCACHE_SINGLE *sp = (SCACHE_SINGLE *) scache;
    const char *myname = "scache_single_find_dest";
    int     fd;

    if (!SCACHE_SINGLE_DEST_BUSY(sp)) {
	if (msg_verbose)
	    msg_info("%s: no destination cache: %s", myname, dest_label);
	return (-1);
    }
    if (strcmp(STR(sp->dest.dest_label), dest_label) == 0) {
	if (msg_verbose)
	    msg_info("%s: found: %s", myname, dest_label);
	if ((fd = scache_single_find_endp(scache, STR(sp->dest.endp_label), endp_prop)) >= 0) {
	    vstring_strcpy(dest_prop, STR(sp->dest.dest_prop));
	    return (fd);
	}
    }
    if (msg_verbose)
	msg_info("%s: not found: %s", myname, dest_label);
    return (-1);
}

/* scache_single_size - size of single-element cache :-) */

static void scache_single_size(SCACHE *scache, SCACHE_SIZE *size)
{
    SCACHE_SINGLE *sp = (SCACHE_SINGLE *) scache;

    size->dest_count = (!SCACHE_SINGLE_DEST_BUSY(sp) ? 0 : 1);
    size->endp_count = (!SCACHE_SINGLE_ENDP_BUSY(sp) ? 0 : 1);
    size->sess_count = (sp->endp.fd < 0 ? 0 : 1);
}

/* scache_single_free - destroy single-element cache object */

static void scache_single_free(SCACHE *scache)
{
    SCACHE_SINGLE *sp = (SCACHE_SINGLE *) scache;

    vstring_free(sp->endp.endp_label);
    vstring_free(sp->endp.endp_prop);
    if (sp->endp.fd >= 0)
	close(sp->endp.fd);

    vstring_free(sp->dest.dest_label);
    vstring_free(sp->dest.dest_prop);
    vstring_free(sp->dest.endp_label);

    myfree((void *) sp);
}

/* scache_single_create - initialize */

SCACHE *scache_single_create(void)
{
    SCACHE_SINGLE *sp = (SCACHE_SINGLE *) mymalloc(sizeof(*sp));

    sp->scache->save_endp = scache_single_save_endp;
    sp->scache->find_endp = scache_single_find_endp;
    sp->scache->save_dest = scache_single_save_dest;
    sp->scache->find_dest = scache_single_find_dest;
    sp->scache->size = scache_single_size;
    sp->scache->free = scache_single_free;

    sp->endp.endp_label = vstring_alloc(10);
    sp->endp.endp_prop = vstring_alloc(10);
    sp->endp.fd = -1;

    sp->dest.dest_label = vstring_alloc(10);
    sp->dest.dest_prop = vstring_alloc(10);
    sp->dest.endp_label = vstring_alloc(10);

    return (sp->scache);
}