xpc: move XPC RPC reply creation to command dispatching

This commit is contained in:
Martin Willi
2013-07-18 12:17:55 +02:00
parent a0c125eacb
commit 790ad9e677
+16 -24
View File
@@ -64,15 +64,10 @@ struct private_xpc_dispatch_t {
/** /**
* Return version of this helper * Return version of this helper
*/ */
static xpc_object_t get_version(private_xpc_dispatch_t *this, static void get_version(private_xpc_dispatch_t *this,
xpc_object_t request, xpc_connection_t client) xpc_object_t request, xpc_object_t reply)
{ {
xpc_object_t reply;
reply = xpc_dictionary_create_reply(request);
xpc_dictionary_set_string(reply, "version", PACKAGE_VERSION); xpc_dictionary_set_string(reply, "version", PACKAGE_VERSION);
return reply;
} }
/** /**
@@ -164,10 +159,9 @@ static bool initiate_cb(u_int32_t *sa, debug_t group, level_t level,
/** /**
* Start initiating an IKE connection * Start initiating an IKE connection
*/ */
xpc_object_t start_connection(private_xpc_dispatch_t *this, void start_connection(private_xpc_dispatch_t *this,
xpc_object_t request, xpc_connection_t client) xpc_object_t request, xpc_object_t reply)
{ {
xpc_object_t reply;
peer_cfg_t *peer_cfg; peer_cfg_t *peer_cfg;
child_cfg_t *child_cfg; child_cfg_t *child_cfg;
char *name, *id, *host; char *name, *id, *host;
@@ -181,7 +175,6 @@ xpc_object_t start_connection(private_xpc_dispatch_t *this,
id = (char*)xpc_dictionary_get_string(request, "id"); id = (char*)xpc_dictionary_get_string(request, "id");
endpoint = xpc_dictionary_get_value(request, "channel"); endpoint = xpc_dictionary_get_value(request, "channel");
channel = xpc_connection_create_from_endpoint(endpoint); channel = xpc_connection_create_from_endpoint(endpoint);
reply = xpc_dictionary_create_reply(request);
if (name && id && host && channel) if (name && id && host && channel)
{ {
@@ -202,8 +195,6 @@ xpc_object_t start_connection(private_xpc_dispatch_t *this,
} }
xpc_dictionary_set_bool(reply, "success", success); xpc_dictionary_set_bool(reply, "success", success);
return reply;
} }
/** /**
@@ -211,8 +202,8 @@ xpc_object_t start_connection(private_xpc_dispatch_t *this,
*/ */
static struct { static struct {
char *name; char *name;
xpc_object_t (*handler)(private_xpc_dispatch_t *this, void (*handler)(private_xpc_dispatch_t *this,
xpc_object_t request, xpc_connection_t client); xpc_object_t request, xpc_object_t reply);
} commands[] = { } commands[] = {
{ "get_version", get_version }, { "get_version", get_version },
{ "start_connection", start_connection }, { "start_connection", start_connection },
@@ -229,33 +220,34 @@ static void handle(private_xpc_dispatch_t *this, xpc_object_t request)
bool found = FALSE; bool found = FALSE;
int i; int i;
client = xpc_dictionary_get_remote_connection(request);
type = xpc_dictionary_get_string(request, "type"); type = xpc_dictionary_get_string(request, "type");
if (type) if (type)
{ {
if (streq(type, "rpc")) if (streq(type, "rpc"))
{ {
reply = xpc_dictionary_create_reply(request);
rpc = xpc_dictionary_get_string(request, "rpc"); rpc = xpc_dictionary_get_string(request, "rpc");
if (rpc) if (reply && rpc)
{ {
for (i = 0; i < countof(commands); i++) for (i = 0; i < countof(commands); i++)
{ {
if (streq(commands[i].name, rpc)) if (streq(commands[i].name, rpc))
{ {
found = TRUE; found = TRUE;
reply = commands[i].handler(this, request, client); commands[i].handler(this, request, reply);
if (reply)
{
xpc_connection_send_message(client, reply);
xpc_release(reply);
}
break; break;
} }
} }
} }
if (!found) if (!found)
{ {
DBG1(DBG_CFG, "received unknown XPC rpc command: %s", rpc); DBG1(DBG_CFG, "received invalid XPC rpc command: %s", rpc);
}
if (reply)
{
client = xpc_dictionary_get_remote_connection(request);
xpc_connection_send_message(client, reply);
xpc_release(reply);
} }
} }
else else