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