vcc.c 2.9 KB

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