Logo Search packages:      
Sourcecode: unbound version File versions  Download package

outside_network.h

Go to the documentation of this file.
/*
 * services/outside_network.h - listen to answers from the network
 *
 * Copyright (c) 2007, NLnet Labs. All rights reserved.
 *
 * This software is open source.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * 
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * 
 * Neither the name of the NLNET LABS nor the names of its contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/**
 * \file
 *
 * This file has functions to send queries to authoritative servers,
 * and wait for the pending answer, with timeouts.
 */

#ifndef OUTSIDE_NETWORK_H
#define OUTSIDE_NETWORK_H

#include "config.h"
#include "util/rbtree.h"
#include "util/netevent.h"
struct pending;
struct pending_timeout;
struct ub_randstate;
struct pending_tcp;
struct waiting_tcp;
struct waiting_udp;
struct infra_cache;
struct port_comm;
struct port_if;

/**
 * Send queries to outside servers and wait for answers from servers.
 * Contains answer-listen sockets.
 */
00063 struct outside_network {
      /** Base for select calls */
00065       struct comm_base* base;
      /** pointer to time in seconds */
00067       uint32_t* now_secs;
      /** pointer to time in microseconds */
00069       struct timeval* now_tv;

      /** buffer shared by UDP connections, since there is only one
          datagram at any time. */
00073       ldns_buffer* udp_buff;
      /** serviced_callbacks malloc overhead when processing multiple
       * identical serviced queries to the same server. */
00076       size_t svcd_overhead;
      /** use x20 bits to encode additional ID random bits */
00078       int use_caps_for_id;

      /** linked list of available commpoints, unused file descriptors,
       * for use as outgoing UDP ports. cp.fd=-1 in them. */
00082       struct port_comm* unused_fds;

      /** array of outgoing IP4 interfaces */
00085       struct port_if* ip4_ifs;
      /** number of outgoing IP4 interfaces */
00087       int num_ip4;

      /** array of outgoing IP6 interfaces */
00090       struct port_if* ip6_ifs;
      /** number of outgoing IP6 interfaces */
00092       int num_ip6;

      /** pending udp queries waiting to be sent out, waiting for fd */
00095       struct pending* udp_wait_first;
      /** last pending udp query in list */
00097       struct pending* udp_wait_last;

      /** pending udp answers. sorted by id, addr */
00100       rbtree_t* pending;
      /** serviced queries, sorted by qbuf, addr, dnssec */
00102       rbtree_t* serviced;
      /** host cache, pointer but not owned by outnet. */
00104       struct infra_cache* infra;
      /** where to get random numbers */
00106       struct ub_randstate* rnd;

      /**
       * Array of tcp pending used for outgoing TCP connections.
       * Each can be used to establish a TCP connection with a server.
       * The file descriptors are -1 if they are free, and need to be 
       * opened for the tcp connection. Can be used for ip4 and ip6.
       */
00114       struct pending_tcp **tcp_conns;
      /** number of tcp communication points. */
00116       size_t num_tcp;
      /** list of tcp comm points that are free for use */
00118       struct pending_tcp* tcp_free;
      /** list of tcp queries waiting for a buffer */
00120       struct waiting_tcp* tcp_wait_first;
      /** last of waiting query list */
00122       struct waiting_tcp* tcp_wait_last;
};

/**
 * Outgoing interface. Ports available and currently used are tracked
 * per interface
 */
00129 struct port_if {
      /** address ready to allocate new socket (except port no). */
00131       struct sockaddr_storage addr;
      /** length of addr field */
00133       socklen_t addrlen;

      /** the available ports array. These are unused.
       * Only the first total-inuse part is filled. */
00137       int* avail_ports;
      /** the total number of available ports (size of the array) */
00139       int avail_total;

      /** array of the commpoints currently in use. 
       * allocated for max number of fds, first part in use. */
00143       struct port_comm** out;
      /** max number of fds, size of out array */
00145       int maxout;
      /** number of commpoints (and thus also ports) in use */
00147       int inuse;
};

