xpc: use IKE_SA specific XPC return channels for further communication
[strongswan.git] / src / frontends / osx / charon-xpc / xpc_channels.c
1 /*
2 * Copyright (C) 2013 Martin Willi
3 * Copyright (C) 2013 revosec AG
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "xpc_channels.h"
17
18 #include <collections/hashtable.h>
19 #include <threading/rwlock.h>
20 #include <daemon.h>
21
22 typedef struct private_xpc_channels_t private_xpc_channels_t;
23
24 /**
25 * Private data of an xpc_channels_t object.
26 */
27 struct private_xpc_channels_t {
28
29 /**
30 * Public xpc_channels_t interface.
31 */
32 xpc_channels_t public;
33
34 /**
35 * Registered channels, IKE_SA unique ID => entry_t
36 */
37 hashtable_t *channels;
38
39 /**
40 * Lock for channels list
41 */
42 rwlock_t *lock;
43 };
44
45 /**
46 * Channel entry
47 */
48 typedef struct {
49 /* XPC channel to App */
50 xpc_connection_t conn;
51 /* associated IKE_SA unique identifier */
52 uintptr_t sa;
53 } entry_t;
54
55 /**
56 * Clean up an entry, cancelling connection
57 */
58 static void destroy_entry(entry_t *entry)
59 {
60 xpc_connection_suspend(entry->conn);
61 xpc_connection_cancel(entry->conn);
62 xpc_release(entry->conn);
63 free(entry);
64 }
65
66 /**
67 * Remove an entry for a given XPC connection
68 */
69 static void remove_conn(private_xpc_channels_t *this, xpc_connection_t conn)
70 {
71 enumerator_t *enumerator;
72 entry_t *entry;
73
74 this->lock->write_lock(this->lock);
75 enumerator = this->channels->create_enumerator(this->channels);
76 while (enumerator->enumerate(enumerator, NULL, &entry))
77 {
78 if (xpc_equal(entry->conn, conn))
79 {
80 this->channels->remove(this->channels, enumerator);
81 destroy_entry(entry);
82 break;
83 }
84 }
85 enumerator->destroy(enumerator);
86 this->lock->unlock(this->lock);
87 }
88
89 /**
90 * Handle a request message from App
91 */
92 static void handle(private_xpc_channels_t *this, xpc_object_t request)
93 {
94 /* TODO: */
95 }
96
97 METHOD(xpc_channels_t, add, void,
98 private_xpc_channels_t *this, xpc_connection_t conn, u_int32_t ike_sa)
99 {
100 entry_t *entry;
101
102 INIT(entry,
103 .conn = conn,
104 .sa = ike_sa,
105 );
106
107 xpc_connection_set_event_handler(entry->conn, ^(xpc_object_t event) {
108
109 if (event == XPC_ERROR_CONNECTION_INVALID ||
110 event == XPC_ERROR_CONNECTION_INTERRUPTED)
111 {
112 remove_conn(this, entry->conn);
113 }
114 else
115 {
116 handle(this, event);
117 }
118 });
119
120 this->lock->write_lock(this->lock);
121 this->channels->put(this->channels, (void*)entry->sa, entry);
122 this->lock->unlock(this->lock);
123
124 xpc_connection_resume(conn);
125 }
126
127 METHOD(listener_t, ike_rekey, bool,
128 private_xpc_channels_t *this, ike_sa_t *old, ike_sa_t *new)
129 {
130 entry_t *entry;
131 uintptr_t sa;
132
133 sa = old->get_unique_id(old);
134 this->lock->write_lock(this->lock);
135 entry = this->channels->remove(this->channels, (void*)sa);
136 if (entry)
137 {
138 entry->sa = new->get_unique_id(new);
139 this->channels->put(this->channels, (void*)entry->sa, entry);
140 }
141 this->lock->unlock(this->lock);
142
143 return TRUE;
144 }
145
146 METHOD(listener_t, ike_updown, bool,
147 private_xpc_channels_t *this, ike_sa_t *ike_sa, bool up)
148 {
149 xpc_object_t msg;
150 entry_t *entry;
151 uintptr_t sa;
152
153 sa = ike_sa->get_unique_id(ike_sa);
154 if (up)
155 {
156 this->lock->read_lock(this->lock);
157 entry = this->channels->get(this->channels, (void*)sa);
158 if (entry)
159 {
160 msg = xpc_dictionary_create(NULL, NULL, 0);
161 xpc_dictionary_set_string(msg, "type", "event");
162 xpc_dictionary_set_string(msg, "event", "up");
163 xpc_connection_send_message(entry->conn, msg);
164 xpc_release(msg);
165 }
166 this->lock->unlock(this->lock);
167 }
168 else
169 {
170 this->lock->write_lock(this->lock);
171 entry = this->channels->remove(this->channels, (void*)sa);
172 this->lock->unlock(this->lock);
173 if (entry)
174 {
175 msg = xpc_dictionary_create(NULL, NULL, 0);
176 xpc_dictionary_set_string(msg, "type", "event");
177 xpc_dictionary_set_string(msg, "event", "down");
178 xpc_connection_send_message(entry->conn, msg);
179 xpc_release(msg);
180 xpc_connection_send_barrier(entry->conn, ^() {
181 destroy_entry(entry);
182 });
183 }
184 }
185 return TRUE;
186 }
187
188 METHOD(xpc_channels_t, destroy, void,
189 private_xpc_channels_t *this)
190 {
191 enumerator_t *enumerator;
192 entry_t *entry;
193
194 enumerator = this->channels->create_enumerator(this->channels);
195 while (enumerator->enumerate(enumerator, NULL, &entry))
196 {
197 destroy_entry(entry);
198 }
199 enumerator->destroy(enumerator);
200
201 this->channels->destroy(this->channels);
202 this->lock->destroy(this->lock);
203 free(this);
204 }
205
206 /**
207 * See header
208 */
209 xpc_channels_t *xpc_channels_create()
210 {
211 private_xpc_channels_t *this;
212
213 INIT(this,
214 .public = {
215 .listener = {
216 .ike_updown = _ike_updown,
217 .ike_rekey = _ike_rekey,
218 },
219 .add = _add,
220 .destroy = _destroy,
221 },
222 .channels = hashtable_create(hashtable_hash_ptr,
223 hashtable_equals_ptr, 4),
224 .lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
225 );
226
227 return &this->public;
228 }