dhcp_settag.c

Go to the documentation of this file.
00001 /*      dhcp_init(), dhcp_settag()                      Author: Kees J. Bot
00002  *                                                              1 Dec 2000
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     /* Initialize a DHCP packet. */
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     /* Add a tag to a DHCP packet.  No padding.  Only do the options field.
00026      * (This is Minix, we don't need megabytes of silly bits of data.)
00027      * The length may be zero to delete a tag.
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             /* The tag is already there, remove it so it gets replaced. */
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     /* Add tag. */
00045     if (len == 0) {
00046         /* We're merely deleting a tag. */
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         /* Oops, it didn't fit?  Is this really Minix??? */
00054         return 0;
00055     }
00056     return 1;
00057 }

Generated on Fri Apr 14 22:57:26 2006 for minix by  doxygen 1.4.6