vcc.c 5.6 KB

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