test_router.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #include <arpa/inet.h>
  2. #include <assert.h>
  3. #include <netinet/in.h>
  4. #include <pthread.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <sys/socket.h>
  9. #include <unistd.h>
  10. #define SERVER_IP "192.168.2.3"
  11. #define FAKE_SERVER_IP "192.168.2.1"
  12. #define CLIENT_IP "192.168.1.1"
  13. #define PORT 34254
  14. #define BUFFER_SIZE 1024
  15. // 错误处理函数
  16. void handle_error_message(const char *message) {
  17. perror(message);
  18. exit(EXIT_FAILURE);
  19. }
  20. // 服务器线程函数
  21. void *server_func(void *arg) {
  22. int sockfd;
  23. struct sockaddr_in server_addr, client_addr;
  24. char buffer[BUFFER_SIZE];
  25. socklen_t client_len = sizeof(client_addr);
  26. if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
  27. handle_error_message("[server] Failed to create socket");
  28. }
  29. memset(&server_addr, 0, sizeof(server_addr));
  30. server_addr.sin_family = AF_INET;
  31. server_addr.sin_port = htons(PORT);
  32. if (inet_pton(AF_INET, SERVER_IP, &server_addr.sin_addr) <= 0) {
  33. handle_error_message("[server] Invalid server IP address");
  34. }
  35. if (bind(sockfd,
  36. (const struct sockaddr *)&server_addr,
  37. sizeof(server_addr)) < 0) {
  38. handle_error_message("[server] Failed to bind to " SERVER_IP);
  39. }
  40. printf("[server] Listening on %s:%d\n", SERVER_IP, PORT);
  41. ssize_t n = recvfrom(sockfd,
  42. buffer,
  43. BUFFER_SIZE,
  44. 0,
  45. (struct sockaddr *)&client_addr,
  46. &client_len);
  47. if (n < 0) {
  48. handle_error_message("[server] Failed to receive");
  49. }
  50. buffer[n] = '\0'; // 确保字符串正确终止
  51. // //debug
  52. // unsigned char *ip_bytes = (unsigned char *)&client_addr.sin_addr.s_addr;
  53. // printf("[DEBUG] Raw IP bytes received: %d.%d.%d.%d\n",
  54. // ip_bytes[0],
  55. // ip_bytes[1],
  56. // ip_bytes[2],
  57. // ip_bytes[3]);
  58. char client_ip_str[INET_ADDRSTRLEN];
  59. inet_ntop(AF_INET, &client_addr.sin_addr, client_ip_str, INET_ADDRSTRLEN);
  60. printf("[server] Received from %s:%d: %s\n",
  61. client_ip_str,
  62. ntohs(client_addr.sin_port),
  63. buffer);
  64. if (sendto(sockfd,
  65. buffer,
  66. n,
  67. 0,
  68. (const struct sockaddr *)&client_addr,
  69. client_len) < 0) {
  70. handle_error_message("[server] Failed to send back");
  71. }
  72. sleep(1);
  73. printf("[server] Echoed back the message\n");
  74. close(sockfd);
  75. printf("server going to exit\n");
  76. return NULL;
  77. }
  78. // 客户端线程函数
  79. void *client_func(void *arg) {
  80. int sockfd;
  81. struct sockaddr_in client_addr, server_addr;
  82. char buffer[BUFFER_SIZE];
  83. const char *msg = "Hello from veth1!";
  84. if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
  85. handle_error_message("[client] Failed to create socket");
  86. }
  87. memset(&client_addr, 0, sizeof(client_addr));
  88. client_addr.sin_family = AF_INET;
  89. client_addr.sin_port = htons(0); // 端口为0,由操作系统自动选择
  90. if (inet_pton(AF_INET, CLIENT_IP, &client_addr.sin_addr) <= 0) {
  91. handle_error_message("[client] Invalid client IP address");
  92. }
  93. if (bind(sockfd,
  94. (const struct sockaddr *)&client_addr,
  95. sizeof(client_addr)) < 0) {
  96. handle_error_message("[client] Failed to bind to " CLIENT_IP);
  97. }
  98. memset(&server_addr, 0, sizeof(server_addr));
  99. server_addr.sin_family = AF_INET;
  100. server_addr.sin_port = htons(PORT);
  101. if (inet_pton(AF_INET, FAKE_SERVER_IP, &server_addr.sin_addr) <= 0) {
  102. handle_error_message("[client] Invalid server IP address for connect");
  103. }
  104. if (connect(sockfd,
  105. (const struct sockaddr *)&server_addr,
  106. sizeof(server_addr)) < 0) {
  107. handle_error_message("[client] Failed to connect");
  108. }
  109. if (send(sockfd, msg, strlen(msg), 0) < 0) {
  110. handle_error_message("[client] Failed to send");
  111. }
  112. printf("[client] Sent: %s\n", msg);
  113. ssize_t n = recv(sockfd, buffer, BUFFER_SIZE, 0);
  114. if (n < 0) {
  115. handle_error_message("[client] Failed to receive");
  116. }
  117. buffer[n] = '\0'; // 确保字符串正确终止
  118. printf("[client] Received echo: %s\n", buffer);
  119. assert(strcmp(msg, buffer) == 0 && "[client] Mismatch in echo!");
  120. close(sockfd);
  121. printf("client goning to exit\n");
  122. return NULL;
  123. }
  124. int main() {
  125. pthread_t server_tid, client_tid;
  126. if (pthread_create(&server_tid, NULL, server_func, NULL) != 0) {
  127. handle_error_message("Failed to create server thread");
  128. }
  129. usleep(200 * 1000); // 200 milliseconds
  130. if (pthread_create(&client_tid, NULL, client_func, NULL) != 0) {
  131. handle_error_message("Failed to create client thread");
  132. }
  133. if (pthread_join(server_tid, NULL) != 0) {
  134. handle_error_message("Failed to join server thread");
  135. }
  136. if (pthread_join(client_tid, NULL) != 0) {
  137. handle_error_message("Failed to join client thread");
  138. }
  139. printf("\nTest completed: veth_a <--> veth_d UDP communication success\n");
  140. return EXIT_SUCCESS;
  141. }