glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / hello / gnunet-hello.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 /**
16  * @file hello/gnunet-hello.c
17  * @brief change HELLO files to never expire
18  * @author Christian Grothoff
19  */
20 #include "platform.h"
21 #include "gnunet_protocols.h"
22 #include "gnunet_hello_lib.h"
23
24 /**
25  * Closure for #add_to_buf().
26  */
27 struct AddContext
28 {
29   /**
30    * Where to add.
31    */
32   char *buf;
33
34   /**
35    * Maximum number of bytes left
36    */
37   size_t max;
38
39   /**
40    * Number of bytes added so far.
41    */
42   size_t ret;
43 };
44
45 static int address_count;
46
47
48 /**
49  * Add the given address with infinit expiration to the buffer.
50  *
51  * @param cls closure
52  * @param address address to add
53  * @param expiration old expiration
54  * @return #GNUNET_OK keep iterating
55  */
56 static int
57 add_to_buf (void *cls,
58             const struct GNUNET_HELLO_Address *address,
59             struct GNUNET_TIME_Absolute expiration)
60 {
61   struct AddContext *ac = cls;
62   size_t ret;
63
64   ret = GNUNET_HELLO_add_address (address,
65                                   GNUNET_TIME_UNIT_FOREVER_ABS,
66                                   ac->buf,
67                                   ac->max);
68   ac->buf += ret;
69   ac->max -= ret;
70   ac->ret += ret;
71   address_count ++;
72   return GNUNET_OK;
73 }
74
75
76 /**
77  * Add addresses from the address list to the HELLO.
78  *
79  * @param cls the HELLO with the addresses to add
80  * @param max maximum space available
81  * @param buf where to add the addresses
82  * @return number of bytes added, 0 to terminate
83  */
84 static ssize_t
85 add_from_hello (void *cls,
86                 size_t max,
87                 void *buf)
88 {
89   struct GNUNET_HELLO_Message **orig = cls;
90   struct AddContext ac;
91
92   if (NULL == *orig)
93     return GNUNET_SYSERR; /* already done */
94   ac.buf = buf;
95   ac.max = max;
96   ac.ret = 0;
97   GNUNET_assert (NULL ==
98                  GNUNET_HELLO_iterate_addresses (*orig,
99                                                  GNUNET_NO, &add_to_buf,
100                                                  &ac));
101   *orig = NULL;
102   return ac.ret;
103 }
104
105
106 int
107 main (int argc, char *argv[])
108 {
109   struct GNUNET_DISK_FileHandle *fh;
110   struct GNUNET_HELLO_Message *orig;
111   struct GNUNET_HELLO_Message *result;
112   struct GNUNET_PeerIdentity pid;
113   uint64_t fsize;
114   address_count = 0;
115
116   GNUNET_log_setup ("gnunet-hello", "INFO", NULL);
117   if (argc != 2)
118   {
119     FPRINTF (stderr,
120              "%s",
121              _("Call with name of HELLO file to modify.\n"));
122     return 1;
123   }
124   if (GNUNET_OK != GNUNET_DISK_file_size (argv[1], &fsize, GNUNET_YES, GNUNET_YES))
125   {
126     FPRINTF (stderr,
127              _("Error accessing file `%s': %s\n"),
128              argv[1],
129              STRERROR (errno));
130     return 1;
131   }
132   if (fsize > 65536)
133   {
134     FPRINTF (stderr,
135              _("File `%s' is too big to be a HELLO\n"),
136              argv[1]);
137     return 1;
138   }
139   if (fsize < sizeof (struct GNUNET_MessageHeader))
140   {
141     FPRINTF (stderr,
142              _("File `%s' is too small to be a HELLO\n"),
143              argv[1]);
144     return 1;
145   }
146   fh = GNUNET_DISK_file_open (argv[1],
147                               GNUNET_DISK_OPEN_READ,
148                               GNUNET_DISK_PERM_USER_READ);
149   if (NULL == fh)
150   {
151     FPRINTF (stderr,
152              _("Error opening file `%s': %s\n"),
153              argv[1],
154              STRERROR (errno));
155     return 1;
156   }
157   {
158     char buf[fsize] GNUNET_ALIGN;
159
160     GNUNET_assert (fsize ==
161                    GNUNET_DISK_file_read (fh, buf, fsize));
162     GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
163     orig = (struct GNUNET_HELLO_Message *) buf;
164     if ( (fsize < GNUNET_HELLO_size (orig)) ||
165          (GNUNET_OK != GNUNET_HELLO_get_id (orig,
166                                             &pid)) )
167     {
168       FPRINTF (stderr,
169                _("Did not find well-formed HELLO in file `%s'\n"),
170                argv[1]);
171       return 1;
172     }
173     {
174       char *pids;
175
176       pids = GNUNET_CRYPTO_eddsa_public_key_to_string (&pid.public_key);
177       fprintf (stdout,
178                "Processing HELLO for peer `%s'\n",
179                pids);
180       GNUNET_free (pids);
181     }
182     result = GNUNET_HELLO_create (&pid.public_key,
183                                   &add_from_hello,
184                                   &orig,
185                                   GNUNET_HELLO_is_friend_only (orig));
186     GNUNET_assert (NULL != result);
187      fh = GNUNET_DISK_file_open (argv[1],
188                                  GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE,
189                                  GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
190      if (NULL == fh)
191      {
192        FPRINTF (stderr,
193                 _("Error opening file `%s': %s\n"),
194                 argv[1],
195                 STRERROR (errno));
196        GNUNET_free (result);
197        return 1;
198      }
199      fsize = GNUNET_HELLO_size (result);
200      if (fsize != GNUNET_DISK_file_write (fh,
201                                           result,
202                                           fsize))
203      {
204        FPRINTF (stderr,
205                 _("Error writing HELLO to file `%s': %s\n"),
206                 argv[1],
207                 STRERROR (errno));
208        (void) GNUNET_DISK_file_close (fh);
209        return 1;
210      }
211     GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
212   }
213   FPRINTF (stderr,
214            _("Modified %u addresses, wrote %u bytes\n"),
215            address_count,
216            (unsigned int) fsize);
217   return 0;
218 }
219
220 /* end of gnunet-hello.c */