vcc.c 4.9 KB

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