| 1 | /* |
| 2 | * Copyright (C) 2018 - Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU Lesser General Public License, version 2.1 only, |
| 6 | * as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This library is distributed in the hope that it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 11 | * for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU Lesser General Public License |
| 14 | * along with this library; if not, write to the Free Software Foundation, |
| 15 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 16 | */ |
| 17 | |
| 18 | #include <lttng/location-internal.h> |
| 19 | #include <common/macros.h> |
| 20 | #include <stdlib.h> |
| 21 | |
| 22 | static |
| 23 | struct lttng_trace_archive_location *lttng_trace_archive_location_create( |
| 24 | enum lttng_trace_archive_location_type type) |
| 25 | { |
| 26 | struct lttng_trace_archive_location *location; |
| 27 | |
| 28 | location = zmalloc(sizeof(*location)); |
| 29 | if (!location) { |
| 30 | goto end; |
| 31 | } |
| 32 | |
| 33 | location->type = type; |
| 34 | end: |
| 35 | return location; |
| 36 | } |
| 37 | |
| 38 | LTTNG_HIDDEN |
| 39 | void lttng_trace_archive_location_destroy( |
| 40 | struct lttng_trace_archive_location *location) |
| 41 | { |
| 42 | if (!location) { |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | switch (location->type) { |
| 47 | case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL: |
| 48 | free(location->types.local.absolute_path); |
| 49 | break; |
| 50 | case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY: |
| 51 | free(location->types.relay.host); |
| 52 | free(location->types.relay.relative_path); |
| 53 | break; |
| 54 | default: |
| 55 | abort(); |
| 56 | } |
| 57 | |
| 58 | free(location); |
| 59 | } |
| 60 | |
| 61 | LTTNG_HIDDEN |
| 62 | struct lttng_trace_archive_location *lttng_trace_archive_location_local_create( |
| 63 | const char *absolute_path) |
| 64 | { |
| 65 | struct lttng_trace_archive_location *location = NULL; |
| 66 | |
| 67 | if (!absolute_path) { |
| 68 | goto end; |
| 69 | } |
| 70 | |
| 71 | location = lttng_trace_archive_location_create( |
| 72 | LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL); |
| 73 | if (!location) { |
| 74 | goto end; |
| 75 | } |
| 76 | |
| 77 | location->types.local.absolute_path = strdup(absolute_path); |
| 78 | if (!location->types.local.absolute_path) { |
| 79 | goto error; |
| 80 | } |
| 81 | |
| 82 | end: |
| 83 | return location; |
| 84 | error: |
| 85 | lttng_trace_archive_location_destroy(location); |
| 86 | return NULL; |
| 87 | } |
| 88 | |
| 89 | LTTNG_HIDDEN |
| 90 | struct lttng_trace_archive_location *lttng_trace_archive_location_relay_create( |
| 91 | const char *host, |
| 92 | enum lttng_trace_archive_location_relay_protocol_type protocol, |
| 93 | uint16_t control_port, uint16_t data_port, |
| 94 | const char *relative_path) |
| 95 | { |
| 96 | struct lttng_trace_archive_location *location = NULL; |
| 97 | |
| 98 | if (!host || !relative_path) { |
| 99 | goto end; |
| 100 | } |
| 101 | |
| 102 | location = lttng_trace_archive_location_create( |
| 103 | LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY); |
| 104 | if (!location) { |
| 105 | goto end; |
| 106 | } |
| 107 | |
| 108 | location->types.relay.host = strdup(host); |
| 109 | if (!location->types.relay.host) { |
| 110 | goto error; |
| 111 | } |
| 112 | location->types.relay.relative_path = strdup(relative_path); |
| 113 | if (!location->types.relay.relative_path) { |
| 114 | goto error; |
| 115 | } |
| 116 | |
| 117 | location->types.relay.protocol = protocol; |
| 118 | location->types.relay.ports.control = control_port; |
| 119 | location->types.relay.ports.data = data_port; |
| 120 | end: |
| 121 | return location; |
| 122 | error: |
| 123 | lttng_trace_archive_location_destroy(location); |
| 124 | return NULL; |
| 125 | } |
| 126 | |
| 127 | enum lttng_trace_archive_location_type lttng_trace_archive_location_get_type( |
| 128 | const struct lttng_trace_archive_location *location) |
| 129 | { |
| 130 | enum lttng_trace_archive_location_type type; |
| 131 | |
| 132 | if (!location) { |
| 133 | type = LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_UNKNOWN; |
| 134 | goto end; |
| 135 | } |
| 136 | |
| 137 | type = location->type; |
| 138 | end: |
| 139 | return type; |
| 140 | } |
| 141 | |
| 142 | enum lttng_trace_archive_location_status |
| 143 | lttng_trace_archive_location_local_get_absolute_path( |
| 144 | const struct lttng_trace_archive_location *location, |
| 145 | const char **absolute_path) |
| 146 | { |
| 147 | enum lttng_trace_archive_location_status status = |
| 148 | LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK; |
| 149 | |
| 150 | if (!location || !absolute_path || |
| 151 | location->type != LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL) { |
| 152 | status = LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_INVALID; |
| 153 | goto end; |
| 154 | } |
| 155 | |
| 156 | *absolute_path = location->types.local.absolute_path; |
| 157 | end: |
| 158 | return status; |
| 159 | } |
| 160 | |
| 161 | enum lttng_trace_archive_location_status |
| 162 | lttng_trace_archive_location_relay_get_host( |
| 163 | const struct lttng_trace_archive_location *location, |
| 164 | const char **relay_host) |
| 165 | { |
| 166 | enum lttng_trace_archive_location_status status = |
| 167 | LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK; |
| 168 | |
| 169 | if (!location || !relay_host || |
| 170 | location->type != LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY) { |
| 171 | status = LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_INVALID; |
| 172 | goto end; |
| 173 | } |
| 174 | |
| 175 | *relay_host = location->types.relay.host; |
| 176 | end: |
| 177 | return status; |
| 178 | } |
| 179 | |
| 180 | enum lttng_trace_archive_location_status |
| 181 | lttng_trace_archive_location_relay_get_relative_path( |
| 182 | const struct lttng_trace_archive_location *location, |
| 183 | const char **relative_path) |
| 184 | { |
| 185 | enum lttng_trace_archive_location_status status = |
| 186 | LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK; |
| 187 | |
| 188 | if (!location || !relative_path || |
| 189 | location->type != LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY) { |
| 190 | status = LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_INVALID; |
| 191 | goto end; |
| 192 | } |
| 193 | |
| 194 | *relative_path = location->types.relay.relative_path; |
| 195 | end: |
| 196 | return status; |
| 197 | } |
| 198 | |
| 199 | enum lttng_trace_archive_location_status |
| 200 | lttng_trace_archive_location_relay_get_control_port( |
| 201 | const struct lttng_trace_archive_location *location, |
| 202 | uint16_t *control_port) |
| 203 | { |
| 204 | enum lttng_trace_archive_location_status status = |
| 205 | LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK; |
| 206 | |
| 207 | if (!location || !control_port || |
| 208 | location->type != LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY) { |
| 209 | status = LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_INVALID; |
| 210 | goto end; |
| 211 | } |
| 212 | |
| 213 | *control_port = location->types.relay.ports.control; |
| 214 | end: |
| 215 | return status; |
| 216 | } |
| 217 | |
| 218 | enum lttng_trace_archive_location_status |
| 219 | lttng_trace_archive_location_relay_get_data_port( |
| 220 | const struct lttng_trace_archive_location *location, |
| 221 | uint16_t *data_port) |
| 222 | { |
| 223 | enum lttng_trace_archive_location_status status = |
| 224 | LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK; |
| 225 | |
| 226 | if (!location || !data_port || |
| 227 | location->type != LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY) { |
| 228 | status = LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_INVALID; |
| 229 | goto end; |
| 230 | } |
| 231 | |
| 232 | *data_port = location->types.relay.ports.data; |
| 233 | end: |
| 234 | return status; |
| 235 | } |
| 236 | |
| 237 | enum lttng_trace_archive_location_status |
| 238 | lttng_trace_archive_location_relay_get_protocol_type( |
| 239 | const struct lttng_trace_archive_location *location, |
| 240 | enum lttng_trace_archive_location_relay_protocol_type *protocol) |
| 241 | { |
| 242 | enum lttng_trace_archive_location_status status = |
| 243 | LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK; |
| 244 | |
| 245 | if (!location || !protocol || |
| 246 | location->type != LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY) { |
| 247 | status = LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_INVALID; |
| 248 | goto end; |
| 249 | } |
| 250 | |
| 251 | *protocol = location->types.relay.protocol; |
| 252 | end: |
| 253 | return status; |
| 254 | } |