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
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.
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.
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/>.
26 #include "system/network.h"
27 #include "system/locale.h"
28 #include "system/filesys.h"
31 Interpret an internet address or name into an IP address in 4 byte form.
33 _PUBLIC_ uint32_t interpret_addr(const char *str)
38 if (str == NULL || *str == 0 ||
39 strcmp(str,"0.0.0.0") == 0) {
42 if (strcmp(str,"255.255.255.255") == 0) {
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) {
51 /* if it's in the form of an IP address then get the lib to interpret it */
52 if (is_ipaddress(str)) {
55 /* otherwise assume it's a network name of some sort and use
57 if ((hp = sys_gethostbyname(str)) == 0) {
58 DEBUG(3,("sys_gethostbyname: Unknown host. %s\n",str));
62 if(hp->h_addr == NULL) {
63 DEBUG(3,("sys_gethostbyname: host address is invalid for host %s\n",str));
66 memcpy((char *)&res,(char *)hp->h_addr, 4);
69 if (res == (uint32_t)-1)
76 A convenient addition to interpret_addr().
78 _PUBLIC_ struct in_addr interpret_addr2(const char *str)
81 uint32_t a = interpret_addr(str);
87 Check if an IP is the 0.0.0.0.
90 _PUBLIC_ bool is_zero_ip(struct in_addr ip)
92 return ip.s_addr == 0;
96 Are two IPs on the same subnet?
99 _PUBLIC_ bool same_net(struct in_addr ip1, struct in_addr ip2, struct in_addr mask)
101 uint32_t net1,net2,nmask;
103 nmask = ntohl(mask.s_addr);
104 net1 = ntohl(ip1.s_addr);
105 net2 = ntohl(ip2.s_addr);
107 return((net1 & nmask) == (net2 & nmask));
111 Return true if a string could be a pure IP address.
114 _PUBLIC_ bool is_ipaddress(const char *str)
116 bool pure_address = true;
119 if (str == NULL) return false;
121 for (i=0; pure_address && str[i]; i++)
122 if (!(isdigit((int)str[i]) || str[i] == '.'))
123 pure_address = false;
125 /* Check that a pure number is not misinterpreted as an IP */
126 pure_address = pure_address && (strchr(str, '.') != NULL);