~ubuntu-branches/ubuntu/hardy/trousers/hardy-proposed

« back to all changes in this revision

Viewing changes to src/tcsd_api/hosttable.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Baumann
  • Date: 2008-01-23 22:03:00 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080123220300-fhtqja3c0oq0gp6z
Tags: 0.3.1-4
* Added patch from Aaron M. Ucko <ucko@debian.org> to allow trousers to
  build successfully on amd64, and presumably also other 64-bit
  architectures (Closes: #457400).
* Including udev rule for /dev/tpm from William Lima
  <wlima.amadeus@gmail.com> as suggested by David Smith <dds@google.com>
  (Closes: #459682).
* Added lintian overrides.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
#include <pthread.h>
16
 
 
17
 
#include "trousers/tss.h"
18
 
#include "trousers_types.h"
19
 
#include "tsplog.h"
20
 
#include "hosttable.h"
21
 
#include "obj.h"
22
 
#include "tcsd.h"
23
 
 
24
 
static struct host_table *ht = NULL;
25
 
 
26
 
TSS_RESULT
27
 
host_table_init()
28
 
{
29
 
        ht = calloc(1, sizeof(struct host_table));
30
 
        if (ht == NULL) {
31
 
                LogError("malloc of %zd bytes failed.", sizeof(struct host_table));
32
 
                return TSPERR(TSS_E_OUTOFMEMORY);
33
 
        }
34
 
 
35
 
        pthread_mutex_init(&ht->lock, NULL);
36
 
 
37
 
        return TSS_SUCCESS;
38
 
}
39
 
 
40
 
void __attribute__ ((constructor)) my_init(void)
41
 
{
42
 
        host_table_init();
43
 
        obj_list_init();
44
 
}
45
 
 
46
 
#if 0
47
 
void
48
 
host_table_final()
49
 
{
50
 
        struct host_table_entry *hte, *next = NULL;
51
 
 
52
 
        pthread_mutex_lock(&(ht->lock));
53
 
 
54
 
        for (hte = ht->entries; hte; hte = next) {
55
 
                if (hte)
56
 
                        next = hte->next;
57
 
                free(hte);
58
 
        }
59
 
 
60
 
        pthread_mutex_unlock(&(ht->lock));
61
 
 
62
 
        free(ht);
63
 
        ht = NULL;
64
 
}
65
 
 
66
 
void __attribute__ ((destructor)) my_fini(void)
67
 
{
68
 
        host_table_final();
69
 
}
70
 
#endif
71
 
 
72
 
TSS_RESULT
73
 
add_table_entry(TSS_HCONTEXT tspContext, BYTE *host, int type, struct host_table_entry **ret)
74
 
{
75
 
        struct host_table_entry *entry, *tmp;
76
 
 
77
 
        if ((entry = calloc(1, sizeof(struct host_table_entry))) == NULL) {
78
 
                LogError("malloc of %zd bytes failed.", sizeof(struct host_table_entry));
79
 
                return TSPERR(TSS_E_OUTOFMEMORY);
80
 
        }
81
 
 
82
 
        entry->tspContext = tspContext;
83
 
        entry->hostname = host;
84
 
        entry->type = type;
85
 
        entry->comm.buf_size = TCSD_INIT_TXBUF_SIZE;
86
 
        entry->comm.buf = calloc(1, entry->comm.buf_size);
87
 
        if (entry->comm.buf == NULL) {
88
 
                LogError("malloc of %u bytes failed.", entry->comm.buf_size);
89
 
                free(entry);
90
 
                return TSPERR(TSS_E_OUTOFMEMORY);
91
 
        }
92
 
        pthread_mutex_init(&entry->lock, NULL);
93
 
 
94
 
        pthread_mutex_lock(&ht->lock);
95
 
 
96
 
        for (tmp = ht->entries; tmp; tmp = tmp->next) {
97
 
                if (tmp->tspContext == entry->tspContext) {
98
 
                        LogError("Tspi_Context_Connect attempted on an already connected context!");
99
 
                        pthread_mutex_unlock(&ht->lock);
100
 
                        return TSPERR(TSS_E_CONNECTION_FAILED);
101
 
                }
102
 
        }
103
 
 
104
 
        if( ht->entries == NULL ) {
105
 
                ht->entries = entry;
106
 
        } else {
107
 
                for (tmp = ht->entries; tmp->next; tmp = tmp->next)
108
 
                        ;
109
 
                tmp->next = entry;
110
 
        }
111
 
 
112
 
        pthread_mutex_unlock(&ht->lock);
113
 
 
114
 
        *ret = entry;
115
 
 
116
 
        return TSS_SUCCESS;
117
 
}
118
 
 
119
 
void
120
 
remove_table_entry(TSS_HCONTEXT tspContext)
121
 
{
122
 
        struct host_table_entry *hte, *prev = NULL;
123
 
 
124
 
        pthread_mutex_lock(&(ht->lock));
125
 
 
126
 
        for (hte = ht->entries; hte; prev = hte, hte = hte->next) {
127
 
                if (hte->tspContext == tspContext) {
128
 
                        if (prev != NULL)
129
 
                                prev->next = hte->next;
130
 
                        else
131
 
                                ht->entries = hte->next;
132
 
                        free(hte->comm.buf);
133
 
                        free(hte);
134
 
                        break;
135
 
                }
136
 
        }
137
 
 
138
 
        pthread_mutex_unlock(&(ht->lock));
139
 
}
140
 
 
141
 
struct host_table_entry *
142
 
get_table_entry(TSS_HCONTEXT tspContext)
143
 
{
144
 
        struct host_table_entry *index = NULL;
145
 
 
146
 
        pthread_mutex_lock(&(ht->lock));
147
 
 
148
 
        for (index = ht->entries; index; index = index->next) {
149
 
                if (index->tspContext == tspContext)
150
 
                        break;
151
 
        }
152
 
 
153
 
        if (index)
154
 
                pthread_mutex_lock(&(index->lock));
155
 
 
156
 
        pthread_mutex_unlock(&(ht->lock));
157
 
 
158
 
        return index;
159
 
}
160
 
 
161
 
void
162
 
put_table_entry(struct host_table_entry *entry)
163
 
{
164
 
        if (entry)
165
 
                pthread_mutex_unlock(&(entry->lock));
166
 
}
167