예제 #1
0
static void
gst_multiudpsink_setup_qos_dscp (GstMultiUDPSink * sink, GSocket * socket)
{
  /* don't touch on -1 */
  if (sink->qos_dscp < 0)
    return;

  if (socket == NULL)
    return;

#ifdef IP_TOS
  {
    gint tos;
    gint fd;

    fd = g_socket_get_fd (socket);

    GST_DEBUG_OBJECT (sink, "setting TOS to %d", sink->qos_dscp);

    /* Extract and shift 6 bits of DSFIELD */
    tos = (sink->qos_dscp & 0x3f) << 2;

    if (setsockopt (fd, IPPROTO_IP, IP_TOS, &tos, sizeof (tos)) < 0) {
      GST_ERROR_OBJECT (sink, "could not set TOS: %s", g_strerror (errno));
    }
#ifdef IPV6_TCLASS
    if (setsockopt (fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof (tos)) < 0) {
      GST_ERROR_OBJECT (sink, "could not set TCLASS: %s", g_strerror (errno));
    }
#endif
  }
#endif
}
예제 #2
0
static void
_update_socket (ArvGvStreamThreadData *thread_data, ArvBuffer *buffer)
{
	int buffer_size, fd;

	if (thread_data->socket_buffer_option == ARV_GV_STREAM_SOCKET_BUFFER_FIXED &&
	    thread_data->socket_buffer_size <= 0)
		return;

	fd = g_socket_get_fd (thread_data->socket);

	switch (thread_data->socket_buffer_option) {
		case ARV_GV_STREAM_SOCKET_BUFFER_FIXED:
			buffer_size = thread_data->socket_buffer_size;
			break;
		case ARV_GV_STREAM_SOCKET_BUFFER_AUTO:
			if (thread_data->socket_buffer_size <= 0)
				buffer_size = buffer->size;
			else
				buffer_size = MIN (buffer->size, thread_data->socket_buffer_size);
			break;
	}

	if (buffer_size != thread_data->current_socket_buffer_size) {
		setsockopt (fd, SOL_SOCKET, SO_RCVBUF, &buffer_size, sizeof (buffer_size));
		thread_data->current_socket_buffer_size = buffer_size;
		arv_debug_stream_thread ("[GvStream::update_socket] Socket buffer size set to %d", buffer_size);
	}
}
예제 #3
0
/**
 * soup_socket_get_fd:
 * @sock: a #SoupSocket
 *
 * Gets @sock's underlying file descriptor.
 *
 * Note that fiddling with the file descriptor may break the
 * #SoupSocket.
 *
 * Return value: @sock's file descriptor.
 */
