/*
 * dovend.c : Inserts all but the first few vendor options.
 */

#include <sys/types.h>

#include <netinet/in.h>
#include <arpa/inet.h>			/* inet_ntoa */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>


#include "defaults.h"
#include "bootp.h"
#include "bootpd.h"
#include "dhcpd.h"
#include "dhcp.h"
#include "report.h"
#include "dovend.h"
#include "my_syslog.h"
#include "utils.h"

#ifdef	__STDC__
#define P(args) args
#else
#define P(args) ()
#endif

int insert_generic P((struct shared_bindata *, byte **, int *, struct host *hp, int proto_flavor));

/*
 * Insert the 2nd part of the options into an option buffer.
 * Return amount of space used.
 *
 * This inserts everything EXCEPT:
 *   magic cookie, subnet mask, gateway, bootsize, extension file
 * Those are handled separately (in bootpd.c) to allow this function
 * to be shared between bootpd and bootpef.
 *
 * When an "extension file" is in use, the options inserted by
 * this function go into the exten_file, not the bootp response.
 */

int
dovend_rfc1497(hp, buf, len, proto_flavor)
	struct host *hp;
	byte *buf;
	int len;
	int proto_flavor; /* PROTO_FLAVOR_BOOTP, PROTO_FLAVOR_DHCP */
{
	int bytesleft = len;
	byte *vp = buf;

	static char noroom[] = "%s: No room for \"%s\" option in BOOTPREPLY";
#define	NEED(LEN, MSG) do                       \
		if (bytesleft < (LEN)) {         	    \
			report(LOG_NOTICE, noroom,          \
				   hp->hostname->string, MSG);  \
			return (vp - buf);                  \
		} while (0)

	/*
	 * Note that the following have already been inserted:
	 *   magic_cookie, subnet_mask, gateway, bootsize
	 *
	 * The remaining options are inserted in order of importance.
	 * (Of course the importance of each is a matter of opinion.)
	 * The option insertion order should probably be configurable.
	 *
	 * This is the order used in the NetBSD version.  Can anyone
	 * explain why the time_offset and swap_server are first? * Also, why is the hostname so far down the list?  -gwr
	 */

	if (hp->flags.time_offset) {
		if (! ((proto_flavor==PROTO_FLAVOR_BOOTP) && filter_bootp_option(hp, TAG_TIME_OFFSET))) {
			NEED(6, "to");
			*vp++ = TAG_TIME_OFFSET;/* -1 byte  */
			*vp++ = 4;				/* -1 byte  */
			insert_u_long(htonl(hp->time_offset), &vp);	/* -4 bytes */
			bytesleft -= 6;
		}
	}
	/*
	 * swap server, root path, dump path
	 */
	if (hp->flags.swap_server) {
		if (! ((proto_flavor==PROTO_FLAVOR_BOOTP) && filter_bootp_option(hp, TAG_SWAP_SERVER))) {
			NEED(6, "sw");
			/* There is just one SWAP_SERVER, so it is not an iplist. */
			*vp++ = TAG_SWAP_SERVER;/* -1 byte  */
			*vp++ = 4;				/* -1 byte  */
			insert_u_long(hp->swap_server.s_addr, &vp);	/* -4 bytes */
			bytesleft -= 6;			/* Fix real count */
		}
	}
	if (hp->flags.root_path) {
		if (! ((proto_flavor==PROTO_FLAVOR_BOOTP) && filter_bootp_option(hp, TAG_ROOT_PATH))) {
			/*
			 * Check for room for root_path.  Add 2 to account for
			 * TAG_ROOT_PATH and length.
			 */
			len = strlen(hp->root_path->string);
			NEED((len + 2), "rp");
			*vp++ = TAG_ROOT_PATH;
			*vp++ = (byte) (len & 0xFF);
			bcopy(hp->root_path->string, vp, len);
			vp += len;
			bytesleft -= len + 2;
		}
	}
	if (hp->flags.dump_file) {
		if (! ((proto_flavor==PROTO_FLAVOR_BOOTP) && filter_bootp_option(hp, TAG_DUMP_FILE))) {
			/*
			 * Check for room for dump_file.  Add 2 to account for
			 * TAG_DUMP_FILE and length.
			 */
			len = strlen(hp->dump_file->string);
			NEED((len + 2), "df");
			*vp++ = TAG_DUMP_FILE;
			*vp++ = (byte) (len & 0xFF);
			bcopy(hp->dump_file->string, vp, len);
			vp += len;
			bytesleft -= len + 2;
		}
	}
	/*
	 * DNS server and domain
	 */
	if (hp->flags.domain_server) {
		if (! ((proto_flavor==PROTO_FLAVOR_BOOTP) && filter_bootp_option(hp, TAG_DOMAIN_SERVER))) {
			if (insert_ip(TAG_DOMAIN_SERVER,
					  	hp->domain_server,
					  	&vp, &bytesleft))
				NEED(8, "ds");
		}
	}
	if (hp->flags.domain_name) {
		if (! ((proto_flavor==PROTO_FLAVOR_BOOTP) && filter_bootp_option(hp, TAG_DOMAIN_NAME))) {
			/*
			 * Check for room for domain_name.  Add 2 to account for
			 * TAG_DOMAIN_NAME and length.
			 */
			len = strlen(hp->domain_name->string);
			NEED((len + 2), "dn");
			*vp++ = TAG_DOMAIN_NAME;
			*vp++ = (byte) (len & 0xFF);
			bcopy(hp->domain_name->string, vp, len);
			vp += len;
			bytesleft -= len + 2;
		}
	}
	/*
	 * NIS (YP) server and domain
	 */
	if (hp->flags.nis_server) {
		if (! ((proto_flavor==PROTO_FLAVOR_BOOTP) && filter_bootp_option(hp, TAG_NIS_SERVER))) {
			if (hp->flags.nis_server) {
				if (insert_ip(TAG_NIS_SERVER,
					  		hp->nis_server,
					  		&vp, &bytesleft))
					NEED(8, "ys");
			}
		}
	}
	if (hp->flags.nis_domain) {
		if (! ((proto_flavor==PROTO_FLAVOR_BOOTP) && filter_bootp_option(hp, TAG_NIS_DOMAIN))) {
			/*
			 * Check for room for nis_domain.  Add 2 to account for
			 * TAG_NIS_DOMAIN and length.
			 */
			len = strlen(hp->nis_domain->string);
			NEED((len + 2), "yd");
			*vp++ = TAG_NIS_DOMAIN;
			*vp++ = (byte) (len & 0xFF);
			bcopy(hp->nis_domain->string, vp, len);
			vp += len;
			bytesleft -= len + 2;
		}
	}
	/* IEN 116 name server */
	if (hp->flags.name_server) {
		if (! ((proto_flavor==PROTO_FLAVOR_BOOTP) && filter_bootp_option(hp, TAG_NAME_SERVER))) {
			if (insert_ip(TAG_NAME_SERVER,
					  	hp->name_server,
					  	&vp, &bytesleft))
				NEED(8, "ns");
		}
	}
	if (hp->flags.rlp_server) {
		if (! ((proto_flavor==PROTO_FLAVOR_BOOTP) && filter_bootp_option(hp, TAG_RLP_SERVER))) {
			if (insert_ip(TAG_RLP_SERVER,
					  	hp->rlp_server,
					  	&vp, &bytesleft))
				NEED(8, "rl");
		}
	}
	/* Time server (RFC 868) */
	if (hp->flags.time_server) {
		if (! ((proto_flavor==PROTO_FLAVOR_BOOTP) && filter_bootp_option(hp, TAG_TIME_SERVER))) {
			if (insert_ip(TAG_TIME_SERVER,
					  	hp->time_server,
					  	&vp, &bytesleft))
				NEED(8, "ts");
		}
	}
	/* NTP (time) Server (RFC 1129) */
	if (hp->flags.ntp_server) {
		if (! ((proto_flavor==PROTO_FLAVOR_BOOTP) && filter_bootp_option(hp, TAG_NTP_SERVER))) {
			if (insert_ip(TAG_NTP_SERVER,
					  	hp->ntp_server,
					  	&vp, &bytesleft))
				NEED(8, "ts");
		}
	}
	/*
	 * I wonder:  If the hostname were "promoted" into the BOOTP
	 * response part, might these "extension" files possibly be
	 * shared between several clients?
	 *
	 * Also, why not just use longer BOOTP packets with all the
	 * additional length used as option data.  This bootpd version
	 * already supports that feature by replying with the same
	 * packet length as the client request packet. -gwr
	 */
	if (hp->flags.name_switch && hp->flags.send_name) {
		if (! ((proto_flavor==PROTO_FLAVOR_BOOTP) && filter_bootp_option(hp, TAG_HOST_NAME))) {
			/*
			 * Check for room for hostname.  Add 2 to account for
			 * TAG_HOST_NAME and length.
			 */
			len = strlen(hp->hostname->string);
			/*
			 * XXX - Too much magic.  The user can always set the hostname
			 * to the short version in the bootptab file. -gwr
			 */
			if ((len + 2) > bytesleft) {
				/*
				 * Not enough room for full (domain-qualified) hostname, try
				 * stripping it down to just the first field (host).
				 */
				char *tmpstr = hp->hostname->string;
				len = 0;
				while (*tmpstr && (*tmpstr != '.')) {
					tmpstr++;
					len++;
				}
			}
			NEED((len + 2), "hn");
			*vp++ = TAG_HOST_NAME;
			*vp++ = (byte) (len & 0xFF);
			bcopy(hp->hostname->string, vp, len);
			vp += len;
			bytesleft -= len + 2;
		}
	}
	/*
	 * The rest of these are less important, so they go last.
	 */
	if (hp->flags.lpr_server) {
		if (! ((proto_flavor==PROTO_FLAVOR_BOOTP) && filter_bootp_option(hp, TAG_LPR_SERVER))) {
			if (insert_ip(TAG_LPR_SERVER,
					  	hp->lpr_server,
					  	&vp, &bytesleft))
				NEED(8, "lp");
		}
	}
	if (hp->flags.cookie_server) {
		if (! ((proto_flavor==PROTO_FLAVOR_BOOTP) && filter_bootp_option(hp, TAG_COOKIE_SERVER))) {
			if (insert_ip(TAG_COOKIE_SERVER,
					  	hp->cookie_server,
					  	&vp, &bytesleft))
				NEED(8, "cs");
		}
	}
	if (hp->flags.log_server) {
		if (! ((proto_flavor==PROTO_FLAVOR_BOOTP) && filter_bootp_option(hp, TAG_LOG_SERVER))) {
			if (insert_ip(TAG_LOG_SERVER,
					  	hp->log_server,
					  	&vp, &bytesleft))
				NEED(8, "lg");
		}
	}
	/* broadcast_addr is actually fairly important, but I added it here to avoid
	   breaking existing responses that were already full */
	if (hp->flags.broadcast_addr) {
		if (! ((proto_flavor==PROTO_FLAVOR_BOOTP) && filter_bootp_option(hp, TAG_BROADCAST_ADDR))) {
			NEED(6, "ba");
			/* There is just one BROADCAST_ADDR, so it is not an iplist. */
			*vp++ = TAG_BROADCAST_ADDR;/* -1 byte  */
			*vp++ = 4;				/* -1 byte  */
			insert_u_long(hp->broadcast_addr.s_addr, &vp);	/* -4 bytes */
			bytesleft -= 6;			/* Fix real count */
		}
	}
	if (hp->flags.perform_router_discovery) {
		if (! ((proto_flavor==PROTO_FLAVOR_BOOTP) && filter_bootp_option(hp, TAG_PERFORM_ROUTER_DISCOVERY))) {
			NEED(3, "rd");
			*vp++ = TAG_PERFORM_ROUTER_DISCOVERY;/* -1 byte  */
			*vp++ = 1;				/* -1 byte  */
			*vp++ = hp->perform_router_discovery; /* -1 byte */
			bytesleft -= 3;
		}
	}
	if (hp->flags.static_route) {
		if (! ((proto_flavor==PROTO_FLAVOR_BOOTP) && filter_bootp_option(hp, TAG_STATIC_ROUTE))) {
			if (insert_ip(TAG_STATIC_ROUTE,
					  	hp->static_route,
					  	&vp, &bytesleft))
				NEED(8, "sr");
		}
	}
	/* we don't do the client_fqdn option (TAG_CLIENT_FQDN) here yet.  It's not clear we should;
	   does it ever make since for the BootP (not DHCP) protocol? */
	if (hp->flags.ethernet_encapsulation) {
		if (! ((proto_flavor==PROTO_FLAVOR_BOOTP) && filter_bootp_option(hp, TAG_ETHERNET_ENCAPSULATION))) {
			NEED(3, "en");
			*vp++ = TAG_ETHERNET_ENCAPSULATION;/* -1 byte  */
			*vp++ = 1;				/* -1 byte  */
			*vp++ = hp->ethernet_encapsulation; /* -1 byte */
			bytesleft -= 3;
		}
	}
	if (hp->flags.nbdd_server) {
		if (! ((proto_flavor==PROTO_FLAVOR_BOOTP) && filter_bootp_option(hp, TAG_NBDD_SERVER))) {
			if (insert_ip(TAG_NBDD_SERVER,
					  	hp->nbdd_server,
					  	&vp, &bytesleft))
				NEED(8, "nd");
		}
	}
	if (hp->flags.smtp_server) {
		if (! ((proto_flavor==PROTO_FLAVOR_BOOTP) && filter_bootp_option(hp, TAG_SMTP_SERVER))) {
			if (insert_ip(TAG_SMTP_SERVER,
					  	hp->smtp_server,
					  	&vp, &bytesleft))
				NEED(8, "ma");
		}
	}
	if (hp->flags.pop3_server) {
		if (! ((proto_flavor==PROTO_FLAVOR_BOOTP) && filter_bootp_option(hp, TAG_POP3_SERVER))) {
			if (insert_ip(TAG_POP3_SERVER,
					  	hp->pop3_server,
					  	&vp, &bytesleft))
				NEED(8, "po");
		}
	}
	if (hp->flags.nntp_server) {
		if (! ((proto_flavor==PROTO_FLAVOR_BOOTP) && filter_bootp_option(hp, TAG_NNTP_SERVER))) {
			if (insert_ip(TAG_NNTP_SERVER,
					  	hp->nntp_server,
					  	&vp, &bytesleft))
				NEED(8, "nn");
		}
	}
	if (hp->flags.www_server) {
		if (! ((proto_flavor==PROTO_FLAVOR_BOOTP) && filter_bootp_option(hp, TAG_WWW_SERVER))) {
			if (insert_ip(TAG_WWW_SERVER,
					  	hp->www_server,
					  	&vp, &bytesleft))
				NEED(8, "ww");
		}
	}
	if (hp->flags.finger_server) {
		if (! ((proto_flavor==PROTO_FLAVOR_BOOTP) && filter_bootp_option(hp, TAG_FINGER_SERVER))) {
			if (insert_ip(TAG_FINGER_SERVER,
					  	hp->finger_server,
					  	&vp, &bytesleft))
				NEED(8, "fi");
		}
	}
	if (hp->flags.irc_server) {
		if (! ((proto_flavor==PROTO_FLAVOR_BOOTP) && filter_bootp_option(hp, TAG_IRC_SERVER))) {
			if (insert_ip(TAG_IRC_SERVER,
					  	hp->irc_server,
					  	&vp, &bytesleft))
				NEED(8, "ir");
		}
	}
	if (hp->flags.ip_forwarding) {
		if (! ((proto_flavor==PROTO_FLAVOR_BOOTP) && filter_bootp_option(hp, TAG_IP_FORWARDING))) {
			NEED(3, "if");
			*vp++ = TAG_IP_FORWARDING;/* -1 byte  */
			*vp++ = 1;				/* -1 byte  */
			*vp++ = hp->ip_forwarding; /* -1 byte */
			bytesleft -= 3;
		}
	}
	if (hp->flags.ip_source_routing) {
		if (! ((proto_flavor==PROTO_FLAVOR_BOOTP) && filter_bootp_option(hp, TAG_IP_SOURCE_ROUTING))) {
			NEED(3, "cr");
			*vp++ = TAG_IP_SOURCE_ROUTING;/* -1 byte  */
			*vp++ = 1;				/* -1 byte  */
			*vp++ = hp->ip_source_routing; /* -1 byte */
			bytesleft -= 3;
		}
	}
	if (hp->flags.ip_ttl) {
		if (! ((proto_flavor==PROTO_FLAVOR_BOOTP) && filter_bootp_option(hp, TAG_IP_TTL))) {
			NEED(3, "tt");
			*vp++ = TAG_IP_TTL;/* -1 byte  */
			*vp++ = 1;				/* -1 byte  */
			*vp++ = hp->ip_ttl; /* -1 byte */
			bytesleft -= 3;
		}
	}
	if (hp->flags.all_subnets_are_local) {
		if (! ((proto_flavor==PROTO_FLAVOR_BOOTP) && filter_bootp_option(hp, TAG_ALL_SUBNETS_ARE_LOCAL))) {
			NEED(3, "sl");
			*vp++ = TAG_ALL_SUBNETS_ARE_LOCAL;/* -1 byte  */
			*vp++ = 1;				/* -1 byte  */
			*vp++ = hp->all_subnets_are_local; /* -1 byte */
			bytesleft -= 3;
		}
	}
	if (hp->flags.perform_mask_discovery) {
		if (! ((proto_flavor==PROTO_FLAVOR_BOOTP) && filter_bootp_option(hp, TAG_PERFORM_MASK_DISCOVERY))) {
			NEED(3, "md");
			*vp++ = TAG_PERFORM_MASK_DISCOVERY;/* -1 byte  */
			*vp++ = 1;				/* -1 byte  */
			*vp++ = hp->perform_mask_discovery; /* -1 byte */
			bytesleft -= 3;
		}
	}
	if (hp->flags.mask_supplier) {
		if (! ((proto_flavor==PROTO_FLAVOR_BOOTP) && filter_bootp_option(hp, TAG_MASK_SUPPLIER))) {
			NEED(3, "mu");
			*vp++ = TAG_MASK_SUPPLIER;/* -1 byte  */
			*vp++ = 1;				/* -1 byte  */
			*vp++ = hp->mask_supplier; /* -1 byte */
			bytesleft -= 3;
		}
	}
	if (hp->flags.router_solicitation_addr) {
		if (! ((proto_flavor==PROTO_FLAVOR_BOOTP) && filter_bootp_option(hp, TAG_ROUTER_SOLICITATION_ADDR))) {
			NEED(6, "rs");
			/* There is just one ROUTER_SOLICITATION_ADDR, so it is not an iplist. */
			*vp++ = TAG_ROUTER_SOLICITATION_ADDR;/* -1 byte  */
			*vp++ = 4;				/* -1 byte  */
			insert_u_long(hp->router_solicitation_addr.s_addr, &vp);	/* -4 bytes */
			bytesleft -= 6;			/* Fix real count */
		}
	}
	if (hp->flags.trailer_encapsulation) {
		if (! ((proto_flavor==PROTO_FLAVOR_BOOTP) && filter_bootp_option(hp, TAG_TRAILER_ENCAPSULATION))) {
			NEED(3, "te");
			*vp++ = TAG_TRAILER_ENCAPSULATION;/* -1 byte  */
			*vp++ = 1;				/* -1 byte  */
			*vp++ = hp->trailer_encapsulation; /* -1 byte */
			bytesleft -= 3;
		}
	}
	if (hp->flags.tcp_ttl) {
		if (! ((proto_flavor==PROTO_FLAVOR_BOOTP) && filter_bootp_option(hp, TAG_TCP_TTL))) {
			NEED(3, "tl");
			*vp++ = TAG_TCP_TTL;/* -1 byte  */
			*vp++ = 1;				/* -1 byte  */
			*vp++ = hp->tcp_ttl; /* -1 byte */
			bytesleft -= 3;
		}
	}
	if (hp->flags.tcp_keepalive_garbage) {
		if (! ((proto_flavor==PROTO_FLAVOR_BOOTP) && filter_bootp_option(hp, TAG_TCP_KEEPALIVE_GARBAGE))) {
			NEED(3, "kg");
			*vp++ = TAG_TCP_KEEPALIVE_GARBAGE;/* -1 byte  */
			*vp++ = 1;				/* -1 byte  */
			*vp++ = hp->tcp_keepalive_garbage; /* -1 byte */
			bytesleft -= 3;
		}
	}
	if (hp->flags.xwindows_font_server) {
		if (! ((proto_flavor==PROTO_FLAVOR_BOOTP) && filter_bootp_option(hp, TAG_XWINDOWS_FONT_SERVER))) {
			if (insert_ip(TAG_XWINDOWS_FONT_SERVER,
					  	hp->xwindows_font_server,
					  	&vp, &bytesleft))
				NEED(8, "xf");
		}
	}
	if (hp->flags.xwindows_display_manager) {
		if (! ((proto_flavor==PROTO_FLAVOR_BOOTP) && filter_bootp_option(hp, TAG_XWINDOWS_DISPLAY_MANAGER))) {
			if (insert_ip(TAG_XWINDOWS_DISPLAY_MANAGER,
					  	hp->xwindows_display_manager,
					  	&vp, &bytesleft))
				NEED(8, "xd");
		}
	}
	if (hp->flags.nisplus_domain) {
		if (! ((proto_flavor==PROTO_FLAVOR_BOOTP) && filter_bootp_option(hp, TAG_NISPLUS_DOMAIN))) {
			/*
			 * Check for room for nisplus_domain.  Add 2 to account for
			 * TAG_NISPLUS_DOMAIN and length.
			 */
			len = strlen(hp->nisplus_domain->string);
			NEED((len + 2), "pd");
			*vp++ = TAG_NISPLUS_DOMAIN;
			*vp++ = (byte) (len & 0xFF);
			bcopy(hp->nisplus_domain->string, vp, len);
			vp += len;
			bytesleft -= len + 2;
		}
	}
	if (hp->flags.nisplus_server) {
		if (! ((proto_flavor==PROTO_FLAVOR_BOOTP) && filter_bootp_option(hp, TAG_NISPLUS_SERVER))) {
			if (hp->flags.nisplus_server) {
				if (insert_ip(TAG_NISPLUS_SERVER,
					  		hp->nisplus_server,
					  		&vp, &bytesleft))
					NEED(8, "ps");
			}
		}
	}
	/*
	 * XXX - Add new tags here (to insert options)
	 */
	if (hp->flags.generic)
		insert_generic(hp->generic, &vp, &bytesleft, hp, proto_flavor);

	/*
	 * The end marker is inserted by the caller.
	 */
	return (vp - buf);
#undef	NEED
}								/* dovend_rfc1497 */



