2 * Copyright (c) 2013 Apple Inc.
9 #include "SNHelperPrivate.h"
11 static xpc_connection_t
12 create_connection(dispatch_queue_t queue
)
14 xpc_connection_t new_connection
;
16 new_connection
= xpc_connection_create_mach_service(kSNHelperService
, queue
, XPC_CONNECTION_MACH_SERVICE_PRIVILEGED
);
17 if (isa_xpc_connection(new_connection
)) {
18 xpc_connection_set_event_handler(new_connection
,
19 ^(xpc_object_t message
) {
20 if (isa_xpc_error(message
)) {
21 syslog(LOG_INFO
, "Got an error on the snhelper connection");
22 } else if (isa_xpc_dictionary(message
)) {
23 syslog(LOG_INFO
, "Got an unexpected message on the snhelper connection");
26 xpc_connection_resume(new_connection
);
29 return new_connection
;
33 copy_response(xpc_object_t request
)
35 dispatch_queue_t conn_queue
= dispatch_queue_create("snhelper request", NULL
);
36 xpc_connection_t connection
= create_connection(conn_queue
);
37 xpc_object_t response
= NULL
;
40 response
= xpc_connection_send_message_with_reply_sync(connection
, request
);
41 xpc_connection_cancel(connection
);
42 xpc_release(connection
);
45 dispatch_release(conn_queue
);
51 flow_divert_uuid_policy_operate(const uuid_t uuid
, int operation
)
54 xpc_object_t request
= xpc_dictionary_create(NULL
, NULL
, 0);
55 xpc_object_t response
;
57 xpc_dictionary_set_uint64(request
, kSNHelperMessageType
, operation
);
58 xpc_dictionary_set_uuid(request
, kSNHelperMessageUUID
, uuid
);
60 response
= copy_response(request
);
62 if (isa_xpc_dictionary(response
)) {
63 result
= xpc_dictionary_get_int64(request
, kSNHelperMessageResult
);
68 xpc_release(response
);
75 snhelper_flow_divert_uuid_policy_add(const uuid_t uuid
)
77 return flow_divert_uuid_policy_operate(uuid
, kSNHelperMessageTypeFlowDivertUUIDAdd
);
81 snhelper_flow_divert_uuid_policy_remove(const uuid_t uuid
)
83 return flow_divert_uuid_policy_operate(uuid
, kSNHelperMessageTypeFlowDivertUUIDRemove
);
87 snhelper_flow_divert_uuid_policy_clear(void)
90 xpc_object_t request
= xpc_dictionary_create(NULL
, NULL
, 0);
91 xpc_object_t response
;
93 xpc_dictionary_set_uint64(request
, kSNHelperMessageType
, kSNHelperMessageTypeFlowDivertUUIDClear
);
95 response
= copy_response(request
);
97 if (isa_xpc_dictionary(response
)) {
98 result
= xpc_dictionary_get_int64(request
, kSNHelperMessageResult
);
103 xpc_release(response
);
104 xpc_release(request
);
110 isa_xpc_object_of_type(xpc_object_t obj
, xpc_type_t type
)
112 return (obj
!= NULL
&& xpc_get_type(obj
) == type
);
116 isa_xpc_connection(xpc_object_t obj
)
118 return isa_xpc_object_of_type(obj
, XPC_TYPE_CONNECTION
);
122 isa_xpc_bool(xpc_object_t obj
)
124 return isa_xpc_object_of_type(obj
, XPC_TYPE_BOOL
);
128 isa_xpc_dictionary(xpc_object_t obj
)
130 return isa_xpc_object_of_type(obj
, XPC_TYPE_DICTIONARY
);
134 isa_xpc_error(xpc_object_t obj
)
136 return isa_xpc_object_of_type(obj
, XPC_TYPE_ERROR
);