i have this program :
Code:
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
int main(void)
{
int fd, n, addrlen;
struct sockaddr_in addr;
struct hostent *h;
struct in_addr *a;
char buffer[128];
fd=socket(AF_INET,SOCK_DGRAM,0);
if(fd == -1) exit(1);
if((h=gethostbyname("tejo.ist.utl.pt"))==NULL)exit(1);
a=(struct in_addr*)h->h_addr_list[0];
printf("(struct in_addr*)h->h_addr_list[0] = %d \n", a->s_addr);
memset((void*)&addr,(int)'\0',sizeof(addr));
addr.sin_family=AF_INET;
addr.sin_addr= *a;
addr.sin_port= htons(8001);
n=sendto(fd,"lll!\n",7,0,(struct sockaddr*)&addr,sizeof(addr));
if(n==-1) exit(1);
addrlen=sizeof(addr);
n=recvfrom(fd,buffer,128,0,(struct sockaddr*)&addr,&addrlen);
if(n==-1)exit(1);
write(1,"echo: ",6);
write(1,buffer,n);
h=gethostbyaddr(&addr.sin_addr,sizeof(addr.sin_addr),AF_INET);
if(h==NULL)
printf("sent by 1 [%s:%hu]\n",inet_ntoa(addr.sin_addr),ntohs(addr.sin_port));
else printf("2 sent by 2 [%s:%hu]\n",h->h_name,ntohs(addr.sin_port));
close(fd);
exit(0);
}
i want to get rid of the functions gethostbyname() and gethostbyaddr()
so i have tried the following:
Code:
#define _POSIX_C_SOURCE 1
#define _XOPEN_SOURCE
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
int main()
{
char *hostname = "tejo.ist.utl.pt";
struct addrinfo hints, *res;
struct in_addr addr;
int err;
char *gg;
int fd, n, addrlen;
struct sockaddr_in addr2;
struct hostent *h;
struct in_addr *a;
char buffer[128];
fd=socket(AF_INET,SOCK_DGRAM,0);
if(fd == -1) exit(1);
memset(&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_STREAM;
hints.ai_family = AF_INET;
hints.ai_flags=AI_CANONNAME;
if ((err = getaddrinfo(hostname, NULL, &hints, &res)) != 0)
{
printf("error %d\n", err);
return 1;
}
addr.s_addr = ((struct sockaddr_in *)(res->ai_addr))->sin_addr.s_addr;
printf("official host name: %s\n",res->ai_canonname);
printf("ip address : %s (%08lX)\n", inet_ntoa(addr), (long unsigned int)ntohl(addr.s_addr));
freeaddrinfo(res);
return 0;
}
but i dont know how to get arround at first with the gethostbyname() and after that i also dont know how to get arround the gethostbyaddr().
so can smb help me? i thank you in advance for the help