1/* 2 * hook.c - Hooking Asynchronous Completion 3 */ 4 5/* 6 * Copyright (c) 2000,2002 Japan Network Information Center. 7 * All rights reserved. 8 * 9 * By using this file, you agree to the terms and conditions set forth bellow. 10 * 11 * LICENSE TERMS AND CONDITIONS 12 * 13 * The following License Terms and Conditions apply, unless a different 14 * license is obtained from Japan Network Information Center ("JPNIC"), 15 * a Japanese association, Kokusai-Kougyou-Kanda Bldg 6F, 2-3-4 Uchi-Kanda, 16 * Chiyoda-ku, Tokyo 101-0047, Japan. 17 * 18 * 1. Use, Modification and Redistribution (including distribution of any 19 * modified or derived work) in source and/or binary forms is permitted 20 * under this License Terms and Conditions. 21 * 22 * 2. Redistribution of source code must retain the copyright notices as they 23 * appear in each source code file, this License Terms and Conditions. 24 * 25 * 3. Redistribution in binary form must reproduce the Copyright Notice, 26 * this License Terms and Conditions, in the documentation and/or other 27 * materials provided with the distribution. For the purposes of binary 28 * distribution the "Copyright Notice" refers to the following language: 29 * "Copyright (c) 2000-2002 Japan Network Information Center. All rights reserved." 30 * 31 * 4. The name of JPNIC may not be used to endorse or promote products 32 * derived from this Software without specific prior written approval of 33 * JPNIC. 34 * 35 * 5. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY JPNIC 36 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 37 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 38 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JPNIC BE LIABLE 39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 40 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 41 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 42 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 43 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 44 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 45 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 46 */ 47 48#include <windows.h> 49#include <stdio.h> 50#include <stdlib.h> 51#include <string.h> 52 53#include "wrapcommon.h" 54 55/* 56 * Hook Managements 57 */ 58 59static HHOOK hookHandle = NULL ; 60 61typedef struct _HOOK *HOOKPTR; 62 63typedef struct _HOOK { 64 HOOKPTR prev; 65 HOOKPTR next; 66 idn_resconf_t ctx; 67 HWND hWnd; 68 u_int wMsg; 69 char FAR *pBuf; 70} HOOKREC; 71 72static HOOKREC hookList = { 0 } ; 73 74static void 75hookListInit(void) { 76 if (hookList.prev == NULL || hookList.next == NULL) { 77 hookList.prev = &hookList; 78 hookList.next = &hookList; 79 } 80} 81 82static HOOKPTR 83hookListSearch(HWND hWnd, u_int wMsg) { 84 HOOKPTR hp; 85 86 for (hp = hookList.next ; hp != &hookList ; hp = hp->next) { 87 if (hp->hWnd == hWnd && hp->wMsg == wMsg) { 88 return (hp); 89 } 90 } 91 return (NULL); 92} 93 94static BOOL 95hookListAppend(HWND hWnd, u_int wMsg, char FAR *buf, idn_resconf_t ctx) { 96 HOOKPTR hp, prev, next; 97 98 if ((hp = (HOOKPTR)malloc(sizeof(HOOKREC))) == NULL) { 99 idnPrintf("cannot create hook record\n"); 100 return (FALSE); 101 } 102 memset(hp, 0, sizeof(*hp)); 103 104 hp->ctx = ctx; 105 hp->hWnd = hWnd; 106 hp->wMsg = wMsg; 107 hp->pBuf = buf; 108 109 prev = hookList.prev; 110 next = prev->next; 111 prev->next = hp; 112 next->prev = hp; 113 hp->next = next; 114 hp->prev = prev; 115 116 return (TRUE); 117} 118 119static void 120hookListDelete(HOOKPTR hp) 121{ 122 HOOKPTR prev, next; 123 124 prev = hp->prev; 125 next = hp->next; 126 prev->next = next; 127 next->prev = prev; 128 129 free(hp); 130} 131 132static void 133hookListDone(void) 134{ 135 HOOKPTR hp; 136 137 while ((hp = hookList.next) != &hookList) { 138 hookListDelete(hp); 139 } 140} 141 142/* 143 * idnHookInit - initialize Hook Management 144 */ 145void 146idnHookInit(void) { 147 hookListInit(); 148} 149 150/* 151 * idnHookDone - finalize Hook Management 152 */ 153void 154idnHookDone(void) { 155 if (hookHandle != NULL) { 156 UnhookWindowsHookEx(hookHandle); 157 hookHandle = NULL; 158 } 159 hookListDone(); 160} 161 162/* 163 * hookProc - hookprocedure, used as WH_GETMESSAGE hook 164 */ 165LRESULT CALLBACK 166hookProc(int nCode, WPARAM wParam, LPARAM lParam) { 167 MSG *pMsg; 168 HOOKPTR pHook; 169 struct hostent *pHost; 170 char nbuff[256]; 171 char hbuff[256]; 172 173 if (nCode < 0) { 174 return (CallNextHookEx(hookHandle, nCode, wParam, lParam)); 175 } else if (nCode != HC_ACTION) { 176 return (0); 177 } 178 if ((pMsg = (MSG *)lParam) == NULL) { 179 return (0); 180 } 181 if ((pHook = hookListSearch(pMsg->hwnd, pMsg->message)) == NULL) { 182 return (0); 183 } 184 185 /* 186 * Convert the Host Name 187 */ 188 pHost = (struct hostent *)pHook->pBuf; 189 idnPrintf("AsyncComplete Resulting <%s>\n", 190 dumpName(pHost->h_name, hbuff, sizeof(hbuff))); 191 if (idnConvRsp(pHook->ctx, pHost->h_name, 192 nbuff, sizeof(nbuff)) == TRUE) { 193 idnPrintf("AsyncComplete Converted <%s>\n", 194 dumpName(nbuff, hbuff, sizeof(hbuff))); 195 strcpy(pHost->h_name, nbuff); 196 } 197 198 /* 199 * Delete target 200 */ 201 hookListDelete(pHook); 202 203 return (0); 204} 205 206/* 207 * idnHook - hook async. completion message 208 */ 209BOOL 210idnHook(HWND hWnd, u_int wMsg, char FAR *buf, idn_resconf_t ctx) 211{ 212 if (hookHandle == NULL) { 213 hookHandle = SetWindowsHookEx(WH_GETMESSAGE, hookProc, 214 NULL, GetCurrentThreadId()); 215 } 216 if (hookHandle == NULL) { 217 idnPrintf("idnHook: cannot set hook\n"); 218 return (FALSE); 219 } 220 if (hookListAppend(hWnd, wMsg, buf, ctx) != TRUE) { 221 return (FALSE); 222 } 223 return (TRUE); 224} 225