-/*
- * Module: dhcpoa
- *
- * Purpose:
- * Types and functions to create new dhcp option areas.
- */
-
-/*
- * Function: dhcpoa_{init_common, init_no_end, init}
- *
- * Purpose:
- * Initialize an option area structure so that it can be used
- * in calling the dhcpoa_* routines.
- */
-static void
-dhcpoa_init_common(dhcpoa_t * oa_p, void * buffer, int size, int reserve)
-{
- bzero(oa_p, sizeof(*oa_p));
- oa_p->oa_buffer = buffer;
- oa_p->oa_size = size;
- oa_p->oa_reserve = reserve;
-}
-
-void
-dhcpoa_init_no_end(dhcpoa_t * oa_p, void * buffer, int size)
-{
- dhcpoa_init_common(oa_p, buffer, size, 0);
- return;
-}
-
-int
-dhcpoa_size(dhcpoa_t * oa_p)
-{
- return (oa_p->oa_size);
-}
-
-void
-dhcpoa_init(dhcpoa_t * oa_p, void * buffer, int size)
-{
- /* initialize the area, reserve space for the end tag */
- dhcpoa_init_common(oa_p, buffer, size, 1);
- return;
-}
-/*
- * Function: dhcpoa_add
- *
- * Purpose:
- * Add an option to the option area.
- */
-dhcpoa_ret_t
-dhcpoa_add(dhcpoa_t * oa_p, dhcptag_t tag, int len, const void * option)
-{
- if (len > DHCP_OPTION_SIZE_MAX) {
- dprintf(("tag %d option %d > %d\n", tag, len, DHCP_OPTION_SIZE_MAX));
- return (dhcpoa_failed_e);
- }
-
- if (oa_p->oa_end_tag) {
- dprintf(("attempt to add data after end tag\n"));
- return (dhcpoa_failed_e);
- }
-
- switch (tag) {
- case dhcptag_end_e:
- if ((oa_p->oa_offset + 1) > oa_p->oa_size) {
- /* this can't happen since we're careful to leave space */
- dprintf(("can't add end tag %d > %d\n",
- oa_p->oa_offset + oa_p->oa_reserve, oa_p->oa_size));
- return (dhcpoa_failed_e);
- }
- ((uint8_t *)oa_p->oa_buffer)[oa_p->oa_offset + DHCP_TAG_OFFSET] = tag;
- oa_p->oa_offset++;
- oa_p->oa_end_tag = 1;
- break;
-
- case dhcptag_pad_e:
- /* 1 for pad tag */
- if ((oa_p->oa_offset + oa_p->oa_reserve + 1) > oa_p->oa_size) {
- dprintf(("can't add pad tag %d > %d\n",
- oa_p->oa_offset + oa_p->oa_reserve + 1, oa_p->oa_size));
- return (dhcpoa_full_e);
- }
- ((uint8_t *)oa_p->oa_buffer)[oa_p->oa_offset + DHCP_TAG_OFFSET] = tag;
- oa_p->oa_offset++;
- break;
-
- default:
- /* 2 for tag/len */
- if ((oa_p->oa_offset + len + 2 + oa_p->oa_reserve) > oa_p->oa_size) {
- dprintf(("can't add tag %d (%d > %d)\n", tag,
- oa_p->oa_offset + len + 2 + oa_p->oa_reserve,
- oa_p->oa_size));
- return (dhcpoa_full_e);
- }
- ((uint8_t *)oa_p->oa_buffer)[oa_p->oa_offset + DHCP_TAG_OFFSET] = tag;
- ((uint8_t *)oa_p->oa_buffer)[oa_p->oa_offset + DHCP_LEN_OFFSET] = (uint8_t)len;
- if (len) {
- memcpy(oa_p->oa_buffer + (DHCP_OPTION_OFFSET + oa_p->oa_offset),
- option, len);
- }
- oa_p->oa_offset += len + DHCP_OPTION_OFFSET;
- break;
- }
- oa_p->oa_option_count++;
- return (dhcpoa_success_e);
-}
-
-/*
- * Function: dhcpoa_add_dhcpmsg
- *
- * Purpose:
- * Add a dhcp message option to the option area.
- */
-dhcpoa_ret_t
-dhcpoa_add_dhcpmsg(dhcpoa_t * oa_p, dhcp_msgtype_t msgtype)
-{
- return (dhcpoa_add(oa_p, dhcptag_dhcp_message_type_e,
- sizeof(msgtype), &msgtype));
-}
-
-int
-dhcpoa_used(dhcpoa_t * oa_p)
-{
- return (oa_p->oa_offset);
-}
-
-int
-dhcpoa_freespace(dhcpoa_t * oa_p)
-{
- int freespace;
-
- freespace = oa_p->oa_size - oa_p->oa_offset - oa_p->oa_reserve;
- if (freespace < 0) {
- freespace = 0;
- }
- return (freespace);
-}
-
-int
-dhcpoa_count(dhcpoa_t * oa_p)
-{
- return (oa_p->oa_option_count);
-}
-
-void *
-dhcpoa_buffer(dhcpoa_t * oa_p)
-{
- return (oa_p->oa_buffer);
-}
-
-