Created
October 18, 2011 01:58
-
-
Save afeinberg/1294434 to your computer and use it in GitHub Desktop.
This file contains 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
/* | |
* tlook.c - [email protected] based on mlook.c | |
* threaded mass resolver | |
* | |
* ./tlook <file> Accepts stdin if no arguments are given resolves both hostname | |
* and IPs | |
* | |
* grep new.cgi access.log | awk '{print $1}' | sort |uniq |tlook > resolved | |
* | |
*/ | |
/* | |
* TODO: rewrite using reentrant versions of appropriate functions, rather | |
* than with mutex locking (should improve performance) | |
*/ | |
#include <stdio.h> | |
#include <netdb.h> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
#include <arpa/inet.h> | |
#include <string.h> | |
#include <pthread.h> | |
#define MAXCHILD 200 | |
char buffers[MAXCHILD][1024]; | |
pthread_mutex_t mutex_IP; | |
pthread_mutex_t mutex_HOST; | |
void * | |
lookitup(void *tid) | |
{ | |
char ipstr[1024]; | |
struct in_addr ip; | |
struct hostent *he; | |
int tnid = *((int *)(tid)); | |
strncpy(ipstr, buffers[tnid], sizeof(ipstr) - 1); | |
if ((ip.s_addr = inet_addr(ipstr)) == -1) { | |
if ((he = gethostbyname(ipstr)) == NULL) { | |
printf("%s -> invalid\n", ipstr); | |
} else { | |
pthread_mutex_lock(&mutex_IP); | |
memcpy(&ip.s_addr, he->h_addr, he->h_length); | |
printf("%s -> %s\n", ipstr, inet_ntoa(ip)); | |
pthread_mutex_unlock(&mutex_IP); | |
} | |
} else { | |
pthread_mutex_lock(&mutex_HOST); | |
if ((he = gethostbyaddr((char *)&ip, sizeof(ip), AF_INET)) == NULL) { | |
printf("%s -> N/A\n", ipstr); | |
} else | |
printf("%s -> %s\n", ipstr, he->h_name); | |
pthread_mutex_unlock(&mutex_HOST); | |
} | |
pthread_exit((void *)0); | |
} | |
int | |
main(int argc, char *argv[]) | |
{ | |
int mchild = MAXCHILD, children = 0, i; | |
char buffer[1024]; | |
FILE *fh; | |
pthread_t threads[MAXCHILD]; | |
pthread_attr_t attr; | |
int rc, status; | |
int *taskids[MAXCHILD]; | |
pthread_mutex_init(&mutex_IP, NULL); | |
pthread_mutex_init(&mutex_HOST, NULL); | |
pthread_attr_init(&attr); | |
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); | |
if (argc < 2) { | |
fh = stdin; | |
} else if ((fh = fopen(argv[1], "r")) == NULL) { | |
printf("file not found: %s\n", argv[1]); | |
exit(-1); | |
} | |
while (fgets(buffer, sizeof(buffer), fh) != NULL) { | |
for (i = 0; i < strlen(buffer); i++) { | |
if (buffer[i] == '\n') { | |
memset(&buffer[i], 0, 1); | |
} | |
} | |
taskids[children] = (int *)malloc(sizeof(int)); | |
*taskids[children] = children; | |
strncpy(buffers[children], buffer, sizeof(buffers[children]) - 1); | |
rc = pthread_create(&threads[children], &attr, lookitup, (void *)taskids[children]); | |
children++; | |
if (children == MAXCHILD) { | |
for (children = 0; children < MAXCHILD; children++) { | |
pthread_join(threads[children], (void **)&status); | |
} | |
children = 0; | |
} | |
} | |
for (i = 0; i < children; i++) { | |
pthread_join(threads[i], (void **)&status); | |
} | |
pthread_mutex_destroy(&mutex_IP); | |
pthread_mutex_destroy(&mutex_HOST); | |
pthread_exit(NULL); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment