Versione di debug funzionante, la salvo prima di rimuovere tutto per mettere
[gapil.git] / sources / sockconn.c
1 /* sockconn.c
2  * 
3  * Copyright (C) 2004 Simone Piccardi
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or (at
8  * your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 /****************************************************************
20  *
21  * Routine sockconn
22  * Return a connected socket given hostname, service, and socket type
23  *
24  * Author: Simone Piccardi
25  * Dec. 2004
26  *
27  * $Id$ 
28  *
29  ****************************************************************/
30 #include <arpa/inet.h>
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <netdb.h>
35 #include <string.h>
36 #include <stdio.h>
37 #include <errno.h>
38 #include <unistd.h>
39
40 int sockconn(char *host, char *serv, int prot, int type) 
41 {
42     struct addrinfo hint, *addr, *save;
43     int res;
44     int sock;
45     int err;
46     char buffer[INET6_ADDRSTRLEN];
47     struct sockaddr_in *ip4;
48     struct sockaddr_in6 *ip6;
49     /* initialize hint structure */
50     memset(&hint, 0, sizeof(struct addrinfo)); 
51     hint.ai_family = PF_UNSPEC;          /* generic address (IPv4 or IPv6) */
52     hint.ai_protocol = prot;             /* protocol */
53     hint.ai_socktype = type;             /* socket type */
54     res = getaddrinfo(host, serv, &hint, &addr);   /* calling getaddrinfo */
55     if (res != 0) {                                /* on error exit */
56         printf("sockconn cannot resolve host %s, service %s, ", host, serv);
57         printf("protocol %d: %s\n", prot, gai_strerror(res));
58         errno = 0;                                 /* clear errno */
59         return -1;
60     }
61     /* loop on possible addresses */
62     save = addr;
63     while (addr != NULL) {
64         if (addr->ai_family == PF_INET) {
65             ip4 = (struct sockaddr_in *) addr->ai_addr;
66             printf("Indirizzo %s\n", inet_ntop(ip4->sin_family, &ip4->sin_addr,
67                                                buffer, sizeof(buffer)));
68         } else {
69             ip6 = (struct sockaddr_in6 *) addr->ai_addr;
70             printf("Indirizzo %s\n", inet_ntop(ip6->sin6_family, 
71                                                &ip6->sin6_addr,
72                                                buffer, sizeof(buffer)));
73         }
74         /* get a socket */
75         sock = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
76         if (sock < 0) {
77             if (addr->ai_next != NULL) {
78                 addr=addr->ai_next;
79                 continue;
80             } else {
81                 printf("sockconn cannot create socket\n");
82                 return sock;
83             }
84         }
85         /* connect the socket */
86         res = connect(sock, addr->ai_addr, addr->ai_addrlen);
87         if (res < 0) {
88             if (addr->ai_next != NULL) {
89                 close(sock);
90                 addr=addr->ai_next;
91                 continue;
92             } else {
93                 err = errno;
94                 close(sock);
95                 errno = err;
96                 printf("sockconn cannot connect socket\n");
97                 return res;
98             }
99         } else break;
100     }
101     freeaddrinfo(save);         /* done, release memory */
102     return sock;
103 }