From Guy Martin:
[obnox/wireshark/wip.git] / update.c
1 /* update.c
2  *
3  * $Id$
4  *
5  * Wireshark - Network traffic analyzer
6  * By Gerald Combs <gerald@wireshark.org>
7  * Copyright 1998 Gerald Combs
8  *
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <glib.h>
30 #include <string.h>
31 #include <stdio.h>
32
33 #include <epan/prefs.h>
34 #include <epan/prefs-int.h>
35 #include <epan/filesystem.h>
36
37 #include "simple_dialog.h"
38 #include "version_info.h"
39
40 #ifdef HAVE_LIBPCAP
41 #include "capture-pcap-util.h"
42 #endif
43
44 #include <wsutil/file_util.h>
45
46 #include <wininet.h>
47 #include "nio-ie5.h"
48
49
50 /* update information about a single component */
51 typedef struct update_info_s {
52     char *prefix;                   /* prefix of the update file keys */
53     gboolean needs_update;          /* does this component need an update */
54     char *version_installed;        /* the version currently installed */
55
56     char *title;                    /* the component title (name) */
57     char *description;              /* description of the component */
58     char *version_recommended;      /* the version recommended */
59     char *url;                      /* the URL for an update */
60     char *md5;                      /* md5 checksum for that update */
61     char *size;                     /* size of that update */
62 } update_info_t;
63
64
65 /* download a complete file from the internet */
66 int
67 download_file(const char *url, const char *filename) {
68     netio_ie5_t * conn;
69     char buf[100];
70     int chunk_len;
71     int fd;
72     int stream_len;
73     int ret = 0;
74
75
76     /* open output file */
77     fd = ws_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
78     if(fd == -1) {
79         g_warning("Couldn't open output file %s!", filename);
80         return -1;
81     }
82
83     /* connect to url */
84     conn = netio_ie5_connect (url);
85     if (conn == NULL) {
86         g_warning("Couldn't connect to %s!", url);
87         return -1;
88     }
89
90     do {
91                 /* XXX - maybe add a progress bar here */
92
93         /* read some bytes from the url */
94         chunk_len = netio_ie5_read (conn, buf, sizeof(buf));
95
96         /* write bytes to the output file */
97         stream_len = ws_write( fd, buf, chunk_len);
98         if(stream_len != chunk_len) {
99             g_warning("output failed: stream_len %u != chunk_len %u", stream_len, chunk_len);
100             ret = -1;
101             break;
102         }
103     } while(chunk_len > 0);
104
105     netio_ie5_disconnect(conn);
106
107     ws_close(fd);
108
109     return ret;
110 }
111
112 update_info_t *
113 update_info_new(void)
114 {
115     return g_malloc0(sizeof(update_info_t));
116 }
117
118 void
119 update_info_delete(update_info_t *update_info)
120 {
121     g_free(update_info->prefix);
122     g_free(update_info->version_installed);
123     g_free(update_info->title);
124     g_free(update_info->description);
125     g_free(update_info->version_recommended);
126     g_free(update_info->url);
127     g_free(update_info->md5);
128     g_free(update_info->size);
129
130     g_free(update_info);
131 }
132
133 /* check a single key value pair */
134 static void
135 update_pref_check(gchar *pref_name, gchar *value, char *check_prefix, char *check_name, char **check_value)
136 {
137     GString *check = g_string_new(check_prefix);
138
139     g_string_append(check, check_name);
140
141     if(strcmp(pref_name, check->str) == 0) {
142         if(*check_value)
143             /* there shouldn't be a duplicate entry in the update file */
144             g_warning("Duplicate of %s: current %s former %s", pref_name, value, *check_value);
145         else
146             *check_value = g_strdup(value);
147     }
148
149     g_string_free(check, TRUE);
150 }
151
152 /* a new key value pair from the update file */
153 static prefs_set_pref_e
154 update_pref(gchar *pref_name, gchar *value, void *private_data)
155 {
156     update_info_t *update_info = private_data;
157
158     update_pref_check(pref_name, value, update_info->prefix, "title",       &update_info->title);
159     update_pref_check(pref_name, value, update_info->prefix, "description", &update_info->description);
160     update_pref_check(pref_name, value, update_info->prefix, "version",     &update_info->version_recommended);
161     update_pref_check(pref_name, value, update_info->prefix, "update.url",  &update_info->url);
162     update_pref_check(pref_name, value, update_info->prefix, "update.md5",  &update_info->md5);
163     update_pref_check(pref_name, value, update_info->prefix, "update.size",  &update_info->size);
164
165     return PREFS_SET_OK;
166 }
167
168 /* display an update_info */
169 static void
170 update_info_display(update_info_t *update_info)
171 {
172     GString *overview;
173
174
175     overview = g_string_new("");
176
177     if(update_info->title) {
178         g_string_append_printf(overview, "%s%s%s",
179             simple_dialog_primary_start(), update_info->title, simple_dialog_primary_end());
180     } else {
181         g_string_append_printf(overview, "%sComponent%s",
182             simple_dialog_primary_start(), simple_dialog_primary_end());
183     }
184
185     g_string_append(overview, "\n\n");
186
187     if(update_info->description)
188         g_string_append_printf(overview, "%s\n\n", update_info->description);
189
190     g_string_append_printf(overview, "Installed: %s\n", update_info->version_installed);
191
192     if(update_info->version_recommended)
193         g_string_append_printf(overview, "Recommended: %s\n", update_info->version_recommended);
194     else
195         g_string_append(overview, "Recommenced: unknown\n");
196
197     if(update_info->version_recommended && update_info->url)
198         g_string_append_printf(overview, "From: %s\n", update_info->url);
199
200     if(update_info->size)
201         g_string_append_printf(overview, "Size: %s", update_info->size);
202
203     simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK, overview->str);
204
205     g_string_free(overview, TRUE);
206
207 }
208
209 /* check the version of the wireshark program */
210 static update_info_t *
211 update_check_wireshark(const char *local_file)
212 {
213     FILE *pf;
214     update_info_t *update_info = update_info_new();
215
216
217     update_info->version_installed = g_strdup(VERSION);
218     update_info->prefix = "wireshark.setup.";
219
220     pf = ws_fopen(local_file, "r");
221     if(pf != NULL) {
222         /* read in update_info of Wireshark */
223         read_prefs_file(local_file, pf, update_pref, update_info);
224         fclose(pf);
225
226         /* check if Wireshark needs an update */
227         if(update_info->version_installed && update_info->version_recommended &&
228             strcmp(update_info->version_installed, update_info->version_recommended) != 0)
229         {
230             update_info->needs_update = TRUE;
231         }
232     } else {
233         g_warning("Could not open %s", local_file);
234     }
235
236     return update_info;
237 }
238
239 /* check the version of winpcap */
240 static update_info_t *
241 update_check_winpcap(const char *local_file)
242 {
243     FILE *pf;
244     update_info_t * update_info = update_info_new();
245     GString *pcap_version_tmp;
246     char *pcap_version = NULL;
247     char *pcap_vstart;
248     char *pcap_vend;
249
250
251     update_info->prefix = "winpcap.";
252
253     pf = ws_fopen(local_file, "r");
254     if(pf != NULL) {
255         /* read in update_info of WinPcap */
256         read_prefs_file(local_file, pf, update_pref, update_info);
257         fclose(pf);
258
259         /* get WinPcap version */
260         /* XXX - what's the "approved" method to get the WinPcap version? */
261         pcap_version_tmp = g_string_new("");
262         get_runtime_pcap_version(pcap_version_tmp);
263
264         /* cut out real version from "combined" version string */
265         pcap_vstart = strstr(pcap_version_tmp->str, "with WinPcap version ");
266         if(pcap_vstart != NULL) {
267             pcap_vstart += sizeof("with WinPcap version");
268             pcap_vend = strstr(pcap_vstart, " ");
269             if(pcap_vend != NULL) {
270                 pcap_vend[0] = 0;
271                 pcap_version = g_strdup(pcap_vstart);
272             }
273         }
274
275         update_info->version_installed = g_strdup(pcap_version);
276
277         if(pcap_version && update_info->version_recommended &&
278             strcmp(pcap_version, update_info->version_recommended) != 0)
279         {
280             update_info->needs_update = TRUE;
281         }
282     } else {
283         g_warning("Could not open %s", local_file);
284     }
285
286     g_string_free(pcap_version_tmp, TRUE);
287     g_free(pcap_version);
288
289     return update_info;
290 }
291
292
293 /* check for all updates */
294 void
295 update_check(gboolean interactive)
296 {
297     char *local_file;
298     const char *url_file = "http://127.0.0.1/wsupdate"; /* XXX - build the URL depending on platform, versions, ... */
299     update_info_t *update_info_wireshark;
300     update_info_t *update_info_winpcap;
301
302
303     /* build update file name */
304     /* XXX - using the personal path, use temp dir instead? */
305     local_file = get_persconffile_path("wsupdate", FALSE, TRUE /*for_writing*/);
306     if(local_file == NULL) {
307         g_warning("Couldn't create output path!");
308         return;
309     }
310
311     /* download update file */
312     if(download_file(url_file, local_file) == -1) {
313         g_warning("Couldn't download update file: %s", local_file);
314         g_free(local_file);
315         return;
316     }
317
318     /* check wireshark */
319     update_info_wireshark = update_check_wireshark(local_file);
320
321     /* check winpcap */
322     update_info_winpcap = update_check_winpcap(local_file);
323
324     /* display results */
325     if(update_info_wireshark->needs_update || update_info_winpcap->needs_update) {
326         if(update_info_wireshark->needs_update)
327             update_info_display(update_info_wireshark);
328         if(update_info_winpcap->needs_update)
329             update_info_display(update_info_winpcap);
330     } else {
331         if(interactive) {
332             simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK, "No updates available");
333         }
334     }
335
336     /* cleanup */
337     update_info_delete(update_info_wireshark);
338     update_info_delete(update_info_winpcap);
339
340     g_free(local_file);
341 }
342