952910a362892377c9318a45030e5af87652efaa
[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/socket.h>
32 #include <netinet/in.h>
33 #include <netdb.h>
34 #include <string.h>
35 #include <stdio.h>
36 #include <errno.h>
37
38 int sockconn(char *host, char *serv, int prot, int type) 
39 {
40     struct addrinfo hint, *addr, *save;
41     int res;
42     int sock;
43     /* initialize hint structure */
44     memset(&hint, 0, sizeof(struct addrinfo)); 
45     hint.ai_family = PF_UNSPEC;          /* generic address (IPv4 or IPv6) */
46     hint.ai_protocol = prot;             /* protocol */
47     hint.ai_socktype = type;             /* socket type */
48     res = getaddrinfo(host, serv, &hint, &addr);   /* calling getaddrinfo */
49     if (res != 0) {                                /* on error exit */
50         printf("sockconn cannot resolve host %s, service %s, ", host, serv);
51         printf("protocol %d: %s\n", prot, gai_strerror(res));
52         errno = 0;                                 /* clear errno */
53         return -1;
54     }
55     /* loop on possible addresses */
56     save = addr;
57     while (addr != NULL) {
58         /* get a socket */
59         sock = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
60         if (sock < 0) {
61             if (addr->ai_next == NULL) {
62                 addr=addr->ai_next;
63                 continue;
64             } else {
65                 printf("sockconn cannot create socket\n");
66                 return sock;
67             }
68         }
69         /* connect the socket */
70         res = connect(sock, addr->ai_addr, addr->ai_addrlen);
71         if (res < 0) {
72             if (addr->ai_next == NULL) {
73                 close(sock);
74                 addr=addr->ai_next;
75                 continue;
76             } else {
77                 printf("sockconn cannot connect socket\n");
78                 return res;
79             }
80         }
81     }
82     freeaddrinfo(save);         /* done, release memory */
83     return sock;
84 }