1 /* 2 * Copyright (c) 1985, 1990 Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)ns.h 4.33 (Berkeley) 8/23/90 34 */ 35 36 /* 37 * Global definitions and variables for the name server. 38 */ 39 40 #include <arpa/inet.h> 41 #include <string.h> 42 43 /* 44 * Timeout time should be around 1 minute or so. Using the 45 * the current simplistic backoff strategy, the sequence 46 * retrys after 4, 8, and 16 seconds. With 3 servers, this 47 * dies out in a little more than a minute. 48 * (sequence RETRYBASE, 2*RETRYBASE, 4*RETRYBASE... for MAXRETRY) 49 */ 50 #define MINROOTS 2 /* min number of root hints */ 51 #define NSMAX 16 /* max number of NS addrs to try */ 52 #define RETRYBASE 4 /* base time between retries */ 53 #define MAXRETRY 3 /* max number of retries per addr */ 54 #define MAXCNAMES 8 /* max # of CNAMES tried per addr */ 55 #define MAXQUERIES 20 /* max # of queries to be made */ 56 /* (prevent "recursive" loops) */ 57 #define INIT_REFRESH 600 /* retry time for initial secondary */ 58 /* contact (10 minutes) */ 59 60 #define ALPHA 0.7 /* How much to preserver of old response time */ 61 #define BETA 1.2 /* How much to penalize response time on failure */ 62 #define GAMMA 0.98 /* How much to decay unused response times */ 63 64 struct zoneinfo { 65 int z_type; /* type of zone */ 66 int z_auth; /* zone is authoritative */ 67 char *z_origin; /* root domain name of zone */ 68 time_t z_time; /* time for next refresh */ 69 time_t z_lastupdate; /* time of last refresh */ 70 u_long z_refresh; /* refresh interval */ 71 u_long z_retry; /* refresh retry interval */ 72 u_long z_expire; /* expiration time for cached info */ 73 u_long z_minimum; /* minimum TTL value */ 74 u_long z_serial; /* changes if zone modified */ 75 char *z_source; /* source location of data */ 76 time_t z_ftime; /* modification time of source file */ 77 int z_addrcnt; /* address count */ 78 struct in_addr z_addr[NSMAX]; /* list of master servers for zone */ 79 int z_state; /* state bits; see below */ 80 u_short z_xferpid; /* xfer child pid */ 81 #ifdef ALLOW_UPDATES 82 int hasChanged; /* non-zero if zone has been updated 83 * since last checkpoint 84 */ 85 #endif ALLOW_UPDATES 86 }; 87 88 /* zone types (z_type) */ 89 #define Z_PRIMARY 1 90 #define Z_SECONDARY 2 91 #define Z_CACHE 3 92 93 /* zone state bits */ 94 #define Z_AUTH 0x01 /* should replace z_auth */ 95 #define Z_NEED_XFER 0x02 /* waiting to do xfer */ 96 #define Z_XFER_RUNNING 0x04 /* asynch. xfer is running */ 97 #define Z_NEED_RELOAD 0x08 /* waiting to do reload */ 98 #define Z_SYSLOGGED 0x10 /* have logged timeout */ 99 #define Z_CHANGED 0x20 /* should replace hasChanged */ 100 #define Z_FOUND 0x40 /* found in boot file when reloading */ 101 #define Z_INCLUDE 0x80 /* set if include used in file */ 102 #define Z_DB_BAD 0x100 /* errors when loading file */ 103 #define Z_TMP_FILE 0x200 /* backup file for xfer is temporary */ 104 #ifdef ALLOW_UPDATES 105 #define Z_DYNAMIC 0x400 /* allow dynamic updates */ 106 #define Z_DYNADDONLY 0x800 /* dynamic mode: add new data only */ 107 #endif ALLOW_UPDATES 108 109 /* xfer exit codes */ 110 #define XFER_UPTODATE 0 /* zone is up-to-date */ 111 #define XFER_SUCCESS 1 /* performed transfer successfully */ 112 #define XFER_TIMEOUT 2 /* no server reachable/xfer timeout */ 113 #define XFER_FAIL 3 /* other failure, has been logged */ 114 115 /* 116 * Structure for recording info on forwarded queries. 117 */ 118 struct qinfo { 119 u_short q_id; /* id of query */ 120 u_short q_nsid; /* id of forwarded query */ 121 int q_dfd; /* UDP file descriptor */ 122 struct sockaddr_in q_from; /* requestor's address */ 123 char *q_msg; /* the message */ 124 int q_msglen; /* len of message */ 125 int q_naddr; /* number of addr's in q_addr */ 126 int q_curaddr; /* last addr sent to */ 127 struct fwdinfo *q_fwd; /* last forwarder used */ 128 time_t q_time; /* time to retry */ 129 struct qinfo *q_next; /* rexmit list (sorted by time) */ 130 struct qinfo *q_link; /* storage list (random order) */ 131 struct qserv { 132 struct sockaddr_in ns_addr; /* addresses of NS's */ 133 struct databuf *ns; /* databuf for NS record */ 134 struct databuf *nsdata; /* databuf for server address */ 135 struct timeval stime; /* time first query started */ 136 int nretry; /* # of times addr retried */ 137 } q_addr[NSMAX]; /* addresses of NS's */ 138 struct databuf *q_usedns[NSMAX]; /* databuf for NS that we've tried */ 139 int q_nusedns; 140 int q_cname; /* # of cnames found */ 141 int q_nqueries; /* # of queries required */ 142 char *q_cmsg; /* the cname message */ 143 int q_cmsglen; /* len of cname message */ 144 struct qstream *q_stream; /* TCP stream, null if UDP */ 145 int q_system; /* boolean, system query */ 146 }; 147 148 #define Q_NEXTADDR(qp,n) \ 149 (((qp)->q_fwd == (struct fwdinfo *)0) ? \ 150 &(qp)->q_addr[n].ns_addr : &(qp)->q_fwd->fwdaddr) 151 152 #define PRIMING_CACHE 42 153 #define QINFO_NULL ((struct qinfo *)0) 154 155 #ifndef XFER 156 extern struct qinfo *qfindid(); 157 extern struct qinfo *qnew(); 158 extern struct qinfo *retryqp; /* next query to retry */ 159 #endif /* XFER */ 160 161 /* 162 * Return codes from ns_forw: 163 */ 164 #define FW_OK 0 165 #define FW_DUP 1 166 #define FW_NOSERVER 2 167 #define FW_SERVFAIL 3 168 169 struct qstream { 170 int s_rfd; /* stream file descriptor */ 171 int s_size; /* expected amount of data to recive */ 172 int s_bufsize; /* amount of data recived in s_buf */ 173 char *s_buf; /* buffer of recived data */ 174 char *s_bufp; /* pointer into s_buf of recived data */ 175 struct qstream *s_next; /* next stream */ 176 struct sockaddr_in s_from; /* address query came from */ 177 u_long s_time; /* time stamp of last transaction */ 178 int s_refcnt; /* number of outstanding queries */ 179 u_short s_tempsize; /* temporary for size from net */ 180 }; 181 182 #define QSTREAM_NULL ((struct qstream *)0) 183 extern struct qstream *streamq; /* stream queue */ 184 185 struct qdatagram { 186 int dq_dfd; /* datagram file descriptor */ 187 struct qdatagram *dq_next; /* next datagram */ 188 struct in_addr dq_addr; /* address of interface */ 189 }; 190 191 #define QDATAGRAM_NULL ((struct qdatagram *)0) 192 extern struct qdatagram *datagramq; /* datagram queue */ 193 194 struct netinfo { 195 struct netinfo *next; 196 u_long net; 197 u_long mask; 198 struct in_addr my_addr; 199 }; 200 201 struct fwdinfo { 202 struct fwdinfo *next; 203 struct sockaddr_in fwdaddr; 204 }; 205 206 struct nets { 207 char *name; 208 long net; 209 struct nets *next; 210 }; 211 212 /* 213 * Statistics Defines 214 */ 215 struct stats { 216 unsigned long cnt; 217 char *description; 218 }; 219 220 /* gross count of UDP packets in and out */ 221 #define S_INPKTS 0 222 #define S_OUTPKTS 1 223 /* gross count of queries and inverse queries received */ 224 #define S_QUERIES 2 225 #define S_IQUERIES 3 226 #define S_DUPQUERIES 4 227 #define S_RESPONSES 5 228 #define S_DUPRESP 6 229 #define S_RESPOK 7 230 #define S_RESPFAIL 8 231 #define S_RESPFORMERR 9 232 #define S_SYSQUERIES 10 233 #define S_PRIMECACHE 11 234 #define S_CHECKNS 12 235 #define S_BADRESPONSES 13 236 #define S_MARTIANS 14 237 #define S_NSTATS 15 /* Careful! */ 238 #ifdef STATS 239 extern struct stats stats[S_NSTATS]; 240 extern unsigned long typestats[T_ANY+1]; 241 #endif 242 243 /* 244 * Attempt to configure for type of function returned by signal-catching 245 * functions (which signal and sigvec.sv_handler take a pointer to). 246 * This can guess for BSD; otherwise, define SIG_FN externally. 247 */ 248 #ifndef SIG_FN 249 #ifdef BSD 250 #if BSD >= 199006 251 #define SIG_FN void /* signal-catching functions return void */ 252 #else 253 #define SIG_FN int /* signal-catching functions return int */ 254 #endif 255 #else /* BSD */ 256 #define SIG_FN void /* signal-catching functions return void */ 257 #endif /* BSD */ 258 #endif 259 260 261 #ifdef DEBUG 262 extern int debug; /* debug flag */ 263 extern FILE *ddt; /* debug file discriptor */ 264 #endif 265 #ifndef XFER 266 extern int ds; /* datagram socket */ 267 extern struct qdatagram *dqp; 268 extern struct timeval tt; /* place to store time */ 269 270 extern struct itimerval ival; /* maintenance interval */ 271 extern struct zoneinfo *zones; /* zone information */ 272 extern int nzones; /* number of zones in use */ 273 274 extern int forward_only; /* true on slave server */ 275 #endif /* XFER */ 276 277 #ifdef vax 278 extern u_short htons(), ntohs(); 279 extern u_long htonl(), ntohl(); 280 #endif 281 282 #define MAX_XFER_TIME 60 * 60 * 2 /* max seconds for an xfer */ 283 #define XFER_TIME_FUDGE 10 /* MAX_XFER_TIME fudge */ 284 285 #ifndef XFER 286 extern int xfer_running_cnt; /* number of xfers running */ 287 extern int xfer_deferred_cnt; /* number of deferred xfers */ 288 #define MAX_XFERS_RUNNING 4 /* max value of xfer_running_cnt */ 289 #endif /* XFER */ 290