+/************************** SET DISPATCHER **************************/
+/*
+ * This is a modified version of the dispatcher in iwlist.
+ * The main difference is that here we may have multiple commands per
+ * line. Also, most commands here do take arguments, and most often
+ * a variable number of them.
+ * Therefore, the handler *must* return how many args were consumed...
+ *
+ * Note that the use of multiple commands per line is not advised
+ * in scripts, as it makes error management hard. All commands before
+ * the error are executed, but commands after the error are not
+ * processed.
+ * We also try to give as much clue as possible via stderr to the caller
+ * on which command did fail, but if there are two time the same command,
+ * you don't know which one failed...
+ */
+
+/*------------------------------------------------------------------*/
+/*
+ * Map command line arguments to the proper procedure...
+ */
+typedef struct iwconfig_entry {
+ const char * cmd; /* Command line shorthand */
+ iw_enum_handler fn; /* Subroutine */
+ int min_count;
+ int request; /* WE numerical ID */
+ const char * name; /* Human readable string */
+ const char * argsname; /* Args as human readable string */
+} iwconfig_cmd;
+
+static const struct iwconfig_entry iwconfig_cmds[] = {
+ { "essid", set_essid_info, 1, SIOCSIWESSID,
+ "Set ESSID", "{NNN|any|on|off}" },
+ { "mode", set_mode_info, 1, SIOCSIWMODE,
+ "Set Mode", "{managed|ad-hoc|master|...}" },
+ { "freq", set_freq_info, 1, SIOCSIWFREQ,
+ "Set Frequency", "N.NNN[k|M|G]" },
+ { "channel", set_freq_info, 1, SIOCSIWFREQ,
+ "Set Frequency", "N" },
+ { "bit", set_bitrate_info, 1, SIOCSIWRATE,
+ "Set Bit Rate", "{N[k|M|G]|auto|fixed}" },
+ { "rate", set_bitrate_info, 1, SIOCSIWRATE,
+ "Set Bit Rate", "{N[k|M|G]|auto|fixed}" },
+ { "enc", set_enc_info, 1, SIOCSIWENCODE,
+ "Set Encode", "{NNNN-NNNN|off}" },
+ { "key", set_enc_info, 1, SIOCSIWENCODE,
+ "Set Encode", "{NNNN-NNNN|off}" },
+ { "power", set_power_info, 1, SIOCSIWPOWER,
+ "Set Power Management", "{period N|timeout N|saving N|off}" },
+#ifndef WE_ESSENTIAL
+ { "nickname", set_nick_info, 1, SIOCSIWNICKN,
+ "Set Nickname", "NNN" },
+ { "nwid", set_nwid_info, 1, SIOCSIWNWID,
+ "Set NWID", "{NN|on|off}" },
+ { "ap", set_apaddr_info, 1, SIOCSIWAP,
+ "Set AP Address", "{N|off|auto}" },
+ { "txpower", set_txpower_info, 1, SIOCSIWTXPOW,
+ "Set Tx Power", "{NmW|NdBm|off|auto}" },
+ { "sens", set_sens_info, 1, SIOCSIWSENS,
+ "Set Sensitivity", "N" },
+ { "retry", set_retry_info, 1, SIOCSIWRETRY,
+ "Set Retry Limit", "{limit N|lifetime N}" },
+ { "rts", set_rts_info, 1, SIOCSIWRTS,
+ "Set RTS Threshold", "{N|auto|fixed|off}" },
+ { "frag", set_frag_info, 1, SIOCSIWFRAG,
+ "Set Fragmentation Threshold", "{N|auto|fixed|off}" },
+ { "modulation", set_modulation_info, 1, SIOCGIWMODUL,
+ "Set Modulation", "{11g|11a|CCK|OFDMg|...}" },
+#endif /* WE_ESSENTIAL */
+ { "commit", set_commit_info, 0, SIOCSIWCOMMIT,
+ "Commit changes", "" },
+ { NULL, NULL, 0, 0, NULL, NULL },
+};
+
+/*------------------------------------------------------------------*/
+/*
+ * Find the most appropriate command matching the command line
+ */
+static inline const iwconfig_cmd *
+find_command(const char * cmd)
+{
+ const iwconfig_cmd * found = NULL;
+ int ambig = 0;
+ unsigned int len = strlen(cmd);
+ int i;
+
+ /* Go through all commands */
+ for(i = 0; iwconfig_cmds[i].cmd != NULL; ++i)
+ {
+ /* No match -> next one */
+ if(strncasecmp(iwconfig_cmds[i].cmd, cmd, len) != 0)
+ continue;
+
+ /* Exact match -> perfect */
+ if(len == strlen(iwconfig_cmds[i].cmd))
+ return &iwconfig_cmds[i];
+
+ /* Partial match */
+ if(found == NULL)
+ /* First time */
+ found = &iwconfig_cmds[i];
+ else
+ /* Another time */
+ if (iwconfig_cmds[i].fn != found->fn)
+ ambig = 1;
+ }
+
+ if(found == NULL)
+ {
+ fprintf(stderr, "iwconfig: unknown command \"%s\"\n", cmd);
+ return NULL;
+ }
+
+ if(ambig)
+ {
+ fprintf(stderr, "iwconfig: command \"%s\" is ambiguous\n", cmd);
+ return NULL;
+ }
+
+ return found;
+}
+
+/*------------------------------------------------------------------*/
+/*
+ * Set the wireless options requested on command line
+ * Find the individual commands and call the appropriate subroutine
+ */
+static int
+set_info(int skfd, /* The socket */
+ char * args[], /* Command line args */
+ int count, /* Args count */
+ char * ifname) /* Dev name */
+{
+ const iwconfig_cmd * iwcmd;
+ int ret;
+
+ /* Loop until we run out of args... */
+ while(count > 0)
+ {
+ /* find the command matching the keyword */
+ iwcmd = find_command(args[0]);
+ if(iwcmd == NULL)
+ {
+ /* Here we have an unrecognised arg... Error already printed out. */
+ return(-1);
+ }
+
+ /* One arg is consumed (the command name) */
+ args++;
+ count--;
+
+ /* Check arg numbers */
+ if(count < iwcmd->min_count)
+ ret = IWERR_ARG_NUM;
+ else
+ ret = 0;
+
+ /* Call the command */
+ if(!ret)
+ ret = (*iwcmd->fn)(skfd, ifname, args, count);
+
+ /* Deal with various errors */
+ if(ret < 0)
+ {
+ int request = iwcmd->request;
+ if(ret == IWERR_GET_EXT)
+ request++; /* Transform the SET into GET */
+
+ fprintf(stderr, "Error for wireless request \"%s\" (%X) :\n",
+ iwcmd->name, request);
+ switch(ret)
+ {
+ case IWERR_ARG_NUM:
+ fprintf(stderr, " too few arguments.\n");
+ break;
+ case IWERR_ARG_TYPE:
+ if(errarg < 0)
+ errarg = 0;
+ if(errarg >= count)
+ errarg = count - 1;
+ fprintf(stderr, " invalid argument \"%s\".\n", args[errarg]);
+ break;
+ case IWERR_ARG_SIZE:
+ fprintf(stderr, " argument too big (max %d)\n", errmax);
+ break;
+ case IWERR_ARG_CONFLICT:
+ if(errarg < 0)
+ errarg = 0;
+ if(errarg >= count)
+ errarg = count - 1;
+ fprintf(stderr, " conflicting argument \"%s\".\n", args[errarg]);
+ break;
+ case IWERR_SET_EXT:
+ fprintf(stderr, " SET failed on device %-1.16s ; %s.\n",
+ ifname, strerror(errno));
+ break;
+ case IWERR_GET_EXT:
+ fprintf(stderr, " GET failed on device %-1.16s ; %s.\n",
+ ifname, strerror(errno));
+ break;
+ }
+ /* Stop processing, we don't know if we are in a consistent state
+ * in reading the command line */
+ return(ret);
+ }
+
+ /* Substract consumed args from command line */
+ args += ret;
+ count -= ret;
+
+ /* Loop back */
+ }
+
+ /* Done, all done */
+ return(0);
+}
+
+/*------------------------------------------------------------------*/
+/*
+ * Display help
+ */
+static inline void
+iw_usage(void)
+{
+ int i;
+
+ fprintf(stderr, "Usage: iwconfig [interface]\n");
+ for(i = 0; iwconfig_cmds[i].cmd != NULL; ++i)
+ fprintf(stderr, " interface %s %s\n",
+ iwconfig_cmds[i].cmd, iwconfig_cmds[i].argsname);
+ fprintf(stderr, " Check man pages for more details.\n");
+}
+
+