/* Return true if we should filter bootp option code 'bootp_options_code' from a BOOTPREPLY for 'hp'.  Else return false. */
int
filter_bootp_option(struct host *hp, int bootp_options_code)
{
	if (!hp) /* error */
		return 0;

	if (hp->flags.bootp_exclude_vector && get_bitvector((Bitvector) &hp->bootp_exclude_vector->data, bootp_options_code))
		return 1;

	if (hp->flags.bootp_include_vector && !get_bitvector((Bitvector) &hp->bootp_include_vector->data, bootp_options_code))
		return 1;

	return 0;

}

/*
 * Insert a tag value, a length value, and a list of IP addresses into the
 * memory buffer indirectly pointed to by "dest".  "tag" is the RFC1048 tag
 * number to use, "iplist" is a pointer to a list of IP addresses
 * (struct in_addr_list), and "bytesleft" points to an integer which
 * indicates the size of the "dest" buffer.
 *
 * Return zero if everything fits.
 *
 * This is used to fill the vendor-specific area of a bootp packet in
 * conformance to RFC1048.
 */

int
insert_ip(tag, iplist, dest, bytesleft)
	byte tag;
	struct in_addr_list *iplist;
	byte **dest;
	int *bytesleft;
{
	struct in_addr *addrptr;
	unsigned addrcount = 1;
	byte *d;

	if (iplist == NULL)
		return (0);

	if (*bytesleft >= 6) {
		d = *dest;				/* Save pointer for later */
		**dest = tag;
		(*dest) += 2;
		(*bytesleft) -= 2;		/* Account for tag and length */
		addrptr = iplist->addr;
		addrcount = iplist->addrcount;
		while ((*bytesleft >= 4) && (addrcount > 0)) {
			insert_u_long(addrptr->s_addr, dest);
			addrptr++;
			addrcount--;
			(*bytesleft) -= 4;	/* Four bytes per address */
		}
		d[1] = (byte) ((*dest - d - 2) & 0xFF);
	}
	return (addrcount);
}



