relay.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. msg[len] = 0;
  47. if (!*req->visible)
  48. relay_group_broadcast(msg, fd, req);
  49. else
  50. /* only send to somebody */
  51. relay_send_to(msg, req->visible, fd, req);
  52. afree(msg, len);
  53. return 0;
  54. }
  55. int relay_send_packet(int fd, const char *restrict buf,
  56. const struct vcc_relay_header *restrict hdr, int size) {
  57. struct vcc_relay_header req;
  58. memcpy(&req, hdr, sizeof(struct vcc_relay_header));
  59. req.size = htonl(sizeof(struct vcc_relay_header) + size);
  60. write(fd, &req, sizeof(struct vcc_relay_header));
  61. write(fd, buf, size);
  62. return 0;
  63. }
  64. int send_to(int fd, const char *restrict msg, int is_only_visible,
  65. const struct vcc_relay_header *restrict hdr, int size) {
  66. struct vcc_relay_header req;
  67. memset(&req, 0, sizeof(struct vcc_relay_header));
  68. req.magic = htonl(VCC_MAGIC_RL);
  69. req.reqtype = htonl(REQ_REL_NEW);
  70. req.uid = htonl(is_only_visible);
  71. req.session = 0;
  72. strncpy(req.usrname, hdr->usrname, USRNAME_SIZE);
  73. relay_send_packet(fd, msg, &req, size);
  74. return 0;
  75. }
  76. int relay_send_to(const char *restrict msg, const char *restrict visible, int expect_fd,
  77. const struct vcc_relay_header *restrict hdr) {
  78. struct klist_node *n;
  79. struct user *u;
  80. vcc_write_all();
  81. for (n = logged_users.next; n != &logged_users; n = n->next) {
  82. u = n->data;
  83. if (n->value == expect_fd)
  84. continue;
  85. if (!strcmp(visible, u->username)) {
  86. send_to(n->value, msg, 1, hdr, strlen(msg));
  87. vcc_log("Relay message sent to the visible user. \n");
  88. return 0;
  89. }
  90. }
  91. vcc_log("*** user not found. \n");
  92. return 1;
  93. }
  94. int relay_broadcast(const char *restrict msg, int expect_fd, const struct vcc_relay_header *restrict hdr) {
  95. struct klist_node *n;
  96. int i, len;
  97. /* make sure about that the write() bottom will execute after these */
  98. vcc_write_all();
  99. len = strlen(msg);
  100. i = 0;
  101. for (n = connections.next; n != &connections; n = n->next) {
  102. if (n->value == expect_fd)
  103. continue;
  104. send_to(n->value, msg, 0, hdr, len);
  105. i++;
  106. }
  107. vcc_log("Relay message sent to %d users. \n", i);
  108. return 0;
  109. }
  110. int relay_group_broadcast(const char *restrict msg, int expect_fd, const struct vcc_relay_header *restrict hdr) {
  111. struct klist_node *session, *n;
  112. int i, len;
  113. vcc_write_all();
  114. len = strlen(msg);
  115. if (!hdr->session)
  116. return relay_broadcast(msg, expect_fd, hdr);
  117. if (!(session = find_session(ntohl(hdr->session))))
  118. return 1;
  119. for (n = session->session->next; n != session->session; n = n->next) {
  120. if (n->value == expect_fd)
  121. continue;
  122. send_to(n->value, msg, 0, hdr, len);
  123. i++;
  124. }
  125. vcc_log("Relay group message sent to %d users. \n", i);
  126. return 0;
  127. }