4e65df07ff0db94cd937ce336c85ff2ff5b2546e
[freeradius.git] / src / lib / misc.c
1 /*
2  * misc.c       Various miscellaneous functions.
3  *
4  * Version:     @(#)misc.c  1.00  19-Jul-1999  miquels@cistron.nl
5  */
6
7 char misc_sccsid[] =
8 "@(#)misc.c     1.00 Copyright 1999 Cistron Internet Services B.V.";
9
10 #include        "autoconf.h"
11
12 #include        <sys/types.h>
13 #include        <sys/socket.h>
14 #include        <sys/time.h>
15 #include        <netinet/in.h>
16 #include        <arpa/inet.h>
17
18 #include        <stdio.h>
19 #include        <stdlib.h>
20 #include        <string.h>
21 #include        <netdb.h>
22 #include        <pwd.h>
23 #include        <time.h>
24 #include        <ctype.h>
25 #include        <signal.h>
26
27 #include        "libradius.h"
28 #include    "missing.h"
29
30 int             librad_dodns = 1;
31 int             librad_debug = 0;
32
33 /*
34  *      Return a printable host name (or IP address in dot notation)
35  *      for the supplied IP address.
36  */
37 char * ip_hostname(UINT4 ipaddr)
38 {
39         struct          hostent *hp;
40         static char     hstname[128];
41
42         hp = gethostbyaddr((char *)&ipaddr, sizeof (struct in_addr), AF_INET);
43         if (hp == 0) {
44                 ip_ntoa(hstname, ipaddr);
45                 return(hstname);
46         }
47         return (char *)hp->h_name;
48 }
49
50
51 /*
52  *      Return an IP address in from a host
53  *      name or address in dot notation.
54  */
55 UINT4 ip_getaddr(char *host)
56 {
57         struct hostent  *hp;
58         UINT4           a, ip_addr();
59
60         if ((a = ip_addr(host)) != 0)
61                 return a;
62
63         if ((hp = gethostbyname(host)) == NULL)
64                 return (UINT4)0;
65
66         return (*(UINT4 *)hp->h_addr);
67 }
68
69
70 /*
71  *      Return an IP address in standard dot notation
72  */
73 char *ip_ntoa(char *buffer, UINT4 ipaddr)
74 {
75         struct in_addr  in;
76         char            *r;
77
78         in.s_addr = ipaddr;
79         r = inet_ntoa(in);
80
81         if (buffer)
82                 strcpy(buffer, r);
83         return buffer ? buffer : r;
84 }
85
86
87 /*
88  *      Return an IP address from
89  *      one supplied in standard dot notation.
90  */
91 UINT4 ip_addr(char *ip_str)
92 {
93         struct in_addr  in;
94
95         if (inet_aton(ip_str, &in) == 0)
96                 return 0;
97         return in.s_addr;
98 }
99