/* 
 * $RCSfile: hoststr.c,v $
 *
 * x-kernel v3.3
 *
 * Copyright (c) 1993,1991,1990,1996  Arizona Board of Regents
 *
 * $Log: hoststr.c,v $
 * Revision 1.2  1996/01/29 19:46:05  slm
 * Updated copyright and version.
 *
 * Revision 1.1  1995/07/28  21:41:44  slm
 * Initial revision
 *
 * Revision 1.21.1.1.1.2  1994/12/02  17:45:37  hkaram
 * Removed some warnings
 *
 * Revision 1.21.1.1.1.1  1994/10/27  20:51:50  hkaram
 * New branch
 *
 * Revision 1.21.1.1  1994/03/14  22:54:49  umass
 * Added support for MAC48bbithosts
 *
 * Revision 1.21  1993/12/15  23:20:27  menze
 * Modifications from UMass:
 *
 *   [ 93/07/27          yates ]
 *     added str2fddiHost(h, s)
 */

#include "xkernel.h"
#include "ip.h"
#include "mac.h"
#ifndef XKMACHKERNEL
#include "x_stdio.h"
#endif /* ! XKMACHKERNEL */

#ifdef XKMACHKERNEL
int sscanf1(char *str, char *format, int *a1);

static int
sscanf4(str, format, a1, a2, a3, a4)
char *str, format;
int *a1, *a2, *a3, *a4;
{
    *a1 = *a2 = *a3 = *a4 = 0;
    while (*str >= '0' && *str <= '9')
        *a1 = 10*(*a1) + (*str++ - '0');
    if (*str++ != '.') return 1;
    while (*str >= '0' && *str <= '9')
        *a2 = 10*(*a2) + (*str++ - '0');
    if (*str++ != '.') return 2;
    while (*str >= '0' && *str <= '9')
        *a3 = 10*(*a3) + (*str++ - '0');
    if (*str++ != '.') return 3;
    while (*str >= '0' && *str <= '9')
        *a4 = 10*(*a4) + (*str++ - '0');
    return 4;
}

static int
hexdigit(a)
char a;
{
    if (a >= '0' && a <= '9') return(a-'0');
    if (a >= 'a' && a <= 'f') return(a-'a'+10);
    if (a >= 'A' && a <= 'F') return(a-'A'+10);
    return -1;
}

static int
sscanf6(str, format, a1, a2, a3, a4, a5, a6)
char *str, format;
int *a1, *a2, *a3, *a4, *a5, *a6;
{
    int n;

/*  printf("Sscanf6 converting %s\n", str); */
    *a1 = *a2 = *a3 = *a4 = *a5 = *a6 = 0;
    while ((n=hexdigit(*str))>=0)
        (*a1 = 16*(*a1) + n, str++);
    if (*str++ != ':') return 1;
    while ((n=hexdigit(*str))>=0)
        (*a2 = 16*(*a2) + n, str++);
    if (*str++ != ':') return 2;
    while ((n=hexdigit(*str))>=0)
        (*a3 = 16*(*a3) + n, str++);
    if (*str++ != ':') return 3;
    while ((n=hexdigit(*str))>=0)
        (*a4 = 16*(*a4) + n, str++);
    if (*str++ != ':') return 4;
    while ((n=hexdigit(*str))>=0)
        (*a5 = 16*(*a5) + n, str++);
    if (*str++ != ':') return 5;
    while ((n=hexdigit(*str))>=0)
        (*a6 = 16*(*a6) + n, str++);
/*
    printf("Sscanf6 results: %d %d %d %d %d %d\n",
	   *a1, *a2, *a3, *a4, *a5, *a6);
 */
    return 6;
}

int
sscanf1(str, format, a1)
char *str, *format;
int *a1;
{
    *a1=0;
    while (*str >= '0' && *str <= '9')
        *a1 = 10*(*a1) + (*str++ - '0');
    return 1;
}

#endif /* XKMACHKERNEL */

XkReturn
str2ipHost(h, s)
IPhost *h;
char *s;
{
    int a, b, c, d;

#ifndef XKMACHKERNEL
    if (sscanf(s, "%d.%d.%d.%d", &a, &b, &c, &d) < 4) {
#else
    if (sscanf4(s, "%d.%d.%d.%d", &a, &b, &c, &d) < 4) {
#endif /* ! XKMACHKERNEL */
	return XK_FAILURE;
    }
    h->a = a;
    h->b = b;
    h->c = c;
    h->d = d;
    return XK_SUCCESS;
}


char *
ipHostStr(h)
IPhost *h;
{
    static char str[4][32];
    static int i=0;
    
    i = ++i % 4;
    if (h == 0)
	return "<null>";
    if (h == (IPhost *)ANY_HOST)
	return "ANY_HOST";
    sprintf(str[i], "%d.%d.%d.%d", h->a, h->b, h->c, h->d);
    return str[i];
}

XkReturn
str2mac48bitHost(h, s)
MAC48bithost *h;
char *s;
{
     int a[6], i;
     
     if ( 
#ifndef XKMACHKERNEL
	 sscanf
#else
	 sscanf6
#endif /* ! XKMACHKERNEL */
	 (s, "%x:%x:%x:%x:%x:%x",
		 a, a+1, a+2, a+3, a+4, a+5) < 6) {
	 return XK_FAILURE;
     }
     for (i=0; i < 6; i++) {
	 if (a[i] > 0xff)
	     return XK_FAILURE;
     }
     h->high = htons((a[0] << 8) + a[1]);
     h->mid = htons((a[2] << 8) + a[3]);
     h->low = htons((a[4] << 8) + a[5]);
     return XK_SUCCESS;
}

XkReturn
str2ethHost(h, s)
ETHhost *h;
char *s;
{
    return str2mac48bitHost((MAC48bithost *)h, s); 
}

XkReturn
str2fddiHost(h, s)
FDDIhost *h;
char *s;
{
    return str2mac48bitHost((MAC48bithost *)h, s); 
}

char *
mac48bitHostStr(h)
MAC48bithost *h;
{
    static char str[4][32];
    static int i=0;
    u_char *hc = (u_char *)h;

    if (h == 0)
	return "<null>";
    if (h == (MAC48bithost *)ANY_HOST)
	return "ANY_HOST";
    i = ++i % 4;
    sprintf(str[i], "%02x:%02x:%02x:%02x:%02x:%02x",
	    hc[0], hc[1], hc[2], hc[3], hc[4], hc[5]);
    return str[i];
}

char *
ethHostStr(h)
ETHhost *h;
{
    return mac48bitHostStr((MAC48bithost *)h);
}

char *
fddiHostStr(h)
FDDIhost *h;
{
    return mac48bitHostStr((MAC48bithost *)h);
}
