From 5a12d423b85ac5decb966bc3c3a797b3ad9a7a3f Mon Sep 17 00:00:00 2001 From: Simone Piccardi Date: Wed, 8 Dec 2004 00:02:51 +0000 Subject: [PATCH] Correzione della stampa per la struttura IPv6 per mygetaddr, rimosse le vecchie versioni delle funzioni sockconn e sockbind, che ho inserito insieme alla nuova ip_ntop dentro SockUtil.c, eliminato SockAddr.c che era un preliminare abortito di SockUtil.c --- sources/Makefile | 8 +- sources/SockAddr.c | 75 ----------------- sources/SockUtil.c | 192 ++++++++++++++++++++++++++++++++++++++++++++ sources/mygetaddr.c | 4 +- sources/sockbind.c | 75 ----------------- sources/sockconn.c | 103 ------------------------ 6 files changed, 201 insertions(+), 256 deletions(-) delete mode 100644 sources/SockAddr.c create mode 100644 sources/SockUtil.c delete mode 100644 sources/sockbind.c delete mode 100644 sources/sockconn.c diff --git a/sources/Makefile b/sources/Makefile index 0a4fcf3..74f0b70 100644 --- a/sources/Makefile +++ b/sources/Makefile @@ -9,7 +9,7 @@ CFLAGJ= -L./ -lgapil LIB = libgapil.so OBJ = FullRead.o FullWrite.o SigHand.o Mutex.o SharedMem.o LockFile.o \ - DirScan.o endian.o sockconn.o sockbind.o + DirScan.o endian.o SockUtil.o FINAL = forktest errcode echo echod daytimed iterdaytimed daytime testfopen \ testren fortune fortuned mqfortune mqfortuned flock myls dirmonitor \ @@ -97,6 +97,12 @@ ipctestid: IPCTestId.c writeshm: WriteShm.c $(CC) $(CFLAGJ) $^ -o $@ +mygetaddr: mygetaddr.c + $(CC) $(CFLAGJ) $^ -o $@ + + + + endtest: endtest.c readshm: ReadShm.c diff --git a/sources/SockAddr.c b/sources/SockAddr.c deleted file mode 100644 index 9c10fab..0000000 --- a/sources/SockAddr.c +++ /dev/null @@ -1,75 +0,0 @@ -/* SockAddr.c - * - * Copyright (C) 2004 Simone Piccardi - * - * This program 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 2 of the License, or (at - * your option) any later version. - * - * This program 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 this program; if not, write to the Free Software - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - */ -/*************************************************************** - * - * File SockAddr.c - * Routines for socket address resolution. - * - * - * Author: S. Piccardi - * - * $Id$ - * - ***************************************************************/ -#include -#include -#include /* standard I/O functions */ -#include /* signal handling declarations */ -#include -#include -#include -#include /* socket function and declaration */ -#include /* resolver functions */ - - -#include "macros.h" -/* ************************************************************************* - * - * Functions for socket address automatic resolution - * - * ************************************************************************* */ -/* - * Function tcp_socket - * Create and attach a SysV shared memory segment to the current process. - * - * First call get a shared memory segment with KEY key access and size SIZE, - * by creating it with R/W privilege for the user (this is the meaning of - * the ored flags). The function return an identifier shmid used for any - * further reference to the shared memory segment. - * Second call attach the shared memory segment to this process and return a - * pointer to it (of void * type). - * Then initialize shared memory to the given value - * - * Input: an host name - * a service name - * Return: the IP address of the host in a sockaddr structure - */ -int tcp_addr(char * address, char * service, struct sockaddr * sa) -{ - struct addrinfo hints = { - ai_flag: AI_CANONNAME, - ai_family: PF_UNSPEC, - ai_socktype: SOCK_STREAM, - ai_protocol: 7 - } - - - /* OK, exit */ - return 0; -} diff --git a/sources/SockUtil.c b/sources/SockUtil.c new file mode 100644 index 0000000..c861144 --- /dev/null +++ b/sources/SockUtil.c @@ -0,0 +1,192 @@ +/* Sockutils.c + * + * Copyright (C) 2004 Simone Piccardi + * + * This program 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 2 of the License, or (at + * your option) any later version. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +/*************************************************************** + * + * File SockUtils.c + * Routines for socket operations. + * + * Define routines for socket handling + * + * Author: S. Piccardi + * + * $Id$ + * + ***************************************************************/ +#include +#include /* standard I/O functions */ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "macros.h" +/************************************************************************** + * + * Routine ip_ntop + * + * Return a string with the numeric address translation of the content + * of an addrinfo stucture + * + * Author: Simone Piccardi + * Dec. 2004 + * + * $Id$ + * + **************************************************************************/ +char *ip_ntop(struct addrinfo *addr, char *dst, socklen_t cnt) +{ +// char buffer[INET6_ADDRSTRLEN]; + char * ret; + struct sockaddr_in *ip4; + struct sockaddr_in6 *ip6; + if (addr->ai_family == PF_INET) { + ip4 = (struct sockaddr_in *) addr->ai_addr; + ret = inet_ntop(ip4->sin_family, &ip4->sin_addr, dst, cnt); + } else { + ip6 = (struct sockaddr_in6 *) addr->ai_addr; + ret = inet_ntop(ip6->sin6_family, &ip6->sin6_addr, dst, cnt); + } + return ret; +} +/**************************************************************** + * + * Routine sockconn + * Return a connected socket given hostname, service, and socket type + * + * Author: Simone Piccardi + * Dec. 2004 + * + * $Id$ + * + ****************************************************************/ +int sockconn(char *host, char *serv, int prot, int type) +{ + struct addrinfo hint, *addr, *save; + int res; + int sock; + /* initialize hint structure */ + memset(&hint, 0, sizeof(struct addrinfo)); + hint.ai_family = PF_UNSPEC; /* generic address (IPv4 or IPv6) */ + hint.ai_protocol = prot; /* protocol */ + hint.ai_socktype = type; /* socket type */ + res = getaddrinfo(host, serv, &hint, &addr); /* calling getaddrinfo */ + if (res != 0) { /* on error exit */ + fprintf(stderr, "sockconn: resolution failed:"); +// fprintf(stderr, "host %s, service %s, protocol %d", host, serv, prot); + fprintf(stderr, " %s\n", gai_strerror(res)); + errno = 0; /* clear errno */ + return -1; + } + /* loop on possible addresses */ + save = addr; + while (addr != NULL) { + /* get a socket */ + sock = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); + if (sock < 0) { + if (addr->ai_next != NULL) { + addr=addr->ai_next; + continue; + } else { + perror("sockconn: cannot create socket"); + return sock; + } + } + /* connect the socket */ + if ( (res = connect(sock, addr->ai_addr, addr->ai_addrlen) < 0)) { + if (addr->ai_next != NULL) { + addr=addr->ai_next; + close(sock); + continue; + } else { + perror("sockconn: cannot connect"); + close(sock); + return res; + } + } else break; + } + freeaddrinfo(save); /* done, release memory */ + return sock; +} +/**************************************************************** + * + * Routine sockbind + * Return a binded socket given hostname, service, and socket type + * + * Author: Simone Piccardi + * Dec. 2004 + * + * $Id$ + * + ****************************************************************/ +int sockbind(char *host, char *serv, int prot, int type) +{ + struct addrinfo hint, *addr, *save; + int res; + int sock; + char buf[INET6_ADDRSTRLEN]; + /* initialize hint structure */ + memset(&hint, 0, sizeof(struct addrinfo)); + hint.ai_flags = AI_PASSIVE; /* address for binding */ + hint.ai_family = PF_UNSPEC; /* generic address (IPv4 or IPv6) */ + hint.ai_protocol = prot; /* protocol */ + hint.ai_socktype = type; /* socket type */ + res = getaddrinfo(host, serv, &hint, &addr); /* calling getaddrinfo */ + if (res != 0) { /* on error exit */ + fprintf(stderr, "sockbind: resolution failed:"); +// fprintf(stderr, "host %s, service %s, protocol %d", host, serv, prot); + fprintf(stderr, " %s\n", gai_strerror(res)); + errno = 0; /* clear errno */ + return -1; + } + /* loop on possible addresses */ + save = addr; + while (addr != NULL) { + /* get a socket */ + sock = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); + if (sock < 0) { + if (addr->ai_next != NULL) { + addr=addr->ai_next; + continue; + } else { + perror("sockconn: cannot create socket"); + return sock; + } + } + /* connect the socket */ + printf("Indirizzo %s\n", ip_ntop(addr, buf, sizeof(buf))); + ; + if ( (res = bind(sock, addr->ai_addr, addr->ai_addrlen)) < 0) { + if (addr->ai_next != NULL) { + addr=addr->ai_next; + close(sock); + continue; + } else { + perror("sockconn: cannot connect"); + close(sock); + return res; + } + } else break; + } + freeaddrinfo(save); /* done, release memory */ + return sock; +} diff --git a/sources/mygetaddr.c b/sources/mygetaddr.c index ca01d74..b33f9c2 100644 --- a/sources/mygetaddr.c +++ b/sources/mygetaddr.c @@ -18,7 +18,7 @@ */ /***************************************************************************** * - * File mygetaddr.c: An example host command + * File mygetaddr.c: An example for getaddrinfo program * * Author: S. Piccardi Nov. 2004 * @@ -174,7 +174,7 @@ int main(int argc, char *argv[]) buffer, sizeof(buffer)); } else if (ptr->ai_family == PF_INET6) { /* if IPv6 */ printf("IPv6 address: \n"); - addr6 = (struct sockaddr_in *) ptr->ai_addr; /* address */ + addr6 = (struct sockaddr_in6 *) ptr->ai_addr; /* address */ port = ntohs(addr6->sin6_port); /* port */ string = inet_ntop(addr6->sin6_family, &addr6->sin6_addr, buffer, sizeof(buffer)); diff --git a/sources/sockbind.c b/sources/sockbind.c deleted file mode 100644 index 4ecbe97..0000000 --- a/sources/sockbind.c +++ /dev/null @@ -1,75 +0,0 @@ -/* sockbind.c - * - * Copyright (C) 2004 Simone Piccardi - * - * This program 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 2 of the License, or (at - * your option) any later version. - * - * This program 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 this program; if not, write to the Free Software - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - */ -/**************************************************************** - * - * Routine sockbind - * Return a binded socket given hostname, service, and socket type - * - * Author: Simone Piccardi - * Dec. 2004 - * - * $Id$ - * - ****************************************************************/ -#include -#include -#include -#include -#include -#include -#include - -int sockbind(char *host, char *serv, int prot, int type) -{ - struct addrinfo hint, *addr, *save; - int res; - int sock; - /* initialize hint structure */ - memset(&hint, 0, sizeof(struct addrinfo)); - hint.ai_flags = AI_PASSIVE; /* address for binding */ - hint.ai_family = PF_UNSPEC; /* generic address (IPv4 or IPv6) */ - hint.ai_protocol = prot; /* protocol */ - hint.ai_socktype = type; /* socket type */ - res = getaddrinfo(host, serv, &hint, &addr); /* calling getaddrinfo */ - if (res != 0) { /* on error exit */ - printf("sockbind cannot resolve host %s, service %s, ", host, serv); - printf("protocol %d: %s\n", prot, gai_strerror(res)); - errno = 0; /* clear errno */ - return -1; - } - /* loop on possible addresses */ - save = addr; - while (addr != NULL) { - /* get a socket */ - sock = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); - if ( (sock < 0) && (addr->ai_next == NULL) ) { - printf("sockbind cannot create socket\n"); - return sock; - } else continue; - /* connect the socket */ - res = bind(sock, addr->ai_addr, addr->ai_addrlen); - if ( (res < 0) && (addr->ai_next == NULL) ) { - printf("sockbind cannot bind socket\n"); - return res; - } - addr=addr->ai_next; - } - freeaddrinfo(save); /* done, release memory */ - return sock; -} diff --git a/sources/sockconn.c b/sources/sockconn.c deleted file mode 100644 index 543abe6..0000000 --- a/sources/sockconn.c +++ /dev/null @@ -1,103 +0,0 @@ -/* sockconn.c - * - * Copyright (C) 2004 Simone Piccardi - * - * This program 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 2 of the License, or (at - * your option) any later version. - * - * This program 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 this program; if not, write to the Free Software - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - */ -/**************************************************************** - * - * Routine sockconn - * Return a connected socket given hostname, service, and socket type - * - * Author: Simone Piccardi - * Dec. 2004 - * - * $Id$ - * - ****************************************************************/ -#include -#include -#include -#include -#include -#include -#include -#include -#include - -int sockconn(char *host, char *serv, int prot, int type) -{ - struct addrinfo hint, *addr, *save; - int res; - int sock; - int err; - char buffer[INET6_ADDRSTRLEN]; - struct sockaddr_in *ip4; - struct sockaddr_in6 *ip6; - /* initialize hint structure */ - memset(&hint, 0, sizeof(struct addrinfo)); - hint.ai_family = PF_UNSPEC; /* generic address (IPv4 or IPv6) */ - hint.ai_protocol = prot; /* protocol */ - hint.ai_socktype = type; /* socket type */ - res = getaddrinfo(host, serv, &hint, &addr); /* calling getaddrinfo */ - if (res != 0) { /* on error exit */ - printf("sockconn cannot resolve host %s, service %s, ", host, serv); - printf("protocol %d: %s\n", prot, gai_strerror(res)); - errno = 0; /* clear errno */ - return -1; - } - /* loop on possible addresses */ - save = addr; - while (addr != NULL) { - if (addr->ai_family == PF_INET) { - ip4 = (struct sockaddr_in *) addr->ai_addr; - printf("Indirizzo %s\n", inet_ntop(ip4->sin_family, &ip4->sin_addr, - buffer, sizeof(buffer))); - } else { - ip6 = (struct sockaddr_in6 *) addr->ai_addr; - printf("Indirizzo %s\n", inet_ntop(ip6->sin6_family, - &ip6->sin6_addr, - buffer, sizeof(buffer))); - } - /* get a socket */ - sock = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); - if (sock < 0) { - if (addr->ai_next != NULL) { - addr=addr->ai_next; - continue; - } else { - printf("sockconn cannot create socket\n"); - return sock; - } - } - /* connect the socket */ - res = connect(sock, addr->ai_addr, addr->ai_addrlen); - if (res < 0) { - if (addr->ai_next != NULL) { - close(sock); - addr=addr->ai_next; - continue; - } else { - err = errno; - close(sock); - errno = err; - printf("sockconn cannot connect socket\n"); - return res; - } - } else break; - } - freeaddrinfo(save); /* done, release memory */ - return sock; -} -- 2.30.2