Created
February 9, 2010 20:08
-
-
Save iamjwc/299606 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// [~]$ g++ isLocalhost.cpp -o isLocalhost && ./isLocalhost 127.0.0.1 google.com localhost | |
// isSlocalhost(127.0.0.1) = 1 | |
// isSlocalhost(google.com) = 0 | |
// isSlocalhost(localhost) = 1 | |
// isLocalhost(127.0.0.1) = 1 | |
// isLocalhost(google.com) = 0 | |
// isLocalhost(localhost) = 1 | |
#include <netdb.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <pthread.h> | |
#include <unistd.h> | |
bool isLocalhost(unsigned int a, unsigned int b, unsigned int c, unsigned int d) { | |
return a == 127 && b == 0 && c == 0 && d == 1; | |
} | |
bool isLocalhost(struct hostent* h) { | |
if(h) { | |
char* ip; | |
for(int i = 0; h->h_addr_list[i]; ++i) { | |
ip = h->h_addr_list[i]; | |
if(isLocalhost(ip[0], ip[1], ip[2], ip[3])) { | |
return 1; | |
} | |
} | |
} | |
return 0; | |
} | |
bool isLocalhost(char* hostname) { | |
struct hostent* h; | |
h = gethostbyname(hostname); | |
return isLocalhost(h); | |
} | |
struct params { | |
pthread_mutex_t mutex; | |
pthread_mutex_t gotValueMutex; | |
struct hostent* h; | |
pthread_t timeoutThread; | |
pthread_t getHostThread; | |
char* hostname; | |
}; | |
//void timeout(void* mutex, void* gotValueMutex, void* h, void* getHostThread) { | |
void* timeout(void* voidP) { | |
params* p = (params*)voidP; | |
sleep(1); | |
///At one point i thought this was needed, but im not smart enough | |
///to know for sure. | |
///pthread_mutex_lock(&(p->mutex)); | |
// Starts the execution back in main thread | |
pthread_mutex_unlock(&p->gotValueMutex); | |
pthread_cancel(p->getHostThread); | |
///pthread_mutex_unlock(&(p->mutex)); | |
pthread_exit(NULL); | |
} | |
//void getHostByName(void* mutex, void* &h, void* hostname) { | |
void* getHostByName(void* voidP) { | |
params* p = (params*)voidP; | |
//XXX: Uncomment this line to demonstrate unresponsive gethostbynam | |
//sleep(100); | |
p->h = gethostbyname(p->hostname); | |
// Starts the execution back in main thread | |
pthread_mutex_unlock(&p->gotValueMutex); | |
pthread_exit(NULL); | |
} | |
bool isSlocalhost(char* hostname) { | |
struct params p; | |
p.h = NULL; | |
pthread_mutex_init(&p.mutex, NULL); | |
pthread_mutex_init(&p.gotValueMutex, NULL); | |
p.hostname = hostname; | |
pthread_mutex_lock(&p.gotValueMutex); | |
///At one point i thought this was needed, but im not smart enough | |
///to know for sure. | |
///pthread_mutex_lock(&p.mutex); | |
pthread_create(&p.getHostThread, NULL, getHostByName, (void*)(&p)); | |
pthread_create(&p.timeoutThread, NULL, timeout, (void*)(&p)); | |
///pthread_mutex_unlock(&(p.mutex)); | |
// Execution will start here when one of the threads unlocks the mutex | |
pthread_mutex_lock(&p.gotValueMutex); | |
return isLocalhost(p.h); | |
} | |
int main(int argc, char** argv) { | |
if(argc == 1) { | |
printf("%s host[ host2]", argv[0]); | |
exit(-1); | |
} | |
for(int i = 1; i < argc; ++i) { | |
printf("isSlocalhost(%s) = ", argv[i]); | |
printf("%d\n", isSlocalhost(argv[i])); | |
} | |
for(int i = 1; i < argc; ++i) { | |
printf("isLocalhost(%s) = ", argv[i]); | |
printf("%d\n", isLocalhost(argv[i])); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment