1 /*
2    Unix SMB/CIFS implementation.
3    Utility functions for Samba
4    Copyright (C) Andrew Tridgell 1992-1999
5    Copyright (C) Jelmer Vernooij 2005
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #ifndef _SAMBA_UTIL_NET_H_
22 #define _SAMBA_UTIL_NET_H_
23 
24 #include "system/network.h"
25 
26 struct samba_sockaddr {
27 	socklen_t sa_socklen;
28 	union {
29 		struct sockaddr sa;
30 		struct sockaddr_in in;
31 #ifdef HAVE_IPV6
32 		struct sockaddr_in6 in6;
33 #endif
34 		struct sockaddr_un un;
35 		struct sockaddr_storage ss;
36 	} u;
37 };
38 
39 /* The following definitions come from lib/util/util_net.c  */
40 
41 void zero_sockaddr(struct sockaddr_storage *pss);
42 
43 bool interpret_string_addr_internal(struct addrinfo **ppres,
44 				    const char *str, int flags);
45 
46 bool interpret_string_addr(struct sockaddr_storage *pss,
47 			   const char *str,
48 			   int flags);
49 
50 /*******************************************************************
51  Map a text hostname or IP address (IPv4 or IPv6) into a
52  struct sockaddr_storage. Version that prefers IPv4.
53 ******************************************************************/
54 
55 bool interpret_string_addr_prefer_ipv4(struct sockaddr_storage *pss,
56 				       const char *str,
57 				       int flags);
58 
59 void set_sockaddr_port(struct sockaddr *psa, uint16_t port);
60 
61 /**
62  Check if an IP is the 0.0.0.0.
63 **/
64 _PUBLIC_ bool is_zero_ip_v4(struct in_addr ip);
65 
66 void in_addr_to_sockaddr_storage(struct sockaddr_storage *ss,
67 				 struct in_addr ip);
68 #if defined(HAVE_IPV6)
69 /**
70  * Convert an IPv6 struct in_addr to a struct sockaddr_storage.
71  */
72 void in6_addr_to_sockaddr_storage(struct sockaddr_storage *ss,
73 				  struct in6_addr ip);
74 #endif
75 /**
76  Are two IPs on the same subnet?
77 **/
78 _PUBLIC_ bool same_net_v4(struct in_addr ip1,struct in_addr ip2,struct in_addr mask);
79 
80 /**
81  Return true if a string could be a pure IP address.
82 **/
83 _PUBLIC_ bool is_ipaddress(const char *str);
84 
85 bool is_broadcast_addr(const struct sockaddr *pss);
86 bool is_loopback_ip_v4(struct in_addr ip);
87 bool is_loopback_addr(const struct sockaddr *pss);
88 bool is_zero_addr(const struct sockaddr_storage *pss);
89 void zero_ip_v4(struct in_addr *ip);
90 bool is_linklocal_addr(const struct sockaddr_storage *pss);
91 /**
92  Interpret an internet address or name into an IP address in 4 byte form.
93 **/
94 _PUBLIC_ uint32_t interpret_addr(const char *str);
95 
96 /**
97  A convenient addition to interpret_addr().
98 **/
99 _PUBLIC_ struct in_addr interpret_addr2(const char *str);
100 
101 _PUBLIC_ bool is_ipaddress_v4(const char *str);
102 _PUBLIC_ bool is_ipv6_literal(const char *str);
103 _PUBLIC_ bool is_ipaddress_v6(const char *str);
104 
105 bool is_address_any(const struct sockaddr *psa);
106 bool same_net(const struct sockaddr *ip1,
107 	      const struct sockaddr *ip2,
108 	      const struct sockaddr *mask);
109 bool sockaddr_equal(const struct sockaddr *ip1,
110 		    const struct sockaddr *ip2);
111 
112 bool is_address_any(const struct sockaddr *psa);
113 uint16_t get_sockaddr_port(const struct sockaddr_storage *pss);
114 char *print_sockaddr_len(char *dest,
115 			 size_t destlen,
116 			 const struct sockaddr *psa,
117 			 socklen_t psalen);
118 char *print_sockaddr(char *dest,
119 			size_t destlen,
120 			const struct sockaddr_storage *psa);
121 char *print_canonical_sockaddr(TALLOC_CTX *ctx,
122 			const struct sockaddr_storage *pss);
123 int get_socket_port(int fd);
124 const char *client_socket_addr(int fd, char *addr, size_t addr_len);
125 
126 void set_socket_options(int fd, const char *options);
127 
128 #endif /* _SAMBA_UTIL_NET_H_ */
129