vcc.c 2.6 KB

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