main.c 4.2 KB

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