cosmetics
[tomato.git] / release / src / router / openvpn / ieproxy.c
blob89977a821aca72c114403edcaf87facdf5e36dc5
1 /*
2 * Copyright (C) 2004 Ewan Bhamrah Harley <code@ewan.info>
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.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program (see the file COPYING included with this
16 * distribution); if not, write to the Free Software Foundation, Inc.,
17 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "syshead.h"
22 #ifdef WIN32
24 #include <wininet.h>
25 #include <malloc.h>
27 LPCTSTR getIeHttpProxyError=NULL;
29 /* getIeHttpProxy fetches the current IE proxy settings for http */
31 LPCTSTR getIeHttpProxy()
33 DWORD psize=0;
34 INTERNET_PROXY_INFO *pinfo;
35 LPTSTR proxyString;
36 LPTSTR p;
37 LPTSTR q;
38 unsigned int len;
40 /* first see how big a buffer we need for the IPO structure */
41 InternetQueryOption(NULL, INTERNET_OPTION_PROXY, NULL, &psize);
42 if(!psize)
44 getIeHttpProxyError="InternetQueryOption failed to return buffer size";
45 return(NULL);
48 /* allocate memory for IPO */
49 pinfo = malloc (psize*sizeof(TCHAR));
50 if (pinfo == NULL)
52 getIeHttpProxyError="malloc failed (1)";
53 return(NULL);
56 /* now run the real query */
57 if(!InternetQueryOption(NULL, INTERNET_OPTION_PROXY, (LPVOID) pinfo, &psize))
59 getIeHttpProxyError="InternetQueryOption() failed to find proxy info";
60 free(pinfo);
61 return(NULL);
65 /* see what sort of result we got */
67 if(pinfo->dwAccessType == INTERNET_OPEN_TYPE_DIRECT)
69 /* No proxy configured */
70 free(pinfo);
71 return("");
73 else if(pinfo->dwAccessType == INTERNET_OPEN_TYPE_PROXY)
75 /* we have a proxy - now parse result string */
76 /* if result string does NOT contain an '=' sign then */
77 /* there is a single proxy for all protocols */
78 for (p=(LPTSTR)pinfo->lpszProxy; *p && *p != '='; p++);
79 if(!*p)
81 /* single proxy */
82 /* allocate a new string to return */
83 len = 1+strlen(pinfo->lpszProxy);
84 proxyString = malloc (len*sizeof(TCHAR));
85 if (proxyString == NULL)
87 getIeHttpProxyError="malloc failed (2)";
88 free(pinfo);
89 return(NULL);
91 strncpy(proxyString, pinfo->lpszProxy,len);
92 proxyString[len]=0;
93 free(pinfo);
94 return(proxyString);
96 else
98 /* multiple space seperated proxies defined in the form */
99 /* protocol=proxyhost[:port] */
100 /* we want the one marked "http=", if any. */
101 p=(LPTSTR)pinfo->lpszProxy;
102 while(*p && strncmp(p, "http=", 5))
104 for(; *p && *p != ' '; p++);
105 if(*p) p++;
107 if(*p)
109 /* found the proxy */
110 p+=5;
111 for(q=p; *q && *q != ' '; q++);
112 /* allocate a buffer for the proxy information */
113 len=1+(q-p);
114 proxyString=malloc(len*sizeof(TCHAR));
115 if(proxyString==NULL)
117 getIeHttpProxyError="malloc failed (3)";
118 free(pinfo);
119 return(NULL);
121 strncpy(proxyString, p, len);
122 proxyString[len]=0;
123 free(pinfo);
124 return(proxyString);
126 else
128 /* No http proxy in list */
129 free(pinfo);
130 return("");
134 else
136 /* InternetQueryOption returned a proxy type we don't know about*/
137 getIeHttpProxyError="Unknown Proxy Type";
138 free(pinfo);
139 return(NULL);
143 #else
144 static void dummy (void) {}
145 #endif /* WIN32 */