0
votes

I am trying to configure my MOCAP node (virtual machine) IP address to match that of MOTIVE software on my PC. The MOCAP.yaml file is intially set to 224.0.0.1 but every time I change it to match the IP address of my machine it cause an EINVAL error like this:

terminate called after throwing an instance of 'SocketException'

what(): Failed to set socket option: EINVAL

=========================================================================

REQUIRED process [mocap_node-1] has died! process has died [pid 4575, exit code -6, cmd /home/radlab/mocap/devel/lib/mocap_optitrack/mocap_node __name:=mocap_node __log:=/home/radlab/.ros/log/3fe16008-4521-11e6-9638-080027e2597b/mocap_node-1.log]. log file: /home/radlab/.ros/log/3fe16008-4521-11e6-9638-080027e2597b/mocap_node-1*.log

Initiating shutdown!

=========================================================================

Here is the code for the socket.cpp

 /*
 * Socket.cpp
 *
 *  Created on: 14.11.2008
 *      Author:
 */

 // Implementation of the Socket class.

#include "mocap_optitrack/socket.h"
#include <cstring>
#include <cerrno>
#include <fcntl.h>
#include <iostream>
#include <stdio.h>
#include <sstream>

#include <ros/ros.h>

UdpMulticastSocket::UdpMulticastSocket( const int local_port, const std::string multicast_ip ) 
{
  // Create a UDP socket
  ROS_INFO( "Creating socket..." );
  m_socket = socket( AF_INET, SOCK_DGRAM, 0 );
  if( m_socket < 0 )
    throw SocketException( strerror( errno ) );

  // Allow reuse of local addresses
  ROS_INFO( "Setting socket options..." );
  int option_value = 1;
  int result = setsockopt( m_socket, SOL_SOCKET, SO_REUSEADDR, (void*)&option_value, sizeof( int ) );
  if( result == -1 )
  {
    std::stringstream error;
    error << "Failed to set socket option: ";
    switch( errno )
    {
      case EBADF:
  error << "EBADF";
  break;
      case EFAULT:
  error << "EFAULT";
  break;
      case EINVAL:
  error << "EINVAL";
  break;
      case ENOPROTOOPT:
  error << "ENOPROTOOPT";
  break;
      case ENOTSOCK:
  error << "ENOTSOCK";
  break;
      default:
  error << "unknown error";
  break;
    }
    throw SocketException( error.str().c_str() );    
  }

  // Fill struct for local address
  memset ( &m_local_addr, 0, sizeof ( m_local_addr ) );
  m_local_addr.sin_family = AF_INET;
  m_local_addr.sin_addr.s_addr = htonl( INADDR_ANY );
  m_local_addr.sin_port = htons( local_port );
  ROS_INFO( "Local address: %s:%i", inet_ntoa( m_local_addr.sin_addr ), ntohs( m_local_addr.sin_port ) );

  // Bind the socket
  ROS_INFO( "Binding socket to local address..." );
  result = bind( m_socket, (sockaddr*)&m_local_addr, sizeof( m_local_addr ));
  if( result == -1 )
  {
    std::stringstream error;
    error << "Failed to bind socket to local address:" << strerror( errno );
    throw SocketException( error.str().c_str() );
  }

  // Join multicast group
  struct ip_mreq mreq;
  mreq.imr_multiaddr.s_addr = inet_addr( multicast_ip.c_str() );
  mreq.imr_interface = m_local_addr.sin_addr;
  ROS_INFO( "Joining multicast group %s...", inet_ntoa( mreq.imr_multiaddr ) );

  result = setsockopt(m_socket, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&mreq, sizeof(mreq));
  if( result == -1 )
  {
    std::stringstream error;
    error << "Failed to set socket option: ";
    switch( errno )
    {
      case EBADF:
  error << "EBADF";
  break;
      case EFAULT:
  error << "EFAULT";
  break;
      case EINVAL:
  error << "EINVAL";
  break;
      case ENOPROTOOPT:
  error << "ENOPROTOOPT";
  break;
      case ENOTSOCK:
  error << "ENOTSOCK";
  break;
      default:
  error << "unknown error";
  break;
    }
     throw SocketException( error.str().c_str() );    
  }

  // Make socket non-blocking
  ROS_INFO( "Enabling non-blocking I/O" );
  int flags = fcntl( m_socket, F_GETFL , 0 );
  result = fcntl(m_socket, F_SETFL, flags | O_NONBLOCK);
  if( result == -1 )
  {
    std::stringstream error;
    error << "Failed to enable non-blocking I/O: " << strerror( errno );
    throw SocketException( error.str().c_str() );
  }
}

UdpMulticastSocket::~UdpMulticastSocket()
{
  close( m_socket );
}

int UdpMulticastSocket::recv()
{
  memset ( buf, 0, MAXRECV + 1 );

  sockaddr_in remote_addr;
  int addr_len = sizeof(struct sockaddr);
  int status = recvfrom(
    m_socket,
    buf,
    MAXRECV,
    0,
    (sockaddr *)&remote_addr,
    (socklen_t*)&addr_len);

  if( status > 0 )
    ROS_DEBUG( "%4i bytes received from %s:%i", status, inet_ntoa( remote_addr.sin_addr ), ntohs( remote_addr.sin_port ) );
  else if( status == 0 )
    ROS_INFO( "Connection closed by peer" );

  return status;
}

Can anyone help me figure out why I am getting this error when I change the IP address?

1
side note: you can avoid this switch( errno ) { ... } with strerror defined in <cstring> - yussuf

1 Answers

0
votes

I don't know what mocap or motiv is, but I think you're trying to use a non-multicast ip-address to join to multicast group in line 79. That's propably what's passed to the function with std::string multicast_ip from the .yaml file.

See man page:

   IP_ADD_MEMBERSHIP (since Linux 1.2)
          Join a multicast group.  Argument is an ip_mreqn structure.

              struct ip_mreqn {
                  struct in_addr imr_multiaddr; /* IP multicast group
                                                   address */
                  struct in_addr imr_address;   /* IP address of local
                                                   interface */
                  int            imr_ifindex;   /* interface index */
              };

          imr_multiaddr contains the address of the multicast group the
          application wants to join or leave.  It must be a valid
          multicast address (or setsockopt(2) fails with the error
          EINVAL).  imr_address is the address of the local interface
          with which the system should join the multicast group; if it
          is equal to INADDR_ANY, an appropriate interface is chosen by
          the system.  imr_ifindex is the interface index of the
          interface that should join/leave the imr_multiaddr group, or 0
          to indicate any interface.

          The ip_mreqn structure is available only since Linux 2.2.  For
          compatibility, the old ip_mreq structure (present since Linux
          1.2) is still supported; it differs from ip_mreqn only by not
          including the imr_ifindex field.  (The kernel determines which
          structure is being passed based on the size passed in optlen.)

          IP_ADD_MEMBERSHIP is valid only for setsockopt(2).