summaryrefslogtreecommitdiff
path: root/src/tspi/rpc/hosttable.c
blob: f3ec2eb97e8fa6080f626fa1f59954f1d50acbf7 (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

/*
 * Licensed Materials - Property of IBM
 *
 * trousers - An open source TCG Software Stack
 *
 * (C) Copyright International Business Machines Corp. 2004
 *
 */


#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "trousers/tss.h"
#include "trousers_types.h"
#include "tsplog.h"
#include "hosttable.h"
#include "obj.h"

struct host_table *ht = NULL;

TSS_RESULT
host_table_init()
{
	ht = calloc(1, sizeof(struct host_table));
	if (ht == NULL) {
		LogError("malloc of %zd bytes failed.", sizeof(struct host_table));
		return TSPERR(TSS_E_OUTOFMEMORY);
	}

	MUTEX_INIT(ht->lock);

	return TSS_SUCCESS;
}

#ifdef SOLARIS
#pragma init(_init)
void _init(void)
#else
void __attribute__ ((constructor)) my_init(void)
#endif
{
	host_table_init();
	__tspi_obj_list_init();
}

void
host_table_final()
{
	struct host_table_entry *hte, *next = NULL;

	MUTEX_LOCK(ht->lock);

	for (hte = ht->entries; hte; hte = next) {
		if (hte)
			next = hte->next;
		if (hte->hostname)
			free(hte->hostname);
		if (hte->comm.buf)
			free(hte->comm.buf);
		free(hte);
	}

	MUTEX_UNLOCK(ht->lock);

	free(ht);
	ht = NULL;
}

#ifdef SOLARIS
#pragma fini(_fini)
void _fini(void)
#else
void __attribute__ ((destructor)) my_fini(void)
#endif
{
	host_table_final();
}

TSS_RESULT
__tspi_add_table_entry(TSS_HCONTEXT tspContext, BYTE *host, int type, struct host_table_entry **ret)
{
	struct host_table_entry *entry, *tmp;

        entry = calloc(1, sizeof(struct host_table_entry));
        if (entry == NULL) {
                LogError("malloc of %zd bytes failed.", sizeof(struct host_table_entry));
                return TSPERR(TSS_E_OUTOFMEMORY);
        }

	entry->tspContext = tspContext;
        entry->hostname = host;
        entry->type = type;
        entry->comm.buf_size = TCSD_INIT_TXBUF_SIZE;
        entry->comm.buf = calloc(1, entry->comm.buf_size);
        if (entry->comm.buf == NULL) {
                LogError("malloc of %u bytes failed.", entry->comm.buf_size);
                free(entry);
                return TSPERR(TSS_E_OUTOFMEMORY);
        }
        MUTEX_INIT(entry->lock);

	MUTEX_LOCK(ht->lock);

	for (tmp = ht->entries; tmp; tmp = tmp->next) {
		if (tmp->tspContext == tspContext) {
			LogError("Tspi_Context_Connect attempted on an already connected context!");
			MUTEX_UNLOCK(ht->lock);
			free(entry->hostname);
			free(entry->comm.buf);
			free(entry);
			return TSPERR(TSS_E_CONNECTION_FAILED);
		}
	}

	if( ht->entries == NULL ) {
		ht->entries = entry;
	} else {
		for (tmp = ht->entries; tmp->next; tmp = tmp->next)
			;
		tmp->next = entry;
	}
	MUTEX_UNLOCK(ht->lock);

	*ret = entry;

	return TSS_SUCCESS;
}

void
remove_table_entry(TSS_HCONTEXT tspContext)
{
	struct host_table_entry *hte, *prev = NULL;

	MUTEX_LOCK(ht->lock);

	for (hte = ht->entries; hte; prev = hte, hte = hte->next) {
		if (hte->tspContext == tspContext) {
			if (prev != NULL)
				prev->next = hte->next;
			else
				ht->entries = hte->next;
			if (hte->hostname)
				free(hte->hostname);
			free(hte->comm.buf);
			free(hte);
			break;
		}
	}

	MUTEX_UNLOCK(ht->lock);
}

struct host_table_entry *
get_table_entry(TSS_HCONTEXT tspContext)
{
	struct host_table_entry *index = NULL;

	MUTEX_LOCK(ht->lock);

	for (index = ht->entries; index; index = index->next) {
		if (index->tspContext == tspContext)
			break;
	}

	if (index)
		MUTEX_LOCK(index->lock);

	MUTEX_UNLOCK(ht->lock);

	return index;
}

void
put_table_entry(struct host_table_entry *entry)
{
	if (entry)
		MUTEX_UNLOCK(entry->lock);
}