session.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 = strndup(name, USRNAME_SIZE)))) {
  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 fd_in_session(struct klist_node *session, int fd) {
  71. struct klist_node *s = session->session, *n;
  72. for (n = s->next; n != s; n = n->next)
  73. if (n->value == fd)
  74. return 1;
  75. return 0;
  76. }
  77. int join_session(int sid, int fd) {
  78. struct klist_node *session, *n;
  79. static const char *join_msg = "%s joined session %s, now there are %d users in this session. \n";
  80. char *t;
  81. if (unlikely(!(session = find_session(sid))))
  82. return 1;
  83. if (fd_in_session(session, fd)) {
  84. vcc_log("%d is already in the session %d. \n", fd, sid);
  85. return 0;
  86. }
  87. if (unlikely(!(n = amalloc(sizeof(struct klist_node))))) {
  88. vcc_log("*** malloc() failed\n");
  89. return 1;
  90. }
  91. if (unlikely(!(t = amalloc(128)))) {
  92. vcc_log("*** malloc() failed\n");
  93. return 1;
  94. }
  95. klist_init(n);
  96. n->value = fd;
  97. n->connection = find_connection(fd);
  98. klist_add(session->session, n);
  99. sprintf(t, join_msg, n->connection->usrname, session->usrname, ++session->session->nusers);
  100. session_broadcast(sid, t, "system", 0, 0);
  101. vcc_log("%d joined session %d. \n", fd, sid);
  102. afree(t, 128);
  103. return 0;
  104. }
  105. int session_broadcast(int sid, char *msg, char *usrname, int except, int flags) {
  106. struct klist_node *session, *n;
  107. struct vcc_request req, *out;
  108. int i;
  109. if (unlikely(!(session = find_session(sid))))
  110. return 1;
  111. memset(&req, 0, REQ_SIZE);
  112. req.magic = htonl(VCC_MAGIC);
  113. req.reqtype = htonl(REQ_MSG_NEW);
  114. req.uid = 0;
  115. req.session = htonl(sid);
  116. req.flags = flags;
  117. memcpy(req.msg, msg, MSG_SIZE);
  118. strncpy(req.usrname, usrname, USRNAME_SIZE);
  119. i = 0;
  120. for (n = session->session->next; n != session->session; n = n->next) {
  121. if (n->value == except)
  122. continue;
  123. if (unlikely(!(out = amalloc(REQ_SIZE)))) {
  124. vcc_log("*** malloc() failed\n");
  125. return 1;
  126. }
  127. memcpy(out, &req, REQ_SIZE);
  128. vcc_send_req(n->connection, out);
  129. i++;
  130. }
  131. vcc_log("(%d) Message sent to %d users. \n", sid, i);
  132. return 0;
  133. }
  134. int send_sessions(int fd) {
  135. struct klist_node *c, *n;
  136. struct vcc_request *req;
  137. int i;
  138. if (unlikely(!(c = find_connection(fd)))) {
  139. vcc_log("*** malloc() failed\n");
  140. return 1;
  141. }
  142. if (unlikely(!(req = amalloc(REQ_SIZE)))) {
  143. vcc_log("*** malloc() failed\n");
  144. return 1;
  145. }
  146. req->magic = htonl(VCC_MAGIC);
  147. req->reqtype = htonl(REQ_CTL_SESS);
  148. for (n = sessions.next, i = 0; n != &sessions; n = n->next, i++) {
  149. strncpy(req->msg + i * USRNAME_SIZE, n->usrname, USRNAME_SIZE);
  150. if (unlikely(i >= (int) MSG_SIZE / USRNAME_SIZE)) {
  151. vcc_log("--- too many sessions. \n");
  152. break;
  153. }
  154. }
  155. req->uid = htonl(i);
  156. vcc_send_req(find_connection(fd), req);
  157. vcc_log("%d session names sent to %d. \n", i, fd);
  158. return 0;
  159. }