xpc: use IKE_SA specific XPC return channels for further communication

This commit is contained in:
Martin Willi
2013-07-18 12:17:55 +02:00
parent bc74e18223
commit 8279ce99c4
4 changed files with 320 additions and 12 deletions
+228
View File
@@ -0,0 +1,228 @@
/*
* Copyright (C) 2013 Martin Willi
* Copyright (C) 2013 revosec AG
*
* 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 <http://www.fsf.org/copyleft/gpl.txt>.
*
* 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 "xpc_channels.h"
#include <collections/hashtable.h>
#include <threading/rwlock.h>
#include <daemon.h>
typedef struct private_xpc_channels_t private_xpc_channels_t;
/**
* Private data of an xpc_channels_t object.
*/
struct private_xpc_channels_t {
/**
* Public xpc_channels_t interface.
*/
xpc_channels_t public;
/**
* Registered channels, IKE_SA unique ID => entry_t
*/
hashtable_t *channels;
/**
* Lock for channels list
*/
rwlock_t *lock;
};
/**
* Channel entry
*/
typedef struct {
/* XPC channel to App */
xpc_connection_t conn;
/* associated IKE_SA unique identifier */
uintptr_t sa;
} entry_t;
/**
* Clean up an entry, cancelling connection
*/
static void destroy_entry(entry_t *entry)
{
xpc_connection_suspend(entry->conn);
xpc_connection_cancel(entry->conn);
xpc_release(entry->conn);
free(entry);
}
/**
* Remove an entry for a given XPC connection
*/
static void remove_conn(private_xpc_channels_t *this, xpc_connection_t conn)
{
enumerator_t *enumerator;
entry_t *entry;
this->lock->write_lock(this->lock);
enumerator = this->channels->create_enumerator(this->channels);
while (enumerator->enumerate(enumerator, NULL, &entry))
{
if (xpc_equal(entry->conn, conn))
{
this->channels->remove(this->channels, enumerator);
destroy_entry(entry);
break;
}
}
enumerator->destroy(enumerator);
this->lock->unlock(this->lock);
}
/**
* Handle a request message from App
*/
static void handle(private_xpc_channels_t *this, xpc_object_t request)
{
/* TODO: */
}
METHOD(xpc_channels_t, add, void,
private_xpc_channels_t *this, xpc_connection_t conn, u_int32_t ike_sa)
{
entry_t *entry;
INIT(entry,
.conn = conn,
.sa = ike_sa,
);
xpc_connection_set_event_handler(entry->conn, ^(xpc_object_t event) {
if (event == XPC_ERROR_CONNECTION_INVALID ||
event == XPC_ERROR_CONNECTION_INTERRUPTED)
{
remove_conn(this, entry->conn);
}
else
{
handle(this, event);
}
});
this->lock->write_lock(this->lock);
this->channels->put(this->channels, (void*)entry->sa, entry);
this->lock->unlock(this->lock);
xpc_connection_resume(conn);
}
METHOD(listener_t, ike_rekey, bool,
private_xpc_channels_t *this, ike_sa_t *old, ike_sa_t *new)
{
entry_t *entry;
uintptr_t sa;
sa = old->get_unique_id(old);
this->lock->write_lock(this->lock);
entry = this->channels->remove(this->channels, (void*)sa);
if (entry)
{
entry->sa = new->get_unique_id(new);
this->channels->put(this->channels, (void*)entry->sa, entry);
}
this->lock->unlock(this->lock);
return TRUE;
}
METHOD(listener_t, ike_updown, bool,
private_xpc_channels_t *this, ike_sa_t *ike_sa, bool up)
{
xpc_object_t msg;
entry_t *entry;
uintptr_t sa;
sa = ike_sa->get_unique_id(ike_sa);
if (up)
{
this->lock->read_lock(this->lock);
entry = this->channels->get(this->channels, (void*)sa);
if (entry)
{
msg = xpc_dictionary_create(NULL, NULL, 0);
xpc_dictionary_set_string(msg, "type", "event");
xpc_dictionary_set_string(msg, "event", "up");
xpc_connection_send_message(entry->conn, msg);
xpc_release(msg);
}
this->lock->unlock(this->lock);
}
else
{
this->lock->write_lock(this->lock);
entry = this->channels->remove(this->channels, (void*)sa);
this->lock->unlock(this->lock);
if (entry)
{
msg = xpc_dictionary_create(NULL, NULL, 0);
xpc_dictionary_set_string(msg, "type", "event");
xpc_dictionary_set_string(msg, "event", "down");
xpc_connection_send_message(entry->conn, msg);
xpc_release(msg);
xpc_connection_send_barrier(entry->conn, ^() {
destroy_entry(entry);
});
}
}
return TRUE;
}
METHOD(xpc_channels_t, destroy, void,
private_xpc_channels_t *this)
{
enumerator_t *enumerator;
entry_t *entry;
enumerator = this->channels->create_enumerator(this->channels);
while (enumerator->enumerate(enumerator, NULL, &entry))
{
destroy_entry(entry);
}
enumerator->destroy(enumerator);
this->channels->destroy(this->channels);
this->lock->destroy(this->lock);
free(this);
}
/**
* See header
*/
xpc_channels_t *xpc_channels_create()
{
private_xpc_channels_t *this;
INIT(this,
.public = {
.listener = {
.ike_updown = _ike_updown,
.ike_rekey = _ike_rekey,
},
.add = _add,
.destroy = _destroy,
},
.channels = hashtable_create(hashtable_hash_ptr,
hashtable_equals_ptr, 4),
.lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
);
return &this->public;
}
@@ -0,0 +1,59 @@
/*
* Copyright (C) 2013 Martin Willi
* Copyright (C) 2013 revosec AG
*
* 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 <http://www.fsf.org/copyleft/gpl.txt>.
*
* 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 xpc_channels xpc_channels
* @{ @ingroup xpc
*/
#ifndef XPC_CHANNELS_H_
#define XPC_CHANNELS_H_
#include <xpc/xpc.h>
#include <bus/bus.h>
typedef struct xpc_channels_t xpc_channels_t;
/**
* XPC to App channel management.
*/
struct xpc_channels_t {
/**
* Implements listener_t.
*/
listener_t listener;
/**
* Associate an IKE_SA unique identifier to an XPC connection.
*
* @param conn XPC connection to channel
* @param ike_sa IKE_SA unique identifier to associate to connection
*/
void (*add)(xpc_channels_t *this, xpc_connection_t conn, u_int32_t ike_sa);
/**
* Destroy a xpc_channels_t.
*/
void (*destroy)(xpc_channels_t *this);
};
/**
* Create a xpc_channels instance.
*/
xpc_channels_t *xpc_channels_create();
#endif /** XPC_CHANNELS_H_ @}*/
+25 -9
View File
@@ -14,6 +14,7 @@
*/
#include "xpc_dispatch.h"
#include "xpc_channels.h"
#include <xpc/xpc.h>
@@ -37,10 +38,15 @@ struct private_xpc_dispatch_t {
*/
xpc_connection_t service;
/**
* GCD queue for XPC events
*/
dispatch_queue_t queue;
/**
* XPC IKE_SA specific channels to App
*/
xpc_channels_t *channels;
/**
* GCD queue for XPC events
*/
dispatch_queue_t queue;
};
/**
@@ -153,14 +159,19 @@ xpc_object_t start_connection(private_xpc_dispatch_t *this,
peer_cfg_t *peer_cfg;
child_cfg_t *child_cfg;
char *name, *id, *host;
u_int32_t sa = 0;
bool success = FALSE;
xpc_endpoint_t endpoint;
xpc_connection_t channel;
u_int32_t ike_sa;
name = (char*)xpc_dictionary_get_string(request, "name");
host = (char*)xpc_dictionary_get_string(request, "host");
id = (char*)xpc_dictionary_get_string(request, "id");
endpoint = xpc_dictionary_get_value(request, "channel");
channel = xpc_connection_create_from_endpoint(endpoint);
reply = xpc_dictionary_create_reply(request);
if (name && id && host)
if (name && id && host && channel)
{
peer_cfg = create_peer_cfg(name, host);
@@ -171,13 +182,14 @@ xpc_object_t start_connection(private_xpc_dispatch_t *this,
peer_cfg->add_child_cfg(peer_cfg, child_cfg->get_ref(child_cfg));
if (charon->controller->initiate(charon->controller, peer_cfg, child_cfg,
(controller_cb_t)initiate_cb, &sa, 0) != SUCCESS)
(controller_cb_t)initiate_cb, &ike_sa, 0) == NEED_MORE)
{
sa = 0;
this->channels->add(this->channels, channel, ike_sa);
success = TRUE;
}
}
xpc_dictionary_set_uint64(reply, "connection", sa);
xpc_dictionary_set_bool(reply, "success", success);
return reply;
}
@@ -256,6 +268,8 @@ static void set_handler(private_xpc_dispatch_t *this)
METHOD(xpc_dispatch_t, destroy, void,
private_xpc_dispatch_t *this)
{
charon->bus->remove_listener(charon->bus, &this->channels->listener);
this->channels->destroy(this->channels);
if (this->service)
{
xpc_connection_suspend(this->service);
@@ -275,9 +289,11 @@ xpc_dispatch_t *xpc_dispatch_create()
.public = {
.destroy = _destroy,
},
.channels = xpc_channels_create(),
.queue = dispatch_queue_create("org.strongswan.charon-xpc.q",
DISPATCH_QUEUE_CONCURRENT),
);
charon->bus->add_listener(charon->bus, &this->channels->listener);
this->service = xpc_connection_create_mach_service(
"org.strongswan.charon-xpc", this->queue,
@@ -7,16 +7,18 @@
objects = {
/* Begin PBXBuildFile section */
5B74984D172AA3550041971E /* xpc_dispatch.c in Sources */ = {isa = PBXBuildFile; fileRef = 5B74984C172AA3550041971E /* xpc_dispatch.c */; };
5B74989217311B200041971E /* xpc_channels.c in Sources */ = {isa = PBXBuildFile; fileRef = 5B74989117311B200041971E /* xpc_channels.c */; };
5BD1CCD71726DB4000587077 /* charon-xpc.c in Sources */ = {isa = PBXBuildFile; fileRef = 5BD1CCD61726DB4000587077 /* charon-xpc.c */; };
5BF60F31173405A000E5D608 /* CoreFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5BD1CCD31726DB4000587077 /* CoreFoundation.framework */; };
5BF60F33173405AC00E5D608 /* Security.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5BD1CCF21727DE3E00587077 /* Security.framework */; };
5BF60F38173405F100E5D608 /* xpc_dispatch.c in Sources */ = {isa = PBXBuildFile; fileRef = 5B74984C172AA3550041971E /* xpc_dispatch.c */; };
5BF60F3E1734070A00E5D608 /* xpc_dispatch.c in Sources */ = {isa = PBXBuildFile; fileRef = 5B74984C172AA3550041971E /* xpc_dispatch.c */; };
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
5B74984C172AA3550041971E /* xpc_dispatch.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = xpc_dispatch.c; sourceTree = "<group>"; };
5B74984E172AA3670041971E /* xpc_dispatch.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = xpc_dispatch.h; sourceTree = "<group>"; };
5B74989017311AFC0041971E /* xpc_channels.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = xpc_channels.h; sourceTree = "<group>"; };
5B74989117311B200041971E /* xpc_channels.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = xpc_channels.c; sourceTree = "<group>"; };
5BD1CCD11726DB4000587077 /* org.strongswan.charon-xpc */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.objfile"; includeInIndex = 0; path = "org.strongswan.charon-xpc"; sourceTree = BUILT_PRODUCTS_DIR; };
5BD1CCD31726DB4000587077 /* CoreFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreFoundation.framework; path = System/Library/Frameworks/CoreFoundation.framework; sourceTree = SDKROOT; };
5BD1CCD61726DB4000587077 /* charon-xpc.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = "charon-xpc.c"; sourceTree = "<group>"; };
@@ -76,6 +78,8 @@
5BD1CCE11726DD9900587077 /* charon-xpc-Info.plist */,
5B74984C172AA3550041971E /* xpc_dispatch.c */,
5B74984E172AA3670041971E /* xpc_dispatch.h */,
5B74989017311AFC0041971E /* xpc_channels.h */,
5B74989117311B200041971E /* xpc_channels.c */,
);
path = "charon-xpc";
sourceTree = "<group>";
@@ -131,7 +135,8 @@
buildActionMask = 2147483647;
files = (
5BD1CCD71726DB4000587077 /* charon-xpc.c in Sources */,
5BF60F38173405F100E5D608 /* xpc_dispatch.c in Sources */,
5B74989217311B200041971E /* xpc_channels.c in Sources */,
5BF60F3E1734070A00E5D608 /* xpc_dispatch.c in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};