wpa-supplicant: remove version tag from directory
[dragonfly.git] / contrib / wpa_supplicant / main.c
1 /*
2  * WPA Supplicant / main() function for UNIX like OSes and MinGW
3  * Copyright (c) 2003-2007, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "includes.h"
16 #ifdef __linux__
17 #include <fcntl.h>
18 #endif /* __linux__ */
19
20 #include "common.h"
21 #include "wpa_supplicant_i.h"
22
23
24 extern const char *wpa_supplicant_version;
25 extern const char *wpa_supplicant_license;
26 #ifndef CONFIG_NO_STDOUT_DEBUG
27 extern const char *wpa_supplicant_full_license1;
28 extern const char *wpa_supplicant_full_license2;
29 extern const char *wpa_supplicant_full_license3;
30 extern const char *wpa_supplicant_full_license4;
31 extern const char *wpa_supplicant_full_license5;
32 #endif /* CONFIG_NO_STDOUT_DEBUG */
33
34 extern struct wpa_driver_ops *wpa_supplicant_drivers[];
35
36
37 static void usage(void)
38 {
39         int i;
40         printf("%s\n\n%s\n"
41                "usage:\n"
42                "  wpa_supplicant [-BddehLqquvwW] [-P<pid file>] "
43                "[-g<global ctrl>] \\\n"
44                "        -i<ifname> -c<config file> [-C<ctrl>] [-D<driver>] "
45                "[-p<driver_param>] \\\n"
46                "        [-b<br_ifname> [-N -i<ifname> -c<conf> [-C<ctrl>] "
47                "[-D<driver>] \\\n"
48                "        [-p<driver_param>] [-b<br_ifname>] ...]\n"
49                "\n"
50                "drivers:\n",
51                wpa_supplicant_version, wpa_supplicant_license);
52
53         for (i = 0; wpa_supplicant_drivers[i]; i++) {
54                 printf("  %s = %s\n",
55                        wpa_supplicant_drivers[i]->name,
56                        wpa_supplicant_drivers[i]->desc);
57         }
58
59 #ifndef CONFIG_NO_STDOUT_DEBUG
60         printf("options:\n"
61                "  -b = optional bridge interface name\n"
62                "  -B = run daemon in the background\n"
63                "  -c = Configuration file\n"
64                "  -C = ctrl_interface parameter (only used if -c is not)\n"
65                "  -i = interface name\n"
66                "  -d = increase debugging verbosity (-dd even more)\n"
67                "  -D = driver name\n"
68                "  -g = global ctrl_interface\n"
69                "  -K = include keys (passwords, etc.) in debug output\n"
70                "  -t = include timestamp in debug messages\n"
71                "  -h = show this help text\n"
72                "  -L = show license (GPL and BSD)\n");
73         printf("  -p = driver parameters\n"
74                "  -P = PID file\n"
75                "  -q = decrease debugging verbosity (-qq even less)\n"
76 #ifdef CONFIG_CTRL_IFACE_DBUS
77                "  -u = enable DBus control interface\n"
78 #endif /* CONFIG_CTRL_IFACE_DBUS */
79                "  -v = show version\n"
80                "  -w = wait for interface to be added, if needed\n"
81                "  -W = wait for a control interface monitor before starting\n"
82                "  -N = start describing new interface\n");
83
84         printf("example:\n"
85                "  wpa_supplicant -Dwext -iwlan0 -c/etc/wpa_supplicant.conf\n");
86 #endif /* CONFIG_NO_STDOUT_DEBUG */
87 }
88
89
90 static void license(void)
91 {
92 #ifndef CONFIG_NO_STDOUT_DEBUG
93         printf("%s\n\n%s%s%s%s%s\n",
94                wpa_supplicant_version,
95                wpa_supplicant_full_license1,
96                wpa_supplicant_full_license2,
97                wpa_supplicant_full_license3,
98                wpa_supplicant_full_license4,
99                wpa_supplicant_full_license5);
100 #endif /* CONFIG_NO_STDOUT_DEBUG */
101 }
102
103
104 static void wpa_supplicant_fd_workaround(void)
105 {
106 #ifdef __linux__
107         int s, i;
108         /* When started from pcmcia-cs scripts, wpa_supplicant might start with
109          * fd 0, 1, and 2 closed. This will cause some issues because many
110          * places in wpa_supplicant are still printing out to stdout. As a
111          * workaround, make sure that fd's 0, 1, and 2 are not used for other
112          * sockets. */
113         for (i = 0; i < 3; i++) {
114                 s = open("/dev/null", O_RDWR);
115                 if (s > 2) {
116                         close(s);
117                         break;
118                 }
119         }
120 #endif /* __linux__ */
121 }
122
123
124 int main(int argc, char *argv[])
125 {
126         int c, i;
127         struct wpa_interface *ifaces, *iface;
128         int iface_count, exitcode = -1;
129         struct wpa_params params;
130         struct wpa_global *global;
131
132         if (os_program_init())
133                 return -1;
134
135         os_memset(&params, 0, sizeof(params));
136         params.wpa_debug_level = MSG_INFO;
137
138         iface = ifaces = os_zalloc(sizeof(struct wpa_interface));
139         if (ifaces == NULL)
140                 return -1;
141         iface_count = 1;
142
143         wpa_supplicant_fd_workaround();
144
145         for (;;) {
146                 c = getopt(argc, argv, "b:Bc:C:D:dg:hi:KLNp:P:qtuvwW");
147                 if (c < 0)
148                         break;
149                 switch (c) {
150                 case 'b':
151                         iface->bridge_ifname = optarg;
152                         break;
153                 case 'B':
154                         params.daemonize++;
155                         break;
156                 case 'c':
157                         iface->confname = optarg;
158                         break;
159                 case 'C':
160                         iface->ctrl_interface = optarg;
161                         break;
162                 case 'D':
163                         iface->driver = optarg;
164                         break;
165                 case 'd':
166 #ifdef CONFIG_NO_STDOUT_DEBUG
167                         printf("Debugging disabled with "
168                                "CONFIG_NO_STDOUT_DEBUG=y build time "
169                                "option.\n");
170                         goto out;
171 #else /* CONFIG_NO_STDOUT_DEBUG */
172                         params.wpa_debug_level--;
173                         break;
174 #endif /* CONFIG_NO_STDOUT_DEBUG */
175                 case 'g':
176                         params.ctrl_interface = optarg;
177                         break;
178                 case 'h':
179                         usage();
180                         exitcode = 0;
181                         goto out;
182                 case 'i':
183                         iface->ifname = optarg;
184                         break;
185                 case 'K':
186                         params.wpa_debug_show_keys++;
187                         break;
188                 case 'L':
189                         license();
190                         exitcode = 0;
191                         goto out;
192                 case 'p':
193                         iface->driver_param = optarg;
194                         break;
195                 case 'P':
196                         os_free(params.pid_file);
197                         params.pid_file = os_rel2abs_path(optarg);
198                         break;
199                 case 'q':
200                         params.wpa_debug_level++;
201                         break;
202                 case 't':
203                         params.wpa_debug_timestamp++;
204                         break;
205 #ifdef CONFIG_CTRL_IFACE_DBUS
206                 case 'u':
207                         params.dbus_ctrl_interface = 1;
208                         break;
209 #endif /* CONFIG_CTRL_IFACE_DBUS */
210                 case 'v':
211                         printf("%s\n", wpa_supplicant_version);
212                         exitcode = 0;
213                         goto out;
214                 case 'w':
215                         params.wait_for_interface++;
216                         break;
217                 case 'W':
218                         params.wait_for_monitor++;
219                         break;
220                 case 'N':
221                         iface_count++;
222                         iface = os_realloc(ifaces, iface_count *
223                                            sizeof(struct wpa_interface));
224                         if (iface == NULL)
225                                 goto out;
226                         ifaces = iface;
227                         iface = &ifaces[iface_count - 1]; 
228                         os_memset(iface, 0, sizeof(*iface));
229                         break;
230                 default:
231                         usage();
232                         exitcode = 0;
233                         goto out;
234                 }
235         }
236
237         exitcode = 0;
238         global = wpa_supplicant_init(&params);
239         if (global == NULL) {
240                 printf("Failed to initialize wpa_supplicant\n");
241                 exitcode = -1;
242                 goto out;
243         }
244
245         for (i = 0; exitcode == 0 && i < iface_count; i++) {
246                 if ((ifaces[i].confname == NULL &&
247                      ifaces[i].ctrl_interface == NULL) ||
248                     ifaces[i].ifname == NULL) {
249                         if (iface_count == 1 && (params.ctrl_interface ||
250                                                  params.dbus_ctrl_interface))
251                                 break;
252                         usage();
253                         exitcode = -1;
254                         break;
255                 }
256                 if (wpa_supplicant_add_iface(global, &ifaces[i]) == NULL)
257                         exitcode = -1;
258         }
259
260         if (exitcode == 0)
261                 exitcode = wpa_supplicant_run(global);
262
263         wpa_supplicant_deinit(global);
264
265 out:
266         os_free(ifaces);
267         os_free(params.pid_file);
268
269         os_program_deinit();
270
271         return exitcode;
272 }