cosmetics
[tomato.git] / release / src / router / rc / cmon.c
blobdd5e4538e1c529af725beb41ca96588de179580b
1 /*
3 Tomato Firmware
4 Copyright (C) 2006-2008 Jonathan Zarate
5 rate limit & connection limit by conanxu
8 TOASTMAN - client monitor
10 Files used with the clientmon function:
12 httpd/bwm.c // write to the correct in/out stats display
13 httpd/tomato.c
14 nvram/defaults.c
15 rc/Makefile
16 rc/cmon.c (this file)
17 rc/rc.h
18 rc/services.c
19 rc/wan.c
20 www/about.asp
21 bwm-common.js // gives names to the imq interfaces
22 www/bwm-realtime.asp
30 #include "rc.h"
31 #include <arpa/inet.h>
33 //#include <sys/stat.h>
35 static char old_ip_address[16]="none";
37 void start_cmon(void)
39 FILE *f;
40 char *p;
41 char *s = "/tmp/start_cmon.sh";
42 char *argv[3];
43 int pid;
47 // Is cmon enabled?
48 // This nvram variable should be either 0 (off) or 1 (on)
50 if (!nvram_get_int("cmon_enable")) return;
52 //read ip address to monitor from nvram
53 p = nvram_safe_get("cmon_ipaddr");
55 if ((f = fopen(s, "w")) == NULL) return;
57 fprintf(f,
58 "#!/bin/sh\n"
62 if (strcmp(old_ip_address, "none"))
64 fprintf(f,
65 "iptables -t mangle -D PREROUTING -s %s -j IMQ --todev 3\n"
66 "iptables -t mangle -D POSTROUTING -d %s -j IMQ --todev 4\n",
67 old_ip_address, old_ip_address
71 fprintf(f,
72 "ip link set imq3 up txqueuelen 100\n"
73 "ip link set imq4 up txqueuelen 100\n"
74 "iptables -t mangle -A PREROUTING -s %s -j IMQ --todev 3\n"
75 "iptables -t mangle -A POSTROUTING -d %s -j IMQ --todev 4\n"
76 "\n", p, p
79 fclose(f);
80 chmod(s, 0700);
81 chdir("/tmp");
83 argv[0] = s;
84 argv[1] = NULL;
85 argv[2] = NULL;
86 if (_eval(argv, NULL, 0, &pid) != 0) {
87 pid = -1;
89 else {
90 kill(pid, 0);
91 strcpy(old_ip_address, p);
94 chdir("/");
100 This is working! All above is OK - Test the stop now...
106 void stop_cmon(void)
108 FILE *f;
109 char *s = "/tmp/stop_cmon.sh";
110 char *argv[3];
111 int pid;
113 if (nvram_get_int("cmon_enable")) return;
115 if ((f = fopen(s, "w")) == NULL) return;
117 fprintf(f,
118 "#!/bin/sh\n"
120 if (strcmp(old_ip_address, "none"))
122 fprintf(f,
123 "iptables -t mangle -D PREROUTING -s %s -j IMQ --todev 3\n"
124 "iptables -t mangle -D POSTROUTING -d %s -j IMQ --todev 4\n",
125 old_ip_address, old_ip_address);
127 fprintf(f,
128 "ip link set imq3 down\n"
129 "ip link set imq4 down\n"
130 "\n"
133 fclose(f);
134 chmod(s, 0700);
135 chdir("/tmp");
137 argv[0] = s;
138 argv[1] = NULL;
139 argv[2] = NULL;
140 if (_eval(argv, NULL, 0, &pid) != 0) {
141 pid = -1;
143 else {
144 kill(pid, 0);
145 strcpy(old_ip_address, "none");
148 chdir("/");