aboutsummaryrefslogtreecommitdiff
path: root/src/setup/gnunet-setup-exit-services.c
blob: 641642e956e3133b3bf8b7eccd7ee92b5831ce22 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
/*
     This file is part of GNUnet.
     Copyright (C) 2013 GNUnet e.V.

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 3, or (at your
     option) any later version.

     GNUnet 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
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     Boston, MA 02110-1301, USA.
*/

/**
 * @file src/setup/gnunet-setup-exit-services.c
 * @brief code for the dialog to configure EXIT records
 * @author Christian Grothoff
 */
#include "gnunet-setup.h"
#include <gnunet/gnunet_util_lib.h>
#include <gdk/gdkkeysyms.h>


/**
 * Columns in the hosted service model.
 */
enum ServiceModelColumns
{
  /**
   * A gchararray
   */
  SERVICE_MC_SERVICE_NAME = 0,

  /**
   * A gboolean
   */
  SERVICE_MC_ISUDP = 1,

  /**
   * A guint
   */
  SERVICE_MC_VISIBLE_PORT = 2,

  /**
   * Destination adddress, a gchararray
   */
  SERVICE_MC_DESTINATION_ADDRESS = 3

};


/**
 * Check if the section represents a hosted service and then update
 * the GtkListStore accordingly.
 *
 * @param cls the list store to modify
 * @param section name of the section
 */
static void
add_name_entry_to_list_store (void *cls, const char *section)
{
  GtkListStore *ls = cls;
  GtkTreeIter iter;
  char *sld;
  char *destination;
  char *redirect;
  char *cpy;
  gboolean udp;
  guint local_port;

  if (NULL == section)
  {
    gtk_list_store_insert_with_values (ls,
                                       &iter,
                                       G_MAXINT,
                                       SERVICE_MC_SERVICE_NAME,
                                       "",
                                       SERVICE_MC_ISUDP,
                                       FALSE,
                                       SERVICE_MC_VISIBLE_PORT,
                                       (guint) 80,
                                       SERVICE_MC_DESTINATION_ADDRESS,
                                       "169.254.86.1:8080",
                                       -1);
    return;
  }

  if ((8 > strlen (section)) ||
      (0 != strcmp (".gnunet.", section + ((strlen (section) - 8)))))
    return;
  sld = GNUNET_strdup (section);
  sld[strlen (section) - 8] = '\0';
  udp = FALSE;
  do
  {
    if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg,
                                                            section,
                                                            (udp)
                                                              ? "UDP_REDIRECTS"
                                                              : "TCP_REDIRECTS",
                                                            &cpy))
    {
      for (redirect = strtok (cpy, " "); NULL != redirect;
           redirect = strtok (NULL, " "))
      {
        if (NULL == (destination = strstr (redirect, ":")))
        {
          GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                      _ ("Option `%s' is not formatted correctly!\n"),
                      redirect);
          continue;
        }
        destination[0] = '\0';
        destination++;

        local_port = atoi (redirect);
        if (! ((local_port > 0) && (local_port < 65536)))
        {
          GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                      _ ("`%s' is not a valid port number!\n"),
                      redirect);
          continue;
        }
        gtk_list_store_insert_with_values (ls,
                                           &iter,
                                           0,
                                           SERVICE_MC_SERVICE_NAME,
                                           sld,
                                           SERVICE_MC_ISUDP,
                                           udp,
                                           SERVICE_MC_VISIBLE_PORT,
                                           local_port,
                                           SERVICE_MC_DESTINATION_ADDRESS,
                                           destination,
                                           -1);
      }
      GNUNET_free (cpy);
    }
    udp = ! udp;
  } while (udp);
  GNUNET_free (sld);
}


/**
 * Initialize the GtkListModel with the hosted service specification.
 *
 * @param cls NULL
 * @param section section with the value (NULL)
 * @param option option name (NULL)
 * @param value value as a string (NULL)
 * @param widget widget to initialize (the GtkTreeView)
 * @param cfg configuration handle
 * @return #GNUNET_OK on success, #GNUNET_SYSERR if there was a problem
 */
