ddos/client.c
author Markus Bröker<broeker.markus@googlemail.com>
Sat, 21 Oct 2017 13:45:05 +0200
changeset 171 c6e0af68825a
parent 160 26414889dded
permissions -rw-r--r--
Entrypoint and RET fixed
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 <sys/types.h>
157
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: 157
diff changeset
    13
#include <netinet/in.h>
157
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    14
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    15
#define MAXLEN 80
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    16
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    17
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
    18
{
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    19
    return write (client_socket, s, strlen (s));
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    20
}
0
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    21
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    22
int main (int argc, char **argv)
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    23
{
157
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    24
    char message[MAXLEN + 1];
0
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    25
    int client_socket;
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    26
    int status;
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    27
157
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    28
    struct addrinfo hints, *result, *rp;
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    29
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    30
    memset (&hints, 0, sizeof (struct addrinfo));
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    31
    hints.ai_family = AF_UNSPEC;
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    32
    hints.ai_socktype = SOCK_STREAM;
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    33
    hints.ai_flags = 0;
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    34
    hints.ai_protocol = IPPROTO_TCP;
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    35
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    36
    if (getaddrinfo ("localhost", "4000", &hints, &result) != 0) {
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    37
        perror ("getaddrinfo");
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    38
        return -1;
0
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    39
    }
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    40
157
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    41
    client_socket = -1;
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    42
    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
    43
        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
    44
            continue;
0
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    45
157
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    46
        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
    47
            break;
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
        close (client_socket);
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    50
        client_socket = -1;
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
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    53
    if (result != NULL)
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    54
        freeaddrinfo (result);
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
    status = client_write (client_socket, "Hello\r\n");
0
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    57
    while (client_socket > 0) {
157
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    58
        status = read (client_socket, message, MAXLEN);
0
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    59
        if (status > 0) {
157
e8fbd0653fda Simplified IPv6 aware ddos demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    60
            message[status] = '\0';
0
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    61
            printf ("SERVER RESPONS: %s", message);
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    62
        } else {
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    63
            perror ("READ");
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    64
            close (client_socket);
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    65
            return 1;
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    66
        }
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
10
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 9
diff changeset
    69
    return close (client_socket);
0
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    70
}