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 (!location_comm_view
.data
) {
131 offset
+= location_comm_view
.size
;
132 location_comm
= (const struct lttng_trace_archive_location_comm
*) view
->data
;
134 switch ((enum lttng_trace_archive_location_type
) location_comm
->type
) {
135 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
:
137 const struct lttng_buffer_view absolute_path_view
=
138 lttng_buffer_view_from_view(view
, offset
,
139 location_comm
->types
.local
.absolute_path_len
);
141 if (!absolute_path_view
.data
) {
144 if (absolute_path_view
.data
[absolute_path_view
.size
- 1] != '\0') {
147 offset
+= absolute_path_view
.size
;
149 *location
= lttng_trace_archive_location_local_create(
150 absolute_path_view
.data
);
156 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
:
158 const struct lttng_buffer_view hostname_view
=
159 lttng_buffer_view_from_view(view
, offset
,
160 location_comm
->types
.relay
.hostname_len
);
161 const struct lttng_buffer_view relative_path_view
=
162 lttng_buffer_view_from_view(view
,
163 offset
+ hostname_view
.size
,
164 location_comm
->types
.relay
.relative_path_len
);
166 if (!hostname_view
.data
|| !relative_path_view
.data
) {
169 if (hostname_view
.data
[hostname_view
.size
- 1] != '\0') {
172 if (relative_path_view
.data
[relative_path_view
.size
- 1] != '\0') {
175 offset
+= hostname_view
.size
+ relative_path_view
.size
;
177 *location
= lttng_trace_archive_location_relay_create(
179 (enum lttng_trace_archive_location_relay_protocol_type
) location_comm
->types
.relay
.protocol
,
180 location_comm
->types
.relay
.ports
.control
,
181 location_comm
->types
.relay
.ports
.data
,
182 relative_path_view
.data
);
198 ssize_t
lttng_trace_archive_location_serialize(
199 const struct lttng_trace_archive_location
*location
,
200 struct lttng_dynamic_buffer
*buffer
)
203 struct lttng_trace_archive_location_comm location_comm
;
205 location_comm
.type
= (int8_t) location
->type
;
207 switch (location
->type
) {
208 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
:
209 location_comm
.types
.local
.absolute_path_len
=
210 strlen(location
->types
.local
.absolute_path
) + 1;
212 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
:
213 location_comm
.types
.relay
.hostname_len
=
214 strlen(location
->types
.relay
.host
) + 1;
215 location_comm
.types
.relay
.protocol
=
216 (int8_t) location
->types
.relay
.protocol
;
217 location_comm
.types
.relay
.ports
.control
=
218 location
->types
.relay
.ports
.control
;
219 location_comm
.types
.relay
.ports
.data
=
220 location
->types
.relay
.ports
.data
;
221 location_comm
.types
.relay
.relative_path_len
=
222 strlen(location
->types
.relay
.relative_path
) + 1;
228 ret
= lttng_dynamic_buffer_append(buffer
, &location_comm
,
229 sizeof(location_comm
));
234 switch (location
->type
) {
235 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
:
236 ret
= lttng_dynamic_buffer_append(buffer
,
237 location
->types
.local
.absolute_path
,
238 location_comm
.types
.local
.absolute_path_len
);
243 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
:
244 ret
= lttng_dynamic_buffer_append(buffer
,
245 location
->types
.relay
.host
,
246 location_comm
.types
.relay
.hostname_len
);
250 ret
= lttng_dynamic_buffer_append(buffer
,
251 location
->types
.relay
.relative_path
,
252 location_comm
.types
.relay
.relative_path_len
);
266 enum lttng_trace_archive_location_type
lttng_trace_archive_location_get_type(
267 const struct lttng_trace_archive_location
*location
)
269 enum lttng_trace_archive_location_type type
;
272 type
= LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_UNKNOWN
;
276 type
= location
->type
;
281 enum lttng_trace_archive_location_status
282 lttng_trace_archive_location_local_get_absolute_path(
283 const struct lttng_trace_archive_location
*location
,
284 const char **absolute_path
)
286 enum lttng_trace_archive_location_status status
=
287 LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
;
289 if (!location
|| !absolute_path
||
290 location
->type
!= LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
) {
291 status
= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_INVALID
;
295 *absolute_path
= location
->types
.local
.absolute_path
;
300 enum lttng_trace_archive_location_status
301 lttng_trace_archive_location_relay_get_host(
302 const struct lttng_trace_archive_location
*location
,
303 const char **relay_host
)
305 enum lttng_trace_archive_location_status status
=
306 LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
;
308 if (!location
|| !relay_host
||
309 location
->type
!= LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
) {
310 status
= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_INVALID
;
314 *relay_host
= location
->types
.relay
.host
;
319 enum lttng_trace_archive_location_status
320 lttng_trace_archive_location_relay_get_relative_path(
321 const struct lttng_trace_archive_location
*location
,
322 const char **relative_path
)
324 enum lttng_trace_archive_location_status status
=
325 LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
;
327 if (!location
|| !relative_path
||
328 location
->type
!= LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
) {
329 status
= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_INVALID
;
333 *relative_path
= location
->types
.relay
.relative_path
;
338 enum lttng_trace_archive_location_status
339 lttng_trace_archive_location_relay_get_control_port(
340 const struct lttng_trace_archive_location
*location
,
341 uint16_t *control_port
)
343 enum lttng_trace_archive_location_status status
=
344 LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
;
346 if (!location
|| !control_port
||
347 location
->type
!= LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
) {
348 status
= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_INVALID
;
352 *control_port
= location
->types
.relay
.ports
.control
;
357 enum lttng_trace_archive_location_status
358 lttng_trace_archive_location_relay_get_data_port(
359 const struct lttng_trace_archive_location
*location
,
362 enum lttng_trace_archive_location_status status
=
363 LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
;
365 if (!location
|| !data_port
||
366 location
->type
!= LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
) {
367 status
= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_INVALID
;
371 *data_port
= location
->types
.relay
.ports
.data
;
376 enum lttng_trace_archive_location_status
377 lttng_trace_archive_location_relay_get_protocol_type(
378 const struct lttng_trace_archive_location
*location
,
379 enum lttng_trace_archive_location_relay_protocol_type
*protocol
)
381 enum lttng_trace_archive_location_status status
=
382 LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
;
384 if (!location
|| !protocol
||
385 location
->type
!= LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
) {
386 status
= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_INVALID
;
390 *protocol
= location
->types
.relay
.protocol
;