4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2012 Milan Jurik. All rights reserved.
31 #include <sys/types.h>
32 #include <libdlwlan.h>
33 #include <libnvpair.h>
35 #include "libnwam_impl.h"
36 #include <libnwam_priv.h>
40 * Internal implementation of libnwam in-memory objects and values. Objects
45 nwam_value_free(nwam_value_t value
)
52 switch (value
->nwv_value_type
) {
53 case NWAM_VALUE_TYPE_BOOLEAN
:
54 free(value
->nwv_values
.nwv_boolean
);
56 case NWAM_VALUE_TYPE_INT64
:
57 free(value
->nwv_values
.nwv_int64
);
59 case NWAM_VALUE_TYPE_UINT64
:
60 free(value
->nwv_values
.nwv_uint64
);
62 case NWAM_VALUE_TYPE_STRING
:
63 for (i
= 0; i
< value
->nwv_value_numvalues
; i
++)
64 free(value
->nwv_values
.nwv_string
[i
]);
65 free(value
->nwv_values
.nwv_string
);
72 nwam_value_create(nwam_value_type_t value_type
, void *values
, uint_t numvalues
,
75 nwam_value_t newvalue
;
76 boolean_t
*values_boolean
;
77 int64_t *values_int64
;
78 uint64_t *values_uint64
;
81 nwam_error_t err
= NWAM_SUCCESS
;
85 if ((newvalue
= calloc(1, sizeof (struct nwam_value
))) == NULL
)
86 return (NWAM_NO_MEMORY
);
88 newvalue
->nwv_value_type
= value_type
;
89 newvalue
->nwv_value_numvalues
= numvalues
;
92 case NWAM_VALUE_TYPE_BOOLEAN
:
93 values_boolean
= values
;
94 if ((newvalue
->nwv_values
.nwv_boolean
=
95 calloc(numvalues
, sizeof (boolean_t
))) == NULL
) {
97 return (NWAM_NO_MEMORY
);
99 for (i
= 0; i
< numvalues
; i
++)
100 newvalue
->nwv_values
.nwv_boolean
[i
] = values_boolean
[i
];
102 case NWAM_VALUE_TYPE_INT64
:
103 values_int64
= values
;
104 if ((newvalue
->nwv_values
.nwv_int64
=
105 calloc(numvalues
, sizeof (int64_t))) == NULL
) {
107 return (NWAM_NO_MEMORY
);
109 for (i
= 0; i
< numvalues
; i
++)
110 newvalue
->nwv_values
.nwv_int64
[i
] = values_int64
[i
];
112 case NWAM_VALUE_TYPE_UINT64
:
113 values_uint64
= values
;
114 if ((newvalue
->nwv_values
.nwv_uint64
=
115 calloc(numvalues
, sizeof (uint64_t))) == NULL
) {
117 return (NWAM_NO_MEMORY
);
119 for (i
= 0; i
< numvalues
; i
++)
120 newvalue
->nwv_values
.nwv_uint64
[i
] = values_uint64
[i
];
122 case NWAM_VALUE_TYPE_STRING
:
123 values_string
= values
;
124 if ((newvalue
->nwv_values
.nwv_string
=
125 calloc(numvalues
, sizeof (char *))) == NULL
) {
127 return (NWAM_NO_MEMORY
);
129 for (i
= 0; i
< numvalues
; i
++) {
130 if (strnlen(values_string
[i
], NWAM_MAX_VALUE_LEN
) ==
131 NWAM_MAX_VALUE_LEN
) {
132 err
= NWAM_ENTITY_INVALID_VALUE
;
133 } else if ((newvalue
->nwv_values
.nwv_string
[i
] =
134 strdup(values_string
[i
])) == NULL
) {
135 err
= NWAM_NO_MEMORY
;
137 if (err
!= NWAM_SUCCESS
) {
138 for (j
= 0; j
< i
; j
++)
140 newvalue
->nwv_values
.nwv_string
[i
]);
141 free(newvalue
->nwv_values
.nwv_string
);
152 return (NWAM_SUCCESS
);
156 nwam_value_copy(nwam_value_t old
, nwam_value_t
*newp
)
160 assert(old
!= NULL
&& newp
!= NULL
);
162 switch (old
->nwv_value_type
) {
163 case NWAM_VALUE_TYPE_BOOLEAN
:
164 values
= old
->nwv_values
.nwv_boolean
;
166 case NWAM_VALUE_TYPE_INT64
:
167 values
= old
->nwv_values
.nwv_int64
;
169 case NWAM_VALUE_TYPE_UINT64
:
170 values
= old
->nwv_values
.nwv_uint64
;
172 case NWAM_VALUE_TYPE_STRING
:
173 values
= old
->nwv_values
.nwv_string
;
176 return (NWAM_INVALID_ARG
);
178 return (nwam_value_create(old
->nwv_value_type
, values
,
179 old
->nwv_value_numvalues
, newp
));
182 nwam_value_create_boolean_array(boolean_t
*values
, uint_t numvalues
,
183 nwam_value_t
*valuep
)
185 return (nwam_value_create(NWAM_VALUE_TYPE_BOOLEAN
, values
, numvalues
,
190 nwam_value_create_boolean(boolean_t value
, nwam_value_t
*valuep
)
192 return (nwam_value_create_boolean_array(&value
, 1, valuep
));
196 nwam_value_create_int64_array(int64_t *values
, uint_t numvalues
,
197 nwam_value_t
*valuep
)
199 return (nwam_value_create(NWAM_VALUE_TYPE_INT64
, values
, numvalues
,
204 nwam_value_create_int64(int64_t value
, nwam_value_t
*valuep
)
206 return (nwam_value_create_int64_array(&value
, 1, valuep
));
210 nwam_value_create_uint64_array(uint64_t *values
, uint_t numvalues
,
211 nwam_value_t
*valuep
)
213 return (nwam_value_create(NWAM_VALUE_TYPE_UINT64
, values
, numvalues
,
218 nwam_value_create_uint64(uint64_t value
, nwam_value_t
*valuep
)
220 return (nwam_value_create_uint64_array(&value
, 1, valuep
));
224 nwam_value_create_string_array(char **values
, uint_t numvalues
,
225 nwam_value_t
*valuep
)
227 return (nwam_value_create(NWAM_VALUE_TYPE_STRING
, values
, numvalues
,
232 nwam_value_create_string(char *value
, nwam_value_t
*valuep
)
234 return (nwam_value_create_string_array(&value
, 1, valuep
));
238 nwam_value_get_boolean_array(nwam_value_t value
, boolean_t
**valuesp
,
241 assert(value
!= NULL
&& numvaluesp
!= NULL
&& valuesp
!= NULL
);
243 *numvaluesp
= value
->nwv_value_numvalues
;
244 *valuesp
= value
->nwv_values
.nwv_boolean
;
245 return (NWAM_SUCCESS
);
249 nwam_value_get_boolean(nwam_value_t value
, boolean_t
*valuep
)
252 boolean_t
*myvaluesp
;
255 err
= nwam_value_get_boolean_array(value
, &myvaluesp
, &numvalues
);
256 if (err
!= NWAM_SUCCESS
)
259 return (NWAM_ENTITY_MULTIPLE_VALUES
);
261 *valuep
= myvaluesp
[0];
262 return (NWAM_SUCCESS
);
266 nwam_value_get_int64_array(nwam_value_t value
, int64_t **valuesp
,
269 assert(value
!= NULL
&& numvaluesp
!= NULL
&& valuesp
!= NULL
);
271 *numvaluesp
= value
->nwv_value_numvalues
;
272 *valuesp
= value
->nwv_values
.nwv_int64
;
273 return (NWAM_SUCCESS
);
277 nwam_value_get_int64(nwam_value_t value
, int64_t *valuep
)
283 err
= nwam_value_get_int64_array(value
, &myvaluesp
, &numvalues
);
284 if (err
!= NWAM_SUCCESS
)
287 return (NWAM_ENTITY_MULTIPLE_VALUES
);
289 *valuep
= myvaluesp
[0];
290 return (NWAM_SUCCESS
);
294 nwam_value_get_uint64_array(nwam_value_t value
, uint64_t **valuesp
,
297 assert(value
!= NULL
&& numvaluesp
!= NULL
&& valuesp
!= NULL
);
299 *numvaluesp
= value
->nwv_value_numvalues
;
300 *valuesp
= value
->nwv_values
.nwv_uint64
;
301 return (NWAM_SUCCESS
);
305 nwam_value_get_uint64(nwam_value_t value
, uint64_t *valuep
)
311 err
= nwam_value_get_uint64_array(value
, &myvaluesp
, &numvalues
);
312 if (err
!= NWAM_SUCCESS
)
315 return (NWAM_ENTITY_MULTIPLE_VALUES
);
317 *valuep
= myvaluesp
[0];
318 return (NWAM_SUCCESS
);
322 nwam_value_get_string_array(nwam_value_t value
, char ***valuesp
,
325 assert(value
!= NULL
&& numvaluesp
!= NULL
&& valuesp
!= NULL
);
327 *numvaluesp
= value
->nwv_value_numvalues
;
328 *valuesp
= value
->nwv_values
.nwv_string
;
329 return (NWAM_SUCCESS
);
333 nwam_value_get_string(nwam_value_t value
, char **valuep
)
339 err
= nwam_value_get_string_array(value
, &myvaluesp
, &numvalues
);
340 if (err
!= NWAM_SUCCESS
)
343 return (NWAM_ENTITY_MULTIPLE_VALUES
);
345 *valuep
= myvaluesp
[0];
346 return (NWAM_SUCCESS
);
350 nwam_value_get_type(nwam_value_t value
, nwam_value_type_t
*typep
)
352 *typep
= value
->nwv_value_type
;
353 return (NWAM_SUCCESS
);
357 nwam_value_get_numvalues(nwam_value_t value
, uint_t
*numvaluesp
)
359 *numvaluesp
= value
->nwv_value_numvalues
;
360 return (NWAM_SUCCESS
);
364 * Generic object data functions. We hide nvlist implementation
365 * from NCP, ENM and location implementations.
368 nwam_alloc_object_list(void *list
)
372 assert(list
!= NULL
);
374 if ((nverr
= nvlist_alloc((nvlist_t
**)list
, NV_UNIQUE_NAME
, 0)) != 0)
375 return (nwam_errno_to_nwam_error(nverr
));
377 return (NWAM_SUCCESS
);
381 nwam_free_object_list(void *list
)
387 nwam_dup_object_list(void *oldlist
, void *newlist
)
391 assert(oldlist
!= NULL
&& newlist
!= NULL
);
393 if ((nverr
= nvlist_dup(oldlist
, newlist
, 0)) != 0)
394 return (nwam_errno_to_nwam_error(nverr
));
396 return (NWAM_SUCCESS
);
399 /* Add child object list to parent object list using property name childname */
401 nwam_object_list_add_object_list(void *parentlist
, char *childname
,
404 return (nwam_errno_to_nwam_error(nvlist_add_nvlist(parentlist
,
405 childname
, childlist
)));
408 /* Remove object list from parent object list */
410 nwam_object_list_remove_object_list(void *parentlist
, char *childname
)
412 return (nwam_errno_to_nwam_error(nvlist_remove_all(parentlist
,
417 * Get next object list (nvlist) after lastname. Used to walk NCUs, ENMs and
418 * locations, each of which is internally represented as an nvlist.
421 nwam_next_object_list(void *parentlist
, char *lastname
, char **childnamep
,
424 nvpair_t
*last
= NULL
, *next
;
427 if (lastname
!= NULL
) {
428 if ((nverr
= nvlist_lookup_nvpair(parentlist
, lastname
, &last
))
430 return (nwam_errno_to_nwam_error(nverr
));
432 if ((next
= nvlist_next_nvpair(parentlist
, last
)) == NULL
)
433 return (NWAM_LIST_END
);
435 *childnamep
= nvpair_name(next
);
437 if (nvpair_type(next
) != DATA_TYPE_NVLIST
)
438 return (NWAM_ERROR_INTERNAL
);
440 if ((nverr
= nvpair_value_nvlist(next
, childlistp
)) != NWAM_SUCCESS
)
441 return (nwam_errno_to_nwam_error(nverr
));
443 return (NWAM_SUCCESS
);
447 * Pack nvlist into contiguous memory. If packed_listp is NULL, we just
448 * return the size of the memory needed to do so.
451 nwam_pack_object_list(void *list
, char **packed_listp
, size_t *packed_sizep
)
455 assert(list
!= NULL
&& packed_sizep
!= NULL
);
457 if (packed_listp
== NULL
) {
458 nverr
= nvlist_size(list
, packed_sizep
, NV_ENCODE_XDR
);
460 nverr
= nvlist_pack(list
, packed_listp
, packed_sizep
,
465 return (nwam_errno_to_nwam_error(nverr
));
467 return (NWAM_SUCCESS
);
471 nwam_unpack_object_list(char *packed_list
, size_t packed_size
,
476 assert(packed_list
!= NULL
&& list
!= NULL
);
478 *((nvlist_t
**)list
) = NULL
;
480 nverr
= nvlist_unpack(packed_list
, packed_size
, (nvlist_t
**)list
, 0);
483 return (nwam_errno_to_nwam_error(nverr
));
485 return (NWAM_SUCCESS
);
489 * Functions to walk, set and get properties in nvlist, translating
490 * between nwam_value_t and nvlist/nvpair representations.
493 nwam_next_object_prop(void *list
, char *lastname
, char **namep
,
494 nwam_value_t
*valuep
)
496 nvpair_t
*last
= NULL
, *next
;
499 if (lastname
!= NULL
) {
500 if ((nverr
= nvlist_lookup_nvpair(list
, lastname
, &last
)) != 0)
501 return (nwam_errno_to_nwam_error(nverr
));
503 if ((next
= nvlist_next_nvpair(list
, last
)) == NULL
)
504 return (NWAM_LIST_END
);
506 *namep
= nvpair_name(next
);
508 return (nwam_get_prop_value(list
, (const char *)*namep
, valuep
));
512 nwam_get_prop_value(void *list
, const char *name
, nwam_value_t
*valuep
)
523 assert(valuep
!= NULL
);
527 if ((nverr
= nvlist_lookup_nvpair(list
, name
, &prop
)) != 0) {
528 /* convert EINVAL to NOT_FOUND */
530 return (NWAM_ENTITY_NOT_FOUND
);
531 return (nwam_errno_to_nwam_error(nverr
));
534 switch (nvpair_type(prop
)) {
535 case DATA_TYPE_BOOLEAN_ARRAY
:
536 if ((nverr
= nvpair_value_boolean_array(prop
,
537 &valbool
, &numvalues
)) != 0)
538 return (nwam_errno_to_nwam_error(nverr
));
539 if ((err
= nwam_value_create_boolean_array(valbool
, numvalues
,
540 valuep
)) != NWAM_SUCCESS
)
543 case DATA_TYPE_INT64_ARRAY
:
544 if ((nverr
= nvpair_value_int64_array(prop
,
545 &valint64
, &numvalues
)) != 0)
546 return (nwam_errno_to_nwam_error(nverr
));
547 if ((err
= nwam_value_create_int64_array(valint64
, numvalues
,
548 valuep
)) != NWAM_SUCCESS
)
551 case DATA_TYPE_UINT64_ARRAY
:
552 if ((nverr
= nvpair_value_uint64_array(prop
,
553 &valuint64
, &numvalues
)) != 0)
554 return (nwam_errno_to_nwam_error(nverr
));
555 if ((err
= nwam_value_create_uint64_array(valuint64
, numvalues
,
556 valuep
)) != NWAM_SUCCESS
)
559 case DATA_TYPE_STRING_ARRAY
:
560 if ((nverr
= nvpair_value_string_array(prop
,
561 &valstr
, &numvalues
)) != 0)
562 return (nwam_errno_to_nwam_error(nverr
));
563 if ((err
= nwam_value_create_string_array(valstr
, numvalues
,
564 valuep
)) != NWAM_SUCCESS
)
568 /* Should not happen */
569 return (NWAM_ERROR_INTERNAL
);
571 return (NWAM_SUCCESS
);
575 nwam_delete_prop(void *list
, const char *name
)
579 if ((nverr
= nvlist_remove_all(list
, name
)) != 0)
580 return (nwam_errno_to_nwam_error(nverr
));
581 return (NWAM_SUCCESS
);
585 nwam_set_prop_value(void *list
, const char *propname
, nwam_value_t value
)
589 nwam_value_type_t type
;
596 assert(list
!= NULL
&& value
!= NULL
);
598 if ((err
= nwam_value_get_type(value
, &type
)) != NWAM_SUCCESS
)
602 case NWAM_VALUE_TYPE_BOOLEAN
:
603 if ((err
= nwam_value_get_boolean_array(value
, &valbool
,
604 &numvalues
)) != NWAM_SUCCESS
)
606 if ((nverr
= nvlist_add_boolean_array(list
, propname
,
607 valbool
, numvalues
)) != 0)
608 return (nwam_errno_to_nwam_error(nverr
));
610 case NWAM_VALUE_TYPE_INT64
:
611 if ((err
= nwam_value_get_int64_array(value
, &valint64
,
612 &numvalues
)) != NWAM_SUCCESS
)
614 if ((nverr
= nvlist_add_int64_array(list
, propname
,
615 valint64
, numvalues
)) != 0)
616 return (nwam_errno_to_nwam_error(nverr
));
618 case NWAM_VALUE_TYPE_UINT64
:
619 if ((err
= nwam_value_get_uint64_array(value
, &valuint64
,
620 &numvalues
)) != NWAM_SUCCESS
)
622 if ((nverr
= nvlist_add_uint64_array(list
, propname
,
623 valuint64
, numvalues
)) != 0)
624 return (nwam_errno_to_nwam_error(nverr
));
626 case NWAM_VALUE_TYPE_STRING
:
627 if ((err
= nwam_value_get_string_array(value
, &valstr
,
628 &numvalues
)) != NWAM_SUCCESS
)
630 if ((nverr
= nvlist_add_string_array(list
, propname
,
631 valstr
, numvalues
)) != 0)
632 return (nwam_errno_to_nwam_error(nverr
));
635 return (NWAM_INVALID_ARG
);
638 return (NWAM_SUCCESS
);
641 /* Map uint64 values to their string counterparts */
643 struct nwam_value_entry
{
644 const char *value_string
;
648 struct nwam_value_entry prop_activation_mode_value_entries
[] =
650 { NWAM_ACTIVATION_MODE_MANUAL_STRING
, NWAM_ACTIVATION_MODE_MANUAL
},
651 { NWAM_ACTIVATION_MODE_SYSTEM_STRING
, NWAM_ACTIVATION_MODE_SYSTEM
},
652 { NWAM_ACTIVATION_MODE_CONDITIONAL_ANY_STRING
,
653 NWAM_ACTIVATION_MODE_CONDITIONAL_ANY
},
654 { NWAM_ACTIVATION_MODE_CONDITIONAL_ALL_STRING
,
655 NWAM_ACTIVATION_MODE_CONDITIONAL_ALL
},
656 { NWAM_ACTIVATION_MODE_PRIORITIZED_STRING
,
657 NWAM_ACTIVATION_MODE_PRIORITIZED
},
661 struct nwam_value_entry ncu_prop_type_entries
[] =
663 { NWAM_NCU_TYPE_LINK_STRING
, NWAM_NCU_TYPE_LINK
},
664 { NWAM_NCU_TYPE_INTERFACE_STRING
, NWAM_NCU_TYPE_INTERFACE
},
668 struct nwam_value_entry ncu_prop_class_entries
[] =
670 { NWAM_NCU_CLASS_PHYS_STRING
, NWAM_NCU_CLASS_PHYS
},
671 { NWAM_NCU_CLASS_IP_STRING
, NWAM_NCU_CLASS_IP
},
675 struct nwam_value_entry ncu_prop_ip_version_entries
[] =
677 { NWAM_IP_VERSION_IPV4_STRING
, IPV4_VERSION
},
678 { NWAM_IP_VERSION_IPV6_STRING
, IPV6_VERSION
},
682 struct nwam_value_entry ncu_prop_ipv4_addrsrc_entries
[] =
684 { NWAM_ADDRSRC_DHCP_STRING
, NWAM_ADDRSRC_DHCP
},
685 { NWAM_ADDRSRC_STATIC_STRING
, NWAM_ADDRSRC_STATIC
},
689 struct nwam_value_entry ncu_prop_ipv6_addrsrc_entries
[] =
691 { NWAM_ADDRSRC_DHCP_STRING
, NWAM_ADDRSRC_DHCP
},
692 { NWAM_ADDRSRC_STATIC_STRING
, NWAM_ADDRSRC_STATIC
},
693 { NWAM_ADDRSRC_AUTOCONF_STRING
, NWAM_ADDRSRC_AUTOCONF
},
697 struct nwam_value_entry ncu_prop_priority_mode_entries
[] =
699 { NWAM_PRIORITY_MODE_EXCLUSIVE_STRING
, NWAM_PRIORITY_MODE_EXCLUSIVE
},
700 { NWAM_PRIORITY_MODE_SHARED_STRING
, NWAM_PRIORITY_MODE_SHARED
},
701 { NWAM_PRIORITY_MODE_ALL_STRING
, NWAM_PRIORITY_MODE_ALL
},
705 struct nwam_value_entry loc_prop_nameservices_entries
[] =
707 { NWAM_NAMESERVICES_DNS_STRING
, NWAM_NAMESERVICES_DNS
},
708 { NWAM_NAMESERVICES_FILES_STRING
, NWAM_NAMESERVICES_FILES
},
709 { NWAM_NAMESERVICES_NIS_STRING
, NWAM_NAMESERVICES_NIS
},
710 { NWAM_NAMESERVICES_LDAP_STRING
, NWAM_NAMESERVICES_LDAP
},
714 struct nwam_value_entry loc_prop_nameservice_configsrc_entries
[] =
716 { NWAM_CONFIGSRC_MANUAL_STRING
, NWAM_CONFIGSRC_MANUAL
},
717 { NWAM_CONFIGSRC_DHCP_STRING
, NWAM_CONFIGSRC_DHCP
},
721 struct nwam_value_entry known_wlan_prop_security_mode_entries
[] =
723 { "none", DLADM_WLAN_SECMODE_NONE
},
724 { "wep", DLADM_WLAN_SECMODE_WEP
},
725 { "wpa", DLADM_WLAN_SECMODE_WPA
},
729 struct nwam_prop_value_entry
{
730 const char *prop_name
;
731 struct nwam_value_entry
*value_entries
;
732 } prop_value_entry_table
[] =
734 { NWAM_NCU_PROP_ACTIVATION_MODE
, prop_activation_mode_value_entries
},
735 { NWAM_NCU_PROP_TYPE
, ncu_prop_type_entries
},
736 { NWAM_NCU_PROP_CLASS
, ncu_prop_class_entries
},
737 { NWAM_NCU_PROP_IP_VERSION
, ncu_prop_ip_version_entries
},
738 { NWAM_NCU_PROP_IPV4_ADDRSRC
, ncu_prop_ipv4_addrsrc_entries
},
739 { NWAM_NCU_PROP_IPV6_ADDRSRC
, ncu_prop_ipv6_addrsrc_entries
},
740 { NWAM_NCU_PROP_PRIORITY_MODE
, ncu_prop_priority_mode_entries
},
741 { NWAM_ENM_PROP_ACTIVATION_MODE
, prop_activation_mode_value_entries
},
742 { NWAM_LOC_PROP_ACTIVATION_MODE
, prop_activation_mode_value_entries
},
743 { NWAM_LOC_PROP_NAMESERVICES
, loc_prop_nameservices_entries
},
744 { NWAM_LOC_PROP_DNS_NAMESERVICE_CONFIGSRC
,
745 loc_prop_nameservice_configsrc_entries
},
746 { NWAM_LOC_PROP_NIS_NAMESERVICE_CONFIGSRC
,
747 loc_prop_nameservice_configsrc_entries
},
748 { NWAM_LOC_PROP_LDAP_NAMESERVICE_CONFIGSRC
,
749 loc_prop_nameservice_configsrc_entries
},
750 { NWAM_KNOWN_WLAN_PROP_SECURITY_MODE
,
751 known_wlan_prop_security_mode_entries
},
756 * Convert uint64 values for property propname into a string representing
757 * that value. Used by enum values.
760 nwam_uint64_get_value_string(const char *propname
, uint64_t val
,
761 const char **valstrp
)
764 int max
= 0; /* largest enum value seen so far */
765 struct nwam_value_entry
*value_entries
;
767 assert(propname
!= NULL
&& valstrp
!= NULL
);
769 for (i
= 0; prop_value_entry_table
[i
].prop_name
!= NULL
; i
++) {
770 if (strcmp(prop_value_entry_table
[i
].prop_name
, propname
) != 0)
773 value_entries
= prop_value_entry_table
[i
].value_entries
;
775 for (j
= 0; value_entries
[j
].value_string
!= NULL
; j
++) {
776 if (value_entries
[j
].value
== val
) {
777 *valstrp
= value_entries
[j
].value_string
;
778 return (NWAM_SUCCESS
);
780 max
= value_entries
[j
].value
> max
?
781 value_entries
[j
].value
: max
;
784 * If trying to get the string for an enum value that doesn't
785 * exist, return NWAM_LIST_END. Otherwise, the input enum
786 * value doesn't exist for the given property.
789 return (NWAM_LIST_END
);
791 return (NWAM_ENTITY_INVALID_VALUE
);
793 return (NWAM_INVALID_ARG
);
797 * Convert string to appropriate uint64 value.
800 nwam_value_string_get_uint64(const char *propname
, const char *valstr
,
804 struct nwam_value_entry
*value_entries
;
806 assert(propname
!= NULL
&& valstr
!= NULL
&& valp
!= NULL
);
808 for (i
= 0; prop_value_entry_table
[i
].prop_name
!= NULL
; i
++) {
809 if (strcmp(prop_value_entry_table
[i
].prop_name
, propname
) != 0)
812 value_entries
= prop_value_entry_table
[i
].value_entries
;
814 for (j
= 0; value_entries
[j
].value_string
!= NULL
; j
++) {
815 if (strcasecmp(value_entries
[j
].value_string
, valstr
)
817 *valp
= value_entries
[j
].value
;
818 return (NWAM_SUCCESS
);
821 return (NWAM_ENTITY_INVALID_VALUE
);
823 return (NWAM_INVALID_ARG
);
826 /* Conditional activation functions */
829 nwam_condition_to_condition_string(nwam_condition_object_type_t object_type
,
830 nwam_condition_t condition
, const char *object_name
, char **stringp
)
832 char *object_type_string
, *condition_string
;
835 assert(stringp
!= NULL
);
839 switch (object_type
) {
840 case NWAM_CONDITION_OBJECT_TYPE_NCP
:
841 object_type_string
= NWAM_CONDITION_OBJECT_TYPE_NCP_STRING
;
843 case NWAM_CONDITION_OBJECT_TYPE_NCU
:
844 object_type_string
= NWAM_CONDITION_OBJECT_TYPE_NCU_STRING
;
846 case NWAM_CONDITION_OBJECT_TYPE_ENM
:
847 object_type_string
= NWAM_CONDITION_OBJECT_TYPE_ENM_STRING
;
849 case NWAM_CONDITION_OBJECT_TYPE_LOC
:
850 object_type_string
= NWAM_CONDITION_OBJECT_TYPE_LOC_STRING
;
852 case NWAM_CONDITION_OBJECT_TYPE_IP_ADDRESS
:
854 NWAM_CONDITION_OBJECT_TYPE_IP_ADDRESS_STRING
;
856 case NWAM_CONDITION_OBJECT_TYPE_ADV_DOMAIN
:
858 NWAM_CONDITION_OBJECT_TYPE_ADV_DOMAIN_STRING
;
860 case NWAM_CONDITION_OBJECT_TYPE_SYS_DOMAIN
:
862 NWAM_CONDITION_OBJECT_TYPE_SYS_DOMAIN_STRING
;
864 case NWAM_CONDITION_OBJECT_TYPE_ESSID
:
865 object_type_string
= NWAM_CONDITION_OBJECT_TYPE_ESSID_STRING
;
867 case NWAM_CONDITION_OBJECT_TYPE_BSSID
:
868 object_type_string
= NWAM_CONDITION_OBJECT_TYPE_BSSID_STRING
;
871 return (NWAM_INVALID_ARG
);
875 case NWAM_CONDITION_IS
:
876 condition_string
= NWAM_CONDITION_IS_STRING
;
878 case NWAM_CONDITION_IS_NOT
:
879 condition_string
= NWAM_CONDITION_IS_NOT_STRING
;
881 case NWAM_CONDITION_CONTAINS
:
882 if (object_type
!= NWAM_CONDITION_OBJECT_TYPE_SYS_DOMAIN
&&
883 object_type
!= NWAM_CONDITION_OBJECT_TYPE_ADV_DOMAIN
&&
884 object_type
!= NWAM_CONDITION_OBJECT_TYPE_ESSID
)
885 return (NWAM_INVALID_ARG
);
886 condition_string
= NWAM_CONDITION_CONTAINS_STRING
;
888 case NWAM_CONDITION_DOES_NOT_CONTAIN
:
889 if (object_type
!= NWAM_CONDITION_OBJECT_TYPE_SYS_DOMAIN
&&
890 object_type
!= NWAM_CONDITION_OBJECT_TYPE_ADV_DOMAIN
&&
891 object_type
!= NWAM_CONDITION_OBJECT_TYPE_ESSID
)
892 return (NWAM_INVALID_ARG
);
894 condition_string
= NWAM_CONDITION_DOES_NOT_CONTAIN_STRING
;
896 case NWAM_CONDITION_IS_IN_RANGE
:
897 if (object_type
!= NWAM_CONDITION_OBJECT_TYPE_IP_ADDRESS
)
898 return (NWAM_INVALID_ARG
);
899 condition_string
= NWAM_CONDITION_IS_IN_RANGE_STRING
;
901 case NWAM_CONDITION_IS_NOT_IN_RANGE
:
902 if (object_type
!= NWAM_CONDITION_OBJECT_TYPE_IP_ADDRESS
)
903 return (NWAM_INVALID_ARG
);
904 condition_string
= NWAM_CONDITION_IS_NOT_IN_RANGE_STRING
;
907 return (NWAM_INVALID_ARG
);
909 if ((string
= malloc(NWAM_MAX_VALUE_LEN
)) == NULL
)
910 return (NWAM_NO_MEMORY
);
911 switch (object_type
) {
912 case NWAM_CONDITION_OBJECT_TYPE_NCP
:
913 case NWAM_CONDITION_OBJECT_TYPE_NCU
:
914 case NWAM_CONDITION_OBJECT_TYPE_ENM
:
915 case NWAM_CONDITION_OBJECT_TYPE_LOC
:
916 (void) snprintf(string
, NWAM_MAX_VALUE_LEN
,
917 "%s %s %s active", object_type_string
,
918 object_name
, condition_string
);
922 case NWAM_CONDITION_OBJECT_TYPE_IP_ADDRESS
:
923 case NWAM_CONDITION_OBJECT_TYPE_ADV_DOMAIN
:
924 case NWAM_CONDITION_OBJECT_TYPE_SYS_DOMAIN
:
925 case NWAM_CONDITION_OBJECT_TYPE_ESSID
:
926 case NWAM_CONDITION_OBJECT_TYPE_BSSID
:
927 (void) snprintf(string
, NWAM_MAX_VALUE_LEN
,
928 "%s %s %s", object_type_string
,
929 condition_string
, object_name
);
935 return (NWAM_INVALID_ARG
);
938 return (NWAM_SUCCESS
);
942 nwam_condition_string_to_condition(const char *string
,
943 nwam_condition_object_type_t
*object_typep
,
944 nwam_condition_t
*conditionp
, char **object_namep
)
947 char *object_type_string
, *object_name
;
948 char *condition_string
, *active_string
;
950 assert(string
!= NULL
&& object_typep
!= NULL
&& conditionp
!= NULL
&&
951 object_namep
!= NULL
);
953 if ((copy
= strdup(string
)) == NULL
)
954 return (NWAM_NO_MEMORY
);
956 if ((object_type_string
= strtok_r(copy
, " \t", &lasts
)) == NULL
) {
958 return (NWAM_INVALID_ARG
);
961 if (strcmp(object_type_string
, NWAM_CONDITION_OBJECT_TYPE_NCP_STRING
)
963 *object_typep
= NWAM_CONDITION_OBJECT_TYPE_NCP
;
964 else if (strcmp(object_type_string
,
965 NWAM_CONDITION_OBJECT_TYPE_NCU_STRING
) == 0)
966 *object_typep
= NWAM_CONDITION_OBJECT_TYPE_NCU
;
967 else if (strcmp(object_type_string
,
968 NWAM_CONDITION_OBJECT_TYPE_ENM_STRING
) == 0)
969 *object_typep
= NWAM_CONDITION_OBJECT_TYPE_ENM
;
970 else if (strcmp(object_type_string
,
971 NWAM_CONDITION_OBJECT_TYPE_LOC_STRING
) == 0)
972 *object_typep
= NWAM_CONDITION_OBJECT_TYPE_LOC
;
973 else if (strcmp(object_type_string
,
974 NWAM_CONDITION_OBJECT_TYPE_IP_ADDRESS_STRING
) == 0)
975 *object_typep
= NWAM_CONDITION_OBJECT_TYPE_IP_ADDRESS
;
976 else if (strcmp(object_type_string
,
977 NWAM_CONDITION_OBJECT_TYPE_ADV_DOMAIN_STRING
) == 0)
978 *object_typep
= NWAM_CONDITION_OBJECT_TYPE_ADV_DOMAIN
;
979 else if (strcmp(object_type_string
,
980 NWAM_CONDITION_OBJECT_TYPE_SYS_DOMAIN_STRING
) == 0)
981 *object_typep
= NWAM_CONDITION_OBJECT_TYPE_SYS_DOMAIN
;
982 else if (strcmp(object_type_string
,
983 NWAM_CONDITION_OBJECT_TYPE_ESSID_STRING
) == 0)
984 *object_typep
= NWAM_CONDITION_OBJECT_TYPE_ESSID
;
985 else if (strcmp(object_type_string
,
986 NWAM_CONDITION_OBJECT_TYPE_BSSID_STRING
) == 0)
987 *object_typep
= NWAM_CONDITION_OBJECT_TYPE_BSSID
;
990 return (NWAM_INVALID_ARG
);
993 if (*object_typep
== NWAM_CONDITION_OBJECT_TYPE_NCP
||
994 *object_typep
== NWAM_CONDITION_OBJECT_TYPE_NCU
||
995 *object_typep
== NWAM_CONDITION_OBJECT_TYPE_ENM
||
996 *object_typep
== NWAM_CONDITION_OBJECT_TYPE_LOC
) {
997 if ((object_name
= strtok_r(NULL
, " \t", &lasts
)) == NULL
) {
999 return (NWAM_INVALID_ARG
);
1001 if ((*object_namep
= strdup(object_name
)) == NULL
) {
1003 return (NWAM_NO_MEMORY
);
1008 if ((condition_string
= strtok_r(NULL
, " \t", &lasts
)) == NULL
) {
1010 if (*object_namep
!= NULL
)
1011 free(*object_namep
);
1012 return (NWAM_INVALID_ARG
);
1014 if (strcmp(condition_string
, NWAM_CONDITION_IS_STRING
) == 0)
1015 *conditionp
= NWAM_CONDITION_IS
;
1016 else if (strcmp(condition_string
, NWAM_CONDITION_IS_NOT_STRING
) == 0)
1017 *conditionp
= NWAM_CONDITION_IS_NOT
;
1018 else if (strcmp(condition_string
, NWAM_CONDITION_CONTAINS_STRING
) == 0)
1019 *conditionp
= NWAM_CONDITION_CONTAINS
;
1020 else if (strcmp(condition_string
,
1021 NWAM_CONDITION_DOES_NOT_CONTAIN_STRING
) == 0)
1022 *conditionp
= NWAM_CONDITION_DOES_NOT_CONTAIN
;
1023 else if (strcmp(condition_string
,
1024 NWAM_CONDITION_IS_IN_RANGE_STRING
) == 0)
1025 *conditionp
= NWAM_CONDITION_IS_IN_RANGE
;
1026 else if (strcmp(condition_string
,
1027 NWAM_CONDITION_IS_NOT_IN_RANGE_STRING
) == 0)
1028 *conditionp
= NWAM_CONDITION_IS_NOT_IN_RANGE
;
1031 if (*object_namep
!= NULL
)
1032 free(*object_namep
);
1033 return (NWAM_INVALID_ARG
);
1036 if (*object_typep
== NWAM_CONDITION_OBJECT_TYPE_NCP
||
1037 *object_typep
== NWAM_CONDITION_OBJECT_TYPE_NCU
||
1038 *object_typep
== NWAM_CONDITION_OBJECT_TYPE_ENM
||
1039 *object_typep
== NWAM_CONDITION_OBJECT_TYPE_LOC
) {
1040 if ((*conditionp
!= NWAM_CONDITION_IS
&&
1041 *conditionp
!= NWAM_CONDITION_IS_NOT
) ||
1042 (active_string
= strtok_r(NULL
, " \t", &lasts
)) == NULL
||
1043 strcmp(active_string
, NWAM_CONDITION_ACTIVE_STRING
) != 0) {
1045 free(*object_namep
);
1046 return (NWAM_INVALID_ARG
);
1049 switch (*conditionp
) {
1050 case NWAM_CONDITION_CONTAINS
:
1051 case NWAM_CONDITION_DOES_NOT_CONTAIN
:
1052 if (*object_typep
!=
1053 NWAM_CONDITION_OBJECT_TYPE_ADV_DOMAIN
&&
1055 NWAM_CONDITION_OBJECT_TYPE_SYS_DOMAIN
&&
1056 *object_typep
!= NWAM_CONDITION_OBJECT_TYPE_ESSID
) {
1058 free(*object_namep
);
1059 return (NWAM_INVALID_ARG
);
1062 case NWAM_CONDITION_IS_IN_RANGE
:
1063 case NWAM_CONDITION_IS_NOT_IN_RANGE
:
1064 if (*object_typep
!=
1065 NWAM_CONDITION_OBJECT_TYPE_IP_ADDRESS
) {
1067 free(*object_namep
);
1068 return (NWAM_INVALID_ARG
);
1073 if ((object_name
= strtok_r(NULL
, " \t", &lasts
)) == NULL
) {
1075 free(*object_namep
);
1076 return (NWAM_INVALID_ARG
);
1078 if ((*object_namep
= strdup(object_name
)) == NULL
) {
1080 free(*object_namep
);
1081 return (NWAM_NO_MEMORY
);
1086 return (NWAM_SUCCESS
);
1090 nwam_condition_rate(nwam_condition_object_type_t object_type
,
1091 nwam_condition_t condition
, uint64_t *ratep
)
1093 assert(ratep
!= NULL
);
1097 switch (object_type
) {
1098 case NWAM_CONDITION_OBJECT_TYPE_NCP
:
1099 case NWAM_CONDITION_OBJECT_TYPE_NCU
:
1100 case NWAM_CONDITION_OBJECT_TYPE_ENM
:
1101 case NWAM_CONDITION_OBJECT_TYPE_LOC
:
1104 case NWAM_CONDITION_OBJECT_TYPE_SYS_DOMAIN
:
1107 case NWAM_CONDITION_OBJECT_TYPE_ADV_DOMAIN
:
1110 case NWAM_CONDITION_OBJECT_TYPE_IP_ADDRESS
:
1113 case NWAM_CONDITION_OBJECT_TYPE_BSSID
:
1116 case NWAM_CONDITION_OBJECT_TYPE_ESSID
:
1120 return (NWAM_INVALID_ARG
);
1123 switch (condition
) {
1124 case NWAM_CONDITION_IS
:
1127 case NWAM_CONDITION_CONTAINS
:
1128 case NWAM_CONDITION_IS_IN_RANGE
:
1131 case NWAM_CONDITION_DOES_NOT_CONTAIN
:
1132 case NWAM_CONDITION_IS_NOT_IN_RANGE
:
1135 case NWAM_CONDITION_IS_NOT
:
1139 return (NWAM_INVALID_ARG
);
1141 return (NWAM_SUCCESS
);