ddos/client.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Fri, 19 Nov 2010 12:23:24 +0100
changeset 158 2cddd4d26139
parent 157 e8fbd0653fda
child 160 26414889dded
permissions -rw-r--r--
removed unnecessary #includes committer: Markus Bröker <mbroeker@largo.homelinux.org>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     1
/**
77
49e0babccb23 HEADER TAGS
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
     2
 * ddos/client.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
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     4
 */
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     5
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     6
#include <stdio.h>
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     7
#include <stdlib.h>
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     8
#include <string.h>
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     9
#include <unistd.h>
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    10
#include <sys/socket.h>
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    11
#include <netinet/in.h>
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    12
#include <arpa/inet.h>
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    13
#include <sys/types.h>
157
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    14
#include <netdb.h>
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    15
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    16
#define MAXLEN 80
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    17
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    18
int client_write (int client_socket, char *s)
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    19
{
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    20
    return write (client_socket, s, strlen (s));
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    21
}
0
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    22
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    23
int main (int argc, char **argv)
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    24
{
157
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    25
    char message[MAXLEN + 1];
0
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    26
    int client_socket;
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    27
    int status;
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    28
157
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    29
    struct addrinfo hints, *result, *rp;
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
    memset (&hints, 0, sizeof (struct addrinfo));
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    32
    hints.ai_family = AF_UNSPEC;
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    33
    hints.ai_socktype = SOCK_STREAM;
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    34
    hints.ai_flags = 0;
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    35
    hints.ai_protocol = IPPROTO_TCP;
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    36
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    37
    if (getaddrinfo ("localhost", "4000", &hints, &result) != 0) {
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    38
        perror ("getaddrinfo");
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    39
        return -1;
0
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    40
    }
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    41
157
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    42
    client_socket = -1;
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    43
    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
    44
        if ((client_socket = socket (rp->ai_family, rp->ai_socktype, rp->ai_protocol)) == -1)
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    45
            continue;
0
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    46
157
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    47
        if (connect (client_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
    48
            break;
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
        close (client_socket);
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    51
        client_socket = -1;
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    52
    }
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    53
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    54
    if (result != NULL)
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    55
        freeaddrinfo (result);
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
    status = client_write (client_socket, "Hello\r\n");
0
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    58
    while (client_socket > 0) {
157
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    59
        status = read (client_socket, message, MAXLEN);
0
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    60
        if (status > 0) {
157
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    61
            message[status] = '\0';
0
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    62
            printf ("SERVER RESPONS: %s", message);
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    63
        } else {
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    64
            perror ("READ");
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    65
            close (client_socket);
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    66
            return 1;
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    67
        }
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    68
    }
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    69
10
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 9
diff changeset
    70
    return close (client_socket);
0
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    71
}