| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- /* 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 <vcc/vcc.h>
- #include <vcc/version.h>
- #define clear_req(req) memset(req, 0, REQ_SIZE)
- extern int fd;
- int vcc_login_bh(struct vcc_request *);
- int do_cmd_ls_bh(struct vcc_request *);
- int send_msg(const char *msg, const char *usrname) {
- struct vcc_request req;
- clear_req(&req);
- req.magic = htonl(VCC_MAGIC);
- req.reqtype = htonl(REQ_MSG_SEND);
- req.uid = 0;
-
- strcpy(req.usrname, usrname);
- strcpy(req.msg, msg);
- write(fd, &req, REQ_SIZE);
- return 0;
- }
- int get_msgs(void) {
- struct vcc_request req;
- clear_req(&req);
- req.magic = htonl(VCC_MAGIC);
- req.reqtype = htonl(REQ_MSG_NEW);
- req.uid = 0;
- write(fd, &req, REQ_SIZE);
- return 0;
- }
- int read_msg(struct vcc_request *buf) {
- return read(fd, buf, REQ_SIZE);
- }
- 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 do_bh(struct vcc_request *req) {
- switch (ntohl(req->reqtype)) {
- case REQ_CTL_LOGIN:
- vcc_login_bh(req);
- break;
- 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 vcc_login(char *usrname, char *passwd) {
- struct vcc_request req;
- clear_req(&req);
- req.magic = htonl(VCC_MAGIC);
- req.reqtype = htonl(REQ_CTL_LOGIN);
- strcpy(req.usrname, usrname);
- strcpy(req.msg, passwd);
- write(fd, &req, REQ_SIZE);
- return 0;
- }
- 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;
- }
|