int
soup_socket_get_fd (SoupSocket *sock)
{
	g_return_val_if_fail (SOUP_IS_SOCKET (sock), -1);

	return g_socket_get_fd (SOUP_SOCKET_GET_PRIVATE (sock)->gsock);
}
예제 #4
0
파일: client.c 프로젝트: shuyufu/zero-copy
static void on_connected (GObject      *source_object,
                          GAsyncResult *res,
                          gpointer      user_data)
{
  GSocketClient     *client      = NULL;
  GSocketConnection *connection  = NULL;
  GSocket           *socket      = NULL;
  gint               fd          = 0;
  GIOChannel        *channel     = NULL;
  GError            *local_error = NULL;

  client = G_SOCKET_CLIENT (source_object);

  connection = g_socket_client_connect_to_host_finish (client, res, &local_error);
  if (local_error)
  {
    g_warning("connect failed: %s", local_error->message);
    g_clear_error (&local_error);
    return;
  }

  socket  = g_socket_connection_get_socket (connection);
  fd      = g_socket_get_fd (socket);

  channel = g_io_channel_unix_new (fd);
  g_io_channel_set_encoding (channel, NULL, NULL);
  g_io_add_watch (channel, G_IO_IN, on_read, g_object_ref (connection));
}
예제 #5
0
gboolean
xdmcp_client_start (XDMCPClient *client)
{
    if (client->priv->socket)
        return TRUE;

    g_autoptr(GError) error = NULL;
    client->priv->socket = g_socket_new (G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_DATAGRAM, G_SOCKET_PROTOCOL_UDP, &error);
    if (error)
        g_warning ("Error creating XDMCP socket: %s", error->message);
    if (!client->priv->socket)
        return FALSE;

    GSocketConnectable *address = g_network_address_new (client->priv->host, client->priv->port);
    GSocketAddressEnumerator *enumerator = g_socket_connectable_enumerate (address);
    while (TRUE)
    {
        g_autoptr(GError) e = NULL;
        g_autoptr(GSocketAddress) socket_address = g_socket_address_enumerator_next (enumerator, NULL, &e);
        if (e)
            g_warning ("Failed to get socket address: %s", e->message);
        if (!socket_address)
            return FALSE;

        if (!g_socket_connect (client->priv->socket, socket_address, NULL, &e))
        {
            g_warning ("Unable to connect XDMCP socket: %s", error->message);
            continue;
        }

        g_io_add_watch (g_io_channel_unix_new (g_socket_get_fd (client->priv->socket)), G_IO_IN, xdmcp_data_cb, client);

        return TRUE;
    }
}
예제 #6
0
파일: http.c 프로젝트: Amelos/moloch
gboolean moloch_http_write_cb(gint UNUSED(fd), GIOCondition UNUSED(cond), gpointer data) {
    MolochConn_t        *conn = data;
    GError              *gerror = 0;

    if (!conn->request) {
        DEBUGCONN("AAA ww clear %s %p fd:%d,%d ww:%d", conn->server->names[0], conn, fd, g_socket_get_fd(conn->conn), conn->writeWatch);
        conn->writeWatch = 0;
        return FALSE;
    }

    int sent = g_socket_send(conn->conn, conn->request->data+conn->sent, conn->request->data_len-conn->sent, NULL, &gerror);
    conn->sent += sent;

    if (gerror) {
        /* Should free stuff here */
        LOG("ERROR: %p: Write Error: %s", (void*)conn, gerror->message);
        g_error_free(gerror);
        DEBUGCONN("AAA ww clear %s %p fd:%d,%d ww:%d", conn->server->names[0], conn, fd, g_socket_get_fd(conn->conn), conn->writeWatch);
        conn->writeWatch = 0;
        return FALSE;
    }

    gboolean finished = conn->sent == conn->request->data_len;
    if (finished) {
        moloch_http_finish(conn, FALSE);
        DEBUGCONN("AAA ww finish %s %p fd:%d ww:%d", conn->server->names[0], conn, g_socket_get_fd(conn->conn), conn->writeWatch);
        conn->writeWatch = 0;
    }

    return !finished;
}
예제 #7
0
static pid_t
_gumd_dbus_server_p2p_get_remote_pid (
        GumdDbusServer *self,
        GDBusMethodInvocation *invocation)
{
    pid_t remote_pid = 0;
    GDBusConnection *connection = NULL;
    gint peer_fd = -1;
    struct ucred peer_cred;
    socklen_t cred_size = sizeof(peer_cred);

    g_return_val_if_fail (invocation && GUMD_IS_DBUS_SERVER_P2P (self),
            remote_pid);

    connection = g_dbus_method_invocation_get_connection (invocation);
    peer_fd = g_socket_get_fd (g_socket_connection_get_socket (
            G_SOCKET_CONNECTION (g_dbus_connection_get_stream(connection))));
    if (peer_fd < 0 || getsockopt (peer_fd, SOL_SOCKET, SO_PEERCRED,
            &peer_cred, &cred_size) != 0) {
        WARN ("getsockopt() for SO_PEERCRED failed");
        return remote_pid;
    }
    DBG ("Remote p2p peer pid=%d uid=%d gid=%d", peer_cred.pid, peer_cred.uid,
            peer_cred.gid);

    return peer_cred.pid;
}
static int
g_socket_output_stream_get_fd (GFileDescriptorBased *fd_based)
{
    GSocketOutputStream *output_stream = G_SOCKET_OUTPUT_STREAM (fd_based);

    return g_socket_get_fd (output_stream->priv->socket);
}
예제 #9
0
static void
send_netlink_request (GUPnPLinuxContextManager *self,
                      guint netlink_message,
                      guint flags)
{
        struct nl_req_s req;
        struct sockaddr_nl dest;
        struct msghdr msg;
        struct iovec io;
        int fd;

        memset (&req, 0, sizeof (req));
        memset (&dest, 0, sizeof (dest));
        memset (&msg, 0, sizeof (msg));

        dest.nl_family = AF_NETLINK;
        req.hdr.nlmsg_len = NLMSG_LENGTH (sizeof (struct rtgenmsg));
        req.hdr.nlmsg_seq = self->priv->nl_seq++;
        req.hdr.nlmsg_type = netlink_message;
        req.hdr.nlmsg_flags = NLM_F_REQUEST | flags;
        req.gen.rtgen_family = AF_INET;

        io.iov_base = &req;
        io.iov_len = req.hdr.nlmsg_len;

        msg.msg_iov = &io;
        msg.msg_iovlen = 1;
        msg.msg_name = &dest;
        msg.msg_namelen = sizeof (dest);

        fd = g_socket_get_fd (self->priv->netlink_socket);
        if (sendmsg (fd, (struct msghdr *) &msg, 0) < 0)
                g_warning ("Could not send netlink message: %s",
                           strerror (errno));
}
예제 #10
0
ArvDevice *
arv_gv_device_new (GInetAddress *interface_address, GInetAddress *device_address)
{
	ArvGvDevice *gv_device;
	ArvGvDeviceIOData *io_data;
	ArvGvDeviceHeartbeatData *heartbeat_data;
	char *address_string;

	g_return_val_if_fail (G_IS_INET_ADDRESS (interface_address), NULL);
	g_return_val_if_fail (G_IS_INET_ADDRESS (device_address), NULL);

	address_string = g_inet_address_to_string (interface_address);
	arv_debug_device ("[GvDevice::new] Interface address = %s", address_string);
	g_free (address_string);
	address_string = g_inet_address_to_string (device_address);
	arv_debug_device ("[GvDevice::new] Device address = %s", address_string);
	g_free (address_string);

	gv_device = g_object_new (ARV_TYPE_GV_DEVICE, NULL);

	io_data = g_new0 (ArvGvDeviceIOData, 1);

	io_data->mutex = g_mutex_new ();
	io_data->packet_id = 65300; /* Start near the end of the circular counter */

	io_data->interface_address = g_inet_socket_address_new (interface_address, 0);
	io_data->device_address = g_inet_socket_address_new (device_address, ARV_GVCP_PORT);
	io_data->socket = g_socket_new (G_SOCKET_FAMILY_IPV4,
					G_SOCKET_TYPE_DATAGRAM,
					G_SOCKET_PROTOCOL_UDP, NULL);
	g_socket_bind (io_data->socket, io_data->interface_address, TRUE, NULL);

	io_data->buffer = g_malloc (ARV_GV_DEVICE_BUFFER_SIZE);
	io_data->gvcp_n_retries = ARV_GV_DEVICE_GVCP_N_RETRIES_DEFAULT;
	io_data->gvcp_timeout_ms = ARV_GV_DEVICE_GVCP_TIMEOUT_MS_DEFAULT;
	io_data->poll_in_event.fd = g_socket_get_fd (io_data->socket);
	io_data->poll_in_event.events =  G_IO_IN;
	io_data->poll_in_event.revents = 0;

	gv_device->priv->io_data = io_data;

	arv_gv_device_load_genicam (gv_device);

	arv_gv_device_take_control (gv_device);

	heartbeat_data = g_new (ArvGvDeviceHeartbeatData, 1);
	heartbeat_data->gv_device = gv_device;
	heartbeat_data->io_data = io_data;
	heartbeat_data->period_us = ARV_GV_DEVICE_HEARTBEAT_PERIOD_US;
	heartbeat_data->cancel = FALSE;

	gv_device->priv->heartbeat_data = heartbeat_data;

	gv_device->priv->heartbeat_thread = g_thread_create (arv_gv_device_heartbeat_thread,
							     gv_device->priv->heartbeat_data,
							     TRUE, NULL);

	return ARV_DEVICE (gv_device);
}
예제 #11
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));
}
예제 #12
0
gboolean
kms_sctp_connection_set_event_subscribe (KmsSCTPConnection * conn,
    KmsSCTPEventFlags events, GError ** err)
{
  g_return_val_if_fail (conn != NULL, FALSE);

#if defined (SCTP_EVENTS)
  {
    struct sctp_event_subscribe sctp_events;

    memset (&sctp_events, 0, sizeof (sctp_events));
    if (events & KMS_SCTP_DATA_IO_EVENT)
      sctp_events.sctp_data_io_event = 1;

    if (events & KMS_SCTP_ASSOCIATION_EVENT)
      sctp_events.sctp_association_event = 1;

    if (events & KMS_SCTP_ADDRESS_EVENT)
      sctp_events.sctp_address_event = 1;

    if (events & KMS_SCTP_SEND_FAILURE_EVENT)
      sctp_events.sctp_send_failure_event = 1;

    if (events & KMS_SCTP_PEER_ERROR_EVENT)
      sctp_events.sctp_peer_error_event = 1;

    if (events & KMS_SCTP_SHUTDOWN_EVENT)
      sctp_events.sctp_shutdown_event = 1;

    if (events & KMS_SCTP_PARTIAL_DELIVERY_EVENT)
      sctp_events.sctp_partial_delivery_event = 1;

    if (events & KMS_SCTP_ADAPTATION_LAYER_EVENT)
      sctp_events.sctp_adaptation_layer_event = 1;

    if (events & KMS_SCTP_AUTHENTICATION_EVENT)
      sctp_events.sctp_authentication_event = 1;

    if (setsockopt (g_socket_get_fd (conn->socket), IPPROTO_SCTP,
            SCTP_EVENTS, &sctp_events, sizeof (sctp_events)) < 0) {
      GST_ERROR ("Could not configure SCTP socket: %s (%d)",
          g_strerror (errno), errno);
      return FALSE;
    }

    return TRUE;
  }
#else
  {
    GST_WARNING ("don't know how to configure SCTP events " "on this OS.");

    g_set_error (err, KMS_SCTP_CONNECTION_ERROR, KMS_CONNECTION_CONFIG_ERROR,
        "Can not configure SCTP socket");

    return FALSE;
  }
#endif
}
예제 #13
0
int
xg_socket_get_fd (GSocket *socket)
{
    int fd;
    fd = g_socket_get_fd(socket);
    if (fd == -1)
        g_critical("g_socket_get_fd() returned -1");
    return fd;
}
예제 #14
0
static ArvGvDiscoverSocketList *
arv_gv_discover_socket_list_new (void)
{
	ArvGvDiscoverSocketList *socket_list;
	GSList *iter;
	struct ifaddrs *ifap  = NULL;
	struct ifaddrs *ifap_iter;
	int i;

	socket_list = g_new0 (ArvGvDiscoverSocketList, 1);

	if (getifaddrs (&ifap) < 0)
		return socket_list;

	for (ifap_iter = ifap; ifap_iter != NULL; ifap_iter = ifap_iter->ifa_next) {
		if ((ifap_iter->ifa_flags & IFF_UP) != 0 &&
		    (ifap_iter->ifa_flags & IFF_POINTOPOINT) == 0 &&
		    (ifap_iter->ifa_addr != NULL) &&
		    (ifap_iter->ifa_addr->sa_family == AF_INET)) {
			ArvGvDiscoverSocket *discover_socket = g_new0 (ArvGvDiscoverSocket, 1);
			GSocketAddress *socket_address;
			GInetAddress *inet_address;
			char *inet_address_string;
			GError *error = NULL;

			socket_address = g_socket_address_new_from_native (ifap_iter->ifa_addr,
									   sizeof (struct sockaddr));
			inet_address = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (socket_address));
			inet_address_string = g_inet_address_to_string (inet_address);
			arv_debug_interface ("[GvDiscoverSocket::new] Add interface %s", inet_address_string);
			g_free (inet_address_string);
			discover_socket->interface_address = g_inet_socket_address_new (inet_address, 0);
			g_object_unref (socket_address);

			discover_socket->socket = g_socket_new (G_SOCKET_FAMILY_IPV4,
								G_SOCKET_TYPE_DATAGRAM,
								G_SOCKET_PROTOCOL_UDP, NULL);
			g_socket_bind (discover_socket->socket, discover_socket->interface_address, TRUE, &error);

			socket_list->sockets = g_slist_prepend (socket_list->sockets, discover_socket);
			socket_list->n_sockets++;
		}
	}

	freeifaddrs (ifap);

	socket_list->poll_fds = g_new (GPollFD, socket_list->n_sockets);
	for (i = 0, iter = socket_list->sockets; iter != NULL; i++, iter = iter->next) {
		ArvGvDiscoverSocket *discover_socket = iter->data;

		socket_list->poll_fds[i].fd = g_socket_get_fd (discover_socket->socket);
		socket_list->poll_fds[i].events =  G_IO_IN;
		socket_list->poll_fds[i].revents = 0;
	}

	return socket_list;
}
예제 #15
0
GSocket *multicast_createSocket(gchar *interface, gchar *groupaddress, guint port,
                                 GSocketAddress **sa)
{
    GError *err = NULL;
    GInetAddress *addr =
        g_inet_address_new_from_string(groupaddress);
    *sa = g_inet_socket_address_new(addr,port);

    GSocket *socket = g_socket_new(G_SOCKET_FAMILY_IPV6,
                        G_SOCKET_TYPE_DATAGRAM,
                        G_SOCKET_PROTOCOL_UDP,
                        NULL);
    ub_assert(socket != NULL);
    
    int on = 1;
    setsockopt(g_socket_get_fd(socket), SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));

    if( g_socket_bind(socket, *sa, TRUE, &err) == FALSE ){
        syslog(LOG_ERR, "net_createSockets: Error while binding udp socket: %s\n", err->message);
        g_error_free(err);
        g_object_unref(*sa);
        g_object_unref(socket);
        return NULL;
    }

    struct addrinfo *resmulti;
    struct ipv6_mreq mreq;
    mreq.ipv6mr_interface = if_nametoindex(interface);
    gchar *tmp = g_inet_address_to_string(addr);
    syslog(LOG_DEBUG,"using address: %s\n",tmp);
    int ret = getaddrinfo(tmp, NULL, NULL, &resmulti);
    g_free(tmp);
    if( ret ){
        syslog(LOG_ERR,"net_multicast.c: %s",  gai_strerror(ret));
        g_object_unref(*sa);
        g_object_unref(socket);
        return NULL;
    }
    mreq.ipv6mr_multiaddr = ((struct sockaddr_in6 *)resmulti->ai_addr)->sin6_addr;
    setsockopt(g_socket_get_fd(socket), IPPROTO_IPV6,
        IPV6_ADD_MEMBERSHIP, (char *)&mreq, sizeof(mreq));
    return socket;
}
예제 #16
0
KmsSCTPResult
kms_sctp_connection_accept (KmsSCTPConnection * conn,
    GCancellable * cancellable, KmsSCTPConnection ** client, GError ** err)
{
  KmsSCTPConnection *ccon;
  GSocket *socket;

  g_return_val_if_fail (conn != NULL, KMS_SCTP_ERROR);
  g_return_val_if_fail (conn->socket != NULL, KMS_SCTP_ERROR);

  socket = g_socket_accept (conn->socket, cancellable, err);
  if (socket == NULL)
    return KMS_SCTP_ERROR;

  ccon = g_slice_new0 (KmsSCTPConnection);

  gst_mini_object_init (GST_MINI_OBJECT_CAST (ccon), 0,
      _kms_sctp_connection_type, NULL, NULL,
      (GstMiniObjectFreeFunction) _kms_sctp_connection_free);

  ccon->socket = socket;

  if (!kms_sctp_connection_set_event_subscribe (ccon, KMS_SCTP_DATA_IO_EVENT,
          err)) {
    kms_sctp_connection_unref (ccon);
    return KMS_SCTP_ERROR;
  }

  if (G_UNLIKELY (GST_LEVEL_DEBUG <= _gst_debug_min))
#if defined (SCTP_INITMSG)
  {
    struct sctp_initmsg initmsg;
    socklen_t optlen;

    if (getsockopt (g_socket_get_fd (socket), IPPROTO_SCTP,
            SCTP_INITMSG, &initmsg, &optlen) < 0) {
      GST_WARNING ("Could not get SCTP configuration: %s (%d)",
          g_strerror (errno), errno);
    } else {
      GST_DEBUG ("SCTP client socket: ostreams %u, instreams %u",
          initmsg.sinit_num_ostreams, initmsg.sinit_num_ostreams);
    }
  }
#else
  {
    GST_WARNING ("don't know how to get the configuration of the "
        "SCTP initiation structure on this OS.");
  }
#endif

  *client = ccon;

  return KMS_SCTP_OK;
}
예제 #17
0
static gboolean
arv_gv_discover_socket_set_broadcast (ArvGvDiscoverSocket *discover_socket, gboolean enable)
{
	int socket_fd;
	int result;

	socket_fd = g_socket_get_fd (discover_socket->socket);

	result = setsockopt (socket_fd, SOL_SOCKET, SO_BROADCAST, (char*)&enable, sizeof (enable));

	return result == 0;
}
예제 #18
0
static gboolean
arv_gv_interface_socket_set_broadcast (GSocket *socket, gboolean enable)
{
	int socket_fd;
	int result;

	socket_fd = g_socket_get_fd (socket);

	result = setsockopt (socket_fd, SOL_SOCKET, SO_BROADCAST, (char*)&enable, sizeof (enable));

	return result == 0;
}
예제 #19
0
static void
connect_cb(GObject *source,
           GAsyncResult *res,
           gpointer user_data)
{
    GSocketConnection *socket_conn;
    PnNode *conn;

    conn = PN_NODE(user_data);
    socket_conn = g_socket_client_connect_to_host_finish(G_SOCKET_CLIENT(source), res, NULL);

    g_object_unref(source);

    g_object_ref(conn);

    if (socket_conn) {
        GIOChannel *channel;
        GSocket *socket;

        conn->socket_conn = socket_conn;
        socket = g_socket_connection_get_socket(socket_conn);
        conn->stream = pn_stream_new(g_socket_get_fd(socket));
        channel = conn->stream->channel;

        PN_NODE_GET_CLASS (conn)->channel_setup (conn, channel);

        conn->status = PN_NODE_STATUS_OPEN;

        pn_info("connected: conn=%p,channel=%p", conn, channel);
        conn->read_watch = g_io_add_watch(channel, G_IO_IN, read_cb, conn);
#if 0
        g_io_add_watch (channel, G_IO_ERR | G_IO_HUP | G_IO_NVAL, close_cb, conn);
#endif
    }
    else {
        conn->error = g_error_new_literal(PN_NODE_ERROR, PN_NODE_ERROR_OPEN,
                                          "Unable to connect");

        pn_node_error(conn);
    }

    {
        PnNodeClass *class;
        class = g_type_class_peek(PN_NODE_TYPE);
        g_signal_emit(G_OBJECT(conn), class->open_sig, 0, conn);
    }

    g_object_unref(conn);
}
예제 #20
0
static void
socket_source_detach (SocketSource *source)
{
  nice_debug ("Detaching source %p (socket %p, FD %d) from context %p",
      source->source, source->socket,
      (source->socket->fileno != NULL) ?
          g_socket_get_fd (source->socket->fileno) : 0,
      (source->source != NULL) ? g_source_get_context (source->source) : 0);

  if (source->source != NULL) {
    g_source_destroy (source->source);
    g_source_unref (source->source);
  }
  source->source = NULL;
}
예제 #21
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);
}
예제 #22
0
static gssize
sctp_socket_receive_with_blocking (GSocket * socket, gchar * buffer, gsize size,
    gboolean blocking, GCancellable * cancellable, guint * streamid,
    GError ** error)
{
  gssize ret;

  if (g_socket_is_closed (socket)) {
    g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
        "Socket is already closed");
    return -1;
  }

  if (g_cancellable_set_error_if_cancelled (cancellable, error))
    return -1;

  while (TRUE) {
    struct sctp_sndrcvinfo sndrcvinfo;
    int flags = 0;

    if (blocking &&
        !g_socket_condition_wait (socket, G_IO_IN, cancellable, error))
      return -1;

    if ((ret = sctp_recvmsg (g_socket_get_fd (socket), buffer, size, NULL, 0,
                &sndrcvinfo, &flags)) < 0) {
      if (errno == EINTR)
        continue;

      if (blocking) {
        if (errno == EWOULDBLOCK || errno == EAGAIN)
          continue;
      }

      g_set_error (error, G_IO_ERROR, errno, "Error receiving data: %s",
          strerror (errno));

      return -1;
    }

    *streamid = sndrcvinfo.sinfo_stream;
    break;
  }

  return ret;
}
예제 #23
0
파일: main.c 프로젝트: hannenz/busy
/* Called onincoming TCP connection
 */