/**
 * Outgoing commpoint for UDP port.
 */
00153 struct port_comm {
      /** next in free list */
00155       struct port_comm* next;
      /** which port number (when in use) */
00157       int number;
      /** interface it is used in */
00159       struct port_if* pif;
      /** index in the out array of the interface */
00161       int index;
      /** number of outstanding queries on this port */
00163       int num_outstanding;
      /** UDP commpoint, fd=-1 if not in use */
00165       struct comm_point* cp;
};

/**
 * A query that has an answer pending for it.
 */
00171 struct pending {
      /** redblacktree entry, key is the pending struct(id, addr). */
00173       rbnode_t node;
      /** the ID for the query. int so that a value out of range can
       * be used to signify a pending that is for certain not present in
       * the rbtree. (and for which deletion is safe). */
00177       unsigned int id;
      /** remote address. */
00179       struct sockaddr_storage addr;
      /** length of addr field in use. */
00181       socklen_t addrlen;
      /** comm point it was sent on (and reply must come back on). */
00183       struct port_comm* pc;
      /** timeout event */
00185       struct comm_timer* timer;
      /** callback for the timeout, error or reply to the message */
00187       comm_point_callback_t* cb;
      /** callback user argument */
00189       void* cb_arg;
      /** the outside network it is part of */
00191       struct outside_network* outnet;

      /*---- filled if udp pending is waiting -----*/
      /** next in waiting list. */
00195       struct pending* next_waiting;
      /** timeout in msec */
00197       int timeout;
      /** The query itself, the query packet to send. */
00199       uint8_t* pkt;
      /** length of query packet. */
00201       size_t pkt_len;
};

/**
 * Pending TCP query to server.
 */
00207 struct pending_tcp {
      /** next in list of free tcp comm points, or NULL. */
00209       struct pending_tcp* next_free;
      /** the ID for the query; checked in reply */
00211       uint16_t id;
      /** tcp comm point it was sent on (and reply must come back on). */
00213       struct comm_point* c;
      /** the query being serviced, NULL if the pending_tcp is unused. */
00215       struct waiting_tcp* query;
};

/**
 * Query waiting for TCP buffer.
 */
00221 struct waiting_tcp {
      /** 
       * next in waiting list.
       * if pkt==0, this points to the pending_tcp structure.
       */
00226       struct waiting_tcp* next_waiting;
      /** timeout event; timer keeps running whether the query is
       * waiting for a buffer or the tcp reply is pending */
00229       struct comm_timer* timer;
      /** the outside network it is part of */
00231       struct outside_network* outnet;
      /** remote address. */
00233       struct sockaddr_storage addr;
      /** length of addr field in use. */
00235       socklen_t addrlen;
      /** 
       * The query itself, the query packet to send.
       * allocated after the waiting_tcp structure.
       * set to NULL when the query is serviced and it part of pending_tcp.
       * if this is NULL, the next_waiting points to the pending_tcp.
       */
00242       uint8_t* pkt;
      /** length of query packet. */
00244       size_t pkt_len;
      /** callback for the timeout, error or reply to the message */
00246       comm_point_callback_t* cb;
      /** callback user argument */
00248       void* cb_arg;
};

/**
 * Callback to party interested in serviced query results.
 */
00254 struct service_callback {
      /** next in callback list */
00256       struct service_callback* next;
      /** callback function */
00258       comm_point_callback_t* cb;
      /** user argument for callback function */
00260       void* cb_arg;
};

/**
 * Query service record.
 * Contains query and destination. UDP, TCP, EDNS are all tried.
 * complete with retries and timeouts. A number of interested parties can
 * receive a callback.
 */