/*
 * Insert generic data into a bootp packet.  The data is assumed to already
 * be in RFC1048 format.  It is inserted using a first-fit algorithm which
 * attempts to insert as many tags as possible.  Tags and data which are
 * too large to fit are skipped; any remaining tags are tried until they
 * have all been exhausted.
 * Return zero if everything fits.
 */

int
insert_generic(gendata, buff, bytesleft, hp, proto_flavor)
	struct shared_bindata *gendata;
	byte **buff;
	int *bytesleft;
	struct host *hp;
	int proto_flavor;
{
	byte *srcptr;
	int length, numbytes;
	int skipped = 0;
	static char noroom[] = "%s: No room for \"T%d\" option in BOOTPREPLY";

	if (gendata == NULL)
		return (0);

	srcptr = gendata->data;
	length = gendata->length;

	while ((length > 0) && (*bytesleft > 0)) {
		switch (*srcptr) {
		case TAG_END:
			length = 0;			/* Force an exit on next iteration */
			break;
		case TAG_PAD:
			*(*buff)++ = *srcptr++;
			(*bytesleft)--;
			length--;
			break;

		/* NOTE: if we want to skip inserting certan generic tags into the packet (e.g. to avoid inserting ones that
		   we've already inserted before, or those that would cause some sort of problem), this is the place to do it.
		   The value of *srcptr (a single byte) is the vendor option code that we are about to try to insert.
		   All we need to do is to define another bitvector member in the struct host, set each bit as we insert each
		   option code, and check here if the option we're about to insert (the code is in *srcptr) already
		   has its bit enabled.
		*/

		default:
			numbytes = srcptr[1] + 2;
			if ((proto_flavor==PROTO_FLAVOR_BOOTP) && filter_bootp_option(hp, *srcptr)) {
				; /* filter option from reply */
			}
			else if (*bytesleft < numbytes) {
				/* not enough space */
				report(LOG_NOTICE, noroom, hp->hostname->string, *srcptr);
				skipped += numbytes;
			} else {
				bcopy(srcptr, *buff, numbytes);
				(*buff) += numbytes;
				(*bytesleft) -= numbytes;
				/* NOTE: if we want to keep track of WHICH generic tags we have succesfully inserted into the packet,
				   this is the place to do it.  The value of *srcptr (a single byte) is the vendor option code that we
				   just inserted.
				*/
			}
			srcptr += numbytes;
			length -= numbytes;
			break;
		}
	} /* while */

	if (length > 0)
		report(LOG_NOTICE, "%d: no room for any more options in BOOTPREPLY, will not try to insert any (more) generic options");

	return (skipped || (length > 0)); 
}

/*
 * Insert the unsigned long "value" into memory starting at the byte
 * pointed to by the byte pointer (*dest).  (*dest) is updated to
 * point to the next available byte.
 *
 * Since it is desirable to internally store network addresses in network
 * byte order (in struct in_addr's), this routine expects longs to be
 * passed in network byte order.
 *
 * However, due to the nature of the main algorithm, the long must be in
 * host byte order, thus necessitating the use of ntohl() first.
 */

void
insert_u_long(value, dest)
	u_int32 value;
	byte **dest;
{
	byte *temp;
	int n;

	value = ntohl(value);		/* Must use host byte order here */
	temp = (*dest += 4);
	for (n = 4; n > 0; n--) {
		*--temp = (byte) (value & 0xFF);
		value >>= 8;
	}
	/* Final result is network byte order */
}
