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