static void on_incoming(GSocketService *service, gpointer udata){
	gint fd;
	GIOChannel *channel;
	GSocketConnection *connection;
	AppData *app_data;

	app_data = udata;

	connection = udata;

/*	syslog(LOG_NOTICE, "Incoming Connection\n");*/
	g_object_ref(connection);

	GSocket *socket = g_socket_connection_get_socket(connection);
	fd = g_socket_get_fd(socket);
	channel = g_io_channel_unix_new(fd);
	g_io_add_watch(channel, G_IO_IN, (GIOFunc)network_read, connection);
}
예제 #24
0
static gboolean
BP_TCPService_IncomingConnection (GSocketService *service,
              GSocketConnection *incomingConnection,
              GObject *source_object,
              gpointer user_data)
{
  BPTCPService *filter = BP_TCPSERVICE (user_data);

  GSocket *socket = g_socket_connection_get_socket(incomingConnection);
  GSocketAddress *sockaddr = g_socket_connection_get_remote_address(incomingConnection, NULL);
  GInetAddress *addr = g_inet_socket_address_get_address(G_INET_SOCKET_ADDRESS(sockaddr));
  gchar* remoteHost = g_inet_address_to_string(addr);
  gint fd = g_socket_get_fd(socket);
  GIOChannel *channel = g_io_channel_unix_new(fd);

  if (filter->mode == BP_MODE_SINK_ONLY) {
    GST_ERROR ("Attempt to accept connection but in sink-only mode");
    return FALSE;
  }

  // Ref connection
  g_object_ref (incomingConnection);

  // Get remote host & port
  filter->remotePort = g_inet_socket_address_get_port(G_INET_SOCKET_ADDRESS(sockaddr));
  g_string_assign(filter->remoteAddress, remoteHost);
  g_free (remoteHost);

  g_message ("New connection from %s:%d", filter->remoteAddress->str, (int) filter->remotePort);
  g_message ("Attaching IO channel watch ...");

  // Add IO watch with pointer to connection handle as user data for callback
  g_io_add_watch(channel, G_IO_IN, (GIOFunc) BP_TCPService_IncomingConnectionReceive, filter);
  g_io_channel_unref (channel);

  g_message ("Registering connection");

  BP_TCPService_UpdateUID (filter);

  ServerPool_AddInstance (filter->uid, filter->service, incomingConnection);

	return FALSE;
}
예제 #25
0
/* Must *not* take the agent lock, since it’s called from within
 * component_set_io_context(), which holds the Component’s I/O lock. */
