main.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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);
  42. char *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";
  121. char key[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. fprintf(stderr, "Key: ");
  137. read(0, key, 64);
  138. crypt_init(key);
  139. fds[0].fd = 0;
  140. fds[0].events = POLLIN;
  141. fds[0].revents = 0;
  142. fds[1].fd = fd;
  143. fds[1].events = POLLIN;
  144. fds[1].revents = 0;
  145. rate = DEFAULT_RATE;
  146. if (unlikely(!(buf = malloc(MSG_SIZE)))) {
  147. fprintf(stderr, "*** malloc() failed\n");
  148. return 1;
  149. }
  150. if (unlikely(!(req = malloc(REQ_SIZE)))) {
  151. fprintf(stderr, "*** malloc() failed\n");
  152. return 1;
  153. }
  154. for (;;) {
  155. n = poll(fds, 2, rate);
  156. if (n == -1) {
  157. if (likely(errno == EINTR))
  158. continue;
  159. perror("poll()");
  160. break;
  161. }
  162. if (!n) {
  163. /* 1 sec timed out */
  164. get_msgs();
  165. continue;
  166. }
  167. if (fds[0].revents & POLLIN) {
  168. /* read keyboard */
  169. memset(buf, 0, MSG_SIZE);
  170. if (read(0, buf, MSG_SIZE) < 0) {
  171. perror("read()");
  172. continue;
  173. }
  174. if (*buf == '-')
  175. do_cmd(buf);
  176. else if (*buf != '\n') {
  177. if (unlikely(!encrypt(buf)))
  178. continue;
  179. send_msg(buf, usrname);
  180. decrypt(buf);
  181. pretty_new_msg(usrname, buf, current_sid);
  182. }
  183. pretty_prompt(usrname);
  184. }
  185. if (fds[1].revents & POLLIN) {
  186. size = read(fd, req, REQ_SIZE);
  187. if (unlikely(!size)) {
  188. fprintf(stderr, "Server closed. \n");
  189. break;
  190. }
  191. if (ntohl(req->reqtype) != REQ_MSG_NEW) {
  192. do_bh(req);
  193. continue;
  194. }
  195. msg = req->msg;
  196. if (strcmp(req->usrname, "system"))
  197. decrypt(msg);
  198. pretty_new_msg(req->usrname, msg, ntohl(req->session));
  199. pretty_prompt(usrname);
  200. }
  201. }
  202. printf("bye. \n");
  203. return 0;
  204. }