multicast_client.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (C) 2022 Amazon.com Inc. or its affiliates. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include <arpa/inet.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <sys/socket.h>
  10. #include <unistd.h>
  11. #ifdef __wasi__
  12. #include <wasi_socket_ext.h>
  13. #endif
  14. static void
  15. init_sockaddr_inet(struct sockaddr_in *addr)
  16. {
  17. addr->sin_family = AF_INET;
  18. addr->sin_port = htons(1234);
  19. }
  20. static void
  21. init_sockaddr_inet6(struct sockaddr_in6 *addr)
  22. {
  23. addr->sin6_family = AF_INET6;
  24. addr->sin6_port = htons(1234);
  25. }
  26. static int
  27. get_ip_addr_type(char *addr, char *buf)
  28. {
  29. if (inet_pton(AF_INET6, addr, buf)) {
  30. return AF_INET6;
  31. }
  32. if (inet_pton(AF_INET, addr, buf)) {
  33. return AF_INET;
  34. }
  35. return -1;
  36. }
  37. static int
  38. is_valid_addr_type(int addr_type)
  39. {
  40. return !(addr_type == -1
  41. || (addr_type != AF_INET && addr_type != AF_INET6));
  42. }
  43. int
  44. main(int argc, char *argv[])
  45. {
  46. struct ipv6_mreq ipv6_group;
  47. struct ip_mreq ipv4_group;
  48. int sd;
  49. int datalen;
  50. char databuf[1024] = { 0 };
  51. char multicast_addr_buffer[16];
  52. struct sockaddr_storage local_address = { 0 };
  53. int addr_type = -1;
  54. int read_result;
  55. int bool_opt = 1;
  56. if (argc < 2) {
  57. printf("Usage is <Multicast IP>\n");
  58. return EXIT_FAILURE;
  59. }
  60. addr_type = get_ip_addr_type(argv[1], multicast_addr_buffer);
  61. if (!is_valid_addr_type(addr_type)) {
  62. printf("Not a valid ipv4 or ipv6 address\n");
  63. return EXIT_FAILURE;
  64. }
  65. if ((sd = socket(addr_type, SOCK_DGRAM, 0)) == -1) {
  66. perror("Failed opening socket");
  67. return EXIT_FAILURE;
  68. }
  69. if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &bool_opt, sizeof(bool_opt))
  70. == -1) {
  71. perror("Failed setting SO_REUSEADDR");
  72. goto fail;
  73. }
  74. if (addr_type == AF_INET) {
  75. init_sockaddr_inet((struct sockaddr_in *)&local_address);
  76. memcpy(&(ipv4_group.imr_multiaddr), multicast_addr_buffer, 4);
  77. ipv4_group.imr_interface.s_addr = htonl(INADDR_ANY);
  78. if (setsockopt(sd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &ipv4_group,
  79. sizeof(ipv4_group))
  80. == -1) {
  81. perror("Failed joining IPv4 multicast group");
  82. goto fail;
  83. }
  84. }
  85. else {
  86. init_sockaddr_inet6((struct sockaddr_in6 *)&local_address);
  87. memcpy(&(ipv6_group.ipv6mr_multiaddr), multicast_addr_buffer, 16);
  88. ipv6_group.ipv6mr_interface = 0;
  89. if (setsockopt(sd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &ipv6_group,
  90. sizeof(ipv6_group))
  91. == -1) {
  92. perror("Failed joining IPv6 multicast group");
  93. goto fail;
  94. }
  95. }
  96. if (bind(sd, (struct sockaddr *)&local_address, sizeof(local_address))
  97. == -1) {
  98. perror("Failed binding socket");
  99. goto fail;
  100. }
  101. printf("Joined multicast group. Waiting for datagram...\n");
  102. datalen = sizeof(databuf) - 1;
  103. read_result = read(sd, databuf, datalen);
  104. if (read_result < 0) {
  105. perror("Failed binding socket");
  106. goto fail;
  107. }
  108. printf("Reading datagram message...OK.\n");
  109. printf("The message from multicast server is: \"%s\"\n", databuf);
  110. close(sd);
  111. return EXIT_SUCCESS;
  112. fail:
  113. close(sd);
  114. return EXIT_FAILURE;
  115. }