Example #1
0
/**
 * gst_net_time_packet_send:
 * @packet: the #GstNetTimePacket to send
 * @socket: socket to send the time packet on
 * @dest_address: address to send the time packet to
 * @error: return address for a #GError, or NULL
 *
 * Sends a #GstNetTimePacket over a socket.
 *
 * MT safe.
 *
 * Returns: TRUE if successful, FALSE in case an error occurred.
 */
gboolean
gst_net_time_packet_send (const GstNetTimePacket * packet,
    GSocket * socket, GSocketAddress * dest_address, GError ** error)
{
  gboolean was_blocking;
  guint8 *buffer;
  gssize res;

  g_return_val_if_fail (packet != NULL, FALSE);
  g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
  g_return_val_if_fail (G_IS_SOCKET_ADDRESS (dest_address), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  was_blocking = g_socket_get_blocking (socket);

  if (was_blocking)
    g_socket_set_blocking (socket, FALSE);

  /* FIXME: avoid pointless alloc/free, serialise into stack-allocated buffer */
  buffer = gst_net_time_packet_serialize (packet);

  res = g_socket_send_to (socket, dest_address, (const gchar *) buffer,
      GST_NET_TIME_PACKET_SIZE, NULL, error);

  /* datagram packets should be sent as a whole or not at all */
  g_assert (res < 0 || res == GST_NET_TIME_PACKET_SIZE);

  g_free (buffer);

  if (was_blocking)
    g_socket_set_blocking (socket, TRUE);

  return (res == GST_NET_TIME_PACKET_SIZE);
}
Example #2
0
/**
 * g_vfs_ftp_connection_accept_data_connection:
 * @conn: a listening connection
 * @cancellable: cancellable to interrupt wait
 * @error: %NULL or location to take a potential error
 *
 * Opens a data connection for @conn by accepting an incoming connection on the
 * address it is listening on via g_vfs_ftp_connection_listen_data_connection(),
 * which must have been called prior to this function.
 * If this function succeeds, a data connection will have been opened, and calls
 * to g_vfs_ftp_connection_get_data_stream() will work.
 *
 * Returns: %TRUE if a connection was successfully acquired
 **/
gboolean
g_vfs_ftp_connection_accept_data_connection (GVfsFtpConnection *conn,
                                             GCancellable *     cancellable,
                                             GError **          error)
{
  GSocket *accepted;
  GCancellable *timer;
  gulong cancel_cb_id;
  GIOCondition condition;

  g_return_val_if_fail (conn != NULL, FALSE);
  g_return_val_if_fail (conn->data == NULL, FALSE);
  g_return_val_if_fail (G_IS_SOCKET (conn->listen_socket), FALSE);

  timer = g_cancellable_new ();
  cancel_cb_id = g_cancellable_connect (cancellable, 
                                        G_CALLBACK (cancel_timer_cb),
                                        timer,
                                        NULL);
  g_object_ref (timer);
  g_timeout_add_seconds_full (G_PRIORITY_DEFAULT,
                              G_VFS_FTP_TIMEOUT_IN_SECONDS,
                              cancel_cancellable,
                              timer,
                              g_object_unref);

  condition = g_socket_condition_wait (conn->listen_socket, G_IO_IN, timer, error);

  g_cancellable_disconnect (cancellable, cancel_cb_id);
  g_object_unref (timer);

  if ((condition & G_IO_IN) == 0)
    {
      if (g_cancellable_is_cancelled (timer) &&
          !g_cancellable_is_cancelled (cancellable))
        {
          g_clear_error (error);
          g_set_error_literal (error, 
                               G_IO_ERROR, G_IO_ERROR_HOST_NOT_FOUND,
                               _("Failed to create active FTP connection. "
                                 "Maybe your router does not support this?"));
        }
      else if (error && *error == NULL)
        {
          g_set_error_literal (error, 
                               G_IO_ERROR, G_IO_ERROR_HOST_NOT_FOUND,
                               _("Failed to create active FTP connection."));
        }
      return FALSE;
    }

  accepted = g_socket_accept (conn->listen_socket, cancellable, error);
  if (accepted == NULL)
    return FALSE;

  conn->data = G_IO_STREAM (g_socket_connection_factory_create_connection (accepted));
  g_object_unref (accepted);
  enable_nodelay (G_SOCKET_CONNECTION (conn->data));
  return TRUE;
}
Example #3
0
static void
set_up_mock_xdg_runtime_dir (void)
{
  GError *error = NULL;
  GSocketAddress *addr;

  mock_bus = g_socket_new (G_SOCKET_FAMILY_UNIX, G_SOCKET_TYPE_STREAM, 0,
      &error);
  g_assert_no_error (error);
  g_assert_true (G_IS_SOCKET (mock_bus));

  /* alters tmpdir in-place */
  if (g_mkdtemp_full (tmpdir, 0700) == NULL)
    {
      int errsv = errno;
      g_error ("g_mkdtemp_full: %s", g_strerror (errsv));
    }

  mock_bus_path = g_strconcat (tmpdir, "/bus", NULL);
  addr = g_unix_socket_address_new (mock_bus_path);
  g_socket_bind (mock_bus, addr, FALSE, &error);
  g_assert_no_error (error);
  g_object_unref (addr);

  g_setenv ("XDG_RUNTIME_DIR", tmpdir, TRUE);
}
Example #4
0
NiceSocket *
nice_tcp_bsd_socket_new_from_gsock (GMainContext *ctx, GSocket *gsock,
    NiceAddress *local_addr, NiceAddress *remote_addr, gboolean reliable)
{
  NiceSocket *sock;
  TcpPriv *priv;

  g_return_val_if_fail (G_IS_SOCKET (gsock), NULL);

  sock = g_slice_new0 (NiceSocket);
  sock->priv = priv = g_slice_new0 (TcpPriv);

  if (ctx == NULL)
    ctx = g_main_context_default ();
  priv->context = g_main_context_ref (ctx);
  priv->remote_addr = *remote_addr;
  priv->error = FALSE;
  priv->reliable = reliable;
  priv->writable_cb = NULL;
  priv->writable_data = NULL;

  sock->type = NICE_SOCKET_TYPE_TCP_BSD;
  sock->fileno = g_object_ref (gsock);
  sock->addr = *local_addr;
  sock->send_messages = socket_send_messages;
  sock->send_messages_reliable = socket_send_messages_reliable;
  sock->recv_messages = socket_recv_messages;
  sock->is_reliable = socket_is_reliable;
  sock->can_send = socket_can_send;
  sock->set_writable_callback = socket_set_writable_callback;
  sock->close = socket_close;

  return sock;
}
Example #5
0
static void
event_cb (GSocketListener      *listener,
          GSocketListenerEvent  event,
          GSocket              *socket,
          gpointer              data)
{
  static GSocketListenerEvent expected_event = G_SOCKET_LISTENER_BINDING;
  gboolean *success = (gboolean *)data;

  g_assert (G_IS_SOCKET_LISTENER (listener));
  g_assert (G_IS_SOCKET (socket));
  g_assert (event == expected_event);

  switch (event)
    {
      case G_SOCKET_LISTENER_BINDING:
        expected_event = G_SOCKET_LISTENER_BOUND;
        break;
      case G_SOCKET_LISTENER_BOUND:
        expected_event = G_SOCKET_LISTENER_LISTENING;
        break;
      case G_SOCKET_LISTENER_LISTENING:
        expected_event = G_SOCKET_LISTENER_LISTENED;
        break;
      case G_SOCKET_LISTENER_LISTENED:
        *success = TRUE;
        break;
    }
}
Example #6
0
/**
 * @param rec The GstRecorder instance.
 * @param element
 * @param socket
 * @memberof GstRecorder
 *
 * Invoked when client socket added on the encoding out port.
 */
static void
gst_recorder_client_socket_added (GstElement * element,
    GSocket * socket, GstRecorder * rec)
{
  g_return_if_fail (G_IS_SOCKET (socket));

  INFO ("client-socket-added: %d", g_socket_get_fd (socket));
}
Example #7
0
/**
 * gst_switch_server_output_client_socket_added:
 *
 * Invoekd when a client socket is added.
 */
static void
gst_switch_server_output_client_socket_added (GstElement * element,
    GSocket * socket, GstSwitchServer * srv)
{
  g_return_if_fail (G_IS_SOCKET (socket));

  //INFO ("client-socket-added: %d", g_socket_get_fd (socket));
}
gssize
sctp_socket_receive (GSocket * socket, gchar * buffer, gsize size,
    GCancellable * cancellable, guint * streamid, GError ** error)
{
  g_return_val_if_fail (G_IS_SOCKET (socket) && buffer != NULL, -1);

  return sctp_socket_receive_with_blocking (socket, buffer, size,
      g_socket_get_blocking (socket), cancellable, streamid, error);
}
gssize
sctp_socket_send (GSocket * socket, guint streamid, guint32 timetolive,
    const gchar * buffer, gsize size, GCancellable * cancellable,
    GError ** error)
{
  g_return_val_if_fail (G_IS_SOCKET (socket) && buffer != NULL, -1);

  return sctp_socket_send_with_blocking (socket, streamid, timetolive, buffer,
      size, g_socket_get_blocking (socket), cancellable, error);
}
Example #10
0
/**
 * SECTION:facqnet
 * @short_description:Net related functions.
 * @title:FacqNet
 * @include:facqnet.h
 *
 * This module contains functions related with the network functions, for
 * example for sending and receiving data.
 *
 * Only two functions are provided for the moment facq_net_send() and
 * facq_net_receive(), check the description of each function for more details.
 *
 */
static gboolean check_values(GSocket *skt,gchar *buf,gsize size)
{
    if(!G_IS_SOCKET(skt))
        goto error;
    if(!buf)
        goto error;
    if(size >= G_MAXSSIZE)
        goto error;
    return TRUE;

error:
    facq_log_write_v(FACQ_LOG_MSG_TYPE_ERROR,
                     "Invalid parameters in send or receive function");
    return FALSE;
}
Example #11
0
/*
 * g_socket_condition_timed_wait:
 *
 * Provides g_socket_condition_timed_wait function for older 
 * glib versions. It's a simplified version of the glib one
 * that should work with all glib version from glib-2.22.
 */
gboolean g_socket_condition_timed_wait(GSocket *socket,GIOCondition condition,gint64 timeout,GCancellable *cancellable,GError **error)
{
	gint64 start_time;
	GPollFD poll_fd[2];
	gint result;
	gint num;

	g_return_val_if_fail(G_IS_SOCKET(socket),FALSE);

	if(g_cancellable_set_error_if_cancelled(cancellable,error))
		return FALSE;

	if(timeout != -1)
		timeout /= 1000;

	start_time = g_get_monotonic_time();

	poll_fd[0].fd = g_socket_get_fd(socket);
	poll_fd[0].events = condition;
	num = 1;

	if(g_cancellable_make_pollfd(cancellable,&poll_fd[1]))
		num++;

	while(TRUE){
		result = g_poll(poll_fd,num,timeout);
		if(result != -1 || errno != EINTR)
			break;

		if(timeout != -1){
			timeout -= (g_get_monotonic_time () - start_time) * 1000;
			if(timeout < 0)
				timeout = 0;
		}
	}
    
	if(num > 1)
		g_cancellable_release_fd(cancellable);

	if(result == 0){
		g_set_error_literal(error,G_IO_ERROR,G_IO_ERROR_TIMED_OUT,
								"Socket I/O timed out");
		return FALSE;
	}

	return !g_cancellable_set_error_if_cancelled(cancellable,error);
}
Example #12
0
/**
 * gst_net_time_packet_receive:
 * @socket: socket to receive the time packet on
 * @src_address: (out): address of variable to return sender address
 * @error: return address for a #GError, or NULL
 *
 * Receives a #GstNetTimePacket over a socket. Handles interrupted system
 * calls, but otherwise returns NULL on error.
 *
 * Returns: (transfer full): a new #GstNetTimePacket, or NULL on error. Free
 *    with gst_net_time_packet_free() when done.
 */
GstNetTimePacket *
gst_net_time_packet_receive (GSocket * socket,
    GSocketAddress ** src_address, GError ** error)
{
  gchar buffer[GST_NET_TIME_PACKET_SIZE];
  GError *err = NULL;
  gssize ret;

  g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  while (TRUE) {
    ret = g_socket_receive_from (socket, src_address, buffer,
        GST_NET_TIME_PACKET_SIZE, NULL, &err);

    if (ret < 0) {
      if (err->code == G_IO_ERROR_WOULD_BLOCK) {
        g_error_free (err);
        err = NULL;
        continue;
      } else {
        goto receive_error;
      }
    } else if (ret < GST_NET_TIME_PACKET_SIZE) {
      goto short_packet;
    } else {
      return gst_net_time_packet_new ((const guint8 *) buffer);
    }
  }

receive_error:
  {
    GST_DEBUG ("receive error: %s", err->message);
    g_propagate_error (error, err);
    return NULL;
  }
short_packet:
  {
    GST_DEBUG ("someone sent us a short packet (%" G_GSSIZE_FORMAT " < %d)",
        ret, GST_NET_TIME_PACKET_SIZE);
    g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA,
        "short time packet (%d < %d)", (int) ret, GST_NET_TIME_PACKET_SIZE);
    return NULL;
  }
}