From 9fe033ea88c2f705ec18c232873d056e0c229d72 Mon Sep 17 00:00:00 2001 From: Gaspard Coulet Date: Wed, 28 Apr 2021 23:05:53 +0200 Subject: Initial commit --- sem_6/HLIN611/tp1.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 sem_6/HLIN611/tp1.c (limited to 'sem_6/HLIN611/tp1.c') diff --git a/sem_6/HLIN611/tp1.c b/sem_6/HLIN611/tp1.c new file mode 100644 index 0000000..7ff5a2b --- /dev/null +++ b/sem_6/HLIN611/tp1.c @@ -0,0 +1,72 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +int main (int argc, char ** argv){ + if (argc != 2){ + printf("Syntaxe : %s nom\n",argv[0]); + return 1; + } + struct addrinfo hints; + struct addrinfo *result, *rp; + int s; + char addrstr[64]; + char ip[64]; + char canonname[256]; + memset(&canonname, 0, 256*sizeof(char)); + memset(&ip, 0, 64*sizeof(char)); + memset(&addrstr,0,64*sizeof(char) ); + hints.ai_family=AF_UNSPEC; + hints.ai_socktype=0; + hints.ai_flags = AI_CANONNAME; + struct addrinfo * p ; + s= getaddrinfo(argv[1],NULL,&hints,&result); + if (s !=0) { + printf("error in getaddrinfo : %s\n", gai_strerror(s)); + return 1; + } + strcpy(canonname, result->ai_canonname); + printf("%s\n",canonname); + int error; + char hostname[NI_MAXHOST] = ""; + char oldhostname[NI_MAXHOST]="uesh"; + for (p = result; p != NULL; p = p->ai_next){ + + error = getnameinfo(p->ai_addr, p->ai_addrlen, hostname, NI_MAXHOST, NULL, 0, 0); + if (error != 0) + { + printf("error in getnameinfo: %s\n", gai_strerror(error)); + return 1; + } + if (strcmp(hostname,"")!=0 && strcmp(hostname,oldhostname)!=0){ + printf("hostname : %s\n", hostname); + strcpy(oldhostname,hostname); + } + void * ptr; + switch(p->ai_family){ + case AF_INET: + ptr = &((struct sockaddr_in * ) p->ai_addr)->sin_addr; + break; + case AF_INET6: + ptr = &((struct sockaddr_in6 * ) p->ai_addr)->sin6_addr; + break; + } + inet_ntop (p->ai_addr->sa_family,ptr, addrstr, 64 ); + char type[100]; + switch (p->ai_socktype){ + case (SOCK_STREAM): + strcpy(type,"STREAM"); + break; + case (SOCK_DGRAM): + strcpy(type,"DGRAM"); + break; + } + printf("IP : %s type : %s\n",addrstr,type); + } + return 0; +} -- cgit v1.2.3