/////////////////////////////////////////////////////////////////
// HRESULT CBase::GetInterface
//
/////////////////////////////////////////////////////////////////
IUnknown* CBase::GetInterface(REFIID riid)
{
	//Delegate to the derived class...
	IUnknown** ppIUnknown = GetInterfaceAddress(riid);
	if(ppIUnknown)
		return *ppIUnknown;

	return NULL;
}
/////////////////////////////////////////////////////////////////
// HRESULT CBase::SetInterface
//
/////////////////////////////////////////////////////////////////
HRESULT CBase::SetInterface(REFIID riid, IUnknown* pIUnknown)
{
	//We need to put the obtained interface in the appropiate member...
	if(pIUnknown)
	{
		//First obtain the correct interface member variable...
		IUnknown** ppInterfaceMember = GetInterfaceAddress(riid);
		if(!ppInterfaceMember)
			return E_NOINTERFACE;

		TRACE_RELEASE(*ppInterfaceMember, GetObjectName());
		TRACE_ADDREF(pIUnknown, GetObjectName());
		*ppInterfaceMember = pIUnknown;
	}

	return S_OK;
}
Exemple #3
0
void PopulatePiConfig(char *ipAddress, char *buf)
{
	char *iface = NULL;
	char addr[16];
	char mask[16];
	char gw[16];

	iface = FindInterfaceForIP(ipAddress);
	if (GetInterfaceAddress(iface, addr, mask, gw))
		return;

	gethostname((char*)buf+7, 31);

	// FIXME, need to fill this in with info on the right interface
	buf[39] = 0x00; // 0 = DHCP, 1 = static

	(*(in_addr_t *)&buf[40]) = inet_addr(addr);
	(*(in_addr_t *)&buf[44]) = inet_addr(gw);
	(*(in_addr_t *)&buf[48]) = inet_addr(mask);
}
Exemple #4
0
MPIPTVSOURCE_API int LeaveMulticastGroupIPv4(CLogger *logger, const TCHAR *protocolName, SOCKET m_socket, const struct sockaddr_in *source, const struct sockaddr_in *local, CParameterCollection *parameters)
{
  ADDRINFOT *networkInterface = NULL;
  ADDRINFOT hints;
  hints.ai_family = local->sin_family;
  hints.ai_socktype = SOCK_DGRAM;
  hints.ai_protocol = IPPROTO_UDP;
  hints.ai_flags = AI_PASSIVE;
  int result = 0;

  if (parameters != NULL)
  {
    // we have set some parameters
    // get interface parameter
    PCParameter networkInterfaceParameter = parameters->GetParameter(INTERFACE_PARAMETER_NAME, true);
    if (networkInterfaceParameter != NULL)
    {
      TCHAR *networkInterfaceAddress = GetInterfaceAddress(logger, protocolName, networkInterfaceParameter->GetValue(), local->sin_family);
      int errorCode = GetIpAddress(networkInterfaceAddress, 0, &networkInterface, &hints);
      if (errorCode != 0)
      {
        logger->Log(LOGGER_ERROR, _T("%s: %s: getaddrinfo() for network interface error: %d"), protocolName, METHOD_LEAVE_MULTICAST_GROUP_IPV4_NAME, errorCode);
        result = -1;
      }
      if (result == 0)
      {
        logger->Log(LOGGER_INFO, _T("%s: %s: try to bind on interface '%s', address: %s"), protocolName, METHOD_LEAVE_MULTICAST_GROUP_IPV4_NAME, networkInterfaceParameter->GetValue(), networkInterfaceAddress);
      }
      FREE_MEM(networkInterfaceAddress);
    }
  }

  if (result == 0)
  {
    union
    {
      struct ip_mreq gr4;
      struct ip_mreq_source gsr4;
    } opt;
    int cmd;
    struct in_addr id;

    if (networkInterface != NULL)
    {
      id = ((sockaddr_in *)networkInterface->ai_addr)->sin_addr;
    }
    else
    {
      id.s_addr = INADDR_ANY;
    }
    socklen_t optlen;

    memset(&opt, 0, sizeof(opt));
    if (source != NULL)
    {
      cmd = IP_DROP_SOURCE_MEMBERSHIP;
      opt.gsr4.imr_multiaddr = local->sin_addr;
      opt.gsr4.imr_sourceaddr = source->sin_addr;
      opt.gsr4.imr_interface = id;
      optlen = sizeof (opt.gsr4);
    }
    else
    {
      cmd = IP_DROP_MEMBERSHIP;
      opt.gr4.imr_multiaddr = local->sin_addr;
      opt.gr4.imr_interface = id;
      optlen = sizeof(opt.gr4);
    }

    logger->Log(LOGGER_INFO, METHOD_MESSAGE_FORMAT, protocolName, METHOD_LEAVE_MULTICAST_GROUP_IPV4_NAME, (source != NULL) ? _T("IP_DROP_SOURCE_MEMBERSHIP multicast request") : _T("IP_DROP_MEMBERSHIP multicast request"));
    result = (SetSocketOption(logger, protocolName, METHOD_LEAVE_MULTICAST_GROUP_IPV4_NAME, (source != NULL) ? _T("IP_DROP_SOURCE_MEMBERSHIP") : _T("IP_DROP_MEMBERSHIP"), m_socket, IPPROTO_IP, cmd, (char *)&opt, optlen) == 0) ? 0 : (-1);
  }

  if (networkInterface != NULL)
  {
    FreeAddrInfo(networkInterface);
    networkInterface = NULL;
  }

  logger->Log(LOGGER_INFO, (result == 0) ? METHOD_END_FORMAT : METHOD_END_FAIL_FORMAT, protocolName, METHOD_LEAVE_MULTICAST_GROUP_IPV4_NAME);
  return result;
}
Exemple #5
0
void Bridge_Initialize(int &eSock, int &dSock)
{
	LogExcess(VB_E131BRIDGE, "Bridge_Initialize()\n");

    // prepare the msg receive buffers
    memset(msgs, 0, sizeof(msgs));
    for (int i = 0; i < MAX_MSG; i++) {
        iovecs[i].iov_base         = buffers[i];
        iovecs[i].iov_len          = BUFSIZE;
        msgs[i].msg_hdr.msg_iov    = &iovecs[i];
        msgs[i].msg_hdr.msg_iovlen = 1;
    }
    
	/* Initialize our Universe Index lookup cache */
	for (int i = 0; i < 65536; i++)
		UniverseCache[i] = BRIDGE_INVALID_UNIVERSE_INDEX;

	LoadInputUniversesFromFile();
	LogInfo(VB_E131BRIDGE, "Universe Count = %d\n",InputUniverseCount);
	InputUniversesPrint();
    
    
    // FIXME FIXME FIXME FIXME
    // This is a total hack to get a file descriptor greater than 2
    // because otherwise, the bind() call later will fail for some reason.
    // FIXME FIXME FIXME FIXME
    int i1 = socket(AF_INET, SOCK_DGRAM, 0);
    int i2 = socket(AF_INET, SOCK_DGRAM, 0);
    int i3 = socket(AF_INET, SOCK_DGRAM, 0);
    
    ddpSock = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, 0);
    if (ddpSock < 0) {
        LogDebug(VB_E131BRIDGE, "e131bridge DDP socket failed: %s", strerror(errno));
        exit(1);
    }
    memset((char *)&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
    addr.sin_port = htons(DDP_PORT);
    addrlen = sizeof(addr);
    // Bind the socket to address/port
    if (bind(ddpSock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
        LogDebug(VB_E131BRIDGE, "e131bridge DDP bind failed: %s", strerror(errno));
        exit(1);
    }

	int            UniverseOctet[2];
	struct ip_mreq mreq;
	char           strMulticastGroup[16];

	/* set up socket */
	bridgeSock = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, 0);
	if (bridgeSock < 0) {
		LogDebug(VB_E131BRIDGE, "e131bridge socket failed: %s", strerror(errno));
		exit(1);
	}

	// FIXME, move this to /etc/sysctl.conf or our startup script
	system("sudo sysctl net/ipv4/igmp_max_memberships=512");

	memset((char *)&addr, 0, sizeof(addr));
	addr.sin_family = AF_INET;
	addr.sin_addr.s_addr = htonl(INADDR_ANY);
	addr.sin_port = htons(E131_DEST_PORT);
	addrlen = sizeof(addr);
	// Bind the socket to address/port
	if (bind(bridgeSock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
		LogDebug(VB_E131BRIDGE, "e131bridge bind failed: %s", strerror(errno));
		exit(1);
	}
    
    //get all the addresses
    struct ifaddrs *interfaces,*tmp;
    getifaddrs(&interfaces);
    
	char address[16];
    address[0] = 0;
	// Join the multicast groups
	for(int i = 0; i < InputUniverseCount; i++)  {
		if (InputUniverses[i].type == E131_TYPE_MULTICAST) {
			UniverseOctet[0] = InputUniverses[i].universe/256;
			UniverseOctet[1] = InputUniverses[i].universe%256;
			sprintf(strMulticastGroup, "239.255.%d.%d", UniverseOctet[0],UniverseOctet[1]);
			mreq.imr_multiaddr.s_addr = inet_addr(strMulticastGroup);

			LogInfo(VB_E131BRIDGE, "Adding group %s\n", strMulticastGroup);

            // add group to groups to listen for on eth0 and wlan0 if it exists
            int multicastJoined = 0;
            tmp = interfaces;
            //loop through all the interfaces and subscribe to the group
            while (tmp) {
                //struct sockaddr_in *sin = (struct sockaddr_in *)tmp->ifa_addr;
                //strcpy(address, inet_ntoa(sin->sin_addr));
                if (tmp->ifa_addr && tmp->ifa_addr->sa_family == AF_INET) {
                    GetInterfaceAddress(tmp->ifa_name, address, NULL, NULL);
                    if (strcmp(address, "127.0.0.1")) {
                        LogDebug(VB_E131BRIDGE, "   Adding interface %s - %s\n", tmp->ifa_name, address);
                        mreq.imr_interface.s_addr = inet_addr(address);
                        if (setsockopt(bridgeSock, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
                            LogWarn(VB_E131BRIDGE, "   Could not setup Multicast Group for interface %s\n", tmp->ifa_name);
                        }
                        multicastJoined = 1;
                    }
                } else if (tmp->ifa_addr && tmp->ifa_addr->sa_family == AF_INET6) {
                    //FIXME for ipv6 multicast
                    //LogDebug(VB_E131BRIDGE, "   Inet6 interface %s\n", tmp->ifa_name);
                }
                tmp = tmp->ifa_next;
            }

			if (!multicastJoined) {
				LogDebug(VB_E131BRIDGE, "  Binding to default interface\n");
				mreq.imr_interface.s_addr = htonl(INADDR_ANY);
				if (setsockopt(bridgeSock, IPPROTO_IP, IP_ADD_MEMBERSHIP,&mreq, sizeof(mreq)) < 0) {
                    LogWarn(VB_E131BRIDGE, "   Could not setup Multicast Group\n");
				}
			}
		}
	}
    freeifaddrs(interfaces);

	StartChannelOutputThread();
    
    if (i1 >= 0) close(i1);
    if (i2 >= 0) close(i2);
    if (i3 >= 0) close(i3);

    eSock = bridgeSock;
    dSock = ddpSock;
}