home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / radius_2.zip / util.c < prev    next >
C/C++ Source or Header  |  1996-05-16  |  5KB  |  231 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    <stdlib.h>
  38. #include    <netdb.h>
  39. #include    <pwd.h>
  40. #include    <time.h>
  41. #include    <ctype.h>
  42.  
  43. #include    "radius.h"
  44.  
  45.  
  46. /*************************************************************************
  47.  *
  48.  *    Function: ip_hostname
  49.  *
  50.  *    Purpose: Return a printable host name (or IP address in dot notation)
  51.  *         for the supplied IP address.
  52.  *
  53.  *************************************************************************/
  54.  
  55. char    *
  56. ip_hostname(ipaddr)
  57. UINT4    ipaddr;
  58. {
  59.     struct    hostent *hp;
  60.     static char    hstname[128];
  61.     UINT4    n_ipaddr;
  62.  
  63.     n_ipaddr = htonl(ipaddr);
  64.     hp = gethostbyaddr((char *)&n_ipaddr, sizeof (struct in_addr), AF_INET);
  65.     if (hp == 0) {
  66.         ipaddr2str(hstname, ipaddr);
  67.         return(hstname);
  68.     }
  69.     return(hp->h_name);
  70. }
  71.  
  72. /*************************************************************************
  73.  *
  74.  *    Function: get_ipaddr
  75.  *
  76.  *    Purpose: Return an IP address in host long notation from a host
  77.  *         name or address in dot notation.
  78.  *
  79.  *************************************************************************/
  80.  
  81. UINT4
  82. get_ipaddr(host)
  83. char    *host;
  84. {
  85.     struct hostent *hp;
  86. /*    static char    buffer[32];*/
  87.     UINT4        ipstr2long();
  88.  
  89.     if(good_ipaddr(host) == 0) {
  90.         return(ipstr2long(host));
  91.     }
  92.     else if((hp = gethostbyname(host)) == (struct hostent *)NULL) {
  93.         return((UINT4)0);
  94.     }
  95.     return(ntohl(*(UINT4 *)hp->h_addr));
  96. }
  97.  
  98. /*************************************************************************
  99.  *
  100.  *    Function: good_ipaddr
  101.  *
  102.  *    Purpose: Check for valid IP address in standard dot notation.
  103.  *
  104.  *************************************************************************/
  105. int
  106. good_ipaddr(addr)
  107. char    *addr;
  108. {
  109.     int    dot_count;
  110.     int    digit_count;
  111.  
  112.     dot_count = 0;
  113.     digit_count = 0;
  114.     while(*addr != '\0' && *addr != ' ') {
  115.         if(*addr == '.') {
  116.             dot_count++;
  117.             digit_count = 0;
  118.         }
  119.         else if(!isdigit(*addr)) {
  120.             dot_count = 5;
  121.         }
  122.         else {
  123.             digit_count++;
  124.             if(digit_count > 3) {
  125.                 dot_count = 5;
  126.             }
  127.         }
  128.         addr++;
  129.     }
  130.     if(dot_count != 3) {
  131.         return(-1);
  132.     }
  133.     else {
  134.         return(0);
  135.     }
  136. }
  137.  
  138. /*************************************************************************
  139.  *
  140.  *    Function: ipaddr2str
  141.  *
  142.  *    Purpose: Return an IP address in standard dot notation for the
  143.  *         provided address in host long notation.
  144.  *
  145.  *************************************************************************/
  146. void
  147. ipaddr2str(buffer, ipaddr)
  148. char    *buffer;
  149. UINT4    ipaddr;
  150. {
  151.     int    addr_byte[4];
  152.     int    i;
  153.     UINT4    xbyte;
  154.  
  155.     for(i = 0;i < 4;i++) {
  156.         xbyte = ipaddr >> (i*8);
  157.         xbyte = xbyte & (UINT4)0x000000FF;
  158.         addr_byte[i] = xbyte;
  159.     }
  160.     sprintf(buffer, "%u.%u.%u.%u", addr_byte[3], addr_byte[2],
  161.         addr_byte[1], addr_byte[0]);
  162. }
  163.  
  164. /*************************************************************************
  165.  *
  166.  *    Function: pairfree
  167.  *
  168.  *    Purpose: Release the memory used by a list of attribute-value
  169.  *         pairs.
  170.  *
  171.  *************************************************************************/
  172. void
  173. pairfree(pair)
  174. VALUE_PAIR    *pair;
  175. {
  176.     VALUE_PAIR    *next;
  177.  
  178.     while(pair != (VALUE_PAIR *)NULL) {
  179.         next = pair->next;
  180.         free(pair);
  181.         pair = next;
  182.     }
  183. }
  184.  
  185. /*************************************************************************
  186.  *
  187.  *    Function: ipstr2long
  188.  *
  189.  *    Purpose: Return an IP address in host long notation from
  190.  *         one supplied in standard dot notation.
  191.  *
  192.  *************************************************************************/
  193.  
  194. UINT4
  195. ipstr2long(ip_str)
  196. char    *ip_str;
  197. {
  198.     char    buf[6];
  199.     char    *ptr;
  200.     int    i;
  201.     int    count;
  202.     UINT4    ipaddr;
  203.     int    cur_byte;
  204.  
  205.     ipaddr = (UINT4)0;
  206.     for(i = 0;i < 4;i++) {
  207.         ptr = buf;
  208.         count = 0;
  209.         *ptr = '\0';
  210.         while(*ip_str != '.' && *ip_str != '\0' && count < 4) {
  211.             if(!isdigit(*ip_str)) {
  212.                 return((UINT4)0);
  213.             }
  214.             *ptr++ = *ip_str++;
  215.             count++;
  216.         }
  217.         if(count >= 4 || count == 0) {
  218.             return((UINT4)0);
  219.         }
  220.         *ptr = '\0';
  221.         cur_byte = atoi(buf);
  222.         if(cur_byte < 0 || cur_byte > 255) {
  223.             return((UINT4)0);
  224.         }
  225.         ip_str++;
  226.         ipaddr = ipaddr << 8 | (UINT4)cur_byte;
  227.     }
  228.     return(ipaddr);
  229. }
  230.  
  231.