Annotation of embedtools/src/dwds.c, revision 1.1.2.10
1.1.2.1 misho 1: #include "global.h"
2: #include "dwds.h"
3:
4:
1.1.2.4 misho 5: sl_config cfg;
6: int Verbose, Kill, nif;
7: char **ifs, szConfig[MAXPATHLEN] = DWDS_CONFIG;
1.1.2.2 misho 8: extern char compiled[], compiledby[], compilehost[];
9:
10:
11: static void
12: Usage()
13: {
1.1.2.3 misho 14: printf( "-= dWDS =- WiFi dynamic WDS service managment for VAP\n"
1.1.2.2 misho 15: "=== %s === %s@%s ===\n\n"
1.1.2.3 misho 16: " Syntax: dwds [options] <interface|any> [interface [...]]\n"
1.1.2.2 misho 17: "\n"
18: "\t-v\t\tVerbose ...\n"
19: "\t-f\t\tForeground, not demonize process ...\n"
20: "\t-c <config>\tConfig file [default=/etc/dwds.conf]\n"
21: "\n", compiled, compiledby, compilehost);
22: }
23:
1.1.2.4 misho 24: static void
25: sigHandler(int sig)
26: {
27: int stat;
28: const u_char *v;
29: char szStr[STRSIZ];
30:
31: switch (sig) {
32: case SIGHUP:
33: UnloadConfig(&cfg);
34: if (LoadConfig(szConfig, &cfg)) {
35: printf("Error:: can`t load config %s ...\n", szConfig);
36: Kill++;
37: } else {
38: closelog();
39:
40: cfg_LoadAttribute(&cfg, CFG("dwds"), CFG("name"), CFG(szStr), STRSIZ, DWDS_NAME);
41: openlog(szStr, LOG_PID | LOG_CONS, LOG_DAEMON);
42: v = cfg_GetAttribute(&cfg, CFG("dwds"), CFG("syslog_upto"));
43: setlogmask(v ? strtol((char*) v, NULL, 0) : 0);
44: }
45: break;
46: case SIGTERM:
47: Kill++;
48: break;
49: case SIGCHLD:
50: while (waitpid(-1, &stat, WNOHANG) > 0);
51: break;
52: }
53: }
54:
55: static int
1.1.2.8 misho 56: RtMsg(struct dwds_if **wds, struct rt_msghdr *msg, size_t len)
1.1.2.4 misho 57: {
1.1.2.8 misho 58: struct if_announcemsghdr *ifan;
59: const u_char *v;
60: struct ether_addr bssid;
1.1.2.9 misho 61: char szStr[STRSIZ] = { 0 }, szCmd[MAXPATHLEN] = { 0 };
1.1.2.10! misho 62: int f, stat;
1.1.2.8 misho 63:
64: assert(wds);
65: assert(msg);
66:
67: if (msg->rtm_version != RTM_VERSION) {
68: syslog(LOG_ERR, "Error:: routing message version %d not understood!\n", msg->rtm_version);
69: return -1;
70: }
71:
72: switch (msg->rtm_type) {
73: case RTM_IFANNOUNCE:
74: ifan = (struct if_announcemsghdr*) msg;
75: switch (ifan->ifan_what) {
76: case IFAN_ARRIVAL:
77: VERB(1) syslog(LOG_INFO, "RTM_IFANNOUNCE: if# %d, what: arrival\n",
78: ifan->ifan_index);
79: break;
80: case IFAN_DEPARTURE:
81: VERB(1) syslog(LOG_INFO, "RTM_IFANNOUNCE: if# %d, what: departure\n",
82: ifan->ifan_index);
83: wifi_destroyWDS(ifan->ifan_name, wds);
84: break;
85: }
86: break;
87: case RTM_IEEE80211:
88: #define V(type) ((struct type *)(&ifan[1]))
89: ifan = (struct if_announcemsghdr*) msg;
90: switch (ifan->ifan_what) {
91: case RTM_IEEE80211_DISASSOC:
92: v = cfg_GetAttribute(&cfg, CFG("dwds"), CFG("discover_on_join"));
93: if (!v || !strtol((char*) v, NULL, 0))
94: break;
95: /* fall thru ... */
96: case RTM_IEEE80211_LEAVE:
1.1.2.9 misho 97: if (wifi_chkIface(ifan->ifan_name, ifs, nif) < 0)
98: break;
99: memcpy(&bssid, V(ieee80211_leave_event)->iev_addr, ETHER_ADDR_LEN);
100: VERB(1) syslog(LOG_INFO, "BSSID:%s Station leave\n", ether_ntoa(&bssid));
101: if (!wifi_leaveWDS(bssid, wds)) {
1.1.2.10! misho 102: /* delete state file ... */
! 103: v = cfg_GetAttribute(&cfg, CFG("dwds"), CFG("state_dir"));
! 104: if (v && strtol((char*) v, NULL, 0)) {
! 105: memset(szCmd, 0, STRSIZ);
! 106: snprintf(szCmd, MAXPATHLEN, "%s/%s-%s", (char*) v,
! 107: ifan->ifan_name, ether_ntoa(&bssid));
! 108: unlink(szCmd);
! 109: VERB(2) syslog(LOG_DEBUG, "Debug:: delete session name %s\n", szCmd);
! 110: }
! 111:
1.1.2.9 misho 112: /* Launch script ... */
113: cfg_LoadAttribute(&cfg, CFG("dwds"), CFG("disassoc_event"),
114: CFG(szStr), STRSIZ, NULL);
115: if (*szStr) {
1.1.2.10! misho 116: memset(szCmd, 0, MAXPATHLEN);
1.1.2.9 misho 117: snprintf(szCmd, MAXPATHLEN, "%s %s %s", szStr,
118: ifan->ifan_name, ether_ntoa(&bssid));
119: VERB(3) syslog(LOG_DEBUG, "Debug:: Command line: %s\n", szCmd);
120:
121: if ((stat = system(szCmd)))
122: syslog(LOG_ERR, "VAP down script %s exited "
123: "with status %d\n", szStr, stat);
124: }
125: }
1.1.2.8 misho 126: break;
127:
128: case RTM_IEEE80211_JOIN:
129: case RTM_IEEE80211_REJOIN:
130: case RTM_IEEE80211_ASSOC:
131: case RTM_IEEE80211_REASSOC:
132: v = cfg_GetAttribute(&cfg, CFG("dwds"), CFG("discover_on_join"));
133: if (!v || !strtol((char*) v, NULL, 0))
134: break;
135: /* fall thru ... */
136: case RTM_IEEE80211_WDS:
1.1.2.9 misho 137: memcpy(&bssid, V(ieee80211_leave_event)->iev_addr, ETHER_ADDR_LEN);
138: VERB(1) syslog(LOG_INFO, "BSSID:%s WDS discovery\n", ether_ntoa(&bssid));
139: if (wifi_chkIface(ifan->ifan_name, ifs, nif) < 0)
140: break;
141: if (!wifi_createWDS(ifan->ifan_name, bssid, wds)) {
1.1.2.10! misho 142: /* create state file ... */
! 143: v = cfg_GetAttribute(&cfg, CFG("dwds"), CFG("state_dir"));
! 144: if (v && strtol((char*) v, NULL, 0)) {
! 145: memset(szCmd, 0, MAXPATHLEN);
! 146: snprintf(szCmd, MAXPATHLEN, "%s/%s-%s", (char*) v,
! 147: ifan->ifan_name, ether_ntoa(&bssid));
! 148: f = open(szCmd, O_WRONLY | O_CREAT, 0644);
! 149: if (f != -1)
! 150: close(f);
! 151: VERB(2) syslog(LOG_DEBUG, "Debug:: create session name %s\n", szCmd);
! 152: }
! 153:
1.1.2.9 misho 154: /* Launch script ... */
155: cfg_LoadAttribute(&cfg, CFG("dwds"), CFG("assoc_event"),
156: CFG(szStr), STRSIZ, NULL);
157: if (*szStr) {
1.1.2.10! misho 158: memset(szCmd, 0, MAXPATHLEN);
1.1.2.9 misho 159: snprintf(szCmd, MAXPATHLEN, "%s %s %s", szStr,
160: ifan->ifan_name, ether_ntoa(&bssid));
161: VERB(3) syslog(LOG_DEBUG, "Debug:: Command line: %s\n", szCmd);
162:
163: if ((stat = system(szCmd)))
164: syslog(LOG_ERR, "VAP up script %s exited "
165: "with status %d\n", szStr, stat);
166: }
167: }
1.1.2.8 misho 168: break;
169: }
170: #undef V
171: break;
172: }
173:
1.1.2.4 misho 174: return 0;
175: }
176:
1.1.2.2 misho 177: // ---------------------------------------------------------------
178:
1.1.2.1 misho 179: int
180: main(int argc, char **argv)
181: {
1.1.2.4 misho 182: char ch, szStr[STRSIZ], fg = 0;
183: const u_char *v, msg[2048];
184: int s;
185: struct sigaction sa;
186: size_t len;
1.1.2.6 misho 187: struct dwds_if *wds = NULL;
1.1.2.2 misho 188:
1.1.2.3 misho 189: while ((ch = getopt(argc, argv, "hvfc:")) != -1)
190: switch (ch) {
191: case 'v':
192: Verbose++;
193: break;
194: case 'f':
195: fg = 1;
196: break;
197: case 'c':
198: strlcpy(szConfig, optarg, MAXPATHLEN);
199: break;
200: case 'h':
201: default:
202: Usage();
203: return 1;
204: }
205: argc -= optind;
206: argv += optind;
207: if (!argc) {
208: printf("Error:: not specified interface for use ...\n");
209: Usage();
210: return 1;
1.1.2.4 misho 211: } else {
212: nif = argc;
213: ifs = argv;
214: }
215: if (LoadConfig(szConfig, &cfg)) {
216: printf("Error:: can`t load config %s ...\n", szConfig);
217: return 1;
218: }
219:
1.1.2.7 misho 220: if (!fg)
1.1.2.4 misho 221: switch (fork()) {
222: case -1:
223: printf("Error:: when fork() #%d - %s\n", errno, strerror(errno));
224: UnloadConfig(&cfg);
225: return 2;
226: case 0 :
227: VERB(1) printf("Going to shadow land ...\n");
228:
229: setsid();
230:
231: memset(&sa, 0, sizeof sa);
232: sa.sa_handler = sigHandler;
233: sigemptyset(&sa.sa_mask);
234: sigaction(SIGHUP, &sa, NULL);
235: sigaction(SIGTERM, &sa, NULL);
236: sigaction(SIGCHLD, &sa, NULL);
237: break;
238: default:
239: goto end;
240: }
241:
242: cfg_LoadAttribute(&cfg, CFG("dwds"), CFG("name"), CFG(szStr), STRSIZ, DWDS_NAME);
243: openlog(szStr, LOG_PID | LOG_CONS, LOG_DAEMON);
244: v = cfg_GetAttribute(&cfg, CFG("dwds"), CFG("syslog_upto"));
245: setlogmask(v ? strtol((char*) v, NULL, 0) : 0);
246:
247: s = socket(PF_ROUTE, SOCK_RAW, 0);
248: if (s == -1) {
249: syslog(LOG_ERR, "Error:: socket() #%d - %s\n", errno, strerror(errno));
250: goto end;
251: }
252:
1.1.2.7 misho 253: if (!(wds = wifi_buildWDS(s, ifs, nif))) {
254: syslog(LOG_ERR, "Error:: Go to dead ...\n");
1.1.2.6 misho 255: goto end;
1.1.2.7 misho 256: }
1.1.2.6 misho 257:
1.1.2.4 misho 258: while (!Kill) {
259: len = read(s, (void*) msg, sizeof msg);
260: if (len == -1) {
261: syslog(LOG_ERR, "Error:: read() #%d - %s\n", errno, strerror(errno));
262: Kill++;
263: } else
1.1.2.8 misho 264: RtMsg(&wds, (struct rt_msghdr*) msg, len);
1.1.2.3 misho 265: }
266:
1.1.2.4 misho 267: close(s);
268: end:
269: closelog();
270: UnloadConfig(&cfg);
1.1.2.1 misho 271: return 0;
272: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>