1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
From 6bfa66069304c1fc1345b4e72762a3b1a80e4338 Mon Sep 17 00:00:00 2001
From: Tobias Brunner <tobias@strongswan.org>
Date: Thu, 11 Jun 2015 15:42:54 +0200
Subject: [PATCH] ike-cfg: Add helper function to determine address family of
IP addresses
All configured static addresses (hostnames, ranges or subnets are not
considered) must be of the same family, otherwise AF_UNSPEC is returned.
---
src/libcharon/config/ike_cfg.c | 47 ++++++++++++++++++++++++++++++++++++++++++
src/libcharon/config/ike_cfg.h | 13 +++++++++++-
2 files changed, 59 insertions(+), 1 deletion(-)
diff --git a/src/libcharon/config/ike_cfg.c b/src/libcharon/config/ike_cfg.c
index 9464ceb..dee9e4c 100644
--- a/src/libcharon/config/ike_cfg.c
+++ b/src/libcharon/config/ike_cfg.c
@@ -1,4 +1,5 @@
/*
+ * Copyright (C) 2012-2015 Tobias Brunner
* Copyright (C) 2005-2007 Martin Willi
* Copyright (C) 2005 Jan Hutter
* Hochschule fuer Technik Rapperswil
@@ -513,6 +514,52 @@ static void parse_addresses(char *str, linked_list_t *hosts,
/**
* Described in header.
*/
+int ike_cfg_get_family(ike_cfg_t *cfg, bool local)
+{
+ private_ike_cfg_t *this = (private_ike_cfg_t*)cfg;
+ enumerator_t *enumerator;
+ host_t *host;
+ char *str;
+ int family = AF_UNSPEC;
+
+ if (local)
+ {
+ enumerator = this->my_hosts->create_enumerator(this->my_hosts);
+ }
+ else
+ {
+ enumerator = this->other_hosts->create_enumerator(this->other_hosts);
+ }
+ while (enumerator->enumerate(enumerator, &str))
+ {
+ if (streq(str, "%any"))
+ { /* ignore %any as its family is undetermined */
+ continue;
+ }
+ host = host_create_from_string(str, 0);
+ if (host)
+ {
+ if (family == AF_UNSPEC)
+ {
+ family = host->get_family(host);
+ }
+ else if (family != host->get_family(host))
+ {
+ /* more than one address family defined */
+ family = AF_UNSPEC;
+ host->destroy(host);
+ break;
+ }
+ }
+ DESTROY_IF(host);
+ }
+ enumerator->destroy(enumerator);
+ return family;
+}
+
+/**
+ * Described in header.
+ */
ike_cfg_t *ike_cfg_create(ike_version_t version, bool certreq, bool force_encap,
char *me, u_int16_t my_port,
char *other, u_int16_t other_port,
diff --git a/src/libcharon/config/ike_cfg.h b/src/libcharon/config/ike_cfg.h
index adfcabf..62f5b74 100644
--- a/src/libcharon/config/ike_cfg.h
+++ b/src/libcharon/config/ike_cfg.h
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2012 Tobias Brunner
+ * Copyright (C) 2012-2015 Tobias Brunner
* Copyright (C) 2005-2007 Martin Willi
* Copyright (C) 2005 Jan Hutter
* Hochschule fuer Technik Rapperswil
@@ -254,4 +254,15 @@ ike_cfg_t *ike_cfg_create(ike_version_t version, bool certreq, bool force_encap,
char *other, u_int16_t other_port,
fragmentation_t fragmentation, u_int8_t dscp);
+/**
+ * Determine the address family of the local or remtoe address(es). If multiple
+ * families are configured AF_UNSPEC is returned. %any is ignored (%any4|6 are
+ * not though).
+ *
+ * @param local TRUE to check local addresses, FALSE for remote
+ * @return address family of address(es) if distinct
+ */
+int ike_cfg_get_family(ike_cfg_t *this, bool local);
+
+
#endif /** IKE_CFG_H_ @}*/
--
2.4.6
|