int
load_hosted_service_configuration (
  const void *cls,
  const char *section,
  const char *option,
  const char *value,
  GObject *widget,
  const struct GNUNET_CONFIGURATION_Handle *cfg)
{
  GtkTreeView *tv;
  GtkListStore *ls;

  tv = GTK_TREE_VIEW (widget);
  if (NULL == tv)
  {
    GNUNET_break (0);
    return GNUNET_SYSERR;
  }
  ls = GTK_LIST_STORE (gtk_tree_view_get_model (tv));
  GNUNET_CONFIGURATION_iterate_sections (cfg,
                                         &add_name_entry_to_list_store,
                                         ls);
  /* finally, add empty entry */
  add_name_entry_to_list_store (ls, NULL);
  return GNUNET_OK;
}


/**
 * Records we use to build DNS information lists.
 */
struct HostedServiceInfo
{
  /**
   * We keep these in a singly-linked list.
   */
  struct HostedServiceInfo *next;

  /**
   * Name of the section in the configuration (must end in ".gnunet.").
   */
  char *section;

  /**
   * String describing all TCP redirects.
   */
  char *tcpred;

  /**
   * String describing all UDP redirects.
   */
  char *udpred;
};


/**
 * Function called for each section in the configuration.
 * Gather existing ttl, section names and altnames.
 *
 * @param cls 'struct HostedServiceInfo**' to create
 * @param section name of a section in the configuration
 */
static void
collect_hosted_service_sections (void *cls, const char *section)
{
  struct HostedServiceInfo **base = cls;
  struct HostedServiceInfo *pos;

  if ((8 > strlen (section)) ||
      (0 != strcmp (".gnunet.", section + ((strlen (section) - 8)))))
    return;
  pos = GNUNET_new (struct HostedServiceInfo);
  pos->section = GNUNET_strdup (section);
  pos->tcpred = GNUNET_strdup ("");
  pos->udpred = GNUNET_strdup ("");
  pos->next = *base;
  *base = pos;
}


/**
 * Function called for each section in the configuration.
 * Removes those ending in '.gnunet.'.
 *
 * @param cls unused
 * @param section name of a section in the configuration
 */
static void
remove_hosted_service_sections (void *cls, const char *section)
{
  if ((8 > strlen (section)) ||
      (0 != strcmp (".gnunet.", section + ((strlen (section) - 8)))))
    return;
  GNUNET_CONFIGURATION_remove_section (cfg, section);
}


/**
 * Given the list store and the data in it, update the
 * configuration file accordingly.
 *
 * @param tm model to use
 */
static void
update_hosted_service_configuration (GtkTreeModel *tm)
{
  GtkTreeIter iter;
  gchar *name;
  guint srcport;
  gchar *targetaddress;
  gboolean is_udp;
  char *tmp;
  struct HostedServiceInfo *head;
  struct HostedServiceInfo *pos;

  head = NULL;
  GNUNET_CONFIGURATION_iterate_sections (cfg,
                                         &collect_hosted_service_sections,
                                         &head);
  if (gtk_tree_model_get_iter_first (tm, &iter))
    do
    {
      gtk_tree_model_get (tm,
                          &iter,
                          SERVICE_MC_SERVICE_NAME,
                          &name,
                          SERVICE_MC_ISUDP,
                          &is_udp,
                          SERVICE_MC_VISIBLE_PORT,
                          &srcport,
                          SERVICE_MC_DESTINATION_ADDRESS,
                          &targetaddress,
                          -1);
      if (0 != strlen (name))
      {
        pos = head;
        GNUNET_asprintf (&tmp, "%s.gnunet.", name);
        while ((NULL != pos) && (0 != strcasecmp (tmp, pos->section)))
          pos = pos->next;
        if (NULL == pos)
        {
          pos = GNUNET_new (struct HostedServiceInfo);
          pos->section = tmp;
          pos->tcpred = GNUNET_strdup ("");
          pos->udpred = GNUNET_strdup ("");
          pos->next = head;
          head = pos;
        }
        else
        {
          GNUNET_free (tmp);
        }
        GNUNET_asprintf (&tmp,
                         "%u:%s %s",
                         srcport,
                         targetaddress,
                         (is_udp) ? pos->udpred : pos->tcpred);
        if (is_udp)
        {
          GNUNET_free (pos->udpred);
          pos->udpred = tmp;
        }
        else
        {
          GNUNET_free (pos->tcpred);
          pos->tcpred = tmp;
        }
      }
      g_free (name);
      g_free (targetaddress);
    } while (TRUE == gtk_tree_model_iter_next (tm, &iter));
  GNUNET_CONFIGURATION_iterate_sections (cfg,
                                         &remove_hosted_service_sections,
                                         NULL);
  while (NULL != head)
  {
    pos = head;
    head = pos->next;
    if (strlen (pos->udpred) > 0)
      GNUNET_CONFIGURATION_set_value_string (cfg,
                                             pos->section,
                                             "UDP_REDIRECTS",
                                             pos->udpred);
    if (strlen (pos->tcpred) > 0)
      GNUNET_CONFIGURATION_set_value_string (cfg,
                                             pos->section,
                                             "TCP_REDIRECTS",
                                             pos->tcpred);
    GNUNET_free (pos->tcpred);
    GNUNET_free (pos->udpred);
    GNUNET_free (pos->section);
    GNUNET_free (pos);
  }
}


