How to convert Json Response to Byte Array and Send to WriteDatagram

I am implemented NetworkExtension Framework, I am using DNS Proxy capability here I am called Handlenewflow method getting remotehostname

based on hostname we are calling my local api getting response

So I need to convert those data and pass to WriteDatagram

//needs to be converted back into a (binary) DNS response for the OS

OS X never given any authorization to the device, personal information and etc disabled applicant under the senior citizen act, Apple iOS Customer never given any authorization whatsoever to third-party only Apple and T-Mobile services as well. Samsung on products who is my accommodation request to convert to original iOS Apple, only and nothing else add-ons third-party services denied removal, correct as soon as possible.

@davidleepablo What is this?

Mash86 wrote:

What is this?

I’ve no idea. I’ve reported it to the moderators, so let’s just focus on the original question.


So I need to convert those data and pass to WriteDatagram

So you’ve issued some sort of Rest call and it’s returned a DNS response encoded as a byte array and you want need to pass that to writeDatagrams(_:sentBy:completionHandler:) method on NEAppProxyUDPFlow. Right?

If so, I have some questions:

  • What language are you programming in?

  • What does the JSON response actually look like? Can you post an example?

  • How are you parsing this JSON response? Using Swift’s Codable feature? Or something else?

IMPORTANT If you post a code snippet, including JSON, put it in a code block, otherwise it’ll be unreadable. See tip 5 in Quinn’s Top Ten DevForums Tips for more.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

What language are you programming in? : Swift UI , Swift

What does the JSON response actually look like? Can you post an example?

[

    "Status" : 0,

    "TC": false,

    "RD": true,

    "RA": true,

    "AD": false,

    "CD": false,

    "Question": [

        [

            "name": "google.com.",

            "type": 1

        ]

    ],

    "Answer": [

        [

            "name": "google.com.",

            "type": 1,

            "TTL": 600,

            "data": "142.251.163.102"

        ],

        [

            "name": "google.com.",

            "type": 1,

            "TTL": 600,

            "data": "142.251.163.139"

        ],

        [

            "name": "google.com.",

            "type": 1,

            "TTL": 600,

            "data": "142.251.163.101"

        ],

        

    ]

]

How are you parsing this JSON response? Using Swift’s Codable feature? Or something else? : Combine framework

try JSONSerialization.data(withJSONObject: response, options: JSONSerialization.WritingOptions.prettyPrinted)

Oh that’s interesting. There is no system API that will take a JSON value like this and serialise it to the binary DNS message format. You’ll need to write or acquire your own code for that. That involves two steps:

  1. Parsing the JSON to some sort of model-level value.

  2. Rendering that model value into the binary DNS message format.

The first is relatively straightforward for the data you posted. I’ve included a snippet below.

IMPORTANT This only works on the data you posted. DNS messages have a lot of flexibility and, if your server sends you JSON in some other folks, you’ll need to adapt the code accordingly.

If you decide to write your own code for the second step, that’s significantly harder. You have to understanding the binary DNS message format. You can find a high-level summary here but I think you’re eventually going to want to read the standards, RFC 1034 and RFC 1035.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"


let json = """
    {
        "Status": 0,
        "TC": false,
        "RD": true,
        "RA": true,
        "AD": false,
        "CD": false,
        "Question": [{
            "name": "google.com.",
            "type": 1
        }],
        "Answer": [{
                "name": "google.com.",
                "type": 1,
                "TTL": 600,
                "data": "142.251.163.102"
            },
            {
                "name": "google.com.",
                "type": 1,
                "TTL": 600,
                "data": "142.251.163.139"
            },
            {
                "name": "google.com.",
                "type": 1,
                "TTL": 600,
                "data": "142.251.163.101"
            }
        ]
    }
    """

struct JSONDNSReply: Codable {
    var status: Int
    var tc: Bool
    var rd: Bool
    var ra: Bool
    var ad: Bool
    var cd: Bool
    
    var questions: [Question]
    var answers: [Answer]

    // Fix the case on all the fields and the pluralisation on the question and
    // answer fields.
    enum CodingKeys: String, CodingKey {
        case status = "Status"
        case tc = "TC"
        case rd = "RD"
        case ra = "RA"
        case ad = "AD"
        case cd = "CD"
        case questions = "Question"
        case answers = "Answer"
    }

    struct Question: Codable {
        var name: String
        var type: Int
    }

    struct Answer: Codable {
        var name: String
        var type: Int
        var ttl: Int
        var data: String
        
        // Fix the case on `TTL`
        enum CodingKeys: String, CodingKey {
            case name = "name"
            case type = "type"
            case ttl = "TTL"
            case data = "data"
        }
    }
}

func test() throws {
    let data = Data(json.utf8)
    let reply = try JSONDecoder().decode(JSONDNSReply.self, from: data)
    print(reply)
}

Hi @eskimo,   I apologise for not putting my query in proper formatted text.  

Ok as per your suggestion I will use codable format for above JSON and then will go for conversion of JSON to Binary DNS Messages format. Firstly Im not going to write my own code for this as I know it's very difficult for me so I will go for some third party options. But important question here I want to ask to you that, These are the steps that Im trying to achieve are these correct?

  1. After getting the call in handleNewFlow method, Im taking the hostname and sending it to our own DNS resolver to resolve.
  2. After getting the JSON response from our DNS resolver, what should we do? - This is the case which we want to you provide your input. because my assumption is that after getting JSON response from our resolver, we will convert that JSON to DNS binary messages format and will send it to writeDataGrams? Is this correct way or not?

  Because as per developer documentation and some of the different posts on developer forum there is established sequence to process the flow after getting the call in handleNewFlow method. and Im not very much clear about it how to use that flow to resolve received query to our custom DNS resolver. or how can we use our custom DNS resolver to resolve queries?

From all of this, our basic requriement and expectation of our client is to resolve all the queries through our own custom DNS resolver.

DNS is a query/reply protocol. The system calls your DNS proxy with a series of flows. On each flow, you’ll read a series of queries. Your proxy is expected to resolve each query and return a reply on the same flow. How you get the reply from the query is up to you.

Each flow is either TCP or UDP:

  • For TCP, you read bytes from the flow and must unframe them to get the stream of queries. To respond to a query you must first frame your reply and then write the bytes to the flow.

  • For UDP, you read datagrams from the flow, where each datagram is a query. To respond to a query you must simply write the reply to the flow as a datagram.

How you resolve a query to a reply is entire up to you. You could, for example, have a fixed table of records and run the query against that. Most folks, however, do this by reaching out across the network. And how you do that depends on what protocol that server speaks.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

How to convert Json Response to Byte Array and Send to WriteDatagram
 
 
Q