vcc.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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_message_broadcast(char *msg, char *usrname);
  36. int nusers = 0;
  37. int vcc_new_connection(int fd, struct in_addr *addr) {
  38. struct klist_node *n;
  39. char *new_msg;
  40. const static char join_msg[] = {"New user from %s joined. Now there are %d users. \n"};
  41. vcc_log("New connection from %s\n", inet_ntoa(*addr));
  42. if (unlikely(!(n = malloc(sizeof(struct klist_node))))) {
  43. vcc_log("*** malloc() failed\n");
  44. return 1;
  45. }
  46. klist_init(n);
  47. n->data = addr;
  48. n->value = fd;
  49. if (unlikely(!(n->sending = malloc(sizeof(struct klist_node))))) {
  50. vcc_log("*** malloc() failed\n");
  51. return 1;
  52. }
  53. klist_init(n->sending);
  54. klist_add(&connections, n);
  55. if (unlikely(!(new_msg = malloc(128)))) {
  56. vcc_log("*** malloc() failed\n");
  57. return 1;
  58. }
  59. sprintf(new_msg, join_msg, inet_ntoa(*addr), ++nusers);
  60. vcc_message_broadcast(new_msg, "system");
  61. free(new_msg);
  62. return 0;
  63. }
  64. int vcc_close_connection(int fd) {
  65. struct klist_node *n, *t;
  66. const static char leave_msg[] = {"An user left, now there are only %d users here. \n"};
  67. char *new_msg;
  68. for (n = connections.next; n != &connections
  69. && n->value != fd; n = n->next)
  70. ;
  71. if (unlikely(!(new_msg = malloc(128)))) {
  72. vcc_log("*** malloc() failed\n");
  73. return 1;
  74. }
  75. sprintf(new_msg, leave_msg, --nusers);
  76. klist_del(&connections, n);
  77. for (t = n->sending->next; t != n->sending; t = n->next)
  78. free(t->data);
  79. free(n->sending);
  80. vcc_log("Client closed. \n");
  81. vcc_message_broadcast(new_msg, "system");
  82. free(new_msg);
  83. return 0;
  84. }
  85. struct klist_node *find_connection(int fd) {
  86. struct klist_node *n;
  87. for (n = connections.next; n != &connections
  88. && n->value != fd; n = n->next)
  89. ;
  90. if (unlikely(n == &connections)) {
  91. vcc_log("--- connection not found: %d\n", fd);
  92. return NULL;
  93. }
  94. return n;
  95. }
  96. int write_packages(struct klist_node *node) {
  97. void *p;
  98. if (unlikely(!node->sending))
  99. return 0;
  100. if (unlikely(klist_empty(node->sending)))
  101. return 0;
  102. p = node->sending->next->data;
  103. write(node->value, p, REQ_SIZE);
  104. free(p);
  105. klist_del(node->sending, node->sending->next);
  106. return 0;
  107. }
  108. int vcc_write_packages(int fd) {
  109. return write_packages(find_connection(fd));
  110. }
  111. int vcc_send_req(struct klist_node *node, void *p) {
  112. struct klist_node *n;
  113. if (unlikely(!(n = malloc(sizeof(struct klist_node))))) {
  114. vcc_log("*** malloc() failed\n");
  115. return 1;
  116. }
  117. n->data = p;
  118. klist_add(node->sending, n);
  119. return 0;
  120. }
  121. /* send to each user include who sends this message */
  122. int vcc_message_broadcast(char *msg, char *usrname) {
  123. struct vcc_request *req;
  124. struct klist_node *n;
  125. int i;
  126. for (n = connections.next, i = 0; n != &connections; n = n->next, i++) {
  127. if (unlikely(!(req = malloc(sizeof(struct vcc_request))))) {
  128. vcc_log("*** malloc() failed\n");
  129. continue;
  130. }
  131. req->magic = htonl(VCC_MAGIC);
  132. req->reqtype = htonl(REQ_MSG_NEW);
  133. req->uid = 0;
  134. strcpy(req->msg, msg);
  135. strcpy(req->usrname, usrname);
  136. vcc_send_req(n, req);
  137. }
  138. vcc_log("Message sent to %d users. \n", i);
  139. return 0;
  140. }
  141. int vcc_update_usrname(int fd, const char *usrname) {
  142. struct klist_node *n = find_connection(fd);
  143. if (unlikely(!n))
  144. return -1;
  145. if (likely(n->usrname))
  146. return 0;
  147. n->usrname = strdup(usrname);
  148. vcc_log("user name %s updated for %d\n", usrname, fd);
  149. return 0;
  150. }
  151. int send_users(int fd) {
  152. struct vcc_request *req;
  153. struct klist_node *n;
  154. int i;
  155. if (unlikely(!(req = malloc(sizeof(struct vcc_request))))) {
  156. vcc_log("*** malloc() failed\n");
  157. return 1;
  158. }
  159. memset(req, 0, REQ_SIZE);
  160. req->magic = htonl(VCC_MAGIC);
  161. req->reqtype = htonl(REQ_CTL_USRS);
  162. for (n = connections.next, i = 0; n != &connections; n = n->next, i++) {
  163. strcpy(req->msg + i * USRNAME_SIZE, n->usrname ? n->usrname : "unnamed");
  164. if (unlikely(i >= MSG_SIZE / USRNAME_SIZE)) {
  165. vcc_log("--- too many users\n");
  166. break;
  167. }
  168. }
  169. req->uid = htonl(i);
  170. vcc_send_req(find_connection(fd), req);
  171. vcc_log("%d user names sent to %d\n", i, fd);
  172. return 0;
  173. }
  174. int vcc_new_msg(int fd, struct vcc_request *req) {
  175. char *msg = req->msg;
  176. if (unlikely(ntohl(req->magic) != VCC_MAGIC)) {
  177. vcc_log("--- bad magic number from %d\n", fd);
  178. return 0;
  179. }
  180. switch (htonl(req->reqtype)) {
  181. case REQ_MSG_SEND:
  182. if (((unsigned char *) msg)[0] == 0xffu || !*msg)
  183. return 0;
  184. vcc_update_usrname(fd, req->usrname);
  185. vcc_log("New message: %s\n", msg);
  186. vcc_message_broadcast(msg, req->usrname);
  187. break;
  188. case REQ_CTL_USRS:
  189. send_users(fd);
  190. break;
  191. default:
  192. vcc_log("--- bad request type from %d\n", fd);
  193. break;
  194. }
  195. return 0;
  196. }