| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #include "agent/v1/connection.pb.h" |
| 4 | #include "agent/v1/disconnect.pb.h" |
| 5 | #include "connection.h" |
| 6 | |
| 7 | #include "schema_wrapper_utils.h" |
| 8 | |
| 9 | #include <sys/time.h> |
| 10 | #include <stdlib.h> |
| 11 | |
| 12 | using namespace agent::v1; |
| 13 | |
| 14 | char *generate_update_agent_connection(size_t *len, const update_agent_connection_t *data) |
| 15 | { |
| 16 | UpdateAgentConnection connupd; |
| 17 | |
| 18 | connupd.set_claim_id(data->claim_id); |
| 19 | connupd.set_reachable(data->reachable); |
| 20 | connupd.set_session_id(data->session_id); |
| 21 | |
| 22 | connupd.set_update_source((data->lwt) ? CONNECTION_UPDATE_SOURCE_LWT : CONNECTION_UPDATE_SOURCE_AGENT); |
| 23 | |
| 24 | struct timeval tv; |
| 25 | gettimeofday(&tv, NULL); |
| 26 | |
| 27 | google::protobuf::Timestamp *timestamp = connupd.mutable_updated_at(); |
| 28 | timestamp->set_seconds(tv.tv_sec); |
| 29 | timestamp->set_nanos(tv.tv_usec * 1000); |
| 30 | |
| 31 | if (data->capabilities) { |
| 32 | const struct capability *capa = data->capabilities; |
| 33 | while (capa->name) { |
| 34 | aclk_lib::v1::Capability *proto_capa = connupd.add_capabilities(); |
| 35 | capability_set(proto_capa, capa); |
| 36 | capa++; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | *len = PROTO_COMPAT_MSG_SIZE(connupd); |
| 41 | char *msg = (char*)mallocz(*len); |
| 42 | if (msg) |
| 43 | connupd.SerializeToArray(msg, *len); |
| 44 | |
| 45 | return msg; |
| 46 | } |
| 47 | |
| 48 | struct disconnect_cmd *parse_disconnect_cmd(const char *data, size_t len) { |
| 49 | DisconnectReq req; |
| 50 | struct disconnect_cmd *res; |
| 51 | |
| 52 | if (!req.ParseFromArray(data, len)) |
| 53 | return NULL; |
| 54 | |
| 55 | res = (struct disconnect_cmd *)callocz(1, sizeof(struct disconnect_cmd)); |
| 56 | |
| 57 | if (!res) |
| 58 | return NULL; |
| 59 | |
| 60 | res->reconnect_after_s = req.reconnect_after_seconds(); |
| 61 | res->permaban = req.permaban(); |
| 62 | res->error_code = req.error_code(); |
| 63 | if (req.error_description().c_str()) { |
| 64 | res->error_description = strdupz(req.error_description().c_str()); |
| 65 | if (!res->error_description) { |
| 66 | freez(res); |
| 67 | return NULL; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | return res; |
| 72 | } |