main.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 <vcc/vcc.h>
  30. #include <vcc/version.h>
  31. int fd;
  32. static int usage(void) {
  33. fprintf(stderr, "Usage: vcc [server]\n");
  34. fprintf(stderr, "As default, server is 124.223.105.230. \n");
  35. return 1;
  36. }
  37. static int init_socket(const char *server_addr) {
  38. struct sockaddr_in addr, server;
  39. if (unlikely((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)) {
  40. perror("socket()");
  41. exit(1);
  42. }
  43. memset(&addr, 0, sizeof(struct sockaddr_in));
  44. addr.sin_family = AF_INET;
  45. addr.sin_addr.s_addr = htonl(INADDR_ANY);
  46. addr.sin_port = htons(0);
  47. if (unlikely(bind(fd, (struct sockaddr *) &addr, sizeof(struct sockaddr_in)) < 0)) {
  48. perror("bind()");
  49. exit(1);
  50. }
  51. memset(&server, 0, sizeof(struct sockaddr_in));
  52. server.sin_family = AF_INET;
  53. server.sin_port = htons(VCC_PORT);
  54. if (unlikely(!inet_aton(server_addr, &server.sin_addr))) {
  55. fprintf(stderr, "invalid ip address. "
  56. "leave blank if you want to connect the default server");
  57. exit(1);
  58. }
  59. if (unlikely(connect(fd, (struct sockaddr *) &server, sizeof(struct sockaddr_in)) < 0)) {
  60. perror("connect()");
  61. exit(1);
  62. }
  63. return fd;
  64. }
  65. static int send_msg(const char *msg) {
  66. char *buf;
  67. if (unlikely(!(buf = malloc(REQ_SIZE)))) {
  68. fprintf(stderr, "*** malloc() failed\n");
  69. exit(1);
  70. }
  71. strcpy(buf, msg);
  72. write(fd, buf, REQ_SIZE);
  73. free(buf);
  74. return 0;
  75. }
  76. static int read_msg(char *buf) {
  77. return read(fd, buf, REQ_SIZE);
  78. }
  79. int main(int argc, const char **argv) {
  80. char *buf;
  81. if (unlikely(argc != 1 && argc != 2))
  82. return usage();
  83. printf(VCCD_VERSION "\n");
  84. if (unlikely(!(buf = malloc(REQ_SIZE)))) {
  85. fprintf(stderr, "*** malloc() failed\n");
  86. return 1;
  87. }
  88. init_socket(argv[1] ? argv[1] : "124.223.105.230");
  89. printf("Connected to server. \n");
  90. send_msg("Hello, server! \n");
  91. read_msg(buf);
  92. printf(buf);
  93. return 0;
  94. }