author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Fri, 19 Nov 2010 14:49:47 +0100 | |
changeset 159 | 44823a881da1 |
parent 157 | e8fbd0653fda |
child 160 | 26414889dded |
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> |
|
157
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
10 |
#include <sys/types.h> |
0 | 11 |
#include <sys/socket.h> |
12 |
#include <netinet/in.h> |
|
157
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
13 |
#include <netdb.h> |
0 | 14 |
#include <arpa/inet.h> |
15 |
#include <sys/types.h> |
|
16 |
#include <signal.h> |
|
17 |
||
157
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
18 |
#define MAXLEN 80 |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
19 |
|
68 | 20 |
int set_proc_limit (int); |
0 | 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 |
{ |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
24 |
return write (client_socket, s, strlen (s)); |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
25 |
} |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
26 |
|
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
27 |
int server_init (char *port) |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
28 |
{ |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
29 |
int server_socket, status; |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
30 |
struct addrinfo hints; |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
31 |
struct addrinfo *result, *rp; |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
32 |
|
159
44823a881da1
set default socket options: IPV6_V6ONLY, SO_REUSEADDR
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
157
diff
changeset
|
33 |
int yes = 1, no = 0; |
157
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
34 |
|
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
35 |
memset (&hints, 0, sizeof (hints)); |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
36 |
hints.ai_family = AF_UNSPEC; |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
37 |
hints.ai_socktype = SOCK_STREAM; |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
38 |
hints.ai_flags = AI_PASSIVE; |
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 |
if (getaddrinfo (NULL, "4000", &hints, &result) < 0) { |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
41 |
perror ("GETADDRINFO"); |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
42 |
return -1; |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
43 |
} |
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 |
server_socket = -1; |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
46 |
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
|
47 |
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
|
48 |
perror ("SOCKET"); |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
49 |
continue; |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
50 |
} |
159
44823a881da1
set default socket options: IPV6_V6ONLY, SO_REUSEADDR
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
157
diff
changeset
|
51 |
// enable rebinding of the socket |
157
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
52 |
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
|
53 |
perror ("setsockopt"); |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
54 |
return -1; |
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 |
// 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
|
57 |
if (rp->ai_family == AF_INET6) |
44823a881da1
set default socket options: IPV6_V6ONLY, SO_REUSEADDR
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
157
diff
changeset
|
58 |
setsockopt (server_socket, IPPROTO_IPV6, IPV6_V6ONLY, &no, sizeof (int)); |
157
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
59 |
|
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
60 |
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
|
61 |
perror ("BIND"); |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
62 |
close (server_socket); |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
63 |
server_socket = -1; |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
64 |
} else { |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
65 |
printf ("bound to an %s socket\n", (rp->ai_family == AF_INET6) ? "IPv6" : "IPv4"); |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
66 |
} |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
67 |
} |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
68 |
|
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
69 |
status = listen (server_socket, 10); |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
70 |
|
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
71 |
if (status != 0) { |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
72 |
perror ("LISTEN"); |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
73 |
return -1; |
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 |
|
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
76 |
if (result != NULL) |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
77 |
freeaddrinfo (result); |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
78 |
|
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
79 |
return server_socket; |
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 |
|
0 | 82 |
int main (int argc, char **argv) |
83 |
{ |
|
84 |
int server_socket; |
|
85 |
int client_socket; |
|
157
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 |
char message[MAXLEN + 1]; |
0 | 88 |
int len; |
157
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
89 |
|
0 | 90 |
pid_t pid; |
91 |
pid_t parent_pid; |
|
92 |
||
157
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
93 |
if ((server_socket = server_init ("4000")) < 0) |
0 | 94 |
return EXIT_FAILURE; |
95 |
||
96 |
int links = 0; |
|
97 |
||
98 |
parent_pid = getpid (); |
|
99 |
||
100 |
/* |
|
101 |
* Child quits immediately, father mustn't wait |
|
102 |
*/ |
|
103 |
signal (SIGCHLD, SIG_IGN); |
|
104 |
||
68 | 105 |
if (set_proc_limit (200) != 0) { |
0 | 106 |
printf ("Cannot limit the process limit\n"); |
107 |
return EXIT_FAILURE; |
|
108 |
} |
|
109 |
||
110 |
for (;;) { |
|
157
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
111 |
client_socket = accept (server_socket, NULL, NULL); |
0 | 112 |
|
113 |
printf ("PARENT PID = %d\n", parent_pid); |
|
114 |
||
115 |
pid = fork (); |
|
116 |
switch (pid) { |
|
117 |
case 0: /* Child */ |
|
118 |
close (server_socket); |
|
157
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
119 |
if ((len = read (client_socket, message, MAXLEN)) == -1) |
0 | 120 |
break; |
157
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
121 |
message[len] = '\0'; |
0 | 122 |
|
157
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
123 |
len = server_write (client_socket, "Please wait...\r\n"); |
0 | 124 |
sleep (10); |
125 |
||
126 |
printf ("DELETING LINK [%04d] %04d\n", getpid (), links--); |
|
127 |
shutdown (client_socket, SHUT_RDWR); |
|
128 |
close (client_socket); |
|
129 |
return 1; |
|
130 |
case -1: |
|
131 |
perror ("Fork Error"); |
|
132 |
close (client_socket); |
|
133 |
/* |
|
157
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
134 |
* server is still alive and will continue when resources are available |
0 | 135 |
*/ |
136 |
break; |
|
137 |
default: /* PID > 0 */ |
|
138 |
close (client_socket); |
|
139 |
printf ("Starting LINK [%04d] %04d\n", pid, links++); |
|
140 |
} |
|
141 |
} |
|
10
f19f44e2e863
a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
142 |
|
f19f44e2e863
a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
143 |
return close (server_socket); |
0 | 144 |
} |