output public key of peer
[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,
63             const struct GNUNET_HELLO_Address *address,
64             struct GNUNET_TIME_Absolute expiration)
65 {
66   struct AddContext *ac = cls;
67   size_t ret;
68
69   ret = GNUNET_HELLO_add_address (address,
70                                   GNUNET_TIME_UNIT_FOREVER_ABS,
71                                   ac->buf,
72                                   ac->max);
73   ac->buf += ret;
74   ac->max -= ret;
75   ac->ret += ret;
76   address_count ++;
77   return GNUNET_OK;
78 }
79
80
81 /**
82  * Add addresses from the address list to the HELLO.
83  *
84  * @param cls the HELLO with the addresses to add
85  * @param max maximum space available
86  * @param buf where to add the addresses
87  * @return number of bytes added, 0 to terminate
88  */
89 static ssize_t
90 add_from_hello (void *cls,
91                 size_t max,
92                 void *buf)
93 {
94   struct GNUNET_HELLO_Message **orig = cls;
95   struct AddContext ac;
96
97   if (NULL == *orig)
98     return GNUNET_SYSERR; /* already done */
99   ac.buf = buf;
100   ac.max = max;
101   ac.ret = 0;
102   GNUNET_assert (NULL ==
103                  GNUNET_HELLO_iterate_addresses (*orig,
104                                                  GNUNET_NO, &add_to_buf,
105                                                  &ac));
106   *orig = NULL;
107   return ac.ret;
108 }
109
110
111 int
112 main (int argc, char *argv[])
113 {
114   struct GNUNET_DISK_FileHandle *fh;
115   struct GNUNET_HELLO_Message *orig;
116   struct GNUNET_HELLO_Message *result;
117   struct GNUNET_CRYPTO_EddsaPublicKey pk;
118   uint64_t fsize;
119   address_count = 0;
120
121   GNUNET_log_setup ("gnunet-hello", "INFO", NULL);
122   if (argc != 2)
123   {
124     FPRINTF (stderr,
125              "%s",
126              _("Call with name of HELLO file to modify.\n"));
127     return 1;
128   }
129   if (GNUNET_OK != GNUNET_DISK_file_size (argv[1], &fsize, GNUNET_YES, GNUNET_YES))
130   {
131     FPRINTF (stderr,
132              _("Error accessing file `%s': %s\n"),
133              argv[1],
134              STRERROR (errno));
135     return 1;
136   }
137   if (fsize > 65536)
138   {
139     FPRINTF (stderr,
140              _("File `%s' is too big to be a HELLO\n"),
141              argv[1]);
142     return 1;
143   }
144   if (fsize < sizeof (struct GNUNET_MessageHeader))
145   {
146     FPRINTF (stderr,
147              _("File `%s' is too small to be a HELLO\n"),
148              argv[1]);
149     return 1;
150   }
151   fh = GNUNET_DISK_file_open (argv[1],
152                               GNUNET_DISK_OPEN_READ,
153                               GNUNET_DISK_PERM_USER_READ);
154   if (NULL == fh)
155   {
156     FPRINTF (stderr,
157              _("Error opening file `%s': %s\n"),
158              argv[1],
159              STRERROR (errno));
160     return 1;
161   }
162   {
163     char buf[fsize] GNUNET_ALIGN;
164
165     GNUNET_assert (fsize ==
166                    GNUNET_DISK_file_read (fh, buf, fsize));
167     GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
168     orig = (struct GNUNET_HELLO_Message *) buf;
169     if ( (fsize != GNUNET_HELLO_size (orig)) ||
170          (GNUNET_OK != GNUNET_HELLO_get_key (orig, &pk)) )
171     {
172       FPRINTF (stderr,
173                _("Did not find well-formed HELLO in file `%s'\n"),
174                argv[1]);
175       return 1;
176     }
177     {
178       char *pid;
179
180       pid = GNUNET_CRYPTO_eddsa_public_key_to_string (&pk);
181       fprintf (stdout,
182                "Processing HELLO for peer `%s'\n",
183                pid);
184       GNUNET_free (pid);
185     }
186     result = GNUNET_HELLO_create (&pk, &add_from_hello,
187                                   &orig,
188                                   GNUNET_HELLO_is_friend_only (orig));
189     GNUNET_assert (NULL != result);
190      fh = GNUNET_DISK_file_open (argv[1],
191                                  GNUNET_DISK_OPEN_WRITE,
192                                  GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
193      if (NULL == fh)
194      {
195        FPRINTF (stderr,
196                 _("Error opening file `%s': %s\n"),
197                 argv[1],
198                 STRERROR (errno));
199        GNUNET_free (result);
200        return 1;
201      }
202      fsize = GNUNET_HELLO_size (result);
203      if (fsize != GNUNET_DISK_file_write (fh,
204                                           result,
205                                           fsize))
206      {
207        FPRINTF (stderr,
208                 _("Error writing HELLO to file `%s': %s\n"),
209                 argv[1],
210                 STRERROR (errno));
211        (void) GNUNET_DISK_file_close (fh);
212        return 1;
213      }
214     GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
215   }
216   FPRINTF (stderr, _("Modified %u addresses \n"), address_count);
217   return 0;
218 }
219
220 /* end of gnunet-hello.c */