1
2 /*
3 * Licensed Materials - Property of IBM
4 *
5 * trousers - An open source TCG Software Stack
6 *
7 * (C) Copyright International Business Machines Corp. 2004
8 *
9 */
10
11
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.h>
15
16 #include "trousers/tss.h"
17 #include "trousers_types.h"
18 #include "tsplog.h"
19 #include "hosttable.h"
20 #include "obj.h"
21
22 static struct host_table *ht = NULL;
23
24 TSS_RESULT
host_table_init()25 host_table_init()
26 {
27 ht = calloc(1, sizeof(struct host_table));
28 if (ht == NULL) {
29 LogError("malloc of %zd bytes failed.", sizeof(struct host_table));
30 return TSPERR(TSS_E_OUTOFMEMORY);
31 }
32
33 MUTEX_INIT(ht->lock);
34
35 return TSS_SUCCESS;
36 }
37
38 #ifdef SOLARIS
39 #pragma init(_init)
_init(void)40 void _init(void)
41 #else
42 static void __attribute__ ((constructor)) my_init(void)
43 #endif
44 {
45 host_table_init();
46 __tspi_obj_list_init();
47 }
48
49 void
host_table_final()50 host_table_final()
51 {
52 struct host_table_entry *hte, *next = NULL;
53
54 MUTEX_LOCK(ht->lock);
55
56 for (hte = ht->entries; hte; hte = next) {
57 if (hte)
58 next = hte->next;
59 if (hte->hostname)
60 free(hte->hostname);
61 if (hte->comm.buf)
62 free(hte->comm.buf);
63 free(hte);
64 }
65
66 MUTEX_UNLOCK(ht->lock);
67
68 free(ht);
69 ht = NULL;
70 }
71
72 #ifdef SOLARIS
73 #pragma fini(_fini)
_fini(void)74 void _fini(void)
75 #else
76 static void __attribute__ ((destructor)) my_fini(void)
77 #endif
78 {
79 host_table_final();
80 }
81
82 TSS_RESULT
__tspi_add_table_entry(TSS_HCONTEXT tspContext,BYTE * host,int type,struct host_table_entry ** ret)83 __tspi_add_table_entry(TSS_HCONTEXT tspContext, BYTE *host, int type, struct host_table_entry **ret)
84 {
85 struct host_table_entry *entry, *tmp;
86 int hostlen;
87
88 entry = calloc(1, sizeof(struct host_table_entry));
89 if (entry == NULL) {
90 LogError("malloc of %zd bytes failed.", sizeof(struct host_table_entry));
91 return TSPERR(TSS_E_OUTOFMEMORY);
92 }
93
94 entry->tspContext = tspContext;
95
96 hostlen = strlen((char *)host)+1;
97 entry->hostname = (BYTE *)calloc(1, hostlen);
98 if (entry->hostname == NULL) {
99 LogError("malloc of %u bytes failed.", hostlen);
100 free(entry);
101 return TSPERR(TSS_E_OUTOFMEMORY);
102 }
103 memcpy(entry->hostname, host, hostlen);
104
105 entry->type = type;
106 entry->comm.buf_size = TCSD_INIT_TXBUF_SIZE;
107 entry->comm.buf = calloc(1, entry->comm.buf_size);
108 if (entry->comm.buf == NULL) {
109 LogError("malloc of %u bytes failed.", entry->comm.buf_size);
110 free(entry);
111 return TSPERR(TSS_E_OUTOFMEMORY);
112 }
113 MUTEX_INIT(entry->lock);
114
115 MUTEX_LOCK(ht->lock);
116
117 for (tmp = ht->entries; tmp; tmp = tmp->next) {
118 if (tmp->tspContext == tspContext) {
119 LogError("Tspi_Context_Connect attempted on an already connected context!");
120 MUTEX_UNLOCK(ht->lock);
121 free(entry->hostname);
122 free(entry->comm.buf);
123 free(entry);
124 return TSPERR(TSS_E_CONNECTION_FAILED);
125 }
126 }
127
128 if( ht->entries == NULL ) {
129 ht->entries = entry;
130 } else {
131 for (tmp = ht->entries; tmp->next; tmp = tmp->next)
132 ;
133 tmp->next = entry;
134 }
135 MUTEX_UNLOCK(ht->lock);
136
137 *ret = entry;
138
139 return TSS_SUCCESS;
140 }
141
142 void
remove_table_entry(TSS_HCONTEXT tspContext)143 remove_table_entry(TSS_HCONTEXT tspContext)
144 {
145 struct host_table_entry *hte, *prev = NULL;
146
147 MUTEX_LOCK(ht->lock);
148
149 for (hte = ht->entries; hte; prev = hte, hte = hte->next) {
150 if (hte->tspContext == tspContext) {
151 if (prev != NULL)
152 prev->next = hte->next;
153 else
154 ht->entries = hte->next;
155 if (hte->hostname)
156 free(hte->hostname);
157 free(hte->comm.buf);
158 free(hte);
159 break;
160 }
161 }
162
163 MUTEX_UNLOCK(ht->lock);
164 }
165
166 struct host_table_entry *
get_table_entry(TSS_HCONTEXT tspContext)167 get_table_entry(TSS_HCONTEXT tspContext)
168 {
169 struct host_table_entry *index = NULL;
170
171 MUTEX_LOCK(ht->lock);
172
173 for (index = ht->entries; index; index = index->next) {
174 if (index->tspContext == tspContext)
175 break;
176 }
177
178 if (index)
179 MUTEX_LOCK(index->lock);
180
181 MUTEX_UNLOCK(ht->lock);
182
183 return index;
184 }
185
186 void
put_table_entry(struct host_table_entry * entry)187 put_table_entry(struct host_table_entry *entry)
188 {
189 if (entry)
190 MUTEX_UNLOCK(entry->lock);
191 }
192
193