summaryrefslogtreecommitdiffstats
path: root/mDNSResponder/mDNSMacOSX/CUPolicy.c
blob: 434e65cdba9bee760cc20be7d0ec2ec8d3f6f5ae (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* -*- Mode: C; tab-width: 4 -*-
 *
 * Copyright (c) 2013 Apple Computer, Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "mDNSMacOSX.h"
#include <network/config.h>

#if TARGET_OS_IPHONE

mDNSexport void CUPInit(mDNS *const m)
{
    
    m->p->handle = cellular_usage_policy_create_client();
    if (!m->p->handle)
    {
        LogMsg("CUPInit: cellular_usage_policy_create_client failed");
    }
}

mDNSexport mDNSBool mDNSPlatformAllowPID(mDNS *const m, DNSQuestion *q)
{
    // Currently the policy applies only for DNS requests sent over cellular interface
    if (m->p->handle && q->qDNSServer && q->qDNSServer->cellIntf)
    {
        mDNSBool allowed;
        if (q->pid)
        {
            allowed = (mDNSBool) cellular_usage_policy_is_data_allowed_for_pid(m->p->handle, q->pid);
            if (!allowed)
            {
                xpc_object_t pidx = xpc_uint64_create(q->pid);
                if (pidx)
                {
                    network_config_cellular_blocked_notify(pidx, NULL, NULL);
                    LogInfo("mDNSPlaformAllowPID: Notified PID(%d) for %##s (%s)", q->pid, q->qname.c, DNSTypeName(q->qtype));
                    xpc_release(pidx);
                }
            }
        }
        else
        {
            allowed = (mDNSBool) cellular_usage_policy_is_data_allowed_for_uuid(m->p->handle, q->uuid);
            if (!allowed)
            {
                xpc_object_t uuidx = xpc_uuid_create(q->uuid);
                if (uuidx)
                {
                    network_config_cellular_blocked_notify(NULL, uuidx, NULL);
                    LogInfo("mDNSPlaformAllowPID: Notified UUID for %##s (%s)", q->qname.c, DNSTypeName(q->qtype));
                    xpc_release(uuidx);
                }
            }
        }
        return allowed;
    }
    else
    {
        return mDNStrue;
    }
}

#else // TARGET_OS_IPHONE

mDNSexport void CUPInit(mDNS *const m)
{
    (void)m; //unused
}

mDNSexport mDNSBool mDNSPlatformAllowPID(mDNS *const m, DNSQuestion *q)
{
    (void)m;    //unused
    (void)q;    //unused
    //LogMsg("mDNSPlatformAllowPID: %##s (%s)", q->qname.c, DNSTypeName(q->qtype));
    return mDNStrue;
}

#endif // TARGET_OS_IPHONE