www.pudn.com > e041tcpipc51.rar > NETUTIL.C


/* Network utility functions for 'TCP/IP Lean' (c) Iosoft Ltd. 2000  */ 
 
#include  
#include  
#include  
#include  
#include "ethernet.h" 
#include "netutil.h" 
#define MACLEN     6 
#define BCASTADDR     0xff,0xff,0xff,0xff,0xff,0xff     // broadcast address 
BYTE bcast[MACLEN]={BCASTADDR};  
 
/* Convert IP address into a string */ 
char *ipstr(LWORD ip, char *s) 
{ 
    sprintf(s, "%lu.%lu.%lu.%lu",(ip>>24)&255,(ip>>16)&255,(ip>>8)&255,ip&255); 
    return(s); 
} 
 
/* Convert Ethernet address into a string (max 17 chars plus null) */ 
char *ethstr(BYTE *addr, char *str) 
{ 
    int i; 
    char *s=str; 
 
    if (!memcmp(addr, bcast, MACLEN)) 
        strcpy(s, "----BROADCAST----"); 
    else for (i=0; i0 ? ":%02x" : "%02x", *addr++); 
    return(str); 
} 
 
/* Convert string to IP addr: first digits form most-significant byte  */ 
LWORD atoip(char *str) 
{ 
    LWORD ip=0L; 
    int i=4, n; 
    char c=1; 
 
    while (--i>=0 && c) 
    { 
        n = 0; 
        while (isdigit(c=*str++)) 
            n = n*10 + c-'0'; 
        ip += (LWORD)n << (i*8); 
    } 
    return(ip); 
} 
 
 
/* Check the given token is at the start of the string 
** Return pointer to the first char after the token, 0 if token not found */ 
char *skiptoken(char *str, char *tok) 
{ 
    int n; 
    char *s=0; 
 
    n = strlen(tok); 
    if (n>0 && str && !strncmp(str, tok, n)) 
        s = str + n; 
    return(s); 
} 
 
/* Return a pointer to the first char after any whitespace */ 
char *skipspace(char *str) 
{ 
    while (isspace(*str)) 
        str++; 
    return(str); 
} 
 
/* Return a pointer to the first char after any whitespace or punctuation */ 
char *skippunct(char *str) 
{ 
    while (isspace(*str) || ispunct(*str)) 
        str++; 
    return(str); 
} 
 
/* Check whether a sequence value lies within two others, return 0 if not */ 
int in_limits(LWORD val, LWORD lo, LWORD hi) 
{ 
    long lodiff, hidiff; 
 
    lodiff = val - lo; 
    hidiff = hi - val; 
    return(lodiff>=0 && hidiff>=0); 
} 
/* Print a hex dump of a buffer */ 
void hexdump(BYTE *buff, WORD len) 
{ 
    BYTE c, str[17]; 
    WORD j, n=0; 
    while (n < len)                /* For each line of 16 bytes... */ 
    { 
        printf("  %04x:", n); 
        for (j=0; j<16; j++)                /* For each byte of 16... */ 
        { 
            printf("%c", j==8 ? '-':' ');   /* Put '-' after 8 bytes */ 
            if (n++ >= len)                 /* If no bytes left... */ 
            { 
                printf("  ");               /* Fill out space */ 
                str[j] = 0; 
            } 
            else                            /* If bytes left... */ 
            { 
                printf("%02x", c = *buff++);/* Print byte value */ 
                str[j] = c>=' '&&c<='~' ? c : '.'; 
            }                               /* Save char if valid */ 
        } 
        str[j] = 0;                        /* Print char string */ 
        printf("  %s\n", str); 
    } 
} 
 
 
/* EOF */