session.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* vccd/session.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 <string.h>
  23. #include <sys/times.h>
  24. #include <config.h>
  25. #include <vcc/vccd.h>
  26. struct klist_node sessions = KLIST_NODE_INIT(&sessions);
  27. int next_sid = 1;
  28. /* it takes 3 * sizeof(struct klist_node) size to create a new session, it's
  29. * not a small one. */
  30. int create_session(int fd, char *name) {
  31. struct klist_node *n, *s, *p;
  32. if (unlikely(!(n = amalloc(sizeof(struct klist_node))))) {
  33. vcc_log("*** malloc() failed\n");
  34. return 1;
  35. }
  36. if (unlikely(!(s = amalloc(sizeof(struct klist_node))))) {
  37. vcc_log("*** malloc() failed\n");
  38. return 1;
  39. }
  40. if (unlikely(!(p = amalloc(sizeof(struct klist_node))))) {
  41. vcc_log("*** malloc() failed\n");
  42. return 1;
  43. }
  44. klist_init(n);
  45. klist_init(s);
  46. klist_init(p);
  47. n->value = next_sid++;
  48. if (unlikely(!(n->usrname = strdup(name)))) {
  49. vcc_log("*** malloc() failed\n");
  50. return 1;
  51. }
  52. klist_add(&sessions, n);
  53. s->value = fd;
  54. s->nusers = 1;
  55. n->session = s;
  56. p->value = fd;
  57. p->connection = find_connection(fd);
  58. klist_add(s, p);
  59. vcc_log("New session '%s' created by %d. \n", name, fd);
  60. return 0;
  61. }
  62. struct klist_node *find_session(int sid) {
  63. struct klist_node *n;
  64. for (n = sessions.next; n != &sessions; n = n->next)
  65. if (n->value == sid)
  66. return n;
  67. vcc_log("--- session not found. \n");
  68. return NULL;
  69. }
  70. int session_broadcast(int sid, char *msg, char *usrname, int except) {
  71. struct klist_node *session, *n;
  72. struct vcc_request req, *out;
  73. int i;
  74. if (unlikely(!(session = find_session(sid))))
  75. return 1;
  76. memset(&req, 0, REQ_SIZE);
  77. req.magic = htonl(VCC_MAGIC);
  78. req.reqtype = htonl(REQ_MSG_NEW);
  79. req.uid = 0;
  80. strcpy(req.msg, msg);
  81. strcpy(req.usrname, usrname);
  82. i = 0;
  83. for (n = session->session->next; n != session->session; n = n->next) {
  84. if (n->value == except)
  85. continue;
  86. if (unlikely(!(out = amalloc(REQ_SIZE)))) {
  87. vcc_log("*** malloc() failed\n");
  88. return 1;
  89. }
  90. memcpy(out, &req, REQ_SIZE);
  91. vcc_send_req(n->connection, out);
  92. i++;
  93. }
  94. vcc_log("(%d) Message sent to %d users. \n", sid, i);
  95. return 0;
  96. }
  97. int send_sessions(int fd) {
  98. struct klist_node *c, *n;
  99. struct vcc_request *req;
  100. int i;
  101. if (unlikely(!(c = find_connection(fd)))) {
  102. vcc_log("*** malloc() failed\n");
  103. return 1;
  104. }
  105. if (unlikely(!(req = amalloc(REQ_SIZE)))) {
  106. vcc_log("*** malloc() failed\n");
  107. return 1;
  108. }
  109. req->magic = htonl(VCC_MAGIC);
  110. req->reqtype = htonl(REQ_CTL_SESS);
  111. for (n = sessions.next, i = 0; n != &sessions; n = n->next, i++) {
  112. strcpy(req->msg + i * USRNAME_SIZE, n->usrname);
  113. if (unlikely(i >= (int) MSG_SIZE / USRNAME_SIZE)) {
  114. vcc_log("--- too many sessions. \n");
  115. break;
  116. }
  117. }
  118. req->uid = htonl(i);
  119. vcc_send_req(find_connection(fd), req);
  120. vcc_log("%d session names sent to %d. \n", i, fd);
  121. return 0;
  122. }