author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Fri, 19 Nov 2010 12:23:24 +0100 | |
changeset 158 | 2cddd4d26139 |
parent 157 | e8fbd0653fda |
child 159 | 44823a881da1 |
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 |
|
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
33 |
int yes = 1; |
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 |
} |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
51 |
|
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 |
} |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
56 |
|
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
57 |
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
|
58 |
perror ("BIND"); |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
59 |
close (server_socket); |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
60 |
server_socket = -1; |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
61 |
} else { |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
62 |
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
|
63 |
} |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
64 |
} |
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 |
status = listen (server_socket, 10); |
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 |
if (status != 0) { |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
69 |
perror ("LISTEN"); |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
70 |
return -1; |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
71 |
} |
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 |
if (result != NULL) |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
74 |
freeaddrinfo (result); |
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 |
return server_socket; |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
77 |
} |
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
78 |
|
0 | 79 |
int main (int argc, char **argv) |
80 |
{ |
|
81 |
int server_socket; |
|
82 |
int client_socket; |
|
157
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
83 |
|
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
84 |
char message[MAXLEN + 1]; |
0 | 85 |
int len; |
157
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
86 |
|
0 | 87 |
pid_t pid; |
88 |
pid_t parent_pid; |
|
89 |
||
157
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
90 |
if ((server_socket = server_init ("4000")) < 0) |
0 | 91 |
return EXIT_FAILURE; |
92 |
||
93 |
int links = 0; |
|
94 |
||
95 |
parent_pid = getpid (); |
|
96 |
||
97 |
/* |
|
98 |
* Child quits immediately, father mustn't wait |
|
99 |
*/ |
|
100 |
signal (SIGCHLD, SIG_IGN); |
|
101 |
||
68 | 102 |
if (set_proc_limit (200) != 0) { |
0 | 103 |
printf ("Cannot limit the process limit\n"); |
104 |
return EXIT_FAILURE; |
|
105 |
} |
|
106 |
||
107 |
for (;;) { |
|
157
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
108 |
client_socket = accept (server_socket, NULL, NULL); |
0 | 109 |
|
110 |
printf ("PARENT PID = %d\n", parent_pid); |
|
111 |
||
112 |
pid = fork (); |
|
113 |
switch (pid) { |
|
114 |
case 0: /* Child */ |
|
115 |
close (server_socket); |
|
157
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
116 |
if ((len = read (client_socket, message, MAXLEN)) == -1) |
0 | 117 |
break; |
157
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
118 |
message[len] = '\0'; |
0 | 119 |
|
157
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
120 |
len = server_write (client_socket, "Please wait...\r\n"); |
0 | 121 |
sleep (10); |
122 |
||
123 |
printf ("DELETING LINK [%04d] %04d\n", getpid (), links--); |
|
124 |
shutdown (client_socket, SHUT_RDWR); |
|
125 |
close (client_socket); |
|
126 |
return 1; |
|
127 |
case -1: |
|
128 |
perror ("Fork Error"); |
|
129 |
close (client_socket); |
|
130 |
/* |
|
157
e8fbd0653fda
Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
131 |
* server is still alive and will continue when resources are available |
0 | 132 |
*/ |
133 |
break; |
|
134 |
default: /* PID > 0 */ |
|
135 |
close (client_socket); |
|
136 |
printf ("Starting LINK [%04d] %04d\n", pid, links++); |
|
137 |
} |
|
138 |
} |
|
10
f19f44e2e863
a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
139 |
|
f19f44e2e863
a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
140 |
return close (server_socket); |
0 | 141 |
} |