vcc.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* vcc/vcc.c
  2. *
  3. * This file is part of vcc.
  4. *
  5. * vcc is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * vcc is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with vcc. If not, see <https://www.gnu.org/licenses/>
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <fcntl.h>
  22. #include <errno.h>
  23. #include <string.h>
  24. #include <sys/stat.h>
  25. #include <sys/types.h>
  26. #include <sys/socket.h>
  27. #include <netinet/in.h>
  28. #include <arpa/inet.h>
  29. #include <poll.h>
  30. #include <signal.h>
  31. #include <vcc/vcc.h>
  32. #include <vcc/version.h>
  33. extern int fd;
  34. int vcc_login_bh(struct vcc_request *);
  35. int send_msg(const char *msg, const char *usrname) {
  36. struct vcc_request req;
  37. req.magic = htonl(VCC_MAGIC);
  38. req.reqtype = htonl(REQ_MSG_SEND);
  39. req.uid = 0;
  40. strcpy(req.usrname, usrname);
  41. strcpy(req.msg, msg);
  42. write(fd, &req, REQ_SIZE);
  43. return 0;
  44. }
  45. int get_msgs(void) {
  46. struct vcc_request req;
  47. req.magic = htonl(VCC_MAGIC);
  48. req.reqtype = htonl(REQ_MSG_NEW);
  49. req.uid = 0;
  50. write(fd, &req, REQ_SIZE);
  51. return 0;
  52. }
  53. int read_msg(struct vcc_request *buf) {
  54. return read(fd, buf, REQ_SIZE);
  55. }
  56. int get_users(void) {
  57. struct vcc_request req;
  58. req.magic = htonl(VCC_MAGIC);
  59. req.reqtype = htonl(REQ_CTL_USRS);
  60. req.uid = 0;
  61. write(fd, &req, REQ_SIZE);
  62. return 0;
  63. }
  64. int do_bh(struct vcc_request *req) {
  65. switch (ntohl(req->reqtype)) {
  66. case REQ_CTL_LOGIN:
  67. vcc_login_bh(req);
  68. break;
  69. default:
  70. fprintf(stderr, "*** unexpected packet. \n");
  71. break;
  72. }
  73. return 0;
  74. }
  75. int vcc_login(char *usrname, char *passwd) {
  76. struct vcc_request req;
  77. req.magic = htonl(VCC_MAGIC);
  78. req.reqtype = htonl(REQ_CTL_LOGIN);
  79. strcpy(req.usrname, usrname);
  80. strcpy(req.msg, passwd);
  81. write(fd, &req, REQ_SIZE);
  82. return 0;
  83. }
  84. int vcc_login_bh(struct vcc_request *req) {
  85. if (ntohl(req->reqtype) != REQ_CTL_LOGIN) {
  86. fprintf(stderr, "*** bad package. \n");
  87. return 1;
  88. }
  89. if (ntohl(req->uid))
  90. printf("login success. \n");
  91. else {
  92. printf("login failed: wrong password or user doesn't exists. \n");
  93. exit(1);
  94. }
  95. return 0;
  96. }