author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Thu, 16 Apr 2009 12:49:12 +0200 | |
changeset 51 | a03372ef9714 |
parent 10 | f19f44e2e863 |
child 68 | d549894aa6a9 |
permissions | -rw-r--r-- |
0 | 1 |
/** |
9
c3fecc82ade6
standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
2 |
* test/demos/ddos/server.c |
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> |
|
10 |
#include <sys/socket.h> |
|
11 |
#include <netinet/in.h> |
|
12 |
#include <arpa/inet.h> |
|
13 |
#include <sys/types.h> |
|
14 |
#include <signal.h> |
|
15 |
||
16 |
int set_limit (int); |
|
17 |
||
18 |
int main (int argc, char **argv) |
|
19 |
{ |
|
20 |
char message[81]; |
|
21 |
int server_socket; |
|
22 |
int client_socket; |
|
23 |
struct sockaddr_in sa; |
|
24 |
struct sockaddr_in ca; |
|
25 |
socklen_t size; |
|
26 |
int len; |
|
27 |
int status; |
|
28 |
pid_t pid; |
|
29 |
pid_t parent_pid; |
|
30 |
||
31 |
server_socket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); |
|
32 |
if (server_socket == -1) { |
|
33 |
perror ("socket"); |
|
34 |
return EXIT_FAILURE; |
|
35 |
} |
|
36 |
||
37 |
sa.sin_family = AF_INET; |
|
38 |
sa.sin_port = htons (4000); |
|
39 |
sa.sin_addr.s_addr = INADDR_ANY; |
|
40 |
||
41 |
size = sizeof (sa); |
|
42 |
||
43 |
status = bind (server_socket, (struct sockaddr *)&sa, size); |
|
44 |
if (status != 0) { |
|
45 |
perror ("BIND"); |
|
46 |
return EXIT_FAILURE; |
|
47 |
} |
|
48 |
||
49 |
status = listen (server_socket, 10); |
|
50 |
if (status != 0) { |
|
51 |
perror ("LISTEN"); |
|
52 |
return EXIT_FAILURE; |
|
53 |
} |
|
54 |
||
55 |
int links = 0; |
|
56 |
||
57 |
parent_pid = getpid (); |
|
58 |
||
59 |
/* |
|
60 |
* Child quits immediately, father mustn't wait |
|
61 |
*/ |
|
62 |
signal (SIGCHLD, SIG_IGN); |
|
63 |
||
64 |
if (set_limit (500) != 0) { |
|
65 |
printf ("Cannot limit the process limit\n"); |
|
66 |
return EXIT_FAILURE; |
|
67 |
} |
|
68 |
||
69 |
for (;;) { |
|
70 |
size = sizeof (ca); |
|
71 |
client_socket = accept (server_socket, (struct sockaddr *)&sa, (socklen_t *) & size); |
|
72 |
||
73 |
printf ("PARENT PID = %d\n", parent_pid); |
|
74 |
||
75 |
pid = fork (); |
|
76 |
switch (pid) { |
|
77 |
case 0: /* Child */ |
|
78 |
close (server_socket); |
|
79 |
if ((len = read (client_socket, message, 80)) == -1) |
|
80 |
break; |
|
81 |
message[len] = 0; |
|
82 |
||
83 |
len = write (client_socket, "Please wait...\r\n", 17); |
|
84 |
sleep (10); |
|
85 |
||
86 |
printf ("DELETING LINK [%04d] %04d\n", getpid (), links--); |
|
87 |
shutdown (client_socket, SHUT_RDWR); |
|
88 |
close (client_socket); |
|
89 |
return 1; |
|
90 |
case -1: |
|
91 |
perror ("Fork Error"); |
|
92 |
close (client_socket); |
|
93 |
/* |
|
94 |
* server is still alive and will continue |
|
95 |
* * when resources are available |
|
96 |
*/ |
|
97 |
break; |
|
98 |
default: /* PID > 0 */ |
|
99 |
close (client_socket); |
|
100 |
printf ("Starting LINK [%04d] %04d\n", pid, links++); |
|
101 |
} |
|
102 |
} |
|
10
f19f44e2e863
a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
103 |
|
f19f44e2e863
a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
104 |
return close (server_socket); |
0 | 105 |
} |