1 | /* dhcp_init(), dhcp_settag() Author: Kees J. Bot
|
---|
2 | * 1 Dec 2000
|
---|
3 | */
|
---|
4 | #define nil ((void*)0)
|
---|
5 | #include <stddef.h>
|
---|
6 | #include <string.h>
|
---|
7 | #include <sys/types.h>
|
---|
8 | #include <net/hton.h>
|
---|
9 | #include <net/gen/in.h>
|
---|
10 | #include <net/gen/dhcp.h>
|
---|
11 |
|
---|
12 | #define arraysize(a) (sizeof(a) / sizeof((a)[0]))
|
---|
13 | #define arraylimit(a) ((a) + arraysize(a))
|
---|
14 |
|
---|
15 | void dhcp_init(dhcp_t *dp)
|
---|
16 | {
|
---|
17 | /* Initialize a DHCP packet. */
|
---|
18 | memset(dp, 0, offsetof(dhcp_t, magic));
|
---|
19 | dp->magic= DHCP_MAGIC;
|
---|
20 | memset(dp->options, 255, sizeof(dp->options));
|
---|
21 | }
|
---|
22 |
|
---|
23 | int dhcp_settag(dhcp_t *dp, int tag, void *data, size_t len)
|
---|
24 | {
|
---|
25 | /* Add a tag to a DHCP packet. No padding. Only do the options field.
|
---|
26 | * (This is Minix, we don't need megabytes of silly bits of data.)
|
---|
27 | * The length may be zero to delete a tag.
|
---|
28 | */
|
---|
29 | u8_t *p;
|
---|
30 | int n;
|
---|
31 |
|
---|
32 | if (tag <= 0 || tag >= 255) return 0;
|
---|
33 |
|
---|
34 | for (p= dp->options; p < arraylimit(dp->options) && *p != 255; p += n) {
|
---|
35 | n= 1 + 1 + p[1];
|
---|
36 | if (*p == tag) {
|
---|
37 | /* The tag is already there, remove it so it gets replaced. */
|
---|
38 | memmove(p, p + n, arraylimit(dp->options) - (p + n));
|
---|
39 | memset(arraylimit(dp->options) - n, 255, n);
|
---|
40 | n= 0;
|
---|
41 | }
|
---|
42 | }
|
---|
43 |
|
---|
44 | /* Add tag. */
|
---|
45 | if (len == 0) {
|
---|
46 | /* We're merely deleting a tag. */
|
---|
47 | } else
|
---|
48 | if (p + 1 + 1 + len <= arraylimit(dp->options)) {
|
---|
49 | *p++ = tag;
|
---|
50 | *p++ = len;
|
---|
51 | memcpy(p, data, len);
|
---|
52 | } else {
|
---|
53 | /* Oops, it didn't fit? Is this really Minix??? */
|
---|
54 | return 0;
|
---|
55 | }
|
---|
56 | return 1;
|
---|
57 | }
|
---|