NSIS-ka
A free C++ implementation of NSIS protocols

Changeset 4159


Ignore:
Timestamp:
Jul 23, 2009, 4:46:19 PM (8 years ago)
Author:
stud-lenk
Message:

Add rudimentary multicast management facilities to TPoverUDP (including test cases)

Location:
protlib/branches/20090723-multicast
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • protlib/branches/20090723-multicast/include/tp_over_udp.h

    r3675 r4159  
    139139  };
    140140
     141  int join_multicast_group(const hostaddress group);
     142  int leave_multicast_group(const hostaddress group);
     143
    141144  int get_listener_socket() const { return master_listener_socket; }
    142145 
  • protlib/branches/20090723-multicast/src/tp_over_udp.cpp

    r4107 r4159  
    491491     
    492492
     493// helper function for joining/leaving an IPv4 multicast group
     494inline int multicast_request_ipv4(int socket, const hostaddress group, const int action)
     495{
     496  struct ip_mreq mreq;
     497  group.get_ip(mreq.imr_multiaddr);
     498  mreq.imr_interface.s_addr=htonl(INADDR_ANY);
     499  return setsockopt(socket, IPPROTO_IP, action, &mreq, sizeof(mreq));
     500}
     501
     502// helper function for joining/leaving an IPv6 multicast group
     503inline int multicast_request_ipv6(int socket, const hostaddress group, const int action)
     504{
     505  struct ipv6_mreq mreq;
     506  group.get_ip(mreq.ipv6mr_multiaddr);
     507  mreq.ipv6mr_interface = INADDR_ANY;
     508  return setsockopt(socket, IPPROTO_IPV6, action, &mreq, sizeof(mreq));
     509}
     510
     511int
     512TPoverUDP::join_multicast_group(const hostaddress group)
     513{
     514  int udp_socket = -1;
     515  do {
     516    udp_socket = get_listener_socket();
     517    DLog(tpparam.name, "UDP socket file descriptor not ready. Waiting for one second...");
     518    sleep(1);
     519  } while (udp_socket == -1);
     520
     521  unsigned char loop = 0;
     522  socklen_t size = sizeof(loop);
     523  int status = -1;
     524  if (group.is_ipv4()) {
     525    // disable loopback of IPv4 multicast packages
     526    if (setsockopt(udp_socket, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, size))
     527      ERRLog(tpparam.name, "Disabling IPv4 multicast loopback of own datagrams failed: " << strerror(errno));
     528    if (getsockopt(udp_socket, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, &size))
     529      ERRLog(tpparam.name, "Querying IPv4 multicast loopback setting failed: " << strerror(errno));
     530    if (loop == 1)
     531      WLog(tpparam.name, "IPv4 multicast loopback of own datagrams is still enabled!");
     532    // join IPv4 multicast group
     533    status = multicast_request_ipv4(udp_socket, group, IP_ADD_MEMBERSHIP);
     534  } else if (group.is_ipv6()) {
     535    // disable loopback of IPv6 multicast packages
     536    if (setsockopt(udp_socket, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &loop, size))
     537      ERRLog(tpparam.name, "Disabling IPv6 multicast loopback of own datagrams failed: " << strerror(errno));
     538    if (getsockopt(udp_socket, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &loop, &size))
     539      ERRLog(tpparam.name, "Querying IPv6 multicast loopback setting failed: " << strerror(errno));
     540    if (loop == 1)
     541      WLog(tpparam.name, "IPv6 multicast loopback of own datagrams is still enabled!");
     542    // join IPv6 multicast group
     543    status = multicast_request_ipv6(udp_socket, group, IPV6_JOIN_GROUP);
     544  }
     545
     546  return status;
     547}
     548
     549int   
     550TPoverUDP::leave_multicast_group(const hostaddress group)
     551{
     552  int udp_socket = -1;
     553  do {
     554    udp_socket = get_listener_socket();
     555    DLog(tpparam.name, "UDP socket file descriptor not ready. Waiting for one second...");
     556    sleep(1);
     557  } while (udp_socket == -1);
     558
     559  int status = -1;
     560  if (group.is_ipv4()) {
     561    // join IPv4 multicast group
     562    status = multicast_request_ipv4(udp_socket, group, IP_DROP_MEMBERSHIP);
     563  } else if (group.is_ipv6()) {
     564    // join IPv6 multicast group
     565    status = multicast_request_ipv6(udp_socket, group, IPV6_LEAVE_GROUP);
     566  }
     567
     568  return status;
     569}
     570
    493571TPoverUDP::~TPoverUDP ()
    494572{
  • protlib/branches/20090723-multicast/test

    • Property svn:ignore
      •  

        old new  
        11test_runner
         2depend
Note: See TracChangeset for help on using the changeset viewer.