Add gui for Comcast DSCP fix.
[tomato.git] / release / src / router / httpd / traceping.c
blob2fc0c544bf1d4e32f6036f19758110984e349b05
1 /*
3 Tomato Firmware
4 Copyright (C) 2006-2009 Jonathan Zarate
6 */
8 #include "tomato.h"
10 #include <ctype.h>
13 static int check_addr(const char *addr, int max)
15 const char *p;
16 char c;
18 if ((addr == NULL) || (addr[0] == 0)) return 0;
19 p = addr;
20 while (*p) {
21 c = *p;
22 if ((!isalnum(c)) && (c != '.') && (c != '-') && (c != ':')) return 0; //Give IPv6 address a chance
23 ++p;
25 return((p - addr) <= max);
28 void wo_trace(char *url)
30 char cmd[256];
31 const char *addr;
33 addr = webcgi_get("addr");
34 if (!check_addr(addr, 64)) return;
36 killall("traceroute", SIGTERM);
38 web_puts("\ntracedata = '");
39 sprintf(cmd, "traceroute -I -m %u -w %u %s", atoi(webcgi_safeget("hops", "0")), atoi(webcgi_safeget("wait", "0")), addr);
40 web_pipecmd(cmd, WOF_JAVASCRIPT);
41 web_puts("';");
44 void wo_ping(char *url)
46 char cmd[256];
47 const char *addr;
49 addr = webcgi_get("addr");
50 if (!check_addr(addr, 64)) return;
52 killall("ping", SIGTERM);
54 web_puts("\npingdata = '");
55 sprintf(cmd, "ping -c %d -s %d %s", atoi(webcgi_safeget("count", "0")), atoi(webcgi_safeget("size", "0")), addr);
56 web_pipecmd(cmd, WOF_JAVASCRIPT);
57 web_puts("';");
62 #include <regex.h>
64 int main(int argc, char **argv)
66 FILE *f;
67 char s[1024];
68 int n;
69 char domain[512];
70 char ip[32];
71 char min[32];
72 regex_t re;
73 regmatch_t rm[10];
74 int i;
76 if ((f = popen("traceroute -I 192.168.0.1", "r")) == NULL) {
77 perror("popen");
78 return 1;
80 // 2 192.168.0.1 (192.168.0.1) 1.908 ms 1.812 ms 1.688 ms
82 while (fgets(s, sizeof(s), f)) {
85 if (regcomp(&re, "^ +[0-9]+ +(.+?) +\\((.+?)\\) +(.+?) ms +(.+?) ms +(.+?) ms", REG_EXTENDED) != 0) {
86 printf("error: regcomp\n");
87 return 1;
89 if ((regexec(&re, s, sizeof(rm) / sizeof(rm[0]), rm, 0) == 0) && (re.re_nsub == 5)) {
90 printf("[");
91 for (i = 1; i < 6; ++i) {
92 s[rm[i].rm_eo] = 0;
93 printf("'%s'%c", s + rm[i].rm_so, (i == 5) ? ' ' : ',');
95 printf("]\n");
96 // printf("%d = %d = [%s]\n", i, rm[i].rm_so, s + rm[i].rm_so);
98 regfree(&re);
100 // sscanf(s, "%d %s (%s) %s ms", &n, domain, ip, min);
101 // printf("[%s] %s %s\n", ip, domain, min);
103 pclose(f);
105 return 0;