connection.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Tue, 01 Jun 2010 13:13:34 +0200
changeset 128 efa226a4801e
parent 111 2247473fd12d
child 131 b5ad49852adc
permissions -rw-r--r--
That's the proper way to use crypt.h 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
10
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    19
int connection (char *ip, unsigned short port)
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    20
{
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    21
    struct hostent *hs;
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    22
    struct sockaddr_in sock;
27
81a574d60c15 typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    23
10
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    24
    int sockfd;
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    25
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    26
    memset (&sock, 0, sizeof (sock));
111
2247473fd12d bsd style socket handling...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 84
diff changeset
    27
    sock.sin_family = PF_INET;
10
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    28
    sock.sin_port = htons (port);
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    29
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    30
    if ((sock.sin_addr.s_addr = inet_addr (ip)) == -1) {
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    31
        if ((hs = gethostbyname (ip)) == NULL) {
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    32
            perror ("[-] Error");
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
        sock.sin_family = hs->h_addrtype;
84
f59d8a8e786f struct hostent has no longer a member h_addr
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    36
        memcpy (&sock.sin_addr.s_addr, hs->h_addr_list[0], hs->h_length);
10
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    37
    }
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    38
111
2247473fd12d bsd style socket handling...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 84
diff changeset
    39
    if ((sockfd = socket (PF_INET, SOCK_STREAM, 0)) < 0) {
10
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    40
        perror ("[-] Error");
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    41
        return -1;
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    42
    }
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    43
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    44
    if (connect (sockfd, (struct sockaddr *)&sock, sizeof (sock)) < 0) {
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    45
        perror ("[-] Error ");
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    46
        return -1;
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    47
    }
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    48
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    49
    return (sockfd);
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    50
}
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
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
    53
{
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    54
    char buffer[1024];
27
81a574d60c15 typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    55
10
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    56
    int sockfd;
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 num;
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    59
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    60
    if (argc != 3) {
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    61
        printf ("Usage: %s <ipaddr> <port>\n", argv[0]);
48
b94d657a9acb Policy Inonsistency on many files
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 29
diff changeset
    62
        return EXIT_FAILURE;
10
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    63
    }
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    64
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    65
    if ((sockfd = connection (argv[1], atoi (argv[2]))) < 0) {
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    66
        printf ("Connection error: %s:%d\n", argv[1], atoi (argv[2]));
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    67
        return EXIT_FAILURE;
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    68
    }
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    69
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    70
    write (sockfd, "GET /\r\n", 7);
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    71
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    72
    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
    73
        buffer[num] = 0;
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    74
        printf ("%s", buffer);
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    75
    }
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
    return close (sockfd);
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    78
}