FBSD: deal with kernel API rename
[openafs.git] / src / rx / rx_identity.c
1 /*
2  * Copyright (c) 2010 Your File System Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24
25 #include <afsconfig.h>
26 #include <afs/param.h>
27
28 #ifndef KERNEL
29 # include <roken.h>
30 #else
31 # include "afs/sysincludes.h"
32 # include "afsincludes.h"
33 #endif
34
35 #include <rx/rx.h>
36 #include <rx/rx_identity.h>
37
38 /*!
39  * Check to see if two rx identities match
40  *
41  * @param a
42  *      First identity
43  * @param b
44  *      Second identity
45  * @returns
46  *      True if a and b are identical, otherwise false
47  */
48 int
49 rx_identity_match(struct rx_identity *a, struct rx_identity *b)
50 {
51     return (a->kind == b->kind && a->exportedName.len == b->exportedName.len
52             && (memcmp(a->exportedName.val, b->exportedName.val,
53                        a->exportedName.len) == 0));
54 }
55
56 /*!
57  * Populate an identity
58  *
59  * Populate an existing identity with copies of the data passed to the
60  * function. This will replace (without freeing) any existing identity
61  * contents.
62  *
63  * @param identity
64  *      The identity to populate
65  * @param kind
66  *      The type of data contained within this identity
67  * @param displayName
68  *      The displayName of this identity
69  * @param enameData
70  *      The opaque data that represents this identity
71  * @param enameLength
72  *      The length of enameData
73  */
74
75 void
76 rx_identity_populate(struct rx_identity *identity, rx_identity_kind kind,
77                      char *displayName, void *enameData, size_t enameLength)
78 {
79     memset(identity, 0, sizeof(struct rx_identity));
80
81     identity->kind = kind;
82     identity->displayName = rxi_Alloc(strlen(displayName)+1);
83     memcpy(identity->displayName, displayName, strlen(displayName)+1);
84
85     rx_opaque_populate(&identity->exportedName, enameData, enameLength);
86 }
87
88 /*!
89  * Copy an identity
90  *
91  * Create a new identity as a copy of an existing one.
92  *
93  * @param from
94  *      The identity to copy from
95  * @return
96  *      The new identity
97  */
98 struct rx_identity *
99 rx_identity_copy(struct rx_identity *from)
100 {
101    return rx_identity_new(from->kind, from->displayName,
102                           from->exportedName.val, from->exportedName.len);
103 }
104
105
106 /*!
107  * Copy an identity's contents
108  *
109  * Copy the contents of one identity into another one. This will replace
110  * (without freeing) any existing identity contents
111  *
112  * @param to
113  *      The identity to copy to
114  * @param from
115  *      The identity to copy from
116  */
117
118 void
119 rx_identity_copyContents(struct rx_identity *to, struct rx_identity *from)
120 {
121     rx_identity_populate(to, from->kind, from->displayName,
122                          from->exportedName.val, from->exportedName.len);
123     return;
124 }
125
126 /*!
127  * Build a new identity
128  *
129  * Create a new identity, with copies of the data passed to this function.
130  *
131  * @param kind
132  *      The type of data contained within this identity
133  * @param displayName
134  *      The displayName of this identity
135  * @param enameData
136  *      The opaque data that represents this identity
137  * @param enameLength
138  *      The length of enameData
139  * @returns
140  *      The new identity
141  */
142
143 struct rx_identity *
144 rx_identity_new(rx_identity_kind kind, char *displayName, void *enameData,
145                 size_t enameLength)
146 {
147     struct rx_identity *identity;
148
149     identity = rxi_Alloc(sizeof(struct rx_identity));
150     rx_identity_populate(identity, kind, displayName, enameData, enameLength);
151
152     return identity;
153 }
154
155 /*!
156  * Free the contents of an identity
157  *
158  * @param identity
159  *      The identity to free the contents of
160  */
161
162 void
163 rx_identity_freeContents(struct rx_identity *identity)
164 {
165     if (identity->displayName) {
166         rxi_Free(identity->displayName, strlen(identity->displayName));
167         identity->displayName = NULL;
168     }
169
170     rx_opaque_freeContents(&identity->exportedName);
171 }
172
173 /*!
174  * Free an identity
175  *
176  * @param identity
177  *      The identity to free (passed by reference)
178  */
179
180 void
181 rx_identity_free(struct rx_identity **identity)
182 {
183     rx_identity_freeContents(*identity);
184     rxi_Free(*identity, sizeof(struct rx_identity));
185     *identity = NULL;
186 }