jingle-s5b/socks5.c
author Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
Wed, 25 Aug 2010 17:15:49 +0200
changeset 159 1df5f5e3f1e7
parent 158 a068e5714120
child 161 fa1d993d05b6
permissions -rw-r--r--
Fix the segfault when quitting in IBB and various other bugs. Add various things in S5B. Session negociation/candidate exchange should now be working.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
154
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
     1
/*
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
     2
 * socks5.c
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
     3
 *
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
     4
 * Copyrigth (C) 2010 Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
     5
 *
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
     6
 * This program is free software; you can redistribute it and/or modify
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
     7
 * it under the terms of the GNU General Public License as published by
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
     8
 * the Free Software Foundation; either version 2 of the License, or (at
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
     9
 * your option) any later version.
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    10
 *
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    11
 * This program is distributed in the hope that it will be useful, but
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    12
 * WITHOUT ANY WARRANTY; without even the implied warranty of
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    14
 * General Public License for more details.
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    15
 *
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    17
 * along with this program; if not, write to the Free Software
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    19
 * USA
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    20
 */
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    21
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    22
#include "config.h"
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    23
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    24
#include <glib.h>
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    25
#include <gio/gio.h>
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    26
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    27
#include <sys/types.h>
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    28
#include <ifaddrs.h>
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    29
#include <net/if.h>
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    30
#include <netinet/in.h>
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    31
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    32
#include <mcabber/xmpp.h>
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    33
#include <mcabber/modules.h>
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    34
#include <mcabber/utils.h>
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    35
#include <mcabber/xmpp_helper.h>
157
8ec7ce3ecaac S5B: Start the "new" function that create a new JingleS5B.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 156
diff changeset
    36
#include <mcabber/settings.h>
154
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    37
#include <mcabber/logprint.h>
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    38
#include <mcabber/hooks.h>
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    39
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    40
#include <jingle/jingle.h>
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    41
#include <jingle/check.h>
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    42
#include <jingle/register.h>
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    43
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    44
#include "socks5.h"
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    45
156
653fa009fea3 Rename ->check() to newfrommessage and call it only on session-initiate.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 154
diff changeset
    46
static gconstpointer newfrommessage(JingleContent *cn, GError **err);
157
8ec7ce3ecaac S5B: Start the "new" function that create a new JingleS5B.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 156
diff changeset
    47
static JingleHandleStatus handle(JingleAction action, gconstpointer data,
8ec7ce3ecaac S5B: Start the "new" function that create a new JingleS5B.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 156
diff changeset
    48
                                 LmMessageNode *node, GError **err);
154
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    49
static void tomessage(gconstpointer data, LmMessageNode *node);
157
8ec7ce3ecaac S5B: Start the "new" function that create a new JingleS5B.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 156
diff changeset
    50
static gconstpointer new(void);
154
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    51
// static void _send(session_content *sc, gconstpointer data, gchar *buf, gsize size);
156
653fa009fea3 Rename ->check() to newfrommessage and call it only on session-initiate.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 154
diff changeset
    52
static void init(session_content *sc);
154
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    53
static void end(session_content *sc, gconstpointer data);
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    54
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    55
static void handle_sock_io(GSocket *sock, GIOCondition cond, gpointer data);
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    56
static GSList *get_all_local_ips();
158
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
    57
static gchar *gen_random_sid(void);
157
8ec7ce3ecaac S5B: Start the "new" function that create a new JingleS5B.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 156
diff changeset
    58
