|
|
@@ -0,0 +1,184 @@
|
|
|
+/* vccd/session.c
|
|
|
+ *
|
|
|
+ * This file is part of vccd.
|
|
|
+ *
|
|
|
+ * vccd is free software: you can redistribute it and/or modify
|
|
|
+ * it under the terms of the GNU General Public License as published by
|
|
|
+ * the Free Software Foundation, either version 3 of the License, or
|
|
|
+ * (at your option) any later version.
|
|
|
+ *
|
|
|
+ * vccd is distributed in the hope that it will be useful,
|
|
|
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
+ * GNU General Public License for more details.
|
|
|
+ *
|
|
|
+ * You should have received a copy of the GNU General Public License
|
|
|
+ * along with vccd. If not, see <https://www.gnu.org/licenses/>
|
|
|
+ */
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#include <stdio.h>
|
|
|
+#include <stdlib.h>
|
|
|
+#include <unistd.h>
|
|
|
+#include <fcntl.h>
|
|
|
+#include <string.h>
|
|
|
+#include <sys/times.h>
|
|
|
+
|
|
|
+#include <config.h>
|
|
|
+#include <vcc/vccd.h>
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+struct klist_node sessions = KLIST_NODE_INIT(&sessions);
|
|
|
+int next_sid = 1;
|
|
|
+
|
|
|
+
|
|
|
+/* it takes 3 * sizeof(struct klist_node) size to create a new session, it's
|
|
|
+ * not a small one. */
|
|
|
+
|
|
|
+int create_session(int fd, char *name) {
|
|
|
+ struct klist_node *n, *s, *p;
|
|
|
+
|
|
|
+ if (unlikely(!(n = amalloc(sizeof(struct klist_node))))) {
|
|
|
+ vcc_log("*** malloc() failed\n");
|
|
|
+
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (unlikely(!(s = amalloc(sizeof(struct klist_node))))) {
|
|
|
+ vcc_log("*** malloc() failed\n");
|
|
|
+
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (unlikely(!(p = amalloc(sizeof(struct klist_node))))) {
|
|
|
+ vcc_log("*** malloc() failed\n");
|
|
|
+
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ klist_init(n);
|
|
|
+ klist_init(s);
|
|
|
+ klist_init(p);
|
|
|
+
|
|
|
+ n->value = next_sid++;
|
|
|
+
|
|
|
+ if (unlikely(!(n->usrname = strdup(name)))) {
|
|
|
+ vcc_log("*** malloc() failed\n");
|
|
|
+
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ klist_add(&sessions, n);
|
|
|
+
|
|
|
+ s->value = fd;
|
|
|
+ s->nusers = 1;
|
|
|
+ n->session = s;
|
|
|
+
|
|
|
+ p->value = fd;
|
|
|
+ p->connection = find_connection(fd);
|
|
|
+
|
|
|
+ klist_add(s, p);
|
|
|
+
|
|
|
+ vcc_log("New session '%s' created by %d. \n", name, fd);
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+struct klist_node *find_session(int sid) {
|
|
|
+ struct klist_node *n;
|
|
|
+
|
|
|
+ for (n = sessions.next; n != &sessions; n = n->next)
|
|
|
+ if (n->value == sid)
|
|
|
+ return n;
|
|
|
+
|
|
|
+ vcc_log("--- session not found. \n");
|
|
|
+
|
|
|
+ return NULL;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+int session_broadcast(int sid, char *msg, char *usrname, int except) {
|
|
|
+ struct klist_node *session, *n;
|
|
|
+ struct vcc_request req, *out;
|
|
|
+ int i;
|
|
|
+
|
|
|
+ if (unlikely(!(session = find_session(sid))))
|
|
|
+ return 1;
|
|
|
+
|
|
|
+ memset(&req, 0, REQ_SIZE);
|
|
|
+
|
|
|
+ req.magic = htonl(VCC_MAGIC);
|
|
|
+ req.reqtype = htonl(REQ_MSG_NEW);
|
|
|
+ req.uid = 0;
|
|
|
+
|
|
|
+ strcpy(req.msg, msg);
|
|
|
+ strcpy(req.usrname, usrname);
|
|
|
+
|
|
|
+ i = 0;
|
|
|
+
|
|
|
+ for (n = session->session->next; n != session->session; n = n->next) {
|
|
|
+ if (n->value == except)
|
|
|
+ continue;
|
|
|
+
|
|
|
+ if (unlikely(!(out = amalloc(REQ_SIZE)))) {
|
|
|
+ vcc_log("*** malloc() failed\n");
|
|
|
+
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ memcpy(out, &req, REQ_SIZE);
|
|
|
+ vcc_send_req(n->connection, out);
|
|
|
+
|
|
|
+ i++;
|
|
|
+ }
|
|
|
+
|
|
|
+ vcc_log("(%d) Message sent to %d users. \n", sid, i);
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+int send_sessions(int fd) {
|
|
|
+ struct klist_node *c, *n;
|
|
|
+ struct vcc_request *req;
|
|
|
+ int i;
|
|
|
+
|
|
|
+ if (unlikely(!(c = find_connection(fd)))) {
|
|
|
+ vcc_log("*** malloc() failed\n");
|
|
|
+
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (unlikely(!(req = amalloc(REQ_SIZE)))) {
|
|
|
+ vcc_log("*** malloc() failed\n");
|
|
|
+
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ req->magic = htonl(VCC_MAGIC);
|
|
|
+ req->reqtype = htonl(REQ_CTL_SESS);
|
|
|
+
|
|
|
+ for (n = sessions.next, i = 0; n != &sessions; n = n->next, i++) {
|
|
|
+ strcpy(req->msg + i * USRNAME_SIZE, n->usrname);
|
|
|
+
|
|
|
+ if (unlikely(i >= (int) MSG_SIZE / USRNAME_SIZE)) {
|
|
|
+ vcc_log("--- too many sessions. \n");
|
|
|
+
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ req->uid = htonl(i);
|
|
|
+
|
|
|
+ vcc_send_req(find_connection(fd), req);
|
|
|
+ vcc_log("%d session names sent to %d. \n", i, fd);
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|