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 struct host_table *ht = NULL; 23 24 TSS_RESULT 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) 40 void _init(void) 41 #else 42 void __attribute__ ((constructor)) my_init(void) 43 #endif 44 { 45 host_table_init(); 46 __tspi_obj_list_init(); 47 } 48 49 void 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) 74 void _fini(void) 75 #else 76 void __attribute__ ((destructor)) my_fini(void) 77 #endif 78 { 79 host_table_final(); 80 } 81 82 TSS_RESULT 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 87 entry = calloc(1, sizeof(struct host_table_entry)); 88 if (entry == NULL) { 89 LogError("malloc of %zd bytes failed.", sizeof(struct host_table_entry)); 90 return TSPERR(TSS_E_OUTOFMEMORY); 91 } 92 93 entry->tspContext = tspContext; 94 entry->hostname = host; 95 entry->type = type; 96 entry->comm.buf_size = TCSD_INIT_TXBUF_SIZE; 97 entry->comm.buf = calloc(1, entry->comm.buf_size); 98 if (entry->comm.buf == NULL) { 99 LogError("malloc of %u bytes failed.", entry->comm.buf_size); 100 free(entry); 101 return TSPERR(TSS_E_OUTOFMEMORY); 102 } 103 MUTEX_INIT(entry->lock); 104 105 MUTEX_LOCK(ht->lock); 106 107 for (tmp = ht->entries; tmp; tmp = tmp->next) { 108 if (tmp->tspContext == tspContext) { 109 LogError("Tspi_Context_Connect attempted on an already connected context!"); 110 MUTEX_UNLOCK(ht->lock); 111 free(entry->hostname); 112 free(entry->comm.buf); 113 free(entry); 114 return TSPERR(TSS_E_CONNECTION_FAILED); 115 } 116 } 117 118 if( ht->entries == NULL ) { 119 ht->entries = entry; 120 } else { 121 for (tmp = ht->entries; tmp->next; tmp = tmp->next) 122 ; 123 tmp->next = entry; 124 } 125 MUTEX_UNLOCK(ht->lock); 126 127 *ret = entry; 128 129 return TSS_SUCCESS; 130 } 131 132 void 133 remove_table_entry(TSS_HCONTEXT tspContext) 134 { 135 struct host_table_entry *hte, *prev = NULL; 136 137 MUTEX_LOCK(ht->lock); 138 139 for (hte = ht->entries; hte; prev = hte, hte = hte->next) { 140 if (hte->tspContext == tspContext) { 141 if (prev != NULL) 142 prev->next = hte->next; 143 else 144 ht->entries = hte->next; 145 if (hte->hostname) 146 free(hte->hostname); 147 free(hte->comm.buf); 148 free(hte); 149 break; 150 } 151 } 152 153 MUTEX_UNLOCK(ht->lock); 154 } 155 156 struct host_table_entry * 157 get_table_entry(TSS_HCONTEXT tspContext) 158 { 159 struct host_table_entry *index = NULL; 160 161 MUTEX_LOCK(ht->lock); 162 163 for (index = ht->entries; index; index = index->next) { 164 if (index->tspContext == tspContext) 165 break; 166 } 167 168 if (index) 169 MUTEX_LOCK(index->lock); 170 171 MUTEX_UNLOCK(ht->lock); 172 173 return index; 174 } 175 176 void 177 put_table_entry(struct host_table_entry *entry) 178 { 179 if (entry) 180 MUTEX_UNLOCK(entry->lock); 181 } 182 183