/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* tcpip720 src/tcpip/usr/sbin/dhcp_dlls/samples/sample_dhcpapi.c 1.1     */
/*                                                                        */
/* Licensed Materials - Property of IBM                                   */
/*                                                                        */
/* COPYRIGHT International Business Machines Corp. 1999                   */
/* All Rights Reserved                                                    */
/*                                                                        */
/* US Government Users Restricted Rights - Use, duplication or            */
/* disclosure restricted by GSA ADP Schedule Contract with IBM Corp.      */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
static char sccsid[] = "@(#)25	1.1  src/tcpip/usr/sbin/dhcp_dlls/samples/sample_dhcpapi.c, dhcp, tcpip720 6/9/99 23:15:20";

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/param.h>
#include <dhcp_api.h>

extern int
Initialize(FILE *fp, caddr_t *instance)
{
	int *intp;
	int level = 0;
	char linebuf[256];
	char *cp;

	intp = (int *) malloc(sizeof(int));
	if (!intp)
	{
		dhcpapi_logmessage(dhcplog_syserr, "malloc() failed");
		return 1;
	}
	else
	{
		*instance = (caddr_t) intp;
		if (fp) while (fgets(linebuf, sizeof(linebuf), fp) != NULL)
		{
			cp = strchr(linebuf, '\n');
			if (cp) *cp = '\0';
			dhcpapi_logmessage(dhcplog_info,
				"(instance %08x) configuration line '%s'",
				*instance, linebuf);
			cp = linebuf;
			while (isspace(*cp) && cp < linebuf + sizeof(linebuf)) cp++;
			if (*cp == '{')
				level++;
			else if (*cp == '}')
				level--;
			if (level < 0)
				break;
		}
	}
	return 0;
}

extern void
Shutdown(caddr_t instance)
{
	dhcpapi_logmessage(dhcplog_event, "(instance %08x) shutdown", instance);
	free((int *) instance);
}

#ifdef SAMPLE_DHCPAPI_MESSAGERECEIVED

extern int
messageReceived(caddr_t instance, struct dhcpmessage **msg, size_t *msgsize,
                const struct in_addr *iprcv,
                const struct in_addr *ipdst,
                const struct in_addr *ipsrc)
{
	u_char *cp;
	char msgbuf[41], *ocp;

	dhcpapi_logmessage(dhcplog_event,
		"(instance %08x) message received",
		instance);
	for (cp = (u_char *) *msg , ocp = msgbuf ;
	     cp < (u_char *) *msg + MIN(20,(*msgsize)) ;
	     cp++, ocp += 2 )
	{
		sprintf(ocp, "%02x", (uint8_t ) *cp);
	}
	msgbuf[40] = '\0';
	dhcpapi_logmessage(dhcplog_event, "message size = %d", *msgsize);
	dhcpapi_logmessage(dhcplog_event, "first part of message = %s", msgbuf);
	dhcpapi_logmessage(dhcplog_event, "receiving IP = %s", inet_ntoa(*iprcv));
	dhcpapi_logmessage(dhcplog_event, "dest. IP = %s", inet_ntoa(*ipdst));
	dhcpapi_logmessage(dhcplog_event, "source IP = %s", inet_ntoa(*ipsrc));
	return 0;
}

#endif

#ifdef SAMPLE_DHCPAPI_ADDRESSOFFERED

extern void
addressOffered(caddr_t instance,
               const struct dhcpclientid *cid,
               const struct in_addr *address,
               const struct dhcpmessage *msg,
               size_t msgsize)
{
	char cidbuffer[256];
	char msgbuf[41], *ocp;
	u_char *cp;

	dhcpapi_logmessage(dhcplog_event,
		"(instance %08x) address offered",
		instance);
	for (cp = (u_char *) msg , ocp = msgbuf ;
	     cp < (u_char *) msg + MIN(20, msgsize) ;
	     cp++, ocp += 2 )
	{
		sprintf(ocp, "%02x", (uint8_t) *cp);
	}
	msgbuf[40] = '\0';
	dhcputil_cid2str(cid, cidbuffer);
	dhcpapi_logmessage(dhcplog_event, "message size = %d", msgsize);
	dhcpapi_logmessage(dhcplog_event, "first part of message = %s", msgbuf);
	dhcpapi_logmessage(dhcplog_event, "address = %s", inet_ntoa(*address));
	dhcpapi_logmessage(dhcplog_event, "client id = %s", cidbuffer);
}

#endif


#ifdef SAMPLE_DHCPAPI_ADDRESSASSIGNED

