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