summaryrefslogtreecommitdiff
path: root/src/common/net.c
blob: 79a5883bf684e308960422010be6c07bfa95b779 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102

/* OpenIDA - Outil d'analyse de fichiers binaires
 * net.c - fonctions complémentaires liées au réseau
 *
 * Copyright (C) 2010 Cyrille Bagard
 *
 *  This file is part of OpenIDA.
 *
 *  OpenIDA is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  OpenIDA is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "net.h"


#include <stdio.h>
#include <string.h>
#include <unistd.h>



/******************************************************************************
*                                                                             *
*  Paramètres  : server = nom ou adresse du serveur à contacter.              *
*                port   = port de connexion.                                  *
*                addr   = éventuelle remontée d'informations. [OUT]           *
*                                                                             *
*  Description : Ouvre une connexion TCP à un serveur quelconque.             *
*                                                                             *
*  Retour      : Flux ouvert en lecture/écriture ou -1 en cas d'échec.        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

int connect_via_tcp(const char *server, const char *port, struct sockaddr_in *addr)
{
    int result;                             /* Bilan à retourner           */
    struct addrinfo hints;                  /* Type de connexion souhaitée */
    struct addrinfo *infos;                 /* Informations disponibles    */
    int ret;                                /* Bilan d'un appel            */
    struct addrinfo *iter;                  /* Boucle de parcours          */

    memset(&hints, 0, sizeof(struct addrinfo));

    hints.ai_family = AF_UNSPEC;        /* IPv4 ou IPv6 */
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = 0;
    hints.ai_protocol = 0;              /* N'importe quel protocole */

    ret = getaddrinfo(server, port, &hints, &infos);
    if (ret != 0)
    {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret));
        return -1;
    }

    for (iter = infos; iter != NULL; iter = iter->ai_next)
    {
        result = socket(iter->ai_family, iter->ai_socktype, iter->ai_protocol);
        if (result == -1) continue;

        ret = connect(result, iter->ai_addr, iter->ai_addrlen);
        if (ret == 0) break;

        perror("connect");
        close(result);

    }

    freeaddrinfo(infos);

    if (iter == NULL) return -1;

    if (addr != NULL)
    {
        ret = getpeername(result, (struct sockaddr *)addr,
                          (socklen_t []){ sizeof(struct sockaddr_in) });

        if (ret == -1)
        {
            perror("getpeername");
            close(result);
            return -1;
        }

    }

    return result;

}