main.c 4.1 KB

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