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