main.c 5.3 KB

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