Remove an extraneous backslash
[obnox/wireshark/wip.git] / nio-ie5.c
1 /*
2  * $Id$
3  *
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"
24 #include <wininet.h>
25 #include "nio-ie5.h"
26
27 #include "glib.h"
28
29 static HINTERNET internet = 0;
30
31
32
33 netio_ie5_t * 
34 netio_ie5_connect (char const *url)
35 {
36   int resend = 0;
37   DWORD type, type_s;
38   netio_ie5_t * netio_ie5_conn;
39   DWORD dw_ret;
40   DWORD flags =
41  /*    INTERNET_FLAG_DONT_CACHE |*/
42     INTERNET_FLAG_KEEP_CONNECTION |
43     INTERNET_FLAG_PRAGMA_NOCACHE |
44     INTERNET_FLAG_RELOAD |
45     INTERNET_FLAG_NO_CACHE_WRITE |
46     INTERNET_FLAG_EXISTING_CONNECT | INTERNET_FLAG_PASSIVE;
47
48   if (internet == 0)
49     {
50       HINSTANCE h = ws_load_library("wininet.dll");
51       if (!h)
52         {
53           /* XXX - how to return an error code? */
54           g_warning("Failed to load wininet.dll");
55           return NULL;
56         }
57       /* pop-up dialup dialog box */
58       /* XXX - do we need the dialup box or simply don't attempt an update in this case? */
59       dw_ret = InternetAttemptConnect (0);
60       if (dw_ret != ERROR_SUCCESS) {
61         g_warning("InternetAttemptConnect failed: %u", dw_ret);
62         return NULL;
63       }
64       internet = InternetOpen ("Wireshark Update", INTERNET_OPEN_TYPE_PRECONFIG,
65                                NULL, NULL, 0);
66       if(internet == NULL) {
67         g_warning("InternetOpen failed %u", GetLastError());
68         return NULL;
69       }
70     }
71
72   netio_ie5_conn = g_malloc(sizeof(netio_ie5_t));
73
74   netio_ie5_conn->connection = InternetOpenUrl (internet, url, NULL, 0, flags, 0);
75
76 try_again:
77
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     }
87 #endif
88
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     }
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     {
106       switch(GetLastError ()) {
107       case ERROR_INTERNET_EXTENDED_ERROR:
108           {
109           char buf[2000];
110           DWORD e, l = sizeof (buf);
111           InternetGetLastResponseInfo (&e, buf, &l);
112           MessageBox (0, buf, "Internet Error", 0);
113           }
114           break;
115       case ERROR_INTERNET_NAME_NOT_RESOLVED:
116           g_warning("Internet error: The servername could not be resolved");
117           break;
118       case ERROR_INTERNET_CANNOT_CONNECT:
119           g_warning("Internet error: Could not connect to the server");
120           break;
121       default:
122           g_warning("Internet error: %u", GetLastError ());
123       }
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);
143               /* XXX - query net_user && net_passwd from user
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
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);
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);
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 }