home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / radius-2.zip / src / util.c < prev    next >
C/C++ Source or Header  |  1994-12-27  |  5KB  |  229 lines

  1. /*
  2.  *
  3.  *    RADIUS
  4.  *    Remote Authentication Dial In User Service
  5.  *
  6.  *
  7.  *    Livingston Enterprises, Inc.
  8.  *    6920 Koll Center Parkway
  9.  *    Pleasanton, CA   94566
  10.  *
  11.  *    Copyright 1992 Livingston Enterprises, Inc.
  12.  *
  13.  *    Permission to use, copy, modify, and distribute this software for any
  14.  *    purpose and without fee is hereby granted, provided that this
  15.  *    copyright and permission notice appear on all copies and supporting
  16.  *    documentation, the name of Livingston Enterprises, Inc. not be used
  17.  *    in advertising or publicity pertaining to distribution of the
  18.  *    program without specific prior permission, and notice be given
  19.  *    in supporting documentation that copying and distribution is by
  20.  *    permission of Livingston Enterprises, Inc.   
  21.  *
  22.  *    Livingston Enterprises, Inc. makes no representations about
  23.  *    the suitability of this software for any purpose.  It is
  24.  *    provided "as is" without express or implied warranty.
  25.  *
  26.  */
  27.  
  28. static char sccsid[] =
  29. "@(#)util.c    1.5 Copyright 1992 Livingston Enterprises Inc";
  30.  
  31. #include    <sys/types.h>
  32. #include    <sys/socket.h>
  33. #include    <sys/time.h>
  34. #include    <netinet/in.h>
  35.  
  36. #include    <stdio.h>
  37. #include    <netdb.h>
  38. #include    <pwd.h>
  39. #include    <time.h>
  40. #include    <ctype.h>
  41.  
  42. #include    "radius.h"
  43.  
  44. /*************************************************************************
  45.  *
  46.  *    Function: ip_hostname
  47.  *
  48.  *    Purpose: Return a printable host name (or IP address in dot notation)
  49.  *         for the supplied IP address.
  50.  *
  51.  *************************************************************************/
  52.  
  53. char    *
  54. ip_hostname(ipaddr)
  55. UINT4    ipaddr;
  56. {
  57.     struct    hostent *hp;
  58.     static char    hstname[128];
  59.     UINT4    n_ipaddr;
  60.  
  61.     n_ipaddr = htonl(ipaddr);
  62.     hp = gethostbyaddr((char *)&n_ipaddr, sizeof (struct in_addr), AF_INET);
  63.     if (hp == 0) {
  64.         ipaddr2str(hstname, ipaddr);
  65.         return(hstname);
  66.     }
  67.     return(hp->h_name);
  68. }
  69.  
  70. /*************************************************************************
  71.  *
  72.  *    Function: get_ipaddr
  73.  *
  74.  *    Purpose: Return an IP address in host long notation from a host
  75.  *         name or address in dot notation.
  76.  *
  77.  *************************************************************************/
  78.  
  79. UINT4
  80. get_ipaddr(host)
  81. char    *host;
  82. {
  83.     struct hostent *hp;
  84.     static char    buffer[32];
  85.     UINT4        ipstr2long();
  86.  
  87.     if(good_ipaddr(host) == 0) {
  88.         return(ipstr2long(host));
  89.     }
  90.     else if((hp = gethostbyname(host)) == (struct hostent *)NULL) {
  91.         return((UINT4)0);
  92.     }
  93.     return(ntohl(*(UINT4 *)hp->h_addr));
  94. }
  95.  
  96. /*************************************************************************
  97.  *
  98.  *    Function: good_ipaddr
  99.  *
  100.  *    Purpose: Check for valid IP address in standard dot notation.
  101.  *
  102.  *************************************************************************/
  103.  
  104. good_ipaddr(addr)
  105. char    *addr;
  106. {
  107.     int    dot_count;
  108.     int    digit_count;
  109.  
  110.     dot_count = 0;
  111.     digit_count = 0;
  112.     while(*addr != '\0' && *addr != ' ') {
  113.         if(*addr == '.') {
  114.             dot_count++;
  115.             digit_count = 0;
  116.         }
  117.         else if(!isdigit(*addr)) {
  118.             dot_count = 5;
  119.         }
  120.         else {
  121.             digit_count++;
  122.             if(digit_count > 3) {
  123.                 dot_count = 5;
  124.             }
  125.         }
  126.         addr++;
  127.     }
  128.     if(dot_count != 3) {
  129.         return(-1);
  130.     }
  131.     else {
  132.         return(0);
  133.     }
  134. }
  135.  
  136. /*************************************************************************
  137.  *
  138.  *    Function: ipaddr2str
  139.  *
  140.  *    Purpose: Return an IP address in standard dot notation for the
  141.  *         provided address in host long notation.
  142.  *
  143.  *************************************************************************/
  144.  
  145. ipaddr2str(buffer, ipaddr)
  146. char    *buffer;
  147. UINT4    ipaddr;
  148. {
  149.     int    addr_byte[4];
  150.     int    i;
  151.     UINT4    xbyte;
  152.  
  153.     for(i = 0;i < 4;i++) {
  154.         xbyte = ipaddr >> (i*8);
  155.         xbyte = xbyte & (UINT4)0x000000FF;
  156.         addr_byte[i] = xbyte;
  157.     }
  158.     sprintf(buffer, "%u.%u.%u.%u", addr_byte[3], addr_byte[2],
  159.         addr_byte[1], addr_byte[0]);
  160. }
  161.  
  162. /*************************************************************************
  163.  *
  164.  *    Function: pairfree
  165.  *
  166.  *    Purpose: Release the memory used by a list of attribute-value
  167.  *         pairs.
  168.  *
  169.  *************************************************************************/
  170.  
  171. pairfree(pair)
  172. VALUE_PAIR    *pair;
  173. {
  174.     VALUE_PAIR    *next;
  175.  
  176.     while(pair != (VALUE_PAIR *)NULL) {
  177.         next = pair->next;
  178.         free(pair);
  179.         pair = next;
  180.     }
  181. }
  182.  
  183. /*************************************************************************
  184.  *
  185.  *    Function: ipstr2long
  186.  *
  187.  *    Purpose: Return an IP address in host long notation from
  188.  *         one supplied in standard dot notation.
  189.  *
  190.  *************************************************************************/
  191.  
  192. UINT4
  193. ipstr2long(ip_str)
  194. char    *ip_str;
  195. {
  196.     char    buf[6];
  197.     char    *ptr;
  198.     int    i;
  199.     int    count;
  200.     UINT4    ipaddr;
  201.     int    cur_byte;
  202.  
  203.     ipaddr = (UINT4)0;
  204.     for(i = 0;i < 4;i++) {
  205.         ptr = buf;
  206.         count = 0;
  207.         *ptr = '\0';
  208.         while(*ip_str != '.' && *ip_str != '\0' && count < 4) {
  209.             if(!isdigit(*ip_str)) {
  210.                 return((UINT4)0);
  211.             }
  212.             *ptr++ = *ip_str++;
  213.             count++;
  214.         }
  215.         if(count >= 4 || count == 0) {
  216.             return((UINT4)0);
  217.         }
  218.         *ptr = '\0';
  219.         cur_byte = atoi(buf);
  220.         if(cur_byte < 0 || cur_byte > 255) {
  221.             return((UINT4)0);
  222.         }
  223.         ip_str++;
  224.         ipaddr = ipaddr << 8 | (UINT4)cur_byte;
  225.     }
  226.     return(ipaddr);
  227. }
  228.  
  229.