etc/services - sync with NetBSD-8
[minix.git] / external / bsd / bind / dist / contrib / idn / idnkit-1.0-src / wsock / common / checkdll.c
blob01b4e7d82916d72629d8ca9b893dbdb5c1a79696
1 /* $NetBSD: checkdll.c,v 1.3 2014/12/10 04:37:56 christos Exp $ */
3 /*
4 * checkdll.c - Winsock DLL/IDN processing status
5 */
7 /*
8 * Copyright (c) 2000,2002 Japan Network Information Center.
9 * All rights reserved.
11 * By using this file, you agree to the terms and conditions set forth bellow.
13 * LICENSE TERMS AND CONDITIONS
15 * The following License Terms and Conditions apply, unless a different
16 * license is obtained from Japan Network Information Center ("JPNIC"),
17 * a Japanese association, Kokusai-Kougyou-Kanda Bldg 6F, 2-3-4 Uchi-Kanda,
18 * Chiyoda-ku, Tokyo 101-0047, Japan.
20 * 1. Use, Modification and Redistribution (including distribution of any
21 * modified or derived work) in source and/or binary forms is permitted
22 * under this License Terms and Conditions.
24 * 2. Redistribution of source code must retain the copyright notices as they
25 * appear in each source code file, this License Terms and Conditions.
27 * 3. Redistribution in binary form must reproduce the Copyright Notice,
28 * this License Terms and Conditions, in the documentation and/or other
29 * materials provided with the distribution. For the purposes of binary
30 * distribution the "Copyright Notice" refers to the following language:
31 * "Copyright (c) 2000-2002 Japan Network Information Center. All rights reserved."
33 * 4. The name of JPNIC may not be used to endorse or promote products
34 * derived from this Software without specific prior written approval of
35 * JPNIC.
37 * 5. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY JPNIC
38 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
39 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
40 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JPNIC BE LIABLE
41 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
42 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
43 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
44 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
45 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
46 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
47 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
50 #include <windows.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include "wrapcommon.h"
56 static int winsock_idx; /* index of winsock_info[] */
58 static struct winsock_type {
59 char *version; /* winsock version */
60 char *name; /* wrapper DLL name */
61 char *original_name; /* original DLL name */
62 } winsock_info[] = {
63 #define IDN_IDX_WS11 0
64 { "1.1", "WSOCK32", "WSOCK32O" },
65 #define IDN_IDX_WS20 1
66 { "2.0", "WS2_32", "WS2_32O" },
67 { NULL, NULL, NULL },
70 static HINSTANCE load_original_dll(void);
71 static BOOL check_idn_processing(void);
72 static BOOL check_dll(const char *name);
74 BOOL
75 idnWinsockVersion(const char *version) {
76 int i;
77 for (i = 0; winsock_info[i].version != NULL; i++) {
78 if (strcmp(winsock_info[i].version, version) == 0) {
79 winsock_idx = i;
80 idnLogPrintf(idn_log_level_trace,
81 "idnWinsockVersion: version %s\n",
82 version);
83 return (TRUE);
86 idnLogPrintf(idn_log_level_fatal,
87 "idnWinsockVersion: unknown winsock version %s\n",
88 version);
89 return (FALSE);
92 HINSTANCE
93 idnWinsockHandle(void) {
94 static HINSTANCE dll_handle = NULL;
95 static int initialized = 0;
97 if (!initialized) {
98 /* Get the handle of the original winsock DLL */
99 idnLogPrintf(idn_log_level_trace,
100 "idnWinsockHandle: loading original DLL..\n");
101 dll_handle = load_original_dll();
103 initialized = 1;
104 return (dll_handle);
107 idn_resconf_t
108 idnGetContext(void) {
109 static int initialized = 0;
110 static idn_resconf_t ctx = NULL;
112 if (!initialized) {
114 * Check whether IDN processing should be done
115 * in this wrapper DLL.
117 idnLogPrintf(idn_log_level_trace,
118 "idnGetContext: checking IDN status..\n");
119 if (check_idn_processing()) {
120 /* Initialize idnkit */
121 ctx = idnConvInit();
122 idnLogPrintf(idn_log_level_info,
123 "Processing context: %08x\n", ctx);
124 } else {
125 idnLogPrintf(idn_log_level_info,
126 "NOT process IDN here\n");
127 ctx = NULL;
129 initialized = 1;
132 return (ctx);
135 static HINSTANCE
136 load_original_dll(void) {
138 * Load Original DLL
140 char dllpath[MAX_PATH];
141 const char *dll_name = winsock_info[winsock_idx].original_name;
142 HINSTANCE handle;
145 * Get idn wrapper's install directory, where the copies of
146 * the original winsock DLLs are saved.
148 dllpath[0] = '\0';
149 if (idnGetInstallDir(dllpath, sizeof(dllpath)) != TRUE) {
150 idnLogPrintf(idn_log_level_fatal,
151 "idnWinsockHandle: cannot find idn wrapper's "
152 "install directory\n");
153 abort();
154 return (NULL); /* for lint */
156 /* Strip the trailing backslash. */
157 if (dllpath[0] != '\0' &&
158 dllpath[strlen(dllpath) - 1] == '\\') {
159 dllpath[strlen(dllpath) - 1] = '\0';
161 /* Is the pathname is insanely long? */
162 if (strlen(dllpath) + strlen(dll_name) + 1 + 4 >= sizeof(dllpath)) {
163 idnLogPrintf(idn_log_level_fatal,
164 "idnWinsockHandle: idn wrapper's install path is "
165 "too long to be true\n");
166 abort();
167 return (NULL); /* for lint */
169 /* Append the DLL name to form a full pathname of the DLL. */
170 strcat(dllpath, "\\");
171 strcat(dllpath, dll_name);
172 strcat(dllpath, ".DLL");
174 idnLogPrintf(idn_log_level_trace,
175 "idnWinsockHandle: loading original winsock DLL (%s)\n",
176 dllpath);
177 if ((handle = LoadLibrary(dllpath)) == NULL) {
178 idnLogPrintf(idn_log_level_fatal,
179 "idnWinsockHandle: no DLL %-.100s\n", dllpath);
180 abort();
181 return (NULL); /* font lint */
183 return (handle);
186 static BOOL
187 check_idn_processing(void) {
188 int where = idnEncodeWhere();
189 BOOL here = FALSE;
191 idnLogPrintf(idn_log_level_trace,
192 "idnGetContext: Winsock%s, where=%d\n",
193 winsock_info[winsock_idx].version, where);
195 switch (winsock_idx) {
196 case IDN_IDX_WS11:
197 switch (where) {
198 case IDN_ENCODE_ALWAYS:
199 case IDN_ENCODE_ONLY11:
200 return (TRUE);
201 case IDN_ENCODE_CHECK:
202 if (!check_dll(winsock_info[winsock_idx].name)) {
203 return (TRUE);
205 break;
207 break;
208 case IDN_IDX_WS20:
209 switch (where) {
210 case IDN_ENCODE_ALWAYS:
211 case IDN_ENCODE_ONLY20:
212 case IDN_ENCODE_CHECK:
213 return (TRUE);
214 break;
216 break;
218 return (FALSE);
221 static BOOL
222 check_dll(const char *name) {
223 HINSTANCE hdll = NULL;
225 #if 1
226 hdll = LoadLibrary(name);
227 #else
229 * Just check the existence of the named DLL, without taking
230 * the trouble of calling DllMain.
232 hdll = LoadLibraryEx(name, NULL, LOAD_LIBRARY_AS_DATAFILE);
233 #endif
234 if (hdll == NULL) {
235 idnLogPrintf(idn_log_level_trace,
236 "idnGetContext: DLL %s does not exist\n");
237 return (FALSE);
238 } else {
239 idnLogPrintf(idn_log_level_trace,
240 "idnGetContext: DLL %s exists\n");
241 FreeLibrary(hdll);
242 return (TRUE);