/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* bos720 src/bos/usr/samples/tcpip/dynload/rnd_ng.c 1.2                  */
/*                                                                        */
/* Licensed Materials - Property of IBM                                   */
/*                                                                        */
/* Restricted Materials 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                                                     */
/*
	    NOTICE TO USERS OF THE SOURCE CODE EXAMPLES

 INTERNATIONAL BUSINESS MACHINES CORPORATION PROVIDES THE SOURCE CODE
 EXAMPLES, BOTH INDIVIDUALLY AND AS ONE OR MORE GROUPS, "AS IS" WITHOUT
 WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT
 LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE
 OF THE SOURCE CODE EXAMPLES, BOTH INDIVIDUALLY AND AS ONE OR MORE GROUPS,
 IS WITH YOU.  SHOULD ANY PART OF THE SOURCE CODE EXAMPLES PROVE
 DEFECTIVE, YOU (AND NOT IBM OR AN AUTHORIZED RISC System/6000* WORKSTATION
 DEALER) ASSUME THE ENTIRE COST OF ALL NECESSARY SERVICING, REPAIR OR
 CORRECTION.

  RISC System/6000 is a trademark of International Business Machines
   Corporation.
*/
/* INCLUDE for structure definitions */
#include <stdlib.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/nameser.h>
#include <sys/errno.h>
#include <resolv.h>
#include <netdb.h>
#include <fcntl.h>
#include <pthread.h>

typedef struct _ng_pvt {
	pthread_t pvt_thd;		
	u_char	pvt_addr[16];		/* buffer for addr return */
	u_char	pvt_ng[121];
	char	pvt_host[1024];		/* buffer for host name */
	char	pvt_user[1024];		/* buffer for user name */
	char	pvt_domain[1024];	/* buffer for domain name */
} ng_pvt;

void 	*ng_pvtinit();
void 	ng_close();
void 	ng_rewind();
void 	ng_minimize();
int	ng_next();
int	ng_test();

void *
ng_pvtinit()
{
	/* Step 1: Allocate memory and clear it */
	ng_pvt *pvt = (ng_pvt *)malloc(sizeof(*pvt));
	bzero(pvt,sizeof(*pvt));

	/* Step 2: Initialize whatever needs be done per-process */
	pvt->pvt_thd = pthread_self();

	return pvt;
}

/*
 * ng_close() - inverse of ng_pvtinit();
 */
void
ng_close(void *this)
{
	if (!this) return;

	free(this);
}

/*
 * ng_rewind() - reset lookup; per-call init
 */
void
ng_rewind(void *this, const char *group)
{

	ng_pvt *pvt = NULL;
	char	s1[1023],
		s2[1023],
		s3[1023];

	if (!this)  {
		errno = EINVAL;
		return;
	}
	pvt = (ng_pvt *) this;

	bzero(s1, sizeof s1);
	bzero(s2, sizeof s2);
	bzero(s3, sizeof s3);
	sprintf(s1,"netgroup.host.%s", group);
	sprintf(s2,"netgroup.user.%s", group);
	sprintf(s3,"netgroup.domain.%s", group);
	strcpy(pvt->pvt_host, s1);
	strcpy(pvt->pvt_user, s2);
	strcpy(pvt->pvt_domain, s3);
}

void
ng_minimize(void *this)
{
	/* Nothing to do */
}

/*
 * ng_test() - return the "next" item for comparison
 */
int
ng_test(void *this, const char *name, const char *host, const char * user, const char *domain)
{
	ng_pvt  *pvt = NULL;
	if (!this)  {
		errno = EINVAL;
		return (NULL);
	}
	pvt = (ng_pvt *) this;

	ng_rewind(this, name);  
	ng_next(this, &pvt->pvt_host, &pvt->pvt_user, &pvt->pvt_domain);
	return (1);
}

/*
 * ng_next() - return the "next" item for comparison
 */
int 
ng_next(void *this, const char **host, const char ** user, const char **domain)
{
	ng_pvt  *pvt = NULL;

	if (!this)  {
		errno = EINVAL;
		return (NULL);
	}
	pvt = (ng_pvt *) this;

	*host = pvt->pvt_host;
	*user = pvt->pvt_user;
	*domain = pvt->pvt_domain;  

	return (1);
}