. pci driver now returns devices, even when they have been pci_reserve()d
[minix3.git] / lib / ip / dhcp_gettag.c
blob3dcb6e827d12a589738177ffc8ee52509aa6b92e
1 /* dhcp_gettag() 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>
12 #define arraysize(a) (sizeof(a) / sizeof((a)[0]))
14 int dhcp_gettag(dhcp_t *dp, int searchtag, u8_t **pdata, size_t *plen)
16 /* Find a tag in the options field, or possibly in the file or sname
17 * fields. Return true iff found, and return the data and/or length if
18 * their pointers are non-null.
20 u8_t *p;
21 u8_t *optfield[3];
22 size_t optlen[3];
23 int i, tag, len;
25 /* The DHCP magic number must be correct, or no tags. */
26 if (dp->magic != DHCP_MAGIC) return 0;
28 optfield[0]= dp->options;
29 optlen[0]= arraysize(dp->options);
30 optfield[1]= dp->file;
31 optlen[1]= 0; /* Unknown if used for options yet. */
32 optfield[2]= dp->sname;
33 optlen[2]= 0;
35 for (i= 0; i < 3; i++) {
36 p= optfield[i];
37 while (p < optfield[i] + optlen[i]) {
38 tag= *p++;
39 if (tag == 255) break;
40 len= tag == 0 ? 0 : *p++;
41 if (tag == searchtag) {
42 if (pdata != nil) *pdata= p;
43 if (plen != nil) *plen= len;
44 return 1;
46 if (tag == DHCP_TAG_OVERLOAD) {
47 /* There are also options in the file or sname field. */
48 if (*p & 1) optlen[1]= arraysize(dp->file);
49 if (*p & 2) optlen[1]= arraysize(dp->sname);
51 p += len;
54 return 0;