Develop and Download Open Source Software

Browse Subversion Repository

Contents of /branches/ttcomtester/teraterm/teraterm/WSAAsyncGetAddrInfo.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 10521 - (show annotations) (download) (as text)
Fri Jan 20 16:03:38 2023 UTC (14 months, 2 weeks ago) by zmatsuo
File MIME type: text/x-csrc
File size: 4355 byte(s)
add communication test tool
1 /*
2 * Copyright (C) 2010- TeraTerm Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 /*
29 * WSAAsyncGetAddrInfo.c -- asynchronous version of getaddrinfo
30 * Copyright(C) 2000-2003 Jun-ya Kato <kato@win6.jp>
31 */
32 #if !defined(_CRTDBG_MAP_ALLOC)
33 #define _CRTDBG_MAP_ALLOC
34 #endif
35 #include <winsock2.h>
36 #include <ws2tcpip.h>
37 #include <stdlib.h>
38 #include <crtdbg.h>
39 #include <wspiapi.h>
40 #include <windows.h>
41 #include <process.h>
42 #include "WSAAsyncGetAddrInfo.h"
43 #include "ttwsk.h"
44
45 struct getaddrinfo_args {
46 HWND hWnd;
47 unsigned int wMsg;
48 char *hostname;
49 char *portname;
50 struct addrinfo hints;
51 struct addrinfo **res;
52 HANDLE *lpHandle;
53 };
54
55 static unsigned __stdcall getaddrinfo_thread(void * p);
56
57 HANDLE PASCAL WSAAsyncGetAddrInfo(HWND hWnd, unsigned int wMsg,
58 const char *hostname,
59 const char *portname,
60 struct addrinfo *hints,
61 struct addrinfo **res)
62 {
63 HANDLE thread;
64 unsigned tid;
65 struct getaddrinfo_args * ga;
66
67 /*
68 * allocate structure to pass args to sub-thread dynamically
69 * WSAAsyncGetAddrInfo() is reentrant
70 */
71 if ((ga = (struct getaddrinfo_args *)malloc(sizeof(struct getaddrinfo_args))) == NULL)
72 return NULL;
73
74 /* packing arguments struct addrinfo_args */
75 ga->hWnd = hWnd;
76 ga->wMsg = wMsg;
77 ga->hostname = _strdup(hostname); // �|�C���^�����n�����A�X���b�h�����s���������B(2012.11.7 yutaka)
78 ga->portname = _strdup(portname);
79 ga->hints = *hints; // �|�C���^�����n�����A�X���b�h�����s���������B(2016.3.11 yutaka)
80 ga->res = res;
81
82 ga->lpHandle = (HANDLE *)malloc(sizeof(HANDLE));
83 if (ga->lpHandle == NULL) {
84 free(ga->hostname);
85 free(ga->portname);
86 return NULL;
87 }
88
89 /* create sub-thread running getaddrinfo() */
90 thread = (HANDLE)_beginthreadex(NULL, 0, getaddrinfo_thread, ga, CREATE_SUSPENDED, &tid);
91 if (thread == 0) {
92 free(ga->lpHandle);
93 free(ga->hostname);
94 free(ga->portname);
95 free(ga);
96 return NULL; // return error
97 }
98
99 /* return thread handle */
100 *ga->lpHandle = thread;
101 ResumeThread(thread);
102 return thread;
103 }
104
105 static unsigned __stdcall getaddrinfo_thread(void * p)
106 {
107 int gai;
108 HWND hWnd;
109 unsigned int wMsg;
110 const char *hostname;
111 const char *portname;
112 struct addrinfo *hints;
113 struct addrinfo **res;
114 struct getaddrinfo_args *ga;
115
116 /* unpacking arguments */
117 ga = (struct getaddrinfo_args *)p;
118 hWnd = ga->hWnd;
119 wMsg = ga->wMsg;
120 hostname = ga->hostname;
121 portname = ga->portname;
122 hints = &ga->hints;
123 res = ga->res;
124
125 /* call getaddrinfo */
126 // gai = Pgetaddrinfo(hostname, portname, hints, res);
127 gai = getaddrinfo(hostname, portname, hints, res);
128
129 /* send value of gai as message to window hWnd */
130 PostMessage(hWnd, wMsg, (WPARAM)*ga->lpHandle, MAKELPARAM(0, gai));
131
132 free(ga->lpHandle);
133 free(ga->hostname);
134 free(ga->portname);
135 free(p);
136
137 return 0;
138 }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26