static gchar *gen_random_cid(void);
154
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    59
static void jingle_socks5_init(void);
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    60
static void jingle_socks5_uninit(void);
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    61
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    62
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    63
const gchar *deps[] = { "jingle", NULL };
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    64
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    65
static JingleTransportFuncs funcs = {
156
653fa009fea3 Rename ->check() to newfrommessage and call it only on session-initiate.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 154
diff changeset
    66
  .newfrommessage = newfrommessage,
157
8ec7ce3ecaac S5B: Start the "new" function that create a new JingleS5B.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 156
diff changeset
    67
  .handle         = handle,
156
653fa009fea3 Rename ->check() to newfrommessage and call it only on session-initiate.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 154
diff changeset
    68
  .tomessage      = tomessage,
157
8ec7ce3ecaac S5B: Start the "new" function that create a new JingleS5B.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 156
diff changeset
    69
  .new            = new,
156
653fa009fea3 Rename ->check() to newfrommessage and call it only on session-initiate.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 154
diff changeset
    70
  .send           = NULL,
653fa009fea3 Rename ->check() to newfrommessage and call it only on session-initiate.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 154
diff changeset
    71
  .init           = init,
653fa009fea3 Rename ->check() to newfrommessage and call it only on session-initiate.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 154
diff changeset
    72
  .end            = end
154
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    73
};
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    74
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    75
module_info_t  info_jingle_s5b = {
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    76
  .branch          = MCABBER_BRANCH,
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    77
  .api             = MCABBER_API_VERSION,
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    78
  .version         = PROJECT_VERSION,
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    79
  .description     = "Jingle SOCKS5 Bytestream (XEP-0260)\n",
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    80
  .requires        = deps,
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    81
  .init            = jingle_socks5_init,
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    82
  .uninit          = jingle_socks5_uninit,
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    83
  .next            = NULL,
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    84
};
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    85
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    86
static const gchar *jingle_s5b_types[] = {
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    87
  "assisted",
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    88
  "direct",
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    89
  "proxy",
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    90
  "tunnel",
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    91
  NULL
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    92
};
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    93
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    94
static const gchar *jingle_s5b_modes[] = {
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    95
  "tcp",
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    96
  "udp",
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    97
  NULL
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    98
};
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
    99
157
8ec7ce3ecaac S5B: Start the "new" function that create a new JingleS5B.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 156
diff changeset
   100
typedef struct {
8ec7ce3ecaac S5B: Start the "new" function that create a new JingleS5B.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 156
diff changeset
   101
  GInetAddress *address;
8ec7ce3ecaac S5B: Start the "new" function that create a new JingleS5B.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 156
diff changeset
   102
  guint32       priority;
159
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   103
} LocalIP;
157
8ec7ce3ecaac S5B: Start the "new" function that create a new JingleS5B.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 156
diff changeset
   104
154
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   105
/**
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   106
 * @brief Linked list of candidates to send on session-initiate
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   107
 */
