connection.cc 1.9 KB

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