hi all.
Can someone explain why U get a segmentation fault in the following block of code
Code:
int setup(char *hostname, int ttl, SOCKET *icmpSock, SOCKADDR_IN *dest)
{
HOSTENT *hp;
unsigned int addr;
int sockRet;
// create socket
*icmpSock = WSASocket(AF_INET, SOCK_RAW, IPPROTO_ICMP, NULL, 0, 0);
if (*icmpSock == INVALID_SOCKET) {
printf("Could not create socket : %d.\n", WSAGetLastError());
return -1;
}
// setup for TTL
sockRet = setsockopt(*icmpSock, IPPROTO_IP, IP_TTL, (const char *)&ttl, sizeof(ttl));
if (sockRet == SOCKET_ERROR) {
printf("ttl setsockopt failed : %d. \n", WSAGetLastError());
return -1;
}
memset(&dest, 0, sizeof(*dest));
addr = inet_addr(hostname);
if (addr == INADDR_NONE) {
hp = gethostbyname(hostname);
if (hp != 0) {
//memcpy(&dest->sin_addr, hp->h_addr, hp->h_length);
dest->sin_addr = *((struct in_addr *)hp->h_addr);
dest->sin_family = hp->h_addrtype;
} else {
printf("unknown hostname || failed to resolve\n");
return -1;
}
} else {
dest->sin_addr.s_addr = addr;
dest->sin_family = AF_INET;
}
return 0;
}
The error is coming from the line dest->sin_addr = *((struct in_addr *)hp->h_addr);
I get the same error if I try to use memcpy as above it.
dest is a pointer to an empty structure.
Thanks.