forked from cleech/open-isns
-
Notifications
You must be signed in to change notification settings - Fork 22
/
attrs.c
1623 lines (1346 loc) · 34.3 KB
/
attrs.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Handle iSNS attributes and attribute lists
*
* Copyright (C) 2007 Olaf Kirch <[email protected]>
*/
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <libisns/util.h>
#include "vendor.h"
#include <libisns/attrs.h>
#include <libisns/isns.h>
/* Implementation limit - sanity checking */
#define ISNS_ATTR_MAX_LEN 8192
static void __isns_attr_set_value(isns_attr_t *, const isns_value_t *);
/*
* Allocate an attribute
*/
isns_attr_t *
isns_attr_alloc(uint32_t tag, const isns_tag_type_t *tag_type, const isns_value_t *value)
{
isns_attr_t *attr;
if (tag_type == NULL)
tag_type = isns_tag_type_by_id(tag);
attr = isns_calloc(1, sizeof(*attr));
if (!attr)
isns_fatal("Out of memory!\n");
attr->ia_users = 1;
attr->ia_tag_id = tag;
attr->ia_tag = tag_type;
__isns_attr_set_value(attr, value);
return attr;
}
isns_attr_t *
isns_attr_get(isns_attr_t *attr)
{
if (attr) {
isns_assert(attr->ia_users);
attr->ia_users++;
}
return attr;
}
void
isns_attr_release(isns_attr_t *attr)
{
const isns_attr_type_t *type;
isns_assert(attr->ia_users);
if (--(attr->ia_users))
return;
type = attr->ia_value.iv_type;
if (type->it_destroy)
type->it_destroy(&attr->ia_value);
isns_free(attr);
}
/*
* Assign a value to an attribute
*/
void
__isns_attr_set_value(isns_attr_t *attr, const isns_value_t *new_value)
{
const isns_attr_type_t *type, *old_type;
isns_value_t *old_value;
old_value = &attr->ia_value;
if (old_value == new_value)
return;
old_type = old_value->iv_type;
if (old_type && old_type->it_destroy)
old_type->it_destroy(old_value);
if (!new_value || !(type = new_value->iv_type))
type = attr->ia_tag->it_type;
/* When assigning the value to the attr, check
* whether it needs special attention. */
if (new_value) {
if (type->it_assign) {
type->it_assign(&attr->ia_value, new_value);
} else {
attr->ia_value = *new_value;
}
}
attr->ia_value.iv_type = type;
}
/*
* Compare two attributes.
* Returns non-null when attributes are the same, else 0.
*/
int
isns_attr_match(const isns_attr_t *a, const isns_attr_t *b)
{
const isns_attr_type_t *type;
if (a->ia_tag_id != b->ia_tag_id)
return 0;
/* NIL acts as a wildcard */
if (a->ia_value.iv_type == &isns_attr_type_nil
|| b->ia_value.iv_type == &isns_attr_type_nil)
return 1;
if (a->ia_value.iv_type != b->ia_value.iv_type)
return 0;
type = a->ia_value.iv_type;
if (type->it_match)
return type->it_match(&a->ia_value, &b->ia_value);
return !memcmp(&a->ia_value, &b->ia_value, sizeof(isns_value_t));
}
/*
* Lexicographical comparison of two attributes.
* Returns -1 when a is less than b, +1 when a is greater than
* b, and 0 if equal.
*/
int
isns_attr_compare(const isns_attr_t *a, const isns_attr_t *b)
{
const isns_attr_type_t *type = a->ia_value.iv_type;
isns_assert(a->ia_tag_id == b->ia_tag_id);
if (type != b->ia_value.iv_type) {
/* One of them must be NIL */
if (type == &isns_attr_type_nil)
return -1;
return 1;
}
/* If both are NIL, consider them equal */
if (type == &isns_attr_type_nil)
return 0;
/* A few types need special comparison functions, but
* most don't. The reason is, we don't care whether the
* ordering this creates is the "canonical" ordering for
* this type, eg for integers. All that matters is that
* there is some consistent ordering suitable for
* DevGetNext.
*/
if (type->it_compare)
return type->it_compare(&a->ia_value, &b->ia_value);
return memcmp(&a->ia_value, &b->ia_value, sizeof(isns_value_t));
}
/*
* Convert a string to an attribute
*/
isns_attr_t *
isns_attr_from_string(uint32_t tag, const char *string)
{
const isns_tag_type_t *tag_type;
int (*parse)(isns_value_t *, const char *);
isns_value_t value;
memset(&value, 0, sizeof(value));
tag_type = isns_tag_type_by_id(tag);
if (!tag_type)
return NULL;
parse = tag_type->it_parse;
if (parse == NULL)
parse = tag_type->it_type->it_parse;
if (!parse || !parse(&value, string))
return NULL;
return isns_attr_alloc(tag, tag_type, &value);
}
/*
* Initialize an attribute list.
*/
void
isns_attr_list_init(isns_attr_list_t *list)
{
memset(list, 0, sizeof(*list));
}
static inline void
__isns_attr_list_resize(isns_attr_list_t *list, unsigned int count)
{
unsigned int max;
max = (list->ial_count + 15) & ~15;
if (count < max)
return;
count = (count + 15) & ~15;
list->ial_data = isns_realloc(list->ial_data, count * sizeof(isns_attr_t *));
if (!list->ial_data)
isns_fatal("Out of memory!\n");
}
void
isns_attr_list_append_list(isns_attr_list_t *dst,
const isns_attr_list_t *src)
{
unsigned int i, j;
__isns_attr_list_resize(dst, dst->ial_count + src->ial_count);
j = dst->ial_count;
for (i = 0; i < src->ial_count; ++i, ++j) {
isns_attr_t *attr = src->ial_data[i];
dst->ial_data[j] = attr;
attr->ia_users++;
}
dst->ial_count = j;
}
void
isns_attr_list_copy(isns_attr_list_t *dst,
const isns_attr_list_t *src)
{
isns_attr_list_destroy(dst);
isns_attr_list_append_list(dst, src);
}
void
isns_attr_list_destroy(isns_attr_list_t *list)
{
unsigned int i;
for (i = 0; i < list->ial_count; ++i) {
isns_attr_t *attr = list->ial_data[i];
isns_attr_release(attr);
}
if (list->ial_data)
isns_free(list->ial_data);
memset(list, 0, sizeof(*list));
}
int
isns_attr_list_remove_tag(isns_attr_list_t *list, uint32_t tag)
{
unsigned int i = 0, j = 0, removed = 0;
for (i = 0; i < list->ial_count; ++i) {
isns_attr_t *attr = list->ial_data[i];
if (attr->ia_tag_id == tag) {
isns_attr_release(attr);
removed++;
} else {
list->ial_data[j++] = attr;
}
}
list->ial_count = j;
return removed;
}
/*
* Locate the given attribute in the list, remove it
* and any following attributes that have a tag from the
* @subordinate_tags list. This is used by the DDDereg
* code to remove DD members.
*/
int
isns_attr_list_remove_member(isns_attr_list_t *list,
const isns_attr_t *match,
const uint32_t *subordinate_tags)
{
unsigned int i = 0, j = 0, k, removed = 0, purging = 0;
while (i < list->ial_count) {
isns_attr_t *attr = list->ial_data[i++];
if (purging && subordinate_tags) {
for (k = 0; subordinate_tags[k]; ++k) {
if (attr->ia_tag_id == subordinate_tags[k])
goto purge_attr;
}
}
purging = 0;
if (!isns_attr_match(attr, match)) {
list->ial_data[j++] = attr;
continue;
}
purge_attr:
isns_attr_release(attr);
purging = 1;
removed++;
}
list->ial_count = j;
return removed;
}
/*
* Find the first attribute with the given tag
*/
static inline isns_attr_t *
__isns_attr_list_find(const isns_attr_list_t *list, uint32_t tag)
{
isns_attr_t *attr;
unsigned int i;
for (i = 0; i < list->ial_count; ++i) {
attr = list->ial_data[i];
if (attr->ia_tag_id == tag)
return attr;
}
return NULL;
}
/*
* Add a new attribute at the end of the list
*/
static inline void
__isns_attr_list_append_attr(isns_attr_list_t *list, isns_attr_t *attr)
{
__isns_attr_list_resize(list, list->ial_count + 1);
list->ial_data[list->ial_count++] = attr;
}
void
isns_attr_list_append_attr(isns_attr_list_t *list, isns_attr_t *attr)
{
attr->ia_users++;
__isns_attr_list_append_attr(list, attr);
}
/*
* Append an element to an attribute list
*/
static void
__isns_attr_list_append(isns_attr_list_t *list,
uint32_t tag, const isns_tag_type_t *tag_type,
const isns_value_t *value)
{
isns_attr_t *attr;
if (tag_type == NULL)
tag_type = isns_tag_type_by_id(tag);
if (value->iv_type != &isns_attr_type_nil
&& value->iv_type != tag_type->it_type) {
isns_warning("Using wrong type (%s) "
"when encoding attribute %04x (%s) - should be %s\n",
value->iv_type->it_name,
tag, tag_type->it_name,
tag_type->it_type->it_name);
}
attr = isns_attr_alloc(tag, tag_type, value);
__isns_attr_list_append_attr(list, attr);
}
/*
* Update an element to an attribute list
*/
static void
__isns_attr_list_update(isns_attr_list_t *list,
uint32_t tag, const isns_tag_type_t *tag_type,
const isns_value_t *value)
{
const isns_attr_type_t *type = value->iv_type;
isns_attr_t *attr;
if (tag_type == NULL)
tag_type = isns_tag_type_by_id(tag);
if (type != &isns_attr_type_nil
&& type != tag_type->it_type) {
isns_warning("Using wrong type (%s) "
"when encoding attribute %04x (%s) - should be %s\n",
type->it_name,
tag, tag_type->it_name,
tag_type->it_type->it_name);
}
if (tag_type->it_multiple
|| (attr = __isns_attr_list_find(list, tag)) == NULL) {
attr = isns_attr_alloc(tag, tag_type, NULL);
__isns_attr_list_append_attr(list, attr);
}
__isns_attr_set_value(attr, value);
}
/*
* Append an element to an attribute list - public interface
*/
void
isns_attr_list_append_value(isns_attr_list_t *list,
uint32_t tag, const isns_tag_type_t *tag_type,
const isns_value_t *value)
{
__isns_attr_list_append(list, tag, tag_type, value);
}
/*
* Update an element of an attribute list - public interface
*/
void
isns_attr_list_update_value(isns_attr_list_t *list,
uint32_t tag, const isns_tag_type_t *tag_type,
const isns_value_t *value)
{
__isns_attr_list_update(list, tag, tag_type, value);
}
void
isns_attr_list_update_attr(isns_attr_list_t *list,
const isns_attr_t *attr)
{
__isns_attr_list_update(list, attr->ia_tag_id,
attr->ia_tag, &attr->ia_value);
}
/*
* Replace an attribute on a list
*/
int
isns_attr_list_replace_attr(isns_attr_list_t *list,
isns_attr_t *attr)
{
unsigned int i;
for (i = 0; i < list->ial_count; ++i) {
isns_attr_t *other = list->ial_data[i];
if (other->ia_tag_id == attr->ia_tag_id) {
list->ial_data[i] = attr;
attr->ia_users++;
isns_attr_release(other);
return 1;
}
}
return 0;
}
/*
* Retrieve an element of an attribute list
*/
int
isns_attr_list_get_attr(const isns_attr_list_t *list,
uint32_t tag, isns_attr_t **result)
{
*result = __isns_attr_list_find(list, tag);
return *result != NULL;
}
int
isns_attr_list_get_value(const isns_attr_list_t *list,
uint32_t tag, isns_value_t *value)
{
isns_attr_t *attr;
if (!(attr = __isns_attr_list_find(list, tag)))
return 0;
*value = attr->ia_value;
return 1;
}
int
isns_attr_list_get_uint32(const isns_attr_list_t *list,
uint32_t tag, uint32_t *value)
{
isns_attr_t *attr;
if (!(attr = __isns_attr_list_find(list, tag))
|| !ISNS_ATTR_IS_UINT32(attr))
return 0;
*value = attr->ia_value.iv_uint32;
return 1;
}
int
isns_attr_list_get_ipaddr(const isns_attr_list_t *list,
uint32_t tag, struct in6_addr *value)
{
isns_attr_t *attr;
if (!(attr = __isns_attr_list_find(list, tag))
|| !ISNS_ATTR_IS_IPADDR(attr))
return 0;
*value = attr->ia_value.iv_ipaddr;
return 1;
}
int
isns_attr_list_get_string(const isns_attr_list_t *list,
uint32_t tag, const char **value)
{
isns_attr_t *attr;
if (!(attr = __isns_attr_list_find(list, tag))
|| !ISNS_ATTR_IS_STRING(attr))
return 0;
*value = attr->ia_value.iv_string;
return 1;
}
int
isns_attr_list_contains(const isns_attr_list_t *list,
uint32_t tag)
{
return __isns_attr_list_find(list, tag) != NULL;
}
/*
* Some attribute types have an implied ordering,
* which is needed for GetNext. This is used to
* compare two lists.
*/
/*
* Typed versions of isns_attr_list_append
*/
void
isns_attr_list_append_nil(isns_attr_list_t *list, uint32_t tag)
{
isns_value_t var = ISNS_VALUE_INIT(nil, 0);
__isns_attr_list_append(list, tag, NULL, &var);
}
void
isns_attr_list_append_string(isns_attr_list_t *list,
uint32_t tag, const char *value)
{
isns_value_t var = ISNS_VALUE_INIT(string, (char *) value);
__isns_attr_list_append(list, tag, NULL, &var);
}
void
isns_attr_list_append_uint32(isns_attr_list_t *list,
uint32_t tag, uint32_t value)
{
isns_value_t var = ISNS_VALUE_INIT(uint32, value);
__isns_attr_list_append(list, tag, NULL, &var);
}
void
isns_attr_list_append_int32(isns_attr_list_t *list,
uint32_t tag, int32_t value)
{
isns_value_t var = ISNS_VALUE_INIT(int32, value);
__isns_attr_list_append(list, tag, NULL, &var);
}
void
isns_attr_list_append_uint64(isns_attr_list_t *list,
uint32_t tag, int64_t value)
{
isns_value_t var = ISNS_VALUE_INIT(uint64, value);
__isns_attr_list_append(list, tag, NULL, &var);
}
void
isns_attr_list_append_ipaddr(isns_attr_list_t *list,
uint32_t tag, const struct in6_addr *value)
{
isns_value_t var = ISNS_VALUE_INIT(ipaddr, *value);
__isns_attr_list_append(list, tag, NULL, &var);
}
/*
* Untyped version of isns_attr_list_append and isns_attr_list_update.
* The caller must make sure that the type of @data matches the tag's type.
*/
int
isns_attr_list_append(isns_attr_list_t *list, uint32_t tag, const void *data)
{
const isns_tag_type_t *tag_type;
isns_value_t var;
if (!(tag_type = isns_tag_type_by_id(tag)))
return 0;
var.iv_type = tag_type->it_type;
if (!var.iv_type->it_set(&var, data))
return 0;
__isns_attr_list_append(list, tag, tag_type, &var);
return 1;
}
int
isns_attr_list_update(isns_attr_list_t *list, uint32_t tag, const void *data)
{
const isns_tag_type_t *tag_type;
isns_attr_type_t *type;
isns_value_t var;
if (!(tag_type = isns_tag_type_by_id(tag)))
return 0;
type = tag_type->it_type;
var.iv_type = type;
if (!type->it_set(&var, data))
return 0;
__isns_attr_list_update(list, tag, tag_type, &var);
return 1;
}
/*
* Validate the attribute list.
*/
int
isns_attr_validate(const isns_attr_t *attr,
const isns_policy_t *policy)
{
const isns_tag_type_t *tag_type;
tag_type = attr->ia_tag;
if (tag_type->it_validate == NULL)
return 1;
return tag_type->it_validate(&attr->ia_value, policy);
}
int
isns_attr_list_validate(const isns_attr_list_t *list,
const isns_policy_t *policy,
unsigned int function)
{
DECLARE_BITMAP(seen, __ISNS_TAG_MAX);
unsigned int i;
for (i = 0; i < list->ial_count; ++i) {
const isns_tag_type_t *tag_type;
isns_attr_t *attr = list->ial_data[i];
uint32_t tag = attr->ia_tag_id;
unsigned int bit;
if (attr == NULL)
return ISNS_INTERNAL_ERROR;
tag_type = attr->ia_tag;
if (tag_type == NULL)
return ISNS_INTERNAL_ERROR;
bit = tag;
if (OPENISNS_IS_PRIVATE_ATTR(tag))
bit -= OPENISNS_VENDOR_PREFIX;
if (bit >= __ISNS_TAG_MAX)
goto invalid;
if (attr->ia_value.iv_type == &isns_attr_type_nil) {
if (test_bit(seen, bit))
goto invalid;
} else
if (attr->ia_value.iv_type == tag_type->it_type) {
if (!tag_type->it_multiple && test_bit(seen, bit))
goto invalid;
if (!isns_attr_validate(attr, policy))
goto invalid;
} else {
return ISNS_INTERNAL_ERROR;
}
if (function == ISNS_DEVICE_ATTRIBUTE_REGISTER
&& tag_type->it_readonly)
goto invalid;
set_bit(seen, bit);
}
return ISNS_SUCCESS;
invalid:
switch (function) {
case ISNS_DEVICE_ATTRIBUTE_REGISTER:
return ISNS_INVALID_REGISTRATION;
case ISNS_DEVICE_DEREGISTER:
return ISNS_INVALID_DEREGISTRATION;
case ISNS_DEVICE_ATTRIBUTE_QUERY:
case ISNS_DEVICE_GET_NEXT:
return ISNS_INVALID_QUERY;
}
return ISNS_ATTRIBUTE_NOT_IMPLEMENTED;
}
/*
* Debug helper: print attribute list
*/
void
isns_attr_list_print(const isns_attr_list_t *list, isns_print_fn_t *fn)
{
unsigned int i;
for (i = 0; i < list->ial_count; ++i)
isns_attr_print(list->ial_data[i], fn);
}
char *
isns_attr_print_value(const isns_attr_t *attr, char *buffer, size_t size)
{
const isns_tag_type_t *tag_type = attr->ia_tag;
const isns_attr_type_t *type = attr->ia_value.iv_type;
if (tag_type->it_print && type == tag_type->it_type)
tag_type->it_print(&attr->ia_value, buffer, size);
else
type->it_print(&attr->ia_value, buffer, size);
return buffer;
}
void
isns_attr_print(const isns_attr_t *attr, isns_print_fn_t *fn)
{
const isns_tag_type_t *tag_type = attr->ia_tag;
const isns_attr_type_t *type = attr->ia_value.iv_type;
uint32_t tag;
char value[512], *vspec = "";
tag = attr->ia_tag_id;
if (OPENISNS_IS_PRIVATE_ATTR(tag)) {
tag -= OPENISNS_VENDOR_PREFIX;
vspec = "v";
}
fn(" %04x%1s %-12s: %s = %s\n",
tag, vspec,
type->it_name,
tag_type? tag_type->it_name : "Unknown Attribute",
isns_attr_print_value(attr, value, sizeof(value)));
}
/*
* TLV encode a single attribute
*/
int
isns_attr_encode(buf_t *bp, const isns_attr_t *attr)
{
const isns_value_t *value = &attr->ia_value;
const isns_attr_type_t *type = value->iv_type;
if (!buf_put32(bp, attr->ia_tag_id)
|| !type->it_encode(bp, value))
return ISNS_INTERNAL_ERROR;
return ISNS_SUCCESS;
}
/*
* TLV decode a single attribute
*/
int
isns_attr_decode(buf_t *bp, isns_attr_t **result)
{
isns_attr_t *attr = NULL;
isns_value_t *value;
uint32_t tag, len = 0;
if (!buf_get32(bp, &tag)
|| !buf_get32(bp, &len))
goto msg_fmt_error;
/* Attributes MUST be word aligned */
if (len & 3)
goto msg_fmt_error;
if (len > ISNS_ATTR_MAX_LEN)
goto msg_fmt_error;
/* Allocate the attribute */
attr = isns_attr_alloc(tag, NULL, NULL);
value = &attr->ia_value;
if (len == 0)
value->iv_type = &isns_attr_type_nil;
if (!value->iv_type->it_decode(bp, len, value))
goto msg_fmt_error;
*result = attr;
return ISNS_SUCCESS;
msg_fmt_error:
isns_error("Error decoding attribute, tag=0x%04x, len=%u\n",
tag, len);
if (attr)
isns_attr_release(attr);
return ISNS_MESSAGE_FORMAT_ERROR;
}
/*
* Decode the list of TLV encoded attributes inside an
* iSNS message.
*/
static int
__isns_attr_list_decode(buf_t *bp, isns_attr_list_t *list, int delimited)
{
int status;
while (buf_avail(bp)) {
isns_attr_t *attr;
status = isns_attr_decode(bp, &attr);
if (status != ISNS_SUCCESS)
return status;
if (delimited && attr->ia_tag_id == ISNS_TAG_DELIMITER) {
isns_attr_release(attr);
break;
}
__isns_attr_list_append_attr(list, attr);
}
return ISNS_SUCCESS;
}
int
isns_attr_list_decode(buf_t *bp, isns_attr_list_t *list)
{
return __isns_attr_list_decode(bp, list, 0);
}
int
isns_attr_list_decode_delimited(buf_t *bp, isns_attr_list_t *list)
{
return __isns_attr_list_decode(bp, list, 1);
}
/*
* Remove all attributes from a list save those matching
* the given tags.
*/
void
isns_attr_list_prune(isns_attr_list_t *list,
const uint32_t *tags, unsigned int num_tags)
{
unsigned int i, j, k;
for (i = j = 0; i < list->ial_count; ++i) {
isns_attr_t *attr = list->ial_data[i];
for (k = 0; k < num_tags; ++k) {
if (attr->ia_tag_id == tags[k]) {
list->ial_data[j++] = attr;
goto next;
}
}
isns_attr_release(attr);
next: ;
}
list->ial_count = j;
}
/*
* TLV ecode the list of attributes to go with
* iSNS message.
*/
int
isns_attr_list_encode(buf_t *bp, const isns_attr_list_t *list)
{
unsigned int i, status = ISNS_SUCCESS;
for (i = 0; i < list->ial_count; ++i) {
struct isns_attr *attr = list->ial_data[i];
status = isns_attr_encode(bp, attr);
if (status)
break;
}
return status;
}
/*
* Encode the delimiter attribute
*/
int
isns_encode_delimiter(buf_t *bp)
{
uint32_t tag = 0, len = 0;
if (!buf_put32(bp, tag)
|| !buf_put32(bp, len))
return ISNS_INTERNAL_ERROR;
return ISNS_SUCCESS;
}
/*
* Padded encoding
*/
static inline int
isns_encode_padded(buf_t *bp, const void *ptr, size_t len)
{
if (!buf_put(bp, ptr, len))
return 0;
if ((len & 3) == 0)
return 1;
return buf_put(bp, "\0\0\0", 4 - (len & 3));
}
/*
* Helper functions to deal with portal information
*/
void
isns_portal_init(isns_portal_info_t *portal,
const struct sockaddr *saddr, int proto)
{
const struct sockaddr_in *sin;
memset(portal, 0, sizeof(*portal));
switch (saddr->sa_family) {
case AF_INET6:
portal->addr = *(const struct sockaddr_in6 *) saddr;
break;
case AF_INET:
sin = (const struct sockaddr_in *) saddr;
portal->addr.sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
portal->addr.sin6_port = sin->sin_port;
portal->addr.sin6_family = AF_INET6;
break;
default:
isns_warning("Unknown address family in isns_portal_init\n");
return;
}
portal->proto = proto;
}
int
isns_portal_from_attr_list(isns_portal_info_t *portal,
uint32_t addr_tag, uint32_t port_tag,
const isns_attr_list_t *list)
{
const isns_attr_t *addr_attr = NULL, *port_attr = NULL;
unsigned int i;
for (i = 0; i + 1 < list->ial_count; ++i) {
const isns_attr_t *attr = list->ial_data[i];
if (!ISNS_ATTR_IS_IPADDR(attr))
continue;
if (addr_tag && attr->ia_tag_id != addr_tag)
continue;
addr_attr = attr;
if (port_tag == 0) {
port_attr = list->ial_data[i + 1];
goto extract_portal;
}
break;
}
/* We have a specific port tag. */
while (++i < list->ial_count) {
const isns_attr_t *attr = list->ial_data[i];
if (attr->ia_tag_id == port_tag) {
port_attr = attr;
goto extract_portal;
}
}
return 0;
extract_portal:
return isns_portal_from_attr_pair(portal,
addr_attr, port_attr);
}
int