2 * Copyright (c) 2013, 2014 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
= (int)xpc_dictionary_get_int64(response
, 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
= (int)xpc_dictionary_get_int64(response
, kSNHelperMessageResult
);
103 xpc_release(response
);
104 xpc_release(request
);
110 snhelper_get_uuid_for_app(const char *appID
, uuid_t uuid
)
113 xpc_object_t request
= xpc_dictionary_create(NULL
, NULL
, 0);
114 xpc_object_t response
;
116 xpc_dictionary_set_uint64(request
, kSNHelperMessageType
, kSNHelperMessageTypeGetUUIDForApp
);
117 xpc_dictionary_set_string(request
, kSNHelperMessageAppID
, appID
);
119 response
= copy_response(request
);
121 if (isa_xpc_dictionary(response
)) {
122 result
= (int)xpc_dictionary_get_int64(response
, kSNHelperMessageResult
);
123 const uint8_t *uuidBytes
= xpc_dictionary_get_uuid(response
, kSNHelperMessageResultData
);
124 if (result
== 0 && uuid
!= NULL
&& uuidBytes
!= NULL
) {
125 memcpy(uuid
, uuidBytes
, sizeof(uuid_t
));
131 xpc_release(response
);
132 xpc_release(request
);
138 isa_xpc_object_of_type(xpc_object_t obj
, xpc_type_t type
)
140 return (obj
!= NULL
&& xpc_get_type(obj
) == type
);
144 isa_xpc_connection(xpc_object_t obj
)
146 return isa_xpc_object_of_type(obj
, XPC_TYPE_CONNECTION
);
150 isa_xpc_bool(xpc_object_t obj
)
152 return isa_xpc_object_of_type(obj
, XPC_TYPE_BOOL
);
156 isa_xpc_dictionary(xpc_object_t obj
)
158 return isa_xpc_object_of_type(obj
, XPC_TYPE_DICTIONARY
);
162 isa_xpc_error(xpc_object_t obj
)
164 return isa_xpc_object_of_type(obj
, XPC_TYPE_ERROR
);