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
| #include <sys/types.h>
#ifdef UNIX /* UNIX/LINUX only. */
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#else
#include <winsock.h>
#include <windows.h>
#endif
#include <string.h>
#include <time.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "log.h"
#include "base64.h"
#define DATALEN 1024
#define SLEEPTIME 500
char * Sendstr( char **ioBuffer, char * IPAdres, unsigned short Portnr );
int sendall(int s, char *buf, int len);
char * Sendstr( char **ioBuffer, char *IPAdres, unsigned short Portnr )
{
/*Author: Declaratie van de benodigde Variables */
char * Buffer;
char * pBuffer;
SOCKET Socket;
int rc;
int bufferlen;
#ifdef UNIX
#else
WSADATA WSAData;
#endif
struct sockaddr_in HostAddress;
LPSTR lpszAscii;
struct hostent * phost = 0;
#ifdef UNIX
#else
/*Author Windows only */
if( ( rc = WSAStartup( 2, &WSAData) ) != 0 )
{
logexit("libhttp.c","Sendstr",23,"WSAStartup failed rc=%d\n",rc);
}
#endif
/*Author zet een socket */
if( ! (Socket = socket( AF_INET, SOCK_STREAM, 0 ) ) )
{
perror("socket");
}
if( ! (phost = gethostbyname (IPAdres) ) )
{
perror("gethostbyname");
}
HostAddress.sin_family = AF_INET;
HostAddress.sin_port = htons(Portnr); /* Netwerk adressering gebruiken */
lpszAscii = IPAdres;
HostAddress.sin_addr.s_addr = ((struct in_addr *)(phost->h_addr))->s_addr;
if( (rc = connect( Socket, (SOCKADDR *)&HostAddress, sizeof( struct sockaddr_in ) ) ) != 0 )
perror("connect");
/*Author: De ingevoerde data versturen */
if( (rc = sendall( Socket, *ioBuffer, strlen(*ioBuffer) ) ) != 0 )
perror("send");
/*Author: De ingevoerde data is verstuurd, we kunnen nu deze geheugenruimte vrijgeven */
/* Free(*ioBuffer); */
/* Toch maar aan einde functie gedaan */
Buffer = (char *)malloc(DATALEN+1);
memset(Buffer,0x00,DATALEN);
Sleep(SLEEPTIME);
if( ( rc = recv( Socket, Buffer, DATALEN-1, 0 ) ) < 0)
{
perror("recieve");
}
/*Author: Als er nog bytes over zijn moeten we nog een realloc doen die dat afdekt */
while(rc > 0)
{
/* Aangezien (char *)*pBuffer momenteel niet gebruikt word kunnen we die pointer wel gebruiken */
/* om het tot nu toe gelezen deel van Buffer te bewaren */
/* Dit enkel om variabelen te sparen, netter was misschien een nieuwe pointer. */
pBuffer = strdup(Buffer);
bufferlen = strlen(Buffer)+DATALEN;
Buffer = (char *)realloc((void *)Buffer,bufferlen);
memset(Buffer,0x00,bufferlen);
strcpy(Buffer,pBuffer);
free(pBuffer);
if( ( rc = recv( Socket, Buffer+strlen(Buffer), DATALEN-1, 0 ) ) < 0)
{
perror("recieve");
}
}
if( (closesocket( Socket ) ) != 0 )
perror("close socket");
#ifdef UNIX
#else
/*Author Windows only */
WSACleanup();
#endif
free(*ioBuffer);
*ioBuffer = Buffer;
return(*ioBuffer);
}
/* http://www.ecst.csuchico.edu/~beej/guide/net/html/advanced.html#sendall */
/* Functie is overgenomen uit Beej's Guide To Network Programming */
/* Gewijzigd, er gaat geen int *len meer in maar een int len. */
int sendall(int s, char *buf, int len)
{
int total = 0; /* how many bytes we've sent */
int bytesleft = len; /* how many we have left to send */
int n;
while(total < len) {
n = send(s, buf+total, bytesleft, 0);
if (n == -1) { break; }
total += n;
bytesleft -= n;
}
return n==-1?-1:0; /* return -1 on failure, 0 on success */
}
int _stdcall httpget(char ** response, char * url, char* host, unsigned short port, int typereq)
{
char * buffer;
int getlength;
getlength = 128 + strlen(url) + strlen(host);
buffer = (char *)malloc (getlength);
sprintf(buffer,
"GET %s "
"HTTP/1.0\r\n"
"Host: %s\r\n"
"\r\n", url, host);
Sendstr( &buffer, host, port );
/*printf("%s",buffer);*/
*response = strdup(buffer);
free(buffer);
return(0);
}
int _stdcall proxyget(char ** response, char * url, char* host, char* proxy, unsigned short port, char* user, char* passwd, int typereq)
{
char * buffer;
char * secret;
char * account;
int getlength;
account = (char *)malloc(2 + strlen(user)+strlen(passwd));
memset(account,0x00, ( 1 + strlen(user)+strlen(passwd) ) );
sprintf(account,"%s:%s", user, passwd );
secret = (char*) malloc(1024);
memset(secret, 0x00, 1024);
encode_base64 (account, secret, strlen(account) );
getlength = 128 + strlen(url) + strlen(host);
buffer = (char *)malloc (getlength);
sprintf(buffer,
"GET %s "
"HTTP/1.0\r\n"
/* "Proxy-Authenticate: basic realm=\"Internet Access\"\r\n" */
"Proxy-Authorization: Basic %s\r\n"
"Proxy-Connection: Keep-Alive\r\n"
"Host: %s\r\n"
"\r\n", url, secret, host);
Sendstr( &buffer, proxy, port );
*response = strdup(buffer);
free(buffer);
free(secret);
free(account);
return(0);
} |