795f4f29b0aaba0e3cd11ca247fa05cd67bf224e
[oweals/gnunet.git] / src / hello / gnunet-hello.c
1 /*
2      This file is part of GNUnet
3      (C) 2012 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20 /**
21  * @file hello/gnunet-hello.c
22  * @brief change HELLO files to never expire
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include "gnunet_protocols.h"
27 #include "gnunet_hello_lib.h"
28
29 /**
30  * Closure for 'add_to_buf'.
31  */
32 struct AddContext
33 {
34   /**
35    * Where to add.
36    */
37   char *buf;
38
39   /**
40    * Maximum number of bytes left
41    */
42   size_t max;
43
44   /**
45    * Number of bytes added so far.
46    */
47   size_t ret;
48 };
49
50 static int address_count;
51
52
53 /**
54  * Add the given address with infinit expiration to the buffer.
55  *
56  * @param cls closure
57  * @param address address to add
58  * @param expiration old expiration
59  * @return GNUNET_OK keep iterating
60  */
61 static int
62 add_to_buf (void *cls, const struct GNUNET_HELLO_Address *address,
63             struct GNUNET_TIME_Absolute expiration)
64 {
65   struct AddContext *ac = cls;
66   size_t ret;
67
68   ret = GNUNET_HELLO_add_address (address,
69                                   GNUNET_TIME_UNIT_FOREVER_ABS,
70                                   ac->buf,
71                                   ac->max);
72   ac->buf += ret;
73   ac->max -= ret;
74   ac->ret += ret;
75   address_count ++;
76   return GNUNET_OK;
77 }
78
79
80 /**
81  * Add addresses from the address list to the HELLO.
82  *
83  * @param cls the HELLO with the addresses to add
84  * @param max maximum space available
85  * @param buf where to add the addresses
86  * @return number of bytes added, 0 to terminate
87  */
88 static size_t
89 add_from_hello (void *cls, size_t max, void *buf)
90 {
91   struct GNUNET_HELLO_Message **orig = cls;
92   struct AddContext ac;
93
94   if (NULL == *orig)
95     return 0; /* already done */
96   ac.buf = buf;
97   ac.max = max;
98   ac.ret = 0;
99   GNUNET_assert (NULL ==
100                  GNUNET_HELLO_iterate_addresses (*orig,
101                                                  GNUNET_NO, &add_to_buf,
102                                                  &ac));
103   *orig = NULL;
104   return ac.ret;
105 }
106
107
108 int
109 main (int argc, char *argv[])
110 {
111   struct GNUNET_DISK_FileHandle *fh;
112   struct GNUNET_HELLO_Message *orig;
113   struct GNUNET_HELLO_Message *result;
114   struct GNUNET_CRYPTO_EccPublicSignKey pk;
115   uint64_t fsize;
116   address_count = 0;
117
118   GNUNET_log_setup ("gnunet-hello", "INFO", NULL);
119   if (argc != 2)
120   {
121     FPRINTF (stderr,
122              "%s",
123              _("Call with name of HELLO file to modify.\n"));
124     return 1;
125   }
126   if (GNUNET_OK != GNUNET_DISK_file_size (argv[1], &fsize, GNUNET_YES, GNUNET_YES))
127   {
128     FPRINTF (stderr,
129              _("Error accessing file `%s': %s\n"),
130              argv[1],
131              STRERROR (errno));
132     return 1;
133   }
134   if (fsize > 65536)
135   {
136     FPRINTF (stderr,
137              _("File `%s' is too big to be a HELLO\n"),
138              argv[1]);
139     return 1;
140   }
141   if (fsize < sizeof (struct GNUNET_MessageHeader))
142   {
143     FPRINTF (stderr,
144              _("File `%s' is too small to be a HELLO\n"),
145              argv[1]);
146     return 1;
147   }
148   fh = GNUNET_DISK_file_open (argv[1],
149                               GNUNET_DISK_OPEN_READ,
150                               GNUNET_DISK_PERM_USER_READ);
151   if (NULL == fh)
152   {
153     FPRINTF (stderr,
154              _("Error opening file `%s': %s\n"),
155              argv[1],
156              STRERROR (errno));
157     return 1;
158   }
159   {
160     char buf[fsize] GNUNET_ALIGN;
161
162     GNUNET_assert (fsize ==
163                    GNUNET_DISK_file_read (fh, buf, fsize));
164     GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
165     orig = (struct GNUNET_HELLO_Message *) buf;
166     if ( (fsize != GNUNET_HELLO_size (orig)) ||
167          (GNUNET_OK != GNUNET_HELLO_get_key (orig, &pk)) )
168     {
169       FPRINTF (stderr,
170                _("Did not find well-formed HELLO in file `%s'\n"),
171                argv[1]);
172       return 1;
173     }
174     result = GNUNET_HELLO_create (&pk, &add_from_hello, &orig,
175                 GNUNET_HELLO_is_friend_only (orig));
176     GNUNET_assert (NULL != result);
177      fh = GNUNET_DISK_file_open (argv[1],
178                                  GNUNET_DISK_OPEN_WRITE,
179                                  GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
180      if (NULL == fh)
181      {
182        FPRINTF (stderr,
183                 _("Error opening file `%s': %s\n"),
184                 argv[1],
185                 STRERROR (errno));
186        GNUNET_free (result);
187        return 1;
188      }
189      fsize = GNUNET_HELLO_size (result);
190      if (fsize != GNUNET_DISK_file_write (fh,
191                                           result,
192                                           fsize))
193      {
194        FPRINTF (stderr,
195                 _("Error writing HELLO to file `%s': %s\n"),
196                 argv[1],
197                 STRERROR (errno));
198        (void) GNUNET_DISK_file_close (fh);
199        return 1;
200      }
201     GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
202   }
203   FPRINTF (stderr, _("Modified %u addresses \n"), address_count);
204   return 0;
205 }
206
207 /* end of gnunet-hello.c */