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