00001
00002
00003
00004 #define nil ((void*)0)
00005 #include <stddef.h>
00006 #include <string.h>
00007 #include <sys/types.h>
00008 #include <net/hton.h>
00009 #include <net/gen/in.h>
00010 #include <net/gen/dhcp.h>
00011
00012 #define arraysize(a) (sizeof(a) / sizeof((a)[0]))
00013 #define arraylimit(a) ((a) + arraysize(a))
00014
00015 void dhcp_init(dhcp_t *dp)
00016 {
00017
00018 memset(dp, 0, offsetof(dhcp_t, magic));
00019 dp->magic= DHCP_MAGIC;
00020 memset(dp->options, 255, sizeof(dp->options));
00021 }
00022
00023 int dhcp_settag(dhcp_t *dp, int tag, void *data, size_t len)
00024 {
00025
00026
00027
00028
00029 u8_t *p;
00030 int n;
00031
00032 if (tag <= 0 || tag >= 255) return 0;
00033
00034 for (p= dp->options; p < arraylimit(dp->options) && *p != 255; p += n) {
00035 n= 1 + 1 + p[1];
00036 if (*p == tag) {
00037
00038 memmove(p, p + n, arraylimit(dp->options) - (p + n));
00039 memset(arraylimit(dp->options) - n, 255, n);
00040 n= 0;
00041 }
00042 }
00043
00044
00045 if (len == 0) {
00046
00047 } else
00048 if (p + 1 + 1 + len <= arraylimit(dp->options)) {
00049 *p++ = tag;
00050 *p++ = len;
00051 memcpy(p, data, len);
00052 } else {
00053
00054 return 0;
00055 }
00056 return 1;
00057 }