main.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. int fd;
  34. static unsigned char empty_package[REQ_SIZE];
  35. static int usage(void) {
  36. fprintf(stderr, "Usage: vcc [server]\n");
  37. fprintf(stderr, "As default, server is 124.223.105.230. \n");
  38. return 1;
  39. }
  40. static int init_socket(const char *server_addr) {
  41. struct sockaddr_in addr, server;
  42. if (unlikely((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)) {
  43. perror("socket()");
  44. exit(1);
  45. }
  46. memset(&addr, 0, sizeof(struct sockaddr_in));
  47. addr.sin_family = AF_INET;
  48. addr.sin_addr.s_addr = htonl(INADDR_ANY);
  49. addr.sin_port = htons(0);
  50. if (unlikely(bind(fd, (struct sockaddr *) &addr, sizeof(struct sockaddr_in)) < 0)) {
  51. perror("bind()");
  52. exit(1);
  53. }
  54. memset(&server, 0, sizeof(struct sockaddr_in));
  55. server.sin_family = AF_INET;
  56. server.sin_port = htons(VCC_PORT);
  57. if (unlikely(!inet_aton(server_addr, &server.sin_addr))) {
  58. fprintf(stderr, "invalid ip address. "
  59. "leave blank if you want to connect the default server");
  60. exit(1);
  61. }
  62. if (unlikely(connect(fd, (struct sockaddr *) &server, sizeof(struct sockaddr_in)) < 0)) {
  63. perror("connect()");
  64. exit(1);
  65. }
  66. return fd;
  67. }
  68. static int send_msg(const char *msg) {
  69. struct vcc_request req;
  70. strcpy(req.msg, msg);
  71. write(fd, &req, REQ_SIZE);
  72. return 0;
  73. }
  74. static int read_msg(char *buf) {
  75. struct vcc_request req;
  76. int n;
  77. n = read(fd, &req, REQ_SIZE);
  78. if (n < 0)
  79. return n;
  80. memcpy(buf, req.msg, MSG_SIZE);
  81. return 0;
  82. }
  83. void alarm_handler(int nr) {
  84. alarm(1);
  85. memset(empty_package, 0xff, REQ_SIZE);
  86. write(fd, empty_package, REQ_SIZE);
  87. return;
  88. }
  89. static int setup_alarm(void) {
  90. struct sigaction act;
  91. act.sa_handler = alarm_handler;
  92. act.sa_flags = SA_RESTART;
  93. sigaction(SIGALRM, &act, NULL);
  94. alarm(1);
  95. return 0;
  96. }
  97. int main(int argc, const char **argv) {
  98. char buf[REQ_SIZE];
  99. int pid;
  100. if (unlikely(argc != 1 && argc != 2))
  101. return usage();
  102. printf(VCCD_VERSION "\n");
  103. init_socket(argv[1] ? argv[1] : "124.223.105.230");
  104. printf("Connected to server. \n");
  105. if ((pid = fork())) {
  106. /* receive messages */
  107. setup_alarm();
  108. for (;;) {
  109. memset(buf, 0, REQ_SIZE);
  110. if (read_msg(buf) == -1) {
  111. printf("Server closed\n");
  112. break;
  113. }
  114. if (!*buf || *(unsigned char *) buf == 0xffu)
  115. continue;
  116. printf("New message: %s\n", buf);
  117. }
  118. }
  119. else
  120. for (;;) {
  121. memset(buf, 0, REQ_SIZE);
  122. if (read(0, buf, REQ_SIZE) <= 0) {
  123. fprintf(stderr, "--- read() failed. \n");
  124. continue;
  125. }
  126. if (!strcmp(buf, "quit"))
  127. break;
  128. send_msg(buf);
  129. }
  130. close(fd);
  131. kill(pid, 9);
  132. return 0;
  133. }