session.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. vcc_info("INEWSESS %s %d\n", name, fd);
  61. return 0;
  62. }
  63. struct klist_node *find_session(int sid) {
  64. struct klist_node *n;
  65. for (n = sessions.next; n != &sessions; n = n->next)
  66. if (n->value == sid)
  67. return n;
  68. vcc_log("--- session not found. \n");
  69. return NULL;
  70. }
  71. int fd_in_session(struct klist_node *session, int fd) {
  72. struct klist_node *s = session->session, *n;
  73. for (n = s->next; n != s; n = n->next)
  74. if (n->value == fd)
  75. return 1;
  76. return 0;
  77. }
  78. int join_session(int sid, int fd) {
  79. struct klist_node *session, *n;
  80. static const char *join_msg = "%s joined session %s, now there are %d users in this session. \n";
  81. char *t;
  82. if (unlikely(!(session = find_session(sid))))
  83. return 1;
  84. if (fd_in_session(session, fd)) {
  85. vcc_log("%d is already in the session %d. \n", fd, sid);
  86. return 0;
  87. }
  88. if (unlikely(!(n = amalloc(sizeof(struct klist_node))))) {
  89. vcc_log("*** malloc() failed\n");
  90. return 1;
  91. }
  92. if (unlikely(!(t = amalloc(128)))) {
  93. vcc_log("*** malloc() failed\n");
  94. return 1;
  95. }
  96. klist_init(n);
  97. n->value = fd;
  98. n->connection = find_connection(fd);
  99. klist_add(session->session, n);
  100. sprintf(t, join_msg, n->connection->usrname, session->usrname, ++session->session->nusers);
  101. session_broadcast(sid, t, "system", 0, 0);
  102. vcc_log("%d joined session %d. \n", fd, sid);
  103. afree(t, 128);
  104. return 0;
  105. }
  106. int quit_session(int sid, int fd) {
  107. struct klist_node *session, *n;
  108. static const char *leave_msg = "%s left session %s, now there are %d users in this session. \n";
  109. char *t;
  110. if (unlikely(!(session = find_session(sid))))
  111. return 1;
  112. if (!fd_in_session(session, fd)) {
  113. vcc_log("%d isn't in the session %d yet\n", fd, sid);
  114. return 0;
  115. }
  116. if (unlikely(!(t = amalloc(128)))) {
  117. vcc_log("*** malloc() failed\n");
  118. return 1;
  119. }
  120. for (n = session->session->next; n != session->session; n = n->next)
  121. if (n->value == fd)
  122. break;
  123. sprintf(t, leave_msg, n->connection->usrname, session->usrname, --session->session->nusers);
  124. session_broadcast(sid, t, "system", fd, 0);
  125. vcc_log("%d left session %d. \n", fd, sid);
  126. klist_del(session->session, n);
  127. afree(n, sizeof(struct klist_node));
  128. afree(t, 128);
  129. return 0;
  130. }
  131. int quit_all_session(int fd) {
  132. struct klist_node *n;
  133. int i;
  134. for (n = sessions.next; n != &sessions; n = n->next) {
  135. i = n->value;
  136. if (fd_in_session(n, fd))
  137. quit_session(i, fd);
  138. }
  139. return 0;
  140. }
  141. int session_broadcast(int sid, char *msg, char *usrname, int except, int flags) {
  142. struct klist_node *session, *n;
  143. struct vcc_request req, *out;
  144. int i;
  145. if (unlikely(!(session = find_session(sid))))
  146. return 1;
  147. memset(&req, 0, REQ_SIZE);
  148. req.magic = htonl(VCC_MAGIC);
  149. req.reqtype = htonl(REQ_MSG_NEW);
  150. req.uid = 0;
  151. req.session = htonl(sid);
  152. req.flags = flags;
  153. memcpy(req.msg, msg, MSG_SIZE);
  154. strncpy(req.usrname, usrname, USRNAME_SIZE);
  155. i = 0;
  156. for (n = session->session->next; n != session->session; n = n->next) {
  157. if (n->value == except)
  158. continue;
  159. if (unlikely(!(out = amalloc(REQ_SIZE)))) {
  160. vcc_log("*** malloc() failed\n");
  161. return 1;
  162. }
  163. memcpy(out, &req, REQ_SIZE);
  164. vcc_send_req(n->connection, out);
  165. i++;
  166. }
  167. vcc_write_all();
  168. vcc_log("(%d) Message sent to %d users. \n", sid, i);
  169. return 0;
  170. }
  171. int session_name(int fd, struct vcc_request *req) {
  172. struct vcc_request *out;
  173. struct klist_node *n;
  174. char *p;
  175. if (unlikely(!(out = amalloc(sizeof(struct vcc_request))))) {
  176. vcc_log("*** malloc() failed\n");
  177. return 1;
  178. }
  179. out->magic = htonl(VCC_MAGIC);
  180. out->reqtype = htonl(REQ_CTL_SENAME);
  181. if (req->session == 0)
  182. p = "broadcast";
  183. else {
  184. if (unlikely(!(n = find_session(ntohl(req->session))))) {
  185. out->session = htonl(-1);
  186. vcc_send_req(find_connection(fd), out);
  187. vcc_log("%d sends a bad session number. \n", fd);
  188. return 1;
  189. }
  190. p = n->usrname;
  191. }
  192. strcpy(out->msg, p);
  193. out->session = req->session;
  194. vcc_send_req(find_connection(fd), out);
  195. vcc_log("sename: %s sent to %d\n", p, fd);
  196. return 0;
  197. }
  198. int send_sessions(int fd) {
  199. struct klist_node *c, *n;
  200. struct vcc_request *req;
  201. int i;
  202. if (unlikely(!(c = find_connection(fd)))) {
  203. vcc_log("*** malloc() failed\n");
  204. return 1;
  205. }
  206. if (unlikely(!(req = amalloc(REQ_SIZE)))) {
  207. vcc_log("*** malloc() failed\n");
  208. return 1;
  209. }
  210. req->magic = htonl(VCC_MAGIC);
  211. req->reqtype = htonl(REQ_CTL_SESS);
  212. for (n = sessions.next, i = 0; n != &sessions; n = n->next, i++) {
  213. strncpy(req->msg + i * USRNAME_SIZE, n->usrname, USRNAME_SIZE);
  214. if (unlikely(i >= (int) MSG_SIZE / USRNAME_SIZE)) {
  215. vcc_log("--- too many sessions. \n");
  216. break;
  217. }
  218. }
  219. req->uid = htonl(i);
  220. vcc_send_req(find_connection(fd), req);
  221. vcc_log("%d session names sent to %d. \n", i, fd);
  222. return 0;
  223. }