<commit_before>/*
 * bonjour-contact.c - Source for SalutBonjourContact
 * Copyright (C) 2012 Collabora Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <dbus/dbus-glib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#undef interface
#include "bonjour-contact.h"

#include <telepathy-glib/channel-factory-iface.h>
#include <telepathy-glib/interfaces.h>

#define DEBUG_FLAG DEBUG_MUC
#include "debug.h"

G_DEFINE_TYPE (SalutBonjourContact, salut_bonjour_contact,
    SALUT_TYPE_CONTACT);

/* properties */
enum {
  PROP_CLIENT = 1,
  LAST_PROP
};

/* private structure */
typedef struct _SalutBonjourContactPrivate SalutBonjourContactPrivate;

struct _SalutBonjourContactPrivate
{
  SalutBonjourDiscoveryClient *discovery_client;
  GSList *resolvers;

  char *full_name;

  gboolean dispose_has_run;
};

struct resolverInfo {
  uint32_t interface;
  const char *name;
  const char *type;
  const char *domain;
};

typedef struct _SalutBonjourResolveCtx
{
  DNSServiceRef resolve_ref;
  DNSServiceRef address_ref;

  SalutBonjourContact *contact;

  char *name;
  char *type;
  char *domain;
  uint32_t interface;
  struct sockaddr *address;
  uint16_t port;

  uint16_t txt_length;
  char *txt_record;
} SalutBonjourResolveCtx;

static void
salut_bonjour_contact_init (SalutBonjourContact *self)
{
  SalutBonjourContactPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
      SALUT_TYPE_BONJOUR_CONTACT, SalutBonjourContactPrivate);

  self->priv = priv;

  priv->full_name = NULL;
  priv->resolvers = NULL;
}

static void
_salut_bonjour_resolve_ctx_free (SalutBonjourContact *self,
                                 SalutBonjourResolveCtx *ctx)
{
  SalutBonjourContactPrivate *priv = self->priv;

  if (ctx->address_ref != NULL)
    {
      salut_bonjour_discovery_client_drop_svc_ref (priv->discovery_client,
          ctx->address_ref);
    }

  if (ctx->txt_record)
    {
      g_free (ctx->txt_record);
    }

  if (ctx->address)
    g_free (ctx->address);

  if (ctx->name)
    g_free (ctx->name);

  if (ctx->type)
    g_free (ctx->type);

  if (ctx->domain)
    g_free (ctx->domain);

  g_slice_free (SalutBonjourResolveCtx, ctx);
}

static void
salut_bonjour_contact_get_property (GObject *object,
                                    guint property_id,
                                    GValue *value,
                                    GParamSpec *pspec)
{
  SalutBonjourContact *self = SALUT_BONJOUR_CONTACT (object);
  SalutBonjourContactPrivate *priv = self->priv;

  switch (property_id)
    {
      case PROP_CLIENT:
        g_value_set_object (value, priv->discovery_client);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
        break;
    }
}

static void
salut_bonjour_contact_set_property (GObject *object,
                                    guint property_id,
                                    const GValue *value,
                                    GParamSpec *pspec)
{
  SalutBonjourContact *self = SALUT_BONJOUR_CONTACT (object);
  SalutBonjourContactPrivate *priv = self->priv;

  switch (property_id)
    {
      case PROP_CLIENT:
        priv->discovery_client = g_value_dup_object (value);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
        break;
    }
}

static gchar *
salut_bonjour_contact_dup_jid (WockyContact *contact)
{
  SalutContact *self = SALUT_CONTACT (contact);

  return g_strdup (self->name);
}

static GList *
salut_bonjour_contact_ll_get_addresses (WockyLLContact *contact)
{
  SalutBonjourContact *self = SALUT_BONJOUR_CONTACT (contact);
  SalutBonjourContactPrivate *priv = self->priv;
  /* omg, GQueue! */
  GQueue queue = G_QUEUE_INIT;
  GSList *l;

  for (l = priv->resolvers; l != NULL; l = l->next)
    {
     SalutBonjourResolveCtx *ctx = l->data;
     uint16_t port;

     if (ctx->address)
       {
         GInetAddress *addr;
         GSocketAddress *socket_address;

         if (ctx->address->sa_family == AF_INET)
           {
             struct sockaddr_in *address = (struct sockaddr_in *) ctx->address;

             port = ctx->port;
             addr = g_inet_address_new_from_bytes (
                 (guint8 *) &(address->sin_addr), G_SOCKET_FAMILY_IPV4);
             DEBUG ("%s", g_inet_address_to_string (addr));
           }
         else if (ctx->address->sa_family == AF_INET6)
           {
             struct sockaddr_in6 *address = (struct sockaddr_in6 *) ctx->address;
             port = ctx->port;
             addr = g_inet_address_new_from_bytes (
                 (uint8_t *) &(address->sin6_addr), G_SOCKET_FAMILY_IPV6);
           }
         else
           g_assert_not_reached ();

         socket_address = g_inet_socket_address_new (addr, port);
         g_object_unref (addr);

         g_queue_push_tail (&queue, socket_address);
       }
    }

  return queue.head;
}

