- /* get options */
- while (1) {
- int option_index = 0;
- c = getopt_long(argc, argv, "c:fbH:h:F:i:np:qr:s:v", arg_options, &option_index);
- if (c == -1) break;
-
- switch (c) {
- case 'c':
- len = strlen(optarg) > 255 ? 255 : strlen(optarg);
- if (client_config.clientid) free(client_config.clientid);
- client_config.clientid = xmalloc(len + 2);
- client_config.clientid[OPT_CODE] = DHCP_CLIENT_ID;
- client_config.clientid[OPT_LEN] = len;
- client_config.clientid[OPT_DATA] = '\0';
- strncpy(client_config.clientid + OPT_DATA, optarg, len);
- break;
- case 'f':
- client_config.foreground = 1;
- break;
- case 'b':
- client_config.background_if_no_lease = 1;
- break;
- case 'h':
- case 'H':
- len = strlen(optarg) > 255 ? 255 : strlen(optarg);
- if (client_config.hostname) free(client_config.hostname);
- client_config.hostname = xmalloc(len + 2);
- client_config.hostname[OPT_CODE] = DHCP_HOST_NAME;
- client_config.hostname[OPT_LEN] = len;
- strncpy(client_config.hostname + 2, optarg, len);
- break;
- case 'F':
- len = strlen(optarg) > 255 ? 255 : strlen(optarg);
- if (client_config.fqdn) free(client_config.fqdn);
- client_config.fqdn = xmalloc(len + 5);
- client_config.fqdn[OPT_CODE] = DHCP_FQDN;
- client_config.fqdn[OPT_LEN] = len + 3;
- /* Flags: 0000NEOS
- S: 1 => Client requests Server to update A RR in DNS as well as PTR
- O: 1 => Server indicates to client that DNS has been updated regardless
- E: 1 => Name data is DNS format, i.e. <4>host<6>domain<4>com<0> not "host.domain.com"
- N: 1 => Client requests Server to not update DNS
- */
- client_config.fqdn[OPT_LEN + 1] = 0x1;
- client_config.fqdn[OPT_LEN + 2] = 0;
- client_config.fqdn[OPT_LEN + 3] = 0;
- strncpy(client_config.fqdn + 5, optarg, len);
- break;
- case 'i':
- client_config.interface = optarg;
- break;
- case 'n':
- client_config.abort_if_no_lease = 1;
- break;
- case 'p':
- client_config.pidfile = optarg;
- break;
- case 'q':
- client_config.quit_after_lease = 1;
- break;
- case 'r':
- requested_ip = inet_addr(optarg);
- break;
- case 's':
- client_config.script = optarg;
- break;
- case 'v':
- printf("udhcpcd, version %s\n\n", VERSION);
- return 0;
- break;
- default:
- show_usage();
- }
+ enum {
+ OPT_c = 1 << 0,
+ OPT_C = 1 << 1,
+ OPT_V = 1 << 2,
+ OPT_f = 1 << 3,
+ OPT_b = 1 << 4,
+ OPT_H = 1 << 5,
+ OPT_h = 1 << 6,
+ OPT_F = 1 << 7,
+ OPT_i = 1 << 8,
+ OPT_n = 1 << 9,
+ OPT_p = 1 << 10,
+ OPT_q = 1 << 11,
+ OPT_R = 1 << 12,
+ OPT_r = 1 << 13,
+ OPT_s = 1 << 14,
+ OPT_T = 1 << 15,
+ OPT_t = 1 << 16,
+ OPT_v = 1 << 17,
+ OPT_S = 1 << 18,
+ };
+#if ENABLE_GETOPT_LONG
+ static const char udhcpc_longopts[] ALIGN1 =
+ "clientid\0" Required_argument "c"
+ "clientid-none\0" No_argument "C"
+ "vendorclass\0" Required_argument "V"
+ "foreground\0" No_argument "f"
+ "background\0" No_argument "b"
+ "hostname\0" Required_argument "H"
+ "hostname\0" Required_argument "h"
+ "fqdn\0" Required_argument "F"
+ "interface\0" Required_argument "i"
+ "now\0" No_argument "n"
+ "pidfile\0" Required_argument "p"
+ "quit\0" No_argument "q"
+ "release\0" No_argument "R"
+ "request\0" Required_argument "r"
+ "script\0" Required_argument "s"
+ "timeout\0" Required_argument "T"
+ "version\0" No_argument "v"
+ "retries\0" Required_argument "t"
+ "syslog\0" No_argument "S"
+ ;
+#endif
+ /* Default options. */
+ client_config.interface = "eth0";
+ client_config.script = DEFAULT_SCRIPT;
+ client_config.retries = 3;
+ client_config.timeout = 3;
+
+ /* Parse command line */
+ opt_complementary = "c--C:C--c" // mutually exclusive
+ ":hH:Hh"; // -h and -H are the same
+#if ENABLE_GETOPT_LONG
+ applet_long_options = udhcpc_longopts;
+#endif
+ opt = getopt32(argv, "c:CV:fbH:h:F:i:np:qRr:s:T:t:vS",
+ &str_c, &str_V, &str_h, &str_h, &str_F,
+ &client_config.interface, &client_config.pidfile, &str_r,
+ &client_config.script, &str_T, &str_t
+ );
+
+ if (opt & OPT_c)
+ client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, str_c, 0);
+ //if (opt & OPT_C)
+ if (opt & OPT_V)
+ client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, str_V, 0);
+ if (opt & OPT_f)
+ client_config.foreground = 1;
+ if (opt & OPT_b)
+ client_config.background_if_no_lease = 1;
+ if (opt & OPT_h)
+ client_config.hostname = alloc_dhcp_option(DHCP_HOST_NAME, str_h, 0);
+ if (opt & OPT_F) {
+ client_config.fqdn = alloc_dhcp_option(DHCP_FQDN, str_F, 3);
+ /* Flags: 0000NEOS
+ S: 1 => Client requests Server to update A RR in DNS as well as PTR
+ O: 1 => Server indicates to client that DNS has been updated regardless
+ E: 1 => Name data is DNS format, i.e. <4>host<6>domain<4>com<0> not "host.domain.com"
+ N: 1 => Client requests Server to not update DNS
+ */
+ client_config.fqdn[OPT_DATA + 0] = 0x1;
+ /* client_config.fqdn[OPT_DATA + 1] = 0; - redundant */
+ /* client_config.fqdn[OPT_DATA + 2] = 0; - redundant */
+ }
+ // if (opt & OPT_i) client_config.interface = ...
+ if (opt & OPT_n)
+ client_config.abort_if_no_lease = 1;
+ // if (opt & OPT_p) client_config.pidfile = ...
+ if (opt & OPT_q)
+ client_config.quit_after_lease = 1;
+ if (opt & OPT_R)
+ client_config.release_on_quit = 1;
+ if (opt & OPT_r)
+ requested_ip = inet_addr(str_r);
+ // if (opt & OPT_s) client_config.script = ...
+ if (opt & OPT_T)
+ client_config.timeout = xatoi_u(str_T);
+ if (opt & OPT_t)
+ client_config.retries = xatoi_u(str_t);
+ if (opt & OPT_v) {
+ printf("version %s\n", BB_VER);
+ return 0;