| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- /* 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 <https://www.gnu.org/licenses/>
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <string.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <poll.h>
- #include <vcc/vcc.h>
- #include <vcc/vccd.h>
- #include <vcc/version.h>
- #include <klist.h>
- struct klist_node connections = KLIST_NODE_INIT(&connections);
- extern int nusers;
- int vcc_new_connection(int fd, struct in_addr *addr) {
- struct klist_node *n;
- vcc_log("New connection from %s\n", inet_ntoa(*addr));
- if (unlikely(!(n = amalloc(sizeof(struct klist_node))))) {
- vcc_log("*** malloc() failed\n");
- return 1;
- }
- klist_init(n);
- n->data = addr;
- n->value = fd;
- if (unlikely(!(n->sending = amalloc(sizeof(struct klist_node))))) {
- vcc_log("*** malloc() failed\n");
- return 1;
- }
- klist_init(n->sending);
- klist_add(&connections, n);
- /* if we kill vcc before it logs in, the nusers will dec but it
- * didn't login the value will be something bad. */
- nusers++;
- return 0;
- }
- int vcc_close_connection(int fd) {
- struct klist_node *n, *t;
- if (unlikely(!(n = find_connection(fd))))
- return 1;
- do_user_logout(fd);
- klist_del(&connections, n);
- for (t = n->sending->next; t != n->sending; t = t->next)
- /* it's dangerous here */
- if (t->data)
- afree(t->data, REQ_SIZE);
-
- afree(n->sending, sizeof(struct klist_node));
- vcc_log("Client closed. \n");
- 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);
- afree(p, REQ_SIZE);
- 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 = amalloc(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, char *usrname, int except_fd) {
- struct vcc_request *req;
- struct klist_node *n;
- int i;
- for (n = connections.next, i = 0; n != &connections; n = n->next) {
- if (n->value == except_fd)
- continue;
- if (unlikely(!(req = amalloc(sizeof(struct vcc_request))))) {
- vcc_log("*** malloc() failed\n");
- continue;
- }
- req->magic = htonl(VCC_MAGIC);
- req->reqtype = htonl(REQ_MSG_NEW);
- req->uid = 0;
- strcpy(req->msg, msg);
- strcpy(req->usrname, usrname);
- vcc_send_req(n, req);
- i++;
- }
- vcc_log("Message sent to %d users. \n", i);
- return 0;
- }
- int vcc_update_usrname(int fd, const char *usrname) {
- struct klist_node *n = find_connection(fd);
- if (unlikely(!n))
- return -1;
- if (likely(n->usrname))
- return 0;
- n->usrname = strdup(usrname);
- vcc_log("user name %s updated for %d\n", usrname, fd);
- return 0;
- }
- int send_users(int fd) {
- struct vcc_request *req;
- struct klist_node *n;
- int i;
- if (unlikely(!(req = amalloc(sizeof(struct vcc_request))))) {
- vcc_log("*** malloc() failed\n");
- return 1;
- }
- memset(req, 0, REQ_SIZE);
- req->magic = htonl(VCC_MAGIC);
- req->reqtype = htonl(REQ_CTL_USRS);
- for (n = connections.next, i = 0; n != &connections; n = n->next, i++) {
- strcpy(req->msg + i * USRNAME_SIZE, n->usrname ? n->usrname : "unnamed");
- if (unlikely(i >= (int) MSG_SIZE / USRNAME_SIZE)) {
- vcc_log("--- too many users\n");
- break;
- }
- }
- req->uid = htonl(i);
- vcc_send_req(find_connection(fd), req);
- vcc_log("%d user names sent to %d\n", i, fd);
- return 0;
- }
- int vcc_new_msg(int fd, struct vcc_request *req) {
- struct klist_node *n = find_connection(fd);
- char *msg;
- int sid;
- if (unlikely(!n))
- return 1;
- msg = req->msg;
- if (unlikely(ntohl(req->magic) != VCC_MAGIC)) {
- vcc_log("--- bad magic number from %d\n", fd);
- return 0;
- }
- switch (htonl(req->reqtype)) {
- case REQ_MSG_NEW:
- /* POLLOUT will be set, just because they sent us a message */
- return 0;
- case REQ_MSG_SEND:
- if (((unsigned char *) msg)[0] == 0xffu || !*msg)
- return 0;
- sid = ntohl(req->session);
- vcc_log("(%d) New message: %s\n", sid, msg);
- if (!sid)
- vcc_message_broadcast(msg, req->usrname, fd);
- else
- session_broadcast(sid, msg, req->usrname, fd);
- break;
- case REQ_CTL_USRS:
- send_users(fd);
- break;
- case REQ_CTL_LOGIN:
- do_user_login(fd, req);
- vcc_update_usrname(fd, req->usrname);
- break;
- case REQ_CTL_NEWSE:
- create_session(fd, req->usrname);
- break;
- case REQ_CTL_SESS:
- send_sessions(fd);
- break;
- default:
- vcc_log("--- bad request type from %d\n", fd);
- break;
- }
- return 0;
- }
|