- if (list->array == NULL) {
- if (list->size == 0)
- list->size = PTRLIST_NUMBER;
- list->count = 0;
- list->array = my_malloc(sizeof(*list->array) * list->size);
- }
- else if (list->size == list->count) {
- dprintf(("doubling %d to %d\n", list->size, list->size * 2));
- list->array = my_realloc(list->array,
- sizeof(*list->array) * list->size,
- sizeof(*list->array) * list->size * 2);
- list->size *= 2;
- }
- if (list->array == NULL)
- return (FALSE);
- return (TRUE);
+ if (list->array == NULL) {
+ if (list->size == 0) {
+ list->size = PTRLIST_NUMBER;
+ }
+ list->count = 0;
+ list->array = my_malloc(sizeof(*list->array) * list->size);
+ } else if (list->size == list->count) {
+ dprintf(("doubling %d to %d\n", list->size, list->size * 2));
+ list->array = my_realloc(list->array,
+ sizeof(*list->array) * list->size,
+ sizeof(*list->array) * list->size * 2);
+ list->size *= 2;
+ }
+ if (list->array == NULL) {
+ return FALSE;
+ }
+ return TRUE;
- if (extra->count == 0)
- return (TRUE);
-
- if ((extra->count + list->count) > list->size) {
- int old_size = list->size;
-
- list->size = extra->count + list->count;
- if (list->array == NULL)
- list->array = my_malloc(sizeof(*list->array) * list->size);
- else
- list->array = my_realloc(list->array, old_size,
- sizeof(*list->array) * list->size);
- }
- if (list->array == NULL)
- return (FALSE);
- bcopy(extra->array, list->array + list->count,
- extra->count * sizeof(*list->array));
- list->count += extra->count;
- return (TRUE);
+ if (extra->count == 0) {
+ return TRUE;
+ }
+
+ if ((extra->count + list->count) > list->size) {
+ int old_size = list->size;
+
+ list->size = extra->count + list->count;
+ if (list->array == NULL) {
+ list->array = my_malloc(sizeof(*list->array) * list->size);
+ } else {
+ list->array = my_realloc(list->array, old_size,
+ sizeof(*list->array) * list->size);
+ }
+ }
+ if (list->array == NULL) {
+ return FALSE;
+ }
+ bcopy(extra->array, list->array + list->count,
+ extra->count * sizeof(*list->array));
+ list->count += extra->count;
+ return TRUE;
- int len;
- const uint8_t * scan;
- uint8_t tag;
-
- dhcpol_init(list);
-
- len = length;
- tag = dhcptag_pad_e;
- for (scan = (const uint8_t *)buffer;
- tag != dhcptag_end_e && len > DHCP_TAG_OFFSET; ) {
-
- tag = scan[DHCP_TAG_OFFSET];
-
- switch (tag) {
- case dhcptag_end_e:
- /* remember that it was terminated */
- dhcpol_add(list, scan);
- scan++;
- len--;
- break;
- case dhcptag_pad_e: /* ignore pad */
- scan++;
- len--;
- break;
- default:
- if (len > DHCP_LEN_OFFSET) {
- uint8_t option_len;
-
- option_len = scan[DHCP_LEN_OFFSET];
- dhcpol_add(list, scan);
- len -= (option_len + DHCP_OPTION_OFFSET);
- scan += (option_len + DHCP_OPTION_OFFSET);
- }
- else {
- len = -1;
- }
- break;
+ int len;
+ const uint8_t * scan;
+ uint8_t tag;
+
+ dhcpol_init(list);
+
+ len = length;
+ tag = dhcptag_pad_e;
+ for (scan = (const uint8_t *)buffer;
+ tag != dhcptag_end_e && len > DHCP_TAG_OFFSET;) {
+ tag = scan[DHCP_TAG_OFFSET];
+
+ switch (tag) {
+ case dhcptag_end_e:
+ /* remember that it was terminated */
+ dhcpol_add(list, scan);
+ scan++;
+ len--;
+ break;
+ case dhcptag_pad_e: /* ignore pad */
+ scan++;
+ len--;
+ break;
+ default:
+ if (len > DHCP_LEN_OFFSET) {
+ uint8_t option_len;
+
+ option_len = scan[DHCP_LEN_OFFSET];
+ dhcpol_add(list, scan);
+ len -= (option_len + DHCP_OPTION_OFFSET);
+ scan += (option_len + DHCP_OPTION_OFFSET);
+ } else {
+ len = -1;
+ }
+ break;
+ }
+ }
+ if (len < 0) {
+ /* ran off the end */
+ dprintf(("dhcp_options: parse failed near tag %d\n", tag));
+ dhcpol_free(list);
+ return FALSE;
- int i = 0;
-
- if (tag == dhcptag_end_e || tag == dhcptag_pad_e)
- return (NULL);
-
- if (start)
- i = *start;
-
- for (; i < dhcpol_count(list); i++) {
- const uint8_t * option = dhcpol_element(list, i);
-
- if (option[DHCP_TAG_OFFSET] == tag) {
- if (len_p)
- *len_p = option[DHCP_LEN_OFFSET];
- if (start)
- *start = i + 1;
- return (option + DHCP_OPTION_OFFSET);
+ int i = 0;
+
+ if (tag == dhcptag_end_e || tag == dhcptag_pad_e) {
+ return NULL;
+ }
+
+ if (start) {
+ i = *start;
+ }
+
+ for (; i < dhcpol_count(list); i++) {
+ const uint8_t * option = dhcpol_element(list, i);
+
+ if (option[DHCP_TAG_OFFSET] == tag) {
+ if (len_p) {
+ *len_p = option[DHCP_LEN_OFFSET];
+ }
+ if (start) {
+ *start = i + 1;
+ }
+ return option + DHCP_OPTION_OFFSET;
+ }
- char rfc_magic[4] = RFC_OPTIONS_MAGIC;
-
- dhcpol_init(options); /* make sure it's empty */
-
- if (len < (sizeof(*pkt) + RFC_MAGIC_SIZE)) {
- dprintf(("dhcp_options: packet is too short: %d < %d\n",
- len, (int)sizeof(*pkt) + RFC_MAGIC_SIZE));
- return (FALSE);
- }
- if (bcmp(pkt->dp_options, rfc_magic, RFC_MAGIC_SIZE)) {
- dprintf(("dhcp_options: missing magic number\n"));
- return (FALSE);
- }
- if (dhcpol_parse_buffer(options, pkt->dp_options + RFC_MAGIC_SIZE,
- len - sizeof(*pkt) - RFC_MAGIC_SIZE) == FALSE)
- return (FALSE);
- { /* get overloaded options */
- const uint8_t * overload;
- int overload_len;
-
- overload = dhcpol_find(options, dhcptag_option_overload_e,
- &overload_len, NULL);
- if (overload && overload_len == 1) { /* has overloaded options */
- dhcpol_t extra;
-
- dhcpol_init(&extra);
- if (*overload == DHCP_OVERLOAD_FILE
- || *overload == DHCP_OVERLOAD_BOTH) {
- if (dhcpol_parse_buffer(&extra, pkt->dp_file,
- sizeof(pkt->dp_file))) {
- dhcpol_concat(options, &extra);
- dhcpol_free(&extra);
- }
- }
- if (*overload == DHCP_OVERLOAD_SNAME
- || *overload == DHCP_OVERLOAD_BOTH) {
- if (dhcpol_parse_buffer(&extra, pkt->dp_sname,
- sizeof(pkt->dp_sname))) {
- dhcpol_concat(options, &extra);
- dhcpol_free(&extra);
+ char rfc_magic[4] = RFC_OPTIONS_MAGIC;
+
+ dhcpol_init(options); /* make sure it's empty */
+
+ if (len < (sizeof(*pkt) + RFC_MAGIC_SIZE)) {
+ dprintf(("dhcp_options: packet is too short: %d < %d\n",
+ len, (int)sizeof(*pkt) + RFC_MAGIC_SIZE));
+ return FALSE;
+ }
+ if (bcmp(pkt->dp_options, rfc_magic, RFC_MAGIC_SIZE)) {
+ dprintf(("dhcp_options: missing magic number\n"));
+ return FALSE;
+ }
+ if (dhcpol_parse_buffer(options, pkt->dp_options + RFC_MAGIC_SIZE,
+ len - sizeof(*pkt) - RFC_MAGIC_SIZE) == FALSE) {
+ return FALSE;
+ }
+ { /* get overloaded options */
+ const uint8_t * overload;
+ int overload_len;
+
+ overload = dhcpol_find(options, dhcptag_option_overload_e,
+ &overload_len, NULL);
+ if (overload && overload_len == 1) { /* has overloaded options */
+ dhcpol_t extra;
+
+ dhcpol_init(&extra);
+ if (*overload == DHCP_OVERLOAD_FILE
+ || *overload == DHCP_OVERLOAD_BOTH) {
+ if (dhcpol_parse_buffer(&extra, pkt->dp_file,
+ sizeof(pkt->dp_file))) {
+ dhcpol_concat(options, &extra);
+ dhcpol_free(&extra);
+ }
+ }
+ if (*overload == DHCP_OVERLOAD_SNAME
+ || *overload == DHCP_OVERLOAD_BOTH) {
+ if (dhcpol_parse_buffer(&extra, pkt->dp_sname,
+ sizeof(pkt->dp_sname))) {
+ dhcpol_concat(options, &extra);
+ dhcpol_free(&extra);
+ }
+ }
- 99, 130, 83, 99,
- 1, 4, 255, 255, 252, 0,
- 3, 4, 17, 202, 40, 1,
- 43, 6, 1, 4, 1, 2, 3, 4,
- 43, 6, 1, 4, 1, 2, 3, 4,
- 255,
+ 99, 130, 83, 99,
+ 1, 4, 255, 255, 252, 0,
+ 3, 4, 17, 202, 40, 1,
+ 43, 6, 1, 4, 1, 2, 3, 4,
+ 43, 6, 1, 4, 1, 2, 3, 4,
+ 255,
- 0x63, 0x82, 0x53, 0x63, 0x35, 0x01, 0x05, 0x36,
- 0x04, 0xc0, 0xa8, 0x01, 0x01, 0x33, 0x04, 0x80,
- 0x00, 0x80, 0x00, 0x01, 0x04, 0xff, 0xff, 0xff,
- 0x00, 0x03, 0x04, 0xc0, 0xa8, 0x01, 0x01, 0x06,
- 0x0c, 0x18, 0x1a, 0xa3, 0x21, 0x18, 0x1a, 0xa3,
- 0x20, 0x18, 0x5e, 0xa3, 0x21, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
+ 0x63, 0x82, 0x53, 0x63, 0x35, 0x01, 0x05, 0x36,
+ 0x04, 0xc0, 0xa8, 0x01, 0x01, 0x33, 0x04, 0x80,
+ 0x00, 0x80, 0x00, 0x01, 0x04, 0xff, 0xff, 0xff,
+ 0x00, 0x03, 0x04, 0xc0, 0xa8, 0x01, 0x01, 0x06,
+ 0x0c, 0x18, 0x1a, 0xa3, 0x21, 0x18, 0x1a, 0xa3,
+ 0x20, 0x18, 0x5e, 0xa3, 0x21, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- { "empty", test_empty, sizeof(test_empty), TRUE },
- { "simple", test_simple, sizeof(test_simple), TRUE },
- { "vendor", test_vendor, sizeof(test_vendor), TRUE },
- { "no_end", test_no_end, sizeof(test_no_end), TRUE },
- { "no magic", test_no_magic, sizeof(test_no_magic), FALSE },
- { "short", test_short, sizeof(test_short), FALSE },
- { NULL, NULL, 0, FALSE },
+ { .name = "empty", .data = test_empty, .len = sizeof(test_empty), .result = TRUE },
+ { .name = "simple", .data = test_simple, .len = sizeof(test_simple), .result = TRUE },
+ { .name = "vendor", .data = test_vendor, .len = sizeof(test_vendor), .result = TRUE },
+ { .name = "no_end", .data = test_no_end, .len = sizeof(test_no_end), .result = TRUE },
+ { .name = "no magic", .data = test_no_magic, .len = sizeof(test_no_magic), .result = FALSE },
+ { .name = "short", .data = test_short, .len = sizeof(test_short), .result = FALSE },
+ { .name = NULL, .data = NULL, .len = 0, .result = FALSE },
- int i;
- dhcpol_t options;
- struct dhcp * pkt = (struct dhcp *)buf;
-
- dhcpol_init(&options);
-
- for (i = 0; tests[i].name; i++) {
- printf("\nTest %d: ", i);
- bcopy(tests[i].data, pkt->dp_options, tests[i].len);
- if (dhcpol_parse_packet(&options, pkt,
- sizeof(*pkt) + tests[i].len)
- != tests[i].result) {
- printf("test '%s' FAILED\n", tests[i].name);
- }
- else {
- printf("test '%s' PASSED\n", tests[i].name);
+ int i;
+ dhcpol_t options;
+ struct dhcp * pkt = (struct dhcp *)buf;
+
+ dhcpol_init(&options);
+
+ for (i = 0; tests[i].name; i++) {
+ printf("\nTest %d: ", i);
+ bcopy(tests[i].data, pkt->dp_options, tests[i].len);
+ if (dhcpol_parse_packet(&options, pkt,
+ sizeof(*pkt) + tests[i].len)
+ != tests[i].result) {
+ printf("test '%s' FAILED\n", tests[i].name);
+ } else {
+ printf("test '%s' PASSED\n", tests[i].name);
+ }
+ dhcpol_free(&options);