MiniDLNA update: 1.0.19.1 to 1.0.20
[tomato.git] / release / src / router / rc / new_arpbind.c
blobf3be223b4c3d3f62b1af9204b87e108adcad355d
1 /*
3 Tomato Firmware
4 Copyright (C) 2006-2008 Jonathan Zarate
5 rate limit & connection limit by conanxu
6 */
8 #include "rc.h"
9 #include <arpa/inet.h>
11 //#include <sys/stat.h>
13 // read nvram into files
14 void new_arpbind_start(void)
16 FILE *f;
17 char *p, *q, *e;
18 char *ipaddr;//ip address
19 char *macaddr;//mac address
20 char *s = "/tmp/new_arpbind_start.sh";
21 char *argv[3];
22 int pid;
23 int i;
24 char lan[24];
25 const char *router_ip;
26 int host[256];
27 char buf_arp[512];
28 char ipbuf[32];
29 int ipn, length;
31 //arpbind is enable
32 if (!nvram_get_int("new_arpbind_enable")) return;
34 //read static dhcp list from nvram
35 p = nvram_safe_get("dhcpd_static");
37 //read arpbind_list into file
38 if ((f = fopen(s, "w")) == NULL) return;
39 fprintf(f,
40 "#!/bin/sh\n"
41 "for HOST in `cat /proc/net/arp |sed -n 's/\\([0-9]*\\.[0-9]*\\.[0-9]*\\.[0-9]*\\).*/\\1/p'`; do arp -d $HOST; done\n"
43 memset(host, 0, sizeof(host));
45 //get network ip prefix
46 router_ip = nvram_safe_get("lan_ipaddr");
47 strlcpy(lan, router_ip, sizeof(lan));
48 if ((p = strrchr(lan, '.')) != NULL) {
49 host[atoi(p+1)] = 1;
50 *p = '\0';
53 // 00:aa:bb:cc:dd:ee<123<xxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 53 w/ delim
54 // 00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 85 w/ delim
55 // 00:aa:bb:cc:dd:ee,00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 106 w/ delim
56 p = nvram_safe_get("dhcpd_static");
57 while ((e = strchr(p, '>')) != NULL) {
58 length = (e - p);
59 if (length > 105) {
60 p = e + 1;
61 continue;
64 strncpy(buf_arp, p, length);
65 buf_arp[length] = 0;
66 p = e + 1;
68 /* get the MAC address */
69 if ((e = strchr(buf_arp, '<')) == NULL) continue;
70 *e = 0;
71 ipaddr = e + 1;
72 macaddr = buf_arp;
73 if ((e = strchr(macaddr, ',')) != NULL){
74 *e = 0;
76 //cprintf ("mac address %s\n", macaddr);
78 /* get the IP adddres */
79 if ((e = strchr(ipaddr, '<')) == NULL) continue;
80 *e = 0;
81 if (strchr(ipaddr, '.') == NULL) {
82 ipn = atoi(ipaddr);
83 if ((ipn <= 0) || (ipn > 255)) continue;
84 sprintf(ipbuf, "%s%d", lan, ipn);
85 ipaddr = ipbuf;
87 else {
88 if (inet_addr(ipaddr) == INADDR_NONE) continue;
90 //cprintf ("ip address %s\n", ipaddr);
92 /* add static arp */
93 if ((*macaddr != 0) && (strcmp(macaddr, "00:00:00:00:00:00") != 0)) {
94 fprintf(f, "arp -s %s %s\n", ipaddr, macaddr);
95 //cprintf ("arp -s %s %s\n", ipaddr, macaddr);
96 if ((q = strrchr(ipaddr, '.')) != NULL) {
97 *q = '\0';
98 if (!strcmp(ipaddr, lan)) host[atoi(q+1)] = 1;
103 if (nvram_get_int("new_arpbind_only")) {
104 for (i = 1; i < 255; i++) {
105 if (!host[i]) {
106 fprintf(f, "arp -s %s.%d 00:00:00:00:00:00\n", lan, i);
111 fclose(f);
112 chmod(s, 0700);
113 chdir("/tmp");
115 argv[0] = s;
116 argv[1] = NULL;
117 argv[2] = NULL;
118 if (_eval(argv, NULL, 0, &pid) != 0) {
119 pid = -1;
121 else {
122 kill(pid, 0);
125 chdir("/");
128 void new_arpbind_stop(void)
130 FILE *f;
131 char *s = "/tmp/new_arpbind_stop.sh";
132 char *argv[3];
133 int pid;
135 if (nvram_get_int("new_arpbind_enable")) return;
137 if ((f = fopen(s, "w")) == NULL) return;
139 fprintf(f,
140 "#!/bin/sh\n"
141 "for HOST in `cat /proc/net/arp |sed -n 's/\\([0-9]*\\.[0-9]*\\.[0-9]*\\.[0-9]*\\).*/\\1/p'`; do arp -d $HOST; done\n"
144 fclose(f);
145 chmod(s, 0700);
146 chdir("/tmp");
148 argv[0] = s;
149 argv[1] = NULL;
150 argv[2] = NULL;
151 if (_eval(argv, NULL, 0, &pid) != 0) {
152 pid = -1;
154 else {
155 kill(pid, 0);
158 chdir("/");