Don't register to udp/tcp port 0, but use dissector_add_handle() instead.
[metze/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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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         ws_close(fd);
87         g_warning("Couldn't connect to %s!", url);
88         return -1;
89     }
90
91     do {
92                 /* XXX - maybe add a progress bar here */
93
94         /* read some bytes from the url */
95         chunk_len = netio_ie5_read (conn, buf, sizeof(buf));
96
97         /* write bytes to the output file */
98         stream_len = ws_write( fd, buf, chunk_len);
99         if(stream_len != chunk_len) {
100             g_warning("output failed: stream_len %u != chunk_len %u", stream_len, chunk_len);
101             ret = -1;
102             break;
103         }
104     } while(chunk_len > 0);
105
106     netio_ie5_disconnect(conn);
107
108     ws_close(fd);
109
110     return ret;
111 }
112
113 update_info_t *
114 update_info_new(void)
115 {
116     return g_malloc0(sizeof(update_info_t));
117 }
118
119 void
120 update_info_delete(update_info_t *update_info)
121 {
122     g_free(update_info->prefix);
123     g_free(update_info->version_installed);
124     g_free(update_info->title);
125     g_free(update_info->description);
126     g_free(update_info->version_recommended);
127     g_free(update_info->url);
128     g_free(update_info->md5);
129     g_free(update_info->size);
130
131     g_free(update_info);
132 }
133
134 /* check a single key value pair */
135 static void
136 update_pref_check(gchar *pref_name, gchar *value, char *check_prefix, char *check_name, char **check_value)
137 {
138     GString *check = g_string_new(check_prefix);
139
140     g_string_append(check, check_name);
141
142     if(strcmp(pref_name, check->str) == 0) {
143         if(*check_value)
144             /* there shouldn't be a duplicate entry in the update file */
145             g_warning("Duplicate of %s: current %s former %s", pref_name, value, *check_value);
146         else
147             *check_value = g_strdup(value);
148     }
149
150     g_string_free(check, TRUE);
151 }
152
153 /* a new key value pair from the update file */
154 static prefs_set_pref_e
155 update_pref(gchar *pref_name, gchar *value, void *private_data)
156 {
157     update_info_t *update_info = private_data;
158
159     update_pref_check(pref_name, value, update_info->prefix, "title",       &update_info->title);
160     update_pref_check(pref_name, value, update_info->prefix, "description", &update_info->description);
161     update_pref_check(pref_name, value, update_info->prefix, "version",     &update_info->version_recommended);
162     update_pref_check(pref_name, value, update_info->prefix, "update.url",  &update_info->url);
163     update_pref_check(pref_name, value, update_info->prefix, "update.md5",  &update_info->md5);
164     update_pref_check(pref_name, value, update_info->prefix, "update.size",  &update_info->size);
165
166     return PREFS_SET_OK;
167 }
168
169 /* display an update_info */
170 static void
171 update_info_display(update_info_t *update_info)
172 {
173     GString *overview;
174
175
176     overview = g_string_new("");
177
178     if(update_info->title) {
179         g_string_append_printf(overview, "%s%s%s",
180             simple_dialog_primary_start(), update_info->title, simple_dialog_primary_end());
181     } else {
182         g_string_append_printf(overview, "%sComponent%s",
183             simple_dialog_primary_start(), simple_dialog_primary_end());
184     }
185
186     g_string_append(overview, "\n\n");
187
188     if(update_info->description)
189         g_string_append_printf(overview, "%s\n\n", update_info->description);
190
191     g_string_append_printf(overview, "Installed: %s\n", update_info->version_installed);
192
193     if(update_info->version_recommended)
194         g_string_append_printf(overview, "Recommended: %s\n", update_info->version_recommended);
195     else
196         g_string_append(overview, "Recommenced: unknown\n");
197
198     if(update_info->version_recommended && update_info->url)
199         g_string_append_printf(overview, "From: %s\n", update_info->url);
200
201     if(update_info->size)
202         g_string_append_printf(overview, "Size: %s", update_info->size);
203
204     simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK, overview->str);
205
206     g_string_free(overview, TRUE);
207
208 }
209
210 /* check the version of the wireshark program */
211 static update_info_t *
212 update_check_wireshark(const char *local_file)
213 {
214     FILE *pf;
215     update_info_t *update_info = update_info_new();
216
217
218     update_info->version_installed = g_strdup(VERSION);
219     update_info->prefix = "wireshark.setup.";
220
221     pf = ws_fopen(local_file, "r");
222     if(pf != NULL) {
223         /* read in update_info of Wireshark */
224         read_prefs_file(local_file, pf, update_pref, update_info);
225         fclose(pf);
226
227         /* check if Wireshark needs an update */
228         if(update_info->version_installed && update_info->version_recommended &&
229             strcmp(update_info->version_installed, update_info->version_recommended) != 0)
230         {
231             update_info->needs_update = TRUE;
232         }
233     } else {
234         g_warning("Could not open %s", local_file);
235     }
236
237     return update_info;
238 }
239
240 /* check the version of winpcap */
241 static update_info_t *
242 update_check_winpcap(const char *local_file)
243 {
244     FILE *pf;
245     update_info_t * update_info = update_info_new();
246     GString *pcap_version_tmp;
247     char *pcap_version = NULL;
248     char *pcap_vstart;
249     char *pcap_vend;
250
251
252     update_info->prefix = "winpcap.";
253
254     pf = ws_fopen(local_file, "r");
255     if(pf != NULL) {
256         /* read in update_info of WinPcap */
257         read_prefs_file(local_file, pf, update_pref, update_info);
258         fclose(pf);
259
260         /* get WinPcap version */
261         /* XXX - what's the "approved" method to get the WinPcap version? */
262         pcap_version_tmp = g_string_new("");
263         get_runtime_pcap_version(pcap_version_tmp);
264
265         /* cut out real version from "combined" version string */
266         pcap_vstart = strstr(pcap_version_tmp->str, "with WinPcap version ");
267         if(pcap_vstart != NULL) {
268             pcap_vstart += sizeof("with WinPcap version");
269             pcap_vend = strstr(pcap_vstart, " ");
270             if(pcap_vend != NULL) {
271                 pcap_vend[0] = 0;
272                 pcap_version = g_strdup(pcap_vstart);
273             }
274         }
275
276         update_info->version_installed = g_strdup(pcap_version);
277
278         if(pcap_version && update_info->version_recommended &&
279             strcmp(pcap_version, update_info->version_recommended) != 0)
280         {
281             update_info->needs_update = TRUE;
282         }
283     } else {
284         g_warning("Could not open %s", local_file);
285     }
286
287     g_string_free(pcap_version_tmp, TRUE);
288     g_free(pcap_version);
289
290     return update_info;
291 }
292
293
294 /* check for all updates */
295 void
296 update_check(gboolean interactive)
297 {
298     char *local_file;
299     const char *url_file = "http://127.0.0.1/wsupdate"; /* XXX - build the URL depending on platform, versions, ... */
300     update_info_t *update_info_wireshark;
301     update_info_t *update_info_winpcap;
302
303
304     /* build update file name */
305     /* XXX - using the personal path, use temp dir instead? */
306     local_file = get_persconffile_path("wsupdate", FALSE, TRUE /*for_writing*/);
307     if(local_file == NULL) {
308         g_warning("Couldn't create output path!");
309         return;
310     }
311
312     /* download update file */
313     if(download_file(url_file, local_file) == -1) {
314         g_warning("Couldn't download update file: %s", local_file);
315         g_free(local_file);
316         return;
317     }
318
319     /* check wireshark */
320     update_info_wireshark = update_check_wireshark(local_file);
321
322     /* check winpcap */
323     update_info_winpcap = update_check_winpcap(local_file);
324
325     /* display results */
326     if(update_info_wireshark->needs_update || update_info_winpcap->needs_update) {
327         if(update_info_wireshark->needs_update)
328             update_info_display(update_info_wireshark);
329         if(update_info_winpcap->needs_update)
330             update_info_display(update_info_winpcap);
331     } else {
332         if(interactive) {
333             simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK, "No updates available");
334         }
335     }
336
337     /* cleanup */
338     update_info_delete(update_info_wireshark);
339     update_info_delete(update_info_winpcap);
340
341     g_free(local_file);
342 }
343