From f02095ee5ceb78f1781a9e250693b8866ca42181 Mon Sep 17 00:00:00 2001 From: deva Date: Sat, 4 Mar 2006 14:53:17 +0000 Subject: *** empty log message *** --- server/multicast.cc | 178 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 178 insertions(+) create mode 100644 server/multicast.cc (limited to 'server/multicast.cc') diff --git a/server/multicast.cc b/server/multicast.cc new file mode 100644 index 0000000..0072c19 --- /dev/null +++ b/server/multicast.cc @@ -0,0 +1,178 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * multicast.cc + * + * Mon Sep 26 12:25:22 CEST 2005 + * Copyright 2005 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of MIaV. + * + * MIaV is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MIaV is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with MIaV; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ +#include "config.h" +#include "multicast.h" + +#include "multicast_configuration.h" + +#include "miav_config.h" + +#include +#include +#include +#include +#include +#include + +// For IP_MTU +//#include +//#ifndef IP_MTU +//#define IP_MTU 14 +//#endif + +#include + +Multicast::Multicast(Info *i, mcastconf_t &mcclientconf) +{ + info = i; + udp_buffer = NULL; + + multicast_audio = mcclientconf.with_audio; + + // Open connection socket + if(!UDPOpen(mcclientconf.addr.c_str(), mcclientconf.port)) + info->error("Error creating socket %s:%d", + mcclientconf.addr.c_str(), + mcclientconf.port); + + int mtu = config->readInt("udp_packet_size"); + + // Create buffer with the size of MTU + // socklen_t mtu_sz; + // if(getsockopt(sock, SOL_IP, IP_MTU, &mtu, &mtu_sz) != -1) { + + udp_buffer_size = mtu - 28; + if(udp_buffer_size < 1) udp_buffer_size = 1; + udp_buffer = new char[udp_buffer_size]; + udp_buffer_pointer = udp_buffer; + info->info("UDP packet buffer size %db", udp_buffer_size); + + //} else { + // info->error("Error getting MTU size from socket: %s", strerror(errno)); + // return; + //} +} + +Multicast::~Multicast() +{ + if(udp_buffer) delete udp_buffer; +} + +int Multicast::Write(void* buf, int size) +{ + if(!udp_buffer) return 0; // no buffer to write in... better break out! + + // info->info("To send: %d", size); + + char *p = (char*)buf; + int left = udp_buffer_size - (udp_buffer_pointer - udp_buffer); + + while(size) { + int to_copy = size > left ? left : size; + + memcpy(udp_buffer_pointer, p, to_copy); + + left-=to_copy; + udp_buffer_pointer += to_copy; + + p+=to_copy; + size-=to_copy; + + // info->info("Copied %d - %d to go", to_copy, size); + + if(left == 0) { + // info->info("Sending full packet"); + write(sock, udp_buffer, udp_buffer_size); + left = udp_buffer_size; + udp_buffer_pointer = udp_buffer; + } + } + return size; +} + +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(const char *address, int port) +{ + int enable = 1L; + struct sockaddr_in stAddr; + struct sockaddr_in stLclAddr; + struct hostent * host; + // int sock; + + stAddr.sin_family = AF_INET; + stAddr.sin_port = htons(port); + if((host = gethostbyname(address)) == NULL) return false; + stAddr.sin_addr = *((struct in_addr *) host->h_addr_list[0]); + + // 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; + + // 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 + 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 + 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) + return false; + } else { + // Bind the socket to port + stLclAddr.sin_family = AF_INET; + stLclAddr.sin_addr.s_addr = htonl(INADDR_ANY); + stLclAddr.sin_port = htons(0); + if(bind(sock, (struct sockaddr*) & stLclAddr, sizeof(stLclAddr)) < 0) + return false; + } + + connect(sock, (struct sockaddr*) & stAddr, sizeof(stAddr)); + + return true; +} -- cgit v1.2.3