mirror of https://github.com/dirtbags/moth.git
add multicaster
This commit is contained in:
parent
77647d7cc1
commit
677c2d9a68
|
@ -0,0 +1,16 @@
|
||||||
|
MULTICASTER_PKGDIR = $(TARGET)/multicaster
|
||||||
|
|
||||||
|
multicaster-install: multicaster-build
|
||||||
|
mkdir -p $(MULTICASTER_PKGDIR)
|
||||||
|
|
||||||
|
mkdir -p $(MULTICASTER_PKGDIR)/bin/
|
||||||
|
$(MAKE) -C packages/multicaster/src install DESTDIR=$(CURDIR)/$(MULTICASTER_PKGDIR)
|
||||||
|
|
||||||
|
multicaster-clean:
|
||||||
|
rm -rf $(MULTICASTER_PKGDIR)
|
||||||
|
$(MAKE) -C packages/multicaster/src clean
|
||||||
|
|
||||||
|
multicaster-build:
|
||||||
|
$(MAKE) -C packages/multicaster/src build
|
||||||
|
|
||||||
|
PACKAGES += multicaster
|
|
@ -0,0 +1,12 @@
|
||||||
|
CFLAGS = -Wall -Werror
|
||||||
|
LDFLAGS = -static
|
||||||
|
TARGETS = multicaster-server multicaster-client
|
||||||
|
|
||||||
|
all: build
|
||||||
|
build: $(TARGETS)
|
||||||
|
|
||||||
|
install: $(TARGETS)
|
||||||
|
install -m 0755 $(TARGETS) $(DESTDIR)/bin
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f *.o $(TARGETS)
|
|
@ -0,0 +1,109 @@
|
||||||
|
/* multicast_client.c
|
||||||
|
* Adopted from tmouse's client/server example code
|
||||||
|
* found at http://cboard.cprogramming.com/showthread.php?t=67469
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <netdb.h>
|
||||||
|
#include <stdio.h> /* for printf() and fprintf() */
|
||||||
|
#include <stdlib.h> /* for atoi() and exit() */
|
||||||
|
#include <string.h> /* for memset() */
|
||||||
|
#include <time.h> /* for timestamps */
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
void DieWithError(const char* errorMessage)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "%s\n", errorMessage);
|
||||||
|
exit(10);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
int sock; /* Socket */
|
||||||
|
char* multicastIP; /* Arg: IP Multicast Address */
|
||||||
|
char* multicastPort; /* Arg: Port */
|
||||||
|
struct addrinfo * multicastAddr = {0}; /* Multicast Address */
|
||||||
|
struct addrinfo * localAddr; /* Local address to bind to */
|
||||||
|
struct addrinfo hints = { 0 }; /* Hints for name lookup */
|
||||||
|
|
||||||
|
if ( argc != 3 )
|
||||||
|
{
|
||||||
|
fprintf(stderr,"Usage: %s <Multicast IP> <Multicast Port>\n", argv[0]);
|
||||||
|
exit(10);
|
||||||
|
}
|
||||||
|
|
||||||
|
multicastIP = argv[1]; /* First arg: Multicast IP address */
|
||||||
|
multicastPort = argv[2]; /* Second arg: Multicast port */
|
||||||
|
|
||||||
|
/* Resolve the multicast group address */
|
||||||
|
hints.ai_family = PF_INET6;
|
||||||
|
hints.ai_flags = AI_NUMERICHOST;
|
||||||
|
if ( getaddrinfo(multicastIP, NULL, &hints, &multicastAddr) != 0 ) DieWithError("getaddrinfo() failed");
|
||||||
|
|
||||||
|
/* Get a local address with the same family as our multicast group */
|
||||||
|
hints.ai_family = multicastAddr->ai_family;
|
||||||
|
hints.ai_socktype = SOCK_DGRAM;
|
||||||
|
hints.ai_flags = AI_PASSIVE; /* Return an address we can bind to */
|
||||||
|
if ( getaddrinfo(NULL, multicastPort, &hints, &localAddr) != 0 )
|
||||||
|
{
|
||||||
|
DieWithError("getaddrinfo() failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Create socket for receiving datagrams */
|
||||||
|
if ( (sock = socket(localAddr->ai_family, localAddr->ai_socktype, 0)) == -1 )
|
||||||
|
{
|
||||||
|
DieWithError("socket() failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
const int trueValue = 1;
|
||||||
|
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const void *) &trueValue, sizeof(trueValue));
|
||||||
|
#ifdef __APPLE__
|
||||||
|
setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, (const void *) &trueValue, sizeof(trueValue));
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Bind to the multicast port */
|
||||||
|
if ( bind(sock, localAddr->ai_addr, localAddr->ai_addrlen) != 0 )
|
||||||
|
{
|
||||||
|
DieWithError("bind() failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Join the multicast group. */
|
||||||
|
if ((multicastAddr->ai_family == PF_INET6)&&(multicastAddr->ai_addrlen == sizeof(struct sockaddr_in6)))
|
||||||
|
{
|
||||||
|
struct sockaddr_in6 *addr = (struct sockaddr_in6 *)(multicastAddr->ai_addr);
|
||||||
|
struct ipv6_mreq multicastRequest; /* Multicast address join structure */
|
||||||
|
|
||||||
|
/* Specify the multicast group */
|
||||||
|
memcpy(&multicastRequest.ipv6mr_multiaddr, &((struct sockaddr_in6*)(multicastAddr->ai_addr))->sin6_addr, sizeof(multicastRequest.ipv6mr_multiaddr));
|
||||||
|
|
||||||
|
printf("scope_id: %d\n", addr->sin6_scope_id);
|
||||||
|
|
||||||
|
/* Accept multicast from any interface */
|
||||||
|
multicastRequest.ipv6mr_interface = addr->sin6_scope_id;
|
||||||
|
|
||||||
|
/* Join the multicast address */
|
||||||
|
if ( setsockopt(sock, IPPROTO_IPV6, IPV6_JOIN_GROUP, (char*) &multicastRequest, sizeof(multicastRequest)) != 0 ) DieWithError("setsockopt(IPV6_JOIN_GROUP) failed");
|
||||||
|
}
|
||||||
|
else DieWithError("Not IPv6");
|
||||||
|
|
||||||
|
freeaddrinfo(localAddr);
|
||||||
|
freeaddrinfo(multicastAddr);
|
||||||
|
|
||||||
|
for (;;) /* Run forever */
|
||||||
|
{
|
||||||
|
char recvString[500]; /* Buffer for received string */
|
||||||
|
int recvStringLen; /* Length of received string */
|
||||||
|
|
||||||
|
/* Receive a single datagram from the server */
|
||||||
|
if ((recvStringLen = recvfrom(sock, recvString, sizeof(recvString) - 1, 0, NULL, 0)) < 0) DieWithError("recvfrom() failed");
|
||||||
|
recvString[recvStringLen] = '\0';
|
||||||
|
|
||||||
|
/* Print the received string */
|
||||||
|
printf("Received string [%s]\n", recvString);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* NOT REACHED */
|
||||||
|
close(sock);
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
|
}
|
|
@ -0,0 +1,71 @@
|
||||||
|
/* multicast_server.c
|
||||||
|
* Adapted from tmouse's IPv6 client/server example code
|
||||||
|
* found at http://cboard.cprogramming.com/showthread.php?t=67469
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h> /* for fprintf() */
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <netdb.h>
|
||||||
|
#include <stdlib.h> /* for atoi() and exit() */
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
static void DieWithError(const char* errorMessage)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "%s\n", errorMessage);
|
||||||
|
exit(10);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
int sock; /* Socket */
|
||||||
|
char* multicastIP; /* Arg: IP Multicast address */
|
||||||
|
char* multicastPort; /* Arg: Server port */
|
||||||
|
char* sendString; /* Arg: String to multicast */
|
||||||
|
size_t sendStringLen; /* Length of string to multicast */
|
||||||
|
struct addrinfo * multicastAddr; /* Multicast address */
|
||||||
|
struct addrinfo hints = { 0 }; /* Hints for name lookup */
|
||||||
|
|
||||||
|
if ( argc != 4 )
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Usage: %s <Multicast Address> <Port> <Send String>\n", argv[0]);
|
||||||
|
exit(10);
|
||||||
|
}
|
||||||
|
|
||||||
|
multicastIP = argv[1]; /* First arg: multicast IP address */
|
||||||
|
multicastPort = argv[2]; /* Second arg: multicast port */
|
||||||
|
sendString = argv[3]; /* Third arg: String to multicast */
|
||||||
|
sendStringLen = strlen(sendString); /* Find length of sendString */
|
||||||
|
|
||||||
|
/* Resolve destination address for multicast datagrams */
|
||||||
|
hints.ai_family = PF_INET6;
|
||||||
|
hints.ai_socktype = SOCK_DGRAM;
|
||||||
|
hints.ai_flags = AI_NUMERICHOST;
|
||||||
|
if (getaddrinfo(multicastIP, multicastPort, &hints, &multicastAddr) != 0) DieWithError("getaddrinfo() failed");
|
||||||
|
|
||||||
|
/* Create socket for sending multicast datagrams */
|
||||||
|
if ((sock = socket(multicastAddr->ai_family, multicastAddr->ai_socktype, 0)) == -1) DieWithError("socket() failed");
|
||||||
|
|
||||||
|
//int scope_id = if_nametoindex("eth0");
|
||||||
|
//if (setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_IF, (const char *) &scope_id, sizeof(scope_id)) != 0) DieWithError("setsockopt(MULTICAST_IF) failed");
|
||||||
|
|
||||||
|
for (;;) /* Run forever */
|
||||||
|
{
|
||||||
|
int sendLen = sendto(sock, sendString, sendStringLen, 0, multicastAddr->ai_addr, multicastAddr->ai_addrlen);
|
||||||
|
if (sendLen == sendStringLen )
|
||||||
|
{
|
||||||
|
printf("Sent [%s] (%i bytes) to %s, port %s\n", sendString, sendLen, multicastIP, multicastPort);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
DieWithError("sendto() sent a different number of bytes than expected");
|
||||||
|
}
|
||||||
|
sleep(1); /* Multicast sendString in datagram to clients every second */
|
||||||
|
}
|
||||||
|
|
||||||
|
/* NOT REACHED */
|
||||||
|
freeaddrinfo(multicastAddr);
|
||||||
|
close(sock);
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in New Issue