add "wpad" DHCP option. Spotted some optimization opportunities: -80 bytes
authorDenis Vlasenko <vda.linux@googlemail.com>
Thu, 23 Nov 2006 12:57:49 +0000 (12:57 -0000)
committerDenis Vlasenko <vda.linux@googlemail.com>
Thu, 23 Nov 2006 12:57:49 +0000 (12:57 -0000)
networking/udhcp/files.c
networking/udhcp/options.c
networking/udhcp/options.h
networking/udhcp/script.c

index 2c2c5422c3e44dd8c071db8f4f2bc678ef8aef7c..829d7e9606337f72299c0e0ae9746c807f4fd278 100644 (file)
@@ -95,7 +95,8 @@ struct option_set *find_option(struct option_set *opt_list, char code)
 
 
 /* add an option to the opt_list */
-static void attach_option(struct option_set **opt_list, struct dhcp_option *option, char *buffer, int length)
+static void attach_option(struct option_set **opt_list,
+               const struct dhcp_option *option, char *buffer, int length)
 {
        struct option_set *existing, *new, **curr;
 
@@ -135,7 +136,7 @@ static int read_opt(const char *const_line, void *arg)
 {
        struct option_set **opt_list = arg;
        char *opt, *val, *endptr;
-       struct dhcp_option *option;
+       const struct dhcp_option *option;
        int retval = 0, length;
        char buffer[8];
        char *line;
@@ -144,16 +145,21 @@ static int read_opt(const char *const_line, void *arg)
 
        /* Cheat, the only const line we'll actually get is "" */
        line = (char *) const_line;
-       if (!(opt = strtok(line, " \t="))) return 0;
+       opt = strtok(line, " \t=");
+       if (!opt) return 0;
 
-       for (option = dhcp_options; option->code; option++)
+       option = dhcp_options;
+       while (1) {
+               if (!option->code)
+                       return 0;
                if (!strcasecmp(option->name, opt))
                        break;
-
-       if (!option->code) return 0;
+                option++;
+       }
 
        do {
-               if (!(val = strtok(NULL, ", \t"))) break;
+               val = strtok(NULL, ", \t");
+               if (!val) break;
                length = option_lengths[option->flags & TYPE_MASK];
                retval = 0;
                opt = buffer; /* new meaning for variable opt */
index 9fcbc84748538ef5f3b171d40cfa1d975940bfc8..4a46da57928c68a1a43acfac88a7caab1bc44c72 100644 (file)
 
 
 /* supported options are easily added here */
-struct dhcp_option dhcp_options[] = {
-       /* name[10]     flags                                   code */
-       {"subnet",      OPTION_IP | OPTION_REQ,                 0x01},
-       {"timezone",    OPTION_S32,                             0x02},
-       {"router",      OPTION_IP | OPTION_LIST | OPTION_REQ,   0x03},
-       {"timesvr",     OPTION_IP | OPTION_LIST,                0x04},
-       {"namesvr",     OPTION_IP | OPTION_LIST,                0x05},
-       {"dns",         OPTION_IP | OPTION_LIST | OPTION_REQ,   0x06},
-       {"logsvr",      OPTION_IP | OPTION_LIST,                0x07},
-       {"cookiesvr",   OPTION_IP | OPTION_LIST,                0x08},
-       {"lprsvr",      OPTION_IP | OPTION_LIST,                0x09},
-       {"hostname",    OPTION_STRING | OPTION_REQ,             0x0c},
-       {"bootsize",    OPTION_U16,                             0x0d},
-       {"domain",      OPTION_STRING | OPTION_REQ,             0x0f},
-       {"swapsvr",     OPTION_IP,                              0x10},
-       {"rootpath",    OPTION_STRING,                          0x11},
-       {"ipttl",       OPTION_U8,                              0x17},
-       {"mtu",         OPTION_U16,                             0x1a},
-       {"broadcast",   OPTION_IP | OPTION_REQ,                 0x1c},
-       {"nisdomain",   OPTION_STRING | OPTION_REQ,             0x28},
-       {"nissrv",      OPTION_IP | OPTION_LIST | OPTION_REQ,   0x29},
-       {"ntpsrv",      OPTION_IP | OPTION_LIST | OPTION_REQ,   0x2a},
-       {"wins",        OPTION_IP | OPTION_LIST,                0x2c},
-       {"requestip",   OPTION_IP,                              0x32},
-       {"lease",       OPTION_U32,                             0x33},
-       {"dhcptype",    OPTION_U8,                              0x35},
-       {"serverid",    OPTION_IP,                              0x36},
-       {"message",     OPTION_STRING,                          0x38},
+const struct dhcp_option dhcp_options[] = {
+       /* name[10]     flags                                   code */
+       {"subnet",      OPTION_IP | OPTION_REQ,                 0x01},
+       {"timezone",    OPTION_S32,                             0x02},
+       {"router",      OPTION_IP | OPTION_LIST | OPTION_REQ,   0x03},
+       {"timesvr",     OPTION_IP | OPTION_LIST,                0x04},
+       {"namesvr",     OPTION_IP | OPTION_LIST,                0x05},
+       {"dns",         OPTION_IP | OPTION_LIST | OPTION_REQ,   0x06},
+       {"logsvr",      OPTION_IP | OPTION_LIST,                0x07},
+       {"cookiesvr",   OPTION_IP | OPTION_LIST,                0x08},
+       {"lprsvr",      OPTION_IP | OPTION_LIST,                0x09},
+       {"hostname",    OPTION_STRING | OPTION_REQ,             0x0c},
+       {"bootsize",    OPTION_U16,                             0x0d},
+       {"domain",      OPTION_STRING | OPTION_REQ,             0x0f},
+       {"swapsvr",     OPTION_IP,                              0x10},
+       {"rootpath",    OPTION_STRING,                          0x11},
+       {"ipttl",       OPTION_U8,                              0x17},
+       {"mtu",         OPTION_U16,                             0x1a},
+       {"broadcast",   OPTION_IP | OPTION_REQ,                 0x1c},
+       {"nisdomain",   OPTION_STRING | OPTION_REQ,             0x28},
+       {"nissrv",      OPTION_IP | OPTION_LIST | OPTION_REQ,   0x29},
+       {"ntpsrv",      OPTION_IP | OPTION_LIST | OPTION_REQ,   0x2a},
+       {"wins",        OPTION_IP | OPTION_LIST,                0x2c},
+       {"requestip",   OPTION_IP,                              0x32},
+       {"lease",       OPTION_U32,                             0x33},
+       {"dhcptype",    OPTION_U8,                              0x35},
+       {"serverid",    OPTION_IP,                              0x36},
+       {"message",     OPTION_STRING,                          0x38},
        {"vendorclass", OPTION_STRING,                          0x3C},
        {"clientid",    OPTION_STRING,                          0x3D},
-       {"tftp",        OPTION_STRING,                          0x42},
-       {"bootfile",    OPTION_STRING,                          0x43},
+       {"tftp",        OPTION_STRING,                          0x42},
+       {"bootfile",    OPTION_STRING,                          0x43},
        {"userclass",   OPTION_STRING,                          0x4D},
-       {"",            0x00,                           0x00}
+       /* MSIE's "Web Proxy Autodiscovery Protocol" support */
+       {"wpad",        OPTION_STRING,                          0xfc},
+       {"",            0x00,                                   0x00}
 };
 
 /* Lengths of the different option types */
-int option_lengths[] = {
-       [OPTION_IP] =           4,
-       [OPTION_IP_PAIR] =      8,
-       [OPTION_BOOLEAN] =      1,
-       [OPTION_STRING] =       1,
-       [OPTION_U8] =           1,
-       [OPTION_U16] =          2,
-       [OPTION_S16] =          2,
-       [OPTION_U32] =          4,
-       [OPTION_S32] =          4
+const unsigned char option_lengths[] = {
+       [OPTION_IP] =      4,
+       [OPTION_IP_PAIR] = 8,
+       [OPTION_BOOLEAN] = 1,
+       [OPTION_STRING] =  1,
+       [OPTION_U8] =      1,
+       [OPTION_U16] =     2,
+       [OPTION_S16] =     2,
+       [OPTION_U32] =     4,
+       [OPTION_S32] =     4
 };
 
 
@@ -150,9 +152,9 @@ int add_option_string(uint8_t *optionptr, uint8_t *string)
 /* add a one to four byte option to a packet */
 int add_simple_option(uint8_t *optionptr, uint8_t code, uint32_t data)
 {
-       struct dhcp_option *dh;
+       const struct dhcp_option *dh;
 
-       for (dh=dhcp_options; dh->code; dh++) {
+       for (dh = dhcp_options; dh->code; dh++) {
                if (dh->code == code) {
                        uint8_t option[6], len;
 
index 1b179f17383f967763ae6f32410f000f0506a6e3..588504e5d84c736f6d0ac9cceee9209425a3f36f 100644 (file)
@@ -26,8 +26,8 @@ struct dhcp_option {
        uint8_t code;
 };
 
-extern struct dhcp_option dhcp_options[];
-extern int option_lengths[];
+extern const struct dhcp_option dhcp_options[];
+extern const unsigned char option_lengths[];
 
 uint8_t *get_option(struct dhcpMessage *packet, int code);
 int end_option(uint8_t *optionptr);
index 6e0ca885f142bdd1abcbb8ad77a1a25c737472d4..d44c80b9f64941fcdc0d5da9c94ff9e1cd31a26c 100644 (file)
@@ -54,7 +54,8 @@ static int mton(struct in_addr *mask)
 
 
 /* Fill dest with the text of option 'option'. */
-static void fill_options(char *dest, uint8_t *option, struct dhcp_option *type_p)
+static void fill_options(char *dest, uint8_t *option,
+                       const struct dhcp_option *type_p)
 {
        int type, optlen;
        uint16_t val_u16;
@@ -152,7 +153,8 @@ static char **fill_envp(struct dhcpMessage *packet)
 
 
        for (i = 0; dhcp_options[i].code; i++) {
-               if (!(temp = get_option(packet, dhcp_options[i].code)))
+               temp = get_option(packet, dhcp_options[i].code);
+               if (temp)
                        continue;
                envp[j] = xmalloc(upper_length(temp[OPT_LEN - 2],
                        dhcp_options[i].flags & TYPE_MASK) + strlen(dhcp_options[i].name) + 2);