1 /* 2 * xfrd-tcp.h - XFR (transfer) Daemon TCP system header file. Manages tcp conn. 3 * 4 * Copyright (c) 2001-2006, NLnet Labs. All rights reserved. 5 * 6 * See LICENSE for the license. 7 * 8 */ 9 10 #ifndef XFRD_TCP_H 11 #define XFRD_TCP_H 12 13 #include "xfrd.h" 14 #ifdef HAVE_TLS_1_3 15 #include <openssl/ssl.h> 16 #endif 17 18 19 struct buffer; 20 struct xfrd_zone; 21 struct xfrd_soa; 22 struct xfrd_state; 23 struct region; 24 struct dname; 25 struct acl_options; 26 27 struct xfrd_tcp_pipeline; 28 typedef struct xfrd_tcp xfrd_tcp_type; 29 typedef struct xfrd_tcp_set xfrd_tcp_set_type; 30 /* 31 * A set of xfrd tcp connections. 32 */ 33 struct xfrd_tcp_set { 34 /* tcp connections, array, each has packet and read/wr state */ 35 struct xfrd_tcp_pipeline **tcp_state; 36 /* max number of tcp connections, size of tcp_state array */ 37 int tcp_max; 38 /* max number of simultaneous connections on a tcp_pipeline */ 39 int tcp_pipeline; 40 /* number of TCP connections in use. */ 41 int tcp_count; 42 /* TCP timeout. */ 43 int tcp_timeout; 44 /* rbtree with pipelines sorted by master */ 45 rbtree_type* pipetree; 46 #ifdef HAVE_TLS_1_3 47 /* XoT: SSL context */ 48 SSL_CTX* ssl_ctx; 49 #endif 50 /* double linked list of zones waiting for a TCP connection */ 51 struct xfrd_zone *tcp_waiting_first, *tcp_waiting_last; 52 }; 53 54 /* 55 * Structure to keep track of an open tcp connection 56 * The xfrd tcp connection is used to first make a request 57 * Then to receive the answer packet(s). 58 */ 59 struct xfrd_tcp { 60 /* tcp connection state */ 61 /* state: reading or writing */ 62 uint8_t is_reading; 63 64 /* how many bytes have been read/written - total, 65 incl. tcp length bytes */ 66 uint32_t total_bytes; 67 68 /* msg len bytes */ 69 uint16_t msglen; 70 71 /* fd of connection. -1 means unconnected */ 72 int fd; 73 74 /* packet buffer of connection */ 75 struct buffer* packet; 76 }; 77 78 /* use illegal pointer value to denote skipped ID number. 79 * if this does not work, we can allocate with malloc */ 80 #define TCP_NULL_SKIP ((struct xfrd_zone*)-1) 81 82 /** 83 * The per-id zone pointers, with TCP_NULL_SKIP or a zone pointer for the 84 * ID value. 85 */ 86 struct xfrd_tcp_pipeline_id { 87 /** rbtree node as first member, this is the key. */ 88 rbnode_type node; 89 /** the ID of this member */ 90 uint16_t id; 91 /** zone pointer or TCP_NULL_SKIP */ 92 struct xfrd_zone* zone; 93 /** next free in free list */ 94 struct xfrd_tcp_pipeline_id* next_free; 95 }; 96 97 /** 98 * The tcp pipeline key structure. By ip_len, ip, num_unused and unique by 99 * pointer value. 100 */ 101 struct xfrd_tcp_pipeline_key { 102 /* the rbtree node, sorted by IP and nr of unused queries */ 103 rbnode_type node; 104 /* destination IP address */ 105 #ifdef INET6 106 struct sockaddr_storage ip; 107 #else 108 struct sockaddr_in ip; 109 #endif /* INET6 */ 110 socklen_t ip_len; 111 /* number of unused IDs. used IDs are waiting to send their query, 112 * or have been sent but not not all answer packets have been received. 113 * Sorted by num_unused, so a lookup smaller-equal for 65536 finds the 114 * connection to that master that has the most free IDs. */ 115 int num_unused; 116 /* number of skip-set IDs (these are 'in-use') */ 117 int num_skip; 118 }; 119 120 /** 121 * Structure to keep track of a pipelined set of queries on 122 * an open tcp connection. The queries may be answered with 123 * interleaved answer packets, the ID number disambiguates. 124 * Sorted by the master IP address so you can use lookup with 125 * smaller-or-equal to find the tcp connection most suitable. 126 */ 127 struct xfrd_tcp_pipeline { 128 /* the key information for the tcp pipeline, in its own 129 * struct so it can be referenced on its own for comparison funcs */ 130 struct xfrd_tcp_pipeline_key key; 131 132 int handler_added; 133 /* the event handler for this pipe (it'll disambiguate by ID) */ 134 struct event handler; 135 136 /* the tcp connection to use for reading */ 137 struct xfrd_tcp* tcp_r; 138 /* the tcp connection to use for writing, if it is done successfully, 139 * then the first zone from the sendlist can be removed. */ 140 struct xfrd_tcp* tcp_w; 141 /* once a byte has been written, handshake complete */ 142 int connection_established; 143 #ifdef HAVE_TLS_1_3 144 /* XoT: SSL object */ 145 SSL *ssl; 146 /* XoT: if SSL handshake is not done, handshake_want indicates the 147 * last error. This may be SSL_ERROR_WANT_READ or SSL_ERROR_WANT_WRITE 148 * when the handshake is still in progress. 149 */ 150 int handshake_want; 151 /* XoT: 1 if the SSL handshake has succeeded, 0 otherwise */ 152 int handshake_done; 153 #endif 154 155 /* list of queries that want to send, first to get write event, 156 * if NULL, no write event interest */ 157 struct xfrd_zone* tcp_send_first, *tcp_send_last; 158 159 /* size of the id and unused arrays. */ 160 int pipe_num; 161 /* list of free xfrd_tcp_pipeline_id nodes, these are not in the 162 * zone_per_id tree. preallocated at pipe_num amount. */ 163 struct xfrd_tcp_pipeline_id* pipe_id_free_list; 164 /* The xfrd_zone pointers, per id number. 165 * The key is struct xfrd_tcp_pipeline_id. 166 * per-ID number the queries that have this ID number, every 167 * query owns one ID numbers (until it is done). NULL: unused 168 * When a query is done but not all answer-packets have been 169 * consumed for that ID number, the rest is skipped, this 170 * is denoted with the pointer-value TCP_NULL_SKIP, the ids that 171 * are skipped are not on the unused list. They may be 172 * removed once the last answer packet is skipped. 173 * pipe_num-num_unused values are in the tree (either 174 * a zone pointer or SKIP) */ 175 rbtree_type* zone_per_id; 176 /* Array of uint16_t, with ID values. 177 * unused ID numbers; the first part of the array contains the IDs */ 178 uint16_t* unused; 179 }; 180 181 /* create set of tcp connections */ 182 struct xfrd_tcp_set* xfrd_tcp_set_create(struct region* region, const char *tls_cert_bundle, int tcp_max, int tcp_pipeline); 183 184 /* init tcp state */ 185 struct xfrd_tcp* xfrd_tcp_create(struct region* region, size_t bufsize); 186 /* obtain tcp connection for a zone (or wait) */ 187 void xfrd_tcp_obtain(struct xfrd_tcp_set* set, struct xfrd_zone* zone); 188 /* release tcp connection for a zone (starts waiting) */ 189 void xfrd_tcp_release(struct xfrd_tcp_set* set, struct xfrd_zone* zone); 190 /* release tcp pipe entirely (does not stop the zones inside it) */ 191 void xfrd_tcp_pipe_release(struct xfrd_tcp_set* set, 192 struct xfrd_tcp_pipeline* tp, int conn); 193 /* use tcp connection to start xfr */ 194 void xfrd_tcp_setup_write_packet(struct xfrd_tcp_pipeline* tp, 195 struct xfrd_zone* zone); 196 /* initialize tcp_state for a zone. Opens the connection. true on success.*/ 197 int xfrd_tcp_open(struct xfrd_tcp_set* set, struct xfrd_tcp_pipeline* tp, 198 struct xfrd_zone* zone); 199 /* read data from tcp, maybe partial read */ 200 void xfrd_tcp_read(struct xfrd_tcp_pipeline* tp); 201 /* write data to tcp, maybe a partial write */ 202 void xfrd_tcp_write(struct xfrd_tcp_pipeline* tp, struct xfrd_zone* zone); 203 /* handle tcp pipe events */ 204 void xfrd_handle_tcp_pipe(int fd, short event, void* arg); 205 206 /* 207 * Read from a stream connection (size16)+packet into buffer. 208 * returns value is 209 * -1 on error. 210 * 0 on short read, call back later. 211 * 1 on completed read. 212 * On first call, make sure total_bytes = 0, msglen=0, buffer_clear(). 213 * and the packet and fd need to be set. 214 */ 215 int conn_read(struct xfrd_tcp* conn); 216 /* 217 * Write to a stream connection (size16)+packet. 218 * return value is 219 * -1 on error. 0 on short write, call back later. 1 completed write. 220 * On first call, make sure total_bytes=0, msglen=buffer_limit(), 221 * buffer_flipped(). packet and fd need to be set. 222 */ 223 int conn_write(struct xfrd_tcp* conn); 224 225 /* setup DNS packet for a query of this type */ 226 void xfrd_setup_packet(struct buffer* packet, 227 uint16_t type, uint16_t klass, const struct dname* dname, uint16_t qid); 228 /* write soa in network format to the packet buffer */ 229 void xfrd_write_soa_buffer(struct buffer* packet, 230 const struct dname* apex, struct xfrd_soa* soa); 231 /* use acl address to setup sockaddr struct, returns length of addr. */ 232 socklen_t xfrd_acl_sockaddr_to(struct acl_options* acl, 233 #ifdef INET6 234 struct sockaddr_storage *to); 235 #else 236 struct sockaddr_in *to); 237 #endif /* INET6 */ 238 239 socklen_t xfrd_acl_sockaddr_frm(struct acl_options* acl, 240 #ifdef INET6 241 struct sockaddr_storage *frm); 242 #else 243 struct sockaddr_in *frm); 244 #endif /* INET6 */ 245 246 /* create pipeline tcp structure */ 247 struct xfrd_tcp_pipeline* xfrd_tcp_pipeline_create(region_type* region, 248 int tcp_pipeline); 249 /* pick num uint16_t values, from 0..max-1, store in array */ 250 void pick_id_values(uint16_t* array, int num, int max); 251 252 #endif /* XFRD_TCP_H */ 253