From 2e7cc07ecde933f52d2d16bf84596ba6da66ebc9 Mon Sep 17 00:00:00 2001 From: Tobias Brunner Date: Tue, 16 Oct 2012 14:29:18 +0200 Subject: Moved host_t and host_resolver_t to a new networking subfolder --- src/charon-nm/nm/nm_service.c | 2 +- src/dumm/iface.h | 2 +- .../jni/libandroidbridge/kernel/network_manager.h | 2 +- .../jni/libandroidbridge/vpnservice_builder.h | 2 +- src/libcharon/config/backend_manager.h | 2 +- src/libcharon/config/ike_cfg.h | 2 +- src/libcharon/config/proposal.h | 2 +- .../payloads/traffic_selector_substructure.h | 2 +- src/libcharon/network/receiver.h | 2 +- src/libcharon/plugins/android/android_handler.c | 2 +- src/libcharon/plugins/dhcp/dhcp_transaction.h | 2 +- src/libcharon/plugins/farp/farp_listener.h | 2 +- src/libcharon/plugins/ha/ha_message.h | 2 +- src/libcharon/plugins/ha/ha_socket.c | 2 +- src/libcharon/plugins/tnc_ifmap/tnc_ifmap_soap.h | 8 +- src/libcharon/processing/jobs/migrate_job.h | 2 +- src/libcharon/processing/jobs/update_sa_job.h | 2 +- src/libhydra/attributes/attribute_provider.h | 2 +- src/libhydra/attributes/mem_pool.h | 2 +- src/libhydra/kernel/kernel_interface.h | 2 +- src/libhydra/kernel/kernel_ipsec.h | 2 +- src/libhydra/kernel/kernel_listener.h | 2 +- src/libhydra/kernel/kernel_net.h | 2 +- src/libhydra/plugins/attr_sql/pool.c | 2 +- src/libhydra/plugins/attr_sql/pool_attributes.c | 2 +- .../plugins/kernel_pfkey/kernel_pfkey_ipsec.c | 2 +- .../plugins/kernel_pfroute/kernel_pfroute_net.c | 2 +- src/libipsec/esp_packet.h | 2 +- src/libipsec/ip_packet.h | 2 +- src/libipsec/ipsec_policy.h | 2 +- src/libipsec/ipsec_policy_mgr.h | 2 +- src/libipsec/ipsec_sa.h | 2 +- src/libipsec/ipsec_sa_mgr.h | 2 +- src/libradius/radius_client.c | 2 +- src/libradius/radius_socket.h | 2 +- src/libstrongswan/Android.mk | 5 +- src/libstrongswan/Makefile.am | 10 +- src/libstrongswan/host_resolver.c | 391 -------------- src/libstrongswan/host_resolver.h | 60 --- src/libstrongswan/library.c | 2 +- src/libstrongswan/library.h | 5 +- src/libstrongswan/networking/host.c | 578 +++++++++++++++++++++ src/libstrongswan/networking/host.h | 220 ++++++++ src/libstrongswan/networking/host_resolver.c | 391 ++++++++++++++ src/libstrongswan/networking/host_resolver.h | 60 +++ src/libstrongswan/selectors/traffic_selector.h | 2 +- src/libstrongswan/utils/host.c | 578 --------------------- src/libstrongswan/utils/host.h | 220 -------- src/libstrongswan/utils/packet.h | 2 +- src/libstrongswan/utils/tun_device.h | 2 +- src/manager/gateway.h | 2 +- 51 files changed, 1305 insertions(+), 1299 deletions(-) delete mode 100644 src/libstrongswan/host_resolver.c delete mode 100644 src/libstrongswan/host_resolver.h create mode 100644 src/libstrongswan/networking/host.c create mode 100644 src/libstrongswan/networking/host.h create mode 100644 src/libstrongswan/networking/host_resolver.c create mode 100644 src/libstrongswan/networking/host_resolver.h delete mode 100644 src/libstrongswan/utils/host.c delete mode 100644 src/libstrongswan/utils/host.h (limited to 'src') diff --git a/src/charon-nm/nm/nm_service.c b/src/charon-nm/nm/nm_service.c index 1eeaccaf6..6fa5e51a4 100644 --- a/src/charon-nm/nm/nm_service.c +++ b/src/charon-nm/nm/nm_service.c @@ -18,7 +18,7 @@ #include "nm_service.h" #include -#include +#include #include #include #include diff --git a/src/dumm/iface.h b/src/dumm/iface.h index e96ee508c..c43113abd 100644 --- a/src/dumm/iface.h +++ b/src/dumm/iface.h @@ -18,7 +18,7 @@ #include #include -#include +#include #define TAP_DEVICE "/dev/net/tun" diff --git a/src/frontends/android/jni/libandroidbridge/kernel/network_manager.h b/src/frontends/android/jni/libandroidbridge/kernel/network_manager.h index 634816405..a35103b89 100644 --- a/src/frontends/android/jni/libandroidbridge/kernel/network_manager.h +++ b/src/frontends/android/jni/libandroidbridge/kernel/network_manager.h @@ -24,7 +24,7 @@ #include #include -#include +#include typedef struct network_manager_t network_manager_t; diff --git a/src/frontends/android/jni/libandroidbridge/vpnservice_builder.h b/src/frontends/android/jni/libandroidbridge/vpnservice_builder.h index 82efd05f7..209090896 100644 --- a/src/frontends/android/jni/libandroidbridge/vpnservice_builder.h +++ b/src/frontends/android/jni/libandroidbridge/vpnservice_builder.h @@ -26,7 +26,7 @@ #include #include -#include +#include typedef struct vpnservice_builder_t vpnservice_builder_t; diff --git a/src/libcharon/config/backend_manager.h b/src/libcharon/config/backend_manager.h index 463a57bac..cc8ef8785 100644 --- a/src/libcharon/config/backend_manager.h +++ b/src/libcharon/config/backend_manager.h @@ -24,7 +24,7 @@ typedef struct backend_manager_t backend_manager_t; #include -#include +#include #include #include #include diff --git a/src/libcharon/config/ike_cfg.h b/src/libcharon/config/ike_cfg.h index 5f48e9660..dc5832927 100644 --- a/src/libcharon/config/ike_cfg.h +++ b/src/libcharon/config/ike_cfg.h @@ -26,7 +26,7 @@ typedef enum ike_version_t ike_version_t; typedef struct ike_cfg_t ike_cfg_t; #include -#include +#include #include #include #include diff --git a/src/libcharon/config/proposal.h b/src/libcharon/config/proposal.h index 33abf006c..ff6267dae 100644 --- a/src/libcharon/config/proposal.h +++ b/src/libcharon/config/proposal.h @@ -28,7 +28,7 @@ typedef struct proposal_t proposal_t; #include #include #include -#include +#include #include #include #include diff --git a/src/libcharon/encoding/payloads/traffic_selector_substructure.h b/src/libcharon/encoding/payloads/traffic_selector_substructure.h index 1ad5fb526..d3fbe8476 100644 --- a/src/libcharon/encoding/payloads/traffic_selector_substructure.h +++ b/src/libcharon/encoding/payloads/traffic_selector_substructure.h @@ -25,7 +25,7 @@ typedef struct traffic_selector_substructure_t traffic_selector_substructure_t; #include -#include +#include #include #include diff --git a/src/libcharon/network/receiver.h b/src/libcharon/network/receiver.h index 9e8edee45..82153613f 100644 --- a/src/libcharon/network/receiver.h +++ b/src/libcharon/network/receiver.h @@ -26,7 +26,7 @@ typedef struct receiver_t receiver_t; #include -#include +#include #include /** diff --git a/src/libcharon/plugins/android/android_handler.c b/src/libcharon/plugins/android/android_handler.c index c5f51ef04..c5e98d315 100644 --- a/src/libcharon/plugins/android/android_handler.c +++ b/src/libcharon/plugins/android/android_handler.c @@ -16,7 +16,7 @@ #include "android_handler.h" -#include +#include #include #include diff --git a/src/libcharon/plugins/dhcp/dhcp_transaction.h b/src/libcharon/plugins/dhcp/dhcp_transaction.h index 19c163f88..35f08e836 100644 --- a/src/libcharon/plugins/dhcp/dhcp_transaction.h +++ b/src/libcharon/plugins/dhcp/dhcp_transaction.h @@ -21,7 +21,7 @@ #ifndef DHCP_TRANSACTION_H_ #define DHCP_TRANSACTION_H_ -#include +#include #include #include diff --git a/src/libcharon/plugins/farp/farp_listener.h b/src/libcharon/plugins/farp/farp_listener.h index 3155f60e2..c7dc56a10 100644 --- a/src/libcharon/plugins/farp/farp_listener.h +++ b/src/libcharon/plugins/farp/farp_listener.h @@ -21,7 +21,7 @@ #ifndef FARP_LISTENER_H_ #define FARP_LISTENER_H_ -#include +#include #include typedef struct farp_listener_t farp_listener_t; diff --git a/src/libcharon/plugins/ha/ha_message.h b/src/libcharon/plugins/ha/ha_message.h index 8cd30f711..2ccb1fc55 100644 --- a/src/libcharon/plugins/ha/ha_message.h +++ b/src/libcharon/plugins/ha/ha_message.h @@ -22,7 +22,7 @@ #define HA_MESSAGE_H_ #include -#include +#include #include #include #include diff --git a/src/libcharon/plugins/ha/ha_socket.c b/src/libcharon/plugins/ha/ha_socket.c index 5196a5dc7..e41e78bbf 100644 --- a/src/libcharon/plugins/ha/ha_socket.c +++ b/src/libcharon/plugins/ha/ha_socket.c @@ -22,7 +22,7 @@ #include #include -#include +#include #include #include diff --git a/src/libcharon/plugins/tnc_ifmap/tnc_ifmap_soap.h b/src/libcharon/plugins/tnc_ifmap/tnc_ifmap_soap.h index 4bf421e33..4efdc779f 100644 --- a/src/libcharon/plugins/tnc_ifmap/tnc_ifmap_soap.h +++ b/src/libcharon/plugins/tnc_ifmap/tnc_ifmap_soap.h @@ -15,14 +15,14 @@ /** * @defgroup tnc_ifmap_soap tnc_ifmap_soap - * @{ @ingroup tnc_ifmap + * @{ @ingroup tnc_ifmap */ #ifndef TNC_IFMAP_SOAP_H_ #define TNC_IFMAP_SOAP_H_ #include -#include +#include #include typedef struct tnc_ifmap_soap_t tnc_ifmap_soap_t; @@ -47,7 +47,7 @@ struct tnc_ifmap_soap_t { bool (*purgePublisher)(tnc_ifmap_soap_t *this); /** - * Publish metadata about established/deleted IKE_SAs + * Publish metadata about established/deleted IKE_SAs * * @param ike_sa IKE_SA for which metadate is published * @param up TRUE if IKE_SEA is up, FALSE if down @@ -56,7 +56,7 @@ struct tnc_ifmap_soap_t { bool (*publish_ike_sa)(tnc_ifmap_soap_t *this, ike_sa_t *ike_sa, bool up); /** - * Publish PEP device-ip metadata + * Publish PEP device-ip metadata * * @param host IP address of local endpoint * @return TRUE if command was successful diff --git a/src/libcharon/processing/jobs/migrate_job.h b/src/libcharon/processing/jobs/migrate_job.h index 09679c734..30c0ad0ac 100644 --- a/src/libcharon/processing/jobs/migrate_job.h +++ b/src/libcharon/processing/jobs/migrate_job.h @@ -24,7 +24,7 @@ typedef struct migrate_job_t migrate_job_t; #include -#include +#include #include #include #include diff --git a/src/libcharon/processing/jobs/update_sa_job.h b/src/libcharon/processing/jobs/update_sa_job.h index e2344fcc4..55a3df83e 100644 --- a/src/libcharon/processing/jobs/update_sa_job.h +++ b/src/libcharon/processing/jobs/update_sa_job.h @@ -24,7 +24,7 @@ typedef struct update_sa_job_t update_sa_job_t; #include -#include +#include #include /** diff --git a/src/libhydra/attributes/attribute_provider.h b/src/libhydra/attributes/attribute_provider.h index e5e556fc4..82d0ee3c2 100644 --- a/src/libhydra/attributes/attribute_provider.h +++ b/src/libhydra/attributes/attribute_provider.h @@ -21,7 +21,7 @@ #ifndef ATTRIBUTE_PROVIDER_H_ #define ATTRIBUTE_PROVIDER_H_ -#include +#include #include #include diff --git a/src/libhydra/attributes/mem_pool.h b/src/libhydra/attributes/mem_pool.h index fa4e6485c..692885ecd 100644 --- a/src/libhydra/attributes/mem_pool.h +++ b/src/libhydra/attributes/mem_pool.h @@ -24,7 +24,7 @@ typedef struct mem_pool_t mem_pool_t; typedef enum mem_pool_op_t mem_pool_op_t; -#include +#include #include /** diff --git a/src/libhydra/kernel/kernel_interface.h b/src/libhydra/kernel/kernel_interface.h index 88d4a5bce..7058466b1 100644 --- a/src/libhydra/kernel/kernel_interface.h +++ b/src/libhydra/kernel/kernel_interface.h @@ -48,7 +48,7 @@ typedef struct kernel_interface_t kernel_interface_t; -#include +#include #include #include diff --git a/src/libhydra/kernel/kernel_ipsec.h b/src/libhydra/kernel/kernel_ipsec.h index ee0ade2aa..1da0805cb 100644 --- a/src/libhydra/kernel/kernel_ipsec.h +++ b/src/libhydra/kernel/kernel_ipsec.h @@ -26,7 +26,7 @@ typedef struct kernel_ipsec_t kernel_ipsec_t; -#include +#include #include #include #include diff --git a/src/libhydra/kernel/kernel_listener.h b/src/libhydra/kernel/kernel_listener.h index 5db297b6f..27ea947eb 100644 --- a/src/libhydra/kernel/kernel_listener.h +++ b/src/libhydra/kernel/kernel_listener.h @@ -25,7 +25,7 @@ typedef struct kernel_listener_t kernel_listener_t; #include #include -#include +#include /** * Interface for components interested in kernel events. diff --git a/src/libhydra/kernel/kernel_net.h b/src/libhydra/kernel/kernel_net.h index 10350d644..0de93eaee 100644 --- a/src/libhydra/kernel/kernel_net.h +++ b/src/libhydra/kernel/kernel_net.h @@ -26,7 +26,7 @@ typedef struct kernel_net_t kernel_net_t; typedef enum kernel_address_type_t kernel_address_type_t; #include -#include +#include #include /** diff --git a/src/libhydra/plugins/attr_sql/pool.c b/src/libhydra/plugins/attr_sql/pool.c index a2000cffe..1d5fddbb7 100644 --- a/src/libhydra/plugins/attr_sql/pool.c +++ b/src/libhydra/plugins/attr_sql/pool.c @@ -23,7 +23,7 @@ #include #include -#include +#include #include #include diff --git a/src/libhydra/plugins/attr_sql/pool_attributes.c b/src/libhydra/plugins/attr_sql/pool_attributes.c index d3fc06eeb..5dcfe85ed 100644 --- a/src/libhydra/plugins/attr_sql/pool_attributes.c +++ b/src/libhydra/plugins/attr_sql/pool_attributes.c @@ -17,7 +17,7 @@ #include #include -#include +#include #include "pool_attributes.h" #include "pool_usage.h" diff --git a/src/libhydra/plugins/kernel_pfkey/kernel_pfkey_ipsec.c b/src/libhydra/plugins/kernel_pfkey/kernel_pfkey_ipsec.c index b099bc714..ed541ad67 100644 --- a/src/libhydra/plugins/kernel_pfkey/kernel_pfkey_ipsec.c +++ b/src/libhydra/plugins/kernel_pfkey/kernel_pfkey_ipsec.c @@ -59,7 +59,7 @@ #include #include -#include +#include #include #include #include diff --git a/src/libhydra/plugins/kernel_pfroute/kernel_pfroute_net.c b/src/libhydra/plugins/kernel_pfroute/kernel_pfroute_net.c index 47a8829f7..0ead65785 100644 --- a/src/libhydra/plugins/kernel_pfroute/kernel_pfroute_net.c +++ b/src/libhydra/plugins/kernel_pfroute/kernel_pfroute_net.c @@ -25,7 +25,7 @@ #include #include -#include +#include #include #include #include diff --git a/src/libipsec/esp_packet.h b/src/libipsec/esp_packet.h index a1d1602c1..4586379b0 100644 --- a/src/libipsec/esp_packet.h +++ b/src/libipsec/esp_packet.h @@ -27,7 +27,7 @@ #include "esp_context.h" #include -#include +#include #include typedef struct esp_packet_t esp_packet_t; diff --git a/src/libipsec/ip_packet.h b/src/libipsec/ip_packet.h index b4fc298ff..722662af7 100644 --- a/src/libipsec/ip_packet.h +++ b/src/libipsec/ip_packet.h @@ -22,7 +22,7 @@ #define IP_PACKET_H_ #include -#include +#include #include typedef struct ip_packet_t ip_packet_t; diff --git a/src/libipsec/ipsec_policy.h b/src/libipsec/ipsec_policy.h index 67ad0b0ed..23a9ea99d 100644 --- a/src/libipsec/ipsec_policy.h +++ b/src/libipsec/ipsec_policy.h @@ -26,7 +26,7 @@ #include "ip_packet.h" #include -#include +#include #include #include diff --git a/src/libipsec/ipsec_policy_mgr.h b/src/libipsec/ipsec_policy_mgr.h index d3ee1074f..ae0a29868 100644 --- a/src/libipsec/ipsec_policy_mgr.h +++ b/src/libipsec/ipsec_policy_mgr.h @@ -27,7 +27,7 @@ #include "ip_packet.h" #include -#include +#include #include #include #include diff --git a/src/libipsec/ipsec_sa.h b/src/libipsec/ipsec_sa.h index 271e0129f..dec688e68 100644 --- a/src/libipsec/ipsec_sa.h +++ b/src/libipsec/ipsec_sa.h @@ -26,7 +26,7 @@ #include "esp_context.h" #include -#include +#include #include #include diff --git a/src/libipsec/ipsec_sa_mgr.h b/src/libipsec/ipsec_sa_mgr.h index db30a86bb..3ff092038 100644 --- a/src/libipsec/ipsec_sa_mgr.h +++ b/src/libipsec/ipsec_sa_mgr.h @@ -28,7 +28,7 @@ #include #include #include -#include +#include typedef struct ipsec_sa_mgr_t ipsec_sa_mgr_t; diff --git a/src/libradius/radius_client.c b/src/libradius/radius_client.c index acdac78c9..143cfa1a5 100644 --- a/src/libradius/radius_client.c +++ b/src/libradius/radius_client.c @@ -20,7 +20,7 @@ #include #include -#include +#include #include #include #include diff --git a/src/libradius/radius_socket.h b/src/libradius/radius_socket.h index 07d642c08..eb510ea89 100644 --- a/src/libradius/radius_socket.h +++ b/src/libradius/radius_socket.h @@ -25,7 +25,7 @@ typedef struct radius_socket_t radius_socket_t; #include "radius_message.h" -#include +#include /** * RADIUS socket to a server. diff --git a/src/libstrongswan/Android.mk b/src/libstrongswan/Android.mk index 9c7ef1d0a..fb224a28b 100644 --- a/src/libstrongswan/Android.mk +++ b/src/libstrongswan/Android.mk @@ -3,7 +3,7 @@ include $(CLEAR_VARS) # copy-n-paste from Makefile.am LOCAL_SRC_FILES := \ -library.c chunk.c debug.c enum.c host_resolver.c settings.c printf_hook.c \ +library.c chunk.c debug.c enum.c settings.c printf_hook.c \ asn1/asn1.c asn1/asn1_parser.c asn1/oid.c bio/bio_reader.c bio/bio_writer.c \ crypto/crypters/crypter.c crypto/hashers/hasher.c crypto/pkcs7.c crypto/pkcs9.c \ crypto/proposal/proposal_keywords.c crypto/proposal/proposal_keywords_static.c \ @@ -22,11 +22,12 @@ credentials/sets/cert_cache.c credentials/sets/mem_cred.c \ credentials/sets/callback_cred.c credentials/auth_cfg.c database/database.c \ database/database_factory.c fetcher/fetcher.c fetcher/fetcher_manager.c eap/eap.c \ ipsec/ipsec_types.c \ +networking/host.c networking/host_resolver.c \ pen/pen.c plugins/plugin_loader.c plugins/plugin_feature.c processing/jobs/job.c \ processing/jobs/callback_job.c processing/processor.c processing/scheduler.c \ selectors/traffic_selector.c threading/thread.c threading/thread_value.c \ threading/mutex.c threading/semaphore.c threading/rwlock.c threading/spinlock.c \ -utils.c utils/host.c utils/packet.c utils/identification.c utils/lexparser.c \ +utils.c utils/packet.c utils/identification.c utils/lexparser.c \ utils/linked_list.c utils/blocking_queue.c utils/hashtable.c utils/enumerator.c \ utils/optionsfrom.c utils/capabilities.c utils/backtrace.c utils/tun_device.c diff --git a/src/libstrongswan/Makefile.am b/src/libstrongswan/Makefile.am index 4017bfcc2..4b8279f7a 100644 --- a/src/libstrongswan/Makefile.am +++ b/src/libstrongswan/Makefile.am @@ -1,7 +1,7 @@ ipseclib_LTLIBRARIES = libstrongswan.la libstrongswan_la_SOURCES = \ -library.c chunk.c debug.c enum.c host_resolver.c settings.c printf_hook.c \ +library.c chunk.c debug.c enum.c settings.c printf_hook.c \ asn1/asn1.c asn1/asn1_parser.c asn1/oid.c bio/bio_reader.c bio/bio_writer.c \ crypto/crypters/crypter.c crypto/hashers/hasher.c crypto/pkcs7.c crypto/pkcs9.c \ crypto/proposal/proposal_keywords.c crypto/proposal/proposal_keywords_static.c \ @@ -20,18 +20,19 @@ credentials/sets/cert_cache.c credentials/sets/mem_cred.c \ credentials/sets/callback_cred.c credentials/auth_cfg.c database/database.c \ database/database_factory.c fetcher/fetcher.c fetcher/fetcher_manager.c eap/eap.c \ ipsec/ipsec_types.c \ +networking/host.c networking/host_resolver.c \ pen/pen.c plugins/plugin_loader.c plugins/plugin_feature.c processing/jobs/job.c \ processing/jobs/callback_job.c processing/processor.c processing/scheduler.c \ selectors/traffic_selector.c threading/thread.c threading/thread_value.c \ threading/mutex.c threading/semaphore.c threading/rwlock.c threading/spinlock.c \ -utils.c utils/host.c utils/packet.c utils/identification.c utils/lexparser.c \ +utils.c utils/packet.c utils/identification.c utils/lexparser.c \ utils/linked_list.c utils/blocking_queue.c utils/hashtable.c utils/enumerator.c \ utils/optionsfrom.c utils/capabilities.c utils/backtrace.c utils/tun_device.c if USE_DEV_HEADERS strongswan_includedir = ${dev_headers} nobase_strongswan_include_HEADERS = \ -library.h chunk.h debug.h enum.h host_resolver.h settings.h printf_hook.h \ +library.h chunk.h debug.h enum.h settings.h printf_hook.h \ asn1/asn1.h asn1/asn1_parser.h asn1/oid.h bio/bio_reader.h bio/bio_writer.h \ crypto/crypters/crypter.h crypto/hashers/hasher.h crypto/mac.h \ crypto/pkcs7.h crypto/pkcs9.h crypto/proposal/proposal_keywords.h \ @@ -55,13 +56,14 @@ credentials/sets/mem_cred.h credentials/sets/callback_cred.h \ credentials/auth_cfg.h credentials/credential_set.h credentials/cert_validator.h \ database/database.h database/database_factory.h fetcher/fetcher.h \ fetcher/fetcher_manager.h eap/eap.h pen/pen.h ipsec/ipsec_types.h \ +networking/host.h networking/host_resolver.h \ plugins/plugin_loader.h plugins/plugin.h plugins/plugin_feature.h \ processing/jobs/job.h processing/jobs/callback_job.h processing/processor.h \ processing/scheduler.h selectors/traffic_selector.h \ threading/thread.h threading/thread_value.h \ threading/mutex.h threading/condvar.h threading/spinlock.h threading/semaphore.h \ threading/rwlock.h threading/rwlock_condvar.h threading/lock_profiler.h \ -utils.h utils/host.h utils/packet.h utils/identification.h utils/lexparser.h \ +utils.h utils/packet.h utils/identification.h utils/lexparser.h \ utils/linked_list.h utils/blocking_queue.h utils/hashtable.h utils/enumerator.h \ utils/optionsfrom.h utils/capabilities.h utils/backtrace.h utils/tun_device.h \ utils/leak_detective.h integrity_checker.h diff --git a/src/libstrongswan/host_resolver.c b/src/libstrongswan/host_resolver.c deleted file mode 100644 index 55b07d318..000000000 --- a/src/libstrongswan/host_resolver.c +++ /dev/null @@ -1,391 +0,0 @@ -/* - * Copyright (C) 2012 Tobias Brunner - * Hochschule fuer Technik Rapperswil - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation; either version 2 of the License, or (at your - * option) any later version. See . - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * for more details. - */ - -#include -#include -#include - -#include "host_resolver.h" - -#include -#include -#include -#include -#include -#include -#include - -/** - * Default minimum and maximum number of threads - */ -#define MIN_THREADS_DEFAULT 0 -#define MAX_THREADS_DEFAULT 3 - -/** - * Timeout in seconds to wait for new queries until a thread may be stopped - */ -#define NEW_QUERY_WAIT_TIMEOUT 30 - -typedef struct private_host_resolver_t private_host_resolver_t; - -/** - * Private data of host_resolver_t - */ -struct private_host_resolver_t { - - /** - * Public interface - */ - host_resolver_t public; - - /** - * Hashtable to check for queued queries, query_t* - */ - hashtable_t *queries; - - /** - * Queue for queries, query_t* - */ - linked_list_t *queue; - - /** - * Mutex to safely access private data - */ - mutex_t *mutex; - - /** - * Condvar to signal arrival of new queries - */ - condvar_t *new_query; - - /** - * Minimum number of resolver threads - */ - u_int min_threads; - - /** - * Maximum number of resolver threads - */ - u_int max_threads; - - /** - * Current number of threads - */ - u_int threads; - - /** - * Current number of busy threads - */ - u_int busy_threads; - - /** - * Pool of threads, thread_t* - */ - linked_list_t *pool; - - /** - * TRUE if no new queries are accepted - */ - bool disabled; - -}; - -typedef struct { - /** DNS name we are looking for */ - char *name; - /** address family we request */ - int family; - /** Condvar to signal completion of a query */ - condvar_t *done; - /** refcount */ - refcount_t refcount; - /** the result if successful */ - host_t *result; -} query_t; - -/** - * Destroy the given query_t object if refcount is zero - */ -static void query_destroy(query_t *this) -{ - if (ref_put(&this->refcount)) - { - DESTROY_IF(this->result); - this->done->destroy(this->done); - free(this->name); - free(this); - } -} - -/** - * Signals all waiting threads and destroys the query - */ -static void query_signal_and_destroy(query_t *this) -{ - this->done->broadcast(this->done); - query_destroy(this); -} - -/** - * Hash a queued query - */ -static u_int query_hash(query_t *this) -{ - return chunk_hash_inc(chunk_create(this->name, strlen(this->name)), - chunk_hash(chunk_from_thing(this->family))); -} - -/** - * Compare two queued queries - */ -static bool query_equals(query_t *this, query_t *other) -{ - return this->family == other->family && streq(this->name, other->name); -} - -/** - * Main function of resolver threads - */ -static void *resolve_hosts(private_host_resolver_t *this) -{ - struct addrinfo hints, *result; - query_t *query; - int error; - bool old, timed_out; - - while (TRUE) - { - this->mutex->lock(this->mutex); - thread_cleanup_push((thread_cleanup_t)this->mutex->unlock, this->mutex); - while (this->queue->remove_first(this->queue, - (void**)&query) != SUCCESS) - { - old = thread_cancelability(TRUE); - timed_out = this->new_query->timed_wait(this->new_query, - this->mutex, NEW_QUERY_WAIT_TIMEOUT * 1000); - thread_cancelability(old); - if (this->disabled) - { - thread_cleanup_pop(TRUE); - return NULL; - } - else if (timed_out && (this->threads > this->min_threads)) - { /* terminate this thread by detaching it */ - thread_t *thread = thread_current(); - - this->threads--; - this->pool->remove(this->pool, thread, NULL); - thread_cleanup_pop(TRUE); - thread->detach(thread); - return NULL; - } - } - this->busy_threads++; - thread_cleanup_pop(TRUE); - - memset(&hints, 0, sizeof(hints)); - hints.ai_family = query->family; - hints.ai_socktype = SOCK_DGRAM; - - thread_cleanup_push((thread_cleanup_t)query_signal_and_destroy, query); - old = thread_cancelability(TRUE); - error = getaddrinfo(query->name, NULL, &hints, &result); - thread_cancelability(old); - thread_cleanup_pop(FALSE); - - this->mutex->lock(this->mutex); - this->busy_threads--; - if (error != 0) - { - DBG1(DBG_LIB, "resolving '%s' failed: %s", query->name, - gai_strerror(error)); - } - else - { /* result is a linked list, but we use only the first address */ - query->result = host_create_from_sockaddr(result->ai_addr); - freeaddrinfo(result); - } - this->queries->remove(this->queries, query); - query->done->broadcast(query->done); - this->mutex->unlock(this->mutex); - query_destroy(query); - } - return NULL; -} - -/** - * Try to convert IP addresses directly - */ -static host_t *try_numeric_lookup(char *name, int family) -{ - struct addrinfo hints, *result; - int error; - host_t *host; - - memset(&hints, 0, sizeof(hints)); - hints.ai_family = family; - hints.ai_socktype = SOCK_DGRAM; - hints.ai_flags = AI_NUMERICHOST; - - error = getaddrinfo(name, NULL, &hints, &result); - if (error != 0) - { /* not an IP address */ - return NULL; - } - else - { /* result is a linked list, but we use only the first address */ - host = host_create_from_sockaddr(result->ai_addr); - freeaddrinfo(result); - } - return host; -} - -METHOD(host_resolver_t, resolve, host_t*, - private_host_resolver_t *this, char *name, int family) -{ - query_t *query, lookup = { - .name = name, - .family = family, - }; - host_t *result; - - if (streq(name, "%any") || streq(name, "0.0.0.0")) - { - return host_create_any(family ? family : AF_INET); - } - if (streq(name, "%any6") || streq(name, "::")) - { - return host_create_any(family ? family : AF_INET6); - } - if (family == AF_INET && strchr(name, ':')) - { /* do not try to convert v6 addresses for v4 family */ - return NULL; - } - result = try_numeric_lookup(name, family); - if (result) - { /* shortcut for numeric IP addresses */ - return result; - } - this->mutex->lock(this->mutex); - if (this->disabled) - { - this->mutex->unlock(this->mutex); - return NULL; - } - query = this->queries->get(this->queries, &lookup); - if (!query) - { - INIT(query, - .name = strdup(name), - .family = family, - .done = condvar_create(CONDVAR_TYPE_DEFAULT), - .refcount = 1, - ); - this->queries->put(this->queries, query, query); - this->queue->insert_last(this->queue, query); - this->new_query->signal(this->new_query); - } - ref_get(&query->refcount); - if (this->busy_threads == this->threads && - this->threads < this->max_threads) - { - thread_t *thread; - - thread = thread_create((thread_main_t)resolve_hosts, this); - if (thread) - { - this->threads++; - this->pool->insert_last(this->pool, thread); - } - } - query->done->wait(query->done, this->mutex); - this->mutex->unlock(this->mutex); - - result = query->result ? query->result->clone(query->result) : NULL; - query_destroy(query); - return result; -} - -METHOD(host_resolver_t, flush, void, - private_host_resolver_t *this) -{ - enumerator_t *enumerator; - query_t *query; - - this->mutex->lock(this->mutex); - enumerator = this->queries->create_enumerator(this->queries); - while (enumerator->enumerate(enumerator, &query, NULL)) - { /* use the hashtable here as we also want to signal dequeued queries */ - this->queries->remove_at(this->queries, enumerator); - query->done->broadcast(query->done); - } - enumerator->destroy(enumerator); - this->queue->destroy_function(this->queue, (void*)query_destroy); - this->queue = linked_list_create(); - this->disabled = TRUE; - /* this will already terminate most idle threads */ - this->new_query->broadcast(this->new_query); - this->mutex->unlock(this->mutex); -} - -METHOD(host_resolver_t, destroy, void, - private_host_resolver_t *this) -{ - thread_t *thread; - - flush(this); - this->pool->invoke_offset(this->pool, offsetof(thread_t, cancel)); - while (this->pool->remove_first(this->pool, (void**)&thread) == SUCCESS) - { - thread->join(thread); - } - this->pool->destroy(this->pool); - this->queue->destroy(this->queue); - this->queries->destroy(this->queries); - this->new_query->destroy(this->new_query); - this->mutex->destroy(this->mutex); - free(this); -} - -/* - * Described in header - */ -host_resolver_t *host_resolver_create() -{ - private_host_resolver_t *this; - - INIT(this, - .public = { - .resolve = _resolve, - .flush = _flush, - .destroy = _destroy, - }, - .queries = hashtable_create((hashtable_hash_t)query_hash, - (hashtable_equals_t)query_equals, 8), - .queue = linked_list_create(), - .pool = linked_list_create(), - .mutex = mutex_create(MUTEX_TYPE_DEFAULT), - .new_query = condvar_create(CONDVAR_TYPE_DEFAULT), - ); - - this->min_threads = max(0, lib->settings->get_int(lib->settings, - "libstrongswan.host_resolver.min_threads", - MIN_THREADS_DEFAULT)); - this->max_threads = max(this->min_threads ?: 1, - lib->settings->get_int(lib->settings, - "libstrongswan.host_resolver.max_threads", - MAX_THREADS_DEFAULT)); - return &this->public; -} diff --git a/src/libstrongswan/host_resolver.h b/src/libstrongswan/host_resolver.h deleted file mode 100644 index f7b8c7e9f..000000000 --- a/src/libstrongswan/host_resolver.h +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright (C) 2012 Tobias Brunner - * Hochschule fuer Technik Rapperswil - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation; either version 2 of the License, or (at your - * option) any later version. See . - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * for more details. - */ - -/** - * @defgroup host_resolver host_resolver - * @{ @ingroup libstrongswan - */ - -#ifndef HOST_RESOLVER_H_ -#define HOST_RESOLVER_H_ - -#include "utils/host.h" - -typedef struct host_resolver_t host_resolver_t; - -/** - * Resolve hosts by DNS name but do so in a separate thread (calling - * getaddrinfo(3) directly might block indefinitely, or at least a very long - * time if no DNS servers are reachable). - */ -struct host_resolver_t { - - /** - * Resolve host from the given DNS name. - * - * @param name name to lookup - * @param family requested address family - * @return resolved host or NULL if failed or canceled - */ - host_t *(*resolve)(host_resolver_t *this, char *name, int family); - - /** - * Flush the queue of queries. No new queries will be accepted afterwards. - */ - void (*flush)(host_resolver_t *this); - - /** - * Destroy a host_resolver_t. - */ - void (*destroy)(host_resolver_t *this); -}; - -/** - * Create a host_resolver_t instance. - */ -host_resolver_t *host_resolver_create(); - -#endif /** HOST_RESOLVER_H_ @}*/ diff --git a/src/libstrongswan/library.c b/src/libstrongswan/library.c index a42d68cbc..90a865a78 100644 --- a/src/libstrongswan/library.c +++ b/src/libstrongswan/library.c @@ -21,7 +21,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/libstrongswan/library.h b/src/libstrongswan/library.h index 5bd0d67eb..a2de74986 100644 --- a/src/libstrongswan/library.h +++ b/src/libstrongswan/library.h @@ -46,6 +46,9 @@ * @defgroup ipsec ipsec * @ingroup libstrongswan * + * @defgroup networking networking + * @ingroup libstrongswan + * * @defgroup plugins plugins * @ingroup libstrongswan * @@ -77,9 +80,9 @@ #include "printf_hook.h" #include "utils.h" #include "chunk.h" -#include "host_resolver.h" #include "settings.h" #include "integrity_checker.h" +#include "networking/host_resolver.h" #include "processing/processor.h" #include "processing/scheduler.h" #include "crypto/crypto_factory.h" diff --git a/src/libstrongswan/networking/host.c b/src/libstrongswan/networking/host.c new file mode 100644 index 000000000..1d0614001 --- /dev/null +++ b/src/libstrongswan/networking/host.c @@ -0,0 +1,578 @@ +/* + * Copyright (C) 2006-2012 Tobias Brunner + * Copyright (C) 2006 Daniel Roethlisberger + * Copyright (C) 2005-2006 Martin Willi + * Copyright (C) 2005 Jan Hutter + * Hochschule fuer Technik Rapperswil + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. See . + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +#include "host.h" + +#include +#include + +#define IPV4_LEN 4 +#define IPV6_LEN 16 + +typedef struct private_host_t private_host_t; + +/** + * Private Data of a host object. + */ +struct private_host_t { + /** + * Public data + */ + host_t public; + + /** + * low-lewel structure, which stores the address + */ + union { + /** generic type */ + struct sockaddr address; + /** maximum sockaddr size */ + struct sockaddr_storage address_max; + /** IPv4 address */ + struct sockaddr_in address4; + /** IPv6 address */ + struct sockaddr_in6 address6; + }; + /** + * length of address structure + */ + socklen_t socklen; +}; + + +METHOD(host_t, get_sockaddr, sockaddr_t*, + private_host_t *this) +{ + return &(this->address); +} + +METHOD(host_t, get_sockaddr_len, socklen_t*, + private_host_t *this) +{ + return &(this->socklen); +} + +METHOD(host_t, is_anyaddr, bool, + private_host_t *this) +{ + static const u_int8_t zeroes[IPV6_LEN]; + + switch (this->address.sa_family) + { + case AF_INET: + { + return memeq(zeroes, &(this->address4.sin_addr.s_addr), IPV4_LEN); + } + case AF_INET6: + { + return memeq(zeroes, &(this->address6.sin6_addr.s6_addr), IPV6_LEN); + } + default: + { + return FALSE; + } + } +} + +/** + * Described in header. + */ +int host_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec, + const void *const *args) +{ + private_host_t *this = *((private_host_t**)(args[0])); + char buffer[INET6_ADDRSTRLEN + 16]; + + if (this == NULL) + { + snprintf(buffer, sizeof(buffer), "(null)"); + } + else if (is_anyaddr(this) && !spec->plus) + { + snprintf(buffer, sizeof(buffer), "%%any%s", + this->address.sa_family == AF_INET6 ? "6" : ""); + } + else + { + void *address; + u_int16_t port; + int len; + + address = &this->address6.sin6_addr; + port = this->address6.sin6_port; + + switch (this->address.sa_family) + { + case AF_INET: + address = &this->address4.sin_addr; + port = this->address4.sin_port; + /* fall */ + case AF_INET6: + + if (inet_ntop(this->address.sa_family, address, + buffer, sizeof(buffer)) == NULL) + { + snprintf(buffer, sizeof(buffer), + "(address conversion failed)"); + } + else if (spec->hash) + { + len = strlen(buffer); + snprintf(buffer + len, sizeof(buffer) - len, + "[%d]", ntohs(port)); + } + break; + default: + snprintf(buffer, sizeof(buffer), "(family not supported)"); + break; + } + } + if (spec->minus) + { + return print_in_hook(data, "%-*s", spec->width, buffer); + } + return print_in_hook(data, "%*s", spec->width, buffer); +} + +METHOD(host_t, get_address, chunk_t, + private_host_t *this) +{ + chunk_t address = chunk_empty; + + switch (this->address.sa_family) + { + case AF_INET: + { + address.ptr = (char*)&(this->address4.sin_addr.s_addr); + address.len = IPV4_LEN; + return address; + } + case AF_INET6: + { + address.ptr = (char*)&(this->address6.sin6_addr.s6_addr); + address.len = IPV6_LEN; + return address; + } + default: + { + /* return empty chunk */ + return address; + } + } +} + +METHOD(host_t, get_family, int, + private_host_t *this) +{ + return this->address.sa_family; +} + +METHOD(host_t, get_port, u_int16_t, + private_host_t *this) +{ + switch (this->address.sa_family) + { + case AF_INET: + { + return ntohs(this->address4.sin_port); + } + case AF_INET6: + { + return ntohs(this->address6.sin6_port); + } + default: + { + return 0; + } + } +} + +METHOD(host_t, set_port, void, + private_host_t *this, u_int16_t port) +{ + switch (this->address.sa_family) + { + case AF_INET: + { + this->address4.sin_port = htons(port); + break; + } + case AF_INET6: + { + this->address6.sin6_port = htons(port); + break; + } + default: + { + break; + } + } +} + +METHOD(host_t, clone_, host_t*, + private_host_t *this) +{ + private_host_t *new; + + new = malloc_thing(private_host_t); + memcpy(new, this, sizeof(private_host_t)); + + return &new->public; +} + +/** + * Implements host_t.ip_equals + */ +static bool ip_equals(private_host_t *this, private_host_t *other) +{ + if (this->address.sa_family != other->address.sa_family) + { + /* 0.0.0.0 and 0::0 are equal */ + return (is_anyaddr(this) && is_anyaddr(other)); + } + + switch (this->address.sa_family) + { + case AF_INET: + { + return memeq(&this->address4.sin_addr, &other->address4.sin_addr, + sizeof(this->address4.sin_addr)); + } + case AF_INET6: + { + return memeq(&this->address6.sin6_addr, &other->address6.sin6_addr, + sizeof(this->address6.sin6_addr)); + } + default: + break; + } + return FALSE; +} + +/** + * Implements host_t.get_differences + */ +static host_diff_t get_differences(host_t *this, host_t *other) +{ + host_diff_t ret = HOST_DIFF_NONE; + + if (!this->ip_equals(this, other)) + { + ret |= HOST_DIFF_ADDR; + } + + if (this->get_port(this) != other->get_port(other)) + { + ret |= HOST_DIFF_PORT; + } + + return ret; +} + +/** + * Implements host_t.equals + */ +static bool equals(private_host_t *this, private_host_t *other) +{ + if (!ip_equals(this, other)) + { + return FALSE; + } + + switch (this->address.sa_family) + { + case AF_INET: + { + return (this->address4.sin_port == other->address4.sin_port); + } + case AF_INET6: + { + return (this->address6.sin6_port == other->address6.sin6_port); + } + default: + break; + } + return FALSE; +} + +METHOD(host_t, destroy, void, + private_host_t *this) +{ + free(this); +} + +/** + * Creates an empty host_t object + */ +static private_host_t *host_create_empty(void) +{ + private_host_t *this; + + INIT(this, + .public = { + .get_sockaddr = _get_sockaddr, + .get_sockaddr_len = _get_sockaddr_len, + .clone = _clone_, + .get_family = _get_family, + .get_address = _get_address, + .get_port = _get_port, + .set_port = _set_port, + .get_differences = get_differences, + .ip_equals = (bool (*)(host_t *,host_t *))ip_equals, + .equals = (bool (*)(host_t *,host_t *)) equals, + .is_anyaddr = _is_anyaddr, + .destroy = _destroy, + }, + ); + + return this; +} + +/* + * Create a %any host with port + */ +static host_t *host_create_any_port(int family, u_int16_t port) +{ + host_t *this; + + this = host_create_any(family); + this->set_port(this, port); + return this; +} + +/* + * Described in header. + */ +host_t *host_create_from_string(char *string, u_int16_t port) +{ + private_host_t *this; + + if (streq(string, "%any")) + { + return host_create_any_port(AF_INET, port); + } + if (streq(string, "%any6")) + { + return host_create_any_port(AF_INET6, port); + } + + this = host_create_empty(); + if (strchr(string, '.')) + { + this->address.sa_family = AF_INET; + } + else + { + this->address.sa_family = AF_INET6; + } + switch (this->address.sa_family) + { + case AF_INET: + { + if (inet_pton(AF_INET, string, &this->address4.sin_addr) <=0) + { + break; + } + this->address4.sin_port = htons(port); + this->socklen = sizeof(struct sockaddr_in); + return &this->public; + } + case AF_INET6: + { + if (inet_pton(AF_INET6, string, &this->address6.sin6_addr) <=0) + { + break; + } + this->address6.sin6_port = htons(port); + this->socklen = sizeof(struct sockaddr_in6); + return &this->public; + } + default: + { + break; + } + } + free(this); + return NULL; +} + +/* + * Described in header. + */ +host_t *host_create_from_sockaddr(sockaddr_t *sockaddr) +{ + private_host_t *this = host_create_empty(); + + switch (sockaddr->sa_family) + { + case AF_INET: + { + memcpy(&this->address4, (struct sockaddr_in*)sockaddr, + sizeof(struct sockaddr_in)); + this->socklen = sizeof(struct sockaddr_in); + return &this->public; + } + case AF_INET6: + { + memcpy(&this->address6, (struct sockaddr_in6*)sockaddr, + sizeof(struct sockaddr_in6)); + this->socklen = sizeof(struct sockaddr_in6); + return &this->public; + } + default: + break; + } + free(this); + return NULL; +} + +/* + * Described in header. + */ +host_t *host_create_from_dns(char *string, int af, u_int16_t port) +{ + host_t *this; + + this = lib->hosts->resolve(lib->hosts, string, af); + if (this) + { + this->set_port(this, port); + } + return this; +} + +/* + * Described in header. + */ +host_t *host_create_from_chunk(int family, chunk_t address, u_int16_t port) +{ + private_host_t *this; + + switch (family) + { + case AF_INET: + if (address.len < IPV4_LEN) + { + return NULL; + } + address.len = IPV4_LEN; + break; + case AF_INET6: + if (address.len < IPV6_LEN) + { + return NULL; + } + address.len = IPV6_LEN; + break; + case AF_UNSPEC: + switch (address.len) + { + case IPV4_LEN: + family = AF_INET; + break; + case IPV6_LEN: + family = AF_INET6; + break; + default: + return NULL; + } + break; + default: + return NULL; + } + this = host_create_empty(); + this->address.sa_family = family; + switch (family) + { + case AF_INET: + memcpy(&this->address4.sin_addr.s_addr, address.ptr, address.len); + this->address4.sin_port = htons(port); + this->socklen = sizeof(struct sockaddr_in); + break; + case AF_INET6: + memcpy(&this->address6.sin6_addr.s6_addr, address.ptr, address.len); + this->address6.sin6_port = htons(port); + this->socklen = sizeof(struct sockaddr_in6); + break; + } + return &this->public; +} + +/* + * Described in header. + */ +host_t *host_create_from_subnet(char *string, int *bits) +{ + char *pos, buf[64]; + host_t *net; + + pos = strchr(string, '/'); + if (pos) + { + if (pos - string >= sizeof(buf)) + { + return NULL; + } + strncpy(buf, string, pos - string); + buf[pos - string] = '\0'; + *bits = atoi(pos + 1); + return host_create_from_string(buf, 0); + } + net = host_create_from_string(string, 0); + if (net) + { + if (net->get_family(net) == AF_INET) + { + *bits = 32; + } + else + { + *bits = 128; + } + } + return net; +} + +/* + * Described in header. + */ +host_t *host_create_any(int family) +{ + private_host_t *this = host_create_empty(); + + memset(&this->address_max, 0, sizeof(struct sockaddr_storage)); + this->address.sa_family = family; + + switch (family) + { + case AF_INET: + { + this->socklen = sizeof(struct sockaddr_in); + return &(this->public); + } + case AF_INET6: + { + this->socklen = sizeof(struct sockaddr_in6); + return &this->public; + } + default: + break; + } + free(this); + return NULL; +} diff --git a/src/libstrongswan/networking/host.h b/src/libstrongswan/networking/host.h new file mode 100644 index 000000000..cae2a4ff0 --- /dev/null +++ b/src/libstrongswan/networking/host.h @@ -0,0 +1,220 @@ +/* + * Copyright (C) 2006-2009 Tobias Brunner + * Copyright (C) 2006 Daniel Roethlisberger + * Copyright (C) 2005-2008 Martin Willi + * Copyright (C) 2005 Jan Hutter + * Hochschule fuer Technik Rapperswil + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. See . + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +/** + * @defgroup host host + * @{ @ingroup networking + */ + +#ifndef HOST_H_ +#define HOST_H_ + +typedef enum host_diff_t host_diff_t; +typedef struct host_t host_t; + +#include +#include +#include +#include +#include +#include + +#include + +/** + * Differences between two hosts. They differ in + * address, port, or both. + */ +enum host_diff_t { + HOST_DIFF_NONE = 0, + HOST_DIFF_ADDR = 1, + HOST_DIFF_PORT = 2, +}; + +/** + * Representates a Host + * + * Host object, identifies a address:port pair and defines some + * useful functions on it. + */ +struct host_t { + + /** + * Build a clone of this host object. + * + * @return cloned host + */ + host_t *(*clone) (host_t *this); + + /** + * Get a pointer to the internal sockaddr struct. + * + * This is used for sending and receiving via sockets. + * + * @return pointer to the internal sockaddr structure + */ + sockaddr_t *(*get_sockaddr) (host_t *this); + + /** + * Get the length of the sockaddr struct. + * + * Depending on the family, the length of the sockaddr struct + * is different. Use this function to get the length of the sockaddr + * struct returned by get_sock_addr. + * + * This is used for sending and receiving via sockets. + * + * @return length of the sockaddr struct + */ + socklen_t *(*get_sockaddr_len) (host_t *this); + + /** + * Gets the family of the address + * + * @return family + */ + int (*get_family) (host_t *this); + + /** + * Checks if the ip address of host is set to default route. + * + * @return TRUE if host is 0.0.0.0 or 0::0, FALSE otherwise + */ + bool (*is_anyaddr) (host_t *this); + + /** + * Get the address of this host as chunk_t + * + * Returned chunk points to internal data. + * + * @return address string, + */ + chunk_t (*get_address) (host_t *this); + + /** + * Get the port of this host + * + * @return port number + */ + u_int16_t (*get_port) (host_t *this); + + /** + * Set the port of this host + * + * @param port port numer + */ + void (*set_port) (host_t *this, u_int16_t port); + + /** + * Compare the ips of two hosts hosts. + * + * @param other the other to compare + * @return TRUE if addresses are equal. + */ + bool (*ip_equals) (host_t *this, host_t *other); + + /** + * Compare two hosts, with port. + * + * @param other the other to compare + * @return TRUE if addresses and ports are equal. + */ + bool (*equals) (host_t *this, host_t *other); + + /** + * Compare two hosts and return the differences. + * + * @param other the other to compare + * @return differences in a combination of host_diff_t's + */ + host_diff_t (*get_differences) (host_t *this, host_t *other); + + /** + * Destroy this host object. + */ + void (*destroy) (host_t *this); +}; + +/** + * Constructor to create a host_t object from an address string. + * + * @param string string of an address, such as "152.96.193.130" + * @param port port number + * @return host_t, NULL if string not an address. + */ +host_t *host_create_from_string(char *string, u_int16_t port); + +/** + * Constructor to create a host_t from a DNS name. + * + * @param string hostname to resolve + * @param family family to prefer, 0 for first match + * @param port port number + * @return host_t, NULL lookup failed + */ +host_t *host_create_from_dns(char *string, int family, u_int16_t port); + +/** + * Constructor to create a host_t object from an address chunk. + * + * If family is AF_UNSPEC, it is guessed using address.len. + * + * @param family Address family, such as AF_INET or AF_INET6 + * @param address address as chunk_t in network order + * @param port port number + * @return host_t, NULL if family not supported/chunk invalid + */ +host_t *host_create_from_chunk(int family, chunk_t address, u_int16_t port); + +/** + * Constructor to create a host_t object from a sockaddr struct + * + * @param sockaddr sockaddr struct which contains family, address and port + * @return host_t, NULL if family not supported + */ +host_t *host_create_from_sockaddr(sockaddr_t *sockaddr); + +/** + * Create a host from a CIDR subnet definition (1.2.3.0/24), return bits. + * + * @param string string to parse + * @param bits gets the number of network bits in CIDR notation + * @return network start address, NULL on error + */ +host_t *host_create_from_subnet(char *string, int *bits); + +/** + * Create a host without an address, a "any" host. + * + * @param family family of the any host + * @return host_t, NULL if family not supported + */ +host_t *host_create_any(int family); + +/** + * printf hook function for host_t. + * + * Arguments are: + * host_t *host + * Use #-modifier to include port number + * Use +-modifier to force numeric representation (instead of e.g. %any) + */ +int host_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec, + const void *const *args); + +#endif /** HOST_H_ @}*/ diff --git a/src/libstrongswan/networking/host_resolver.c b/src/libstrongswan/networking/host_resolver.c new file mode 100644 index 000000000..55b07d318 --- /dev/null +++ b/src/libstrongswan/networking/host_resolver.c @@ -0,0 +1,391 @@ +/* + * Copyright (C) 2012 Tobias Brunner + * Hochschule fuer Technik Rapperswil + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. See . + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +#include +#include +#include + +#include "host_resolver.h" + +#include +#include +#include +#include +#include +#include +#include + +/** + * Default minimum and maximum number of threads + */ +#define MIN_THREADS_DEFAULT 0 +#define MAX_THREADS_DEFAULT 3 + +/** + * Timeout in seconds to wait for new queries until a thread may be stopped + */ +#define NEW_QUERY_WAIT_TIMEOUT 30 + +typedef struct private_host_resolver_t private_host_resolver_t; + +/** + * Private data of host_resolver_t + */ +struct private_host_resolver_t { + + /** + * Public interface + */ + host_resolver_t public; + + /** + * Hashtable to check for queued queries, query_t* + */ + hashtable_t *queries; + + /** + * Queue for queries, query_t* + */ + linked_list_t *queue; + + /** + * Mutex to safely access private data + */ + mutex_t *mutex; + + /** + * Condvar to signal arrival of new queries + */ + condvar_t *new_query; + + /** + * Minimum number of resolver threads + */ + u_int min_threads; + + /** + * Maximum number of resolver threads + */ + u_int max_threads; + + /** + * Current number of threads + */ + u_int threads; + + /** + * Current number of busy threads + */ + u_int busy_threads; + + /** + * Pool of threads, thread_t* + */ + linked_list_t *pool; + + /** + * TRUE if no new queries are accepted + */ + bool disabled; + +}; + +typedef struct { + /** DNS name we are looking for */ + char *name; + /** address family we request */ + int family; + /** Condvar to signal completion of a query */ + condvar_t *done; + /** refcount */ + refcount_t refcount; + /** the result if successful */ + host_t *result; +} query_t; + +/** + * Destroy the given query_t object if refcount is zero + */ +static void query_destroy(query_t *this) +{ + if (ref_put(&this->refcount)) + { + DESTROY_IF(this->result); + this->done->destroy(this->done); + free(this->name); + free(this); + } +} + +/** + * Signals all waiting threads and destroys the query + */ +static void query_signal_and_destroy(query_t *this) +{ + this->done->broadcast(this->done); + query_destroy(this); +} + +/** + * Hash a queued query + */ +static u_int query_hash(query_t *this) +{ + return chunk_hash_inc(chunk_create(this->name, strlen(this->name)), + chunk_hash(chunk_from_thing(this->family))); +} + +/** + * Compare two queued queries + */ +static bool query_equals(query_t *this, query_t *other) +{ + return this->family == other->family && streq(this->name, other->name); +} + +/** + * Main function of resolver threads + */ +static void *resolve_hosts(private_host_resolver_t *this) +{ + struct addrinfo hints, *result; + query_t *query; + int error; + bool old, timed_out; + + while (TRUE) + { + this->mutex->lock(this->mutex); + thread_cleanup_push((thread_cleanup_t)this->mutex->unlock, this->mutex); + while (this->queue->remove_first(this->queue, + (void**)&query) != SUCCESS) + { + old = thread_cancelability(TRUE); + timed_out = this->new_query->timed_wait(this->new_query, + this->mutex, NEW_QUERY_WAIT_TIMEOUT * 1000); + thread_cancelability(old); + if (this->disabled) + { + thread_cleanup_pop(TRUE); + return NULL; + } + else if (timed_out && (this->threads > this->min_threads)) + { /* terminate this thread by detaching it */ + thread_t *thread = thread_current(); + + this->threads--; + this->pool->remove(this->pool, thread, NULL); + thread_cleanup_pop(TRUE); + thread->detach(thread); + return NULL; + } + } + this->busy_threads++; + thread_cleanup_pop(TRUE); + + memset(&hints, 0, sizeof(hints)); + hints.ai_family = query->family; + hints.ai_socktype = SOCK_DGRAM; + + thread_cleanup_push((thread_cleanup_t)query_signal_and_destroy, query); + old = thread_cancelability(TRUE); + error = getaddrinfo(query->name, NULL, &hints, &result); + thread_cancelability(old); + thread_cleanup_pop(FALSE); + + this->mutex->lock(this->mutex); + this->busy_threads--; + if (error != 0) + { + DBG1(DBG_LIB, "resolving '%s' failed: %s", query->name, + gai_strerror(error)); + } + else + { /* result is a linked list, but we use only the first address */ + query->result = host_create_from_sockaddr(result->ai_addr); + freeaddrinfo(result); + } + this->queries->remove(this->queries, query); + query->done->broadcast(query->done); + this->mutex->unlock(this->mutex); + query_destroy(query); + } + return NULL; +} + +/** + * Try to convert IP addresses directly + */ +static host_t *try_numeric_lookup(char *name, int family) +{ + struct addrinfo hints, *result; + int error; + host_t *host; + + memset(&hints, 0, sizeof(hints)); + hints.ai_family = family; + hints.ai_socktype = SOCK_DGRAM; + hints.ai_flags = AI_NUMERICHOST; + + error = getaddrinfo(name, NULL, &hints, &result); + if (error != 0) + { /* not an IP address */ + return NULL; + } + else + { /* result is a linked list, but we use only the first address */ + host = host_create_from_sockaddr(result->ai_addr); + freeaddrinfo(result); + } + return host; +} + +METHOD(host_resolver_t, resolve, host_t*, + private_host_resolver_t *this, char *name, int family) +{ + query_t *query, lookup = { + .name = name, + .family = family, + }; + host_t *result; + + if (streq(name, "%any") || streq(name, "0.0.0.0")) + { + return host_create_any(family ? family : AF_INET); + } + if (streq(name, "%any6") || streq(name, "::")) + { + return host_create_any(family ? family : AF_INET6); + } + if (family == AF_INET && strchr(name, ':')) + { /* do not try to convert v6 addresses for v4 family */ + return NULL; + } + result = try_numeric_lookup(name, family); + if (result) + { /* shortcut for numeric IP addresses */ + return result; + } + this->mutex->lock(this->mutex); + if (this->disabled) + { + this->mutex->unlock(this->mutex); + return NULL; + } + query = this->queries->get(this->queries, &lookup); + if (!query) + { + INIT(query, + .name = strdup(name), + .family = family, + .done = condvar_create(CONDVAR_TYPE_DEFAULT), + .refcount = 1, + ); + this->queries->put(this->queries, query, query); + this->queue->insert_last(this->queue, query); + this->new_query->signal(this->new_query); + } + ref_get(&query->refcount); + if (this->busy_threads == this->threads && + this->threads < this->max_threads) + { + thread_t *thread; + + thread = thread_create((thread_main_t)resolve_hosts, this); + if (thread) + { + this->threads++; + this->pool->insert_last(this->pool, thread); + } + } + query->done->wait(query->done, this->mutex); + this->mutex->unlock(this->mutex); + + result = query->result ? query->result->clone(query->result) : NULL; + query_destroy(query); + return result; +} + +METHOD(host_resolver_t, flush, void, + private_host_resolver_t *this) +{ + enumerator_t *enumerator; + query_t *query; + + this->mutex->lock(this->mutex); + enumerator = this->queries->create_enumerator(this->queries); + while (enumerator->enumerate(enumerator, &query, NULL)) + { /* use the hashtable here as we also want to signal dequeued queries */ + this->queries->remove_at(this->queries, enumerator); + query->done->broadcast(query->done); + } + enumerator->destroy(enumerator); + this->queue->destroy_function(this->queue, (void*)query_destroy); + this->queue = linked_list_create(); + this->disabled = TRUE; + /* this will already terminate most idle threads */ + this->new_query->broadcast(this->new_query); + this->mutex->unlock(this->mutex); +} + +METHOD(host_resolver_t, destroy, void, + private_host_resolver_t *this) +{ + thread_t *thread; + + flush(this); + this->pool->invoke_offset(this->pool, offsetof(thread_t, cancel)); + while (this->pool->remove_first(this->pool, (void**)&thread) == SUCCESS) + { + thread->join(thread); + } + this->pool->destroy(this->pool); + this->queue->destroy(this->queue); + this->queries->destroy(this->queries); + this->new_query->destroy(this->new_query); + this->mutex->destroy(this->mutex); + free(this); +} + +/* + * Described in header + */ +host_resolver_t *host_resolver_create() +{ + private_host_resolver_t *this; + + INIT(this, + .public = { + .resolve = _resolve, + .flush = _flush, + .destroy = _destroy, + }, + .queries = hashtable_create((hashtable_hash_t)query_hash, + (hashtable_equals_t)query_equals, 8), + .queue = linked_list_create(), + .pool = linked_list_create(), + .mutex = mutex_create(MUTEX_TYPE_DEFAULT), + .new_query = condvar_create(CONDVAR_TYPE_DEFAULT), + ); + + this->min_threads = max(0, lib->settings->get_int(lib->settings, + "libstrongswan.host_resolver.min_threads", + MIN_THREADS_DEFAULT)); + this->max_threads = max(this->min_threads ?: 1, + lib->settings->get_int(lib->settings, + "libstrongswan.host_resolver.max_threads", + MAX_THREADS_DEFAULT)); + return &this->public; +} diff --git a/src/libstrongswan/networking/host_resolver.h b/src/libstrongswan/networking/host_resolver.h new file mode 100644 index 000000000..f944a9cdf --- /dev/null +++ b/src/libstrongswan/networking/host_resolver.h @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2012 Tobias Brunner + * Hochschule fuer Technik Rapperswil + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. See . + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +/** + * @defgroup host_resolver host_resolver + * @{ @ingroup networking + */ + +#ifndef HOST_RESOLVER_H_ +#define HOST_RESOLVER_H_ + +#include "host.h" + +typedef struct host_resolver_t host_resolver_t; + +/** + * Resolve hosts by DNS name but do so in a separate thread (calling + * getaddrinfo(3) directly might block indefinitely, or at least a very long + * time if no DNS servers are reachable). + */ +struct host_resolver_t { + + /** + * Resolve host from the given DNS name. + * + * @param name name to lookup + * @param family requested address family + * @return resolved host or NULL if failed or canceled + */ + host_t *(*resolve)(host_resolver_t *this, char *name, int family); + + /** + * Flush the queue of queries. No new queries will be accepted afterwards. + */ + void (*flush)(host_resolver_t *this); + + /** + * Destroy a host_resolver_t. + */ + void (*destroy)(host_resolver_t *this); +}; + +/** + * Create a host_resolver_t instance. + */ +host_resolver_t *host_resolver_create(); + +#endif /** HOST_RESOLVER_H_ @}*/ diff --git a/src/libstrongswan/selectors/traffic_selector.h b/src/libstrongswan/selectors/traffic_selector.h index 39b8879fd..b6da391aa 100644 --- a/src/libstrongswan/selectors/traffic_selector.h +++ b/src/libstrongswan/selectors/traffic_selector.h @@ -27,7 +27,7 @@ typedef enum ts_type_t ts_type_t; typedef struct traffic_selector_t traffic_selector_t; #include -#include +#include /** * Traffic selector types. diff --git a/src/libstrongswan/utils/host.c b/src/libstrongswan/utils/host.c deleted file mode 100644 index 1d0614001..000000000 --- a/src/libstrongswan/utils/host.c +++ /dev/null @@ -1,578 +0,0 @@ -/* - * Copyright (C) 2006-2012 Tobias Brunner - * Copyright (C) 2006 Daniel Roethlisberger - * Copyright (C) 2005-2006 Martin Willi - * Copyright (C) 2005 Jan Hutter - * Hochschule fuer Technik Rapperswil - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation; either version 2 of the License, or (at your - * option) any later version. See . - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * for more details. - */ - -#include "host.h" - -#include -#include - -#define IPV4_LEN 4 -#define IPV6_LEN 16 - -typedef struct private_host_t private_host_t; - -/** - * Private Data of a host object. - */ -struct private_host_t { - /** - * Public data - */ - host_t public; - - /** - * low-lewel structure, which stores the address - */ - union { - /** generic type */ - struct sockaddr address; - /** maximum sockaddr size */ - struct sockaddr_storage address_max; - /** IPv4 address */ - struct sockaddr_in address4; - /** IPv6 address */ - struct sockaddr_in6 address6; - }; - /** - * length of address structure - */ - socklen_t socklen; -}; - - -METHOD(host_t, get_sockaddr, sockaddr_t*, - private_host_t *this) -{ - return &(this->address); -} - -METHOD(host_t, get_sockaddr_len, socklen_t*, - private_host_t *this) -{ - return &(this->socklen); -} - -METHOD(host_t, is_anyaddr, bool, - private_host_t *this) -{ - static const u_int8_t zeroes[IPV6_LEN]; - - switch (this->address.sa_family) - { - case AF_INET: - { - return memeq(zeroes, &(this->address4.sin_addr.s_addr), IPV4_LEN); - } - case AF_INET6: - { - return memeq(zeroes, &(this->address6.sin6_addr.s6_addr), IPV6_LEN); - } - default: - { - return FALSE; - } - } -} - -/** - * Described in header. - */ -int host_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec, - const void *const *args) -{ - private_host_t *this = *((private_host_t**)(args[0])); - char buffer[INET6_ADDRSTRLEN + 16]; - - if (this == NULL) - { - snprintf(buffer, sizeof(buffer), "(null)"); - } - else if (is_anyaddr(this) && !spec->plus) - { - snprintf(buffer, sizeof(buffer), "%%any%s", - this->address.sa_family == AF_INET6 ? "6" : ""); - } - else - { - void *address; - u_int16_t port; - int len; - - address = &this->address6.sin6_addr; - port = this->address6.sin6_port; - - switch (this->address.sa_family) - { - case AF_INET: - address = &this->address4.sin_addr; - port = this->address4.sin_port; - /* fall */ - case AF_INET6: - - if (inet_ntop(this->address.sa_family, address, - buffer, sizeof(buffer)) == NULL) - { - snprintf(buffer, sizeof(buffer), - "(address conversion failed)"); - } - else if (spec->hash) - { - len = strlen(buffer); - snprintf(buffer + len, sizeof(buffer) - len, - "[%d]", ntohs(port)); - } - break; - default: - snprintf(buffer, sizeof(buffer), "(family not supported)"); - break; - } - } - if (spec->minus) - { - return print_in_hook(data, "%-*s", spec->width, buffer); - } - return print_in_hook(data, "%*s", spec->width, buffer); -} - -METHOD(host_t, get_address, chunk_t, - private_host_t *this) -{ - chunk_t address = chunk_empty; - - switch (this->address.sa_family) - { - case AF_INET: - { - address.ptr = (char*)&(this->address4.sin_addr.s_addr); - address.len = IPV4_LEN; - return address; - } - case AF_INET6: - { - address.ptr = (char*)&(this->address6.sin6_addr.s6_addr); - address.len = IPV6_LEN; - return address; - } - default: - { - /* return empty chunk */ - return address; - } - } -} - -METHOD(host_t, get_family, int, - private_host_t *this) -{ - return this->address.sa_family; -} - -METHOD(host_t, get_port, u_int16_t, - private_host_t *this) -{ - switch (this->address.sa_family) - { - case AF_INET: - { - return ntohs(this->address4.sin_port); - } - case AF_INET6: - { - return ntohs(this->address6.sin6_port); - } - default: - { - return 0; - } - } -} - -METHOD(host_t, set_port, void, - private_host_t *this, u_int16_t port) -{ - switch (this->address.sa_family) - { - case AF_INET: - { - this->address4.sin_port = htons(port); - break; - } - case AF_INET6: - { - this->address6.sin6_port = htons(port); - break; - } - default: - { - break; - } - } -} - -METHOD(host_t, clone_, host_t*, - private_host_t *this) -{ - private_host_t *new; - - new = malloc_thing(private_host_t); - memcpy(new, this, sizeof(private_host_t)); - - return &new->public; -} - -/** - * Implements host_t.ip_equals - */ -static bool ip_equals(private_host_t *this, private_host_t *other) -{ - if (this->address.sa_family != other->address.sa_family) - { - /* 0.0.0.0 and 0::0 are equal */ - return (is_anyaddr(this) && is_anyaddr(other)); - } - - switch (this->address.sa_family) - { - case AF_INET: - { - return memeq(&this->address4.sin_addr, &other->address4.sin_addr, - sizeof(this->address4.sin_addr)); - } - case AF_INET6: - { - return memeq(&this->address6.sin6_addr, &other->address6.sin6_addr, - sizeof(this->address6.sin6_addr)); - } - default: - break; - } - return FALSE; -} - -/** - * Implements host_t.get_differences - */ -static host_diff_t get_differences(host_t *this, host_t *other) -{ - host_diff_t ret = HOST_DIFF_NONE; - - if (!this->ip_equals(this, other)) - { - ret |= HOST_DIFF_ADDR; - } - - if (this->get_port(this) != other->get_port(other)) - { - ret |= HOST_DIFF_PORT; - } - - return ret; -} - -/** - * Implements host_t.equals - */ -static bool equals(private_host_t *this, private_host_t *other) -{ - if (!ip_equals(this, other)) - { - return FALSE; - } - - switch (this->address.sa_family) - { - case AF_INET: - { - return (this->address4.sin_port == other->address4.sin_port); - } - case AF_INET6: - { - return (this->address6.sin6_port == other->address6.sin6_port); - } - default: - break; - } - return FALSE; -} - -METHOD(host_t, destroy, void, - private_host_t *this) -{ - free(this); -} - -/** - * Creates an empty host_t object - */ -static private_host_t *host_create_empty(void) -{ - private_host_t *this; - - INIT(this, - .public = { - .get_sockaddr = _get_sockaddr, - .get_sockaddr_len = _get_sockaddr_len, - .clone = _clone_, - .get_family = _get_family, - .get_address = _get_address, - .get_port = _get_port, - .set_port = _set_port, - .get_differences = get_differences, - .ip_equals = (bool (*)(host_t *,host_t *))ip_equals, - .equals = (bool (*)(host_t *,host_t *)) equals, - .is_anyaddr = _is_anyaddr, - .destroy = _destroy, - }, - ); - - return this; -} - -/* - * Create a %any host with port - */ -static host_t *host_create_any_port(int family, u_int16_t port) -{ - host_t *this; - - this = host_create_any(family); - this->set_port(this, port); - return this; -} - -/* - * Described in header. - */ -host_t *host_create_from_string(char *string, u_int16_t port) -{ - private_host_t *this; - - if (streq(string, "%any")) - { - return host_create_any_port(AF_INET, port); - } - if (streq(string, "%any6")) - { - return host_create_any_port(AF_INET6, port); - } - - this = host_create_empty(); - if (strchr(string, '.')) - { - this->address.sa_family = AF_INET; - } - else - { - this->address.sa_family = AF_INET6; - } - switch (this->address.sa_family) - { - case AF_INET: - { - if (inet_pton(AF_INET, string, &this->address4.sin_addr) <=0) - { - break; - } - this->address4.sin_port = htons(port); - this->socklen = sizeof(struct sockaddr_in); - return &this->public; - } - case AF_INET6: - { - if (inet_pton(AF_INET6, string, &this->address6.sin6_addr) <=0) - { - break; - } - this->address6.sin6_port = htons(port); - this->socklen = sizeof(struct sockaddr_in6); - return &this->public; - } - default: - { - break; - } - } - free(this); - return NULL; -} - -/* - * Described in header. - */ -host_t *host_create_from_sockaddr(sockaddr_t *sockaddr) -{ - private_host_t *this = host_create_empty(); - - switch (sockaddr->sa_family) - { - case AF_INET: - { - memcpy(&this->address4, (struct sockaddr_in*)sockaddr, - sizeof(struct sockaddr_in)); - this->socklen = sizeof(struct sockaddr_in); - return &this->public; - } - case AF_INET6: - { - memcpy(&this->address6, (struct sockaddr_in6*)sockaddr, - sizeof(struct sockaddr_in6)); - this->socklen = sizeof(struct sockaddr_in6); - return &this->public; - } - default: - break; - } - free(this); - return NULL; -} - -/* - * Described in header. - */ -host_t *host_create_from_dns(char *string, int af, u_int16_t port) -{ - host_t *this; - - this = lib->hosts->resolve(lib->hosts, string, af); - if (this) - { - this->set_port(this, port); - } - return this; -} - -/* - * Described in header. - */ -host_t *host_create_from_chunk(int family, chunk_t address, u_int16_t port) -{ - private_host_t *this; - - switch (family) - { - case AF_INET: - if (address.len < IPV4_LEN) - { - return NULL; - } - address.len = IPV4_LEN; - break; - case AF_INET6: - if (address.len < IPV6_LEN) - { - return NULL; - } - address.len = IPV6_LEN; - break; - case AF_UNSPEC: - switch (address.len) - { - case IPV4_LEN: - family = AF_INET; - break; - case IPV6_LEN: - family = AF_INET6; - break; - default: - return NULL; - } - break; - default: - return NULL; - } - this = host_create_empty(); - this->address.sa_family = family; - switch (family) - { - case AF_INET: - memcpy(&this->address4.sin_addr.s_addr, address.ptr, address.len); - this->address4.sin_port = htons(port); - this->socklen = sizeof(struct sockaddr_in); - break; - case AF_INET6: - memcpy(&this->address6.sin6_addr.s6_addr, address.ptr, address.len); - this->address6.sin6_port = htons(port); - this->socklen = sizeof(struct sockaddr_in6); - break; - } - return &this->public; -} - -/* - * Described in header. - */ -host_t *host_create_from_subnet(char *string, int *bits) -{ - char *pos, buf[64]; - host_t *net; - - pos = strchr(string, '/'); - if (pos) - { - if (pos - string >= sizeof(buf)) - { - return NULL; - } - strncpy(buf, string, pos - string); - buf[pos - string] = '\0'; - *bits = atoi(pos + 1); - return host_create_from_string(buf, 0); - } - net = host_create_from_string(string, 0); - if (net) - { - if (net->get_family(net) == AF_INET) - { - *bits = 32; - } - else - { - *bits = 128; - } - } - return net; -} - -/* - * Described in header. - */ -host_t *host_create_any(int family) -{ - private_host_t *this = host_create_empty(); - - memset(&this->address_max, 0, sizeof(struct sockaddr_storage)); - this->address.sa_family = family; - - switch (family) - { - case AF_INET: - { - this->socklen = sizeof(struct sockaddr_in); - return &(this->public); - } - case AF_INET6: - { - this->socklen = sizeof(struct sockaddr_in6); - return &this->public; - } - default: - break; - } - free(this); - return NULL; -} diff --git a/src/libstrongswan/utils/host.h b/src/libstrongswan/utils/host.h deleted file mode 100644 index a8b010544..000000000 --- a/src/libstrongswan/utils/host.h +++ /dev/null @@ -1,220 +0,0 @@ -/* - * Copyright (C) 2006-2009 Tobias Brunner - * Copyright (C) 2006 Daniel Roethlisberger - * Copyright (C) 2005-2008 Martin Willi - * Copyright (C) 2005 Jan Hutter - * Hochschule fuer Technik Rapperswil - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation; either version 2 of the License, or (at your - * option) any later version. See . - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * for more details. - */ - -/** - * @defgroup host host - * @{ @ingroup utils - */ - -#ifndef HOST_H_ -#define HOST_H_ - -typedef enum host_diff_t host_diff_t; -typedef struct host_t host_t; - -#include -#include -#include -#include -#include -#include - -#include - -/** - * Differences between two hosts. They differ in - * address, port, or both. - */ -enum host_diff_t { - HOST_DIFF_NONE = 0, - HOST_DIFF_ADDR = 1, - HOST_DIFF_PORT = 2, -}; - -/** - * Representates a Host - * - * Host object, identifies a address:port pair and defines some - * useful functions on it. - */ -struct host_t { - - /** - * Build a clone of this host object. - * - * @return cloned host - */ - host_t *(*clone) (host_t *this); - - /** - * Get a pointer to the internal sockaddr struct. - * - * This is used for sending and receiving via sockets. - * - * @return pointer to the internal sockaddr structure - */ - sockaddr_t *(*get_sockaddr) (host_t *this); - - /** - * Get the length of the sockaddr struct. - * - * Depending on the family, the length of the sockaddr struct - * is different. Use this function to get the length of the sockaddr - * struct returned by get_sock_addr. - * - * This is used for sending and receiving via sockets. - * - * @return length of the sockaddr struct - */ - socklen_t *(*get_sockaddr_len) (host_t *this); - - /** - * Gets the family of the address - * - * @return family - */ - int (*get_family) (host_t *this); - - /** - * Checks if the ip address of host is set to default route. - * - * @return TRUE if host is 0.0.0.0 or 0::0, FALSE otherwise - */ - bool (*is_anyaddr) (host_t *this); - - /** - * Get the address of this host as chunk_t - * - * Returned chunk points to internal data. - * - * @return address string, - */ - chunk_t (*get_address) (host_t *this); - - /** - * Get the port of this host - * - * @return port number - */ - u_int16_t (*get_port) (host_t *this); - - /** - * Set the port of this host - * - * @param port port numer - */ - void (*set_port) (host_t *this, u_int16_t port); - - /** - * Compare the ips of two hosts hosts. - * - * @param other the other to compare - * @return TRUE if addresses are equal. - */ - bool (*ip_equals) (host_t *this, host_t *other); - - /** - * Compare two hosts, with port. - * - * @param other the other to compare - * @return TRUE if addresses and ports are equal. - */ - bool (*equals) (host_t *this, host_t *other); - - /** - * Compare two hosts and return the differences. - * - * @param other the other to compare - * @return differences in a combination of host_diff_t's - */ - host_diff_t (*get_differences) (host_t *this, host_t *other); - - /** - * Destroy this host object. - */ - void (*destroy) (host_t *this); -}; - -/** - * Constructor to create a host_t object from an address string. - * - * @param string string of an address, such as "152.96.193.130" - * @param port port number - * @return host_t, NULL if string not an address. - */ -host_t *host_create_from_string(char *string, u_int16_t port); - -/** - * Constructor to create a host_t from a DNS name. - * - * @param string hostname to resolve - * @param family family to prefer, 0 for first match - * @param port port number - * @return host_t, NULL lookup failed - */ -host_t *host_create_from_dns(char *string, int family, u_int16_t port); - -/** - * Constructor to create a host_t object from an address chunk. - * - * If family is AF_UNSPEC, it is guessed using address.len. - * - * @param family Address family, such as AF_INET or AF_INET6 - * @param address address as chunk_t in network order - * @param port port number - * @return host_t, NULL if family not supported/chunk invalid - */ -host_t *host_create_from_chunk(int family, chunk_t address, u_int16_t port); - -/** - * Constructor to create a host_t object from a sockaddr struct - * - * @param sockaddr sockaddr struct which contains family, address and port - * @return host_t, NULL if family not supported - */ -host_t *host_create_from_sockaddr(sockaddr_t *sockaddr); - -/** - * Create a host from a CIDR subnet definition (1.2.3.0/24), return bits. - * - * @param string string to parse - * @param bits gets the number of network bits in CIDR notation - * @return network start address, NULL on error - */ -host_t *host_create_from_subnet(char *string, int *bits); - -/** - * Create a host without an address, a "any" host. - * - * @param family family of the any host - * @return host_t, NULL if family not supported - */ -host_t *host_create_any(int family); - -/** - * printf hook function for host_t. - * - * Arguments are: - * host_t *host - * Use #-modifier to include port number - * Use +-modifier to force numeric representation (instead of e.g. %any) - */ -int host_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec, - const void *const *args); - -#endif /** HOST_H_ @}*/ diff --git a/src/libstrongswan/utils/packet.h b/src/libstrongswan/utils/packet.h index 5c4440115..b6e28199a 100644 --- a/src/libstrongswan/utils/packet.h +++ b/src/libstrongswan/utils/packet.h @@ -26,7 +26,7 @@ typedef struct packet_t packet_t; #include -#include +#include /** * Abstraction of an IP/UDP-Packet, contains data, sender and receiver. diff --git a/src/libstrongswan/utils/tun_device.h b/src/libstrongswan/utils/tun_device.h index 71af0386b..be97432ff 100644 --- a/src/libstrongswan/utils/tun_device.h +++ b/src/libstrongswan/utils/tun_device.h @@ -24,7 +24,7 @@ #define TUN_DEVICE_H_ #include -#include +#include typedef struct tun_device_t tun_device_t; diff --git a/src/manager/gateway.h b/src/manager/gateway.h index db44a2ffa..99e79eef5 100644 --- a/src/manager/gateway.h +++ b/src/manager/gateway.h @@ -21,7 +21,7 @@ #ifndef GATEWAY_H_ #define GATEWAY_H_ -#include +#include #include typedef struct gateway_t gateway_t; -- cgit v1.2.3