3 * Copyright (c) 2018-2019 Apple Computer, Inc. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 #ifndef __DSO_TRANSPORT_H
20 #define __DSO_TRANSPORT_H
22 #ifdef DSO_USES_NETWORK_FRAMEWORK
23 #include <Network/Network.h>
26 // Maximum number of IP addresses that we'll deal with as a result of looking up a name
27 // to which to connect.
28 #define MAX_DSO_CONNECT_ADDRS 16
30 // Threshold above which we indicate that a DSO connection isn't writable. This is advisory,
31 // but e.g. for a Discovery Relay, if the remote proxy isn't consuming what we are sending, we
32 // should start dropping packets on the floor rather than just queueing more and more packets.
33 // 60k may actually be too much. This is used when we're using NW Framework, because it doesn't
34 // allow us to use TCP_NOTSENT_LOWAT directly.
35 #define MAX_UNSENT_BYTES 60000
37 struct dso_transport
{
38 dso_state_t
*dso
; // DSO state for which this is the transport
39 struct dso_transport
*next
; // Transport is on list of transports.
40 void *event_context
; // I/O event context
41 mDNSAddr remote_addr
; // The IP address to which we have connected
42 int remote_port
; // The port to which we have connected
44 #ifdef DSO_USES_NETWORK_FRAMEWORK
45 nw_connection_t connection
;
46 dispatch_data_t to_write
;
47 size_t bytes_to_write
;
49 uint32_t serial
; // Serial number for locating possibly freed dso_transport_t structs
50 bool write_failed
; // This is set if any of the parts of the dso_write process fail
52 TCPSocket
*connection
; // Socket connected to Discovery Proxy
54 size_t message_length
; // Length of message we are currently accumulating, if known
55 uint8_t *inbuf
; // Buffer for incoming messages.
57 uint8_t *inbufp
; // Current read pointer (may not be in inbuf)
58 bool need_length
; // True if we need a 2-byte length
60 uint8_t lenbuf
[2]; // Buffer for storing the length in a DNS TCP message
62 #define MAX_WRITE_HUNKS 4 // When writing a DSO message, we need this many separate hunks.
63 const uint8_t *to_write
[MAX_WRITE_HUNKS
];
64 ssize_t write_lengths
[MAX_WRITE_HUNKS
];
66 #endif // DSO_USES_NETWORK_FRAMEWORK
68 uint8_t *outbuf
; // Output buffer for building and sending DSO messages
72 typedef struct dso_lookup dso_lookup_t
;
78 typedef struct dso_connect_state dso_connect_state_t
;
79 struct dso_connect_state
{
80 dso_connect_state_t
*next
;
81 dso_event_callback_t callback
;
85 TCPListener
*listener
;
90 mDNSAddr addresses
[MAX_DSO_CONNECT_ADDRS
];
91 mDNSIPPort ports
[MAX_DSO_CONNECT_ADDRS
];
95 mDNSIPPort config_port
, connect_port
;
96 #ifdef DSO_USES_NETWORK_FRAMEWORK
98 nw_connection_t connection
;
104 int max_outstanding_queries
;
106 mDNSs32 reconnect_time
;
110 TCPListener
*listener
;
111 dso_event_callback_t callback
;
113 } dso_listen_context_t
;
115 void dso_transport_init(void);
116 mStatus
dso_set_connection(dso_state_t
*dso
, TCPSocket
*socket
);
117 void dso_schedule_reconnect(mDNS
*m
, dso_connect_state_t
*cs
, mDNSs32 when
);
118 void dso_set_callback(dso_state_t
*dso
, void *context
, dso_event_callback_t cb
);
119 mStatus
dso_message_write(dso_state_t
*dso
, dso_message_t
*msg
, bool disregard_low_water
);
120 dso_connect_state_t
*dso_connect_state_create(const char *host
, mDNSAddr
*addr
, mDNSIPPort port
,
121 int num_outstanding_queries
,
122 size_t inbuf_size
, size_t outbuf_size
,
123 dso_event_callback_t callback
,
124 dso_state_t
*dso
, void *context
, const char *detail
);
125 #ifdef DSO_USES_NETWORK_FRAMEWORK
126 void dso_connect_state_use_tls(dso_connect_state_t
*cs
);
128 void dso_connect_state_drop(dso_connect_state_t
*cs
);
129 bool dso_connect(dso_connect_state_t
*connect_state
);
130 mStatus
dso_listen(dso_connect_state_t
*listen_context
);
131 bool dso_write_start(dso_transport_t
*transport
, size_t length
);
132 bool dso_write_finish(dso_transport_t
*transport
);
133 void dso_write(dso_transport_t
*transport
, const uint8_t *buf
, size_t length
);
134 #endif // __DSO_TRANSPORT_H
139 // c-file-style: "bsd"
142 // indent-tabs-mode: nil