SOCKET WSPSocket(
int
AddressFamily,
int
SocketType,
int
Protocol) {
/
/
/
<summary>
/
/
/
类似于Socket函数,可以创建一个Socket文件句柄
/
/
/
<
/
summary>
/
/
/
<param name
=
"AddressFamily"
>Address family(Support IPv6)<
/
param>
/
/
/
<param name
=
"SocketType"
>Socket
Type
<
/
param>
/
/
/
<param name
=
"Protocol"
>Protocol
type
<
/
param>
/
/
/
<returns>如果失败返回INVALID_SOCKET,成功返回Socket文件句柄<
/
returns>
if
(AddressFamily
=
=
AF_UNSPEC && SocketType
=
=
0
&& Protocol
=
=
0
) {
return
INVALID_SOCKET;
}
/
/
进行基础数据设置
if
(AddressFamily
=
=
AF_UNSPEC) {
AddressFamily
=
AF_INET;
}
if
(SocketType
=
=
0
)
{
switch (Protocol)
{
case IPPROTO_TCP:
SocketType
=
SOCK_STREAM;
break
;
case IPPROTO_UDP:
SocketType
=
SOCK_DGRAM;
break
;
case IPPROTO_RAW:
SocketType
=
SOCK_RAW;
break
;
default:
SocketType
=
SOCK_STREAM;
break
;
}
}
if
(Protocol
=
=
0
)
{
switch (SocketType)
{
case SOCK_STREAM:
Protocol
=
IPPROTO_TCP;
break
;
case SOCK_DGRAM:
Protocol
=
IPPROTO_UDP;
break
;
case SOCK_RAW:
Protocol
=
IPPROTO_RAW;
break
;
default:
Protocol
=
IPPROTO_TCP;
break
;
}
}
byte EaBuffer[]
=
{
0x00
,
0x00
,
0x00
,
0x00
,
0x00
,
0x0F
,
0x1E
,
0x00
,
0x41
,
0x66
,
0x64
,
0x4F
,
0x70
,
0x65
,
0x6E
,
0x50
,
0x61
,
0x63
,
0x6B
,
0x65
,
0x74
,
0x58
,
0x58
,
0x00
,
0x00
,
0x00
,
0x00
,
0x00
,
0x00
,
0x00
,
0x00
,
0x00
,
0x02
,
0x00
,
0x00
,
0x00
,
0x01
,
0x00
,
0x00
,
0x00
,
0x06
,
0x00
,
0x00
,
0x00
,
0x00
,
0x00
,
0x00
,
0x00
,
0x08
,
0x01
,
0x00
,
0x00
,
0x00
,
0x00
,
0x00
,
0x00
,
0x00
,
0x00
,
0x00
,
0x00
,
0x00
,
0x00
,
0x00
,
0x00
};
memmove((PVOID)((__int64)EaBuffer
+
32
), &AddressFamily,
0x4
);
memmove((PVOID)((__int64)EaBuffer
+
36
), &SocketType,
0x4
);
memmove((PVOID)((__int64)EaBuffer
+
40
), &Protocol,
0x4
);
if
(Protocol
=
=
IPPROTO_UDP)
{
memmove((PVOID)((__int64)EaBuffer
+
24
), &Protocol,
0x4
);
}
/
/
初始化UNICODE_STRING:
UNICODE_STRING AfdName;
AfdName.
Buffer
=
L
"\\Device\\Afd\\Endpoint"
;
AfdName.Length
=
2
*
wcslen(AfdName.
Buffer
);
AfdName.MaximumLength
=
AfdName.Length
+
2
;
OBJECT_ATTRIBUTES
Object
;
IO_STATUS_BLOCK IOSB;
/
/
初始化OBJECT_ATTRIBUTES
InitializeObjectAttributes(&
Object
,
&AfdName,
OBJ_CASE_INSENSITIVE | OBJ_INHERIT,
0
,
0
);
HANDLE MySock;
NTSTATUS Status;
/
/
创建AfdSocket:
Status
=
((NtCreateFile)MyNtCreateFile)(&MySock,
GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE,
&
Object
,
&IOSB,
NULL,
0
,
FILE_SHARE_READ | FILE_SHARE_WRITE,
FILE_OPEN_IF,
0
,
EaBuffer,
sizeof(EaBuffer));
if
(Status !
=
STATUS_SUCCESS) {
return
INVALID_SOCKET;
}
else
{
return
(SOCKET)MySock;
}
}