00269 struct serviced_query {
      /** The rbtree node, key is this record */
00271       rbnode_t node;
      /** The query that needs to be answered. Starts with flags u16,
       * then qdcount, ..., including qname, qtype, qclass. Does not include
       * EDNS record. */
00275       uint8_t* qbuf;
      /** length of qbuf. */
00277       size_t qbuflen;
      /** If an EDNS section is included, the DO/CD bit will be turned on. */
00279       int dnssec;
      /** where to send it */
00281       struct sockaddr_storage addr;
      /** length of addr field in use. */
00283       socklen_t addrlen;
      /** current status */
00285       enum serviced_query_status {
            /** initial status */
00287             serviced_initial,
            /** UDP with EDNS sent */
00289             serviced_query_UDP_EDNS,
            /** UDP without EDNS sent */
00291             serviced_query_UDP,
            /** TCP with EDNS sent */
00293             serviced_query_TCP_EDNS,
            /** TCP without EDNS sent */
00295             serviced_query_TCP
      }     
            /** variable with current status */ 
            status;
      /** true if serviced_query is scheduled for deletion already */
00300       int to_be_deleted;
      /** number of UDP retries */
00302       int retry;
      /** time last UDP was sent */
00304       struct timeval last_sent_time;
      /** outside network this is part of */
00306       struct outside_network* outnet;
      /** list of interested parties that need callback on results. */
00308       struct service_callback* cblist;
      /** the UDP or TCP query that is pending, see status which */
00310       void* pending;
};

/**
 * Create outside_network structure with N udp ports.
 * @param base: the communication base to use for event handling.
 * @param bufsize: size for network buffers.
 * @param num_ports: number of udp ports to open per interface.
 * @param ifs: interface names (or NULL for default interface).
 *    These interfaces must be able to access all authoritative servers.
 * @param num_ifs: number of names in array ifs.
 * @param do_ip4: service IP4.
 * @param do_ip6: service IP6.
 * @param num_tcp: number of outgoing tcp buffers to preallocate.
 * @param infra: pointer to infra cached used for serviced queries.
 * @param rnd: stored to create random numbers for serviced queries.
 * @param use_caps_for_id: enable to use 0x20 bits to encode id randomness.
 * @param availports: array of available ports. 
 * @param numavailports: number of available ports in array.
 * @return: the new structure (with no pending answers) or NULL on error.
 */
struct outside_network* outside_network_create(struct comm_base* base,
      size_t bufsize, size_t num_ports, char** ifs, int num_ifs,
      int do_ip4, int do_ip6, size_t num_tcp, struct infra_cache* infra, 
      struct ub_randstate* rnd, int use_caps_for_id, int* availports, 
      int numavailports);

/**
 * Delete outside_network structure.
 * @param outnet: object to delete.
 */
void outside_network_delete(struct outside_network* outnet);

/**
 * Send UDP query, create pending answer.
 * Changes the ID for the query to be random and unique for that destination.
 * @param outnet: provides the event handling
 * @param packet: wireformat query to send to destination.
 * @param addr: address to send to.
 * @param addrlen: length of addr.
 * @param timeout: in milliseconds from now.
 * @param callback: function to call on error, timeout or reply.
 * @param callback_arg: user argument for callback function.
 * @return: NULL on error for malloc or socket. Else the pending query object.
 */
struct pending* pending_udp_query(struct outside_network* outnet, 
      ldns_buffer* packet, struct sockaddr_storage* addr, 
      socklen_t addrlen, int timeout, comm_point_callback_t* callback, 
      void* callback_arg);

/**
 * Send TCP query. May wait for TCP buffer. Selects ID to be random, and 
 * checks id.
 * @param outnet: provides the event handling.
 * @param packet: wireformat query to send to destination. copied from.
 * @param addr: address to send to.
 * @param addrlen: length of addr.
 * @param timeout: in seconds from now.
 *    Timer starts running now. Timer may expire if all buffers are used,
 *    without any query been sent to the server yet.
 * @param callback: function to call on error, timeout or reply.
 * @param callback_arg: user argument for callback function.
 * @return: false on error for malloc or socket. Else the pending TCP object.
 */
