summaryrefslogtreecommitdiff
path: root/release/src/router/httpd/devlist.c
blob: b72fd58b3abceb117b54ad012b713ff01e788425 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*

	Tomato Firmware
	Copyright (C) 2006-2009 Jonathan Zarate

*/

#include "tomato.h"

#include <ctype.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <sys/stat.h>
#include <sys/types.h>

#include <wlutils.h>


void asp_arplist(int argc, char **argv)
{
	FILE *f;
	char s[512];
	char ip[16];
	char mac[18];
	char dev[17];
	char comma;
	unsigned int flags;

	/*
		cat /proc/net/arp
		IP address       HW type     Flags       HW address            Mask     Device
		192.168.0.1      0x1         0x2         00:01:02:03:04:05     *        vlan1
	*/

	web_puts("\narplist = [");
	comma = ' ';
	if ((f = fopen("/proc/net/arp", "r")) != NULL) {
		while (fgets(s, sizeof(s), f)) {
			if (sscanf(s, "%15s %*s 0x%X %17s %*s %16s", ip, &flags, mac, dev) != 4) continue;
			if ((strlen(mac) != 17) || (strcmp(mac, "00:00:00:00:00:00") == 0)) continue;
			if (flags == 0) continue;
//			if ((nvram_match("wan_ifname", dev)) && (!nvram_match("wan_ipaddr", ip))) continue; // half
			web_printf("%c['%s','%s','%s']", comma, ip, mac, dev);
			comma = ',';
		}
		fclose(f);
	}
	web_puts("];\n");
}

// checkme: any easier way to do this?	zzz
static int get_wds_ifname(const struct ether_addr *ea, char *ifname)
{
	struct ifreq ifr;
	int sd;
	int i;
	struct ether_addr e;

	if ((sd = socket(PF_INET, SOCK_DGRAM, 0)) >= 0) {
		// wds doesn't show up under SIOCGIFCONF; seems to start at 17 (?)
		for (i = 1; i < 32; ++i) {
			ifr.ifr_ifindex = i;
			if ((ioctl(sd, SIOCGIFNAME, &ifr) == 0) &&
				(strncmp(ifr.ifr_name, "wds", 3) == 0) &&
				(wl_ioctl(ifr.ifr_name, WLC_WDS_GET_REMOTE_HWADDR, &e.octet, sizeof(e.octet)) == 0)) {
				if (memcmp(ea->octet, e.octet, sizeof(e.octet)) == 0) {
					close(sd);
					strlcpy(ifname, ifr.ifr_name, 16);
					return 1;
				}
			}
		}
		close(sd);
	}
	return 0;
}