static void
_bonjour_add_address (SalutBonjourResolveCtx *ctx,
                      GArray *addresses)
{
  salut_contact_address_t s_address;
  const struct sockaddr *address = ctx->address;<commit_msg>bonjour-contact: Remove unwanted debug messages<commit_after>/*
 * bonjour-contact.c - Source for SalutBonjourContact
 * Copyright (C) 2012 Collabora Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <dbus/dbus-glib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#undef interface
#include "bonjour-contact.h"

#include <telepathy-glib/channel-factory-iface.h>
#include <telepathy-glib/interfaces.h>

#define DEBUG_FLAG DEBUG_MUC
#include "debug.h"

G_DEFINE_TYPE (SalutBonjourContact, salut_bonjour_contact,
    SALUT_TYPE_CONTACT);

/* properties */
enum {
  PROP_CLIENT = 1,
  LAST_PROP
};

/* private structure */
typedef struct _SalutBonjourContactPrivate SalutBonjourContactPrivate;

struct _SalutBonjourContactPrivate
{
  SalutBonjourDiscoveryClient *discovery_client;
  GSList *resolvers;

  char *full_name;

  gboolean dispose_has_run;
};

struct resolverInfo {
  uint32_t interface;
  const char *name;
  const char *type;
  const char *domain;
};

typedef struct _SalutBonjourResolveCtx
{
  DNSServiceRef resolve_ref;
  DNSServiceRef address_ref;

  SalutBonjourContact *contact;

  char *name;
  char *type;
  char *domain;
  uint32_t interface;
  struct sockaddr *address;
  uint16_t port;

  uint16_t txt_length;
  char *txt_record;
} SalutBonjourResolveCtx;

static void
salut_bonjour_contact_init (SalutBonjourContact *self)
{
  SalutBonjourContactPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
      SALUT_TYPE_BONJOUR_CONTACT, SalutBonjourContactPrivate);

  self->priv = priv;

  priv->full_name = NULL;
  priv->resolvers = NULL;
}

static void
_salut_bonjour_resolve_ctx_free (SalutBonjourContact *self,
                                 SalutBonjourResolveCtx *ctx)
{
  SalutBonjourContactPrivate *priv = self->priv;

  if (ctx->address_ref != NULL)
    {
      salut_bonjour_discovery_client_drop_svc_ref (priv->discovery_client,
          ctx->address_ref);
    }

  if (ctx->txt_record)
    {
      g_free (ctx->txt_record);
    }

  if (ctx->address)
    g_free (ctx->address);

  if (ctx->name)
    g_free (ctx->name);

  if (ctx->type)
    g_free (ctx->type);

  if (ctx->domain)
    g_free (ctx->domain);

  g_slice_free (SalutBonjourResolveCtx, ctx);
}

static void
salut_bonjour_contact_get_property (GObject *object,
                                    guint property_id,
                                    GValue *value,
                                    GParamSpec *pspec)
{
  SalutBonjourContact *self = SALUT_BONJOUR_CONTACT (object);
  SalutBonjourContactPrivate *priv = self->priv;

  switch (property_id)
    {
      case PROP_CLIENT:
        g_value_set_object (value, priv->discovery_client);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
        break;
    }
}

static void
salut_bonjour_contact_set_property (GObject *object,
                                    guint property_id,
                                    const GValue *value,
                                    GParamSpec *pspec)
{
  SalutBonjourContact *self = SALUT_BONJOUR_CONTACT (object);
  SalutBonjourContactPrivate *priv = self->priv;

  switch (property_id)
    {
      case PROP_CLIENT:
        priv->discovery_client = g_value_dup_object (value);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
        break;
    }
}

static gchar *
salut_bonjour_contact_dup_jid (WockyContact *contact)
{
  SalutContact *self = SALUT_CONTACT (contact);

  return g_strdup (self->name);
}

static GList *
salut_bonjour_contact_ll_get_addresses (WockyLLContact *contact)
{
  SalutBonjourContact *self = SALUT_BONJOUR_CONTACT (contact);
  SalutBonjourContactPrivate *priv = self->priv;
  /* omg, GQueue! */
  GQueue queue = G_QUEUE_INIT;
  GSList *l;

  for (l = priv->resolvers; l != NULL; l = l->next)
    {
     SalutBonjourResolveCtx *ctx = l->data;
     uint16_t port;

     if (ctx->address)
       {
         GInetAddress *addr;
         GSocketAddress *socket_address;

         if (ctx->address->sa_family == AF_INET)
           {
             struct sockaddr_in *address = (struct sockaddr_in *) ctx->address;

             port = ctx->port;
             addr = g_inet_address_new_from_bytes (
                 (guint8 *) &(address->sin_addr), G_SOCKET_FAMILY_IPV4);
           }
         else if (ctx->address->sa_family == AF_INET6)
           {
             struct sockaddr_in6 *address = (struct sockaddr_in6 *) ctx->address;
             port = ctx->port;
             addr = g_inet_address_new_from_bytes (
                 (uint8_t *) &(address->sin6_addr), G_SOCKET_FAMILY_IPV6);
           }
         else
           g_assert_not_reached ();

         socket_address = g_inet_socket_address_new (addr, port);
         g_object_unref (addr);

         g_queue_push_tail (&queue, socket_address);
       }
    }

  return queue.head;
}

static void
_bonjour_add_address (SalutBonjourResolveCtx *ctx,
                      GArray *addresses)
{
  salut_contact_address_t s_address;
  const struct sockaddr *address = ctx->address;
{'fraction_non_alphanumeric': 0.08806279109884423, 'fraction_numerical': 0.006210108676901846, 'mean_word_length': 2.856002660458929, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 91, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2059631', 'n_tokens_mistral': 3853, 'n_tokens_neox': 3732, 'n_words': 891}