Exemple #1
0
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;
}
Exemple #2
0
/** Create listening OSocket objects, and register them with the EventLoop.
 *
 * If callback is non-NULL, it is registered to the OCOMM eventloop to handle
 * monitor_in events.
 *
 * \param name name of the object, used for debugging
 * \param node address or name to listen on; defaults to all if NULL
 * \param service symbolic name or port number of the service to bind to
 * \param callback function to call when a client connects
 * \param handle pointer to opaque data passed to callback function
 * \return a pointer to a linked list of Socket objects
 *
 * \see socket_in_new
 */
Socket*
socket_server_new(const char* name, const char* node, const char* service, o_so_connect_callback callback, void* handle)
{
  Socket *socketlist;
  SocketInt *it;

  socketlist = socket_in_new(name, node, service, TRUE);

  for (it=(SocketInt*)socketlist; it; it=(SocketInt*)it->next) {
    listen(it->sockfd, 5);
    it->connect_callback = callback;
    it->connect_handle = handle;

    if (callback) {
      // Wait for a client to connect. Handle that in callback
      eventloop_on_monitor_in_channel((Socket*)it, on_client_connect, NULL, it);
    }
  }
  return socketlist;
}
Exemple #3
0
/** Create a listening Socket object and register it to the global EventLoop.
 *
 * If callback is non-NULL, it is registered to the OCOMM eventloop to handle
 * monitor_in events.
 *
 * \param name name of this object, used for debugging
 * \param port port to listen on
 * \param callback function to call when a client connects
 * \param handle pointer to opaque data passed to callback function
 * \return a pointer to the SocketInt object (cast as a Socket)
 */
Socket*
socket_server_new(
  char* name,
  int port,
  o_so_connect_callback callback,
  void* handle
) {
  SocketInt* self;
  if ((self = (SocketInt*)socket_in_new(name, port, TRUE)) == NULL)
    return NULL;

  listen(self->sockfd, 5);
  self->connect_callback = callback;
  self->connect_handle = handle;

  if (callback) {
    // Wait for a client to connect. Handle that in callback
    eventloop_on_monitor_in_channel((Socket*)self, on_client_connect, NULL, self);
  }
  return (Socket*)self;
}