Exemplo n.º 1
0
Arquivo: socket.c Projeto: maxott/oml
Socket*
socket_mc_in_new(
  char* name,   //! Name used for debugging
  char* addr,   //! IP address of the multicast socket/channel.
  int port, //! Port used
  char* iface //! Name of the interface (eth0/eth1) to bind to
) {
  SocketInt* self;
  if ((self = (SocketInt*)socket_in_new(name, port, FALSE)) == NULL)
    return NULL;

  //  self->addr = addr;
  //self->localport = port;
  self->iface = iface;

  // JOIN multicast group on default interface
  self->imreq.imr_multiaddr.s_addr = inet_addr(addr);
  self->imreq.imr_interface.s_addr = iface2addr(name, iface);

  if (setsockopt(self->sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
         (const void *)&(self->imreq),
         sizeof(struct ip_mreq)) < 0) {
    o_log(O_LOG_ERROR, "socket:%s: Error while joining a multicast group\n", name);
    return NULL;
  }
  o_log(O_LOG_DEBUG, "socket:%s: Ready to receive data on multicast address: %s\n",
    name, addr);
  return (Socket*)self;
}
Exemplo n.º 2
0
Socket*
socket_mc_out_new(
  char* name,   //! Name used for debugging
  char* mcast_addr,   //! IP address of the multicast socket/channel.
  int mcast_port, //! Port used
  char* iface //! Name of the interface (eth0/eth1) to bind to
) {
  SocketInt* self;
//  if ((self = (SocketInt*)socket_new(name, mcast_addr, 0)) == NULL)
//    return NULL;
  if ((self = (SocketInt*)socket_new(name, FALSE)) == NULL)
    return NULL;

  // Multicast parameters
  unsigned char ttl = 3;
  unsigned char one = 3;  // loopback

  struct in_addr addr;
  addr.s_addr = iface2addr(name, iface);
  o_log(O_LOG_DEBUG, "socket:%s: Binding to %x\n", name, addr.s_addr);
  if (setsockopt(self->sockfd, IPPROTO_IP, IP_MULTICAST_IF, &addr, sizeof(addr)) < 0) {
    o_log (O_LOG_ERROR, "socket:%s: Setting outgoing interface for socket\n\t%s",
       name, strerror(errno));
    socket_free((Socket*)socket);
    return NULL;
  }

  if (setsockopt(self->sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl,
        sizeof(unsigned char)) < 0) {
    o_log(O_LOG_ERROR, "socket:%s: While setting TTL parameter for multicast socket\n\t%s",
      name, strerror(errno));
    socket_free((Socket*)socket);
    return NULL;
  }
  if (setsockopt(self->sockfd, IPPROTO_IP, IP_MULTICAST_LOOP,
         &one, sizeof(unsigned char)) < 0) {
    o_log(O_LOG_ERROR, "socket%s: While setting the loopback on multicast socket\n\t%s",
      name, strerror(errno));
    socket_free((Socket*)socket);
    return NULL;
  }

  //  self->addr = mcast_addr;
  self->servAddr.sin_port = htons(mcast_port);
  self->servAddr.sin_addr.s_addr = inet_addr(mcast_addr);
  o_log(O_LOG_DEBUG, "socket:%s: Ready to send data on: %s:%d\n",
    name, mcast_addr, mcast_port);
  return (Socket*)self;
}