Allow IPv6 address entry in tools>ping - Loosens valid character check
[tomato/davidwu.git] / release / src / router / httpd / traceping.c
blob34005a65e107eff4e17c7fb543e49cdfd45095fa
1 /*
3 Tomato Firmware
4 Copyright (C) 2006-2009 Jonathan Zarate
6 */
8 #include "tomato.h"
10 #include <ctype.h>
12 static int check_addr(const char *addr, int max)
14 const char *p;
15 char c;
17 if ((addr == NULL) || (addr[0] == 0)) return 0;
18 p = addr;
19 while (*p) {
20 c = *p;
21 if ((!isalnum(c)) && (c != '.') && (c != '-') && (c != ':')) return 0; //Give IPv6 addresses a chance
22 ++p;
24 return((p - addr) <= max);
27 void wo_trace(char *url)
29 char cmd[256];
30 const char *addr;
32 addr = webcgi_get("addr");
33 if (!check_addr(addr, 64)) return;
35 killall("traceroute", SIGTERM);
37 web_puts("\ntracedata = '");
38 sprintf(cmd, "traceroute -I -m %u -w %u %s", atoi(webcgi_safeget("hops", "0")), atoi(webcgi_safeget("wait", "0")), addr);
39 web_pipecmd(cmd, WOF_JAVASCRIPT);
40 web_puts("';");
43 void wo_ping(char *url)
45 char cmd[256];
46 const char *addr;
48 addr = webcgi_get("addr");
49 if (!check_addr(addr, 64)) return;
51 killall("ping", SIGTERM);
53 web_puts("\npingdata = '");
54 sprintf(cmd, "ping -c %d -s %d %s", atoi(webcgi_safeget("count", "0")), atoi(webcgi_safeget("size", "0")), addr);
55 web_pipecmd(cmd, WOF_JAVASCRIPT);
56 web_puts("';");
61 #include <regex.h>
63 int main(int argc, char **argv)
65 FILE *f;
66 char s[1024];
67 int n;
68 char domain[512];
69 char ip[32];
70 char min[32];
71 regex_t re;
72 regmatch_t rm[10];
73 int i;
75 if ((f = popen("traceroute -I 192.168.0.1", "r")) == NULL) {
76 perror("popen");
77 return 1;
79 // 2 192.168.0.1 (192.168.0.1) 1.908 ms 1.812 ms 1.688 ms
81 while (fgets(s, sizeof(s), f)) {
84 if (regcomp(&re, "^ +[0-9]+ +(.+?) +\\((.+?)\\) +(.+?) ms +(.+?) ms +(.+?) ms", REG_EXTENDED) != 0) {
85 printf("error: regcomp\n");
86 return 1;
88 if ((regexec(&re, s, sizeof(rm) / sizeof(rm[0]), rm, 0) == 0) && (re.re_nsub == 5)) {
89 printf("[");
90 for (i = 1; i < 6; ++i) {
91 s[rm[i].rm_eo] = 0;
92 printf("'%s'%c", s + rm[i].rm_so, (i == 5) ? ' ' : ',');
94 printf("]\n");
95 // printf("%d = %d = [%s]\n", i, rm[i].rm_so, s + rm[i].rm_so);
97 regfree(&re);
99 // sscanf(s, "%d %s (%s) %s ms", &n, domain, ip, min);
100 // printf("[%s] %s %s\n", ip, domain, min);
102 pclose(f);
104 return 0;