LOCAL: ignore build dirs
[wireshark-wip.git] / nio-ie5.c
blob6ad3311da663ea01ffc45826f9a7f93a71e17d9d
1 /*
2 * $Id$
4 * Copyright (c) 2000, Red Hat, Inc.
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.
11 * A copy of the GNU General Public License can be found at
12 * http://www.gnu.org/
14 * Written by DJ Delorie <dj@cygnus.com>
15 * Modified by Ulf Lamping to meet Wireshark use
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. */
23 #include "windows.h"
24 #include <wininet.h>
25 #include "nio-ie5.h"
27 #include <glib.h>
29 static HINTERNET internet = 0;
33 netio_ie5_t *
34 netio_ie5_connect (char const *url)
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;
48 if (internet == 0)
50 HINSTANCE h = ws_load_library("wininet.dll");
51 if (!h)
53 /* XXX - how to return an error code? */
54 g_warning("Failed to load wininet.dll");
55 return NULL;
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;
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;
72 netio_ie5_conn = g_malloc(sizeof(netio_ie5_t));
74 netio_ie5_conn->connection = InternetOpenUrl (internet, url, NULL, 0, flags, 0);
76 try_again:
78 #if 0
79 /* XXX - implement this option */
80 if (net_user && net_passwd)
82 InternetSetOption (connection, INTERNET_OPTION_USERNAME,
83 net_user, strlen (net_user));
84 InternetSetOption (connection, INTERNET_OPTION_PASSWORD,
85 net_passwd, strlen (net_passwd));
87 #endif
89 #if 0
90 /* XXX - implement this option */
91 if (net_proxy_user && net_proxy_passwd)
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));
98 #endif
100 if (resend)
101 if (!HttpSendRequest (netio_ie5_conn->connection, 0, 0, 0, 0))
102 netio_ie5_conn->connection = 0;
104 if (!netio_ie5_conn->connection)
106 switch(GetLastError ()) {
107 case ERROR_INTERNET_EXTENDED_ERROR:
109 char buf[2000];
110 DWORD e, l = sizeof (buf);
111 InternetGetLastResponseInfo (&e, buf, &l);
112 MessageBox (0, buf, "Internet Error", 0);
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 ());
124 return NULL;
127 type_s = sizeof (type);
128 InternetQueryOption (netio_ie5_conn->connection, INTERNET_OPTION_HANDLE_TYPE,
129 &type, &type_s);
131 switch (type)
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))
140 if (type == 401) /* authorization required */
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;
148 else if (type == 407) /* proxy authorization required */
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;
156 else if (type >= 300)
158 g_warning("Failed with HTTP response %u", type);
159 g_free(netio_ie5_conn);
160 return NULL;
165 return netio_ie5_conn;
168 void
169 netio_ie5_flush_io (netio_ie5_t * netio_e5_conn)
171 DWORD actual = 0;
172 char buf[1024];
175 InternetReadFile (netio_e5_conn->connection, buf, 1024, &actual);
177 while (actual > 0);
180 void
181 netio_ie5_disconnect (netio_ie5_t * netio_e5_conn)
183 if (netio_e5_conn->connection)
184 InternetCloseHandle (netio_e5_conn->connection);
185 g_free(netio_e5_conn);
189 netio_ie5_ok (netio_ie5_t * netio_e5_conn)
191 return (netio_e5_conn->connection == NULL) ? 0 : 1;
195 netio_ie5_read (netio_ie5_t * netio_e5_conn, char *buf, int nbytes)
197 DWORD actual;
198 if (InternetReadFile (netio_e5_conn->connection, buf, nbytes, &actual))
199 return actual;
200 return -1;