vcc.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* vccd/vcc.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. struct klist_node connections = KLIST_NODE_INIT(&connections);
  35. int vcc_new_connection(int fd, struct in_addr *addr) {
  36. struct klist_node *n;
  37. vcc_log("New connection from %s\n", inet_ntoa(*addr));
  38. if (unlikely(!(n = malloc(sizeof(struct klist_node))))) {
  39. vcc_log("*** malloc() failed\n");
  40. return 1;
  41. }
  42. klist_init(n);
  43. n->data = addr;
  44. n->value = fd;
  45. if (unlikely(!(n->sending = malloc(sizeof(struct klist_node))))) {
  46. vcc_log("*** malloc() failed\n");
  47. return 1;
  48. }
  49. klist_init(n->sending);
  50. klist_add(&connections, n);
  51. return 0;
  52. }
  53. int vcc_close_connection(int fd) {
  54. struct klist_node *n, *t;
  55. for (n = connections.next; n != &connections
  56. && n->value != fd; n = n->next)
  57. ;
  58. klist_del(&connections, n);
  59. for (t = n->sending->next; t != n->sending; t = n->next)
  60. free(t->data);
  61. free(n->sending);
  62. vcc_log("Client closed. \n");
  63. return 0;
  64. }
  65. struct klist_node *find_connection(int fd) {
  66. struct klist_node *n;
  67. for (n = connections.next; n != &connections
  68. && n->value != fd; n = n->next)
  69. ;
  70. if (unlikely(n == &connections)) {
  71. vcc_log("--- connection not found: %d\n", fd);
  72. return NULL;
  73. }
  74. return n;
  75. }
  76. int write_packages(struct klist_node *node) {
  77. void *p;
  78. if (unlikely(!node->sending))
  79. return 0;
  80. if (unlikely(klist_empty(node->sending)))
  81. return 0;
  82. p = node->sending->next->data;
  83. write(node->value, p, REQ_SIZE);
  84. free(p);
  85. klist_del(node->sending, node->sending->next);
  86. return 0;
  87. }
  88. int vcc_write_packages(int fd) {
  89. return write_packages(find_connection(fd));
  90. }
  91. int vcc_send_req(struct klist_node *node, void *p) {
  92. struct klist_node *n;
  93. if (unlikely(!(n = malloc(sizeof(struct klist_node))))) {
  94. vcc_log("*** malloc() failed\n");
  95. return 1;
  96. }
  97. n->data = p;
  98. klist_add(node->sending, n);
  99. return 0;
  100. }
  101. /* send to each user include who sends this message */
  102. int vcc_message_broadcast(char *msg) {
  103. struct klist_node *n;
  104. int i;
  105. char *buf;
  106. for (n = connections.next, i = 0; n != &connections; n = n->next, i++) {
  107. if (unlikely(!(buf = malloc(REQ_SIZE)))) {
  108. vcc_log("*** malloc() failed\n");
  109. continue;
  110. }
  111. strcpy(buf, msg);
  112. vcc_send_req(n, buf);
  113. }
  114. vcc_log("Message sent to %d users. \n", i);
  115. return 0;
  116. }
  117. int vcc_new_msg(int fd, char *msg) {
  118. if (((unsigned char *) msg)[0] == 0xffu)
  119. return 0;
  120. vcc_log("New message: %s\n", msg);
  121. vcc_message_broadcast(msg);
  122. return 0;
  123. }