diff options
author | Nathan Evans <evans@in.tum.de> | 2008-05-14 03:03:45 +0000 |
---|---|---|
committer | Nathan Evans <evans@in.tum.de> | 2008-05-14 03:03:45 +0000 |
commit | 2fbef21403b73adcd31f8c438431cf8ae3388195 (patch) | |
tree | fcdd2d7042279fe7a3c2741056f007e94fb5af5d | |
parent | 581da6c02d51a1c6f899f1aba8944466856b54db (diff) | |
download | libmicrohttpd-2fbef21403b73adcd31f8c438431cf8ae3388195.tar.gz libmicrohttpd-2fbef21403b73adcd31f8c438431cf8ae3388195.zip |
-rw-r--r-- | src/examples/tor_dos_server.c | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/src/examples/tor_dos_server.c b/src/examples/tor_dos_server.c new file mode 100644 index 00000000..dba98c41 --- /dev/null +++ b/src/examples/tor_dos_server.c | |||
@@ -0,0 +1,109 @@ | |||
1 | /* | ||
2 | This file is part of libmicrohttpd | ||
3 | (C) 2007 Christian Grothoff | ||
4 | |||
5 | libmicrohttpd is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 2, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | libmicrohttpd is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with libmicrohttpd; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file tor_dos_server.c | ||
23 | * @brief serve up random data forever | ||
24 | * @author Meh | ||
25 | */ | ||
26 | |||
27 | #include "config.h" | ||
28 | #include <microhttpd.h> | ||
29 | #include <stdlib.h> | ||
30 | #include <sys/types.h> | ||
31 | #include <sys/stat.h> | ||
32 | #ifndef MINGW | ||
33 | #include <unistd.h> | ||
34 | #endif | ||
35 | #include <string.h> | ||
36 | #include <stdio.h> | ||
37 | #include <sys/time.h> | ||
38 | |||
39 | #define PAGE "<html><head><title>File not found</title></head><body>File not found</body></html>" | ||
40 | |||
41 | static int | ||
42 | random_data_feeder (void *cls, size_t pos, char *buf, int max) | ||
43 | { | ||
44 | |||
45 | memset(buf,'d',max); | ||
46 | |||
47 | return 1; | ||
48 | } | ||
49 | |||
50 | static int | ||
51 | ahc_echo (void *cls, | ||
52 | struct MHD_Connection *connection, | ||
53 | const char *url, | ||
54 | const char *method, | ||
55 | const char *upload_data, | ||
56 | const char *version, unsigned int *upload_data_size, void **ptr) | ||
57 | { | ||
58 | static int aptr; | ||
59 | struct MHD_Response *response; | ||
60 | int ret; | ||
61 | struct stat buf; | ||
62 | |||
63 | fprintf(stderr,"received request!\n"); | ||
64 | if (0 != strcmp (method, MHD_HTTP_METHOD_GET)) | ||
65 | { | ||
66 | fprintf(stderr,"Unknown method! %s\n",method); | ||
67 | return MHD_NO; /* unexpected method */ | ||
68 | } | ||
69 | if (&aptr != *ptr) | ||
70 | { | ||
71 | /* do never respond on first call */ | ||
72 | *ptr = &aptr; | ||
73 | return MHD_YES; | ||
74 | } | ||
75 | |||
76 | response = MHD_create_response_from_callback (-1, 32 * 1024, /* 32k page size */ | ||
77 | &random_data_feeder, | ||
78 | NULL, | ||
79 | (MHD_ContentReaderFreeCallback) | ||
80 | & fclose); | ||
81 | ret = MHD_queue_response (connection, MHD_HTTP_OK, response); | ||
82 | MHD_destroy_response (response); | ||
83 | |||
84 | |||
85 | return ret; | ||
86 | } | ||
87 | |||
88 | int | ||
89 | main (int argc, char *const *argv) | ||
90 | { | ||
91 | struct MHD_Daemon *d; | ||
92 | |||
93 | if (argc != 3) | ||
94 | { | ||
95 | printf ("%s PORT SECONDS-TO-RUN\n", argv[0]); | ||
96 | return 1; | ||
97 | } | ||
98 | d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG, | ||
99 | atoi (argv[1]), | ||
100 | NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_CONNECTION_MEMORY_LIMIT, 1024 * 1024 * 10, MHD_OPTION_END); | ||
101 | if (d == NULL) | ||
102 | return 1; | ||
103 | |||
104 | sleep(atoi(argv[2])); | ||
105 | |||
106 | |||
107 | MHD_stop_daemon (d); | ||
108 | return 0; | ||
109 | } | ||