connection.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Wed, 01 Dec 2010 20:07:39 +0100
changeset 163 780bf4f348f3
parent 153 b223089872b6
permissions -rw-r--r--
modularized get demo 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
/**
163
780bf4f348f3 modularized get demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 153
diff changeset
     2
 * tcpconnect.c
780bf4f348f3 modularized get demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 153
diff changeset
     3
 * Copyright (C) 2010 Markus Broeker
10
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     4
 */
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
#include <stdio.h>
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     7
#include <stdlib.h>
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     8
#include <unistd.h>
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     9
#include <string.h>
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    10
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    11
#include <netdb.h>
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    12
#include <arpa/inet.h>
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    13
111
2247473fd12d bsd style socket handling...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 84
diff changeset
    14
#include <netinet/in.h>
2247473fd12d bsd style socket handling...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 84
diff changeset
    15
#include <sys/types.h>
2247473fd12d bsd style socket handling...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 84
diff changeset
    16
#include <sys/socket.h>
2247473fd12d bsd style socket handling...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 84
diff changeset
    17
163
780bf4f348f3 modularized get demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 153
diff changeset
    18
int tcpconnect (char *server, char *port)
10
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    19
{
153
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    20
    struct addrinfo hints;
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    21
    struct addrinfo *result, *rp;
27
81a574d60c15 typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    22
153
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    23
    int sockfd = -1;
10
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    24
153
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    25
    memset (&hints, 0, sizeof (struct addrinfo));
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    26
    hints.ai_family = AF_UNSPEC;
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    27
    hints.ai_socktype = SOCK_STREAM;
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    28
    hints.ai_flags = 0;
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    29
    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
    30
153
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    31
    if (getaddrinfo (server, port, &hints, &result) != 0) {
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    32
        perror ("getaddrinfo");
10
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    33
        return -1;
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    34
    }
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    35
153
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    36
    for (rp = result; rp != NULL; rp = rp->ai_next) {
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    37
        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
    38
            continue;
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    39
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    40
        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
    41
            break;
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    42
        }
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    43
        close (sockfd);
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    44
        sockfd = -1;
10
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    45
    }
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    46
153
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    47
    if (result != NULL)
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    48
        freeaddrinfo (result);
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    49
10
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    50
    return (sockfd);
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    51
}
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    52
163
780bf4f348f3 modularized get demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 153
diff changeset
    53
char *get (int sockfd, const char *path)
780bf4f348f3 modularized get demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 153
diff changeset
    54
{
780bf4f348f3 modularized get demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 153
diff changeset
    55
    char *result, *current;
780bf4f348f3 modularized get demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 153
diff changeset
    56
    char buffer[1024];
780bf4f348f3 modularized get demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 153
diff changeset
    57
    int len;
780bf4f348f3 modularized get demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 153
diff changeset
    58
    int i = 1;
780bf4f348f3 modularized get demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 153
diff changeset
    59
780bf4f348f3 modularized get demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 153
diff changeset
    60
    len = sprintf (buffer, "GET %s HTTP/1.0\r\n\r\n", path);
780bf4f348f3 modularized get demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 153
diff changeset
    61
780bf4f348f3 modularized get demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 153
diff changeset
    62
    if (write (sockfd, buffer, len) == -1) {
780bf4f348f3 modularized get demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 153
diff changeset
    63
        perror ("write");
780bf4f348f3 modularized get demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 153
diff changeset
    64
        return NULL;
780bf4f348f3 modularized get demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 153
diff changeset
    65
    }
780bf4f348f3 modularized get demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 153
diff changeset
    66
780bf4f348f3 modularized get demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 153
diff changeset
    67
    result = malloc (1024);
780bf4f348f3 modularized get demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 153
diff changeset
    68
    while ((len = read (sockfd, buffer, 1023)) > 0) {
780bf4f348f3 modularized get demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 153
diff changeset
    69
        buffer[len] = '\0';
780bf4f348f3 modularized get demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 153
diff changeset
    70
        strcat (result, buffer);
780bf4f348f3 modularized get demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 153
diff changeset
    71
        if ((current = realloc (result, 1024 * ++i)) == NULL) {
780bf4f348f3 modularized get demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 153
diff changeset
    72
            break;
780bf4f348f3 modularized get demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 153
diff changeset
    73
        }
780bf4f348f3 modularized get demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 153
diff changeset
    74
        result = current;
780bf4f348f3 modularized get demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 153
diff changeset
    75
    }
780bf4f348f3 modularized get demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 153
diff changeset
    76
780bf4f348f3 modularized get demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 153
diff changeset
    77
    return result;
780bf4f348f3 modularized get demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 153
diff changeset
    78
}
780bf4f348f3 modularized get demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 153
diff changeset
    79
10
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    80
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
    81
{
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    82
    int sockfd;
163
780bf4f348f3 modularized get demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 153
diff changeset
    83
    char *result;
10
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    84
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    85
    if (argc != 3) {
153
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    86
        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
    87
        return EXIT_FAILURE;
10
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    88
    }
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    89
163
780bf4f348f3 modularized get demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 153
diff changeset
    90
    if ((sockfd = tcpconnect (argv[1], argv[2])) < 0) {
153
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 131
diff changeset
    91
        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
    92
        return EXIT_FAILURE;
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    93
    }
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    94
163
780bf4f348f3 modularized get demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 153
diff changeset
    95
    result = get (sockfd, "/");
780bf4f348f3 modularized get demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 153
diff changeset
    96
    printf ("RESPONSE: %s", result);
10
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    97
163
780bf4f348f3 modularized get demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 153
diff changeset
    98
    if (result)
780bf4f348f3 modularized get demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 153
diff changeset
    99
        free (result);
10
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   100
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   101
    return close (sockfd);
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   102
}