1 /*
2  * Copyright (C) 2013, 2014 Nikos Mavrogiannopoulos
3  * Copyright (C) 2014 Red Hat, Inc.
4  *
5  * Author: Nikos Mavrogiannopoulos
6  *
7  * This file is part of ocserv.
8  *
9  * ocserv is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1 of
12  * the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>
21  */
22 #ifndef IP_UTIL_H
23 # define IP_UTIL_H
24 
25 #include <sys/socket.h>
26 #include <netinet/in.h>
27 
28 #define MAX_IP_STR 46
29 // Lower MTU bound is the value defined in RFC 791
30 #define RFC_791_MTU (68)
31 // Upper bound is the maximum DTLS frame size
32 #define MAX_DTLS_MTU (1<<14)
33 
34 void set_mtu_disc(int fd, int family, int val);
35 int ip_route_sanity_check(void *pool, char **_route);
36 
37 int ip_cmp(const struct sockaddr_storage *s1, const struct sockaddr_storage *s2);
38 char* ipv4_prefix_to_strmask(void *pool, unsigned prefix);
39 unsigned ipv6_prefix_to_mask(struct in6_addr *in6, unsigned prefix);
valid_ipv6_prefix(unsigned prefix)40 inline static int valid_ipv6_prefix(unsigned prefix)
41 {
42 	if (prefix > 10 && prefix <= 128)
43 		return 1;
44 	else
45 		return 0;
46 }
47 
48 char *ipv4_route_to_cidr(void *pool, const char *route);
49 
50 /* Helper casts */
51 #define SA_IN_P(p) (&((struct sockaddr_in *)(p))->sin_addr)
52 #define SA_IN_U8_P(p) ((uint8_t*)(&((struct sockaddr_in *)(p))->sin_addr))
53 #define SA_IN6_P(p) (&((struct sockaddr_in6 *)(p))->sin6_addr)
54 #define SA_IN6_U8_P(p) ((uint8_t*)(&((struct sockaddr_in6 *)(p))->sin6_addr))
55 
56 #define SA_IN_PORT(p) (((struct sockaddr_in *)(p))->sin_port)
57 #define SA_IN6_PORT(p) (((struct sockaddr_in6 *)(p))->sin6_port)
58 
59 #define SA_IN_P_GENERIC(addr, size) ((size==sizeof(struct sockaddr_in))?SA_IN_U8_P(addr):SA_IN6_U8_P(addr))
60 #define SA_IN_P_TYPE(addr, type) ((type==AF_INET)?SA_IN_U8_P(addr):SA_IN6_U8_P(addr))
61 #define SA_IN_SIZE(size) ((size==sizeof(struct sockaddr_in))?sizeof(struct in_addr):sizeof(struct in6_addr))
62 
63 char *human_addr2(const struct sockaddr *sa, socklen_t salen,
64 		       void *buf, size_t buflen, unsigned full);
65 
66 #define human_addr(x, y, z, w) human_addr2(x, y, z, w, 1)
67 
68 #endif
69