2 * Copyright (C) 2018 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
8 #include <lttng/location-internal.h>
9 #include <common/macros.h>
13 struct lttng_trace_archive_location
*lttng_trace_archive_location_create(
14 enum lttng_trace_archive_location_type type
)
16 struct lttng_trace_archive_location
*location
;
18 location
= zmalloc(sizeof(*location
));
23 location
->type
= type
;
29 void lttng_trace_archive_location_destroy(
30 struct lttng_trace_archive_location
*location
)
36 switch (location
->type
) {
37 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
:
38 free(location
->types
.local
.absolute_path
);
40 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
:
41 free(location
->types
.relay
.host
);
42 free(location
->types
.relay
.relative_path
);
52 struct lttng_trace_archive_location
*lttng_trace_archive_location_local_create(
53 const char *absolute_path
)
55 struct lttng_trace_archive_location
*location
= NULL
;
61 location
= lttng_trace_archive_location_create(
62 LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
);
67 location
->types
.local
.absolute_path
= strdup(absolute_path
);
68 if (!location
->types
.local
.absolute_path
) {
75 lttng_trace_archive_location_destroy(location
);
80 struct lttng_trace_archive_location
*lttng_trace_archive_location_relay_create(
82 enum lttng_trace_archive_location_relay_protocol_type protocol
,
83 uint16_t control_port
, uint16_t data_port
,
84 const char *relative_path
)
86 struct lttng_trace_archive_location
*location
= NULL
;
88 if (!host
|| !relative_path
) {
92 location
= lttng_trace_archive_location_create(
93 LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
);
98 location
->types
.relay
.host
= strdup(host
);
99 if (!location
->types
.relay
.host
) {
102 location
->types
.relay
.relative_path
= strdup(relative_path
);
103 if (!location
->types
.relay
.relative_path
) {
107 location
->types
.relay
.protocol
= protocol
;
108 location
->types
.relay
.ports
.control
= control_port
;
109 location
->types
.relay
.ports
.data
= data_port
;
113 lttng_trace_archive_location_destroy(location
);
118 ssize_t
lttng_trace_archive_location_create_from_buffer(
119 const struct lttng_buffer_view
*view
,
120 struct lttng_trace_archive_location
**location
)
123 const struct lttng_trace_archive_location_comm
*location_comm
;
124 struct lttng_buffer_view location_comm_view
;
126 location_comm_view
= lttng_buffer_view_from_view(view
, 0,
127 sizeof(*location_comm
));
128 if (!lttng_buffer_view_is_valid(&location_comm_view
)) {
132 offset
+= location_comm_view
.size
;
133 location_comm
= (const struct lttng_trace_archive_location_comm
*) location_comm_view
.data
;
135 switch ((enum lttng_trace_archive_location_type
) location_comm
->type
) {
136 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
:
138 const struct lttng_buffer_view absolute_path_view
=
139 lttng_buffer_view_from_view(view
, offset
,
140 location_comm
->types
.local
.absolute_path_len
);
142 if (!lttng_buffer_view_is_valid(&absolute_path_view
)) {
146 if (absolute_path_view
.data
[absolute_path_view
.size
- 1] != '\0') {
149 offset
+= absolute_path_view
.size
;
151 *location
= lttng_trace_archive_location_local_create(
152 absolute_path_view
.data
);
158 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
:
160 const struct lttng_buffer_view hostname_view
=
161 lttng_buffer_view_from_view(view
, offset
,
162 location_comm
->types
.relay
.hostname_len
);
163 const struct lttng_buffer_view relative_path_view
=
164 lttng_buffer_view_from_view(view
,
165 offset
+ hostname_view
.size
,
166 location_comm
->types
.relay
.relative_path_len
);
168 if (!lttng_buffer_view_is_valid(&hostname_view
) ||
169 !lttng_buffer_view_is_valid(
170 &relative_path_view
)) {
174 if (hostname_view
.data
[hostname_view
.size
- 1] != '\0') {
177 if (relative_path_view
.data
[relative_path_view
.size
- 1] != '\0') {
180 offset
+= hostname_view
.size
+ relative_path_view
.size
;
182 *location
= lttng_trace_archive_location_relay_create(
184 (enum lttng_trace_archive_location_relay_protocol_type
) location_comm
->types
.relay
.protocol
,
185 location_comm
->types
.relay
.ports
.control
,
186 location_comm
->types
.relay
.ports
.data
,
187 relative_path_view
.data
);
203 ssize_t
lttng_trace_archive_location_serialize(
204 const struct lttng_trace_archive_location
*location
,
205 struct lttng_dynamic_buffer
*buffer
)
208 struct lttng_trace_archive_location_comm location_comm
;
210 location_comm
.type
= (int8_t) location
->type
;
212 switch (location
->type
) {
213 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
:
214 location_comm
.types
.local
.absolute_path_len
=
215 strlen(location
->types
.local
.absolute_path
) + 1;
217 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
:
218 location_comm
.types
.relay
.hostname_len
=
219 strlen(location
->types
.relay
.host
) + 1;
220 location_comm
.types
.relay
.protocol
=
221 (int8_t) location
->types
.relay
.protocol
;
222 location_comm
.types
.relay
.ports
.control
=
223 location
->types
.relay
.ports
.control
;
224 location_comm
.types
.relay
.ports
.data
=
225 location
->types
.relay
.ports
.data
;
226 location_comm
.types
.relay
.relative_path_len
=
227 strlen(location
->types
.relay
.relative_path
) + 1;
233 ret
= lttng_dynamic_buffer_append(buffer
, &location_comm
,
234 sizeof(location_comm
));
239 switch (location
->type
) {
240 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
:
241 ret
= lttng_dynamic_buffer_append(buffer
,
242 location
->types
.local
.absolute_path
,
243 location_comm
.types
.local
.absolute_path_len
);
248 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
:
249 ret
= lttng_dynamic_buffer_append(buffer
,
250 location
->types
.relay
.host
,
251 location_comm
.types
.relay
.hostname_len
);
255 ret
= lttng_dynamic_buffer_append(buffer
,
256 location
->types
.relay
.relative_path
,
257 location_comm
.types
.relay
.relative_path_len
);
271 enum lttng_trace_archive_location_type
lttng_trace_archive_location_get_type(
272 const struct lttng_trace_archive_location
*location
)
274 enum lttng_trace_archive_location_type type
;
277 type
= LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_UNKNOWN
;
281 type
= location
->type
;
286 enum lttng_trace_archive_location_status
287 lttng_trace_archive_location_local_get_absolute_path(
288 const struct lttng_trace_archive_location
*location
,
289 const char **absolute_path
)
291 enum lttng_trace_archive_location_status status
=
292 LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
;
294 if (!location
|| !absolute_path
||
295 location
->type
!= LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
) {
296 status
= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_INVALID
;
300 *absolute_path
= location
->types
.local
.absolute_path
;
305 enum lttng_trace_archive_location_status
306 lttng_trace_archive_location_relay_get_host(
307 const struct lttng_trace_archive_location
*location
,
308 const char **relay_host
)
310 enum lttng_trace_archive_location_status status
=
311 LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
;
313 if (!location
|| !relay_host
||
314 location
->type
!= LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
) {
315 status
= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_INVALID
;
319 *relay_host
= location
->types
.relay
.host
;
324 enum lttng_trace_archive_location_status
325 lttng_trace_archive_location_relay_get_relative_path(
326 const struct lttng_trace_archive_location
*location
,
327 const char **relative_path
)
329 enum lttng_trace_archive_location_status status
=
330 LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
;
332 if (!location
|| !relative_path
||
333 location
->type
!= LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
) {
334 status
= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_INVALID
;
338 *relative_path
= location
->types
.relay
.relative_path
;
343 enum lttng_trace_archive_location_status
344 lttng_trace_archive_location_relay_get_control_port(
345 const struct lttng_trace_archive_location
*location
,
346 uint16_t *control_port
)
348 enum lttng_trace_archive_location_status status
=
349 LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
;
351 if (!location
|| !control_port
||
352 location
->type
!= LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
) {
353 status
= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_INVALID
;
357 *control_port
= location
->types
.relay
.ports
.control
;
362 enum lttng_trace_archive_location_status
363 lttng_trace_archive_location_relay_get_data_port(
364 const struct lttng_trace_archive_location
*location
,
367 enum lttng_trace_archive_location_status status
=
368 LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
;
370 if (!location
|| !data_port
||
371 location
->type
!= LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
) {
372 status
= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_INVALID
;
376 *data_port
= location
->types
.relay
.ports
.data
;
381 enum lttng_trace_archive_location_status
382 lttng_trace_archive_location_relay_get_protocol_type(
383 const struct lttng_trace_archive_location
*location
,
384 enum lttng_trace_archive_location_relay_protocol_type
*protocol
)
386 enum lttng_trace_archive_location_status status
=
387 LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
;
389 if (!location
|| !protocol
||
390 location
->type
!= LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
) {
391 status
= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_INVALID
;
395 *protocol
= location
->types
.relay
.protocol
;