test_veth_bridge.c 5.0 KB

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