main.c 4.0 KB

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