switching to ECDHE cryptography f, implementation is incomplete and INSECURE, do...
[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_hello_lib.h"
27
28 /**
29  * Closure for 'add_to_buf'.
30  */
31 struct AddContext
32 {
33   /**
34    * Where to add.
35    */
36   char *buf;
37   
38   /**
39    * Maximum number of bytes left
40    */
41   size_t max;
42
43   /**
44    * Number of bytes added so far.
45    */
46   size_t ret;
47 };
48
49
50 /**
51  * Add the given address with infinit expiration to the buffer.
52  *
53  * @param cls closure
54  * @param address address to add
55  * @param expiration old expiration
56  * @return GNUNET_OK keep iterating
57  */
58 static int
59 add_to_buf (void *cls, const struct GNUNET_HELLO_Address *address,
60             struct GNUNET_TIME_Absolute expiration)
61 {
62   struct AddContext *ac = cls;
63   size_t ret;
64
65   ret = GNUNET_HELLO_add_address (address, 
66                                   GNUNET_TIME_UNIT_FOREVER_ABS,
67                                   ac->buf,
68                                   ac->max);
69   ac->buf += ret;
70   ac->max -= ret;
71   ac->ret += ret;  
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 size_t
85 add_from_hello (void *cls, size_t max, void *buf)
86 {
87   struct GNUNET_HELLO_Message **orig = cls;
88   struct AddContext ac;
89
90   if (NULL == *orig)
91     return 0; /* already done */
92   ac.buf = buf;
93   ac.max = max;
94   ac.ret = 0;
95   GNUNET_assert (NULL ==
96                  GNUNET_HELLO_iterate_addresses (*orig, 
97                                                  GNUNET_NO, &add_to_buf,
98                                                  &ac));
99   *orig = NULL;
100   return ac.ret;
101 }
102
103
104 int
105 main (int argc, char *argv[])
106 {
107   struct GNUNET_DISK_FileHandle *fh;
108   struct GNUNET_HELLO_Message *orig;
109   struct GNUNET_HELLO_Message *result;
110   struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pk;
111   uint64_t fsize;
112
113   GNUNET_log_setup ("gnunet-hello", "INFO", NULL);
114   if (argc != 2)
115   {
116     FPRINTF (stderr,
117              "%s",
118              _("Call with name of HELLO file to modify.\n"));
119     return 1;
120   }
121   if (GNUNET_OK != GNUNET_DISK_file_size (argv[1], &fsize, GNUNET_YES, GNUNET_YES))
122   {
123     FPRINTF (stderr,
124              _("Error accessing file `%s': %s\n"),
125              argv[1],
126              STRERROR (errno));
127     return 1;
128   }
129   if (fsize > 65536)
130   {
131     FPRINTF (stderr,
132              _("File `%s' is too big to be a HELLO\n"),
133              argv[1]);
134     return 1;
135   }
136   if (fsize < sizeof (struct GNUNET_MessageHeader))
137   {
138     FPRINTF (stderr,
139              _("File `%s' is too small to be a HELLO\n"),
140              argv[1]);
141     return 1;
142   }
143   fh = GNUNET_DISK_file_open (argv[1], 
144                               GNUNET_DISK_OPEN_READ,
145                               GNUNET_DISK_PERM_USER_READ);
146   if (NULL == fh)
147   {
148     FPRINTF (stderr,
149              _("Error opening file `%s': %s\n"),
150              argv[1],
151              STRERROR (errno));
152     return 1;
153   }
154   {
155     char buf[fsize] GNUNET_ALIGN;
156     
157     GNUNET_assert (fsize == 
158                    GNUNET_DISK_file_read (fh, buf, fsize));
159     GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
160     orig = (struct GNUNET_HELLO_Message *) buf;
161     if ( (fsize != GNUNET_HELLO_size (orig)) ||
162          (GNUNET_OK != GNUNET_HELLO_get_key (orig, &pk)) )
163     {
164       FPRINTF (stderr,
165                _("Did not find well-formed HELLO in file `%s'\n"),
166                argv[1]);
167       return 1;
168     }
169     result = GNUNET_HELLO_create (&pk, &add_from_hello, &orig);
170     GNUNET_assert (NULL != result);
171      fh = GNUNET_DISK_file_open (argv[1], 
172                                  GNUNET_DISK_OPEN_WRITE,
173                                  GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
174      if (NULL == fh)
175      {
176        FPRINTF (stderr,
177                 _("Error opening file `%s': %s\n"),
178                 argv[1],
179                 STRERROR (errno));
180        GNUNET_free (result);
181        return 1;
182      }
183      fsize = GNUNET_HELLO_size (result);
184      if (fsize != GNUNET_DISK_file_write (fh,
185                                           result,
186                                           fsize))
187      {
188        FPRINTF (stderr,
189                 _("Error writing HELLO to file `%s': %s\n"),
190                 argv[1],
191                 STRERROR (errno));
192        (void) GNUNET_DISK_file_close (fh);
193        return 1;
194      }
195     GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
196   }
197   return 0;
198 }
199
200 /* end of gnunet-hello.c */