vcc.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 do_cmd_ls_bh(struct vcc_request *);
  36. int send_msg(const char *msg, const char *usrname) {
  37. struct vcc_request req;
  38. req.magic = htonl(VCC_MAGIC);
  39. req.reqtype = htonl(REQ_MSG_SEND);
  40. req.uid = 0;
  41. strcpy(req.usrname, usrname);
  42. strcpy(req.msg, msg);
  43. write(fd, &req, REQ_SIZE);
  44. return 0;
  45. }
  46. int get_msgs(void) {
  47. struct vcc_request req;
  48. memset(&req, 0, REQ_SIZE);
  49. req.magic = htonl(VCC_MAGIC);
  50. req.reqtype = htonl(REQ_MSG_NEW);
  51. req.uid = 0;
  52. write(fd, &req, REQ_SIZE);
  53. return 0;
  54. }
  55. int read_msg(struct vcc_request *buf) {
  56. return read(fd, buf, REQ_SIZE);
  57. }
  58. int get_users(void) {
  59. struct vcc_request req;
  60. req.magic = htonl(VCC_MAGIC);
  61. req.reqtype = htonl(REQ_CTL_USRS);
  62. req.uid = 0;
  63. write(fd, &req, REQ_SIZE);
  64. return 0;
  65. }
  66. int do_bh(struct vcc_request *req) {
  67. switch (ntohl(req->reqtype)) {
  68. case REQ_CTL_LOGIN:
  69. vcc_login_bh(req);
  70. break;
  71. case REQ_CTL_USRS:
  72. do_cmd_ls_bh(req);
  73. break;
  74. default:
  75. fprintf(stderr, "*** unexpected packet type %d. \n", ntohl(req->reqtype));
  76. break;
  77. }
  78. return 0;
  79. }
  80. int vcc_login(char *usrname, char *passwd) {
  81. struct vcc_request req;
  82. req.magic = htonl(VCC_MAGIC);
  83. req.reqtype = htonl(REQ_CTL_LOGIN);
  84. strcpy(req.usrname, usrname);
  85. strcpy(req.msg, passwd);
  86. write(fd, &req, REQ_SIZE);
  87. return 0;
  88. }
  89. int vcc_login_bh(struct vcc_request *req) {
  90. if (ntohl(req->reqtype) != REQ_CTL_LOGIN) {
  91. fprintf(stderr, "*** bad package. \n");
  92. return 1;
  93. }
  94. if (ntohl(req->uid))
  95. printf("ready. \n");
  96. else {
  97. fprintf(stderr, "login failed: wrong password or user doesn't exists. \n");
  98. fprintf(stderr, "if you haven't got an account on this server, ask the administrator "
  99. "and you can get one. \n");
  100. exit(1);
  101. }
  102. return 0;
  103. }