diff options
Diffstat (limited to 'src/multicast.cc')
-rw-r--r-- | src/multicast.cc | 51 |
1 files changed, 40 insertions, 11 deletions
diff --git a/src/multicast.cc b/src/multicast.cc index 0cf1b87..b64bfde 100644 --- a/src/multicast.cc +++ b/src/multicast.cc @@ -35,8 +35,18 @@ Multicast::Multicast(Info *i) { - char addr[] = "192.168.0.10"; - int port = 666; + /* + Multicast adresses: + 224.0.0.1 All systems on this subnet + 224.0.0.2 All routers on this subnet + 224.0.0.5 OSPF routers + 224.0.0.6 OSPF designated routers + 224.0.0.12 DHCP server/relay agent + */ + + // Multicast to all systems on this subnet + char addr[] = "224.0.0.1"; + int port = 1234; info = i; if(!UDPOpen(addr, port)) info->error("Error creating socket %s:%d", addr, port); @@ -46,16 +56,33 @@ Multicast::~Multicast() { } -void Multicast::Write(char* buf, int size) +void Multicast::Write(void* buf, int size) { - if(write(sock, buf, size) != size) info->error("Error Writing to socket."); + // char addr[] = "192.168.0.10"; + // int port = 1234; + + if(write(sock, buf, size) != size) { + info->error("Error Writing to socket."); + // if(!UDPOpen(addr, port)) info->error("Error creating socket %s:%d", addr, port); + } } +#define USE_MULTICAST + +bool Multicast::is_address_multicast(unsigned long address) +{ + if((address & 255) >= 224 && (address & 255) <= 239) { + info->info("Address is multicast."); + return true; + } + info->info("Address is NOT multicast."); + return false; +} /* * open UDP socket */ -bool Multicast::UDPOpen(char * address, int port) +bool Multicast::UDPOpen(char *address, int port) { int enable = 1L; struct sockaddr_in stAddr; @@ -68,25 +95,27 @@ bool Multicast::UDPOpen(char * address, int port) if((host = gethostbyname(address)) == NULL) return false; stAddr.sin_addr = *((struct in_addr *) host->h_addr_list[0]); - /* Create a UDP socket */ + // Create a UDP socket if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) return false; - /* Allow multiple instance of the client to share the same address and port */ if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &enable, sizeof(unsigned long int)) < 0) return false; + // Allow multiple instance of the client to share the same address and port + if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &enable, sizeof(unsigned long int)) < 0) + return false; #ifdef USE_MULTICAST - /* If the address is multicast, register to the multicast group */ + // If the address is multicast, register to the multicast group if(is_address_multicast(stAddr.sin_addr.s_addr)) { struct ip_mreq stMreq; - /* Bind the socket to port */ + // Bind the socket to port stLclAddr.sin_family = AF_INET; stLclAddr.sin_addr.s_addr = htonl(INADDR_ANY); stLclAddr.sin_port = stAddr.sin_port; if(bind(sock, (struct sockaddr*) & stLclAddr, sizeof(stLclAddr)) < 0) return false; - /* Register to a multicast address */ + // Register to a multicast address stMreq.imr_multiaddr.s_addr = stAddr.sin_addr.s_addr; stMreq.imr_interface.s_addr = INADDR_ANY; if(setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *) & stMreq, sizeof(stMreq)) < 0) @@ -95,7 +124,7 @@ bool Multicast::UDPOpen(char * address, int port) else #endif { - /* Bind the socket to port */ + // Bind the socket to port stLclAddr.sin_family = AF_INET; stLclAddr.sin_addr.s_addr = htonl(INADDR_ANY); stLclAddr.sin_port = htons(0); |