3 * Copyright (C) 2004 Simone Piccardi
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.
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.
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.
19 /***************************************************************
22 * Routines for socket operations.
24 * Define routines for socket handling
30 ***************************************************************/
31 #include <sys/types.h>
32 #include <stdio.h> /* standard I/O functions */
36 #include <arpa/inet.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <netinet/in.h>
43 /**************************************************************************
47 * Return a string with the numeric address translation of the content
48 * of an addrinfo stucture
50 * Author: Simone Piccardi
55 **************************************************************************/
56 char *ip_ntop(struct addrinfo *addr, char *dst, socklen_t cnt)
58 // char buffer[INET6_ADDRSTRLEN];
60 struct sockaddr_in *ip4;
61 struct sockaddr_in6 *ip6;
62 if (addr->ai_family == PF_INET) {
63 ip4 = (struct sockaddr_in *) addr->ai_addr;
64 ret = inet_ntop(ip4->sin_family, &ip4->sin_addr, dst, cnt);
66 ip6 = (struct sockaddr_in6 *) addr->ai_addr;
67 ret = inet_ntop(ip6->sin6_family, &ip6->sin6_addr, dst, cnt);
71 /****************************************************************
74 * Return a connected socket given hostname, service, and socket type
76 * Author: Simone Piccardi
81 ****************************************************************/
82 int sockconn(char *host, char *serv, int prot, int type)
84 struct addrinfo hint, *addr, *save;
87 /* initialize hint structure */
88 memset(&hint, 0, sizeof(struct addrinfo));
89 hint.ai_family = PF_UNSPEC; /* generic address (IPv4 or IPv6) */
90 hint.ai_protocol = prot; /* protocol */
91 hint.ai_socktype = type; /* socket type */
92 res = getaddrinfo(host, serv, &hint, &addr); /* calling getaddrinfo */
93 if (res != 0) { /* on error exit */
94 fprintf(stderr, "sockconn: resolution failed:");
95 // fprintf(stderr, "host %s, service %s, protocol %d", host, serv, prot);
96 fprintf(stderr, " %s\n", gai_strerror(res));
97 errno = 0; /* clear errno */
100 /* loop on possible addresses */
102 while (addr != NULL) {
104 sock = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
106 if (addr->ai_next != NULL) {
110 perror("sockconn: cannot create socket");
114 /* connect the socket */
115 if ( (res = connect(sock, addr->ai_addr, addr->ai_addrlen) < 0)) {
116 if (addr->ai_next != NULL) {
121 perror("sockconn: cannot connect");
127 freeaddrinfo(save); /* done, release memory */
130 /****************************************************************
133 * Return a binded socket given hostname, service, and socket type
135 * Author: Simone Piccardi
140 ****************************************************************/
141 int sockbind(char *host, char *serv, int prot, int type)
143 struct addrinfo hint, *addr, *save;
146 char buf[INET6_ADDRSTRLEN];
147 /* initialize hint structure */
148 memset(&hint, 0, sizeof(struct addrinfo));
149 hint.ai_flags = AI_PASSIVE; /* address for binding */
150 hint.ai_family = PF_UNSPEC; /* generic address (IPv4 or IPv6) */
151 hint.ai_protocol = prot; /* protocol */
152 hint.ai_socktype = type; /* socket type */
153 res = getaddrinfo(host, serv, &hint, &addr); /* calling getaddrinfo */
154 if (res != 0) { /* on error exit */
155 fprintf(stderr, "sockbind: resolution failed:");
156 // fprintf(stderr, "host %s, service %s, protocol %d", host, serv, prot);
157 fprintf(stderr, " %s\n", gai_strerror(res));
158 errno = 0; /* clear errno */
161 /* loop on possible addresses */
163 while (addr != NULL) {
165 sock = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
167 if (addr->ai_next != NULL) {
171 perror("sockconn: cannot create socket");
175 /* connect the socket */
176 printf("Indirizzo %s\n", ip_ntop(addr, buf, sizeof(buf)));
178 if ( (res = bind(sock, addr->ai_addr, addr->ai_addrlen)) < 0) {
179 if (addr->ai_next != NULL) {
184 perror("sockconn: cannot connect");
190 freeaddrinfo(save); /* done, release memory */