summaryrefslogtreecommitdiff
path: root/test/regression/dhcp_test/netx_dhcp_clone_function.c
blob: 061cfac0bc365eb51b0506c2076650513f0e91ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/***************************************************************************/
/* Copyright (c) 2024 Microsoft Corporation                                */
/* Copyright (c) 2026 Eclipse ThreadX contributors                         */
/*                                                                         */
/* This program and the accompanying materials are made available under    */
/* the terms of the MIT License which is available at                      */
/* https://opensource.org/licenses/MIT.                                    */
/*                                                                         */
/* SPDX-License-Identifier: MIT                                            */
/***************************************************************************/

                                 
#include   "netx_dhcp_clone_function.h"           
#include   "nxd_dhcp_client.h"
#include   "nxd_dhcp_server.h"
                                                        

UINT  dhcp_get_option_value(UCHAR *bootp_message, UINT option, ULONG *value, UINT length)
{

UCHAR *data;


    /* Find the option.  */
    if ((option != NX_DHCP_OPTION_PAD) && (option != NX_DHCP_OPTION_END))
    {

        /* Search the buffer for the option.  */
        data =  dhcp_search_buffer(bootp_message, option, length);

        /* Check to see if the option was found.  */
        if (data != NX_NULL)
        {

            /* Check for the proper size.  */
            if (*data > 4)
            {

                /* Check for the gateway option.  */
                if (option == NX_DHCP_OPTION_GATEWAYS)
                {

                    /* Pickup the first gateway address.  */
                    *value =  dhcp_get_data(data + 1, 4);

                    /* For now, just disregard any additional gateway addresses.  */
                    return(NX_SUCCESS);
                }
                else
                {

                    /* Invalid size, return error.  */
                    return(NX_SIZE_ERROR);
                }
            }
            else
            {

                /* Get the actual value.  */
                *value = dhcp_get_data(data + 1, *data);
                return(NX_SUCCESS);  
            }
        }
    }

    /* Return an error if not found.  */
    return(NX_OPTION_ERROR);
}
UCHAR  *dhcp_search_buffer(UCHAR *bootp_message, UINT option, UINT length)
{

UCHAR   *data;
UINT    i;


    /* Setup buffer pointer.  */
    data = &bootp_message[NX_BOOTP_OFFSET_OPTIONS];
    i = NX_BOOTP_OFFSET_OPTIONS;

    /* Search as long as there are valid options.   */
    while (i < length)
    {

        /* Simply skip any padding */
        if (*data == NX_DHCP_OPTION_PAD)
        {

            data++;
            i++;
        }

        /* On a match, return a pointer to the size.  */
        else if (*data == option)
        {

            /* Return a pointer to the option size byte.  */
            return(data + 1);
        }

        /* Otherwise skip the option by adding the size to the pointer.  */
        else
        {

        UINT size = *(++data);

            /* skip the data plus the size byte */
            data += size + 1;
            i += size + 1;
        }
    }

    /* Return NULL to indicate the option was not found.  */
    return(NX_NULL);
}
ULONG  dhcp_get_data(UCHAR *data, UINT size)
{

ULONG   value = 0;

   
    /* Process the data retrieval request.  */
    while (size-- > 0)
    {

        /* Build return value.  */
        value = (value << 8) | *data++;
    }

    /* Return value.  */
    return(value);
}