2023-05-07 11:50:35 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2023-05-07 15:59:43 +00:00
|
|
|
#include <errno.h>
|
2023-05-07 11:50:35 +00:00
|
|
|
#include <unistd.h>
|
2023-05-07 15:59:43 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <winsock2.h>
|
|
|
|
#include <ws2tcpip.h>
|
2023-05-09 13:24:19 +00:00
|
|
|
//#pragma does not work on mingw
|
|
|
|
//#pragma comment(lib, "Ws2_32.lib")
|
2023-05-07 15:59:43 +00:00
|
|
|
#else
|
2023-05-07 11:50:35 +00:00
|
|
|
#include <arpa/inet.h>
|
2023-05-07 15:59:43 +00:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#endif
|
2023-05-07 11:50:35 +00:00
|
|
|
|
2023-05-07 15:59:43 +00:00
|
|
|
#define MCAST_PORT 4532
|
|
|
|
#define MCAST_ADDR "224.0.0.1"
|
|
|
|
#define BUFFER_SIZE 4096
|
2023-05-07 11:50:35 +00:00
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
2023-05-07 15:59:43 +00:00
|
|
|
int sock;
|
|
|
|
struct sockaddr_in mcast_addr;
|
|
|
|
char buffer[BUFFER_SIZE];
|
|
|
|
int bytes_received;
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
WSADATA wsaData;
|
|
|
|
|
|
|
|
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "WSAStartup failed: %d\n", WSAGetLastError());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2023-05-07 11:50:35 +00:00
|
|
|
|
2023-05-07 15:59:43 +00:00
|
|
|
if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
|
2023-05-07 11:50:35 +00:00
|
|
|
{
|
2023-05-07 15:59:43 +00:00
|
|
|
perror("socket() failed");
|
2023-05-07 11:50:35 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the SO_REUSEADDR option to allow multiple processes to use the same address
|
|
|
|
int optval = 1;
|
|
|
|
|
2023-05-08 12:37:13 +00:00
|
|
|
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&optval,
|
2023-05-07 15:59:43 +00:00
|
|
|
sizeof(optval)) < 0)
|
2023-05-07 11:50:35 +00:00
|
|
|
{
|
2023-05-07 15:59:43 +00:00
|
|
|
//rig_debug(RIG_DEBUG_ERR, "%s: setsockopt: %s\n", __func__, strerror(errno));
|
|
|
|
//return -RIG_EIO;
|
|
|
|
return 1;
|
2023-05-07 11:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-07 15:59:43 +00:00
|
|
|
memset(&mcast_addr, 0, sizeof(mcast_addr));
|
|
|
|
mcast_addr.sin_family = AF_INET;
|
|
|
|
mcast_addr.sin_port = htons(MCAST_PORT);
|
|
|
|
mcast_addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
|
|
|
|
if (bind(sock, (struct sockaddr *)&mcast_addr, sizeof(mcast_addr)) < 0)
|
2023-05-07 11:50:35 +00:00
|
|
|
{
|
2023-05-07 15:59:43 +00:00
|
|
|
perror("bind() failed");
|
2023-05-07 11:50:35 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2023-05-07 15:59:43 +00:00
|
|
|
struct ip_mreq mreq;
|
|
|
|
|
|
|
|
mreq.imr_multiaddr.s_addr = inet_addr(MCAST_ADDR);
|
|
|
|
|
2023-05-07 11:50:35 +00:00
|
|
|
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
|
|
|
|
|
2023-05-07 15:59:43 +00:00
|
|
|
if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&mreq,
|
|
|
|
sizeof(mreq)) < 0)
|
2023-05-07 11:50:35 +00:00
|
|
|
{
|
2023-05-07 15:59:43 +00:00
|
|
|
perror("setsockopt() failed");
|
2023-05-07 11:50:35 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
2023-05-07 15:59:43 +00:00
|
|
|
bytes_received = recvfrom(sock, buffer, BUFFER_SIZE, 0, NULL, 0);
|
2023-05-07 11:50:35 +00:00
|
|
|
|
2023-05-07 15:59:43 +00:00
|
|
|
if (bytes_received < 0)
|
2023-05-07 11:50:35 +00:00
|
|
|
{
|
2023-05-07 15:59:43 +00:00
|
|
|
perror("recvfrom() failed");
|
|
|
|
break;
|
2023-05-07 11:50:35 +00:00
|
|
|
}
|
|
|
|
|
2023-05-07 15:59:43 +00:00
|
|
|
buffer[bytes_received] = '\0';
|
2023-05-08 22:15:26 +00:00
|
|
|
printf("%s\n", buffer);
|
2023-05-07 11:50:35 +00:00
|
|
|
}
|
|
|
|
|
2023-05-07 15:59:43 +00:00
|
|
|
// Drop membership before closing the socket
|
|
|
|
if (setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, (char *)&mreq,
|
|
|
|
sizeof(mreq)) < 0)
|
2023-05-07 11:50:35 +00:00
|
|
|
{
|
2023-05-07 15:59:43 +00:00
|
|
|
perror("setsockopt() failed");
|
2023-05-07 11:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
close(sock);
|
2023-05-07 15:59:43 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
WSACleanup();
|
|
|
|
#endif
|
2023-05-07 11:50:35 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2023-05-07 15:59:43 +00:00
|
|
|
|