Пример #1
0
static void
notify_status_cb(InfTcpConnection* connection,
                 const gchar* property,
		 gpointer user_data)
{
  InfTcpConnectionStatus status;
  InfIpAddress* addr;
  guint port;
  gchar* addr_str;

  g_object_get(
    G_OBJECT(connection),
    "status", &status,
    "remote-address", &addr,
    "remote-port", &port,
    NULL
  );

  addr_str = inf_ip_address_to_string(addr);
  inf_ip_address_free(addr);

  switch(status)
  {
  case INF_TCP_CONNECTION_CONNECTING:
    printf("Connecting to %s:%u\n", addr_str, port);
    break;
  case INF_TCP_CONNECTION_CONNECTED:
    printf("Connected to %s:%u\n", addr_str, port);

    g_object_get(
      G_OBJECT(connection),
      "local-address", &addr,
      "local-port", &port,
      NULL
    );

    g_free(addr_str);
    addr_str = inf_ip_address_to_string(addr);
    inf_ip_address_free(addr);

    printf("Connected from %s:%u\n", addr_str, port);
    inf_tcp_connection_send(connection, "Hello, World!\n", 14);
    break;
  case INF_TCP_CONNECTION_CLOSED:
    printf("Connection to %s:%u closed\n", addr_str, port);
    if(inf_standalone_io_loop_running(INF_STANDALONE_IO(user_data)))
      inf_standalone_io_loop_quit(INF_STANDALONE_IO(user_data));
    break;
  default:
    g_assert_not_reached();
    break;
  }

  g_free(addr_str);
}
Пример #2
0
static void
resolved_cb(InfNameResolver* resolver,
            const GError* error,
            gpointer user_data)
{
  gchar* hostname;
  gchar* service;
  gchar* srv;
  guint i;
  gchar* str;

  if(error != NULL)
  {
    fprintf(stderr, "Resolver error: %s\n", error->message);
  }
  else
  {
    g_object_get(
      G_OBJECT(resolver),
      "hostname", &hostname,
      "service", &service,
      "srv", &srv,
      NULL
    );

    printf(
      "Resolved hostname %s (SRV %s):\n",
      hostname,
      srv ? srv : "(nul)"
    );

    g_free(hostname);
    g_free(service);
    g_free(srv);

    for(i = 0; i < inf_name_resolver_get_n_addresses(resolver); ++i)
    {
      str = inf_ip_address_to_string(
        inf_name_resolver_get_address(resolver, i)
      );

      printf(
        "  %u: %s (port %u)\n",
        i,
        str,
        inf_name_resolver_get_port(resolver, i)
      );

      g_free(str);
    }
  }
}
Пример #3
0
static void
conn_received_cb(InfTcpConnection* connection,
                 gconstpointer data,
                 guint len,
                 gpointer user_data)
{
  InfIpAddress* addr;
  gchar* str;
  g_object_get(G_OBJECT(connection), "remote-address", &addr, NULL);
  str = inf_ip_address_to_string(addr);
  inf_ip_address_free(addr);

  printf("Data from %s: %.*s\n", str, (int)len, (const char*)data);
  g_free(str);
}
Пример #4
0
static void
conn_error_cb(InfTcpConnection* connection,
              GError* error,
              gpointer user_data)
{
  InfIpAddress* addr;
  gchar* str;
  g_object_get(G_OBJECT(connection), "remote-address", &addr, NULL);
  str = inf_ip_address_to_string(addr);
  inf_ip_address_free(addr);

  printf("Error from %s: %s\n", str, error->message);
  g_free(str);

  g_object_unref(G_OBJECT(connection));
}
Пример #5
0
static void
infd_xmpp_server_new_connection_cb(InfdTcpServer* tcp_server,
                                   InfTcpConnection* tcp_connection,
                                   gpointer user_data)
{
  InfdXmppServer* xmpp_server;
  InfdXmppServerPrivate* priv;
  InfXmppConnection* xmpp_connection;
  InfIpAddress* addr;
  gchar* addr_str;

  xmpp_server = INFD_XMPP_SERVER(user_data);
  priv = INFD_XMPP_SERVER_PRIVATE(xmpp_server);

  /* TODO: We could perform a reverse DNS lookup to find the client hostname
   * here. */
  g_object_get(G_OBJECT(tcp_connection), "remote-address", &addr, NULL);
  addr_str = inf_ip_address_to_string(addr);
  inf_ip_address_free(addr);

  xmpp_connection = inf_xmpp_connection_new(
    tcp_connection,
    INF_XMPP_CONNECTION_SERVER,
    priv->local_hostname,
    addr_str,
    priv->security_policy,
    priv->tls_creds,
    priv->sasl_context,
    priv->sasl_own_context != NULL ? "ANONYMOUS" : priv->sasl_mechanisms
  );

  g_free(addr_str);

  /* We could, alternatively, keep the connection around until authentication
   * has completed and emit the new_connection signal after that, to guarantee
   * that the connection is open when new_connection is emitted. */
  infd_xml_server_new_connection(
    INFD_XML_SERVER(xmpp_server),
    INF_XML_CONNECTION(xmpp_connection)
  );

  g_object_unref(G_OBJECT(xmpp_connection));
}
Пример #6
0
static void
new_connection_cb(InfdTcpServer* server,
                  InfTcpConnection* connection,
                  gpointer user_data)
{
  InfIpAddress* addr;
  gchar* str;
  g_object_get(G_OBJECT(connection), "remote-address", &addr, NULL);
  str = inf_ip_address_to_string(addr);
  inf_ip_address_free(addr);

  printf("Connection from %s\n", str);
  g_free(str);

  g_signal_connect(
    G_OBJECT(connection),
    "received",
    G_CALLBACK(conn_received_cb),
    NULL
  );

  g_signal_connect(
    G_OBJECT(connection),
    "error",
    G_CALLBACK(conn_error_cb),
    NULL
  );

  g_signal_connect(
    G_OBJECT(connection),
    "notify::status",
    G_CALLBACK(conn_notify_status_cb),
    NULL
  );

  g_object_ref(G_OBJECT(connection));
}
Пример #7
0
static void
conn_notify_status_cb(InfTcpConnection* connection,
                      GParamSpec* spec,
                      gpointer user_data)
{
  InfIpAddress* addr;
  InfTcpConnectionStatus status;
  gchar* str;

  g_object_get(
    G_OBJECT(connection),
    "remote-address", &addr,
    "status", &status,
    NULL
  );

  str = inf_ip_address_to_string(addr);
  inf_ip_address_free(addr);

  if(status == INF_TCP_CONNECTION_CLOSED)
    printf("Connection close from %s\n", str);

  g_free(str);
}
Пример #8
0
static void
notify_status_cb(InfdTcpServer* server,
                 GParamSpec* pspec,
                 gpointer user_data)
{
  InfdTcpServerStatus status;
  InfIpAddress* addr;
  guint port;
  gchar* addr_str;

  g_object_get(
    G_OBJECT(server),
    "status", &status,
    "local-address", &addr,
    "local-port", &port,
    NULL
  );

  addr_str = inf_ip_address_to_string(addr);
  inf_ip_address_free(addr);

  switch(status)
  {
  case INFD_TCP_SERVER_CLOSED:
    printf("Server closed\n");
    break;
  case INFD_TCP_SERVER_OPEN:
    printf("Server listening on %s:%u\n", addr_str, port);
    break;
  default:
    g_assert_not_reached();
    break;
  }

  g_free(addr_str);
}
Пример #9
0
static void
notify_status_cb(InfTcpConnection* connection,
                 GParamSpec* pspec,
                 gpointer user_data)
{
  InfTcpConnectionStatus status;
  InfIpAddress* addr;
  guint port;
  InfNameResolver* resolver;
  gchar* addr_str_tmp;
  gchar* addr_str;

  g_object_get(
    G_OBJECT(connection),
    "status", &status,
    "remote-address", &addr,
    "remote-port", &port,
    "resolver", &resolver,
    NULL
  );

  if(addr != NULL)
  {
    addr_str_tmp = inf_ip_address_to_string(addr);
    addr_str = g_strdup_printf("%s:%u", addr_str_tmp, port);
    g_free(addr_str_tmp);
  }
  else
  {
    g_object_get(G_OBJECT(resolver), "hostname", &addr_str, NULL);
  }

  if(addr != NULL)
    inf_ip_address_free(addr);
  if(resolver != NULL)
    g_object_unref(resolver);

  switch(status)
  {
  case INF_TCP_CONNECTION_CONNECTING:
    printf("Connecting to %s\n", addr_str);
    break;
  case INF_TCP_CONNECTION_CONNECTED:
    printf("Connected to %s\n", addr_str);

    g_object_get(
      G_OBJECT(connection),
      "local-address", &addr,
      "local-port", &port,
      NULL
    );

    g_free(addr_str);
    addr_str = inf_ip_address_to_string(addr);
    inf_ip_address_free(addr);

    printf("Connected from %s:%u\n", addr_str, port);
    inf_tcp_connection_send(connection, "Hello, World!\n", 14);
    break;
  case INF_TCP_CONNECTION_CLOSED:
    printf("Connection to %s closed\n", addr_str);
    if(inf_standalone_io_loop_running(INF_STANDALONE_IO(user_data)))
      inf_standalone_io_loop_quit(INF_STANDALONE_IO(user_data));
    break;
  default:
    g_assert_not_reached();
    break;
  }

  g_free(addr_str);
}