Allow the folders in the About Wireshark/Folders list to be double clicked on to...
[obnox/wireshark/wip.git] / nio-ie5.c
1 /*
2  * Copyright (c) 2000, Red Hat, Inc.
3  *
4  *     This program is free software; you can redistribute it and/or modify
5  *     it under the terms of the GNU General Public License as published by
6  *     the Free Software Foundation; either version 2 of the License, or
7  *     (at your option) any later version.
8  *
9  *     A copy of the GNU General Public License can be found at
10  *     http://www.gnu.org/
11  *
12  * Written by DJ Delorie <dj@cygnus.com>
13  * Modified by Ulf Lamping to meet Wireshark use
14  *
15  */
16
17 /* The purpose of this file is to manage internet downloads using the
18    Internet Explorer version 5 DLLs.  To use this method, the user
19    must already have installed and configured IE5.  */
20
21 #include "windows.h"\r
22 #include <wininet.h>\r
23 #include "nio-ie5.h"\r
24 \r
25 #include "glib.h"\r
26
27 static HINTERNET internet = 0;\r
28 \r
29
30
31 netio_ie5_t * 
32 netio_ie5_connect (char const *url)
33 {
34   int resend = 0;
35   DWORD type, type_s;\r
36   netio_ie5_t * netio_ie5_conn;\r
37   DWORD dw_ret;\r
38   DWORD flags =\r
39  /*    INTERNET_FLAG_DONT_CACHE |*/\r
40     INTERNET_FLAG_KEEP_CONNECTION |\r
41     INTERNET_FLAG_PRAGMA_NOCACHE |\r
42     INTERNET_FLAG_RELOAD |\r
43     INTERNET_FLAG_NO_CACHE_WRITE |\r
44     INTERNET_FLAG_EXISTING_CONNECT | INTERNET_FLAG_PASSIVE;\r
45 \r
46   if (internet == 0)
47     {
48       HINSTANCE h = LoadLibrary ("wininet.dll");
49       if (!h)
50         {\r
51           /* XXX - how to return an error code? */
52           g_warning("Failed to load wininet.dll");
53           return NULL;
54         }\r
55       /* pop-up dialup dialog box */\r
56       /* XXX - do we need the dialup box or simply don't attempt an update in this case? */\r
57       dw_ret = InternetAttemptConnect (0);
58       if (dw_ret != ERROR_SUCCESS) {\r
59         g_warning("InternetAttemptConnect failed: %u", dw_ret);\r
60         return NULL;\r
61       }
62       internet = InternetOpen ("Wireshark Update", INTERNET_OPEN_TYPE_PRECONFIG,
63                                NULL, NULL, 0);\r
64       if(internet == NULL) {\r
65         g_warning("InternetOpen failed %u", GetLastError());\r
66         return NULL;\r
67       }
68     }
69
70   netio_ie5_conn = g_malloc(sizeof(netio_ie5_t));\r
71 \r
72   netio_ie5_conn->connection = InternetOpenUrl (internet, url, NULL, 0, flags, 0);
73
74 try_again:
75 \r
76 #if 0
77         /* XXX - implement this option */
78   if (net_user && net_passwd)
79     {
80       InternetSetOption (connection, INTERNET_OPTION_USERNAME,
81                          net_user, strlen (net_user));
82       InternetSetOption (connection, INTERNET_OPTION_PASSWORD,
83                          net_passwd, strlen (net_passwd));
84     }\r
85 #endif
86 \r
87 #if 0
88         /* XXX - implement this option */
89   if (net_proxy_user && net_proxy_passwd)
90     {
91       InternetSetOption (connection, INTERNET_OPTION_PROXY_USERNAME,
92                          net_proxy_user, strlen (net_proxy_user));
93       InternetSetOption (connection, INTERNET_OPTION_PROXY_PASSWORD,
94                          net_proxy_passwd, strlen (net_proxy_passwd));
95     }\r
96 #endif
97
98   if (resend)
99     if (!HttpSendRequest (netio_ie5_conn->connection, 0, 0, 0, 0))
100       netio_ie5_conn->connection = 0;
101
102   if (!netio_ie5_conn->connection)
103     {\r
104       switch(GetLastError ()) {\r
105       case ERROR_INTERNET_EXTENDED_ERROR:\r
106           {
107           char buf[2000];
108           DWORD e, l = sizeof (buf);
109           InternetGetLastResponseInfo (&e, buf, &l);
110           MessageBox (0, buf, "Internet Error", 0);\r
111           }\r
112           break;
113       case ERROR_INTERNET_NAME_NOT_RESOLVED:\r
114           g_warning("Internet error: The servername could not be resolved");\r
115           break;\r
116       case ERROR_INTERNET_CANNOT_CONNECT:\r
117           g_warning("Internet error: Could not connect to the server");\r
118           break;\r
119       default:\r
120           g_warning("Internet error: %u", GetLastError ());\r
121       }\r
122       return NULL;
123     }
124
125   type_s = sizeof (type);
126   InternetQueryOption (netio_ie5_conn->connection, INTERNET_OPTION_HANDLE_TYPE,
127                        &type, &type_s);
128
129   switch (type)
130     {
131     case INTERNET_HANDLE_TYPE_HTTP_REQUEST:
132     case INTERNET_HANDLE_TYPE_CONNECT_HTTP:
133       type_s = sizeof (DWORD);
134       if (HttpQueryInfo (netio_ie5_conn->connection,
135                          HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER,
136                          &type, &type_s, NULL))
137         {
138           if (type == 401)      /* authorization required */
139             {
140               netio_ie5_flush_io (netio_ie5_conn);\r
141               /* XXX - query net_user && net_passwd from user\r
142               get_auth (NULL);*/
143               resend = 1;
144               goto try_again;
145             }
146           else if (type == 407) /* proxy authorization required */
147             {
148               netio_ie5_flush_io (netio_ie5_conn);
149               /* XXX - query net_proxy_user && net_proxy_passwd from user\r
150               get_proxy_auth (NULL);*/
151               resend = 1;
152               goto try_again;
153             }
154           else if (type >= 300)
155             {
156               g_warning("Failed with HTTP response %u", type);\r
157               g_free(netio_ie5_conn);
158               return NULL;
159             }
160         }
161     }
162         
163         return netio_ie5_conn;
164 }
165
166 void
167 netio_ie5_flush_io (netio_ie5_t * netio_e5_conn)
168 {
169   DWORD actual = 0;
170   char buf[1024];
171   do
172     {
173       InternetReadFile (netio_e5_conn->connection, buf, 1024, &actual);
174     }
175   while (actual > 0);
176 }
177
178 void
179 netio_ie5_disconnect (netio_ie5_t * netio_e5_conn)
180 {
181   if (netio_e5_conn->connection)
182     InternetCloseHandle (netio_e5_conn->connection);\r
183   g_free(netio_e5_conn);
184 }
185
186 int
187 netio_ie5_ok (netio_ie5_t * netio_e5_conn)
188 {
189   return (netio_e5_conn->connection == NULL) ? 0 : 1;
190 }
191
192 int
193 netio_ie5_read (netio_ie5_t * netio_e5_conn, char *buf, int nbytes)
194 {
195   DWORD actual;
196   if (InternetReadFile (netio_e5_conn->connection, buf, nbytes, &actual))
197     return actual;
198   return -1;
199 }