00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00036 #include <errno.h>
00037 #include <stdarg.h>
00038 #include <stdlib.h>
00039 #include <stdio.h>
00040 #include <string.h>
00041 #include <syslog.h>
00042 #include <unistd.h>
00043 #include "vif.h"
00044 #include "vifctl_print.h"
00045
00046
00047 char *exec_name = NULL;
00048 char vifctl_version[] = "0.3.0 (beta)\0";
00049 int vifctl_dbg = 0;
00050 int create_vif = 0;
00051 int delete_vif = 0;
00052 int list_vif = 0;
00053 int notify_id = -1;
00054 const char *arg_name = NULL;
00056 extern int (*vifctl_debug)(const char* format, ...);
00057 extern void (*vifctl_debug_args)(const char* format, va_list args);
00058
00059
00060
00061
00063 void
00064 vifctl_usage(int rtrn)
00065 {
00066 printf("Usage:\t%s [-cdhlr] VIFNAME\n", exec_name);
00067 printf("\tManage virtual wireless interfaces - Vifctl v%s\n", PACKAGE_VERSION);
00068 printf("%s\t-c VIFNAME\tCreate virtual wireless interface\n", exec_name);
00069 printf("\t-r VIFNAME\tRemove virtual wireless interface\n");
00070 printf("\t-l\t\tList virtual wireless interfaces\n");
00071 printf("\t-d\t\tEnable verbose output\n");
00072 printf("\t-h\t\tPrint this help\n");
00073 exit(rtrn);
00074 }
00075
00076
00078 int
00079 ignore_debug(const char* text, ...)
00080 {
00081 return 0;
00082 }
00083
00084
00086 int
00087 print_debug(const char *format, ...)
00088 {
00089 va_list arg;
00090
00091 va_start(arg, format);
00092 vifctl_debug_args(format, arg);
00093 va_end(arg);
00094
00095 return 0;
00096 }
00097
00098
00100 void
00101 ignore_debug_args(const char *format, va_list arg)
00102 {
00103 }
00104
00105
00107 void
00108 print_debug_args(const char *format, va_list arg)
00109 {
00110 FILE *stdout = fopen("/dev/stdout", "a");
00111
00112 vfprintf(stdout, format, arg);
00113 fclose(stdout);
00114 }
00115
00116
00117 int
00118 vifctl_error(const char *format, ...)
00119 {
00120 int chars_printed = 0;
00121 va_list arg;
00122
00123 FILE *stderr = fopen("/dev/stderr", "a");
00124 if (stderr == NULL)
00125 return -1;
00126 chars_printed = fprintf(stderr, "Error: ");
00127 va_start(arg, format);
00128 chars_printed += vfprintf(stderr, format, arg);
00129 va_end(arg);
00130 fclose(stderr);
00131
00132 return chars_printed;
00133 }
00134
00135
00136 void
00137 vifctl_log(const char *format, ...)
00138 {
00139 va_list arg;
00140
00141
00142 openlog(exec_name, LOG_PID, LOG_SYSLOG);
00143 va_start(arg, format);
00144 vsyslog(LOG_INFO, format, arg);
00145 closelog();
00146 va_end(arg);
00147
00148
00149 FILE *stdout = fopen("/dev/stdout", "a");
00150 if (stdout == NULL)
00151 return;
00152 va_start(arg, format);
00153 vfprintf(stdout, format, arg);
00154 va_end(arg);
00155 fclose(stdout);
00156 }
00157
00158
00160 ssize_t
00161 vifctl_parse_args(int argc, char *argv[])
00162 {
00163 int opt = 0;
00164 int num_args = argc - 1;
00165 ssize_t list_size = 0;
00166
00167
00168 vifctl_debug = ignore_debug;
00169 vifctl_debug_args = ignore_debug_args;
00170
00171
00172 if (num_args == 0)
00173 {
00174 vifctl_error("Missing argument\n");
00175 vifctl_usage(EXIT_FAILURE);
00176 }
00177
00178
00179 while ((opt = getopt(argc, argv, "dhc:r:l")) != -1)
00180 {
00181 switch (opt)
00182 {
00183 case 'd':
00184 vifctl_dbg = 1;
00185 break;
00186 case 'h':
00187 vifctl_usage(EXIT_SUCCESS);
00188 break;
00189 case 'c':
00190 if (delete_vif == 1)
00191 {
00192 vifctl_error("create+remove? Make up your mind!\n");
00193 exit(EINVAL);
00194 }
00195 create_vif = 1;
00196 arg_name = optarg;
00197 break;
00198 case 'r':
00199 if (create_vif == 1)
00200 {
00201 vifctl_error("create+remove? Make up your mind!\n");
00202 exit(EINVAL);
00203 }
00204 delete_vif = 1;
00205 arg_name = optarg;
00206 break;
00207 case 'l':
00208 list_vif = 1;
00209 break;
00210 }
00211 }
00212
00213
00214 if (vifctl_dbg == 1)
00215 {
00216 vifctl_debug = print_debug;
00217 vif_set_debug_function(vifctl_debug);
00218 vifctl_debug_args = print_debug_args;
00219 }
00220 vif_set_error_function(vifctl_error);
00221
00222
00223 if (arg_name != NULL)
00224 {
00225 if (strlen(arg_name) > IFNAMSIZ)
00226 {
00227 vifctl_error("Interface name too long\n");
00228 exit(EXIT_FAILURE);
00229 }
00230 if (strlen(arg_name) < 2)
00231 {
00232 vifctl_error("Interface name '%s' too short\n", arg_name);
00233 exit(EXIT_FAILURE);
00234 }
00235 }
00236
00237 list_size = vif_init();
00238 if ((list_size == MAX_NUM_VIF) && (create_vif == 1))
00239 {
00240 vifctl_error("Already have maximum of %i interfaces\n", MAX_NUM_VIF);
00241 exit(EXIT_FAILURE);
00242 }
00243 if ((list_size == 0) && (delete_vif == 1))
00244 {
00245 vifctl_error("No virtual wireless interfaces found\n");
00246 exit(EXIT_FAILURE);
00247 }
00248
00249 if ((arg_name == NULL) && (list_vif == 0))
00250 {
00251 vifctl_error("Missing Argument, need at least -c, -r or -l\n");
00252 vifctl_usage(EXIT_FAILURE);
00253 }
00254
00255 return list_size;
00256 }
00257
00258
00260 int
00261 main(int argc, char *argv[])
00262 {
00263 int i = 0;
00264 int rtrn = 0;
00265 char name_list[MAX_NUM_VIF][IFNAMSIZ + 1];
00266 int list_size = 0;
00267
00268
00269 if (strrchr(argv[0], '/') == NULL)
00270 exec_name = argv[0];
00271 else
00272 exec_name = strrchr(argv[0], '/') + 1;
00273
00274
00275 list_size = vifctl_parse_args(argc, argv);
00276
00277 if (create_vif == 1)
00278 {
00279 rtrn = vif_create(arg_name);
00280 if (rtrn == 0)
00281 vifctl_log("Virtual wireless interface %s created\n", arg_name);
00282 }
00283 else if (delete_vif == 1)
00284 {
00285 rtrn = vif_delete(arg_name);
00286 if (rtrn == 0)
00287 vifctl_log("Interface %s deleted\n", arg_name);
00288 else
00289 vifctl_error("Failed to delete %s\n", arg_name);
00290 }
00291 if (list_vif == 1)
00292 {
00293 if (list_size == 0)
00294 {
00295 printf("No virtual wireless interfaces found\n");
00296 rtrn = EXIT_FAILURE;
00297 } else {
00298 rtrn = vif_get_names(name_list);
00299 if (rtrn < 1)
00300 {
00301 vifctl_error("main(): vif_get_names() failed\n");
00302 rtrn = EXIT_FAILURE;
00303 } else {
00304 printf("Virtual wireless interfaces:\t");
00305 for (i = 0; i < rtrn; i++)
00306 printf("%s ", name_list[i]);
00307 printf("\n");
00308 }
00309 }
00310 }
00311
00312 return rtrn;
00313 }