vcc.c 5.5 KB

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