00001
00016 #include <stdlib.h>
00017 #include <stdio.h>
00018 #include <unistd.h>
00019 #include <string.h>
00020 #include <fcntl.h>
00021
00022 #include "common.h"
00023 #include "wpa_supplicant_i.h"
00024
00025
00026 extern const char *wpa_supplicant_version;
00027 extern const char *wpa_supplicant_license;
00028 #ifndef CONFIG_NO_STDOUT_DEBUG
00029 extern const char *wpa_supplicant_full_license;
00030 #endif
00031
00032 extern struct wpa_driver_ops *wpa_supplicant_drivers[];
00033
00034
00035 static void usage(void)
00036 {
00037 int i;
00038 printf("%s\n\n%s\n"
00039 "usage:\n"
00040 " wpa_supplicant [-BddehLqqvwW] [-P<pid file>] "
00041 "[-g<global ctrl>] \\\n"
00042 " -i<ifname> -c<config file> [-C<ctrl>] [-D<driver>] "
00043 "[-p<driver_param>] \\\n"
00044 " [-N -i<ifname> -c<conf> [-C<ctrl>] [-D<driver>] "
00045 "[-p<driver_param>] ...]\n"
00046 "\n"
00047 "drivers:\n",
00048 wpa_supplicant_version, wpa_supplicant_license);
00049
00050 for (i = 0; wpa_supplicant_drivers[i]; i++) {
00051 printf(" %s = %s\n",
00052 wpa_supplicant_drivers[i]->name,
00053 wpa_supplicant_drivers[i]->desc);
00054 }
00055
00056 #ifndef CONFIG_NO_STDOUT_DEBUG
00057 printf("options:\n"
00058 " -B = run daemon in the background\n"
00059 " -c = Configuration file\n"
00060 " -C = ctrl_interface parameter (only used if -c is not)\n"
00061 " -i = interface name\n"
00062 " -d = increase debugging verbosity (-dd even more)\n"
00063 " -D = driver name\n"
00064 " -g = global ctrl_interface\n"
00065 " -K = include keys (passwords, etc.) in debug output\n"
00066 " -t = include timestamp in debug messages\n"
00067 " -h = show this help text\n"
00068 " -L = show license (GPL and BSD)\n"
00069 " -p = driver parameters\n"
00070 " -P = PID file\n"
00071 " -q = decrease debugging verbosity (-qq even less)\n"
00072 " -v = show version\n"
00073 " -w = wait for interface to be added, if needed\n"
00074 " -W = wait for a control interface monitor before starting\n"
00075 " -N = start describing new interface\n");
00076
00077 printf("example:\n"
00078 " wpa_supplicant -Dwext -iwlan0 -c/etc/wpa_supplicant.conf\n");
00079 #endif
00080 }
00081
00082
00083 static void license(void)
00084 {
00085 #ifndef CONFIG_NO_STDOUT_DEBUG
00086 printf("%s\n\n%s\n",
00087 wpa_supplicant_version, wpa_supplicant_full_license);
00088 #endif
00089 }
00090
00091
00092 static void wpa_supplicant_fd_workaround(void)
00093 {
00094 int s, i;
00095
00096
00097
00098
00099
00100 for (i = 0; i < 3; i++) {
00101 s = open("/dev/null", O_RDWR);
00102 if (s > 2) {
00103 close(s);
00104 break;
00105 }
00106 }
00107 }
00108
00109
00110 int main(int argc, char *argv[])
00111 {
00112 int c, i;
00113 struct wpa_interface *ifaces, *iface;
00114 int iface_count, exitcode = -1;
00115 struct wpa_params params;
00116 struct wpa_global *global;
00117
00118 #ifdef CONFIG_NATIVE_WINDOWS
00119 WSADATA wsaData;
00120 if (WSAStartup(MAKEWORD(2, 0), &wsaData)) {
00121 printf("Could not find a usable WinSock.dll\n");
00122 return -1;
00123 }
00124 #endif
00125
00126 memset(¶ms, 0, sizeof(params));
00127 params.wpa_debug_level = MSG_INFO;
00128
00129 iface = ifaces = malloc(sizeof(struct wpa_interface));
00130 if (ifaces == NULL)
00131 return -1;
00132 memset(iface, 0, sizeof(*iface));
00133 iface_count = 1;
00134
00135 wpa_supplicant_fd_workaround();
00136
00137 for (;;) {
00138 c = getopt(argc, argv, "Bc:C:D:dg:hi:KLNp:P:qtvwW");
00139 if (c < 0)
00140 break;
00141 switch (c) {
00142 case 'B':
00143 params.daemonize++;
00144 break;
00145 case 'c':
00146 iface->confname = optarg;
00147 break;
00148 case 'C':
00149 iface->ctrl_interface = optarg;
00150 break;
00151 case 'D':
00152 iface->driver = optarg;
00153 break;
00154 case 'd':
00155 #ifdef CONFIG_NO_STDOUT_DEBUG
00156 printf("Debugging disabled with "
00157 "CONFIG_NO_STDOUT_DEBUG=y build time "
00158 "option.\n");
00159 goto out;
00160 #else
00161 params.wpa_debug_level--;
00162 break;
00163 #endif
00164 case 'g':
00165 params.ctrl_interface = optarg;
00166 break;
00167 case 'h':
00168 usage();
00169 goto out;
00170 case 'i':
00171 iface->ifname = optarg;
00172 break;
00173 case 'K':
00174 params.wpa_debug_show_keys++;
00175 break;
00176 case 'L':
00177 license();
00178 goto out;
00179 case 'p':
00180 iface->driver_param = optarg;
00181 break;
00182 case 'P':
00183 params.pid_file = rel2abs_path(optarg);
00184 break;
00185 case 'q':
00186 params.wpa_debug_level++;
00187 break;
00188 case 't':
00189 params.wpa_debug_timestamp++;
00190 break;
00191 case 'v':
00192 printf("%s\n", wpa_supplicant_version);
00193 goto out;
00194 case 'w':
00195 params.wait_for_interface++;
00196 break;
00197 case 'W':
00198 params.wait_for_monitor++;
00199 break;
00200 case 'N':
00201 iface_count++;
00202 iface = realloc(ifaces, iface_count *
00203 sizeof(struct wpa_interface));
00204 if (iface == NULL)
00205 goto out;
00206 ifaces = iface;
00207 iface = &ifaces[iface_count - 1];
00208 memset(iface, 0, sizeof(*iface));
00209 break;
00210 default:
00211 usage();
00212 goto out;
00213 }
00214 }
00215
00216 exitcode = 0;
00217 global = wpa_supplicant_init(¶ms);
00218 if (global == NULL) {
00219 printf("Failed to initialize wpa_supplicant\n");
00220 exitcode = -1;
00221 }
00222
00223 for (i = 0; exitcode == 0 && i < iface_count; i++) {
00224 if ((ifaces[i].confname == NULL &&
00225 ifaces[i].ctrl_interface == NULL) ||
00226 ifaces[i].ifname == NULL) {
00227 if (iface_count == 1 && params.ctrl_interface)
00228 break;
00229 usage();
00230 exitcode = -1;
00231 break;
00232 }
00233 if (wpa_supplicant_add_iface(global, &ifaces[i]) == NULL)
00234 exitcode = -1;
00235 }
00236
00237 if (exitcode == 0)
00238 exitcode = wpa_supplicant_run(global);
00239
00240 wpa_supplicant_deinit(global);
00241
00242 out:
00243 free(ifaces);
00244 free(params.pid_file);
00245
00246 #ifdef CONFIG_NATIVE_WINDOWS
00247 WSACleanup();
00248 #endif
00249
00250 return exitcode;
00251 }
00252