Repository navigation
Expand file tree
/
Copy pathdhcp_gettag.c
More file actions
executable file
·55 lines (50 loc) · 1.4 KB
/
Copy pathdhcp_gettag.c
File metadata and controls
executable file
·55 lines (50 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
<
754B
div data-line-number="11" class="react-line-number react-code-text" style="padding-right:16px">1112
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
/* dhcp_gettag() Author: Kees J. Bot
* 1 Dec 2000
*/
#define nil ((void*)0)
#include <stddef.h>
#include <string.h>
#include <sys/types.h>
#include <net/hton.h>
#include <net/gen/in.h>
#include <net/gen/dhcp.h>
#define arraysize(a) (sizeof(a) / sizeof((a)[0]))
int dhcp_gettag(dhcp_t *dp, int searchtag, u8_t **pdata, size_t *plen)
{
/* Find a tag in the options field, or possibly in the file or sname
* fields. Return true iff found, and return the data and/or length if
* their pointers are non-null.
*/
u8_t *p;
u8_t *optfield[3];
size_t optlen[3];
int i, tag, len;
/* The DHCP magic number must be correct, or no tags. */
if (dp->magic != DHCP_MAGIC) return 0;
optfield[0]= dp->options;
optlen[0]= arraysize(dp->options);
optfield[1]= dp->file;
optlen[1]= 0; /* Unknown if used for options yet. */
optfield[2]= dp->sname;
optlen[2]= 0;
for (i= 0; i < 3; i++) {
p= optfield[i];
while (p < optfield[i] + optlen[i]) {
tag= *p++;
if (tag == 255) break;
len= tag == 0 ? 0 : *p++;
if (tag == searchtag) {
if (pdata != nil) *pdata= p;
if (plen != nil) *plen= len;
return 1;
}
if (tag == DHCP_TAG_OVERLOAD) {
/* There are also options in the file or sname field. */
if (*p & 1) optlen[1]= arraysize(dp->file);
if (*p & 2) optlen[1]= arraysize(dp->sname);
}
p += len;
}
}
return 0;
}