/**
 * The user has edited the DNS name of a service we're offering.
 * Update the GtkTreeModel (at the given path) and update the
 * respective service entry in the configuration file.  Finally,
 * if the edited path is for a "fresh" entry, create another empty
 * one at the bottom.  If the hostname was set to empty, remove
 * the entire entry from the configuration and the model.
 *
 * @param renderer GtkCellRendererText that changed
 * @param path GtkTreePath identifying where in the Model the change is
 * @param new_text the new text that was stored in the line
 * @param user_data NULL
 */
static void
save_hosted_service_name (GtkCellRendererText *renderer,
                          gchar *path,
                          gchar *new_text,
                          gpointer user_data)
{
  GtkTreeModel *tm;
  GtkListStore *ls;
  GtkTreeIter iter;
  gchar *old;

  tm = GTK_TREE_MODEL (
    GNUNET_SETUP_get_object ("GNUNET_setup_hosted_service_liststore"));
  if (NULL == tm)
  {
    GNUNET_break (0);
    return;
  }
  ls = GTK_LIST_STORE (tm);
  if (NULL == ls)
  {
    GNUNET_break (0);
    return;
  }
  if (TRUE != gtk_tree_model_get_iter_from_string (tm, &iter, path))
  {
    GNUNET_break (0);
    return;
  }
  gtk_tree_model_get (tm, &iter, SERVICE_MC_SERVICE_NAME, &old, -1);
  if ((0 != strlen (old)) && (0 == strlen (new_text)))
  {
    /* deletion */
    gtk_list_store_remove (ls, &iter);
    g_free (old);
    /* update configuration */
    update_hosted_service_configuration (tm);
    return;
  }
  /* update model */
  gtk_list_store_set (ls, &iter, SERVICE_MC_SERVICE_NAME, new_text, -1);
  /* update configuration */
  update_hosted_service_configuration (tm);
  if ((0 == strlen (old)) && (0 != strlen (new_text)))
  {
    /* need another empty entry at the end for future expansion */
    add_name_entry_to_list_store (GTK_LIST_STORE (tm), NULL);
  }
  g_free (old);
}


/**
 * Initialize the GtkListModel with the VPN's DNS service specification.
 *
 * @param cls NULL
 * @param section section with the value (NULL)
 * @param option option name (NULL)
 * @param widget widget to initialize (the GtkTreeView)
 * @param cfg configuration handle
 * @return #GNUNET_OK on success, #GNUNET_SYSERR if there was a problem
 */
int
hosted_service_name_install_edited_handler (
  const void *cls,
  const char *section,
  const char *option,
  GObject *widget,
  struct GNUNET_CONFIGURATION_Handle *cfg)
{
  static int once;
  GtkCellRendererText *rt;

  rt = GTK_CELL_RENDERER_TEXT (widget);
  if (NULL == rt)
    return GNUNET_SYSERR;
  if (0 != once++)
    return GNUNET_OK;
  g_signal_connect (rt, "edited", G_CALLBACK (&save_hosted_service_name), NULL);
  return GNUNET_OK;
}