struct waiting_tcp* pending_tcp_query(struct outside_network* outnet, 
      ldns_buffer* packet, struct sockaddr_storage* addr, 
      socklen_t addrlen, int timeout, comm_point_callback_t* callback, 
      void* callback_arg);

/**
 * Delete pending answer.
 * @param outnet: outside network the pending query is part of.
 *    Internal feature: if outnet is NULL, p is not unlinked from rbtree.
 * @param p: deleted
 */
void pending_delete(struct outside_network* outnet, struct pending* p);

/**
 * Perform a serviced query to the authoritative servers.
 * Duplicate efforts are detected, and EDNS, TCP and UDP retry is performed.
 * @param outnet: outside network, with rbtree of serviced queries.
 * @param qname: what qname to query.
 * @param qnamelen: length of qname in octets including 0 root label.
 * @param qtype: rrset type to query (host format)
 * @param qclass: query class. (host format)
 * @param flags: flags u16 (host format), includes opcode, CD bit.
 * @param dnssec: if set, DO bit is set in EDNS queries.
 *    If the value includes BIT_CD, CD bit is set when in EDNS queries.
 *    If the value includes BIT_DO, DO bit is set when in EDNS queries.
 * @param callback: callback function.
 * @param callback_arg: user argument to callback function.
 * @param addr: to which server to send the query.
 * @param addrlen: length of addr.
 * @param buff: scratch buffer to create query contents in. Empty on exit.
 * @param arg_compare: function to compare callback args, return true if 
 *    identical. It is given the callback_arg and args that are listed.
 * @return 0 on error, or pointer to serviced query that is used to answer
 *    this serviced query may be shared with other callbacks as well.
 */
struct serviced_query* outnet_serviced_query(struct outside_network* outnet,
      uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass,
      uint16_t flags, int dnssec, struct sockaddr_storage* addr, 
      socklen_t addrlen, comm_point_callback_t* callback, 
      void* callback_arg, ldns_buffer* buff, 
      int (*arg_compare)(void*,void*));

/**
 * Remove service query callback.
 * If that leads to zero callbacks, the query is completely cancelled.
 * @param sq: serviced query to adjust.
 * @param cb_arg: callback argument of callback that needs removal.
 *    same as the callback_arg to outnet_serviced_query().
 */
void outnet_serviced_query_stop(struct serviced_query* sq, void* cb_arg);

/**
 * Get memory size in use by outside network.
 * Counts buffers and outstanding query (serviced queries) malloced data.
 * @param outnet: outside network structure.
 * @return size in bytes.
 */
size_t outnet_get_mem(struct outside_network* outnet);

/**
 * Get memory size in use by serviced query while it is servicing callbacks.
 * This takes into account the pre-deleted status of it; it will be deleted
 * when the callbacks are done.
 * @param sq: serviced query. 
 * @return size in bytes.
 */
size_t serviced_get_mem(struct serviced_query* sq);

/** callback for incoming udp answers from the network */
int outnet_udp_cb(struct comm_point* c, void* arg, int error,
      struct comm_reply *reply_info);

/** callback for pending tcp connections */
int outnet_tcp_cb(struct comm_point* c, void* arg, int error,
      struct comm_reply *reply_info);

/** callback for udp timeout */
void pending_udp_timer_cb(void *arg);

/** callback for outgoing TCP timer event */
void outnet_tcptimer(void* arg);

/** callback for serviced query UDP answers */
int serviced_udp_callback(struct comm_point* c, void* arg, int error,
        struct comm_reply* rep);

/** TCP reply or error callback for serviced queries */
int serviced_tcp_callback(struct comm_point* c, void* arg, int error,
        struct comm_reply* rep);

/** compare function of pending rbtree */
int pending_cmp(const void* key1, const void* key2);

/** compare function of serviced query rbtree */
int serviced_cmp(const void* key1, const void* key2);

#endif /* OUTSIDE_NETWORK_H */

Generated by  Doxygen 1.6.0   Back to index