vcc.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 current_sid = 0;
  36. int vcc_login_bh(struct vcc_request *);
  37. int do_cmd_ls_bh(struct vcc_request *);
  38. int do_cmd_lsse_bh(struct vcc_request *);
  39. int init_socket(const char *server_addr, int server_port) {
  40. struct sockaddr_in addr, server;
  41. if (unlikely((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)) {
  42. perror("socket()");
  43. exit(1);
  44. }
  45. memset(&addr, 0, sizeof(struct sockaddr_in));
  46. addr.sin_family = AF_INET;
  47. addr.sin_addr.s_addr = htonl(INADDR_ANY);
  48. addr.sin_port = htons(0);
  49. if (unlikely(bind(fd, (struct sockaddr *) &addr, sizeof(struct sockaddr_in)) < 0)) {
  50. perror("bind()");
  51. exit(1);
  52. }
  53. memset(&server, 0, sizeof(struct sockaddr_in));
  54. server.sin_family = AF_INET;
  55. server.sin_port = htons(server_port);
  56. if (unlikely(!inet_aton(server_addr, &server.sin_addr))) {
  57. fprintf(stderr, "invalid ip address. "
  58. "leave blank if you want to connect the default server. \n");
  59. exit(1);
  60. }
  61. if (unlikely(connect(fd, (struct sockaddr *) &server, sizeof(struct sockaddr_in)) < 0)) {
  62. perror("connect()");
  63. exit(1);
  64. }
  65. return fd;
  66. }
  67. int send_msg(const char *msg, const char *usrname) {
  68. struct vcc_request req;
  69. clear_req(&req);
  70. req.magic = htonl(VCC_MAGIC_CR);
  71. req.reqtype = htonl(REQ_MSG_SEND);
  72. req.uid = 0;
  73. req.session = htonl(current_sid);
  74. strncpy(req.usrname, usrname, USRNAME_SIZE);
  75. memcpy(req.msg, msg, MSG_SIZE);
  76. write(fd, &req, REQ_SIZE);
  77. return 0;
  78. }
  79. int get_msgs(void) {
  80. struct vcc_request req;
  81. clear_req(&req);
  82. req.magic = htonl(VCC_MAGIC);
  83. req.reqtype = htonl(REQ_MSG_NEW);
  84. req.uid = 0;
  85. write(fd, &req, REQ_SIZE);
  86. return 0;
  87. }
  88. int read_msg(struct vcc_request *buf) {
  89. return read(fd, buf, REQ_SIZE);
  90. }
  91. int get_users(void) {
  92. struct vcc_request req;
  93. clear_req(&req);
  94. req.magic = htonl(VCC_MAGIC);
  95. req.reqtype = htonl(REQ_CTL_USRS);
  96. req.uid = 0;
  97. write(fd, &req, REQ_SIZE);
  98. return 0;
  99. }
  100. int get_sessions(void) {
  101. struct vcc_request req;
  102. clear_req(&req);
  103. req.magic = htonl(VCC_MAGIC);
  104. req.reqtype = htonl(REQ_CTL_SESS);
  105. req.uid = 0;
  106. write(fd, &req, REQ_SIZE);
  107. return 0;
  108. }
  109. int do_bh(struct vcc_request *req) {
  110. switch (ntohl(req->reqtype)) {
  111. case REQ_CTL_LOGIN:
  112. vcc_login_bh(req);
  113. break;
  114. case REQ_CTL_USRS:
  115. do_cmd_ls_bh(req);
  116. break;
  117. case REQ_CTL_SESS:
  118. do_cmd_lsse_bh(req);
  119. break;
  120. default:
  121. fprintf(stderr, "*** unexpected packet type %d. \n", ntohl(req->reqtype));
  122. break;
  123. }
  124. return 0;
  125. }
  126. int vcc_login(char *usrname, char *passwd) {
  127. struct vcc_request req;
  128. clear_req(&req);
  129. req.magic = htonl(VCC_MAGIC);
  130. req.reqtype = htonl(REQ_CTL_LOGIN);
  131. strncpy(req.usrname, usrname, USRNAME_SIZE);
  132. strncpy(req.msg, passwd, MSG_SIZE);
  133. write(fd, &req, REQ_SIZE);
  134. return 0;
  135. }
  136. int vcc_login_bh(struct vcc_request *req) {
  137. if (ntohl(req->reqtype) != REQ_CTL_LOGIN) {
  138. fprintf(stderr, "*** bad package. \n");
  139. return 1;
  140. }
  141. if (ntohl(req->uid))
  142. printf("ready. \n");
  143. else {
  144. fprintf(stderr, "login failed: wrong password or user doesn't exists. \n");
  145. fprintf(stderr, "if you haven't got an account on this server, ask the administrator "
  146. "and you can get one. \n");
  147. exit(1);
  148. }
  149. return 0;
  150. }
  151. int vcc_create_session(char *name) {
  152. struct vcc_request req;
  153. clear_req(&req);
  154. req.magic = htonl(VCC_MAGIC);
  155. req.reqtype = htonl(REQ_CTL_NEWSE);
  156. strncpy(req.usrname, name, USRNAME_SIZE);
  157. write(fd, &req, REQ_SIZE);
  158. return 0;
  159. }
  160. int join_session(int sid) {
  161. struct vcc_request req;
  162. clear_req(&req);
  163. req.magic = htonl(VCC_MAGIC);
  164. req.reqtype = htonl(REQ_CTL_JOINS);
  165. req.session = htonl(sid);
  166. write(fd, &req, REQ_SIZE);
  167. return 0;
  168. }