author | Markus Bröker<broeker.markus@googlemail.com> |
Sun, 10 Feb 2019 13:17:01 +0100 | |
changeset 173 | 374a86886bc5 |
parent 161 | 36f763c8ab16 |
permissions | -rw-r--r-- |
0 | 1 |
/** |
77 | 2 |
* ddos/server.c |
9
c3fecc82ade6
standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
3 |
* Copyright (C) 2008 Markus Broeker |
0 | 4 |
*/ |
5 |
||
6 |
#include <stdio.h> |
|
7 |
#include <stdlib.h> |
|
8 |
#include <string.h> |
|
9 |
#include <unistd.h> |
|
160
26414889dded
removed unneeded includes and code cleanups
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
159
diff
changeset
|
10 |
#include <sys/socket.h> |
157
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
11 |
#include <sys/types.h> |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
12 |
#include <netdb.h> |
160
26414889dded
removed unneeded includes and code cleanups
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
159
diff
changeset
|
13 |
#include <netinet/in.h> |
0 | 14 |
#include <signal.h> |
15 |
||
68 | 16 |
int set_proc_limit (int); |
0 | 17 |
|
160
26414889dded
removed unneeded includes and code cleanups
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
159
diff
changeset
|
18 |
#define SVPORT "4000" |
26414889dded
removed unneeded includes and code cleanups
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
159
diff
changeset
|
19 |
#define MAXLEN 1024 |
26414889dded
removed unneeded includes and code cleanups
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
159
diff
changeset
|
20 |
#define MAXMEM 200 |
26414889dded
removed unneeded includes and code cleanups
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
159
diff
changeset
|
21 |
|
157
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
22 |
int server_write (int client_socket, char *s) |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
23 |
{ |
160
26414889dded
removed unneeded includes and code cleanups
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
159
diff
changeset
|
24 |
if (s == NULL) { |
26414889dded
removed unneeded includes and code cleanups
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
159
diff
changeset
|
25 |
fprintf (stderr, "Invalid data: s == NULL"); |
26414889dded
removed unneeded includes and code cleanups
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
159
diff
changeset
|
26 |
return -1; |
26414889dded
removed unneeded includes and code cleanups
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
159
diff
changeset
|
27 |
} |
26414889dded
removed unneeded includes and code cleanups
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
159
diff
changeset
|
28 |
|
157
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
29 |
return write (client_socket, s, strlen (s)); |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
30 |
} |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
31 |
|
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
32 |
int server_init (char *port) |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
33 |
{ |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
34 |
int server_socket, status; |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
35 |
struct addrinfo hints; |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
36 |
struct addrinfo *result, *rp; |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
37 |
|
159
44823a881da1
set default socket options: IPV6_V6ONLY, SO_REUSEADDR
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
157
diff
changeset
|
38 |
int yes = 1, no = 0; |
157
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
39 |
|
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
40 |
memset (&hints, 0, sizeof (hints)); |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
41 |
hints.ai_family = AF_UNSPEC; |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
42 |
hints.ai_socktype = SOCK_STREAM; |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
43 |
hints.ai_flags = AI_PASSIVE; |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
44 |
|
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
45 |
if (getaddrinfo (NULL, "4000", &hints, &result) < 0) { |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
46 |
perror ("GETADDRINFO"); |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
47 |
return -1; |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
48 |
} |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
49 |
|
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
50 |
server_socket = -1; |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
51 |
for (rp = result; rp != NULL; rp = rp->ai_next) { |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
52 |
if ((server_socket = socket (rp->ai_family, rp->ai_socktype, rp->ai_protocol)) < 0) { |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
53 |
perror ("SOCKET"); |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
54 |
continue; |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
55 |
} |
159
44823a881da1
set default socket options: IPV6_V6ONLY, SO_REUSEADDR
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
157
diff
changeset
|
56 |
// enable rebinding of the socket |
157
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
57 |
if (setsockopt (server_socket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof (int)) == -1) { |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
58 |
perror ("setsockopt"); |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
59 |
return -1; |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
60 |
} |
159
44823a881da1
set default socket options: IPV6_V6ONLY, SO_REUSEADDR
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
157
diff
changeset
|
61 |
// disable IPV6ONLY, eg enable an hybrid socket |
44823a881da1
set default socket options: IPV6_V6ONLY, SO_REUSEADDR
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
157
diff
changeset
|
62 |
if (rp->ai_family == AF_INET6) |
160
26414889dded
removed unneeded includes and code cleanups
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
159
diff
changeset
|
63 |
if (setsockopt (server_socket, IPPROTO_IPV6, IPV6_V6ONLY, &no, sizeof (int)) == -1) |
26414889dded
removed unneeded includes and code cleanups
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
159
diff
changeset
|
64 |
perror ("setsockopt"); |
157
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
65 |
|
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
66 |
if ((bind (server_socket, rp->ai_addr, rp->ai_addrlen)) == -1) { |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
67 |
perror ("BIND"); |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
68 |
close (server_socket); |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
69 |
server_socket = -1; |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
70 |
} else { |
160
26414889dded
removed unneeded includes and code cleanups
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
159
diff
changeset
|
71 |
printf ("bound to an %s socket on port %s\n", (rp->ai_family == AF_INET6) ? "IPv6" : "IPv4", port); |
157
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
72 |
} |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
73 |
} |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
74 |
|
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
75 |
status = listen (server_socket, 10); |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
76 |
|
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
77 |
if (status != 0) { |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
78 |
perror ("LISTEN"); |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
79 |
return -1; |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
80 |
} |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
81 |
|
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
82 |
if (result != NULL) |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
83 |
freeaddrinfo (result); |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
84 |
|
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
85 |
return server_socket; |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
86 |
} |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
87 |
|
0 | 88 |
int main (int argc, char **argv) |
89 |
{ |
|
90 |
int server_socket; |
|
91 |
int client_socket; |
|
157
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
92 |
|
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
93 |
char message[MAXLEN + 1]; |
0 | 94 |
int len; |
157
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
95 |
|
0 | 96 |
pid_t pid; |
97 |
pid_t parent_pid; |
|
98 |
||
160
26414889dded
removed unneeded includes and code cleanups
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
159
diff
changeset
|
99 |
if ((server_socket = server_init (SVPORT)) < 0) |
0 | 100 |
return EXIT_FAILURE; |
101 |
||
102 |
int links = 0; |
|
103 |
||
104 |
parent_pid = getpid (); |
|
105 |
||
106 |
/* |
|
107 |
* Child quits immediately, father mustn't wait |
|
108 |
*/ |
|
109 |
signal (SIGCHLD, SIG_IGN); |
|
110 |
||
68 | 111 |
if (set_proc_limit (200) != 0) { |
161
36f763c8ab16
spell checking: 'cannot set the limit' sounds better...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
160
diff
changeset
|
112 |
printf ("Cannot set the process limit\n"); |
0 | 113 |
return EXIT_FAILURE; |
114 |
} |
|
115 |
||
116 |
for (;;) { |
|
157
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
117 |
client_socket = accept (server_socket, NULL, NULL); |
0 | 118 |
|
119 |
printf ("PARENT PID = %d\n", parent_pid); |
|
120 |
||
121 |
pid = fork (); |
|
122 |
switch (pid) { |
|
123 |
case 0: /* Child */ |
|
124 |
close (server_socket); |
|
157
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
125 |
if ((len = read (client_socket, message, MAXLEN)) == -1) |
0 | 126 |
break; |
157
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
127 |
message[len] = '\0'; |
0 | 128 |
|
157
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
129 |
len = server_write (client_socket, "Please wait...\r\n"); |
0 | 130 |
sleep (10); |
131 |
||
132 |
printf ("DELETING LINK [%04d] %04d\n", getpid (), links--); |
|
133 |
shutdown (client_socket, SHUT_RDWR); |
|
134 |
close (client_socket); |
|
135 |
return 1; |
|
136 |
case -1: |
|
137 |
perror ("Fork Error"); |
|
138 |
close (client_socket); |
|
139 |
/* |
|
157
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
140 |
* server is still alive and will continue when resources are available |
0 | 141 |
*/ |
142 |
break; |
|
143 |
default: /* PID > 0 */ |
|
144 |
close (client_socket); |
|
145 |
printf ("Starting LINK [%04d] %04d\n", pid, links++); |
|
146 |
} |
|
147 |
} |
|
10
f19f44e2e863
a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
148 |
|
f19f44e2e863
a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
149 |
return close (server_socket); |
0 | 150 |
} |