static void
socket_source_attach (SocketSource *socket_source, GMainContext *context)
{
  GSource *source;

  /* Create a source. */
  source = g_socket_create_source (socket_source->socket->fileno,
      G_IO_IN, NULL);
  g_source_set_callback (source, (GSourceFunc) component_io_cb,
      socket_source, NULL);

  /* Add the source. */
  nice_debug ("Attaching source %p (socket %p, FD %d) to context %p", source,
      socket_source->socket, g_socket_get_fd (socket_source->socket->fileno),
      context);

  g_assert (socket_source->source == NULL);
  socket_source->source = source;
  g_source_attach (source, context);
}
static void _connect_to_host_ready(GObject *source_object, GAsyncResult *res, gpointer user_data) {
	GSocketClient *socket_client = G_SOCKET_CLIENT(source_object);
	GSimpleAsyncResult *result = G_SIMPLE_ASYNC_RESULT(user_data);
	IdleServerConnection *conn = IDLE_SERVER_CONNECTION(g_async_result_get_source_object(G_ASYNC_RESULT(result)));
	IdleServerConnectionPrivate *priv = IDLE_SERVER_CONNECTION_GET_PRIVATE(conn);
	GInputStream *input_stream;
	GSocket *socket_;
	GSocketConnection *socket_connection;
	gint nodelay = 1;
	gint socket_fd;
	GError *error = NULL;

	socket_connection = g_socket_client_connect_to_host_finish(socket_client, res, &error);
	if (socket_connection == NULL) {
		IDLE_DEBUG("g_socket_client_connect_to_host failed: %s", error->message);
		g_simple_async_result_set_error(result, TP_ERROR, TP_ERROR_NETWORK_ERROR, "%s", error->message);
		g_error_free(error);
		change_state(conn, SERVER_CONNECTION_STATE_NOT_CONNECTED, SERVER_CONNECTION_STATE_REASON_ERROR);
		g_object_unref(conn);
		goto cleanup;
	}

	socket_ = g_socket_connection_get_socket(socket_connection);
	g_socket_set_keepalive(socket_, TRUE);

	socket_fd = g_socket_get_fd(socket_);
	setsockopt(socket_fd, IPPROTO_TCP, TCP_NODELAY, &nodelay, sizeof(nodelay));

	g_tcp_connection_set_graceful_disconnect(G_TCP_CONNECTION(socket_connection), TRUE);

	priv->io_stream = G_IO_STREAM(socket_connection);

	input_stream = g_io_stream_get_input_stream(priv->io_stream);
	_input_stream_read(conn, input_stream, _input_stream_read_ready);
	change_state(conn, SERVER_CONNECTION_STATE_CONNECTED, SERVER_CONNECTION_STATE_REASON_REQUESTED);

cleanup:
	g_simple_async_result_complete(result);
	g_object_unref(result);
}
예제 #27
0
static gssize
sctp_socket_send_with_blocking (GSocket * socket, guint streamid,
    guint32 timetolive, const gchar * buffer, gsize size, gboolean blocking,
    GCancellable * cancellable, GError ** error)
{
  gssize ret;

  if (g_socket_is_closed (socket)) {
    g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
        "Socket is already closed");
    return -1;
  }

  if (g_cancellable_set_error_if_cancelled (cancellable, error))
    return -1;

  while (TRUE) {
    if (blocking &&
        !g_socket_condition_wait (socket, G_IO_OUT, cancellable, error))
      return -1;

    if ((ret = sctp_sendmsg (g_socket_get_fd (socket), buffer, size, NULL, 0,
                0, 0, streamid, timetolive, 0)) < 0) {
      if (errno == EINTR)
        continue;

      if (blocking) {
        if (errno == EWOULDBLOCK || errno == EAGAIN)
          continue;
      }

      g_set_error (error, G_IO_ERROR, errno, "Error sending data: %s",
          strerror (errno));
      return -1;
    }
    break;
  }

  return ret;
}
예제 #28
0
gboolean
kms_sctp_connection_set_init_config (KmsSCTPConnection * conn,
    guint16 num_ostreams, guint16 max_instreams, guint16 max_attempts,
    guint16 max_init_timeo, GError ** err)
{
  g_return_val_if_fail (conn != NULL, FALSE);

#if defined (SCTP_INITMSG)
  {
    struct sctp_initmsg initmsg;

    memset (&initmsg, 0, sizeof (initmsg));
    initmsg.sinit_num_ostreams = num_ostreams;
    initmsg.sinit_max_instreams = max_instreams;
    initmsg.sinit_max_attempts = max_attempts;
    initmsg.sinit_max_init_timeo = max_init_timeo;

    if (setsockopt (g_socket_get_fd (conn->socket), IPPROTO_SCTP,
            SCTP_INITMSG, &initmsg, sizeof (initmsg)) < 0) {
      GST_ERROR ("Could not configure SCTP socket: %s (%d)",
          g_strerror (errno), errno);
      return FALSE;
    }

    return TRUE;
  }
#else
  {
    GST_WARNING ("don't know how to configure the SCTP initiation "
        "parameters on this OS.");

    g_set_error (err, KMS_SCTP_CONNECTION_ERROR, KMS_CONNECTION_CONFIG_ERROR,
        "Can not configure SCTP socket");

    return FALSE;
  }
#endif
}
예제 #29
0
//New tcp conection
gboolean new_connection(GSocketService *service, GSocketConnection *connection, GObject *source_object, gpointer user_data) {

  //New client connected
  GSocketAddress *sockaddr = g_socket_connection_get_remote_address(connection, NULL);
  GInetAddress *addr = g_inet_socket_address_get_address(G_INET_SOCKET_ADDRESS(sockaddr));
  guint16 port = g_inet_socket_address_get_port(G_INET_SOCKET_ADDRESS(sockaddr));

  if (verbose) {
    printf("App Server: New Connection from %s:%d\n", g_inet_address_to_string(addr), port);
    fflush(stdout);
  }

  //Record client (if new)
  add_client(g_inet_address_to_string(addr), connection);

  g_object_ref (connection);
  GSocket *socket = g_socket_connection_get_socket(connection);

  gint fd = g_socket_get_fd(socket);
  GIOChannel *channel = g_io_channel_unix_new(fd);
  g_io_add_watch(channel, G_IO_IN, (GIOFunc) network_read, connection);
  return TRUE;
}
예제 #30
0
KmsSCTPResult
kms_sctp_connection_connect (KmsSCTPConnection * conn,
    GCancellable * cancellable, GError ** err)
{
  g_return_val_if_fail (conn != NULL, KMS_SCTP_ERROR);
  g_return_val_if_fail (conn->socket != NULL, KMS_SCTP_ERROR);
  g_return_val_if_fail (conn->saddr != NULL, KMS_SCTP_ERROR);

  if (g_socket_is_connected (conn->socket))
    return KMS_SCTP_OK;

  /* connect to server */
  if (!g_socket_connect (conn->socket, conn->saddr, cancellable, err))
    return KMS_SCTP_ERROR;

  if (G_UNLIKELY (GST_LEVEL_DEBUG <= _gst_debug_min)) {
#if defined (SCTP_INITMSG)
    struct sctp_initmsg initmsg;
    socklen_t optlen;

    if (getsockopt (g_socket_get_fd (conn->socket), IPPROTO_SCTP,
            SCTP_INITMSG, &initmsg, &optlen) < 0)
      GST_WARNING ("Could not get SCTP configuration: %s (%d)",
          g_strerror (errno), errno);
    else
      GST_DEBUG ("SCTP client socket: ostreams %u, instreams %u",
          initmsg.sinit_num_ostreams, initmsg.sinit_num_ostreams);
#else
    GST_WARNING ("don't know how to get the configuration of the "
        "SCTP initiation structure on this OS.");
#endif
  }

  GST_DEBUG ("connected sctp socket");

  return KMS_SCTP_OK;
}