Neels Hofmeyr | 008ce4b | 2019-12-04 01:04:32 +0100 | [diff] [blame^] | 1 | /* 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/gsup_peer_id.h> |
| 23 | |
| 24 | bool osmo_ipa_name_is_empty(struct osmo_ipa_name *ipa_name) |
| 25 | { |
| 26 | return (!ipa_name) || (!ipa_name->len); |
| 27 | } |
| 28 | |
| 29 | int 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 | |
| 42 | static 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 | |
| 51 | int 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 | |
| 61 | int 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 | /* Return an unquoted string, not including the terminating zero. Used for writing VTY config. */ |
| 94 | const char *osmo_ipa_name_to_str(const struct osmo_ipa_name *ipa_name) |
| 95 | { |
| 96 | size_t len = ipa_name->len; |
| 97 | if (!len) |
| 98 | return ""; |
| 99 | if (ipa_name->val[len-1] == '\0') |
| 100 | len--; |
| 101 | return osmo_escape_str_c(OTC_SELECT, (char*)ipa_name->val, len); |
| 102 | } |
| 103 | |
| 104 | bool osmo_gsup_peer_id_is_empty(struct osmo_gsup_peer_id *gsup_peer_id) |
| 105 | { |
| 106 | if (!gsup_peer_id) |
| 107 | return true; |
| 108 | switch (gsup_peer_id->type) { |
| 109 | case OSMO_GSUP_PEER_ID_EMPTY: |
| 110 | return true; |
| 111 | case OSMO_GSUP_PEER_ID_IPA_NAME: |
| 112 | return osmo_ipa_name_is_empty(&gsup_peer_id->ipa_name); |
| 113 | default: |
| 114 | return false; |
| 115 | } |
| 116 | } |
| 117 | int osmo_gsup_peer_id_set(struct osmo_gsup_peer_id *gsup_peer_id, enum osmo_gsup_peer_id_type type, |
| 118 | const uint8_t *val, size_t len) |
| 119 | { |
| 120 | gsup_peer_id->type = type; |
| 121 | switch (type) { |
| 122 | case OSMO_GSUP_PEER_ID_IPA_NAME: |
| 123 | return osmo_ipa_name_set(&gsup_peer_id->ipa_name, val, len); |
| 124 | default: |
| 125 | return -EINVAL; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | int osmo_gsup_peer_id_set_str(struct osmo_gsup_peer_id *gsup_peer_id, enum osmo_gsup_peer_id_type type, |
| 130 | const char *str_fmt, ...) |
| 131 | { |
| 132 | va_list ap; |
| 133 | int rc; |
| 134 | |
| 135 | switch (type) { |
| 136 | case OSMO_GSUP_PEER_ID_IPA_NAME: |
| 137 | va_start(ap, str_fmt); |
| 138 | rc = osmo_ipa_name_set_str_va(&gsup_peer_id->ipa_name, str_fmt, ap); |
| 139 | va_end(ap); |
| 140 | return rc; |
| 141 | default: |
| 142 | return -EINVAL; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | int osmo_gsup_peer_id_cmp(const struct osmo_gsup_peer_id *a, const struct osmo_gsup_peer_id *b) |
| 147 | { |
| 148 | if (a->type != b->type) |
| 149 | return OSMO_CMP(a->type, b->type); |
| 150 | switch (a->type) { |
| 151 | case OSMO_GSUP_PEER_ID_IPA_NAME: |
| 152 | return osmo_ipa_name_cmp(&a->ipa_name, &b->ipa_name); |
| 153 | default: |
| 154 | return -EINVAL; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | const struct value_string osmo_gsup_peer_id_type_names[] = { |
| 159 | { OSMO_GSUP_PEER_ID_IPA_NAME, "IPA-name" }, |
| 160 | {} |
| 161 | }; |
| 162 | |
| 163 | /* Return an unquoted string, not including the terminating zero. Used for writing VTY config. */ |
| 164 | const char *osmo_gsup_peer_id_to_str(const struct osmo_gsup_peer_id *gpi) |
| 165 | { |
| 166 | switch (gpi->type) { |
| 167 | case OSMO_GSUP_PEER_ID_IPA_NAME: |
| 168 | return osmo_ipa_name_to_str(&gpi->ipa_name); |
| 169 | default: |
| 170 | return osmo_gsup_peer_id_type_name(gpi->type); |
| 171 | } |
| 172 | } |