extern void
addressAssigned(caddr_t instance,
                const struct dhcpclientid *cid,
                const struct in_addr *address,
                const char *hostname,
                const char *domain,
                const struct dhcpmessage *msg,
                size_t msgsize)
{
	char cidbuffer[256];
	char msgbuf[41], *ocp;
	u_char *cp;

	dhcpapi_logmessage(dhcplog_event,
		"(instance %08x) address assigned",
		instance);
	for (cp = (u_char *) msg , ocp = msgbuf ;
	     cp < (u_char *) msg + MIN(20, msgsize) ;
	     cp++, ocp += 2 )
	{
		sprintf(ocp, "%02x", (uint8_t) *cp);
	}
	msgbuf[40] = '\0';
	dhcputil_cid2str(cid, cidbuffer);
	dhcpapi_logmessage(dhcplog_event, "message size = %d", msgsize);
	dhcpapi_logmessage(dhcplog_event, "first part of message = %s", msgbuf);
	dhcpapi_logmessage(dhcplog_event, "address = %s", inet_ntoa(*address));
	dhcpapi_logmessage(dhcplog_event, "client id = %s", cidbuffer);
	if (hostname)
		dhcpapi_logmessage(dhcplog_event, "hostname = %s", hostname);
	if (domain)
		dhcpapi_logmessage(dhcplog_event, "domain = %s", domain);
}

#endif


#ifdef SAMPLE_DHCPAPI_ADDRESSRELEASED

extern void
addressReleased(caddr_t instance,
                const struct dhcpclientid *cid,
                const struct in_addr *address,
                const char *hostname,
                const char *domain)
{
	char cidbuffer[256];

	dhcputil_cid2str(cid, cidbuffer);
	dhcpapi_logmessage(dhcplog_event,
		"(instance %08x) address released",
		instance);
	dhcpapi_logmessage(dhcplog_event, "address = %s", inet_ntoa(*address));
	dhcpapi_logmessage(dhcplog_event, "client id = %s", cidbuffer);
	if (hostname)
		dhcpapi_logmessage(dhcplog_event, "hostname = %s", hostname);
	if (domain)
		dhcpapi_logmessage(dhcplog_event, "domain = %s", domain);
}

#endif

#ifdef SAMPLE_DHCPAPI_ADDRESSEXPIRED

extern void
addressExpired(caddr_t instance,
               const struct dhcpclientid *cid,
               const struct in_addr *address,
               const char *hostname,
               const char *domain)
{
	char cidbuffer[256];

	dhcputil_cid2str(cid, cidbuffer);
	dhcpapi_logmessage(dhcplog_event,
		"(instance %08x) address expired",
		instance);
	dhcpapi_logmessage(dhcplog_event, "address = %s", inet_ntoa(*address));
	dhcpapi_logmessage(dhcplog_event, "client id = %s", cidbuffer);
	if (hostname)
		dhcpapi_logmessage(dhcplog_event, "hostname = %s", hostname);
	if (domain)
		dhcpapi_logmessage(dhcplog_event, "domain = %s", domain);
}

#endif

#ifdef SAMPLE_DHCPAPI_ADDRESSDELETED

extern void
addressDeleted(caddr_t instance,
               const struct dhcpclientid *cid,
               const struct in_addr *address,
               const char *hostname,
               const char *domain)
{
	char cidbuffer[256];

	dhcputil_cid2str(cid, cidbuffer);
	dhcpapi_logmessage(dhcplog_event,
		"(instance %08x) address deleted",
		instance);
	dhcpapi_logmessage(dhcplog_event, "address = %s", inet_ntoa(*address));
	dhcpapi_logmessage(dhcplog_event, "client id = %s", cidbuffer);
	if (hostname)
		dhcpapi_logmessage(dhcplog_event, "hostname = %s", hostname);
	if (domain)
		dhcpapi_logmessage(dhcplog_event, "domain = %s", domain);
}

#endif
#ifdef SAMPLE_DHCPAPI_ADDRESSDECLINED

extern void
addressDeclined(caddr_t instance,
                const struct dhcpclientid *cid,
                const struct in_addr *address,
                const char *hostname,
                const char *domain)
{
	char cidbuffer[256];

	dhcputil_cid2str(cid, cidbuffer);
	dhcpapi_logmessage(dhcplog_event,
		"(instance %08x) address declined",
		instance);
	dhcpapi_logmessage(dhcplog_event, "address = %s", inet_ntoa(*address));
	dhcpapi_logmessage(dhcplog_event, "client id = %s", cidbuffer);
	if (hostname)
		dhcpapi_logmessage(dhcplog_event, "hostname = %s", hostname);
	if (domain)
		dhcpapi_logmessage(dhcplog_event, "domain = %s", domain);
}

#endif
