relay.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* vccd/relay.c
  2. *
  3. * This file is part of vccd.
  4. *
  5. * vccd is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * vccd is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with vccd. If not, see <https://www.gnu.org/licenses/>
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <fcntl.h>
  22. #include <errno.h>
  23. #include <string.h>
  24. #include <sys/stat.h>
  25. #include <sys/types.h>
  26. #include <sys/socket.h>
  27. #include <netinet/in.h>
  28. #include <arpa/inet.h>
  29. #include <poll.h>
  30. #include <vcc/vcc.h>
  31. #include <vcc/vccd.h>
  32. #include <vcc/version.h>
  33. #include <klist.h>
  34. extern struct klist_node connections, logged_users;
  35. int relay_new_msg(int fd, void *t) {
  36. struct vcc_relay_header *req;
  37. char *msg;
  38. int len;
  39. req = t;
  40. len = ntohl(req->size) - sizeof(struct vcc_relay_header);
  41. if (unlikely(!(msg = amalloc(len)))) {
  42. fprintf(stderr, "*** malloc() failed\n");
  43. return 1;
  44. }
  45. read_until(fd, msg, len);
  46. if (!*req->visible)
  47. relay_group_broadcast(msg, fd, req);
  48. else
  49. /* only send to somebody */
  50. relay_send_to(msg, req->visible, fd, req);
  51. afree(msg, len);
  52. return 0;
  53. }
  54. int relay_send_packet(int fd, const char *restrict buf,
  55. const struct vcc_relay_header *restrict hdr, int size) {
  56. struct vcc_relay_header req;
  57. memcpy(&req, hdr, sizeof(struct vcc_relay_header));
  58. req.size = htonl(sizeof(struct vcc_relay_header) + size);
  59. write(fd, &req, sizeof(struct vcc_relay_header));
  60. write(fd, buf, size);
  61. return 0;
  62. }
  63. int send_to(int fd, const char *restrict msg, int is_only_visible,
  64. const struct vcc_relay_header *restrict hdr, int size) {
  65. struct vcc_relay_header req;
  66. memset(&req, 0, sizeof(struct vcc_relay_header));
  67. req.magic = htonl(VCC_MAGIC_RL);
  68. req.reqtype = htonl(REQ_REL_NEW);
  69. req.uid = htonl(is_only_visible);
  70. req.session = 0;
  71. strncpy(req.usrname, hdr->usrname, USRNAME_SIZE);
  72. relay_send_packet(fd, msg, &req, size);
  73. return 0;
  74. }
  75. int relay_send_to(const char *restrict msg, const char *restrict visible, int expect_fd,
  76. const struct vcc_relay_header *restrict hdr) {
  77. struct klist_node *n;
  78. struct user *u;
  79. vcc_write_all();
  80. for (n = logged_users.next; n != &logged_users; n = n->next) {
  81. u = n->data;
  82. if (n->value == expect_fd)
  83. continue;
  84. if (!strcmp(visible, u->username)) {
  85. send_to(n->value, msg, 1, hdr, strlen(msg));
  86. vcc_log("Relay message sent to the visible user. \n");
  87. return 0;
  88. }
  89. }
  90. vcc_log("*** user not found. \n");
  91. return 1;
  92. }
  93. int relay_broadcast(const char *restrict msg, int expect_fd, const struct vcc_relay_header *restrict hdr) {
  94. struct klist_node *n;
  95. int i, len;
  96. /* make sure about that the write() bottom will execute after these */
  97. vcc_write_all();
  98. len = strlen(msg);
  99. i = 0;
  100. for (n = connections.next; n != &connections; n = n->next) {
  101. if (n->value == expect_fd)
  102. continue;
  103. send_to(n->value, msg, 0, hdr, len);
  104. i++;
  105. }
  106. vcc_log("Relay message sent to %d users. \n", i);
  107. return 0;
  108. }
  109. int relay_group_broadcast(const char *restrict msg, int expect_fd, const struct vcc_relay_header *restrict hdr) {
  110. struct klist_node *session, *n;
  111. int i, len;
  112. vcc_write_all();
  113. len = strlen(msg);
  114. if (!hdr->session)
  115. return relay_broadcast(msg, expect_fd, hdr);
  116. if (!(session = find_session(ntohl(hdr->session))))
  117. return 1;
  118. for (n = session->session->next; n != session->session; n = n->next) {
  119. if (n->value == expect_fd)
  120. continue;
  121. send_to(n->value, msg, 0, hdr, len);
  122. i++;
  123. }
  124. vcc_log("Relay group message sent to %d users. \n", i);
  125. return 0;
  126. }