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