main.c 4.7 KB

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