
#include "V.h"
/*
* Out of frustration with the incredibly  complicated  internet  address  data
* structures,  here are routines that takes any sort of pointer to a 4-byte IP
* address in network or host order, and returns a pointer  to  the  equivalent
* ASCII dot-notation string. Note that we cycle among NB char buffers, so that
* up to NB addresses may be generated for a single printf() call.
*/
#define NB 8
static char ipdotb[NB][NB*NETADDRLEN+1];
static int  ipdotn = 0;					/* The last-used buffer */

char* ipdot(x) 		/* Network byte order */
	byte *x;	/* Unsigned to prevent sign extension */
{
	if (!x) return(m_null);
	ipdotn = (ipdotn + 1) % NB;
	sprintf(ipdotb[ipdotn],"%d.%d.%d.%d", x[0], x[1], x[2], x[3]);
	return(ipdotb[ipdotn]);
}
char *
iphdot(x)		/* Host byte order */
	byte *x;	/* Use unsigned to prevent sign extension */
{
	if (!x) return(m_null);
	ipdotn = (ipdotn + 1) % NB;
#if defined(USE_pthreads) && (USE_pthreads > 0)
	if (V_locking && use_pthreads) {
		pthread_lock_global_np();
		P6 "\t\t\tpthread_lock_global_np() in %s",Fctname D;
		fflush(Vout);
	}
#endif /*USE_pthreads*/
	sprintf(ipdotb[ipdotn],"%d.%d.%d.%d",
#ifdef         BIGENDIAN	/* 4321 [Motorola, SPARC]*/
#define ENDIAN BIGENDIAN
		x[3], x[2], x[1], x[0]
#endif
#ifdef         LITTLENDIAN	/* 1234 [Intel, VAX] */
#define ENDIAN LITTLENDIAN
		x[0], x[1], x[2], x[3]
#endif
#ifdef         MIXEDENDIAN	/* 2143 [some 16-bit cpus] */
#define ENDIAN MIXEDENDIAN
		x[1], x[0], x[3] x[2],
#endif
#ifndef ENDIAN		/* Assume network order (which is BIGENDIAN) */
		x[3], x[2], x[1], x[0]
#endif
	);
#if defined(USE_pthreads) && (USE_pthreads > 0)
	if (V_locking && use_pthreads) {
		P6 "\t\t\tpthread_unlock_global_np() in %s",Fctname D;
		fflush(Vout);
		pthread_unlock_global_np();
	}
#endif /*USE_pthreads*/
	V2 "ipdot: ### Warning, host byte order is unknown, using network order." D;
	return(ipdotb[ipdotn]);
}
