main.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 <klist.h>
  33. #include <vcc/vcc.h>
  34. #include <vcc/version.h>
  35. #include <vcc/pretty.h>
  36. /* we tried to use libreadline, but after using that, we feel strange
  37. * because... because of what? i don't know. */
  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. if (*buf == '-')
  126. do_cmd(buf);
  127. else if (*buf != '\n') {
  128. send_msg(buf, usrname);
  129. pretty_new_msg(usrname, buf, current_sid);
  130. }
  131. pretty_prompt(usrname);
  132. return 0;
  133. }
  134. int main(int argc, char **argv) {
  135. struct pollfd fds[2];
  136. struct vcc_request *req;
  137. char *buf, *msg, *ip = "124.223.105.230";
  138. char key[64], iv[64];
  139. int n, size, port = VCC_PORT, got_usrname;
  140. (void) argc;
  141. #ifdef _WIN32
  142. setlocale(LC_ALL, ".UTF-8");
  143. #else
  144. setlocale(LC_ALL, "C.UTF-8");
  145. #endif
  146. printf(VCC_VERSION "\n");
  147. got_usrname = parse_args(argv, &port, &ip);
  148. init_socket(ip, port);
  149. printf("Connected to server. \n");
  150. login(got_usrname);
  151. show_algo_info();
  152. memset(key, 0, 64);
  153. memset(iv, 0, 64);
  154. fprintf(stderr, "Key: ");
  155. read(0, key, 64);
  156. fprintf(stderr, "IV: ");
  157. read(0, iv, 64);
  158. crypt_init(key, iv);
  159. fds[0].fd = 0;
  160. fds[0].events = POLLIN;
  161. fds[0].revents = 0;
  162. fds[1].fd = fd;
  163. fds[1].events = POLLIN;
  164. fds[1].revents = 0;
  165. rate = DEFAULT_RATE;
  166. if (unlikely(!(buf = malloc(MSG_SIZE)))) {
  167. fprintf(stderr, "*** malloc() failed\n");
  168. return 1;
  169. }
  170. if (unlikely(!(req = malloc(REQ_SIZE)))) {
  171. fprintf(stderr, "*** malloc() failed\n");
  172. return 1;
  173. }
  174. for (;;) {
  175. n = poll(fds, 2, rate);
  176. if (n == -1) {
  177. if (likely(errno == EINTR))
  178. continue;
  179. perror("poll()");
  180. break;
  181. }
  182. if (!n) {
  183. /* 1 sec timed out */
  184. get_msgs();
  185. continue;
  186. }
  187. if (fds[0].revents & POLLIN) {
  188. read(0, buf, MSG_SIZE);
  189. do_handle_command(buf);
  190. }
  191. if (fds[1].revents & POLLIN) {
  192. size = read(fd, req, REQ_SIZE);
  193. if (unlikely(!size)) {
  194. fprintf(stderr, "Server closed. \n");
  195. break;
  196. }
  197. if (ntohl(req->reqtype) != REQ_MSG_NEW) {
  198. do_bh(req);
  199. continue;
  200. }
  201. msg = req->msg;
  202. if (!strcmp(msg, "CQD")) {
  203. pretty_cqd(req->usrname);
  204. pretty_prompt(usrname);
  205. continue;
  206. }
  207. if (unlikely(is_user_banned(req->usrname)))
  208. continue;
  209. if (ntohl(req->flags) & FLAG_ENCRYPTED)
  210. decrypt(msg);
  211. pretty_new_msg(req->usrname, msg, ntohl(req->session));
  212. pretty_prompt(usrname);
  213. }
  214. }
  215. printf("bye. \n");
  216. return 0;
  217. }