/* vccd/vcc.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 */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include struct klist_node connections = KLIST_NODE_INIT(&connections); int vcc_message_broadcast(char *msg); int nusers = 0; int vcc_new_connection(int fd, struct in_addr *addr) { struct klist_node *n; char *new_msg; const static char join_msg[] = {"system: New user from %s joined. Now there are %d users. \n"}; vcc_log("New connection from %s\n", inet_ntoa(*addr)); if (unlikely(!(n = malloc(sizeof(struct klist_node))))) { vcc_log("*** malloc() failed\n"); return 1; } klist_init(n); n->data = addr; n->value = fd; if (unlikely(!(n->sending = malloc(sizeof(struct klist_node))))) { vcc_log("*** malloc() failed\n"); return 1; } klist_init(n->sending); klist_add(&connections, n); if (unlikely(!(new_msg = malloc(128)))) { vcc_log("*** malloc() failed\n"); return 1; } sprintf(new_msg, join_msg, inet_ntoa(*addr), ++nusers); vcc_message_broadcast(new_msg); free(new_msg); return 0; } int vcc_close_connection(int fd) { struct klist_node *n, *t; const static char leave_msg[] = {"system: A user left, now there are only %d users here. \n"}; char *new_msg; for (n = connections.next; n != &connections && n->value != fd; n = n->next) ; if (unlikely(!(new_msg = malloc(128)))) { vcc_log("*** malloc() failed\n"); return 1; } sprintf(new_msg, leave_msg, --nusers); klist_del(&connections, n); for (t = n->sending->next; t != n->sending; t = n->next) free(t->data); free(n->sending); vcc_log("Client closed. \n"); vcc_message_broadcast(new_msg); free(new_msg); return 0; } struct klist_node *find_connection(int fd) { struct klist_node *n; for (n = connections.next; n != &connections && n->value != fd; n = n->next) ; if (unlikely(n == &connections)) { vcc_log("--- connection not found: %d\n", fd); return NULL; } return n; } int write_packages(struct klist_node *node) { void *p; if (unlikely(!node->sending)) return 0; if (unlikely(klist_empty(node->sending))) return 0; p = node->sending->next->data; write(node->value, p, REQ_SIZE); free(p); klist_del(node->sending, node->sending->next); return 0; } int vcc_write_packages(int fd) { return write_packages(find_connection(fd)); } int vcc_send_req(struct klist_node *node, void *p) { struct klist_node *n; if (unlikely(!(n = malloc(sizeof(struct klist_node))))) { vcc_log("*** malloc() failed\n"); return 1; } n->data = p; klist_add(node->sending, n); return 0; } /* send to each user include who sends this message */ int vcc_message_broadcast(char *msg) { struct vcc_request *req; struct klist_node *n; int i; for (n = connections.next, i = 0; n != &connections; n = n->next, i++) { if (unlikely(!(req = malloc(sizeof(struct vcc_request))))) { vcc_log("*** malloc() failed\n"); continue; } strcpy(req->msg, msg); vcc_send_req(n, req); } vcc_log("Message sent to %d users. \n", i); return 0; } int vcc_new_msg(int fd, struct vcc_request *req) { char *msg = req->msg; if (((unsigned char *) msg)[0] == 0xffu || !*msg) return 0; vcc_log("New message: %s\n", msg); vcc_message_broadcast(msg); return 0; }