void asp_devlist(int argc, char **argv)
{
	char *p;
	FILE *f;
	char buf[1024];
	int i;
	char comma;

	// must be here for easier call via update.cgi. arg is ignored
	asp_arplist(0, NULL);
	asp_wlnoise(0, NULL);

	//

	p = js_string(nvram_safe_get("dhcpd_static"));
	web_printf("dhcpd_static = '%s'.split('>');\n", p ? p : "");
	free(p);

	//

#if 1
	char *wlif;
	scb_val_t rssi;
	sta_info_t sti;
	int cmd;
	struct maclist *mlist;
	int mlsize;
	char ifname[16];

	web_puts("wldev = [");
	comma = ' ';
	mlsize = sizeof(struct maclist) + (255 * sizeof(struct ether_addr));
	if ((mlist = malloc(mlsize)) != NULL) {
		wlif = nvram_safe_get("wl0_ifname");
		cmd = WLC_GET_ASSOCLIST;
		while (1) {
			mlist->count = 255;
			if (wl_ioctl(wlif, cmd, mlist, mlsize) == 0) {
				for (i = 0; i < mlist->count; ++i) {
					rssi.ea = mlist->ea[i];
					rssi.val = 0;
					if (wl_ioctl(wlif, WLC_GET_RSSI, &rssi, sizeof(rssi)) != 0) continue;

					// sta_info0<mac>
					memset(&sti, 0, sizeof(sti));
					strcpy((char *)&sti, "sta_info");
					memcpy((char *)&sti + 9, rssi.ea.octet, 6);
					if (wl_ioctl(wlif, WLC_GET_VAR, &sti, sizeof(sti)) != 0) continue;

					p = wlif;
					if (sti.flags & WL_STA_WDS) {
						if (cmd != WLC_GET_WDSLIST) continue;
						if ((sti.flags & WL_WDS_LINKUP) == 0) continue;
						if (get_wds_ifname(&rssi.ea, ifname)) p = ifname;
					}

					web_printf("%c['%s','%s',%d]",
						comma,
						p,
						ether_etoa(rssi.ea.octet, buf),
						rssi.val);
					comma = ',';
				}
			}
			if (cmd == WLC_GET_WDSLIST) break;
			cmd = WLC_GET_WDSLIST;
		}
		free(mlist);
	}
#else
	char *wlif;
	scb_val_t rssi;
	sta_info_t sti;
	int j;
	struct maclist *mlist;
	int mlsize;
	char ifname[16];

	web_puts("wldev = [");
	comma = ' ';
	mlsize = sizeof(struct maclist) + (127 * sizeof(struct ether_addr));
	if ((mlist = malloc(mlsize)) != NULL) {
		for (j = 0; j < 2; ++j) {
			wlif = nvram_safe_get("wl0_ifname");
			strcpy((char *)mlist, j ? "autho_sta_list" : "authe_sta_list");
			if (wl_ioctl(wlif, WLC_GET_VAR, mlist, mlsize) == 0) {
				for (i = 0; i < mlist->count; ++i) {
					rssi.ea = mlist->ea[i];
					rssi.val = 0;
					if (wl_ioctl(wlif, WLC_GET_RSSI, &rssi, sizeof(rssi)) != 0) continue;

					// sta_info0<mac>
					memset(&sti, 0, sizeof(sti));
					strcpy((char *)&sti, "sta_info");
					memcpy((char *)&sti + 9, rssi.ea.octet, 6);
					if (wl_ioctl(wlif, WLC_GET_VAR, &sti, sizeof(sti)) != 0) continue;

					p = wlif;
					if (sti.flags & WL_STA_WDS) {
						if ((sti.flags & WL_WDS_LINKUP) == 0) continue;
						if (get_wds_ifname(&rssi.ea, ifname)) p = ifname;
					}

					web_printf("%c['%s','%s',%d]",
						comma,
						p,
						ether_etoa(rssi.ea.octet, buf),
						rssi.val);
					comma = ',';
				}
			}
		}
		free(mlist);
	}
#endif

	web_puts("];\n");

	//

	unsigned long expires;
	char mac[32];
	char ip[32];
	char hostname[256];
	char *host;

	web_puts("dhcpd_lease = [");
	if (nvram_match("lan_proto", "dhcp")) {
		f_write("/var/tmp/dhcp/leases.!", NULL, 0, 0, 0666);

		// dump the leases to a file
		if (killall("dnsmasq", SIGUSR2) == 0) {
			// helper in dnsmasq will remove this when it's done
			f_wait_notexists("/var/tmp/dhcp/leases.!", 5);
		}

		if ((f = fopen("/var/tmp/dhcp/leases", "r")) != NULL) {
			comma = ' ';
			while (fgets(buf, sizeof(buf), f)) {
				if (sscanf(buf, "%lu %17s %15s %255s", &expires, mac, ip, hostname) != 4) continue;
				host = js_string((hostname[0] == '*') ? "" : hostname);
				web_printf("%c['%s','%s','%s','%s']", comma,
						(host ? host : ""), ip, mac, ((expires == 0) ? "non-expiring" : reltime(buf, expires)));
				free(host);
				comma = ',';
			}
			fclose(f);
		}
		unlink("/var/tmp/dhcp/leases");
	}
	web_puts("];");
}