/**
 * The user has edited the DNS name of a service we're offering.
 * Update the GtkTreeModel (at the given path) and update the
 * respective service entry in the configuration file.  Finally,
 * if the edited path is for a "fresh" entry, create another empty
 * one at the bottom.  If the hostname was set to empty, remove
 * the entire entry from the configuration and the model.
 *
 * @param renderer GtkCellRendererToggle that changed
 * @param path GtkTreePath identifying where in the Model the change is
 * @param user_data NULL
 */
static void
save_hosted_service_is_udp (GtkCellRendererToggle *renderer,
                            gchar *path,
                            gpointer user_data)
{
  GtkTreeModel *tm;
  GtkListStore *ls;
  GtkTreeIter iter;
  gboolean is_udp;

  tm = GTK_TREE_MODEL (
    GNUNET_SETUP_get_object ("GNUNET_setup_hosted_service_liststore"));
  if (NULL == tm)
  {
    GNUNET_break (0);
    return;
  }
  ls = GTK_LIST_STORE (tm);
  if (NULL == ls)
  {
    GNUNET_break (0);
    return;
  }
  if (TRUE != gtk_tree_model_get_iter_from_string (tm, &iter, path))
  {
    GNUNET_break (0);
    return;
  }
  /* update model */
  gtk_tree_model_get (tm, &iter, SERVICE_MC_ISUDP, &is_udp, -1);
  gtk_list_store_set (ls, &iter, SERVICE_MC_ISUDP, ! is_udp, -1);
  /* update configuration */
  update_hosted_service_configuration (tm);
}


/**
 * Initialize the GtkListModel with the VPN's DNS service specification.
 *
 * @param cls NULL
 * @param section section with the value (NULL)
 * @param option option name (NULL)
 * @param widget widget to initialize (the GtkTreeView)
 * @param cfg configuration handle
 * @return #GNUNET_OK on success, #GNUNET_SYSERR if there was a problem
 */
int
hosted_service_is_udp_install_toggled_handler (
  const void *cls,
  const char *section,
  const char *option,
  GObject *widget,
  struct GNUNET_CONFIGURATION_Handle *cfg)
{
  static int once;
  GtkCellRendererToggle *rt;

  rt = GTK_CELL_RENDERER_TOGGLE (widget);
  if (NULL == rt)
    return GNUNET_SYSERR;
  if (0 != once++)
    return GNUNET_OK;
  g_signal_connect (rt,
                    "toggled",
                    G_CALLBACK (&save_hosted_service_is_udp),
                    NULL);
  return GNUNET_OK;
}


/**
 * The user has edited the DNS name of a service we're offering.
 * Update the GtkTreeModel (at the given path) and update the
 * respective service entry in the configuration file.  Finally,
 * if the edited path is for a "fresh" entry, create another empty
 * one at the bottom.  If the hostname was set to empty, remove
 * the entire entry from the configuration and the model.
 *
 * @param renderer GtkCellRendererText that changed
 * @param path GtkTreePath identifying where in the Model the change is
 * @param new_text the new text that was stored in the line
 * @param user_data NULL
 */
static void
save_hosted_service_visible_port (GtkCellRendererText *renderer,
                                  gchar *path,
                                  gchar *new_text,
                                  gpointer user_data)
{
  GtkTreeModel *tm;
  GtkListStore *ls;
  GtkTreeIter iter;
  int port;

  if ((1 != sscanf (new_text, "%d", &port)) || (port < 1) ||
      (port > UINT16_MAX))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                _ ("Illegal value `%s' for port\n"),
                new_text);
    return;
  }
  tm = GTK_TREE_MODEL (
    GNUNET_SETUP_get_object ("GNUNET_setup_hosted_service_liststore"));
  if (NULL == tm)
  {
    GNUNET_break (0);
    return;
  }
  ls = GTK_LIST_STORE (tm);
  if (NULL == ls)
  {
    GNUNET_break (0);
    return;
  }
  if (TRUE != gtk_tree_model_get_iter_from_string (tm, &iter, path))
  {
    GNUNET_break (0);
    return;
  }
  /* update model */
  gtk_list_store_set (ls, &iter, SERVICE_MC_VISIBLE_PORT, port, -1);
  /* update configuration */
  update_hosted_service_configuration (tm);
}


