connection.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Sat, 13 Nov 2010 04:05:14 +0100
changeset 153 b223089872b6
parent 131 b5ad49852adc
child 163 780bf4f348f3
permissions -rw-r--r--
ipv6 support added committer: Markus Bröker <mbroeker@largo.homelinux.org>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     1
/**
77
49e0babccb23 HEADER TAGS
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 48
diff changeset
     2
 * connection.c
10
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     3
 * Copyright (C) 2008 mussolini
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     4
 * Copyright (C) 2008 mbroeker
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     5
 */
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     6
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     7
#include <stdio.h>
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     8
#include <stdlib.h>
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     9
#include <unistd.h>
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    10
#include <string.h>
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    11
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    12
#include <netdb.h>
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    13
#include <arpa/inet.h>
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    14
111
2247473fd12d bsd style socket handling...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 84
diff changeset
    15
#include <netinet/in.h>
2247473fd12d bsd style socket handling...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 84
diff changeset
    16
#include <sys/types.h>
2247473fd12d bsd style socket handling...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 84
diff changeset
    17
#include <sys/socket.h>
2247473fd12d bsd style socket handling...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 84
diff changeset
    18
153
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    19
int connection (char *server, char *port)
10
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    20
{
153
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    21
    struct addrinfo hints;
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    22
    struct addrinfo *result, *rp;
27
81a574d60c15 typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    23
153
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    24
    int sockfd = -1;
10
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    25
153
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    26
    memset (&hints, 0, sizeof (struct addrinfo));
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    27
    hints.ai_family = AF_UNSPEC;
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    28
    hints.ai_socktype = SOCK_STREAM;
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    29
    hints.ai_flags = 0;
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    30
    hints.ai_protocol = IPPROTO_TCP;
10
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    31
153
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    32
    if (getaddrinfo (server, port, &hints, &result) != 0) {
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    33
        perror ("getaddrinfo");
10
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    34
        return -1;
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    35
    }
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    36
153
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    37
    for (rp = result; rp != NULL; rp = rp->ai_next) {
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    38
        if ((sockfd = socket (rp->ai_family, rp->ai_socktype, rp->ai_protocol)) == -1)
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    39
            continue;
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    40
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    41
        if (connect (sockfd, rp->ai_addr, rp->ai_addrlen) != -1) {
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    42
            break;
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    43
        }
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    44
        close (sockfd);
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    45
        sockfd = -1;
10
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    46
    }
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    47
153
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    48
    if (result != NULL)
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    49
        freeaddrinfo (result);
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    50
10
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    51
    return (sockfd);
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    52
}
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    53
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    54
int main (int argc, char **argv)
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    55
{
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    56
    char buffer[1024];
27
81a574d60c15 typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    57
10
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    58
    int sockfd;
27
81a574d60c15 typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    59
10
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    60
    int num;
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    61
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    62
    if (argc != 3) {
153
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    63
        printf ("Usage: %s <ipaddr> <service>\n", argv[0]);
48
b94d657a9acb Policy Inonsistency on many files
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 29
diff changeset
    64
        return EXIT_FAILURE;
10
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    65
    }
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    66
153
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    67
    if ((sockfd = connection (argv[1], argv[2])) < 0) {
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    68
        printf ("Connection error: %s:%s\n", argv[1], argv[2]);
10
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    69
        return EXIT_FAILURE;
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    70
    }
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    71
153
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    72
    if (write (sockfd, "GET / HTTP/1.0\r\n\r\n", 18) == -1) {
131
b5ad49852adc check for the return values
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 111
diff changeset
    73
        perror ("write");
b5ad49852adc check for the return values
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 111
diff changeset
    74
        return EXIT_FAILURE;
b5ad49852adc check for the return values
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 111
diff changeset
    75
    }
10
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    76
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    77
    while ((num = read (sockfd, buffer, 1023)) != 0) {
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    78
        buffer[num] = 0;
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    79
        printf ("%s", buffer);
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    80
    }
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    81
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    82
    return close (sockfd);
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    83
}