| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425 |
- /* vcc/vcc.c
- *
- * This file is part of vcc.
- *
- * vcc 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.
- *
- * vcc 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 vcc. 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 <signal.h>
- #include <klist.h>
- #include <vcc/vcc.h>
- #include <vcc/plugin.h>
- #include <vcc/version.h>
- #include <vcc/interfaces.h>
- #define clear_req(req) memset(req, 0, sizeof(*req))
- int current_sid = 0;
- int init_socket(const char *server_addr, int server_port) {
- struct sockaddr_in addr, server;
- if (unlikely((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)) {
- perror("socket()");
- exit(1);
- }
- memset(&addr, 0, sizeof(struct sockaddr_in));
- addr.sin_family = AF_INET;
- addr.sin_addr.s_addr = htonl(INADDR_ANY);
- addr.sin_port = htons(0);
- if (unlikely(bind(fd, (struct sockaddr *) &addr, sizeof(struct sockaddr_in)) < 0)) {
- perror("bind()");
- exit(1);
- }
- memset(&server, 0, sizeof(struct sockaddr_in));
- server.sin_family = AF_INET;
- server.sin_port = htons(server_port);
- if (unlikely(!inet_aton(server_addr, &server.sin_addr))) {
- fprintf(stderr, _("invalid ip address. "
- "leave blank if you want to connect the default server. \n"));
- exit(1);
- }
- if (unlikely(connect(fd, (struct sockaddr *) &server, sizeof(struct sockaddr_in)) < 0)) {
- perror("connect()");
- exit(1);
- }
- return fd;
- }
- int __send_msg(const char *msg, const char *usrname, int flags) {
- struct vcc_request req;
- clear_req(&req);
- req.magic = htonl(VCC_MAGIC);
- req.reqtype = htonl(REQ_MSG_SEND);
- req.uid = 0;
- req.session = htonl(current_sid);
- req.flags = htonl(flags);
-
- strncpy(req.usrname, usrname, USRNAME_SIZE);
- strncpy(req.msg, msg, MSG_SIZE);
- write(fd, &req, REQ_SIZE);
- return 0;
- }
- extern inline int send_msg(const char *msg, const char *usrname) {
- return __send_msg(msg, usrname, 0);
- }
- extern inline int send_msg_encrypted(const char *msg, const char *usrname) {
- return __send_msg(msg, usrname, FLAG_ENCRYPTED);
- }
- int send_relay_msg(const char *msg, const char *usrname, const char *visible) {
- struct vcc_relay_header req;
- int len;
- len = strlen(msg);
- clear_req(&req);
- req.magic = htonl(VCC_MAGIC_RL);
- req.reqtype = htonl(REQ_REL_MSG);
- req.uid = 0;
- req.session = htonl(current_sid);
- req.size = htonl(sizeof(struct vcc_relay_header) + len);
- strncpy(req.usrname, usrname, USRNAME_SIZE);
- if (visible)
- strncpy(req.visible, visible, USRNAME_SIZE);
- else
- memset(req.visible, 0, USRNAME_SIZE);
- write(fd, &req, sizeof(struct vcc_relay_header));
- write(fd, msg, len);
- return 0;
- }
- int read_until(int fd, void *p, int until) {
- int did_read = 0;
- int n;
- while ((did_read += n = read(fd, p, until)) < until) {
- if (!n)
- return 0;
- p += n;
- }
-
- return until;
- }
- char *relay_get_msg(int fd, struct vcc_relay_header *hdr) {
- char *p;
- int size;
- size = ntohl(hdr->size);
- if (unlikely(!(p = malloc(size)))) {
- fprintf(stderr, "*** malloc() failed\n");
- return NULL;
- }
- memset(p, 0, size);
- read_until(fd, p, size - sizeof(struct vcc_relay_header));
- return p;
- }
- int get_user_info(char *usrname) {
- struct vcc_request req;
- clear_req(&req);
- req.magic = htonl(VCC_MAGIC);
- req.reqtype = htonl(REQ_CTL_UINFO);
- req.uid = 0;
- strncpy(req.msg, usrname, USRNAME_SIZE);
- write(fd, &req, REQ_SIZE);
-
- return 0;
- }
- int get_users(void) {
- struct vcc_request req;
- clear_req(&req);
- req.magic = htonl(VCC_MAGIC);
- req.reqtype = htonl(REQ_CTL_USRS);
- req.uid = 0;
- write(fd, &req, REQ_SIZE);
- return 0;
- }
- int get_sessions(void) {
- struct vcc_request req;
- clear_req(&req);
- req.magic = htonl(VCC_MAGIC);
- req.reqtype = htonl(REQ_CTL_SESS);
- req.uid = 0;
- write(fd, &req, REQ_SIZE);
- return 0;
- }
- int do_bh(struct vcc_request *req) {
- switch (ntohl(req->reqtype)) {
- case REQ_CTL_LOGIN:
- vcc_login_bh(req);
- break;
- #if 0
- /* obsolete, see do_relay_bh() */
- case REQ_CTL_USRS:
- do_cmd_ls_bh(req);
- break;
- #endif
- case REQ_CTL_SESS:
- do_cmd_lsse_bh(req);
- break;
- case REQ_CTL_UINFO:
- do_cmd_uinfo_bh(req);
- break;
- case REQ_SYS_SCRINC:
- do_cmd_incr_bh(req);
- break;
- default:
- fprintf(stderr, "*** unexpected packet type %d. \n", ntohl(req->reqtype));
- break;
- }
- return 0;
- }
- int do_relay_bh(struct vcc_relay_header *req) {
- switch (ntohl(req->reqtype)) {
- case REQ_CTL_USRS:
- do_cmd_ls_bh(req);
- break;
- default:
- fprintf(stderr, "*** unexpected packet type %d. \n", ntohl(req->reqtype));
- break;
- }
- return 0;
- }
- int __login(char *usrname, char *passwd, int type) {
- struct vcc_request req;
- clear_req(&req);
- req.magic = htonl(VCC_MAGIC);
- /* REQ_CTL_LOGIN or REQ_CTL_IALOG */
- req.reqtype = htonl(type);
- strncpy(req.usrname, usrname, USRNAME_SIZE);
- strncpy(req.msg, passwd, MSG_SIZE);
- write(fd, &req, REQ_SIZE);
- return 0;
- }
- int vcc_login(char *usrname, char *passwd) {
- return __login(usrname, passwd, REQ_CTL_LOGIN);
- }
- int ia_login(char *session) {
- return __login(usrname, session, REQ_CTL_IALOG);
- }
- int vcc_login_bh(struct vcc_request *req) {
- if (ntohl(req->reqtype) != REQ_CTL_LOGIN) {
- fprintf(stderr, "*** bad package. \n");
- return 1;
- }
- if (ntohl(req->uid))
- printf(_("ready. \n"));
- else {
- fprintf(stderr, _("login failed: wrong password or user doesn't exists. \n"));
- fprintf(stderr, _("if you haven't got an account on this server, ask the administrator "
- "and you can get one. \n"));
- exit(1);
- }
- return 0;
- }
- int vcc_create_session(char *name) {
- struct vcc_request req;
- clear_req(&req);
- req.magic = htonl(VCC_MAGIC);
- req.reqtype = htonl(REQ_CTL_NEWSE);
- strncpy(req.usrname, name, USRNAME_SIZE);
- write(fd, &req, REQ_SIZE);
- return 0;
- }
- int join_session(int sid) {
- struct vcc_request req;
- clear_req(&req);
- req.magic = htonl(VCC_MAGIC);
- req.reqtype = htonl(REQ_CTL_JOINS);
- req.session = htonl(sid);
- write(fd, &req, REQ_SIZE);
- return 0;
- }
- int session_name(int sid, char *buf) {
- struct vcc_request req;
- clear_req(&req);
- req.magic = htonl(VCC_MAGIC);
- req.reqtype = htonl(REQ_CTL_SENAME);
- req.session = htonl(sid);
- write(fd, &req, REQ_SIZE);
- read(fd, &req, REQ_SIZE);
- if (ntohl(req.session) == -1u)
- strcpy(buf, "*** no such session");
- else
- strcpy(buf, req.msg);
- return 0;
- }
- int incr_score(char *name, int incr) {
- struct vcc_request req;
- clear_req(&req);
- req.magic = htonl(VCC_MAGIC);
- req.reqtype = htonl(REQ_SYS_SCRINC);
- req.session = htonl(incr);
- strcpy(req.usrname, name);
- write(fd, &req, REQ_SIZE);
- return 0;
- }
- int is_user_banned(char *usrname) {
- struct klist_node *item;
- struct banlist *ban;
- for (item = banned_people.next; item != &banned_people; item = item->next) {
- ban = banlist_of(item);
- if (ban->usrname && !strcmp(usrname, ban->usrname)) {
- return 1;
- }
- }
- return 0;
- }
- int get_sys_info(void) {
- struct vcc_request req;
- clear_req(&req);
- req.magic = htonl(VCC_MAGIC);
- req.reqtype = htonl(REQ_SYS_INFO);
- strcpy(req.usrname, usrname);
- write(fd, &req, REQ_SIZE);
- return 0;
- }
|