|
| 1 | +/************************************************************\ |
| 2 | + * Copyright 2025 Lawrence Livermore National Security, LLC |
| 3 | + * (c.f. AUTHORS, NOTICE.LLNS, COPYING) |
| 4 | + * |
| 5 | + * This file is part of the Flux resource manager framework. |
| 6 | + * For details, see https://github.com/flux-framework. |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: LGPL-3.0 |
| 9 | +\************************************************************/ |
| 10 | + |
| 11 | +#if HAVE_CONFIG_H |
| 12 | +#include "config.h" |
| 13 | +#endif |
| 14 | +#include <stdlib.h> |
| 15 | +#include <jansson.h> |
| 16 | +#include <flux/core.h> |
| 17 | + |
| 18 | +#include "src/common/libutil/errprintf.h" |
| 19 | +#include "src/common/libutil/errno_safe.h" |
| 20 | +#include "ccan/str/str.h" |
| 21 | + |
| 22 | +#include "bizcard.h" |
| 23 | + |
| 24 | +struct bizcard { |
| 25 | + json_t *obj; |
| 26 | + char *encoded; |
| 27 | + size_t cursor; |
| 28 | + int refcount; |
| 29 | +}; |
| 30 | + |
| 31 | +void bizcard_decref (struct bizcard *bc) |
| 32 | +{ |
| 33 | + if (bc && --bc->refcount == 0) { |
| 34 | + int saved_errno = errno; |
| 35 | + json_decref (bc->obj); |
| 36 | + free (bc->encoded); |
| 37 | + free (bc); |
| 38 | + errno = saved_errno; |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +struct bizcard *bizcard_incref (struct bizcard *bc) |
| 43 | +{ |
| 44 | + if (bc) |
| 45 | + bc->refcount++; |
| 46 | + return bc; |
| 47 | +} |
| 48 | + |
| 49 | +struct bizcard *bizcard_create (const char *hostname, const char *pubkey) |
| 50 | +{ |
| 51 | + struct bizcard *bc; |
| 52 | + |
| 53 | + if (!hostname || !pubkey) { |
| 54 | + errno = EINVAL; |
| 55 | + return NULL; |
| 56 | + } |
| 57 | + if (!(bc = calloc (1, sizeof (*bc)))) |
| 58 | + return NULL; |
| 59 | + bc->refcount = 1; |
| 60 | + if (!(bc->obj = json_pack ("{s:s s:s s:[]}", |
| 61 | + "host", hostname, |
| 62 | + "pubkey", pubkey, |
| 63 | + "uri"))) { |
| 64 | + errno = ENOMEM; |
| 65 | + bizcard_decref (bc); |
| 66 | + return NULL; |
| 67 | + } |
| 68 | + return bc; |
| 69 | +} |
| 70 | + |
| 71 | +const char *bizcard_encode (const struct bizcard *bc_const) |
| 72 | +{ |
| 73 | + struct bizcard *bc = (struct bizcard *)bc_const; |
| 74 | + if (!bc) { |
| 75 | + errno = EINVAL; |
| 76 | + return NULL; |
| 77 | + } |
| 78 | + char *s; |
| 79 | + if (!(s = json_dumps (bc->obj, JSON_COMPACT))) { |
| 80 | + errno = ENOMEM; |
| 81 | + return NULL; |
| 82 | + } |
| 83 | + free (bc->encoded); |
| 84 | + bc->encoded = s; |
| 85 | + return bc->encoded; |
| 86 | +} |
| 87 | + |
| 88 | +struct bizcard *bizcard_decode (const char *s, flux_error_t *error) |
| 89 | +{ |
| 90 | + struct bizcard *bc; |
| 91 | + json_error_t jerror; |
| 92 | + |
| 93 | + if (!(bc = calloc (1, sizeof (*bc)))) |
| 94 | + return NULL; |
| 95 | + bc->refcount = 1; |
| 96 | + if (!(bc->obj = json_loads (s, 0, &jerror))) { |
| 97 | + errprintf (error, "%s", jerror.text); |
| 98 | + errno = EINVAL; |
| 99 | + goto error; |
| 100 | + } |
| 101 | + if (json_unpack_ex (bc->obj, |
| 102 | + &jerror, |
| 103 | + JSON_VALIDATE_ONLY, |
| 104 | + "{s:s s:s s:o}", |
| 105 | + "host", |
| 106 | + "pubkey", |
| 107 | + "uri") < 0) { |
| 108 | + errprintf (error, "%s", jerror.text); |
| 109 | + errno = EINVAL; |
| 110 | + goto error; |
| 111 | + } |
| 112 | + return bc; |
| 113 | +error: |
| 114 | + bizcard_decref (bc); |
| 115 | + return NULL; |
| 116 | +} |
| 117 | + |
| 118 | +int bizcard_uri_append (struct bizcard *bc, const char *uri) |
| 119 | +{ |
| 120 | + json_t *a; |
| 121 | + json_t *o; |
| 122 | + |
| 123 | + if (!bc |
| 124 | + || !uri |
| 125 | + || !strstr (uri, "://") |
| 126 | + || !(a = json_object_get (bc->obj, "uri"))) { |
| 127 | + errno = EINVAL; |
| 128 | + return -1; |
| 129 | + } |
| 130 | + if (!(o = json_string (uri)) || json_array_append_new (a, o) < 0) { |
| 131 | + json_decref (o); |
| 132 | + errno = ENOMEM; |
| 133 | + return -1; |
| 134 | + } |
| 135 | + return 0; |
| 136 | +} |
| 137 | + |
| 138 | +const char *bizcard_uri_next (const struct bizcard *bc_const) |
| 139 | +{ |
| 140 | + struct bizcard *bc = (struct bizcard *)bc_const; |
| 141 | + json_t *a; |
| 142 | + json_t *o; |
| 143 | + const char *s; |
| 144 | + |
| 145 | + if (!bc |
| 146 | + || !(a = json_object_get (bc->obj, "uri")) |
| 147 | + || !(o = json_array_get (a, bc->cursor)) |
| 148 | + || !(s = json_string_value (o))) |
| 149 | + return NULL; |
| 150 | + bc->cursor++; |
| 151 | + return s; |
| 152 | +} |
| 153 | + |
| 154 | +const char *bizcard_uri_first (const struct bizcard *bc_const) |
| 155 | +{ |
| 156 | + struct bizcard *bc = (struct bizcard *)bc_const; |
| 157 | + if (bc) |
| 158 | + bc->cursor = 0; |
| 159 | + return bizcard_uri_next (bc); |
| 160 | +} |
| 161 | + |
| 162 | +const char *bizcard_uri_find (const struct bizcard *bc, const char *scheme) |
| 163 | +{ |
| 164 | + const char *uri; |
| 165 | + |
| 166 | + uri = bizcard_uri_first (bc); |
| 167 | + while (uri) { |
| 168 | + if (!scheme || strstarts (uri, scheme)) |
| 169 | + return uri; |
| 170 | + uri = bizcard_uri_next (bc); |
| 171 | + } |
| 172 | + return NULL; |
| 173 | +} |
| 174 | + |
| 175 | +const char *bizcard_pubkey (const struct bizcard *bc) |
| 176 | +{ |
| 177 | + const char *s; |
| 178 | + if (!bc || json_unpack (bc->obj, "{s:s}", "pubkey", &s) < 0) |
| 179 | + return NULL; |
| 180 | + return s; |
| 181 | +} |
| 182 | + |
| 183 | +const char *bizcard_hostname (const struct bizcard *bc) |
| 184 | +{ |
| 185 | + const char *s; |
| 186 | + if (!bc || json_unpack (bc->obj, "{s:s}", "host", &s) < 0) |
| 187 | + return NULL; |
| 188 | + return s; |
| 189 | +} |
| 190 | + |
| 191 | +// vi:ts=4 sw=4 expandtab |
0 commit comments