]> git.saurik.com Git - apple/mdnsresponder.git/blob - DSO/dso-transport.h
mDNSResponder-1096.100.3.tar.gz
[apple/mdnsresponder.git] / DSO / dso-transport.h
1 /* dso-transport.h
2 *
3 * Copyright (c) 2018-2019 Apple Computer, Inc. All rights reserved.
4 *
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
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 *
17 */
18
19 #ifndef __DSO_TRANSPORT_H
20 #define __DSO_TRANSPORT_H
21
22 #ifdef DSO_USES_NETWORK_FRAMEWORK
23 #include <Network/Network.h>
24 #endif
25
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
29
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
36
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
43
44 #ifdef DSO_USES_NETWORK_FRAMEWORK
45 nw_connection_t connection;
46 dispatch_data_t to_write;
47 size_t bytes_to_write;
48 size_t unsent_bytes;
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
51 #else
52 TCPSocket *connection; // Socket connected to Discovery Proxy
53 size_t bytes_needed;
54 size_t message_length; // Length of message we are currently accumulating, if known
55 uint8_t *inbuf; // Buffer for incoming messages.
56 size_t inbuf_size;
57 uint8_t *inbufp; // Current read pointer (may not be in inbuf)
58 bool need_length; // True if we need a 2-byte length
59
60 uint8_t lenbuf[2]; // Buffer for storing the length in a DNS TCP message
61
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];
65 int num_to_write;
66 #endif // DSO_USES_NETWORK_FRAMEWORK
67
68 uint8_t *outbuf; // Output buffer for building and sending DSO messages
69 size_t outbuf_size;
70 };
71
72 typedef struct dso_lookup dso_lookup_t;
73 struct dso_lookup {
74 dso_lookup_t *next;
75 DNSServiceRef sdref;
76 };
77
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;
82 dso_state_t *dso;
83 char *detail;
84 void *context;
85 TCPListener *listener;
86
87 char *hostname;
88 int num_addrs;
89 int cur_addr;
90 mDNSAddr addresses[MAX_DSO_CONNECT_ADDRS];
91 mDNSIPPort ports[MAX_DSO_CONNECT_ADDRS];
92 DNSServiceRef lookup;
93
94 mDNSBool connecting;
95 mDNSIPPort config_port, connect_port;
96 #ifdef DSO_USES_NETWORK_FRAMEWORK
97 uint32_t serial;
98 nw_connection_t connection;
99 bool tls_enabled;
100 #else
101 size_t inbuf_size;
102 #endif
103 size_t outbuf_size;
104 int max_outstanding_queries;
105 mDNSs32 last_event;
106 mDNSs32 reconnect_time;
107 };
108
109 typedef struct {
110 TCPListener *listener;
111 dso_event_callback_t callback;
112 void *context;
113 } dso_listen_context_t;
114
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);
127 #endif
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
135
136 // Local Variables:
137 // mode: C
138 // tab-width: 4
139 // c-file-style: "bsd"
140 // c-basic-offset: 4
141 // fill-column: 108
142 // indent-tabs-mode: nil
143 // End: