-
Notifications
You must be signed in to change notification settings - Fork 26
/
getnext.c
258 lines (226 loc) · 6.5 KB
/
getnext.c
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
* Handle iSNS DevGetNext
*
* Copyright (C) 2007 Olaf Kirch <[email protected]>
*/
#include <stdlib.h>
#include <string.h>
#include "config.h"
#include <libisns/isns.h>
#include <libisns/attrs.h>
#include <libisns/message.h>
#include "security.h"
#include "objects.h"
#include "db.h"
#include <libisns/util.h>
/*
* Create a GetNext query, and set the source name
*/
static isns_simple_t *
__isns_create_getnext(isns_source_t *source,
const isns_attr_list_t *key,
const isns_attr_list_t *scope)
{
isns_simple_t *simp;
simp = isns_simple_create(ISNS_DEVICE_GET_NEXT, source, key);
if (simp && scope)
isns_attr_list_copy(&simp->is_operating_attrs,
scope);
return simp;
}
isns_simple_t *
isns_create_getnext(isns_client_t *clnt,
isns_object_template_t *tmpl,
const isns_attr_list_t *scope)
{
isns_simple_t *simp;
unsigned int i;
simp = __isns_create_getnext(clnt->ic_source, NULL, scope);
if (simp == NULL)
return NULL;
for (i = 0; i < tmpl->iot_num_keys; ++i) {
isns_attr_list_append_nil(&simp->is_message_attrs,
tmpl->iot_keys[i]);
}
return simp;
}
isns_simple_t *
isns_create_getnext_followup(isns_client_t *clnt,
const isns_simple_t *resp,
const isns_attr_list_t *scope)
{
return __isns_create_getnext(clnt->ic_source,
&resp->is_message_attrs, scope);
}
/*
* Get the list of objects matching this query
*/
static int
isns_getnext_get_object(isns_simple_t *qry, isns_db_t *db,
isns_object_t **result)
{
isns_scope_t *scope;
isns_attr_list_t *keys = &qry->is_message_attrs, match;
isns_object_template_t *tmpl;
unsigned int i;
/*
* 5.6.5.3.
* The Message Key Attribute may be an Entity Identifier (EID),
* iSCSI Name, iSCSI Index, Portal IP Address and TCP/UDP Port,
* Portal Index, PG Index, FC Node Name WWNN, or FC Port Name
* WWPN.
*
* Implementer's comment: In other words, it must be the
* key attr(s) of a specific object type, or an index attribute.
*/
if ((tmpl = isns_object_template_for_key_attrs(keys)) != NULL) {
if (keys->ial_count != tmpl->iot_num_keys)
return ISNS_INVALID_QUERY;
} else if (keys->ial_count == 1) {
isns_attr_t *attr = keys->ial_data[0];
tmpl = isns_object_template_for_index_tag(attr->ia_tag_id);
}
if (tmpl == NULL)
return ISNS_INVALID_QUERY;
/* Verify whether the client is permitted to retrieve
* objects of the given type. */
if (!isns_policy_validate_object_type(qry->is_policy, tmpl,
qry->is_function))
return ISNS_SOURCE_UNAUTHORIZED;
/*
* 5.6.5.3.
* The Operating Attributes can be used to specify the scope
* of the DevGetNext request, and to specify the attributes of
* the next object, which are to be returned in the DevGetNext
* response message. All Operating Attributes MUST be attributes
* of the object type identified by the Message Key.
*/
match = qry->is_operating_attrs;
for (i = 0; i < match.ial_count; ++i) {
isns_attr_t *attr = match.ial_data[i];
if (tmpl != isns_object_template_for_tag(attr->ia_tag_id))
return ISNS_INVALID_QUERY;
}
/*
* 5.6.5.3.
* Non-zero-length TLV attributes in the Operating Attributes
* are used to scope the DevGetNext message.
* [...]
* Zero-length TLV attributes MUST be listed after non-zero-length
* attributes in the Operating Attributes of the DevGetNext
* request message.
*/
for (i = 0; i < match.ial_count; ++i) {
if (ISNS_ATTR_IS_NIL(match.ial_data[i])) {
match.ial_count = i;
break;
}
}
/* Get the scope for the originating node. */
scope = isns_scope_for_call(db, qry);
*result = isns_scope_get_next(scope, tmpl, keys, &match);
isns_scope_release(scope);
if (*result == NULL)
return ISNS_NO_SUCH_ENTRY;
return ISNS_SUCCESS;
}
/*
* Create a Query Response
*/
static isns_simple_t *
isns_create_getnext_response(isns_source_t *source,
const isns_simple_t *qry, isns_object_t *obj)
{
const isns_attr_list_t *req_attrs = NULL;
isns_attr_list_t requested;
isns_simple_t *resp;
unsigned int i;
resp = __isns_create_getnext(source, NULL, NULL);
/*
* 5.7.5.3. Device Get Next Response (DevGetNextRsp)
* The Message Key Attribute field returns the object keys
* for the next object after the Message Key Attribute in the
* original DevGetNext message.
*
* Implementer's note: slightly convoluted English here.
* I *think* this means the key attributes of the object
* we matched.
*/
if (!isns_object_get_key_attrs(obj, &resp->is_message_attrs))
return NULL;
/*
* 5.7.5.3.
* The Operating Attribute field returns the Operating Attributes
* of the next object as requested in the original DevGetNext
* message. The values of the Operating Attributes are those
* associated with the object identified by the Message Key
* Attribute field of the DevGetNextRsp message.
*
* Implementer's note: the RFC doesn't say clearly what to
* do when the list of operating attributes does not
* contain any NIL TLVs. Let's default to the same
* behavior as elsewhere, and return all attributes
* in this case.
*/
req_attrs = &qry->is_operating_attrs;
for (i = 0; i < req_attrs->ial_count; ++i) {
if (ISNS_ATTR_IS_NIL(req_attrs->ial_data[i]))
break;
}
requested.ial_count = req_attrs->ial_count - i;
requested.ial_data = req_attrs->ial_data + i;
if (requested.ial_count)
req_attrs = &requested;
else
req_attrs = NULL;
isns_object_get_attrlist(obj,
&resp->is_operating_attrs,
req_attrs);
return resp;
}
/*
* Process a GetNext request
*/
int
isns_process_getnext(isns_server_t *srv, isns_simple_t *call, isns_simple_t **result)
{
isns_simple_t *reply = NULL;
isns_object_t *obj = NULL;
isns_db_t *db = srv->is_db;
int status;
/* Get the next object */
status = isns_getnext_get_object(call, db, &obj);
if (status != ISNS_SUCCESS)
goto done;
/* If it's a virtual object, rebuild it */
if (obj->ie_rebuild)
obj->ie_rebuild(obj, srv->is_db);
/* Success: create a new simple message, and
* send it in our reply. */
reply = isns_create_getnext_response(srv->is_source, call, obj);
if (reply == NULL)
status = ISNS_INTERNAL_ERROR;
done:
if (obj)
isns_object_release(obj);
*result = reply;
return status;
}
/*
* Parse the object in a getnext response
*/
int
isns_getnext_response_get_object(isns_simple_t *qry,
isns_object_t **result)
{
isns_object_template_t *tmpl;
tmpl = isns_object_template_for_key_attrs(&qry->is_operating_attrs);
if (tmpl == NULL) {
isns_error("Cannot determine object type in GetNext response\n");
return ISNS_ATTRIBUTE_NOT_IMPLEMENTED;
}
*result = isns_create_object(tmpl,
&qry->is_operating_attrs,
NULL);
return ISNS_SUCCESS;
}