/****************************************************************************
Program: $Id: trace.c,v 1.6 2006/11/19 19:28:53 rbeverly Exp $
Author: Rob Beverly <rbeverly@mit.edu>
Date: $Date: 2006/11/19 19:28:53 $
Description: Traceroute wrapper
****************************************************************************/
#include "spoof.h"
#ifdef _WIN32
#include <Windows.h>
#else
#include <signal.h>
#endif
#define TRWAIT 10 // wait up to 10 seconds for results
#define NUM_TR_EXEC 4
int alrm_brk = 0;
int findtr(char *traceroute) {
char tr[NUM_TR_EXEC][SMALLBUF] = {
"/usr/local/sbin/traceroute",
"/usr/sbin/traceroute",
"/sbin/traceroute",
"/usr/sbin/tracepath" };
FILE *fp;
int i;
#ifdef _WIN32
snprintf(traceroute, BUFSIZE, "tracert -d");
return(1);
#endif
for (i=0;i<NUM_TR_EXEC;i++) {
fp = fopen(tr[i], "r");
if (fp != NULL) {
fclose(fp);
snprintf(traceroute, BUFSIZE, "%s -n", tr[i]);
return(1);
}
}
/* fall through - couldn't find a traceroute on the system */
if (DEBUG) printf("** No traceroute found on system\n");
return(-1);
}
void catch() {
alrm_brk++;
}
int runtrace(char *tracecmd, char *dest, char *result) {
FILE *fp;
char buff[BUFSIZE];
char *c;
int read = 0;
int ret = 0;
#ifdef _WIN32
MSG msg;
SetTimer(NULL, 0, TRWAIT * 1000, NULL);
#else
signal(SIGALRM, (void *) catch);
alarm(TRWAIT);
#endif
snprintf(buff, BUFSIZE, "%s %s", tracecmd, dest);
snprintf(tracecmd, BUFSIZE, "%s", buff);
printf(">> Running Trace (please wait): %s\n", tracecmd);
fp = popen(tracecmd, "r");
c = result;
while (!feof(fp)) {
#ifdef _WIN32
PeekMessage(&msg, NULL, 0, 0, 0);
if (msg.message == WM_TIMER) {
alrm_brk++;
}
#endif
if (alrm_brk) return(ret);
fgets(buff, BUFSIZE, fp);
if (!feof(fp)) {
read += strlen(buff);
if (read < BIGBUF) {
sprintf(c, "%s", buff);
c += strlen(buff);
} else {
return(ret);
}
// printf("alrm_brk: %d %s", alrm_brk, buff);
}
}
ret = pclose(fp);
return(ret);
}
#if 0
int main() {
char tracecmd[BUFSIZE];
char tracebuf[BIGBUF];
findtr(tracecmd);
runtrace(tracecmd, "128.61.2.1", tracebuf);
printf("string: %s\n", tracebuf);
exit(0);
}
#endif
syntax highlighted by Code2HTML, v. 0.9.1