blob: de7b4bc5605ab24c503d560e5c06714d3b734c3c [file] [log] [blame]
Neels Hofmeyrc79bcde2019-12-04 01:04:32 +01001/* Copyright 2019 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
2 *
3 * All Rights Reserved
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <errno.h>
20#include <string.h>
21#include <osmocom/core/utils.h>
22#include <osmocom/gsupclient/cni_peer_id.h>
23
24bool osmo_ipa_name_is_empty(const struct osmo_ipa_name *ipa_name)
25{
26 return (!ipa_name) || (!ipa_name->len);
27}
28
29int osmo_ipa_name_set(struct osmo_ipa_name *ipa_name, const uint8_t *val, size_t len)
30{
31 if (!val || !len) {
32 *ipa_name = (struct osmo_ipa_name){};
33 return 0;
34 }
35 if (len > sizeof(ipa_name->val))
36 return -ENOSPC;
37 ipa_name->len = len;
38 memcpy(ipa_name->val, val, len);
39 return 0;
40}
41
42static int osmo_ipa_name_set_str_va(struct osmo_ipa_name *ipa_name, const char *str_fmt, va_list ap)
43{
44 if (!str_fmt)
45 return osmo_ipa_name_set(ipa_name, NULL, 0);
46 vsnprintf((char*)(ipa_name->val), sizeof(ipa_name->val), str_fmt, ap);
47 ipa_name->len = strlen((char*)(ipa_name->val))+1;
48 return 0;
49}
50
51int osmo_ipa_name_set_str(struct osmo_ipa_name *ipa_name, const char *str_fmt, ...)
52{
53 va_list ap;
54 int rc;
55 va_start(ap, str_fmt);
56 rc = osmo_ipa_name_set_str_va(ipa_name, str_fmt, ap);
57 va_end(ap);
58 return rc;
59}
60
61int osmo_ipa_name_cmp(const struct osmo_ipa_name *a, const struct osmo_ipa_name *b)
62{
63 int cmp;
64 if (a == b)
65 return 0;
66 if (!a)
67 return -1;
68 if (!b)
69 return 1;
70 if (!a->len && !b->len)
71 return 0;
72 if (!a->len && b->len)
73 return -1;
74 if (!b->len && a->len)
75 return 1;
76
77 if (a->len == b->len)
78 return memcmp(a->val, b->val, a->len);
79 else if (a->len < b->len) {
80 cmp = memcmp(a->val, b->val, a->len);
81 if (!cmp)
82 cmp = -1;
83 return cmp;
84 } else {
85 /* a->len > b->len */
86 cmp = memcmp(a->val, b->val, b->len);
87 if (!cmp)
88 cmp = 1;
89 return cmp;
90 }
91}
92
93/* Call osmo_ipa_name_to_str_c with OTC_SELECT. */
94const char *osmo_ipa_name_to_str(const struct osmo_ipa_name *ipa_name)
95{
96 return osmo_ipa_name_to_str_c(OTC_SELECT, ipa_name);
97}
98
99/* Return an unquoted string, not including the terminating zero. Used for writing VTY config. */
100const char *osmo_ipa_name_to_str_c(void *ctx, const struct osmo_ipa_name *ipa_name)
101{
102 size_t len = ipa_name->len;
103 if (!len)
104 return talloc_strdup(ctx, "");
105 if (ipa_name->val[len-1] == '\0')
106 len--;
107 return osmo_escape_str_c(ctx, (char*)ipa_name->val, len);
108}
109
110bool osmo_cni_peer_id_is_empty(const struct osmo_cni_peer_id *cni_peer_id)
111{
112 if (!cni_peer_id)
113 return true;
114 switch (cni_peer_id->type) {
115 case OSMO_CNI_PEER_ID_EMPTY:
116 return true;
117 case OSMO_CNI_PEER_ID_IPA_NAME:
118 return osmo_ipa_name_is_empty(&cni_peer_id->ipa_name);
119 default:
120 return false;
121 }
122}
123int osmo_cni_peer_id_set(struct osmo_cni_peer_id *cni_peer_id, enum osmo_cni_peer_id_type type,
124 const uint8_t *val, size_t len)
125{
126 cni_peer_id->type = type;
127 switch (type) {
128 case OSMO_CNI_PEER_ID_IPA_NAME:
129 return osmo_ipa_name_set(&cni_peer_id->ipa_name, val, len);
130 default:
131 return -EINVAL;
132 }
133}
134
135int osmo_cni_peer_id_set_str(struct osmo_cni_peer_id *cni_peer_id, enum osmo_cni_peer_id_type type,
136 const char *str_fmt, ...)
137{
138 va_list ap;
139 int rc;
140
141 *cni_peer_id = (struct osmo_cni_peer_id){};
142
143 switch (type) {
144 case OSMO_CNI_PEER_ID_IPA_NAME:
145 cni_peer_id->type = OSMO_CNI_PEER_ID_IPA_NAME;
146 va_start(ap, str_fmt);
147 rc = osmo_ipa_name_set_str_va(&cni_peer_id->ipa_name, str_fmt, ap);
148 va_end(ap);
149 return rc;
150 default:
151 return -EINVAL;
152 }
153}
154
155int osmo_cni_peer_id_cmp(const struct osmo_cni_peer_id *a, const struct osmo_cni_peer_id *b)
156{
157 if (a->type != b->type)
158 return OSMO_CMP(a->type, b->type);
159 switch (a->type) {
160 case OSMO_CNI_PEER_ID_IPA_NAME:
161 return osmo_ipa_name_cmp(&a->ipa_name, &b->ipa_name);
162 default:
163 return -EINVAL;
164 }
165}
166
167const struct value_string osmo_cni_peer_id_type_names[] = {
168 { OSMO_CNI_PEER_ID_IPA_NAME, "IPA-name" },
169 {}
170};
171
172/* Call osmo_cni_peer_id_to_str_c with OTC_SELECT */
173const char *osmo_cni_peer_id_to_str(const struct osmo_cni_peer_id *cpi)
174{
175 return osmo_cni_peer_id_to_str_c(OTC_SELECT, cpi);
176}
177
178/* Return an unquoted string, not including the terminating zero. Used for writing VTY config. */
179const char *osmo_cni_peer_id_to_str_c(void *ctx, const struct osmo_cni_peer_id *cpi)
180{
181 switch (cpi->type) {
182 case OSMO_CNI_PEER_ID_IPA_NAME:
183 return osmo_ipa_name_to_str_c(ctx, &cpi->ipa_name);
184 default:
185 return talloc_strdup(ctx, osmo_cni_peer_id_type_name(cpi->type));
186 }
187}