main.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /* vcc/main.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 <locale.h>
  25. #include <sys/stat.h>
  26. #include <sys/types.h>
  27. #include <sys/socket.h>
  28. #include <netinet/in.h>
  29. #include <arpa/inet.h>
  30. #include <poll.h>
  31. #include <signal.h>
  32. #include <readline/readline.h>
  33. #include <readline/history.h>
  34. #include <klist.h>
  35. #include <vcc/vcc.h>
  36. #include <vcc/version.h>
  37. #include <vcc/pretty.h>
  38. #define DEFAULT_RATE 1000
  39. int send_msg(const char *msg, const char *usrname);
  40. int read_msg(struct vcc_request *buf);
  41. int get_msgs(void);
  42. int vcc_login(char *, char *);
  43. int is_user_banned(char *);
  44. int show_algo_info(void);
  45. int crypt_init(void *key, void *iv);
  46. int encrypt(char *);
  47. int decrypt(char *);
  48. int do_cmd(char *s);
  49. int do_bh(struct vcc_request *);
  50. int do_cmd_lself(char *);
  51. int init_socket(const char *server_addr, int server_port);
  52. int fd;
  53. int rate;
  54. char usrname[USRNAME_SIZE];
  55. struct klist_node banned_people = KLIST_NODE_INIT(&banned_people);
  56. extern int current_sid;
  57. static char const *help_msg = {
  58. "Usage: vcc [-up] [server]\n"
  59. "Options: \n"
  60. " -u --user Set the username on the server. "
  61. "If unspecified, you must input it interatively. \n"
  62. " -p --port Specified the port on the server. "
  63. "Default port is 46. You must keep this as same "
  64. "as the port on the server. \n"
  65. " [server] Address of the server. "
  66. "As default, server is 124.223.105.230. "
  67. };
  68. static int usage(void) {
  69. fputs(help_msg, stderr);
  70. return 1;
  71. }
  72. static int login(int got_usrname) {
  73. char *passwd;
  74. if (!got_usrname) {
  75. fprintf(stderr, "login as: ");
  76. scanf("%s", usrname);
  77. }
  78. passwd = getpass("password: ");
  79. vcc_login(usrname, passwd);
  80. do_cmd_lself(NULL);
  81. return 0;
  82. }
  83. int parse_args(char **argv, int *port, char **ip) {
  84. int i = 1;
  85. /*
  86. * unknown: 0
  87. * usrname: 1
  88. * port: 2
  89. */
  90. int type = 0;
  91. int got_usrname = 0;
  92. char *arg;
  93. while (argv[i]) {
  94. arg = argv[i];
  95. switch (type) {
  96. case 0:
  97. if (!strcmp(arg, "-u") || !strcmp(arg, "--user")) {
  98. type = 1;
  99. } else if (!strcmp(arg, "-p") || !strcmp(arg, "--port")) {
  100. type = 2;
  101. } else if (arg[0] != '-') {
  102. *ip = arg;
  103. } else {
  104. exit(usage());
  105. }
  106. break;
  107. case 1:
  108. printf("%s %s\n", arg, usrname);
  109. strcpy(usrname, arg);
  110. got_usrname = 1;
  111. type = 0;
  112. break;
  113. case 2:
  114. *port = atoi(arg);
  115. type = 0;
  116. break;
  117. default:
  118. abort();
  119. }
  120. ++i;
  121. }
  122. return got_usrname;
  123. }
  124. int do_handle_command(char *buf) {
  125. add_history(buf);
  126. if (*buf == '-')
  127. do_cmd(buf);
  128. else if (*buf != '\n') {
  129. send_msg(buf, usrname);
  130. pretty_new_msg(usrname, buf, current_sid);
  131. }
  132. pretty_prompt(usrname);
  133. return 0;
  134. }
  135. int main(int argc, char **argv) {
  136. struct pollfd fds[2];
  137. struct vcc_request *req;
  138. char *buf, *msg, *ip = "124.223.105.230";
  139. char key[64], iv[64];
  140. int n, size, port = VCC_PORT, got_usrname;
  141. (void) argc;
  142. #ifdef _WIN32
  143. setlocale(LC_ALL, ".UTF-8");
  144. #else
  145. setlocale(LC_ALL, "C.UTF-8");
  146. #endif
  147. printf(VCC_VERSION "\n");
  148. got_usrname = parse_args(argv, &port, &ip);
  149. init_socket(ip, port);
  150. printf("Connected to server. \n");
  151. login(got_usrname);
  152. show_algo_info();
  153. memset(key, 0, 64);
  154. memset(iv, 0, 64);
  155. fprintf(stderr, "Key: ");
  156. read(0, key, 64);
  157. fprintf(stderr, "IV: ");
  158. read(0, iv, 64);
  159. crypt_init(key, iv);
  160. fds[0].fd = 0;
  161. fds[0].events = POLLIN;
  162. fds[0].revents = 0;
  163. fds[1].fd = fd;
  164. fds[1].events = POLLIN;
  165. fds[1].revents = 0;
  166. rate = DEFAULT_RATE;
  167. if (unlikely(!(buf = malloc(MSG_SIZE)))) {
  168. fprintf(stderr, "*** malloc() failed\n");
  169. return 1;
  170. }
  171. if (unlikely(!(req = malloc(REQ_SIZE)))) {
  172. fprintf(stderr, "*** malloc() failed\n");
  173. return 1;
  174. }
  175. for (;;) {
  176. n = poll(fds, 2, rate);
  177. if (n == -1) {
  178. if (likely(errno == EINTR))
  179. continue;
  180. perror("poll()");
  181. break;
  182. }
  183. if (!n) {
  184. /* 1 sec timed out */
  185. get_msgs();
  186. continue;
  187. }
  188. if (fds[0].revents & POLLIN)
  189. rl_callback_read_char();
  190. if (fds[1].revents & POLLIN) {
  191. size = read(fd, req, REQ_SIZE);
  192. if (unlikely(!size)) {
  193. fprintf(stderr, "Server closed. \n");
  194. break;
  195. }
  196. if (ntohl(req->reqtype) != REQ_MSG_NEW) {
  197. do_bh(req);
  198. continue;
  199. }
  200. msg = req->msg;
  201. if (!strcmp(msg, "CQD")) {
  202. pretty_cqd(req->usrname);
  203. pretty_prompt(usrname);
  204. continue;
  205. }
  206. if (unlikely(is_user_banned(req->usrname)))
  207. continue;
  208. if (ntohl(req->flags) & FLAG_ENCRYPTED)
  209. decrypt(msg);
  210. pretty_new_msg(req->usrname, msg, ntohl(req->session));
  211. pretty_prompt(usrname);
  212. }
  213. }
  214. printf("bye. \n");
  215. return 0;
  216. }