Fix const, dupes.
[nivanova/samba-autobuild/.git] / lib / util / util_net.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Samba utility functions
4    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008
5    Copyright (C) Andrew Tridgell 1992-1998
6    Copyright (C) Jeremy Allison 2001-2002
7    Copyright (C) Simo Sorce 2001
8    Copyright (C) Jim McDonough (jmcd@us.ibm.com)  2003.
9    Copyright (C) James J Myers 2003
10     
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 3 of the License, or
14    (at your option) any later version.
15    
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20    
21    You should have received a copy of the GNU General Public License
22    along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 */
24
25 #include "includes.h"
26 #include "system/network.h"
27 #include "system/locale.h"
28 #include "system/filesys.h"
29
30 /**
31  Interpret an internet address or name into an IP address in 4 byte form.
32 **/
33 _PUBLIC_ uint32_t interpret_addr(const char *str)
34 {
35         struct hostent *hp;
36         uint32_t res;
37
38         if (str == NULL || *str == 0 ||
39             strcmp(str,"0.0.0.0") == 0) {
40                 return 0;
41         }
42         if (strcmp(str,"255.255.255.255") == 0) {
43                 return 0xFFFFFFFF;
44         }
45         /* recognise 'localhost' as a special name. This fixes problems with
46            some hosts that don't have localhost in /etc/hosts */
47         if (strcasecmp(str,"localhost") == 0) {
48                 str = "127.0.0.1";
49         }
50
51         /* if it's in the form of an IP address then get the lib to interpret it */
52         if (is_ipaddress(str)) {
53                 res = inet_addr(str);
54         } else {
55                 /* otherwise assume it's a network name of some sort and use 
56                         sys_gethostbyname */
57                 if ((hp = sys_gethostbyname(str)) == 0) {
58                         DEBUG(3,("sys_gethostbyname: Unknown host. %s\n",str));
59                         return 0;
60                 }
61
62                 if(hp->h_addr == NULL) {
63                         DEBUG(3,("sys_gethostbyname: host address is invalid for host %s\n",str));
64                         return 0;
65                 }
66                 memcpy((char *)&res,(char *)hp->h_addr, 4);
67         }
68
69         if (res == (uint32_t)-1)
70                 return(0);
71
72         return(res);
73 }
74
75 /**
76  A convenient addition to interpret_addr().
77 **/
78 _PUBLIC_ struct in_addr interpret_addr2(const char *str)
79 {
80         struct in_addr ret;
81         uint32_t a = interpret_addr(str);
82         ret.s_addr = a;
83         return ret;
84 }
85
86 /**
87  Check if an IP is the 0.0.0.0.
88 **/
89
90 _PUBLIC_ bool is_zero_ip(struct in_addr ip)
91 {
92         return ip.s_addr == 0;
93 }
94
95 /**
96  Are two IPs on the same subnet?
97 **/
98
99 _PUBLIC_ bool same_net(struct in_addr ip1, struct in_addr ip2, struct in_addr mask)
100 {
101         uint32_t net1,net2,nmask;
102
103         nmask = ntohl(mask.s_addr);
104         net1  = ntohl(ip1.s_addr);
105         net2  = ntohl(ip2.s_addr);
106             
107         return((net1 & nmask) == (net2 & nmask));
108 }
109
110 /**
111  Return true if a string could be a pure IP address.
112 **/
113
114 _PUBLIC_ bool is_ipaddress(const char *str)
115 {
116         bool pure_address = true;
117         int i;
118
119         if (str == NULL) return false;
120
121         for (i=0; pure_address && str[i]; i++)
122                 if (!(isdigit((int)str[i]) || str[i] == '.'))
123                         pure_address = false;
124
125         /* Check that a pure number is not misinterpreted as an IP */
126         pure_address = pure_address && (strchr(str, '.') != NULL);
127
128         return pure_address;
129 }
130
131