1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
/*++
Copyright (c) Microsoft Corporation. All rights reserved.
Module Name:
Connection.cpp
Abstract:
Module for the socket connection specfic routines in the driver.
Makes Connection to the server given server host and port address.
Environment:
User mode only
--*/
#include "internal.h"
#include "connection.tmh"
CConnection::CConnection()
/*++
Routine Description:
Constructor for connection object
Arguments:
None
Return Value:
VOID
--*/
{
// Initialize the socket member as Invalid
Trace(
TRACE_LEVEL_INFORMATION,
"%!FUNC!"
);
m_socket = INVALID_SOCKET;
}
HRESULT
CConnection::Connect(
IN IWDFDevice *pDevice
)
/*++
Routine Description:
This routine is for the initialization of the connection object associated with
the File Object . It is invoked from the dispatch OnCreateFile on the default
queue callback of the driver. It socket connection to the client.
Arguments:
pDevice = Wdf Device Object
Return Value:
S_OK if success , error HRESULT otherwise
--*/
{
Trace(
TRACE_LEVEL_INFORMATION,
"%!FUNC!"
);
HRESULT hr = S_OK;
addrinfoW* info = NULL ;
PWSTR hostStr = NULL;
PWSTR portStr = NULL;
//
// Reads the host and port strings stored in the device context.
//
DeviceContext *pContext = NULL;
hr = pDevice->RetrieveContext((void**)&pContext);
if ( FAILED(hr) )
{
Trace(
TRACE_LEVEL_ERROR,
L"ERROR: unable to retrieve context from wdf device object %!hresult!",
hr
);
goto Clean0;
}
hostStr = pContext->hostStr;
portStr = pContext->portStr;
//
// lookup hostname with addrinfo hints;
//
addrinfoW hints;
ZeroMemory(&hints,sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
int n = GetAddrInfoW(hostStr, portStr, &hints, &info);
if (n != 0)
{
DWORD err = WSAGetLastError();
Trace(
TRACE_LEVEL_ERROR,
L"ERROR: Unable to find address/port of host %!winerr!",
err
);
hr = HRESULT_FROM_WIN32(err);
goto Clean0;
}
//
// Create a socket with this infomation recvd in getaddrinfo
//
m_socket = socket(info->ai_family,info->ai_socktype,info->ai_protocol);
if (m_socket == INVALID_SOCKET)
{
DWORD err = WSAGetLastError();
Trace(
TRACE_LEVEL_ERROR,
L"ERROR: Unable to create socket %!winerr!",
err
);
hr = HRESULT_FROM_WIN32(err);
goto Clean0;
}
//
// If that succeeds , proceed to connect to the socket
//
ATLASSERT(info->ai_addrlen <= 0x7fffffff);
int nret = connect(m_socket,info->ai_addr,(int)info->ai_addrlen);
if (nret == SOCKET_ERROR)
{
DWORD err = WSAGetLastError();
Trace(
TRACE_LEVEL_ERROR,
L"ERROR: Unable to connect to host %!winerr!",
err
);
hr = HRESULT_FROM_WIN32(err);
goto Clean0;
}
Clean0:
if (info != NULL)
{
FreeAddrInfoW(info);
}
if (FAILED(hr) && m_socket != INVALID_SOCKET)
{
closesocket(m_socket);
m_socket = INVALID_SOCKET;
}
return hr;
}
HANDLE
CConnection::GetSocketHandle(
)
/*++
Routine Description:
Function returns the socket handle associated with this connection object
Arguments:
None
Return Value:
Socket handle if valid socket
INVALID_HANDLE_VALUE otherwise
--*/
{
Trace(
TRACE_LEVEL_INFORMATION,
"%!FUNC!"
);
if ( INVALID_SOCKET != m_socket )
{
return (HANDLE)m_socket ;
}
else
{
return INVALID_HANDLE_VALUE;
}
}
VOID
CConnection::Close()
/*++
Routine Description:
Closes the socket connection to the server associated with this connection object
Arguments:
None
Return Value:
None
--*/
{
Trace(
TRACE_LEVEL_INFORMATION,
"%!FUNC!"
);
if (m_socket != INVALID_SOCKET)
{
closesocket(m_socket);
m_socket = INVALID_SOCKET;
}
}
|