/**
 * Initialize the GtkListModel with the VPN's DNS service specification.
 *
 * @param cls NULL
 * @param section section with the value (NULL)
 * @param option option name (NULL)
 * @param widget widget to initialize (the GtkTreeView)
 * @param cfg configuration handle
 * @return #GNUNET_OK on success, #GNUNET_SYSERR if there was a problem
 */
int
hosted_service_visible_port_install_edited_handler (
  const void *cls,
  const char *section,
  const char *option,
  GObject *widget,
  struct GNUNET_CONFIGURATION_Handle *cfg)
{
  static int once;
  GtkCellRendererText *rt;

  rt = GTK_CELL_RENDERER_TEXT (widget);
  if (NULL == rt)
    return GNUNET_SYSERR;
  if (0 != once++)
    return GNUNET_OK;
  g_signal_connect (rt,
                    "edited",
                    G_CALLBACK (&save_hosted_service_visible_port),
                    NULL);
  return GNUNET_OK;
}


/**
 * The user has edited the DNS name of a service we're offering.
 * Update the GtkTreeModel (at the given path) and update the
 * respective service entry in the configuration file.  Finally,
 * if the edited path is for a "fresh" entry, create another empty
 * one at the bottom.  If the hostname was set to empty, remove
 * the entire entry from the configuration and the model.
 *
 * @param renderer GtkCellRendererText that changed
 * @param path GtkTreePath identifying where in the Model the change is
 * @param new_text the new text that was stored in the line
 * @param user_data NULL
 */
static void
save_hosted_service_destination (GtkCellRendererText *renderer,
                                 gchar *path,
                                 gchar *new_text,
                                 gpointer user_data)
{
  GtkTreeModel *tm;
  GtkListStore *ls;
  GtkTreeIter iter;
  struct sockaddr_in v4;
  struct sockaddr_in6 v6;

  if (('[' != new_text[0]) &&
      (GNUNET_OK !=
       GNUNET_STRINGS_to_address_ipv4 (new_text, strlen (new_text), &v4)))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                _ ("Illegal IPv4 destination address `%s'\n"),
                new_text);
    return;
  }
  if (('[' == new_text[0]) &&
      (GNUNET_OK !=
       GNUNET_STRINGS_to_address_ipv6 (new_text, strlen (new_text), &v6)))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                _ ("Illegal IPv6 destination address `%s'\n"),
                new_text);
    return;
  }
  tm = GTK_TREE_MODEL (
    GNUNET_SETUP_get_object ("GNUNET_setup_hosted_service_liststore"));
  if (NULL == tm)
  {
    GNUNET_break (0);
    return;
  }
  ls = GTK_LIST_STORE (tm);
  if (NULL == ls)
  {
    GNUNET_break (0);
    return;
  }
  if (TRUE != gtk_tree_model_get_iter_from_string (tm, &iter, path))
  {
    GNUNET_break (0);
    return;
  }
  /* update model */
  gtk_list_store_set (ls, &iter, SERVICE_MC_DESTINATION_ADDRESS, new_text, -1);
  /* update configuration */
  update_hosted_service_configuration (tm);
}


/**
 * Initialize the GtkListModel with the VPN's DNS service specification.
 *
 * @param cls NULL
 * @param section section with the value (NULL)
 * @param option option name (NULL)
 * @param widget widget to initialize (the GtkTreeView)
 * @param cfg configuration handle
 * @return #GNUNET_OK on success, #GNUNET_SYSERR if there was a problem
 */
int
hosted_service_destination_install_edited_handler (
  const void *cls,
  const char *section,
  const char *option,
  GObject *widget,
  struct GNUNET_CONFIGURATION_Handle *cfg)
{
  static int once;
  GtkCellRendererText *rt;

  rt = GTK_CELL_RENDERER_TEXT (widget);
  if (NULL == rt)
    return GNUNET_SYSERR;
  if (0 != once++)
    return GNUNET_OK;
  g_signal_connect (rt,
                    "edited",
                    G_CALLBACK (&save_hosted_service_destination),
                    NULL);
  return GNUNET_OK;
}


/* end of gnunet-setup-dns-exit.c */