aboutsummaryrefslogtreecommitdiff
path: root/src/gns/gnocksy/gns_glue.c
blob: 54e6916fc2d9613db8577ec5dd65a277abdff35a (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
#include <stdio.h>
#include <string.h>

/*
 * Glue function to return the authoritative part
 * of a name. i.e. the site of origin
 *
 * @param name the name to process
 * @param auth pointer where the result is stored
 * @return 0 on success < 0 on failure
 */
int
gns_glue_get_auth ( char* name, char* auth )
{
  char cmd[1024];
  char line[1024];
  FILE *p;

  sprintf (cmd, "%s %s", "gnunet-gns -a", name);

  p = popen(cmd, "r");

  if (p != NULL)
  {
    while (fgets (line, sizeof(line), p) != NULL)
    {
      if (line[strlen(line)-1] == '\n')
      {
        line[strlen(line)-1] = '\0';
        strcpy (auth, line);
        return 0;
      }
    }

  }

  fclose (p);

  return -1;
}

/*
 * Glue function to return the short version of
 * a given name
 *
 * @param name the name to shorten
 * @param shortened pointer where the result will be stored
 * @return 0 on success < 0 on failure
 */
int
gns_glue_shorten ( char* name, char* shortened )
{
  char cmd[1024];
  char line[1024];
  FILE *p;

  sprintf (cmd, "%s %s", "gnunet-gns -r -s", name);

  p = popen(cmd, "r");

  if (p != NULL)
  {
    while (fgets (line, sizeof(line), p) != NULL)
    {
      if (line[strlen(line)-1] == '\n')
      {
        line[strlen(line)-1] = '\0';
        strcpy (shortened, line);
        return 0;
      }
    }

  }

  fclose (p);

  return -1;
}


/*
 * Glue function to expand .+ urls and shorted the
 * resulting name
 *
 * @param to_expand the .+ name to expand
 * @param host the site of origin
 * @param shortened the expanded and shortened result pointer
 */
int
gns_glue_expand_and_shorten( char* to_expand, char* host, char* shortened )
{
  char cmd[1024];
  char line[1024];
  FILE *p;
  char sorig[256];
  char expanded[256];

  sprintf (shortened, "%s%s", to_expand, host); //TODO this is a mockup
  return 0;

  sprintf (cmd, "%s %s", "gnunet-gns -a", host);

  p = popen(cmd, "r");

  if (p != NULL)
  {
    while (fgets (line, sizeof(line), p) != NULL)
    {
      if (line[strlen(line)-1] == '\n')
      {
        line[strlen(line)-1] = '\0';
        strcpy (sorig, line);
        return 0;
      }
    }

  }

  fclose (p);

  sprintf (expanded, "%s.%s", to_expand, sorig);
  
  sprintf (cmd, "%s %s", "gnunet-gns -r -s", expanded);
 
  p = popen(cmd, "r");

  if (p != NULL)
  {
    while (fgets (line, sizeof(line), p) != NULL)
    {
      if (line[strlen(line)-1] == '\n')
      {
        line[strlen(line)-1] = '\0';
        strcpy (shortened, line);
        return 0;
      }
    }

  }

  fclose (p);

  return -1;
}