From 20d2dafe03f5ab376e6f5a5e8168d7016ccb55aa Mon Sep 17 00:00:00 2001 From: Simone Piccardi Date: Sat, 30 Sep 2006 15:05:43 +0000 Subject: [PATCH] =?utf8?q?Aggiunto=20esempio=20di=20uso=20delle=20{{{ioctl?= =?utf8?q?}}}=20per=20la=20lettura=20delle=20propriet=C3=A0=20delle=20inte?= =?utf8?q?rfacce=20di=20rete,=20e=20messo=20dentro=20la=20libreria=20la=20?= =?utf8?q?funzione=20{{{is=5Fclosing}}}.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- sources/Gapil.h | 1 + sources/Makefile | 7 ++- sources/iflist.c | 126 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 sources/iflist.c diff --git a/sources/Gapil.h b/sources/Gapil.h index 68fbc24..b83fefd 100644 --- a/sources/Gapil.h +++ b/sources/Gapil.h @@ -138,3 +138,4 @@ int sockbindopt(char *host, char *serv, int prot, int type, int reuse); * General purpose functions. See corresponding .c */ int endian(void); +int is_closing(int sock); diff --git a/sources/Makefile b/sources/Makefile index 56cbff4..b9345b3 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 SockUtil.o full_fread.o full_fwrite.o + DirScan.o endian.o SockUtil.o full_fread.o full_fwrite.o is_closing.o FINAL = forktest errcode echo echod daytimed iterdaytimed daytime testfopen \ testren fortune fortuned mqfortune mqfortuned flock myls dirmonitor \ @@ -108,7 +108,6 @@ endtest: endtest.c readshm: ReadShm.c $(CC) $(CFLAGJ) $^ -o $@ - wwwd: wwwd.c $(CC) $(CFLAGJ) $(CFLAGS) $^ -o $@ @@ -118,6 +117,10 @@ acctctrl: AcctCtrl.c getcap: getcap.c $(CC) $(CFLAGS) $^ -lcap -o $@ +iflist: iflist.c + $(CC) $(CFLAGS) $^ -o $@ + + # Macro per la generazione della tarball dei sorgenti package: clean gapil_source.tgz diff --git a/sources/iflist.c b/sources/iflist.c new file mode 100644 index 0000000..6020b52 --- /dev/null +++ b/sources/iflist.c @@ -0,0 +1,126 @@ +/* iflist.c + * + * Copyright (C) 2006 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 ifist.c: to get a list of network interface and their addresses + * + * Author: S. Piccardi Sep. 2006 + * + *****************************************************************************/ +#include +#include +#include /* C standard library */ +#include /* I/O standard library */ +#include +#include +#include +#include +#include +#include + +/* + * Program iflist + * + * Use ioctl to get a list of network interfaces and address + */ + +/* Help printing routine */ +void usage(void); + +int main(int argc, char *argv[]) +{ +/* + * Variables definition + */ + int i, num, ret, sock; + struct ifconf iflist; + char buffer[4096]; + struct sockaddr_in * address; + /* + * Input section: decode command line parameters + * Use getopt function + */ + opterr = 0; /* don't want writing to stderr */ + while ( (i = getopt(argc, argv, "h")) != -1) { + switch (i) { + /* + * Handling options + */ + case 'h': /* help option */ + printf("Wrong -h option use\n"); + usage(); + return -1; + break; + case '?': /* unrecognized options */ + printf("Unrecognized options -%c\n",optopt); + usage(); + default: /* should not reached */ + usage(); + } + } + /* *********************************************************** + * + * Options processing completed + * + * Main code beginning + * + * ***********************************************************/ + if ((argc - optind) != 0) { + printf("Wrong number of arguments %d\n", argc - optind); + usage(); + } + iflist.ifc_len = sizeof(buffer); + iflist.ifc_buf = buffer; + + sock = socket(PF_INET, SOCK_STREAM, 0); + if (sock < 0) { + perror("Socket creation error"); + return 1; + } + ret = ioctl(sock, SIOCGIFCONF, &iflist); + if (ret < 0) { + perror("ioctl failed"); + return 1; + } + if (iflist.ifc_len == sizeof(buffer)) { + printf("Probable overflow, too many interfaces, cannot read\n"); + return 1; + } else { + num = iflist.ifc_len/sizeof(struct ifreq); + printf("Found %i interfaces \n", num); + } + for (i=0; i < num; i++) { + address = (struct sockaddr_in *) &iflist.ifc_req[i].ifr_addr; + printf("Interface %s, address %s\n", iflist.ifc_req[i].ifr_name, + inet_ntoa(address->sin_addr)); + } + + return 0; +} + +/* + * routine to print usage info and exit + */ +void usage(void) { + printf("Program iflist: list active network interface with address \n"); + printf("Usage:\n"); + printf(" iflist [-h] \n"); + printf(" -h print this help\n"); + exit(1); +} -- 2.30.2