159
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   108
static GSList *local_ips = NULL;
154
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   109
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   110
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   111
static gint index_in_array(const gchar *str, const gchar **array)
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   112
{
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   113
  gint i;
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   114
  for (i = 0; array[i]; i++) {
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   115
    if (!g_strcmp0(array[i], str)) {
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   116
      return i;
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   117
    }
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   118
  }
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   119
  return -1;
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   120
}
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   121
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   122
static gint prioritycmp(gconstpointer a, gconstpointer b)
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   123
{
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   124
  S5BCandidate *s1 = (S5BCandidate *)a, *s2 = (S5BCandidate *)b;
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   125
  if (s1->priority < s2->priority) {
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   126
    return 1;
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   127
  } else if (s1->priority > s2->priority) {
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   128
    return -1;
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   129
  } else {
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   130
    return 0;
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   131
  }
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   132
}
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   133
158
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   134
/**
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   135
 * @brief Parse a list of <candidate> elements
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   136
 * @return a list of S5BCandidate
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   137
 */
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   138
static GSList *parse_candidates(LmMessageNode *node)
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   139
{
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   140
  LmMessageNode *node2;
159
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   141
  GSList *list = NULL;
158
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   142
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   143
  for (node2 = node->children; node2; node2 = node2->next) {
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   144
    if (g_strcmp0(node->name, "candidate"))
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   145
        continue;
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   146
    const gchar *portstr, *prioritystr, *typestr;
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   147
    S5BCandidate *cand = g_new0(S5BCandidate, 1);
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   148
    cand->cid    = g_strdup(lm_message_node_get_attribute(node2, "cid"));
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   149
    cand->host   = g_strdup(lm_message_node_get_attribute(node2, "host"));
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   150
    cand->jid    = g_strdup(lm_message_node_get_attribute(node2, "jid"));
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   151
    portstr      = lm_message_node_get_attribute(node2, "port");
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   152
    prioritystr  = lm_message_node_get_attribute(node2, "priority");
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   153
    typestr      = lm_message_node_get_attribute(node2, "type");
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   154
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   155
    if (!cand->cid || !cand->host || !cand->jid || !prioritystr) {
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   156
      g_free(cand);
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   157
      continue;
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   158
    }
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   159
    cand->port     = g_ascii_strtoull(portstr, NULL, 10);
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   160
    cand->priority = g_ascii_strtoull(prioritystr, NULL, 10);
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   161
    cand->type     = index_in_array(typestr, jingle_s5b_types);
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   162
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   163
    if (cand->type == -1) {
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   164
      g_free(cand);
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   165
      continue;
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   166
    }
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   167
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   168
    list = g_slist_prepend(list, cand);
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   169
  }
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   170
  list = g_slist_sort(list, prioritycmp);
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   171
  return list;
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   172
}
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   173
159
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   174
static GSList *get_our_candidates(guint16 port)
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   175
{
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   176
  GSList *our_candidates = NULL, *entry;
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   177
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   178
  for (entry = local_ips; entry; entry = entry->next) {
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   179
    LocalIP *lcand = (LocalIP *)entry->data;
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   180
    S5BCandidate *cand = g_new0(S5BCandidate, 1);
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   181
    cand->cid      = gen_random_cid();
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   182
    cand->host     = g_inet_address_to_string(lcand->address);
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   183
    cand->jid      = g_strdup(lm_connection_get_jid(lconnection));
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   184
    cand->port     = port;
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   185
    cand->priority = lcand->priority;
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   186
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   187
    our_candidates = g_slist_prepend(our_candidates, cand);
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   188
  }
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   189
  our_candidates = g_slist_sort(our_candidates, prioritycmp);
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   190
  return our_candidates;
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   191
}
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   192
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   193
/**
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   194
 * @brief Get a port number by settings or randomly
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   195
 * @return A guint16 containing the port number
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   196
 * */
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   197
static guint16 get_port(void)
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   198
{
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   199
  // TODO: find a way to make sure the port is not already used
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   200
  guint64 portstart, portend;
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   201
  guint16 port;
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   202
  const gchar *port_range = settings_opt_get("jingle_s5b_portrange");
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   203
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   204
  if (port_range != NULL) {
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   205
    sscanf(port_range, "%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT, &portstart, &portend);
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   206
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   207
    if ((portstart >= 1024 && portstart <= (guint16)~0) &&
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   208
        (portend >= 1024 && portend <= (guint16)~0) && portstart <= portend) {
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   209
      port = g_random_int_range(portstart, portend);
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   210
    } else {
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   211
      scr_LogPrint(LPRINT_LOGNORM, "Jingle S5B: Invalid port range specified");
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   212
      port = g_random_int_range(1024, (guint16)~0);
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   213
    }
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   214
  } else {
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   215
    port = g_random_int_range(1024, (guint16)~0);
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   216
  }
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   217
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   218
  return port;
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   219
}
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   220
156
653fa009fea3 Rename ->check() to newfrommessage and call it only on session-initiate.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 154
diff changeset
   221
static gconstpointer newfrommessage(JingleContent *cn, GError **err)
154
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   222
{
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   223
  JingleS5B *js5b;
159
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   224
  LmMessageNode *node = cn->transport;
154
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   225
  const gchar *modestr;
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   226
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   227
  js5b = g_new0(JingleS5B, 1);
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   228
  modestr    = lm_message_node_get_attribute(node, "mode");
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   229
  js5b->mode = index_in_array(modestr, jingle_s5b_modes);
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   230
  js5b->sid  = g_strdup(lm_message_node_get_attribute(node, "sid"));
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   231
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   232
  if (!js5b->sid) {
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   233
    g_set_error(err, JINGLE_CHECK_ERROR, JINGLE_CHECK_ERROR_MISSING,
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   234
                "an attribute of the transport element is missing");
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   235
    g_free(js5b);
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   236
    return NULL;
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   237
  }
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   238
158
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   239
  js5b->candidates = parse_candidates(node);
159
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   240
  js5b->ourcandidates = get_our_candidates(get_port());
154
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   241
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   242
  return (gconstpointer) js5b;
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   243
}
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   244
157
8ec7ce3ecaac S5B: Start the "new" function that create a new JingleS5B.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 156
diff changeset
   245
static gconstpointer new(void)
8ec7ce3ecaac S5B: Start the "new" function that create a new JingleS5B.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 156
diff changeset
   246
{
8ec7ce3ecaac S5B: Start the "new" function that create a new JingleS5B.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 156
diff changeset
   247
  JingleS5B *js5b = g_new0(JingleS5B, 1);
159
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   248
158
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   249
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   250
  js5b->mode = JINGLE_S5B_TCP;
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   251
  js5b->sid  = gen_random_sid();
159
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   252
  // the user can manually specify a port range to use in for format:
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   253
  // portstart-portend
158
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   254
157
8ec7ce3ecaac S5B: Start the "new" function that create a new JingleS5B.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 156
diff changeset
   255
159
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   256
  js5b->ourcandidates = get_our_candidates(get_port());
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   257
157
8ec7ce3ecaac S5B: Start the "new" function that create a new JingleS5B.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 156
diff changeset
   258
  return js5b;
8ec7ce3ecaac S5B: Start the "new" function that create a new JingleS5B.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 156
diff changeset
   259
}
8ec7ce3ecaac S5B: Start the "new" function that create a new JingleS5B.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 156
diff changeset
   260
158
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   261
static JingleHandleStatus
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   262
handle_session_accept(JingleS5B *js5b, LmMessageNode *node, GError **err)
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   263
{
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   264
  js5b->candidates = parse_candidates(node);
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   265
  return JINGLE_STATUS_HANDLED;
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   266
}
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   267
157
8ec7ce3ecaac S5B: Start the "new" function that create a new JingleS5B.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 156
diff changeset
   268
static JingleHandleStatus handle(JingleAction action, gconstpointer data,
8ec7ce3ecaac S5B: Start the "new" function that create a new JingleS5B.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 156
diff changeset
   269
                                 LmMessageNode *node, GError **err)
8ec7ce3ecaac S5B: Start the "new" function that create a new JingleS5B.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 156
diff changeset
   270
{
158
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   271
  JingleS5B *js5b = (JingleS5B *)data;
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   272
157
8ec7ce3ecaac S5B: Start the "new" function that create a new JingleS5B.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 156
diff changeset
   273
  if (action == JINGLE_SESSION_ACCEPT) {
158
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   274
    return handle_session_accept(js5b, node, err);
157
8ec7ce3ecaac S5B: Start the "new" function that create a new JingleS5B.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 156
diff changeset
   275
  }
8ec7ce3ecaac S5B: Start the "new" function that create a new JingleS5B.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 156
diff changeset
   276
  return JINGLE_STATUS_NOT_HANDLED;
8ec7ce3ecaac S5B: Start the "new" function that create a new JingleS5B.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 156
diff changeset
   277
}
8ec7ce3ecaac S5B: Start the "new" function that create a new JingleS5B.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 156
diff changeset
   278
154
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   279
static void tomessage(gconstpointer data, LmMessageNode *node)
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   280
{
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   281
  JingleS5B *js5 = (JingleS5B *)data;
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   282
  S5BCandidate *js5c;
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   283
  
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   284
  LmMessageNode *node2, *node3;
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   285
  gchar *port;
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   286
  gchar *priority;
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   287
  GSList *el;
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   288
  
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   289
  if (lm_message_node_get_child(node, "transport") != NULL)
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   290
    return;
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   291
  
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   292
  node2 = lm_message_node_add_child(node, "transport", NULL);
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   293
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   294
  lm_message_node_set_attributes(node2, "xmlns", NS_JINGLE_TRANSPORT_SOCKS5,
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   295
                                 "sid", js5->sid,
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   296
                                 "mode", jingle_s5b_modes[js5->mode],
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   297
                                 NULL);
158
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   298
  for (el = js5->ourcandidates; el; el = el->next) {
154
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   299
    js5c = (S5BCandidate*) el->data;
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   300
    node3 = lm_message_node_add_child(node2, "candidate", NULL);
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   301
    
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   302
    port = g_strdup_printf("%" G_GUINT16_FORMAT, js5c->port);
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   303
    priority = g_strdup_printf("%" G_GUINT64_FORMAT, js5c->priority);
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   304
    
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   305
    lm_message_node_set_attributes(node3, "cid", js5c->cid,
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   306
                                   "host", js5c->host,
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   307
                                   "jid", js5c->jid,
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   308
                                   "port", port,
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   309
                                   "priority", priority,
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   310
                                   "type", jingle_s5b_types[js5c->type],
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   311
                                   NULL);
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   312
    g_free(port);
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   313
    g_free(priority);
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   314
  }
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   315
}
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   316
156
653fa009fea3 Rename ->check() to newfrommessage and call it only on session-initiate.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 154
diff changeset
   317
static void init(session_content *sc)
154
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   318
{
156
653fa009fea3 Rename ->check() to newfrommessage and call it only on session-initiate.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 154
diff changeset
   319
  JingleS5B *js5 = NULL;
154
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   320
  GInetAddress *addr;
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   321
  GSocketAddress *saddr;
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   322
  GSource *socksource;
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   323
  GError *err = NULL;
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   324
  g_assert(js5->sock == NULL);
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   325
156
653fa009fea3 Rename ->check() to newfrommessage and call it only on session-initiate.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 154
diff changeset
   326
  addr = g_inet_address_new_from_string("127.0.0.1");
154
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   327
  js5->sock = g_socket_new(g_inet_address_get_family(addr), G_SOCKET_TYPE_STREAM,
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   328
                           G_SOCKET_PROTOCOL_TCP, &err);
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   329
  if (js5->sock == NULL) {
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   330
    scr_LogPrint(LPRINT_LOGNORM, "Jingle SOCKS5: Error while creating a new socket: %s",
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   331
                 err->message != NULL ? err->message : "(no message)");
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   332
    return; // TODO: we need a way to return errors...
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   333
  }
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   334
  g_socket_set_blocking(js5->sock, FALSE);
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   335
  socksource = g_socket_create_source(js5->sock, ~0, NULL);
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   336
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   337
  g_source_set_callback(socksource, (GSourceFunc)handle_sock_io, NULL, NULL);
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   338
  g_source_attach(socksource, NULL);
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   339
  g_source_unref(socksource);
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   340
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   341
  saddr = g_inet_socket_address_new(addr, 31337);
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   342
  if (!g_socket_connect(js5->sock, saddr, NULL, &err)) {
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   343
    scr_LogPrint(LPRINT_LOGNORM, "Jingle SOCKS5: Error while connecting to the host: %s",
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   344
                 err->message != NULL ? err->message : "(no message)");
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   345
    return;
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   346
  }
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   347
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   348
}
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   349
157
8ec7ce3ecaac S5B: Start the "new" function that create a new JingleS5B.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 156
diff changeset
   350
static void end(session_content *sc, gconstpointer data) {
8ec7ce3ecaac S5B: Start the "new" function that create a new JingleS5B.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 156
diff changeset
   351
  return;
8ec7ce3ecaac S5B: Start the "new" function that create a new JingleS5B.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 156
diff changeset
   352
}
8ec7ce3ecaac S5B: Start the "new" function that create a new JingleS5B.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 156
diff changeset
   353
154
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   354
/**
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   355
 * Handle any event on a sock
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   356
 */
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   357
static void handle_sock_io(GSocket *sock, GIOCondition cond, gpointer data)
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   358
{
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   359
  switch (cond) {
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   360
    case G_IO_IN:
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   361
      break;
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   362
    case G_IO_OUT:
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   363
      break;
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   364
    case G_IO_ERR:
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   365
      break;
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   366
    case G_IO_HUP:
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   367
      break;
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   368
    default:
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   369
      ;
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   370
      // ?!
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   371
  }
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   372
}
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   373
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   374
/**
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   375
 * @brief Discover all IPs of this computer
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   376
 * @return A linked list of GInetAddress
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   377
 */
159
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   378
static GSList *get_all_local_ips(void) {
154
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   379
  GSList *addresses = NULL;
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   380
  GInetAddress *thisaddr;
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   381
  GSocketFamily family;
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   382
  struct ifaddrs *first, *ifaddr;
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   383
  struct sockaddr_in *native;
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   384
  struct sockaddr_in6 *native6;
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   385
  const guint8 *addrdata;
157
8ec7ce3ecaac S5B: Start the "new" function that create a new JingleS5B.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 156
diff changeset
   386
  guint16 ifacecounter = 0; // for lack of a better method
159
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   387
  LocalIP *candidate;
154
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   388
157
8ec7ce3ecaac S5B: Start the "new" function that create a new JingleS5B.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 156
diff changeset
   389
  gint rval = getifaddrs(&first);
8ec7ce3ecaac S5B: Start the "new" function that create a new JingleS5B.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 156
diff changeset
   390
  if (!rval)
8ec7ce3ecaac S5B: Start the "new" function that create a new JingleS5B.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 156
diff changeset
   391
    return NULL;
154
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   392
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   393
  for (ifaddr = first; ifaddr; ifaddr = ifaddr->ifa_next) {
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   394
    if (!(ifaddr->ifa_flags & IFF_UP) || ifaddr->ifa_flags & IFF_LOOPBACK)
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   395
      continue;
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   396
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   397
    if (ifaddr->ifa_addr->sa_family == AF_INET) {
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   398
      native = (struct sockaddr_in *)ifaddr->ifa_addr;
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   399
      addrdata = (const guint8 *)&native->sin_addr.s_addr;
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   400
      family = G_SOCKET_FAMILY_IPV4;
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   401
    } else if (ifaddr->ifa_addr->sa_family == AF_INET6) {
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   402
      native6 = (struct sockaddr_in6 *)ifaddr->ifa_addr;
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   403
      addrdata = (const guint8 *)&native6->sin6_addr.s6_addr;
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   404
      family = G_SOCKET_FAMILY_IPV6;
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   405
    } else
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   406
      continue;
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   407
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   408
    thisaddr = g_inet_address_new_from_bytes(addrdata, family);
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   409
    if (g_inet_address_get_is_link_local(thisaddr)) {
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   410
      g_object_unref(thisaddr);
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   411
      continue;
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   412
    }/* else if (g_inset_address_get_is_site_local(thisaddr)) {
156
653fa009fea3 Rename ->check() to newfrommessage and call it only on session-initiate.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 154
diff changeset
   413
      // TODO: should we offer a way to filter the offer of LAN ips ?
154
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   414
    } */
159
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   415
    candidate = g_new0(LocalIP, 1);
157
8ec7ce3ecaac S5B: Start the "new" function that create a new JingleS5B.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 156
diff changeset
   416
    candidate->address  = thisaddr;
8ec7ce3ecaac S5B: Start the "new" function that create a new JingleS5B.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 156
diff changeset
   417
    candidate->priority = (1<<16)*126+ifacecounter;
8ec7ce3ecaac S5B: Start the "new" function that create a new JingleS5B.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 156
diff changeset
   418
    addresses = g_slist_prepend(addresses, candidate);
8ec7ce3ecaac S5B: Start the "new" function that create a new JingleS5B.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 156
diff changeset
   419
    ++ifacecounter;
154
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   420
  }
157
8ec7ce3ecaac S5B: Start the "new" function that create a new JingleS5B.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 156
diff changeset
   421
  freeifaddrs(first);
8ec7ce3ecaac S5B: Start the "new" function that create a new JingleS5B.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 156
diff changeset
   422
154
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   423
  return addresses;
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   424
}
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   425
158
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   426
static gchar *random_str(guint len)
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   427
{
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   428
  gchar *str;
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   429
  gchar car[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   430
  gint i;
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   431
  str = g_new0(gchar, 8);
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   432
  for (i = 0; i < len; i++)
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   433
    str[i] = car[g_random_int_range(0, sizeof(car)/sizeof(car[0]))];
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   434
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   435
  str[len] = '\0';
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   436
  return str;	
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   437
}
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   438
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   439
static gchar *gen_random_sid(void)
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   440
{
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   441
  return random_str(7);
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   442
}
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   443
157
8ec7ce3ecaac S5B: Start the "new" function that create a new JingleS5B.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 156
diff changeset
   444
static gchar *gen_random_cid(void)
8ec7ce3ecaac S5B: Start the "new" function that create a new JingleS5B.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 156
diff changeset
   445
{
158
a068e5714120 JS5B: Start the handle function
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 157
diff changeset
   446
  return random_str(7);
157
8ec7ce3ecaac S5B: Start the "new" function that create a new JingleS5B.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 156
diff changeset
   447
}
8ec7ce3ecaac S5B: Start the "new" function that create a new JingleS5B.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 156
diff changeset
   448
159
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   449
static void free_localip(LocalIP *l) {
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   450
  g_object_unref(l->address);
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   451
  g_free(l);
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   452
}
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   453
154
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   454
static void jingle_socks5_init(void)
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   455
{
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   456
  g_type_init();
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   457
  jingle_register_transport(NS_JINGLE_TRANSPORT_SOCKS5, &funcs,
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   458
                            JINGLE_TRANSPORT_STREAMING,
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   459
                            JINGLE_TRANSPORT_PRIO_HIGH);
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   460
  xmpp_add_feature(NS_JINGLE_TRANSPORT_SOCKS5);
159
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   461
  local_ips = get_all_local_ips();
154
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   462
}
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   463
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   464
static void jingle_socks5_uninit(void)
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   465
{
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   466
  xmpp_del_feature(NS_JINGLE_TRANSPORT_SOCKS5);
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   467
  jingle_unregister_transport(NS_JINGLE_TRANSPORT_SOCKS5);
159
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   468
  g_slist_foreach(local_ips, (GFunc)free_localip, NULL);
1df5f5e3f1e7 Fix the segfault when quitting in IBB and various other bugs.
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents: 158
diff changeset
   469
  g_slist_free(local_ips);
154
1ffa1733d4b6 Add a function to find local ips in S5B
Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
parents:
diff changeset
   470
}