Merge branch 'Static-ARP' into Toastman-RT
[tomato.git] / release / src / router / rc / new_arpbind.c
blobd86a0de8a5ef8fdbb9858ecf78b9f98dfce6d26f
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>
12 //#include <sys/stat.h>
14 // read nvram into files
15 void new_arpbind_start(void)
17 FILE *f;
18 char *p, *q, *e;
19 char *ipaddr;//ip address
20 char *macaddr;//mac address
21 char *s = "/tmp/new_arpbind_start.sh";
22 char *argv[3];
23 int pid;
24 int i;
25 char lan[24];
26 const char *router_ip;
27 int host[256];
28 char buf_arp[512];
29 char ipbuf[32];
30 int ipn, length;
32 //arpbind is enable
33 if (!nvram_get_int("new_arpbind_enable")) return;
35 //read static dhcp list from nvram
36 p = nvram_safe_get("dhcpd_static");
38 //read arpbind_list into file
39 if ((f = fopen(s, "w")) == NULL) return;
40 fprintf(f,
41 "#!/bin/sh\n"
42 "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"
44 memset(host, 0, sizeof(host));
46 //get network ip prefix
47 router_ip = nvram_safe_get("lan_ipaddr");
48 strlcpy(lan, router_ip, sizeof(lan));
49 if ((p = strrchr(lan, '.')) != NULL) {
50 host[atoi(p+1)] = 1;
51 *p = '\0';
54 // 00:aa:bb:cc:dd:ee<123<xxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 53 w/ delim
55 // 00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 85 w/ delim
56 // 00:aa:bb:cc:dd:ee,00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 106 w/ delim
57 p = nvram_safe_get("dhcpd_static");
58 while ((e = strchr(p, '>')) != NULL) {
59 length = (e - p);
60 if (length > 105) {
61 p = e + 1;
62 continue;
65 strncpy(buf_arp, p, length);
66 buf_arp[length] = 0;
67 p = e + 1;
69 /* get the MAC address */
70 if ((e = strchr(buf_arp, '<')) == NULL) continue;
71 *e = 0;
72 ipaddr = e + 1;
73 macaddr = buf_arp;
74 if ((e = strchr(macaddr, ',')) != NULL){
75 *e = 0;
77 //cprintf ("mac address %s\n", macaddr);
79 /* get the IP adddres */
80 if ((e = strchr(ipaddr, '<')) == NULL) continue;
81 *e = 0;
82 if (strchr(ipaddr, '.') == NULL) {
83 ipn = atoi(ipaddr);
84 if ((ipn <= 0) || (ipn > 255)) continue;
85 sprintf(ipbuf, "%s%d", lan, ipn);
86 ipaddr = ipbuf;
88 else {
89 if (inet_addr(ipaddr) == INADDR_NONE) continue;
91 //cprintf ("ip address %s\n", ipaddr);
93 /* add static arp */
94 if ((*macaddr != 0) && (strcmp(macaddr, "00:00:00:00:00:00") != 0)) {
95 fprintf(f, "arp -s %s %s\n", ipaddr, macaddr);
96 //cprintf ("arp -s %s %s\n", ipaddr, macaddr);
97 if ((q = strrchr(ipaddr, '.')) != NULL) {
98 *q = '\0';
99 if (!strcmp(ipaddr, lan)) host[atoi(q+1)] = 1;
104 if (nvram_get_int("new_arpbind_only")) {
105 for (i = 1; i < 255; i++) {
106 if (!host[i]) {
107 fprintf(f, "arp -s %s.%d 00:00:00:00:00:00\n", lan, i);
112 fclose(f);
113 chmod(s, 0700);
114 chdir("/tmp");
116 argv[0] = s;
117 argv[1] = NULL;
118 argv[2] = NULL;
119 if (_eval(argv, NULL, 0, &pid) != 0) {
120 pid = -1;
122 else {
123 kill(pid, 0);
126 chdir("/");
129 void new_arpbind_stop(void)
131 FILE *f;
132 char *s = "/tmp/new_arpbind_stop.sh";
133 char *argv[3];
134 int pid;
136 if (nvram_get_int("new_arpbind_enable")) return;
138 if ((f = fopen(s, "w")) == NULL) return;
140 fprintf(f,
141 "#!/bin/sh\n"
142 "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"
145 fclose(f);
146 chmod(s, 0700);
147 chdir("/tmp");
149 argv[0] = s;
150 argv[1] = NULL;
151 argv[2] = NULL;
152 if (_eval(argv, NULL, 0, &pid) != 0) {
153 pid = -1;
155 else {
156 kill(pid, 0);
159 chdir("/");