relay.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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_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 send_to(int fd, const char *restrict msg, int is_only_visible,
  55. const struct vcc_relay_header *restrict hdr, int size) {
  56. struct vcc_relay_header req;
  57. memset(&req, 0, sizeof(struct vcc_relay_header));
  58. req.magic = htonl(VCC_MAGIC_RL);
  59. req.reqtype = htonl(REQ_REL_NEW);
  60. req.uid = htonl(is_only_visible);
  61. req.session = 0;
  62. req.size = htonl(sizeof(struct vcc_relay_header) + size);
  63. strncpy(req.usrname, hdr->usrname, USRNAME_SIZE);
  64. /* direct writes here
  65. * vcc_send_req(n, req);
  66. * */
  67. write(fd, &req, sizeof(struct vcc_relay_header));
  68. write(fd, msg, size);
  69. return 0;
  70. }
  71. int relay_send_to(const char *restrict msg, const char *restrict visible, int expect_fd,
  72. const struct vcc_relay_header *restrict hdr) {
  73. struct klist_node *n;
  74. struct user *u;
  75. for (n = logged_users.next; n != &logged_users; n = n->next) {
  76. u = n->data;
  77. if (n->value == expect_fd)
  78. continue;
  79. if (!strcmp(visible, u->username)) {
  80. send_to(n->value, msg, 1, hdr, strlen(msg));
  81. vcc_log("Relay message sent to the visible user. \n");
  82. return 0;
  83. }
  84. }
  85. vcc_log("*** user not found. \n");
  86. return 1;
  87. }
  88. int relay_broadcast(const char *restrict msg, int expect_fd, const struct vcc_relay_header *restrict hdr) {
  89. struct klist_node *n;
  90. int i, len;
  91. /* make sure about that the write() bottom will execute after these */
  92. vcc_write_all();
  93. len = strlen(msg);
  94. i = 0;
  95. for (n = connections.next; n != &connections; n = n->next) {
  96. if (n->value == expect_fd)
  97. continue;
  98. send_to(n->value, msg, 0, hdr, len);
  99. i++;
  100. }
  101. vcc_log("Relay message sent to %d users. \n", i);
  102. return 0;
  103. }