summaryrefslogtreecommitdiff
path: root/src/tcp.c
blob: 659a39ea84c53cedb24a416a0a2511aec1e5972b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <arpa/inet.h>
#include <errno.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "tcp.h"

int tcp_connect(const char* const host, const int port)
{
    char ip[INET6_ADDRSTRLEN];
    hostname_to_ip(host , ip);
    printf("%s resolved to %s\n" , host, ip);

    struct sockaddr_in sa;
    int sd = socket(AF_INET, SOCK_STREAM, 0);
    memset(&sa, '\0', sizeof(sa));
    sa.sin_family = AF_INET;
    sa.sin_port = htons(port);
    inet_pton(AF_INET, ip, &sa.sin_addr);

    int err = connect(sd, (struct sockaddr *) &sa, sizeof(sa));
    if (err < 0) {
        fprintf(stderr, "Couldn't connect %s:%d : %s\n", host, port, strerror(errno));
        return -1;
    }

    return sd;
}

void tcp_close(const int sd)
{
    shutdown(sd, SHUT_RDWR);
    close(sd);
}

int hostname_to_ip(const char* const hostname, char *ip)
{
    struct addrinfo hints, *servinfo; //, *p;
    struct sockaddr_in *h;
    int rv, ret = 0;

    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC; // use AF_INET6 to force IPv6
    hints.ai_socktype = SOCK_STREAM;

    if ( (rv = getaddrinfo( hostname , "http" , &hints , &servinfo)) != 0) 
    {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
        ret = 1;
    } else {
        //// loop through all the results and connect to the first we can
        //for(p = servinfo; p != NULL; p = p->ai_next)
        //{
        //    h = (struct sockaddr_in *) p->ai_addr;
        //    strcpy(ip , inet_ntoa( h->sin_addr ) );
        //}
        h = (struct sockaddr_in *) servinfo->ai_addr;
        strcpy(ip, inet_ntoa(h->sin_addr));
    }

    freeaddrinfo(servinfo); // all done with this structure
    return ret;
}