main.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 do_cmd(char *s);
  41. int do_bh(struct vcc_request *);
  42. int fd;
  43. int rate;
  44. char usrname[USRNAME_SIZE];
  45. static int usage(void) {
  46. fprintf(stderr, "Usage: vcc [server]\n");
  47. fprintf(stderr, "As default, server is 124.223.105.230. \n");
  48. return 1;
  49. }
  50. static int init_socket(const char *server_addr) {
  51. struct sockaddr_in addr, server;
  52. if (unlikely((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)) {
  53. perror("socket()");
  54. exit(1);
  55. }
  56. memset(&addr, 0, sizeof(struct sockaddr_in));
  57. addr.sin_family = AF_INET;
  58. addr.sin_addr.s_addr = htonl(INADDR_ANY);
  59. addr.sin_port = htons(0);
  60. if (unlikely(bind(fd, (struct sockaddr *) &addr, sizeof(struct sockaddr_in)) < 0)) {
  61. perror("bind()");
  62. exit(1);
  63. }
  64. memset(&server, 0, sizeof(struct sockaddr_in));
  65. server.sin_family = AF_INET;
  66. server.sin_port = htons(VCC_PORT);
  67. if (unlikely(!inet_aton(server_addr, &server.sin_addr))) {
  68. fprintf(stderr, "invalid ip address. "
  69. "leave blank if you want to connect the default server");
  70. exit(1);
  71. }
  72. if (unlikely(connect(fd, (struct sockaddr *) &server, sizeof(struct sockaddr_in)) < 0)) {
  73. perror("connect()");
  74. exit(1);
  75. }
  76. return fd;
  77. }
  78. static int login(void) {
  79. char *passwd;
  80. fprintf(stderr, "login as: ");
  81. scanf("%s", usrname);
  82. passwd = getpass("password: ");
  83. vcc_login(usrname, passwd);
  84. return 0;
  85. }
  86. int main(int argc, const char **argv) {
  87. struct pollfd fds[2];
  88. struct vcc_request *req;
  89. char *buf, *msg;
  90. int n, size;
  91. #ifdef _WIN32
  92. setlocale(LC_ALL, ".UTF-8");
  93. #else
  94. setlocale(LC_ALL, "C.UTF-8");
  95. #endif
  96. if (unlikely(argc != 1 && argc != 2))
  97. return usage();
  98. printf(VCCD_VERSION "\n");
  99. init_socket(argv[1] ? argv[1] : "124.223.105.230");
  100. printf("Connected to server. \n");
  101. login();
  102. fds[0].fd = 0;
  103. fds[0].events = POLLIN;
  104. fds[0].revents = 0;
  105. fds[1].fd = fd;
  106. fds[1].events = POLLIN;
  107. fds[1].revents = 0;
  108. rate = DEFAULT_RATE;
  109. if (unlikely(!(buf = malloc(MSG_SIZE)))) {
  110. fprintf(stderr, "*** malloc() failed\n");
  111. return 1;
  112. }
  113. if (unlikely(!(req = malloc(REQ_SIZE)))) {
  114. fprintf(stderr, "*** malloc() failed\n");
  115. return 1;
  116. }
  117. for (;;) {
  118. n = poll(fds, 2, rate);
  119. if (n == -1) {
  120. if (likely(errno == EINTR))
  121. continue;
  122. perror("poll()");
  123. break;
  124. }
  125. if (!n) {
  126. /* 1 sec timed out */
  127. get_msgs();
  128. continue;
  129. }
  130. if (fds[0].revents & POLLIN) {
  131. /* read keyboard */
  132. memset(buf, 0, MSG_SIZE);
  133. if (read(0, buf, MSG_SIZE) < 0) {
  134. perror("read()");
  135. continue;
  136. }
  137. if (*buf == '-')
  138. do_cmd(buf);
  139. else if (*buf != '\n') {
  140. send_msg(buf, usrname);
  141. pretty_new_msg(usrname, buf);
  142. }
  143. pretty_prompt(usrname);
  144. }
  145. if (fds[1].revents & POLLIN) {
  146. size = read(fd, req, REQ_SIZE);
  147. if (unlikely(!size)) {
  148. fprintf(stderr, "Server closed. \n");
  149. break;
  150. }
  151. if (ntohl(req->reqtype) != REQ_MSG_NEW) {
  152. do_bh(req);
  153. continue;
  154. }
  155. msg = req->msg;
  156. if (!*msg || *(unsigned char *) msg == 0xffu)
  157. continue;
  158. pretty_new_msg(req->usrname, msg);
  159. pretty_prompt(usrname);
  160. }
  161. }
  162. printf("bye. \n");
  163. return 0;
  164. }