paragraph for gnunet devs that don't know how to use the web
[oweals/gnunet.git] / src / gns / nss / nss_gns_query.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2012 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include <string.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include "nss_gns_query.h"
22 #include <arpa/inet.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26
27
28 /**
29  * Wrapper function that uses gnunet-gns cli tool to resolve
30  * an IPv4/6 address.
31  *
32  * @param af address family
33  * @param name the name to resolve
34  * @param u the userdata (result struct)
35  * @return -1 on error else 0
36  */
37 int
38 gns_resolve_name (int af,
39                   const char *name,
40                   struct userdata *u)
41 {
42   FILE *p;
43   char *cmd;
44   char line[128];
45   int ret;
46
47   if (AF_INET6 == af)
48   {
49     if (-1 == asprintf (&cmd,
50                         "%s -t AAAA -u %s\n",
51                         "gnunet-gns -r", name))
52       return -1;
53   }
54   else
55   {
56     if (-1 == asprintf (&cmd,
57                         "%s %s\n",
58                         "gnunet-gns -r -u", name))
59       return -1;
60   }
61   if (NULL == (p = popen (cmd, "r")))
62   {
63     free (cmd);
64     return -1;
65   }
66   while (NULL != fgets (line, sizeof(line), p))
67   {
68     if (u->count >= MAX_ENTRIES)
69       break;
70     if (line[strlen(line)-1] == '\n')
71     {
72       line[strlen(line)-1] = '\0';
73       if (AF_INET == af)
74       {
75         if (inet_pton(af, line, &(u->data.ipv4[u->count])))
76         {
77           u->count++;
78           u->data_len += sizeof(ipv4_address_t);
79         }
80         else
81         {
82           pclose (p);
83           free (cmd);
84           return -1;
85         }
86       }
87       else if (AF_INET6 == af)
88       {
89         if (inet_pton(af, line, &(u->data.ipv6[u->count])))
90         {
91           u->count++;
92           u->data_len += sizeof(ipv6_address_t);
93         }
94         else
95         {
96           pclose (p);
97           free (cmd);
98           return -1;
99         }
100       }
101     }
102   }
103   ret = pclose (p);
104   free (cmd);
105   if (4 == ret)
106     return -2; /* not for GNS */
107   if (3 == ret)
108     return -3; /* timeout */
109   return 0;
110 }
111 /* end of nss_gns_query.c */